* tree-ssa.c (target_for_debug_bind, verify_phi_args,
[official-gcc.git] / gcc / dwarf2out.c
blob4a3df339df2c6a6816ac8b8dbdb2466a7492c592
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static GTY(()) section *debug_line_section;
164 static GTY(()) section *debug_skeleton_line_section;
165 static GTY(()) section *debug_loc_section;
166 static GTY(()) section *debug_pubnames_section;
167 static GTY(()) section *debug_pubtypes_section;
168 static GTY(()) section *debug_str_section;
169 static GTY(()) section *debug_str_dwo_section;
170 static GTY(()) section *debug_str_offsets_section;
171 static GTY(()) section *debug_ranges_section;
172 static GTY(()) section *debug_frame_section;
174 /* Maximum size (in bytes) of an artificially generated label. */
175 #define MAX_ARTIFICIAL_LABEL_BYTES 30
177 /* According to the (draft) DWARF 3 specification, the initial length
178 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
179 bytes are 0xffffffff, followed by the length stored in the next 8
180 bytes.
182 However, the SGI/MIPS ABI uses an initial length which is equal to
183 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
185 #ifndef DWARF_INITIAL_LENGTH_SIZE
186 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
187 #endif
189 /* Round SIZE up to the nearest BOUNDARY. */
190 #define DWARF_ROUND(SIZE,BOUNDARY) \
191 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
193 /* CIE identifier. */
194 #if HOST_BITS_PER_WIDE_INT >= 64
195 #define DWARF_CIE_ID \
196 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
197 #else
198 #define DWARF_CIE_ID DW_CIE_ID
199 #endif
202 /* A vector for a table that contains frame description
203 information for each routine. */
204 #define NOT_INDEXED (-1U)
205 #define NO_INDEX_ASSIGNED (-2U)
207 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
209 struct GTY((for_user)) indirect_string_node {
210 const char *str;
211 unsigned int refcount;
212 enum dwarf_form form;
213 char *label;
214 unsigned int index;
217 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
219 typedef const char *compare_type;
221 static hashval_t hash (indirect_string_node *);
222 static bool equal (indirect_string_node *, const char *);
225 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
227 /* With split_debug_info, both the comp_dir and dwo_name go in the
228 main object file, rather than the dwo, similar to the force_direct
229 parameter elsewhere but with additional complications:
231 1) The string is needed in both the main object file and the dwo.
232 That is, the comp_dir and dwo_name will appear in both places.
234 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
235 DW_FORM_GNU_str_index.
237 3) GCC chooses the form to use late, depending on the size and
238 reference count.
240 Rather than forcing the all debug string handling functions and
241 callers to deal with these complications, simply use a separate,
242 special-cased string table for any attribute that should go in the
243 main object file. This limits the complexity to just the places
244 that need it. */
246 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
248 static GTY(()) int dw2_string_counter;
250 /* True if the compilation unit places functions in more than one section. */
251 static GTY(()) bool have_multiple_function_sections = false;
253 /* Whether the default text and cold text sections have been used at all. */
255 static GTY(()) bool text_section_used = false;
256 static GTY(()) bool cold_text_section_used = false;
258 /* The default cold text section. */
259 static GTY(()) section *cold_text_section;
261 /* The DIE for C++14 'auto' in a function return type. */
262 static GTY(()) dw_die_ref auto_die;
264 /* The DIE for C++14 'decltype(auto)' in a function return type. */
265 static GTY(()) dw_die_ref decltype_auto_die;
267 /* Forward declarations for functions defined in this file. */
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. Some constants have a large uniform precision, so
372 we get the precision needed for the actual value of the number. */
374 static unsigned int
375 get_full_len (const wide_int &op)
377 int prec = wi::min_precision (op, UNSIGNED);
378 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379 / HOST_BITS_PER_WIDE_INT);
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 enum debug_struct_file criterion;
386 tree type_decl;
387 bool generic = lang_hooks.types.generic_p (type);
389 if (generic)
390 criterion = debug_struct_generic[usage];
391 else
392 criterion = debug_struct_ordinary[usage];
394 if (criterion == DINFO_STRUCT_FILE_NONE)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396 if (criterion == DINFO_STRUCT_FILE_ANY)
397 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401 if (type_decl != NULL)
403 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
414 switch to the data section instead, and write out a synthetic start label
415 for collect2 the first time around. */
417 static void
418 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
420 if (eh_frame_section == 0)
422 int flags;
424 if (EH_TABLES_CAN_BE_READ_ONLY)
426 int fde_encoding;
427 int per_encoding;
428 int lsda_encoding;
430 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
431 /*global=*/0);
432 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
433 /*global=*/1);
434 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
435 /*global=*/0);
436 flags = ((! flag_pic
437 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
438 && (fde_encoding & 0x70) != DW_EH_PE_aligned
439 && (per_encoding & 0x70) != DW_EH_PE_absptr
440 && (per_encoding & 0x70) != DW_EH_PE_aligned
441 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
442 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
443 ? 0 : SECTION_WRITE);
445 else
446 flags = SECTION_WRITE;
448 #ifdef EH_FRAME_SECTION_NAME
449 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
450 #else
451 eh_frame_section = ((flags == SECTION_WRITE)
452 ? data_section : readonly_data_section);
453 #endif /* EH_FRAME_SECTION_NAME */
456 switch_to_section (eh_frame_section);
458 #ifdef EH_FRAME_THROUGH_COLLECT2
459 /* We have no special eh_frame section. Emit special labels to guide
460 collect2. */
461 if (!back)
463 tree label = get_file_function_name ("F");
464 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
465 targetm.asm_out.globalize_label (asm_out_file,
466 IDENTIFIER_POINTER (label));
467 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
469 #endif
472 /* Switch [BACK] to the eh or debug frame table section, depending on
473 FOR_EH. */
475 static void
476 switch_to_frame_table_section (int for_eh, bool back)
478 if (for_eh)
479 switch_to_eh_frame_section (back);
480 else
482 if (!debug_frame_section)
483 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
484 SECTION_DEBUG, NULL);
485 switch_to_section (debug_frame_section);
489 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
491 enum dw_cfi_oprnd_type
492 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
494 switch (cfi)
496 case DW_CFA_nop:
497 case DW_CFA_GNU_window_save:
498 case DW_CFA_remember_state:
499 case DW_CFA_restore_state:
500 return dw_cfi_oprnd_unused;
502 case DW_CFA_set_loc:
503 case DW_CFA_advance_loc1:
504 case DW_CFA_advance_loc2:
505 case DW_CFA_advance_loc4:
506 case DW_CFA_MIPS_advance_loc8:
507 return dw_cfi_oprnd_addr;
509 case DW_CFA_offset:
510 case DW_CFA_offset_extended:
511 case DW_CFA_def_cfa:
512 case DW_CFA_offset_extended_sf:
513 case DW_CFA_def_cfa_sf:
514 case DW_CFA_restore:
515 case DW_CFA_restore_extended:
516 case DW_CFA_undefined:
517 case DW_CFA_same_value:
518 case DW_CFA_def_cfa_register:
519 case DW_CFA_register:
520 case DW_CFA_expression:
521 return dw_cfi_oprnd_reg_num;
523 case DW_CFA_def_cfa_offset:
524 case DW_CFA_GNU_args_size:
525 case DW_CFA_def_cfa_offset_sf:
526 return dw_cfi_oprnd_offset;
528 case DW_CFA_def_cfa_expression:
529 return dw_cfi_oprnd_loc;
531 default:
532 gcc_unreachable ();
536 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
538 enum dw_cfi_oprnd_type
539 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
541 switch (cfi)
543 case DW_CFA_def_cfa:
544 case DW_CFA_def_cfa_sf:
545 case DW_CFA_offset:
546 case DW_CFA_offset_extended_sf:
547 case DW_CFA_offset_extended:
548 return dw_cfi_oprnd_offset;
550 case DW_CFA_register:
551 return dw_cfi_oprnd_reg_num;
553 case DW_CFA_expression:
554 return dw_cfi_oprnd_loc;
556 default:
557 return dw_cfi_oprnd_unused;
561 /* Output one FDE. */
563 static void
564 output_fde (dw_fde_ref fde, bool for_eh, bool second,
565 char *section_start_label, int fde_encoding, char *augmentation,
566 bool any_lsda_needed, int lsda_encoding)
568 const char *begin, *end;
569 static unsigned int j;
570 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
572 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
573 /* empty */ 0);
574 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
575 for_eh + j);
576 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
577 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
578 if (!XCOFF_DEBUGGING_INFO || for_eh)
580 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
581 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
582 " indicating 64-bit DWARF extension");
583 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
584 "FDE Length");
586 ASM_OUTPUT_LABEL (asm_out_file, l1);
588 if (for_eh)
589 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
590 else
591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
592 debug_frame_section, "FDE CIE offset");
594 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
595 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
597 if (for_eh)
599 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
600 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
601 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
602 "FDE initial location");
603 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
604 end, begin, "FDE address range");
606 else
608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
612 if (augmentation[0])
614 if (any_lsda_needed)
616 int size = size_of_encoded_value (lsda_encoding);
618 if (lsda_encoding == DW_EH_PE_aligned)
620 int offset = ( 4 /* Length */
621 + 4 /* CIE offset */
622 + 2 * size_of_encoded_value (fde_encoding)
623 + 1 /* Augmentation size */ );
624 int pad = -offset & (PTR_SIZE - 1);
626 size += pad;
627 gcc_assert (size_of_uleb128 (size) == 1);
630 dw2_asm_output_data_uleb128 (size, "Augmentation size");
632 if (fde->uses_eh_lsda)
634 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
635 fde->funcdef_number);
636 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
637 gen_rtx_SYMBOL_REF (Pmode, l1),
638 false,
639 "Language Specific Data Area");
641 else
643 if (lsda_encoding == DW_EH_PE_aligned)
644 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
645 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
646 "Language Specific Data Area (none)");
649 else
650 dw2_asm_output_data_uleb128 (0, "Augmentation size");
653 /* Loop through the Call Frame Instructions associated with this FDE. */
654 fde->dw_fde_current_label = begin;
656 size_t from, until, i;
658 from = 0;
659 until = vec_safe_length (fde->dw_fde_cfi);
661 if (fde->dw_fde_second_begin == NULL)
663 else if (!second)
664 until = fde->dw_fde_switch_cfi_index;
665 else
666 from = fde->dw_fde_switch_cfi_index;
668 for (i = from; i < until; i++)
669 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
672 /* If we are to emit a ref/link from function bodies to their frame tables,
673 do it now. This is typically performed to make sure that tables
674 associated with functions are dragged with them and not discarded in
675 garbage collecting links. We need to do this on a per function basis to
676 cope with -ffunction-sections. */
678 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
679 /* Switch to the function section, emit the ref to the tables, and
680 switch *back* into the table section. */
681 switch_to_section (function_section (fde->decl));
682 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
683 switch_to_frame_table_section (for_eh, true);
684 #endif
686 /* Pad the FDE out to an address sized boundary. */
687 ASM_OUTPUT_ALIGN (asm_out_file,
688 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
689 ASM_OUTPUT_LABEL (asm_out_file, l2);
691 j += 2;
694 /* Return true if frame description entry FDE is needed for EH. */
696 static bool
697 fde_needed_for_eh_p (dw_fde_ref fde)
699 if (flag_asynchronous_unwind_tables)
700 return true;
702 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
703 return true;
705 if (fde->uses_eh_lsda)
706 return true;
708 /* If exceptions are enabled, we have collected nothrow info. */
709 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
710 return false;
712 return true;
715 /* Output the call frame information used to record information
716 that relates to calculating the frame pointer, and records the
717 location of saved registers. */
719 static void
720 output_call_frame_info (int for_eh)
722 unsigned int i;
723 dw_fde_ref fde;
724 dw_cfi_ref cfi;
725 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
726 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
727 bool any_lsda_needed = false;
728 char augmentation[6];
729 int augmentation_size;
730 int fde_encoding = DW_EH_PE_absptr;
731 int per_encoding = DW_EH_PE_absptr;
732 int lsda_encoding = DW_EH_PE_absptr;
733 int return_reg;
734 rtx personality = NULL;
735 int dw_cie_version;
737 /* Don't emit a CIE if there won't be any FDEs. */
738 if (!fde_vec)
739 return;
741 /* Nothing to do if the assembler's doing it all. */
742 if (dwarf2out_do_cfi_asm ())
743 return;
745 /* If we don't have any functions we'll want to unwind out of, don't emit
746 any EH unwind information. If we make FDEs linkonce, we may have to
747 emit an empty label for an FDE that wouldn't otherwise be emitted. We
748 want to avoid having an FDE kept around when the function it refers to
749 is discarded. Example where this matters: a primary function template
750 in C++ requires EH information, an explicit specialization doesn't. */
751 if (for_eh)
753 bool any_eh_needed = false;
755 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
757 if (fde->uses_eh_lsda)
758 any_eh_needed = any_lsda_needed = true;
759 else if (fde_needed_for_eh_p (fde))
760 any_eh_needed = true;
761 else if (TARGET_USES_WEAK_UNWIND_INFO)
762 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
765 if (!any_eh_needed)
766 return;
769 /* We're going to be generating comments, so turn on app. */
770 if (flag_debug_asm)
771 app_enable ();
773 /* Switch to the proper frame section, first time. */
774 switch_to_frame_table_section (for_eh, false);
776 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
777 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
779 /* Output the CIE. */
780 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
781 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
782 if (!XCOFF_DEBUGGING_INFO || for_eh)
784 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
785 dw2_asm_output_data (4, 0xffffffff,
786 "Initial length escape value indicating 64-bit DWARF extension");
787 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
788 "Length of Common Information Entry");
790 ASM_OUTPUT_LABEL (asm_out_file, l1);
792 /* Now that the CIE pointer is PC-relative for EH,
793 use 0 to identify the CIE. */
794 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
795 (for_eh ? 0 : DWARF_CIE_ID),
796 "CIE Identifier Tag");
798 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
799 use CIE version 1, unless that would produce incorrect results
800 due to overflowing the return register column. */
801 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
802 dw_cie_version = 1;
803 if (return_reg >= 256 || dwarf_version > 2)
804 dw_cie_version = 3;
805 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
807 augmentation[0] = 0;
808 augmentation_size = 0;
810 personality = current_unit_personality;
811 if (for_eh)
813 char *p;
815 /* Augmentation:
816 z Indicates that a uleb128 is present to size the
817 augmentation section.
818 L Indicates the encoding (and thus presence) of
819 an LSDA pointer in the FDE augmentation.
820 R Indicates a non-default pointer encoding for
821 FDE code pointers.
822 P Indicates the presence of an encoding + language
823 personality routine in the CIE augmentation. */
825 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
826 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
827 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
829 p = augmentation + 1;
830 if (personality)
832 *p++ = 'P';
833 augmentation_size += 1 + size_of_encoded_value (per_encoding);
834 assemble_external_libcall (personality);
836 if (any_lsda_needed)
838 *p++ = 'L';
839 augmentation_size += 1;
841 if (fde_encoding != DW_EH_PE_absptr)
843 *p++ = 'R';
844 augmentation_size += 1;
846 if (p > augmentation + 1)
848 augmentation[0] = 'z';
849 *p = '\0';
852 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
853 if (personality && per_encoding == DW_EH_PE_aligned)
855 int offset = ( 4 /* Length */
856 + 4 /* CIE Id */
857 + 1 /* CIE version */
858 + strlen (augmentation) + 1 /* Augmentation */
859 + size_of_uleb128 (1) /* Code alignment */
860 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
861 + 1 /* RA column */
862 + 1 /* Augmentation size */
863 + 1 /* Personality encoding */ );
864 int pad = -offset & (PTR_SIZE - 1);
866 augmentation_size += pad;
868 /* Augmentations should be small, so there's scarce need to
869 iterate for a solution. Die if we exceed one uleb128 byte. */
870 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
874 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
875 if (dw_cie_version >= 4)
877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
878 dw2_asm_output_data (1, 0, "CIE Segment Size");
880 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
881 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
882 "CIE Data Alignment Factor");
884 if (dw_cie_version == 1)
885 dw2_asm_output_data (1, return_reg, "CIE RA Column");
886 else
887 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
889 if (augmentation[0])
891 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
892 if (personality)
894 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
895 eh_data_format_name (per_encoding));
896 dw2_asm_output_encoded_addr_rtx (per_encoding,
897 personality,
898 true, NULL);
901 if (any_lsda_needed)
902 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
903 eh_data_format_name (lsda_encoding));
905 if (fde_encoding != DW_EH_PE_absptr)
906 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
907 eh_data_format_name (fde_encoding));
910 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
911 output_cfi (cfi, NULL, for_eh);
913 /* Pad the CIE out to an address sized boundary. */
914 ASM_OUTPUT_ALIGN (asm_out_file,
915 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
916 ASM_OUTPUT_LABEL (asm_out_file, l2);
918 /* Loop through all of the FDE's. */
919 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
921 unsigned int k;
923 /* Don't emit EH unwind info for leaf functions that don't need it. */
924 if (for_eh && !fde_needed_for_eh_p (fde))
925 continue;
927 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
928 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
929 augmentation, any_lsda_needed, lsda_encoding);
932 if (for_eh && targetm.terminate_dw2_eh_frame_info)
933 dw2_asm_output_data (4, 0, "End of Table");
935 /* Turn off app to make assembly quicker. */
936 if (flag_debug_asm)
937 app_disable ();
940 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
942 static void
943 dwarf2out_do_cfi_startproc (bool second)
945 int enc;
946 rtx ref;
947 rtx personality = get_personality_function (current_function_decl);
949 fprintf (asm_out_file, "\t.cfi_startproc\n");
951 if (personality)
953 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
954 ref = personality;
956 /* ??? The GAS support isn't entirely consistent. We have to
957 handle indirect support ourselves, but PC-relative is done
958 in the assembler. Further, the assembler can't handle any
959 of the weirder relocation types. */
960 if (enc & DW_EH_PE_indirect)
961 ref = dw2_force_const_mem (ref, true);
963 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
964 output_addr_const (asm_out_file, ref);
965 fputc ('\n', asm_out_file);
968 if (crtl->uses_eh_lsda)
970 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
973 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
974 current_function_funcdef_no);
975 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
976 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
981 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
987 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
988 this allocation may be done before pass_final. */
990 dw_fde_ref
991 dwarf2out_alloc_current_fde (void)
993 dw_fde_ref fde;
995 fde = ggc_cleared_alloc<dw_fde_node> ();
996 fde->decl = current_function_decl;
997 fde->funcdef_number = current_function_funcdef_no;
998 fde->fde_index = vec_safe_length (fde_vec);
999 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1000 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1001 fde->nothrow = crtl->nothrow;
1002 fde->drap_reg = INVALID_REGNUM;
1003 fde->vdrap_reg = INVALID_REGNUM;
1005 /* Record the FDE associated with this function. */
1006 cfun->fde = fde;
1007 vec_safe_push (fde_vec, fde);
1009 return fde;
1012 /* Output a marker (i.e. a label) for the beginning of a function, before
1013 the prologue. */
1015 void
1016 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1017 const char *file ATTRIBUTE_UNUSED)
1019 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1020 char * dup_label;
1021 dw_fde_ref fde;
1022 section *fnsec;
1023 bool do_frame;
1025 current_function_func_begin_label = NULL;
1027 do_frame = dwarf2out_do_frame ();
1029 /* ??? current_function_func_begin_label is also used by except.c for
1030 call-site information. We must emit this label if it might be used. */
1031 if (!do_frame
1032 && (!flag_exceptions
1033 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1034 return;
1036 fnsec = function_section (current_function_decl);
1037 switch_to_section (fnsec);
1038 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1039 current_function_funcdef_no);
1040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 dup_label = xstrdup (label);
1043 current_function_func_begin_label = dup_label;
1045 /* We can elide the fde allocation if we're not emitting debug info. */
1046 if (!do_frame)
1047 return;
1049 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1050 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1051 would include pass_dwarf2_frame. If we've not created the FDE yet,
1052 do so now. */
1053 fde = cfun->fde;
1054 if (fde == NULL)
1055 fde = dwarf2out_alloc_current_fde ();
1057 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1058 fde->dw_fde_begin = dup_label;
1059 fde->dw_fde_current_label = dup_label;
1060 fde->in_std_section = (fnsec == text_section
1061 || (cold_text_section && fnsec == cold_text_section));
1063 /* We only want to output line number information for the genuine dwarf2
1064 prologue case, not the eh frame case. */
1065 #ifdef DWARF2_DEBUGGING_INFO
1066 if (file)
1067 dwarf2out_source_line (line, file, 0, true);
1068 #endif
1070 if (dwarf2out_do_cfi_asm ())
1071 dwarf2out_do_cfi_startproc (false);
1072 else
1074 rtx personality = get_personality_function (current_function_decl);
1075 if (!current_unit_personality)
1076 current_unit_personality = personality;
1078 /* We cannot keep a current personality per function as without CFI
1079 asm, at the point where we emit the CFI data, there is no current
1080 function anymore. */
1081 if (personality && current_unit_personality != personality)
1082 sorry ("multiple EH personalities are supported only with assemblers "
1083 "supporting .cfi_personality directive");
1087 /* Output a marker (i.e. a label) for the end of the generated code
1088 for a function prologue. This gets called *after* the prologue code has
1089 been generated. */
1091 void
1092 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1093 const char *file ATTRIBUTE_UNUSED)
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1106 /* Output a marker (i.e. a label) for the beginning of the generated code
1107 for a function epilogue. This gets called *before* the prologue code has
1108 been generated. */
1110 void
1111 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1114 dw_fde_ref fde = cfun->fde;
1115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1117 if (fde->dw_fde_vms_begin_epilogue)
1118 return;
1120 /* Output a label to mark the endpoint of the code generated for this
1121 function. */
1122 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1123 current_function_funcdef_no);
1124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1129 /* Output a marker (i.e. a label) for the absolute end of the generated code
1130 for a function definition. This gets called *after* the epilogue code has
1131 been generated. */
1133 void
1134 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1135 const char *file ATTRIBUTE_UNUSED)
1137 dw_fde_ref fde;
1138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1140 last_var_location_insn = NULL;
1141 cached_next_real_insn = NULL;
1143 if (dwarf2out_do_cfi_asm ())
1144 fprintf (asm_out_file, "\t.cfi_endproc\n");
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_LABEL (asm_out_file, label);
1151 fde = cfun->fde;
1152 gcc_assert (fde != NULL);
1153 if (fde->dw_fde_second_begin == NULL)
1154 fde->dw_fde_end = xstrdup (label);
1157 void
1158 dwarf2out_frame_finish (void)
1160 /* Output call frame information. */
1161 if (targetm.debug_unwind_info () == UI_DWARF2)
1162 output_call_frame_info (0);
1164 /* Output another copy for the unwinder. */
1165 if ((flag_unwind_tables || flag_exceptions)
1166 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1167 output_call_frame_info (1);
1170 /* Note that the current function section is being used for code. */
1172 static void
1173 dwarf2out_note_section_used (void)
1175 section *sec = current_function_section ();
1176 if (sec == text_section)
1177 text_section_used = true;
1178 else if (sec == cold_text_section)
1179 cold_text_section_used = true;
1182 static void var_location_switch_text_section (void);
1183 static void set_cur_line_info_table (section *);
1185 void
1186 dwarf2out_switch_text_section (void)
1188 section *sect;
1189 dw_fde_ref fde = cfun->fde;
1191 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1193 if (!in_cold_section_p)
1195 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1196 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1197 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1199 else
1201 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1202 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1203 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1205 have_multiple_function_sections = true;
1207 /* There is no need to mark used sections when not debugging. */
1208 if (cold_text_section != NULL)
1209 dwarf2out_note_section_used ();
1211 if (dwarf2out_do_cfi_asm ())
1212 fprintf (asm_out_file, "\t.cfi_endproc\n");
1214 /* Now do the real section switch. */
1215 sect = current_function_section ();
1216 switch_to_section (sect);
1218 fde->second_in_std_section
1219 = (sect == text_section
1220 || (cold_text_section && sect == cold_text_section));
1222 if (dwarf2out_do_cfi_asm ())
1223 dwarf2out_do_cfi_startproc (true);
1225 var_location_switch_text_section ();
1227 if (cold_text_section != NULL)
1228 set_cur_line_info_table (sect);
1231 /* And now, the subset of the debugging information support code necessary
1232 for emitting location expressions. */
1234 /* Data about a single source file. */
1235 struct GTY((for_user)) dwarf_file_data {
1236 const char * filename;
1237 int emitted_number;
1240 /* Describe an entry into the .debug_addr section. */
1242 enum ate_kind {
1243 ate_kind_rtx,
1244 ate_kind_rtx_dtprel,
1245 ate_kind_label
1248 struct GTY((for_user)) addr_table_entry {
1249 enum ate_kind kind;
1250 unsigned int refcount;
1251 unsigned int index;
1252 union addr_table_entry_struct_union
1254 rtx GTY ((tag ("0"))) rtl;
1255 char * GTY ((tag ("1"))) label;
1257 GTY ((desc ("%1.kind"))) addr;
1260 /* Location lists are ranges + location descriptions for that range,
1261 so you can track variables that are in different places over
1262 their entire life. */
1263 typedef struct GTY(()) dw_loc_list_struct {
1264 dw_loc_list_ref dw_loc_next;
1265 const char *begin; /* Label and addr_entry for start of range */
1266 addr_table_entry *begin_entry;
1267 const char *end; /* Label for end of range */
1268 char *ll_symbol; /* Label for beginning of location list.
1269 Only on head of list */
1270 const char *section; /* Section this loclist is relative to */
1271 dw_loc_descr_ref expr;
1272 hashval_t hash;
1273 /* True if all addresses in this and subsequent lists are known to be
1274 resolved. */
1275 bool resolved_addr;
1276 /* True if this list has been replaced by dw_loc_next. */
1277 bool replaced;
1278 bool emitted;
1279 /* True if the range should be emitted even if begin and end
1280 are the same. */
1281 bool force;
1282 } dw_loc_list_node;
1284 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1285 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1287 /* Convert a DWARF stack opcode into its string name. */
1289 static const char *
1290 dwarf_stack_op_name (unsigned int op)
1292 const char *name = get_DW_OP_name (op);
1294 if (name != NULL)
1295 return name;
1297 return "OP_<unknown>";
1300 /* Return a pointer to a newly allocated location description. Location
1301 descriptions are simple expression terms that can be strung
1302 together to form more complicated location (address) descriptions. */
1304 static inline dw_loc_descr_ref
1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 unsigned HOST_WIDE_INT oprnd2)
1308 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1310 descr->dw_loc_opc = op;
1311 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd1.val_entry = NULL;
1313 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315 descr->dw_loc_oprnd2.val_entry = NULL;
1316 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1318 return descr;
1321 /* Return a pointer to a newly allocated location description for
1322 REG and OFFSET. */
1324 static inline dw_loc_descr_ref
1325 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1327 if (reg <= 31)
1328 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 offset, 0);
1330 else
1331 return new_loc_descr (DW_OP_bregx, reg, offset);
1334 /* Add a location description term to a location description expression. */
1336 static inline void
1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1339 dw_loc_descr_ref *d;
1341 /* Find the end of the chain. */
1342 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1345 *d = descr;
1348 /* Compare two location operands for exact equality. */
1350 static bool
1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1353 if (a->val_class != b->val_class)
1354 return false;
1355 switch (a->val_class)
1357 case dw_val_class_none:
1358 return true;
1359 case dw_val_class_addr:
1360 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1362 case dw_val_class_offset:
1363 case dw_val_class_unsigned_const:
1364 case dw_val_class_const:
1365 case dw_val_class_range_list:
1366 case dw_val_class_lineptr:
1367 case dw_val_class_macptr:
1368 /* These are all HOST_WIDE_INT, signed or unsigned. */
1369 return a->v.val_unsigned == b->v.val_unsigned;
1371 case dw_val_class_loc:
1372 return a->v.val_loc == b->v.val_loc;
1373 case dw_val_class_loc_list:
1374 return a->v.val_loc_list == b->v.val_loc_list;
1375 case dw_val_class_die_ref:
1376 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377 case dw_val_class_fde_ref:
1378 return a->v.val_fde_index == b->v.val_fde_index;
1379 case dw_val_class_lbl_id:
1380 case dw_val_class_high_pc:
1381 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382 case dw_val_class_str:
1383 return a->v.val_str == b->v.val_str;
1384 case dw_val_class_flag:
1385 return a->v.val_flag == b->v.val_flag;
1386 case dw_val_class_file:
1387 return a->v.val_file == b->v.val_file;
1388 case dw_val_class_decl_ref:
1389 return a->v.val_decl_ref == b->v.val_decl_ref;
1391 case dw_val_class_const_double:
1392 return (a->v.val_double.high == b->v.val_double.high
1393 && a->v.val_double.low == b->v.val_double.low);
1395 case dw_val_class_wide_int:
1396 return *a->v.val_wide == *b->v.val_wide;
1398 case dw_val_class_vec:
1400 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1401 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1403 return (a_len == b_len
1404 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1407 case dw_val_class_data8:
1408 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1410 case dw_val_class_vms_delta:
1411 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1412 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1414 case dw_val_class_discr_value:
1415 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1416 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1417 case dw_val_class_discr_list:
1418 /* It makes no sense comparing two discriminant value lists. */
1419 return false;
1421 gcc_unreachable ();
1424 /* Compare two location atoms for exact equality. */
1426 static bool
1427 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1429 if (a->dw_loc_opc != b->dw_loc_opc)
1430 return false;
1432 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1433 address size, but since we always allocate cleared storage it
1434 should be zero for other types of locations. */
1435 if (a->dtprel != b->dtprel)
1436 return false;
1438 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1439 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1442 /* Compare two complete location expressions for exact equality. */
1444 bool
1445 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1447 while (1)
1449 if (a == b)
1450 return true;
1451 if (a == NULL || b == NULL)
1452 return false;
1453 if (!loc_descr_equal_p_1 (a, b))
1454 return false;
1456 a = a->dw_loc_next;
1457 b = b->dw_loc_next;
1462 /* Add a constant OFFSET to a location expression. */
1464 static void
1465 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1467 dw_loc_descr_ref loc;
1468 HOST_WIDE_INT *p;
1470 gcc_assert (*list_head != NULL);
1472 if (!offset)
1473 return;
1475 /* Find the end of the chain. */
1476 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1479 p = NULL;
1480 if (loc->dw_loc_opc == DW_OP_fbreg
1481 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1482 p = &loc->dw_loc_oprnd1.v.val_int;
1483 else if (loc->dw_loc_opc == DW_OP_bregx)
1484 p = &loc->dw_loc_oprnd2.v.val_int;
1486 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1487 offset. Don't optimize if an signed integer overflow would happen. */
1488 if (p != NULL
1489 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1490 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1491 *p += offset;
1493 else if (offset > 0)
1494 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1496 else
1498 loc->dw_loc_next = int_loc_descriptor (-offset);
1499 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1503 /* Add a constant OFFSET to a location list. */
1505 static void
1506 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1508 dw_loc_list_ref d;
1509 for (d = list_head; d != NULL; d = d->dw_loc_next)
1510 loc_descr_plus_const (&d->expr, offset);
1513 #define DWARF_REF_SIZE \
1514 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1516 static unsigned long int get_base_type_offset (dw_die_ref);
1518 /* Return the size of a location descriptor. */
1520 static unsigned long
1521 size_of_loc_descr (dw_loc_descr_ref loc)
1523 unsigned long size = 1;
1525 switch (loc->dw_loc_opc)
1527 case DW_OP_addr:
1528 size += DWARF2_ADDR_SIZE;
1529 break;
1530 case DW_OP_GNU_addr_index:
1531 case DW_OP_GNU_const_index:
1532 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1533 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1534 break;
1535 case DW_OP_const1u:
1536 case DW_OP_const1s:
1537 size += 1;
1538 break;
1539 case DW_OP_const2u:
1540 case DW_OP_const2s:
1541 size += 2;
1542 break;
1543 case DW_OP_const4u:
1544 case DW_OP_const4s:
1545 size += 4;
1546 break;
1547 case DW_OP_const8u:
1548 case DW_OP_const8s:
1549 size += 8;
1550 break;
1551 case DW_OP_constu:
1552 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1553 break;
1554 case DW_OP_consts:
1555 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1556 break;
1557 case DW_OP_pick:
1558 size += 1;
1559 break;
1560 case DW_OP_plus_uconst:
1561 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1562 break;
1563 case DW_OP_skip:
1564 case DW_OP_bra:
1565 size += 2;
1566 break;
1567 case DW_OP_breg0:
1568 case DW_OP_breg1:
1569 case DW_OP_breg2:
1570 case DW_OP_breg3:
1571 case DW_OP_breg4:
1572 case DW_OP_breg5:
1573 case DW_OP_breg6:
1574 case DW_OP_breg7:
1575 case DW_OP_breg8:
1576 case DW_OP_breg9:
1577 case DW_OP_breg10:
1578 case DW_OP_breg11:
1579 case DW_OP_breg12:
1580 case DW_OP_breg13:
1581 case DW_OP_breg14:
1582 case DW_OP_breg15:
1583 case DW_OP_breg16:
1584 case DW_OP_breg17:
1585 case DW_OP_breg18:
1586 case DW_OP_breg19:
1587 case DW_OP_breg20:
1588 case DW_OP_breg21:
1589 case DW_OP_breg22:
1590 case DW_OP_breg23:
1591 case DW_OP_breg24:
1592 case DW_OP_breg25:
1593 case DW_OP_breg26:
1594 case DW_OP_breg27:
1595 case DW_OP_breg28:
1596 case DW_OP_breg29:
1597 case DW_OP_breg30:
1598 case DW_OP_breg31:
1599 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1600 break;
1601 case DW_OP_regx:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 break;
1604 case DW_OP_fbreg:
1605 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1606 break;
1607 case DW_OP_bregx:
1608 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1609 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1610 break;
1611 case DW_OP_piece:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_bit_piece:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1617 break;
1618 case DW_OP_deref_size:
1619 case DW_OP_xderef_size:
1620 size += 1;
1621 break;
1622 case DW_OP_call2:
1623 size += 2;
1624 break;
1625 case DW_OP_call4:
1626 size += 4;
1627 break;
1628 case DW_OP_call_ref:
1629 size += DWARF_REF_SIZE;
1630 break;
1631 case DW_OP_implicit_value:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1633 + loc->dw_loc_oprnd1.v.val_unsigned;
1634 break;
1635 case DW_OP_GNU_implicit_pointer:
1636 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1637 break;
1638 case DW_OP_GNU_entry_value:
1640 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1641 size += size_of_uleb128 (op_size) + op_size;
1642 break;
1644 case DW_OP_GNU_const_type:
1646 unsigned long o
1647 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1648 size += size_of_uleb128 (o) + 1;
1649 switch (loc->dw_loc_oprnd2.val_class)
1651 case dw_val_class_vec:
1652 size += loc->dw_loc_oprnd2.v.val_vec.length
1653 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1654 break;
1655 case dw_val_class_const:
1656 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1657 break;
1658 case dw_val_class_const_double:
1659 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1660 break;
1661 case dw_val_class_wide_int:
1662 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1663 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1664 break;
1665 default:
1666 gcc_unreachable ();
1668 break;
1670 case DW_OP_GNU_regval_type:
1672 unsigned long o
1673 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1674 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1675 + size_of_uleb128 (o);
1677 break;
1678 case DW_OP_GNU_deref_type:
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1682 size += 1 + size_of_uleb128 (o);
1684 break;
1685 case DW_OP_GNU_convert:
1686 case DW_OP_GNU_reinterpret:
1687 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1688 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1689 else
1691 unsigned long o
1692 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1693 size += size_of_uleb128 (o);
1695 break;
1696 case DW_OP_GNU_parameter_ref:
1697 size += 4;
1698 break;
1699 default:
1700 break;
1703 return size;
1706 /* Return the size of a series of location descriptors. */
1708 unsigned long
1709 size_of_locs (dw_loc_descr_ref loc)
1711 dw_loc_descr_ref l;
1712 unsigned long size;
1714 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1715 field, to avoid writing to a PCH file. */
1716 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1718 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1719 break;
1720 size += size_of_loc_descr (l);
1722 if (! l)
1723 return size;
1725 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1727 l->dw_loc_addr = size;
1728 size += size_of_loc_descr (l);
1731 return size;
1734 /* Return the size of the value in a DW_AT_discr_value attribute. */
1736 static int
1737 size_of_discr_value (dw_discr_value *discr_value)
1739 if (discr_value->pos)
1740 return size_of_uleb128 (discr_value->v.uval);
1741 else
1742 return size_of_sleb128 (discr_value->v.sval);
1745 /* Return the size of the value in a DW_discr_list attribute. */
1747 static int
1748 size_of_discr_list (dw_discr_list_ref discr_list)
1750 int size = 0;
1752 for (dw_discr_list_ref list = discr_list;
1753 list != NULL;
1754 list = list->dw_discr_next)
1756 /* One byte for the discriminant value descriptor, and then one or two
1757 LEB128 numbers, depending on whether it's a single case label or a
1758 range label. */
1759 size += 1;
1760 size += size_of_discr_value (&list->dw_discr_lower_bound);
1761 if (list->dw_discr_range != 0)
1762 size += size_of_discr_value (&list->dw_discr_upper_bound);
1764 return size;
1767 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1768 static void get_ref_die_offset_label (char *, dw_die_ref);
1769 static unsigned long int get_ref_die_offset (dw_die_ref);
1771 /* Output location description stack opcode's operands (if any).
1772 The for_eh_or_skip parameter controls whether register numbers are
1773 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1774 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1775 info). This should be suppressed for the cases that have not been converted
1776 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1778 static void
1779 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1782 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784 switch (loc->dw_loc_opc)
1786 #ifdef DWARF2_DEBUGGING_INFO
1787 case DW_OP_const2u:
1788 case DW_OP_const2s:
1789 dw2_asm_output_data (2, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const4u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const4s:
1802 dw2_asm_output_data (4, val1->v.val_int, NULL);
1803 break;
1804 case DW_OP_const8u:
1805 if (loc->dtprel)
1807 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1808 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1809 val1->v.val_addr);
1810 fputc ('\n', asm_out_file);
1811 break;
1813 /* FALLTHRU */
1814 case DW_OP_const8s:
1815 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1816 dw2_asm_output_data (8, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_skip:
1819 case DW_OP_bra:
1821 int offset;
1823 gcc_assert (val1->val_class == dw_val_class_loc);
1824 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826 dw2_asm_output_data (2, offset, NULL);
1828 break;
1829 case DW_OP_implicit_value:
1830 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1831 switch (val2->val_class)
1833 case dw_val_class_const:
1834 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1835 break;
1836 case dw_val_class_vec:
1838 unsigned int elt_size = val2->v.val_vec.elt_size;
1839 unsigned int len = val2->v.val_vec.length;
1840 unsigned int i;
1841 unsigned char *p;
1843 if (elt_size > sizeof (HOST_WIDE_INT))
1845 elt_size /= 2;
1846 len *= 2;
1848 for (i = 0, p = val2->v.val_vec.array;
1849 i < len;
1850 i++, p += elt_size)
1851 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1852 "fp or vector constant word %u", i);
1854 break;
1855 case dw_val_class_const_double:
1857 unsigned HOST_WIDE_INT first, second;
1859 if (WORDS_BIG_ENDIAN)
1861 first = val2->v.val_double.high;
1862 second = val2->v.val_double.low;
1864 else
1866 first = val2->v.val_double.low;
1867 second = val2->v.val_double.high;
1869 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1870 first, NULL);
1871 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1872 second, NULL);
1874 break;
1875 case dw_val_class_wide_int:
1877 int i;
1878 int len = get_full_len (*val2->v.val_wide);
1879 if (WORDS_BIG_ENDIAN)
1880 for (i = len - 1; i >= 0; --i)
1881 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1882 val2->v.val_wide->elt (i), NULL);
1883 else
1884 for (i = 0; i < len; ++i)
1885 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1886 val2->v.val_wide->elt (i), NULL);
1888 break;
1889 case dw_val_class_addr:
1890 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1891 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1892 break;
1893 default:
1894 gcc_unreachable ();
1896 break;
1897 #else
1898 case DW_OP_const2u:
1899 case DW_OP_const2s:
1900 case DW_OP_const4u:
1901 case DW_OP_const4s:
1902 case DW_OP_const8u:
1903 case DW_OP_const8s:
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 case DW_OP_implicit_value:
1907 /* We currently don't make any attempt to make sure these are
1908 aligned properly like we do for the main unwind info, so
1909 don't support emitting things larger than a byte if we're
1910 only doing unwinding. */
1911 gcc_unreachable ();
1912 #endif
1913 case DW_OP_const1u:
1914 case DW_OP_const1s:
1915 dw2_asm_output_data (1, val1->v.val_int, NULL);
1916 break;
1917 case DW_OP_constu:
1918 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1919 break;
1920 case DW_OP_consts:
1921 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1922 break;
1923 case DW_OP_pick:
1924 dw2_asm_output_data (1, val1->v.val_int, NULL);
1925 break;
1926 case DW_OP_plus_uconst:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_breg0:
1930 case DW_OP_breg1:
1931 case DW_OP_breg2:
1932 case DW_OP_breg3:
1933 case DW_OP_breg4:
1934 case DW_OP_breg5:
1935 case DW_OP_breg6:
1936 case DW_OP_breg7:
1937 case DW_OP_breg8:
1938 case DW_OP_breg9:
1939 case DW_OP_breg10:
1940 case DW_OP_breg11:
1941 case DW_OP_breg12:
1942 case DW_OP_breg13:
1943 case DW_OP_breg14:
1944 case DW_OP_breg15:
1945 case DW_OP_breg16:
1946 case DW_OP_breg17:
1947 case DW_OP_breg18:
1948 case DW_OP_breg19:
1949 case DW_OP_breg20:
1950 case DW_OP_breg21:
1951 case DW_OP_breg22:
1952 case DW_OP_breg23:
1953 case DW_OP_breg24:
1954 case DW_OP_breg25:
1955 case DW_OP_breg26:
1956 case DW_OP_breg27:
1957 case DW_OP_breg28:
1958 case DW_OP_breg29:
1959 case DW_OP_breg30:
1960 case DW_OP_breg31:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_regx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1972 break;
1973 case DW_OP_fbreg:
1974 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1975 break;
1976 case DW_OP_bregx:
1978 unsigned r = val1->v.val_unsigned;
1979 if (for_eh_or_skip >= 0)
1980 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1981 gcc_assert (size_of_uleb128 (r)
1982 == size_of_uleb128 (val1->v.val_unsigned));
1983 dw2_asm_output_data_uleb128 (r, NULL);
1984 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986 break;
1987 case DW_OP_piece:
1988 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1989 break;
1990 case DW_OP_bit_piece:
1991 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1993 break;
1994 case DW_OP_deref_size:
1995 case DW_OP_xderef_size:
1996 dw2_asm_output_data (1, val1->v.val_int, NULL);
1997 break;
1999 case DW_OP_addr:
2000 if (loc->dtprel)
2002 if (targetm.asm_out.output_dwarf_dtprel)
2004 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2005 DWARF2_ADDR_SIZE,
2006 val1->v.val_addr);
2007 fputc ('\n', asm_out_file);
2009 else
2010 gcc_unreachable ();
2012 else
2014 #ifdef DWARF2_DEBUGGING_INFO
2015 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2016 #else
2017 gcc_unreachable ();
2018 #endif
2020 break;
2022 case DW_OP_GNU_addr_index:
2023 case DW_OP_GNU_const_index:
2024 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2025 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2026 "(index into .debug_addr)");
2027 break;
2029 case DW_OP_call2:
2030 case DW_OP_call4:
2032 unsigned long die_offset
2033 = get_ref_die_offset (val1->v.val_die_ref.die);
2034 /* Make sure the offset has been computed and that we can encode it as
2035 an operand. */
2036 gcc_assert (die_offset > 0
2037 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2038 ? 0xffff
2039 : 0xffffffff));
2040 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2041 die_offset, NULL);
2043 break;
2045 case DW_OP_GNU_implicit_pointer:
2047 char label[MAX_ARTIFICIAL_LABEL_BYTES
2048 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2049 gcc_assert (val1->val_class == dw_val_class_die_ref);
2050 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2051 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2052 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2054 break;
2056 case DW_OP_GNU_entry_value:
2057 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2058 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2059 break;
2061 case DW_OP_GNU_const_type:
2063 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2064 gcc_assert (o);
2065 dw2_asm_output_data_uleb128 (o, NULL);
2066 switch (val2->val_class)
2068 case dw_val_class_const:
2069 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, l, NULL);
2071 dw2_asm_output_data (l, val2->v.val_int, NULL);
2072 break;
2073 case dw_val_class_vec:
2075 unsigned int elt_size = val2->v.val_vec.elt_size;
2076 unsigned int len = val2->v.val_vec.length;
2077 unsigned int i;
2078 unsigned char *p;
2080 l = len * elt_size;
2081 dw2_asm_output_data (1, l, NULL);
2082 if (elt_size > sizeof (HOST_WIDE_INT))
2084 elt_size /= 2;
2085 len *= 2;
2087 for (i = 0, p = val2->v.val_vec.array;
2088 i < len;
2089 i++, p += elt_size)
2090 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2091 "fp or vector constant word %u", i);
2093 break;
2094 case dw_val_class_const_double:
2096 unsigned HOST_WIDE_INT first, second;
2097 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2099 dw2_asm_output_data (1, 2 * l, NULL);
2100 if (WORDS_BIG_ENDIAN)
2102 first = val2->v.val_double.high;
2103 second = val2->v.val_double.low;
2105 else
2107 first = val2->v.val_double.low;
2108 second = val2->v.val_double.high;
2110 dw2_asm_output_data (l, first, NULL);
2111 dw2_asm_output_data (l, second, NULL);
2113 break;
2114 case dw_val_class_wide_int:
2116 int i;
2117 int len = get_full_len (*val2->v.val_wide);
2118 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2120 dw2_asm_output_data (1, len * l, NULL);
2121 if (WORDS_BIG_ENDIAN)
2122 for (i = len - 1; i >= 0; --i)
2123 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2124 else
2125 for (i = 0; i < len; ++i)
2126 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2128 break;
2129 default:
2130 gcc_unreachable ();
2133 break;
2134 case DW_OP_GNU_regval_type:
2136 unsigned r = val1->v.val_unsigned;
2137 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2138 gcc_assert (o);
2139 if (for_eh_or_skip >= 0)
2141 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2142 gcc_assert (size_of_uleb128 (r)
2143 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2146 dw2_asm_output_data_uleb128 (o, NULL);
2148 break;
2149 case DW_OP_GNU_deref_type:
2151 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2152 gcc_assert (o);
2153 dw2_asm_output_data (1, val1->v.val_int, NULL);
2154 dw2_asm_output_data_uleb128 (o, NULL);
2156 break;
2157 case DW_OP_GNU_convert:
2158 case DW_OP_GNU_reinterpret:
2159 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 else
2163 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2164 gcc_assert (o);
2165 dw2_asm_output_data_uleb128 (o, NULL);
2167 break;
2169 case DW_OP_GNU_parameter_ref:
2171 unsigned long o;
2172 gcc_assert (val1->val_class == dw_val_class_die_ref);
2173 o = get_ref_die_offset (val1->v.val_die_ref.die);
2174 dw2_asm_output_data (4, o, NULL);
2176 break;
2178 default:
2179 /* Other codes have no operands. */
2180 break;
2184 /* Output a sequence of location operations.
2185 The for_eh_or_skip parameter controls whether register numbers are
2186 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2187 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2188 info). This should be suppressed for the cases that have not been converted
2189 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2191 void
2192 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2194 for (; loc != NULL; loc = loc->dw_loc_next)
2196 enum dwarf_location_atom opc = loc->dw_loc_opc;
2197 /* Output the opcode. */
2198 if (for_eh_or_skip >= 0
2199 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2201 unsigned r = (opc - DW_OP_breg0);
2202 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2203 gcc_assert (r <= 31);
2204 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2206 else if (for_eh_or_skip >= 0
2207 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2209 unsigned r = (opc - DW_OP_reg0);
2210 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2211 gcc_assert (r <= 31);
2212 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2215 dw2_asm_output_data (1, opc,
2216 "%s", dwarf_stack_op_name (opc));
2218 /* Output the operand(s) (if any). */
2219 output_loc_operands (loc, for_eh_or_skip);
2223 /* Output location description stack opcode's operands (if any).
2224 The output is single bytes on a line, suitable for .cfi_escape. */
2226 static void
2227 output_loc_operands_raw (dw_loc_descr_ref loc)
2229 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2230 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2232 switch (loc->dw_loc_opc)
2234 case DW_OP_addr:
2235 case DW_OP_GNU_addr_index:
2236 case DW_OP_GNU_const_index:
2237 case DW_OP_implicit_value:
2238 /* We cannot output addresses in .cfi_escape, only bytes. */
2239 gcc_unreachable ();
2241 case DW_OP_const1u:
2242 case DW_OP_const1s:
2243 case DW_OP_pick:
2244 case DW_OP_deref_size:
2245 case DW_OP_xderef_size:
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_raw (1, val1->v.val_int);
2248 break;
2250 case DW_OP_const2u:
2251 case DW_OP_const2s:
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_raw (2, val1->v.val_int);
2254 break;
2256 case DW_OP_const4u:
2257 case DW_OP_const4s:
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_raw (4, val1->v.val_int);
2260 break;
2262 case DW_OP_const8u:
2263 case DW_OP_const8s:
2264 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2265 fputc (',', asm_out_file);
2266 dw2_asm_output_data_raw (8, val1->v.val_int);
2267 break;
2269 case DW_OP_skip:
2270 case DW_OP_bra:
2272 int offset;
2274 gcc_assert (val1->val_class == dw_val_class_loc);
2275 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2277 fputc (',', asm_out_file);
2278 dw2_asm_output_data_raw (2, offset);
2280 break;
2282 case DW_OP_regx:
2284 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2285 gcc_assert (size_of_uleb128 (r)
2286 == size_of_uleb128 (val1->v.val_unsigned));
2287 fputc (',', asm_out_file);
2288 dw2_asm_output_data_uleb128_raw (r);
2290 break;
2292 case DW_OP_constu:
2293 case DW_OP_plus_uconst:
2294 case DW_OP_piece:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2297 break;
2299 case DW_OP_bit_piece:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2302 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2303 break;
2305 case DW_OP_consts:
2306 case DW_OP_breg0:
2307 case DW_OP_breg1:
2308 case DW_OP_breg2:
2309 case DW_OP_breg3:
2310 case DW_OP_breg4:
2311 case DW_OP_breg5:
2312 case DW_OP_breg6:
2313 case DW_OP_breg7:
2314 case DW_OP_breg8:
2315 case DW_OP_breg9:
2316 case DW_OP_breg10:
2317 case DW_OP_breg11:
2318 case DW_OP_breg12:
2319 case DW_OP_breg13:
2320 case DW_OP_breg14:
2321 case DW_OP_breg15:
2322 case DW_OP_breg16:
2323 case DW_OP_breg17:
2324 case DW_OP_breg18:
2325 case DW_OP_breg19:
2326 case DW_OP_breg20:
2327 case DW_OP_breg21:
2328 case DW_OP_breg22:
2329 case DW_OP_breg23:
2330 case DW_OP_breg24:
2331 case DW_OP_breg25:
2332 case DW_OP_breg26:
2333 case DW_OP_breg27:
2334 case DW_OP_breg28:
2335 case DW_OP_breg29:
2336 case DW_OP_breg30:
2337 case DW_OP_breg31:
2338 case DW_OP_fbreg:
2339 fputc (',', asm_out_file);
2340 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2341 break;
2343 case DW_OP_bregx:
2345 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2346 gcc_assert (size_of_uleb128 (r)
2347 == size_of_uleb128 (val1->v.val_unsigned));
2348 fputc (',', asm_out_file);
2349 dw2_asm_output_data_uleb128_raw (r);
2350 fputc (',', asm_out_file);
2351 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2353 break;
2355 case DW_OP_GNU_implicit_pointer:
2356 case DW_OP_GNU_entry_value:
2357 case DW_OP_GNU_const_type:
2358 case DW_OP_GNU_regval_type:
2359 case DW_OP_GNU_deref_type:
2360 case DW_OP_GNU_convert:
2361 case DW_OP_GNU_reinterpret:
2362 case DW_OP_GNU_parameter_ref:
2363 gcc_unreachable ();
2364 break;
2366 default:
2367 /* Other codes have no operands. */
2368 break;
2372 void
2373 output_loc_sequence_raw (dw_loc_descr_ref loc)
2375 while (1)
2377 enum dwarf_location_atom opc = loc->dw_loc_opc;
2378 /* Output the opcode. */
2379 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2381 unsigned r = (opc - DW_OP_breg0);
2382 r = DWARF2_FRAME_REG_OUT (r, 1);
2383 gcc_assert (r <= 31);
2384 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2386 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2388 unsigned r = (opc - DW_OP_reg0);
2389 r = DWARF2_FRAME_REG_OUT (r, 1);
2390 gcc_assert (r <= 31);
2391 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2393 /* Output the opcode. */
2394 fprintf (asm_out_file, "%#x", opc);
2395 output_loc_operands_raw (loc);
2397 if (!loc->dw_loc_next)
2398 break;
2399 loc = loc->dw_loc_next;
2401 fputc (',', asm_out_file);
2405 /* This function builds a dwarf location descriptor sequence from a
2406 dw_cfa_location, adding the given OFFSET to the result of the
2407 expression. */
2409 struct dw_loc_descr_node *
2410 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2412 struct dw_loc_descr_node *head, *tmp;
2414 offset += cfa->offset;
2416 if (cfa->indirect)
2418 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2419 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2420 head->dw_loc_oprnd1.val_entry = NULL;
2421 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2422 add_loc_descr (&head, tmp);
2423 if (offset != 0)
2425 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2426 add_loc_descr (&head, tmp);
2429 else
2430 head = new_reg_loc_descr (cfa->reg, offset);
2432 return head;
2435 /* This function builds a dwarf location descriptor sequence for
2436 the address at OFFSET from the CFA when stack is aligned to
2437 ALIGNMENT byte. */
2439 struct dw_loc_descr_node *
2440 build_cfa_aligned_loc (dw_cfa_location *cfa,
2441 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2443 struct dw_loc_descr_node *head;
2444 unsigned int dwarf_fp
2445 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2447 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2448 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2450 head = new_reg_loc_descr (dwarf_fp, 0);
2451 add_loc_descr (&head, int_loc_descriptor (alignment));
2452 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2453 loc_descr_plus_const (&head, offset);
2455 else
2456 head = new_reg_loc_descr (dwarf_fp, offset);
2457 return head;
2460 /* And now, the support for symbolic debugging information. */
2462 /* .debug_str support. */
2464 static void dwarf2out_init (const char *);
2465 static void dwarf2out_finish (const char *);
2466 static void dwarf2out_early_finish (const char *);
2467 static void dwarf2out_assembly_start (void);
2468 static void dwarf2out_define (unsigned int, const char *);
2469 static void dwarf2out_undef (unsigned int, const char *);
2470 static void dwarf2out_start_source_file (unsigned, const char *);
2471 static void dwarf2out_end_source_file (unsigned);
2472 static void dwarf2out_function_decl (tree);
2473 static void dwarf2out_begin_block (unsigned, unsigned);
2474 static void dwarf2out_end_block (unsigned, unsigned);
2475 static bool dwarf2out_ignore_block (const_tree);
2476 static void dwarf2out_early_global_decl (tree);
2477 static void dwarf2out_late_global_decl (tree);
2478 static void dwarf2out_type_decl (tree, int);
2479 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2480 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2481 dw_die_ref);
2482 static void dwarf2out_abstract_function (tree);
2483 static void dwarf2out_var_location (rtx_insn *);
2484 static void dwarf2out_size_function (tree);
2485 static void dwarf2out_begin_function (tree);
2486 static void dwarf2out_end_function (unsigned int);
2487 static void dwarf2out_register_main_translation_unit (tree unit);
2488 static void dwarf2out_set_name (tree, tree);
2490 /* The debug hooks structure. */
2492 const struct gcc_debug_hooks dwarf2_debug_hooks =
2494 dwarf2out_init,
2495 dwarf2out_finish,
2496 dwarf2out_early_finish,
2497 dwarf2out_assembly_start,
2498 dwarf2out_define,
2499 dwarf2out_undef,
2500 dwarf2out_start_source_file,
2501 dwarf2out_end_source_file,
2502 dwarf2out_begin_block,
2503 dwarf2out_end_block,
2504 dwarf2out_ignore_block,
2505 dwarf2out_source_line,
2506 dwarf2out_begin_prologue,
2507 #if VMS_DEBUGGING_INFO
2508 dwarf2out_vms_end_prologue,
2509 dwarf2out_vms_begin_epilogue,
2510 #else
2511 debug_nothing_int_charstar,
2512 debug_nothing_int_charstar,
2513 #endif
2514 dwarf2out_end_epilogue,
2515 dwarf2out_begin_function,
2516 dwarf2out_end_function, /* end_function */
2517 dwarf2out_register_main_translation_unit,
2518 dwarf2out_function_decl, /* function_decl */
2519 dwarf2out_early_global_decl,
2520 dwarf2out_late_global_decl,
2521 dwarf2out_type_decl, /* type_decl */
2522 dwarf2out_imported_module_or_decl,
2523 debug_nothing_tree, /* deferred_inline_function */
2524 /* The DWARF 2 backend tries to reduce debugging bloat by not
2525 emitting the abstract description of inline functions until
2526 something tries to reference them. */
2527 dwarf2out_abstract_function, /* outlining_inline_function */
2528 debug_nothing_rtx_code_label, /* label */
2529 debug_nothing_int, /* handle_pch */
2530 dwarf2out_var_location,
2531 dwarf2out_size_function, /* size_function */
2532 dwarf2out_switch_text_section,
2533 dwarf2out_set_name,
2534 1, /* start_end_main_source_file */
2535 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2538 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2540 dwarf2out_init,
2541 debug_nothing_charstar,
2542 debug_nothing_charstar,
2543 debug_nothing_void,
2544 debug_nothing_int_charstar,
2545 debug_nothing_int_charstar,
2546 debug_nothing_int_charstar,
2547 debug_nothing_int,
2548 debug_nothing_int_int, /* begin_block */
2549 debug_nothing_int_int, /* end_block */
2550 debug_true_const_tree, /* ignore_block */
2551 dwarf2out_source_line, /* source_line */
2552 debug_nothing_int_charstar, /* begin_prologue */
2553 debug_nothing_int_charstar, /* end_prologue */
2554 debug_nothing_int_charstar, /* begin_epilogue */
2555 debug_nothing_int_charstar, /* end_epilogue */
2556 debug_nothing_tree, /* begin_function */
2557 debug_nothing_int, /* end_function */
2558 debug_nothing_tree, /* register_main_translation_unit */
2559 debug_nothing_tree, /* function_decl */
2560 debug_nothing_tree, /* early_global_decl */
2561 debug_nothing_tree, /* late_global_decl */
2562 debug_nothing_tree_int, /* type_decl */
2563 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2564 debug_nothing_tree, /* deferred_inline_function */
2565 debug_nothing_tree, /* outlining_inline_function */
2566 debug_nothing_rtx_code_label, /* label */
2567 debug_nothing_int, /* handle_pch */
2568 debug_nothing_rtx_insn, /* var_location */
2569 debug_nothing_tree, /* size_function */
2570 debug_nothing_void, /* switch_text_section */
2571 debug_nothing_tree_tree, /* set_name */
2572 0, /* start_end_main_source_file */
2573 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2576 /* NOTE: In the comments in this file, many references are made to
2577 "Debugging Information Entries". This term is abbreviated as `DIE'
2578 throughout the remainder of this file. */
2580 /* An internal representation of the DWARF output is built, and then
2581 walked to generate the DWARF debugging info. The walk of the internal
2582 representation is done after the entire program has been compiled.
2583 The types below are used to describe the internal representation. */
2585 /* Whether to put type DIEs into their own section .debug_types instead
2586 of making them part of the .debug_info section. Only supported for
2587 Dwarf V4 or higher and the user didn't disable them through
2588 -fno-debug-types-section. It is more efficient to put them in a
2589 separate comdat sections since the linker will then be able to
2590 remove duplicates. But not all tools support .debug_types sections
2591 yet. */
2593 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2595 /* Various DIE's use offsets relative to the beginning of the
2596 .debug_info section to refer to each other. */
2598 typedef long int dw_offset;
2600 struct comdat_type_node;
2602 /* The entries in the line_info table more-or-less mirror the opcodes
2603 that are used in the real dwarf line table. Arrays of these entries
2604 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2605 supported. */
2607 enum dw_line_info_opcode {
2608 /* Emit DW_LNE_set_address; the operand is the label index. */
2609 LI_set_address,
2611 /* Emit a row to the matrix with the given line. This may be done
2612 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2613 special opcodes. */
2614 LI_set_line,
2616 /* Emit a DW_LNS_set_file. */
2617 LI_set_file,
2619 /* Emit a DW_LNS_set_column. */
2620 LI_set_column,
2622 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2623 LI_negate_stmt,
2625 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2626 LI_set_prologue_end,
2627 LI_set_epilogue_begin,
2629 /* Emit a DW_LNE_set_discriminator. */
2630 LI_set_discriminator
2633 typedef struct GTY(()) dw_line_info_struct {
2634 enum dw_line_info_opcode opcode;
2635 unsigned int val;
2636 } dw_line_info_entry;
2639 struct GTY(()) dw_line_info_table {
2640 /* The label that marks the end of this section. */
2641 const char *end_label;
2643 /* The values for the last row of the matrix, as collected in the table.
2644 These are used to minimize the changes to the next row. */
2645 unsigned int file_num;
2646 unsigned int line_num;
2647 unsigned int column_num;
2648 int discrim_num;
2649 bool is_stmt;
2650 bool in_use;
2652 vec<dw_line_info_entry, va_gc> *entries;
2656 /* Each DIE attribute has a field specifying the attribute kind,
2657 a link to the next attribute in the chain, and an attribute value.
2658 Attributes are typically linked below the DIE they modify. */
2660 typedef struct GTY(()) dw_attr_struct {
2661 enum dwarf_attribute dw_attr;
2662 dw_val_node dw_attr_val;
2664 dw_attr_node;
2667 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2668 The children of each node form a circular list linked by
2669 die_sib. die_child points to the node *before* the "first" child node. */
2671 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2672 union die_symbol_or_type_node
2674 const char * GTY ((tag ("0"))) die_symbol;
2675 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2677 GTY ((desc ("%0.comdat_type_p"))) die_id;
2678 vec<dw_attr_node, va_gc> *die_attr;
2679 dw_die_ref die_parent;
2680 dw_die_ref die_child;
2681 dw_die_ref die_sib;
2682 dw_die_ref die_definition; /* ref from a specification to its definition */
2683 dw_offset die_offset;
2684 unsigned long die_abbrev;
2685 int die_mark;
2686 unsigned int decl_id;
2687 enum dwarf_tag die_tag;
2688 /* Die is used and must not be pruned as unused. */
2689 BOOL_BITFIELD die_perennial_p : 1;
2690 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2691 /* Whether this DIE was removed from the DIE tree, for example via
2692 prune_unused_types. We don't consider those present from the
2693 DIE lookup routines. */
2694 BOOL_BITFIELD removed : 1;
2695 /* Lots of spare bits. */
2697 die_node;
2699 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2700 static bool early_dwarf;
2701 static bool early_dwarf_finished;
2702 struct set_early_dwarf {
2703 bool saved;
2704 set_early_dwarf () : saved(early_dwarf)
2706 gcc_assert (! early_dwarf_finished);
2707 early_dwarf = true;
2709 ~set_early_dwarf () { early_dwarf = saved; }
2712 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2713 #define FOR_EACH_CHILD(die, c, expr) do { \
2714 c = die->die_child; \
2715 if (c) do { \
2716 c = c->die_sib; \
2717 expr; \
2718 } while (c != die->die_child); \
2719 } while (0)
2721 /* The pubname structure */
2723 typedef struct GTY(()) pubname_struct {
2724 dw_die_ref die;
2725 const char *name;
2727 pubname_entry;
2730 struct GTY(()) dw_ranges {
2731 /* If this is positive, it's a block number, otherwise it's a
2732 bitwise-negated index into dw_ranges_by_label. */
2733 int num;
2736 /* A structure to hold a macinfo entry. */
2738 typedef struct GTY(()) macinfo_struct {
2739 unsigned char code;
2740 unsigned HOST_WIDE_INT lineno;
2741 const char *info;
2743 macinfo_entry;
2746 struct GTY(()) dw_ranges_by_label {
2747 const char *begin;
2748 const char *end;
2751 /* The comdat type node structure. */
2752 struct GTY(()) comdat_type_node
2754 dw_die_ref root_die;
2755 dw_die_ref type_die;
2756 dw_die_ref skeleton_die;
2757 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2758 comdat_type_node *next;
2761 /* A list of DIEs for which we can't determine ancestry (parent_die
2762 field) just yet. Later in dwarf2out_finish we will fill in the
2763 missing bits. */
2764 typedef struct GTY(()) limbo_die_struct {
2765 dw_die_ref die;
2766 /* The tree for which this DIE was created. We use this to
2767 determine ancestry later. */
2768 tree created_for;
2769 struct limbo_die_struct *next;
2771 limbo_die_node;
2773 typedef struct skeleton_chain_struct
2775 dw_die_ref old_die;
2776 dw_die_ref new_die;
2777 struct skeleton_chain_struct *parent;
2779 skeleton_chain_node;
2781 /* Define a macro which returns nonzero for a TYPE_DECL which was
2782 implicitly generated for a type.
2784 Note that, unlike the C front-end (which generates a NULL named
2785 TYPE_DECL node for each complete tagged type, each array type,
2786 and each function type node created) the C++ front-end generates
2787 a _named_ TYPE_DECL node for each tagged type node created.
2788 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2789 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2790 front-end, but for each type, tagged or not. */
2792 #define TYPE_DECL_IS_STUB(decl) \
2793 (DECL_NAME (decl) == NULL_TREE \
2794 || (DECL_ARTIFICIAL (decl) \
2795 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2796 /* This is necessary for stub decls that \
2797 appear in nested inline functions. */ \
2798 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2799 && (decl_ultimate_origin (decl) \
2800 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2802 /* Information concerning the compilation unit's programming
2803 language, and compiler version. */
2805 /* Fixed size portion of the DWARF compilation unit header. */
2806 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2807 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2809 /* Fixed size portion of the DWARF comdat type unit header. */
2810 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2811 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2812 + DWARF_OFFSET_SIZE)
2814 /* Fixed size portion of public names info. */
2815 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2817 /* Fixed size portion of the address range info. */
2818 #define DWARF_ARANGES_HEADER_SIZE \
2819 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2820 DWARF2_ADDR_SIZE * 2) \
2821 - DWARF_INITIAL_LENGTH_SIZE)
2823 /* Size of padding portion in the address range info. It must be
2824 aligned to twice the pointer size. */
2825 #define DWARF_ARANGES_PAD_SIZE \
2826 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2827 DWARF2_ADDR_SIZE * 2) \
2828 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2830 /* Use assembler line directives if available. */
2831 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2832 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2833 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2834 #else
2835 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2836 #endif
2837 #endif
2839 /* Minimum line offset in a special line info. opcode.
2840 This value was chosen to give a reasonable range of values. */
2841 #define DWARF_LINE_BASE -10
2843 /* First special line opcode - leave room for the standard opcodes. */
2844 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2846 /* Range of line offsets in a special line info. opcode. */
2847 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2849 /* Flag that indicates the initial value of the is_stmt_start flag.
2850 In the present implementation, we do not mark any lines as
2851 the beginning of a source statement, because that information
2852 is not made available by the GCC front-end. */
2853 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2855 /* Maximum number of operations per instruction bundle. */
2856 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2857 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2858 #endif
2860 /* This location is used by calc_die_sizes() to keep track
2861 the offset of each DIE within the .debug_info section. */
2862 static unsigned long next_die_offset;
2864 /* Record the root of the DIE's built for the current compilation unit. */
2865 static GTY(()) dw_die_ref single_comp_unit_die;
2867 /* A list of type DIEs that have been separated into comdat sections. */
2868 static GTY(()) comdat_type_node *comdat_type_list;
2870 /* A list of CU DIEs that have been separated. */
2871 static GTY(()) limbo_die_node *cu_die_list;
2873 /* A list of DIEs with a NULL parent waiting to be relocated. */
2874 static GTY(()) limbo_die_node *limbo_die_list;
2876 /* A list of DIEs for which we may have to generate
2877 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2878 static GTY(()) limbo_die_node *deferred_asm_name;
2880 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2882 typedef const char *compare_type;
2884 static hashval_t hash (dwarf_file_data *);
2885 static bool equal (dwarf_file_data *, const char *);
2888 /* Filenames referenced by this compilation unit. */
2889 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2891 struct decl_die_hasher : ggc_ptr_hash<die_node>
2893 typedef tree compare_type;
2895 static hashval_t hash (die_node *);
2896 static bool equal (die_node *, tree);
2898 /* A hash table of references to DIE's that describe declarations.
2899 The key is a DECL_UID() which is a unique number identifying each decl. */
2900 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2902 struct block_die_hasher : ggc_ptr_hash<die_struct>
2904 static hashval_t hash (die_struct *);
2905 static bool equal (die_struct *, die_struct *);
2908 /* A hash table of references to DIE's that describe COMMON blocks.
2909 The key is DECL_UID() ^ die_parent. */
2910 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2912 typedef struct GTY(()) die_arg_entry_struct {
2913 dw_die_ref die;
2914 tree arg;
2915 } die_arg_entry;
2918 /* Node of the variable location list. */
2919 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2920 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2921 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2922 in mode of the EXPR_LIST node and first EXPR_LIST operand
2923 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2924 location or NULL for padding. For larger bitsizes,
2925 mode is 0 and first operand is a CONCAT with bitsize
2926 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2927 NULL as second operand. */
2928 rtx GTY (()) loc;
2929 const char * GTY (()) label;
2930 struct var_loc_node * GTY (()) next;
2933 /* Variable location list. */
2934 struct GTY ((for_user)) var_loc_list_def {
2935 struct var_loc_node * GTY (()) first;
2937 /* Pointer to the last but one or last element of the
2938 chained list. If the list is empty, both first and
2939 last are NULL, if the list contains just one node
2940 or the last node certainly is not redundant, it points
2941 to the last node, otherwise points to the last but one.
2942 Do not mark it for GC because it is marked through the chain. */
2943 struct var_loc_node * GTY ((skip ("%h"))) last;
2945 /* Pointer to the last element before section switch,
2946 if NULL, either sections weren't switched or first
2947 is after section switch. */
2948 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2950 /* DECL_UID of the variable decl. */
2951 unsigned int decl_id;
2953 typedef struct var_loc_list_def var_loc_list;
2955 /* Call argument location list. */
2956 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2957 rtx GTY (()) call_arg_loc_note;
2958 const char * GTY (()) label;
2959 tree GTY (()) block;
2960 bool tail_call_p;
2961 rtx GTY (()) symbol_ref;
2962 struct call_arg_loc_node * GTY (()) next;
2966 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2968 typedef const_tree compare_type;
2970 static hashval_t hash (var_loc_list *);
2971 static bool equal (var_loc_list *, const_tree);
2974 /* Table of decl location linked lists. */
2975 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2977 /* Head and tail of call_arg_loc chain. */
2978 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2979 static struct call_arg_loc_node *call_arg_loc_last;
2981 /* Number of call sites in the current function. */
2982 static int call_site_count = -1;
2983 /* Number of tail call sites in the current function. */
2984 static int tail_call_site_count = -1;
2986 /* A cached location list. */
2987 struct GTY ((for_user)) cached_dw_loc_list_def {
2988 /* The DECL_UID of the decl that this entry describes. */
2989 unsigned int decl_id;
2991 /* The cached location list. */
2992 dw_loc_list_ref loc_list;
2994 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2996 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2999 typedef const_tree compare_type;
3001 static hashval_t hash (cached_dw_loc_list *);
3002 static bool equal (cached_dw_loc_list *, const_tree);
3005 /* Table of cached location lists. */
3006 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3008 /* A pointer to the base of a list of references to DIE's that
3009 are uniquely identified by their tag, presence/absence of
3010 children DIE's, and list of attribute/value pairs. */
3011 static GTY((length ("abbrev_die_table_allocated")))
3012 dw_die_ref *abbrev_die_table;
3014 /* Number of elements currently allocated for abbrev_die_table. */
3015 static GTY(()) unsigned abbrev_die_table_allocated;
3017 /* Number of elements in abbrev_die_table currently in use. */
3018 static GTY(()) unsigned abbrev_die_table_in_use;
3020 /* A hash map to remember the stack usage for DWARF procedures. The value
3021 stored is the stack size difference between before the DWARF procedure
3022 invokation and after it returned. In other words, for a DWARF procedure
3023 that consumes N stack slots and that pushes M ones, this stores M - N. */
3024 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3026 /* Size (in elements) of increments by which we may expand the
3027 abbrev_die_table. */
3028 #define ABBREV_DIE_TABLE_INCREMENT 256
3030 /* A global counter for generating labels for line number data. */
3031 static unsigned int line_info_label_num;
3033 /* The current table to which we should emit line number information
3034 for the current function. This will be set up at the beginning of
3035 assembly for the function. */
3036 static GTY(()) dw_line_info_table *cur_line_info_table;
3038 /* The two default tables of line number info. */
3039 static GTY(()) dw_line_info_table *text_section_line_info;
3040 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3042 /* The set of all non-default tables of line number info. */
3043 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3045 /* A flag to tell pubnames/types export if there is an info section to
3046 refer to. */
3047 static bool info_section_emitted;
3049 /* A pointer to the base of a table that contains a list of publicly
3050 accessible names. */
3051 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3053 /* A pointer to the base of a table that contains a list of publicly
3054 accessible types. */
3055 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3057 /* A pointer to the base of a table that contains a list of macro
3058 defines/undefines (and file start/end markers). */
3059 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3061 /* True if .debug_macinfo or .debug_macros section is going to be
3062 emitted. */
3063 #define have_macinfo \
3064 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3065 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3066 && !macinfo_table->is_empty ())
3068 /* Array of dies for which we should generate .debug_ranges info. */
3069 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3071 /* Number of elements currently allocated for ranges_table. */
3072 static GTY(()) unsigned ranges_table_allocated;
3074 /* Number of elements in ranges_table currently in use. */
3075 static GTY(()) unsigned ranges_table_in_use;
3077 /* Array of pairs of labels referenced in ranges_table. */
3078 static GTY ((length ("ranges_by_label_allocated")))
3079 dw_ranges_by_label *ranges_by_label;
3081 /* Number of elements currently allocated for ranges_by_label. */
3082 static GTY(()) unsigned ranges_by_label_allocated;
3084 /* Number of elements in ranges_by_label currently in use. */
3085 static GTY(()) unsigned ranges_by_label_in_use;
3087 /* Size (in elements) of increments by which we may expand the
3088 ranges_table. */
3089 #define RANGES_TABLE_INCREMENT 64
3091 /* Whether we have location lists that need outputting */
3092 static GTY(()) bool have_location_lists;
3094 /* Unique label counter. */
3095 static GTY(()) unsigned int loclabel_num;
3097 /* Unique label counter for point-of-call tables. */
3098 static GTY(()) unsigned int poc_label_num;
3100 /* The last file entry emitted by maybe_emit_file(). */
3101 static GTY(()) struct dwarf_file_data * last_emitted_file;
3103 /* Number of internal labels generated by gen_internal_sym(). */
3104 static GTY(()) int label_num;
3106 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3108 /* Instances of generic types for which we need to generate debug
3109 info that describe their generic parameters and arguments. That
3110 generation needs to happen once all types are properly laid out so
3111 we do it at the end of compilation. */
3112 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3114 /* Offset from the "steady-state frame pointer" to the frame base,
3115 within the current function. */
3116 static HOST_WIDE_INT frame_pointer_fb_offset;
3117 static bool frame_pointer_fb_offset_valid;
3119 static vec<dw_die_ref> base_types;
3121 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3122 once all arguments are parsed. */
3123 static vec<dw_die_ref> *string_types;
3125 /* Flags to represent a set of attribute classes for attributes that represent
3126 a scalar value (bounds, pointers, ...). */
3127 enum dw_scalar_form
3129 dw_scalar_form_constant = 0x01,
3130 dw_scalar_form_exprloc = 0x02,
3131 dw_scalar_form_reference = 0x04
3134 /* Forward declarations for functions defined in this file. */
3136 static int is_pseudo_reg (const_rtx);
3137 static tree type_main_variant (tree);
3138 static int is_tagged_type (const_tree);
3139 static const char *dwarf_tag_name (unsigned);
3140 static const char *dwarf_attr_name (unsigned);
3141 static const char *dwarf_form_name (unsigned);
3142 static tree decl_ultimate_origin (const_tree);
3143 static tree decl_class_context (tree);
3144 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3145 static inline enum dw_val_class AT_class (dw_attr_node *);
3146 static inline unsigned int AT_index (dw_attr_node *);
3147 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3148 static inline unsigned AT_flag (dw_attr_node *);
3149 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3150 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3151 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3152 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3153 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3154 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3155 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3156 unsigned int, unsigned char *);
3157 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3158 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3159 static inline const char *AT_string (dw_attr_node *);
3160 static enum dwarf_form AT_string_form (dw_attr_node *);
3161 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3162 static void add_AT_specification (dw_die_ref, dw_die_ref);
3163 static inline dw_die_ref AT_ref (dw_attr_node *);
3164 static inline int AT_ref_external (dw_attr_node *);
3165 static inline void set_AT_ref_external (dw_attr_node *, int);
3166 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3167 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3168 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3169 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3170 dw_loc_list_ref);
3171 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3172 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3173 static void remove_addr_table_entry (addr_table_entry *);
3174 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3175 static inline rtx AT_addr (dw_attr_node *);
3176 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3177 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3178 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3179 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3180 unsigned HOST_WIDE_INT);
3181 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3182 unsigned long, bool);
3183 static inline const char *AT_lbl (dw_attr_node *);
3184 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3185 static const char *get_AT_low_pc (dw_die_ref);
3186 static const char *get_AT_hi_pc (dw_die_ref);
3187 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3188 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3189 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3190 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3191 static bool is_cxx (void);
3192 static bool is_fortran (void);
3193 static bool is_ada (void);
3194 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3195 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3196 static void add_child_die (dw_die_ref, dw_die_ref);
3197 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3198 static dw_die_ref lookup_type_die (tree);
3199 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3200 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3201 static void equate_type_number_to_die (tree, dw_die_ref);
3202 static dw_die_ref lookup_decl_die (tree);
3203 static var_loc_list *lookup_decl_loc (const_tree);
3204 static void equate_decl_number_to_die (tree, dw_die_ref);
3205 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3206 static void print_spaces (FILE *);
3207 static void print_die (dw_die_ref, FILE *);
3208 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3209 static dw_die_ref pop_compile_unit (dw_die_ref);
3210 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3211 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3212 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3213 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3214 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3215 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3216 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3217 struct md5_ctx *, int *);
3218 struct checksum_attributes;
3219 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3220 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3221 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3222 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3223 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3224 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3225 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3226 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3227 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3228 static void compute_section_prefix (dw_die_ref);
3229 static int is_type_die (dw_die_ref);
3230 static int is_comdat_die (dw_die_ref);
3231 static int is_symbol_die (dw_die_ref);
3232 static inline bool is_template_instantiation (dw_die_ref);
3233 static void assign_symbol_names (dw_die_ref);
3234 static void break_out_includes (dw_die_ref);
3235 static int is_declaration_die (dw_die_ref);
3236 static int should_move_die_to_comdat (dw_die_ref);
3237 static dw_die_ref clone_as_declaration (dw_die_ref);
3238 static dw_die_ref clone_die (dw_die_ref);
3239 static dw_die_ref clone_tree (dw_die_ref);
3240 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3241 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3242 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3243 static dw_die_ref generate_skeleton (dw_die_ref);
3244 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3245 dw_die_ref,
3246 dw_die_ref);
3247 static void break_out_comdat_types (dw_die_ref);
3248 static void copy_decls_for_unworthy_types (dw_die_ref);
3250 static void add_sibling_attributes (dw_die_ref);
3251 static void output_location_lists (dw_die_ref);
3252 static int constant_size (unsigned HOST_WIDE_INT);
3253 static unsigned long size_of_die (dw_die_ref);
3254 static void calc_die_sizes (dw_die_ref);
3255 static void calc_base_type_die_sizes (void);
3256 static void mark_dies (dw_die_ref);
3257 static void unmark_dies (dw_die_ref);
3258 static void unmark_all_dies (dw_die_ref);
3259 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3260 static unsigned long size_of_aranges (void);
3261 static enum dwarf_form value_format (dw_attr_node *);
3262 static void output_value_format (dw_attr_node *);
3263 static void output_abbrev_section (void);
3264 static void output_die_abbrevs (unsigned long, dw_die_ref);
3265 static void output_die_symbol (dw_die_ref);
3266 static void output_die (dw_die_ref);
3267 static void output_compilation_unit_header (void);
3268 static void output_comp_unit (dw_die_ref, int);
3269 static void output_comdat_type_unit (comdat_type_node *);
3270 static const char *dwarf2_name (tree, int);
3271 static void add_pubname (tree, dw_die_ref);
3272 static void add_enumerator_pubname (const char *, dw_die_ref);
3273 static void add_pubname_string (const char *, dw_die_ref);
3274 static void add_pubtype (tree, dw_die_ref);
3275 static void output_pubnames (vec<pubname_entry, va_gc> *);
3276 static void output_aranges (void);
3277 static unsigned int add_ranges_num (int);
3278 static unsigned int add_ranges (const_tree);
3279 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3280 bool *, bool);
3281 static void output_ranges (void);
3282 static dw_line_info_table *new_line_info_table (void);
3283 static void output_line_info (bool);
3284 static void output_file_names (void);
3285 static dw_die_ref base_type_die (tree, bool);
3286 static int is_base_type (tree);
3287 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3288 static int decl_quals (const_tree);
3289 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3290 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3291 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3292 static int type_is_enum (const_tree);
3293 static unsigned int dbx_reg_number (const_rtx);
3294 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3295 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3296 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3297 enum var_init_status);
3298 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3299 enum var_init_status);
3300 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3301 enum var_init_status);
3302 static int is_based_loc (const_rtx);
3303 static bool resolve_one_addr (rtx *);
3304 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3305 enum var_init_status);
3306 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3307 enum var_init_status);
3308 struct loc_descr_context;
3309 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3310 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3311 static dw_loc_list_ref loc_list_from_tree (tree, int,
3312 const struct loc_descr_context *);
3313 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3314 const struct loc_descr_context *);
3315 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3316 static tree field_type (const_tree);
3317 static unsigned int simple_type_align_in_bits (const_tree);
3318 static unsigned int simple_decl_align_in_bits (const_tree);
3319 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3320 struct vlr_context;
3321 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3322 HOST_WIDE_INT *);
3323 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3324 dw_loc_list_ref);
3325 static void add_data_member_location_attribute (dw_die_ref, tree,
3326 struct vlr_context *);
3327 static bool add_const_value_attribute (dw_die_ref, rtx);
3328 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3329 static void insert_wide_int (const wide_int &, unsigned char *, int);
3330 static void insert_float (const_rtx, unsigned char *);
3331 static rtx rtl_for_decl_location (tree);
3332 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3333 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3334 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3335 static void add_name_attribute (dw_die_ref, const char *);
3336 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3337 static void add_comp_dir_attribute (dw_die_ref);
3338 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3339 const struct loc_descr_context *);
3340 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3341 const struct loc_descr_context *);
3342 static void add_subscript_info (dw_die_ref, tree, bool);
3343 static void add_byte_size_attribute (dw_die_ref, tree);
3344 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3345 struct vlr_context *);
3346 static void add_bit_size_attribute (dw_die_ref, tree);
3347 static void add_prototyped_attribute (dw_die_ref, tree);
3348 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3349 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3350 static void add_src_coords_attributes (dw_die_ref, tree);
3351 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3352 static void add_discr_value (dw_die_ref, dw_discr_value *);
3353 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3354 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3355 static void push_decl_scope (tree);
3356 static void pop_decl_scope (void);
3357 static dw_die_ref scope_die_for (tree, dw_die_ref);
3358 static inline int local_scope_p (dw_die_ref);
3359 static inline int class_scope_p (dw_die_ref);
3360 static inline int class_or_namespace_scope_p (dw_die_ref);
3361 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3362 static void add_calling_convention_attribute (dw_die_ref, tree);
3363 static const char *type_tag (const_tree);
3364 static tree member_declared_type (const_tree);
3365 #if 0
3366 static const char *decl_start_label (tree);
3367 #endif
3368 static void gen_array_type_die (tree, dw_die_ref);
3369 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3370 #if 0
3371 static void gen_entry_point_die (tree, dw_die_ref);
3372 #endif
3373 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3374 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3375 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3376 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3377 static void gen_formal_types_die (tree, dw_die_ref);
3378 static void gen_subprogram_die (tree, dw_die_ref);
3379 static void gen_variable_die (tree, tree, dw_die_ref);
3380 static void gen_const_die (tree, dw_die_ref);
3381 static void gen_label_die (tree, dw_die_ref);
3382 static void gen_lexical_block_die (tree, dw_die_ref);
3383 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3384 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3385 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3386 static dw_die_ref gen_compile_unit_die (const char *);
3387 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3388 static void gen_member_die (tree, dw_die_ref);
3389 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3390 enum debug_info_usage);
3391 static void gen_subroutine_type_die (tree, dw_die_ref);
3392 static void gen_typedef_die (tree, dw_die_ref);
3393 static void gen_type_die (tree, dw_die_ref);
3394 static void gen_block_die (tree, dw_die_ref);
3395 static void decls_for_scope (tree, dw_die_ref);
3396 static bool is_naming_typedef_decl (const_tree);
3397 static inline dw_die_ref get_context_die (tree);
3398 static void gen_namespace_die (tree, dw_die_ref);
3399 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3400 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3401 static dw_die_ref force_decl_die (tree);
3402 static dw_die_ref force_type_die (tree);
3403 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3404 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3405 static struct dwarf_file_data * lookup_filename (const char *);
3406 static void retry_incomplete_types (void);
3407 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3408 static void gen_generic_params_dies (tree);
3409 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3410 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3411 static void splice_child_die (dw_die_ref, dw_die_ref);
3412 static int file_info_cmp (const void *, const void *);
3413 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3414 const char *, const char *);
3415 static void output_loc_list (dw_loc_list_ref);
3416 static char *gen_internal_sym (const char *);
3417 static bool want_pubnames (void);
3419 static void prune_unmark_dies (dw_die_ref);
3420 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3421 static void prune_unused_types_mark (dw_die_ref, int);
3422 static void prune_unused_types_walk (dw_die_ref);
3423 static void prune_unused_types_walk_attribs (dw_die_ref);
3424 static void prune_unused_types_prune (dw_die_ref);
3425 static void prune_unused_types (void);
3426 static int maybe_emit_file (struct dwarf_file_data *fd);
3427 static inline const char *AT_vms_delta1 (dw_attr_node *);
3428 static inline const char *AT_vms_delta2 (dw_attr_node *);
3429 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3430 const char *, const char *);
3431 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3432 static void gen_remaining_tmpl_value_param_die_attribute (void);
3433 static bool generic_type_p (tree);
3434 static void schedule_generic_params_dies_gen (tree t);
3435 static void gen_scheduled_generic_parms_dies (void);
3437 static const char *comp_dir_string (void);
3439 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3441 /* enum for tracking thread-local variables whose address is really an offset
3442 relative to the TLS pointer, which will need link-time relocation, but will
3443 not need relocation by the DWARF consumer. */
3445 enum dtprel_bool
3447 dtprel_false = 0,
3448 dtprel_true = 1
3451 /* Return the operator to use for an address of a variable. For dtprel_true, we
3452 use DW_OP_const*. For regular variables, which need both link-time
3453 relocation and consumer-level relocation (e.g., to account for shared objects
3454 loaded at a random address), we use DW_OP_addr*. */
3456 static inline enum dwarf_location_atom
3457 dw_addr_op (enum dtprel_bool dtprel)
3459 if (dtprel == dtprel_true)
3460 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3461 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3462 else
3463 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3466 /* Return a pointer to a newly allocated address location description. If
3467 dwarf_split_debug_info is true, then record the address with the appropriate
3468 relocation. */
3469 static inline dw_loc_descr_ref
3470 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3472 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3474 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3475 ref->dw_loc_oprnd1.v.val_addr = addr;
3476 ref->dtprel = dtprel;
3477 if (dwarf_split_debug_info)
3478 ref->dw_loc_oprnd1.val_entry
3479 = add_addr_table_entry (addr,
3480 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3481 else
3482 ref->dw_loc_oprnd1.val_entry = NULL;
3484 return ref;
3487 /* Section names used to hold DWARF debugging information. */
3489 #ifndef DEBUG_INFO_SECTION
3490 #define DEBUG_INFO_SECTION ".debug_info"
3491 #endif
3492 #ifndef DEBUG_DWO_INFO_SECTION
3493 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3494 #endif
3495 #ifndef DEBUG_ABBREV_SECTION
3496 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_DWO_ABBREV_SECTION
3499 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3500 #endif
3501 #ifndef DEBUG_ARANGES_SECTION
3502 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3503 #endif
3504 #ifndef DEBUG_ADDR_SECTION
3505 #define DEBUG_ADDR_SECTION ".debug_addr"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION
3508 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3509 #endif
3510 #ifndef DEBUG_DWO_MACINFO_SECTION
3511 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3512 #endif
3513 #ifndef DEBUG_DWO_MACRO_SECTION
3514 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3515 #endif
3516 #ifndef DEBUG_MACRO_SECTION
3517 #define DEBUG_MACRO_SECTION ".debug_macro"
3518 #endif
3519 #ifndef DEBUG_LINE_SECTION
3520 #define DEBUG_LINE_SECTION ".debug_line"
3521 #endif
3522 #ifndef DEBUG_DWO_LINE_SECTION
3523 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3524 #endif
3525 #ifndef DEBUG_LOC_SECTION
3526 #define DEBUG_LOC_SECTION ".debug_loc"
3527 #endif
3528 #ifndef DEBUG_DWO_LOC_SECTION
3529 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3530 #endif
3531 #ifndef DEBUG_PUBNAMES_SECTION
3532 #define DEBUG_PUBNAMES_SECTION \
3533 ((debug_generate_pub_sections == 2) \
3534 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3535 #endif
3536 #ifndef DEBUG_PUBTYPES_SECTION
3537 #define DEBUG_PUBTYPES_SECTION \
3538 ((debug_generate_pub_sections == 2) \
3539 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3540 #endif
3541 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3542 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3543 #ifndef DEBUG_STR_OFFSETS_SECTION
3544 #define DEBUG_STR_OFFSETS_SECTION \
3545 (!dwarf_split_debug_info \
3546 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3547 #endif
3548 #ifndef DEBUG_STR_DWO_SECTION
3549 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3550 #endif
3551 #ifndef DEBUG_STR_SECTION
3552 #define DEBUG_STR_SECTION ".debug_str"
3553 #endif
3554 #ifndef DEBUG_RANGES_SECTION
3555 #define DEBUG_RANGES_SECTION ".debug_ranges"
3556 #endif
3558 /* Standard ELF section names for compiled code and data. */
3559 #ifndef TEXT_SECTION_NAME
3560 #define TEXT_SECTION_NAME ".text"
3561 #endif
3563 /* Section flags for .debug_str section. */
3564 #define DEBUG_STR_SECTION_FLAGS \
3565 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3566 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3567 : SECTION_DEBUG)
3569 /* Section flags for .debug_str.dwo section. */
3570 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3572 /* Labels we insert at beginning sections we can reference instead of
3573 the section names themselves. */
3575 #ifndef TEXT_SECTION_LABEL
3576 #define TEXT_SECTION_LABEL "Ltext"
3577 #endif
3578 #ifndef COLD_TEXT_SECTION_LABEL
3579 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3580 #endif
3581 #ifndef DEBUG_LINE_SECTION_LABEL
3582 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3583 #endif
3584 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3585 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3586 #endif
3587 #ifndef DEBUG_INFO_SECTION_LABEL
3588 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3589 #endif
3590 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3591 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3592 #endif
3593 #ifndef DEBUG_ABBREV_SECTION_LABEL
3594 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3595 #endif
3596 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3597 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3598 #endif
3599 #ifndef DEBUG_ADDR_SECTION_LABEL
3600 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3601 #endif
3602 #ifndef DEBUG_LOC_SECTION_LABEL
3603 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3604 #endif
3605 #ifndef DEBUG_RANGES_SECTION_LABEL
3606 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3607 #endif
3608 #ifndef DEBUG_MACINFO_SECTION_LABEL
3609 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3610 #endif
3611 #ifndef DEBUG_MACRO_SECTION_LABEL
3612 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3613 #endif
3614 #define SKELETON_COMP_DIE_ABBREV 1
3615 #define SKELETON_TYPE_DIE_ABBREV 2
3617 /* Definitions of defaults for formats and names of various special
3618 (artificial) labels which may be generated within this file (when the -g
3619 options is used and DWARF2_DEBUGGING_INFO is in effect.
3620 If necessary, these may be overridden from within the tm.h file, but
3621 typically, overriding these defaults is unnecessary. */
3623 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3624 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3625 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3626 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3627 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3628 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3629 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3630 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3631 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3632 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3633 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3634 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3635 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3636 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3638 #ifndef TEXT_END_LABEL
3639 #define TEXT_END_LABEL "Letext"
3640 #endif
3641 #ifndef COLD_END_LABEL
3642 #define COLD_END_LABEL "Letext_cold"
3643 #endif
3644 #ifndef BLOCK_BEGIN_LABEL
3645 #define BLOCK_BEGIN_LABEL "LBB"
3646 #endif
3647 #ifndef BLOCK_END_LABEL
3648 #define BLOCK_END_LABEL "LBE"
3649 #endif
3650 #ifndef LINE_CODE_LABEL
3651 #define LINE_CODE_LABEL "LM"
3652 #endif
3655 /* Return the root of the DIE's built for the current compilation unit. */
3656 static dw_die_ref
3657 comp_unit_die (void)
3659 if (!single_comp_unit_die)
3660 single_comp_unit_die = gen_compile_unit_die (NULL);
3661 return single_comp_unit_die;
3664 /* We allow a language front-end to designate a function that is to be
3665 called to "demangle" any name before it is put into a DIE. */
3667 static const char *(*demangle_name_func) (const char *);
3669 void
3670 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3672 demangle_name_func = func;
3675 /* Test if rtl node points to a pseudo register. */
3677 static inline int
3678 is_pseudo_reg (const_rtx rtl)
3680 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3681 || (GET_CODE (rtl) == SUBREG
3682 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3685 /* Return a reference to a type, with its const and volatile qualifiers
3686 removed. */
3688 static inline tree
3689 type_main_variant (tree type)
3691 type = TYPE_MAIN_VARIANT (type);
3693 /* ??? There really should be only one main variant among any group of
3694 variants of a given type (and all of the MAIN_VARIANT values for all
3695 members of the group should point to that one type) but sometimes the C
3696 front-end messes this up for array types, so we work around that bug
3697 here. */
3698 if (TREE_CODE (type) == ARRAY_TYPE)
3699 while (type != TYPE_MAIN_VARIANT (type))
3700 type = TYPE_MAIN_VARIANT (type);
3702 return type;
3705 /* Return nonzero if the given type node represents a tagged type. */
3707 static inline int
3708 is_tagged_type (const_tree type)
3710 enum tree_code code = TREE_CODE (type);
3712 return (code == RECORD_TYPE || code == UNION_TYPE
3713 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3716 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3718 static void
3719 get_ref_die_offset_label (char *label, dw_die_ref ref)
3721 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3724 /* Return die_offset of a DIE reference to a base type. */
3726 static unsigned long int
3727 get_base_type_offset (dw_die_ref ref)
3729 if (ref->die_offset)
3730 return ref->die_offset;
3731 if (comp_unit_die ()->die_abbrev)
3733 calc_base_type_die_sizes ();
3734 gcc_assert (ref->die_offset);
3736 return ref->die_offset;
3739 /* Return die_offset of a DIE reference other than base type. */
3741 static unsigned long int
3742 get_ref_die_offset (dw_die_ref ref)
3744 gcc_assert (ref->die_offset);
3745 return ref->die_offset;
3748 /* Convert a DIE tag into its string name. */
3750 static const char *
3751 dwarf_tag_name (unsigned int tag)
3753 const char *name = get_DW_TAG_name (tag);
3755 if (name != NULL)
3756 return name;
3758 return "DW_TAG_<unknown>";
3761 /* Convert a DWARF attribute code into its string name. */
3763 static const char *
3764 dwarf_attr_name (unsigned int attr)
3766 const char *name;
3768 switch (attr)
3770 #if VMS_DEBUGGING_INFO
3771 case DW_AT_HP_prologue:
3772 return "DW_AT_HP_prologue";
3773 #else
3774 case DW_AT_MIPS_loop_unroll_factor:
3775 return "DW_AT_MIPS_loop_unroll_factor";
3776 #endif
3778 #if VMS_DEBUGGING_INFO
3779 case DW_AT_HP_epilogue:
3780 return "DW_AT_HP_epilogue";
3781 #else
3782 case DW_AT_MIPS_stride:
3783 return "DW_AT_MIPS_stride";
3784 #endif
3787 name = get_DW_AT_name (attr);
3789 if (name != NULL)
3790 return name;
3792 return "DW_AT_<unknown>";
3795 /* Convert a DWARF value form code into its string name. */
3797 static const char *
3798 dwarf_form_name (unsigned int form)
3800 const char *name = get_DW_FORM_name (form);
3802 if (name != NULL)
3803 return name;
3805 return "DW_FORM_<unknown>";
3808 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3809 instance of an inlined instance of a decl which is local to an inline
3810 function, so we have to trace all of the way back through the origin chain
3811 to find out what sort of node actually served as the original seed for the
3812 given block. */
3814 static tree
3815 decl_ultimate_origin (const_tree decl)
3817 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3818 return NULL_TREE;
3820 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3821 we're trying to output the abstract instance of this function. */
3822 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3823 return NULL_TREE;
3825 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3826 most distant ancestor, this should never happen. */
3827 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3829 return DECL_ABSTRACT_ORIGIN (decl);
3832 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3833 of a virtual function may refer to a base class, so we check the 'this'
3834 parameter. */
3836 static tree
3837 decl_class_context (tree decl)
3839 tree context = NULL_TREE;
3841 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3842 context = DECL_CONTEXT (decl);
3843 else
3844 context = TYPE_MAIN_VARIANT
3845 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3847 if (context && !TYPE_P (context))
3848 context = NULL_TREE;
3850 return context;
3853 /* Add an attribute/value pair to a DIE. */
3855 static inline void
3856 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3858 /* Maybe this should be an assert? */
3859 if (die == NULL)
3860 return;
3862 vec_safe_reserve (die->die_attr, 1);
3863 vec_safe_push (die->die_attr, *attr);
3866 static inline enum dw_val_class
3867 AT_class (dw_attr_node *a)
3869 return a->dw_attr_val.val_class;
3872 /* Return the index for any attribute that will be referenced with a
3873 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3874 are stored in dw_attr_val.v.val_str for reference counting
3875 pruning. */
3877 static inline unsigned int
3878 AT_index (dw_attr_node *a)
3880 if (AT_class (a) == dw_val_class_str)
3881 return a->dw_attr_val.v.val_str->index;
3882 else if (a->dw_attr_val.val_entry != NULL)
3883 return a->dw_attr_val.val_entry->index;
3884 return NOT_INDEXED;
3887 /* Add a flag value attribute to a DIE. */
3889 static inline void
3890 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3892 dw_attr_node attr;
3894 attr.dw_attr = attr_kind;
3895 attr.dw_attr_val.val_class = dw_val_class_flag;
3896 attr.dw_attr_val.val_entry = NULL;
3897 attr.dw_attr_val.v.val_flag = flag;
3898 add_dwarf_attr (die, &attr);
3901 static inline unsigned
3902 AT_flag (dw_attr_node *a)
3904 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3905 return a->dw_attr_val.v.val_flag;
3908 /* Add a signed integer attribute value to a DIE. */
3910 static inline void
3911 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3913 dw_attr_node attr;
3915 attr.dw_attr = attr_kind;
3916 attr.dw_attr_val.val_class = dw_val_class_const;
3917 attr.dw_attr_val.val_entry = NULL;
3918 attr.dw_attr_val.v.val_int = int_val;
3919 add_dwarf_attr (die, &attr);
3922 static inline HOST_WIDE_INT
3923 AT_int (dw_attr_node *a)
3925 gcc_assert (a && AT_class (a) == dw_val_class_const);
3926 return a->dw_attr_val.v.val_int;
3929 /* Add an unsigned integer attribute value to a DIE. */
3931 static inline void
3932 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3933 unsigned HOST_WIDE_INT unsigned_val)
3935 dw_attr_node attr;
3937 attr.dw_attr = attr_kind;
3938 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3939 attr.dw_attr_val.val_entry = NULL;
3940 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3941 add_dwarf_attr (die, &attr);
3944 static inline unsigned HOST_WIDE_INT
3945 AT_unsigned (dw_attr_node *a)
3947 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3948 return a->dw_attr_val.v.val_unsigned;
3951 /* Add an unsigned wide integer attribute value to a DIE. */
3953 static inline void
3954 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3955 const wide_int& w)
3957 dw_attr_node attr;
3959 attr.dw_attr = attr_kind;
3960 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3961 attr.dw_attr_val.val_entry = NULL;
3962 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3963 *attr.dw_attr_val.v.val_wide = w;
3964 add_dwarf_attr (die, &attr);
3967 /* Add an unsigned double integer attribute value to a DIE. */
3969 static inline void
3970 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3971 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3973 dw_attr_node attr;
3975 attr.dw_attr = attr_kind;
3976 attr.dw_attr_val.val_class = dw_val_class_const_double;
3977 attr.dw_attr_val.val_entry = NULL;
3978 attr.dw_attr_val.v.val_double.high = high;
3979 attr.dw_attr_val.v.val_double.low = low;
3980 add_dwarf_attr (die, &attr);
3983 /* Add a floating point attribute value to a DIE and return it. */
3985 static inline void
3986 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3987 unsigned int length, unsigned int elt_size, unsigned char *array)
3989 dw_attr_node attr;
3991 attr.dw_attr = attr_kind;
3992 attr.dw_attr_val.val_class = dw_val_class_vec;
3993 attr.dw_attr_val.val_entry = NULL;
3994 attr.dw_attr_val.v.val_vec.length = length;
3995 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3996 attr.dw_attr_val.v.val_vec.array = array;
3997 add_dwarf_attr (die, &attr);
4000 /* Add an 8-byte data attribute value to a DIE. */
4002 static inline void
4003 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4004 unsigned char data8[8])
4006 dw_attr_node attr;
4008 attr.dw_attr = attr_kind;
4009 attr.dw_attr_val.val_class = dw_val_class_data8;
4010 attr.dw_attr_val.val_entry = NULL;
4011 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4012 add_dwarf_attr (die, &attr);
4015 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4016 dwarf_split_debug_info, address attributes in dies destined for the
4017 final executable have force_direct set to avoid using indexed
4018 references. */
4020 static inline void
4021 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4022 bool force_direct)
4024 dw_attr_node attr;
4025 char * lbl_id;
4027 lbl_id = xstrdup (lbl_low);
4028 attr.dw_attr = DW_AT_low_pc;
4029 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4030 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4031 if (dwarf_split_debug_info && !force_direct)
4032 attr.dw_attr_val.val_entry
4033 = add_addr_table_entry (lbl_id, ate_kind_label);
4034 else
4035 attr.dw_attr_val.val_entry = NULL;
4036 add_dwarf_attr (die, &attr);
4038 attr.dw_attr = DW_AT_high_pc;
4039 if (dwarf_version < 4)
4040 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4041 else
4042 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4043 lbl_id = xstrdup (lbl_high);
4044 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4045 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4046 && dwarf_split_debug_info && !force_direct)
4047 attr.dw_attr_val.val_entry
4048 = add_addr_table_entry (lbl_id, ate_kind_label);
4049 else
4050 attr.dw_attr_val.val_entry = NULL;
4051 add_dwarf_attr (die, &attr);
4054 /* Hash and equality functions for debug_str_hash. */
4056 hashval_t
4057 indirect_string_hasher::hash (indirect_string_node *x)
4059 return htab_hash_string (x->str);
4062 bool
4063 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4065 return strcmp (x1->str, x2) == 0;
4068 /* Add STR to the given string hash table. */
4070 static struct indirect_string_node *
4071 find_AT_string_in_table (const char *str,
4072 hash_table<indirect_string_hasher> *table)
4074 struct indirect_string_node *node;
4076 indirect_string_node **slot
4077 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4078 if (*slot == NULL)
4080 node = ggc_cleared_alloc<indirect_string_node> ();
4081 node->str = ggc_strdup (str);
4082 *slot = node;
4084 else
4085 node = *slot;
4087 node->refcount++;
4088 return node;
4091 /* Add STR to the indirect string hash table. */
4093 static struct indirect_string_node *
4094 find_AT_string (const char *str)
4096 if (! debug_str_hash)
4097 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4099 return find_AT_string_in_table (str, debug_str_hash);
4102 /* Add a string attribute value to a DIE. */
4104 static inline void
4105 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4107 dw_attr_node attr;
4108 struct indirect_string_node *node;
4110 node = find_AT_string (str);
4112 attr.dw_attr = attr_kind;
4113 attr.dw_attr_val.val_class = dw_val_class_str;
4114 attr.dw_attr_val.val_entry = NULL;
4115 attr.dw_attr_val.v.val_str = node;
4116 add_dwarf_attr (die, &attr);
4119 static inline const char *
4120 AT_string (dw_attr_node *a)
4122 gcc_assert (a && AT_class (a) == dw_val_class_str);
4123 return a->dw_attr_val.v.val_str->str;
4126 /* Call this function directly to bypass AT_string_form's logic to put
4127 the string inline in the die. */
4129 static void
4130 set_indirect_string (struct indirect_string_node *node)
4132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4133 /* Already indirect is a no op. */
4134 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4136 gcc_assert (node->label);
4137 return;
4139 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4140 ++dw2_string_counter;
4141 node->label = xstrdup (label);
4143 if (!dwarf_split_debug_info)
4145 node->form = DW_FORM_strp;
4146 node->index = NOT_INDEXED;
4148 else
4150 node->form = DW_FORM_GNU_str_index;
4151 node->index = NO_INDEX_ASSIGNED;
4155 /* Find out whether a string should be output inline in DIE
4156 or out-of-line in .debug_str section. */
4158 static enum dwarf_form
4159 find_string_form (struct indirect_string_node *node)
4161 unsigned int len;
4163 if (node->form)
4164 return node->form;
4166 len = strlen (node->str) + 1;
4168 /* If the string is shorter or equal to the size of the reference, it is
4169 always better to put it inline. */
4170 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4171 return node->form = DW_FORM_string;
4173 /* If we cannot expect the linker to merge strings in .debug_str
4174 section, only put it into .debug_str if it is worth even in this
4175 single module. */
4176 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4177 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4178 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4179 return node->form = DW_FORM_string;
4181 set_indirect_string (node);
4183 return node->form;
4186 /* Find out whether the string referenced from the attribute should be
4187 output inline in DIE or out-of-line in .debug_str section. */
4189 static enum dwarf_form
4190 AT_string_form (dw_attr_node *a)
4192 gcc_assert (a && AT_class (a) == dw_val_class_str);
4193 return find_string_form (a->dw_attr_val.v.val_str);
4196 /* Add a DIE reference attribute value to a DIE. */
4198 static inline void
4199 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4201 dw_attr_node attr;
4202 gcc_checking_assert (targ_die != NULL);
4204 /* With LTO we can end up trying to reference something we didn't create
4205 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4206 if (targ_die == NULL)
4207 return;
4209 attr.dw_attr = attr_kind;
4210 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4211 attr.dw_attr_val.val_entry = NULL;
4212 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4213 attr.dw_attr_val.v.val_die_ref.external = 0;
4214 add_dwarf_attr (die, &attr);
4217 /* Change DIE reference REF to point to NEW_DIE instead. */
4219 static inline void
4220 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4222 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4223 ref->dw_attr_val.v.val_die_ref.die = new_die;
4224 ref->dw_attr_val.v.val_die_ref.external = 0;
4227 /* Add an AT_specification attribute to a DIE, and also make the back
4228 pointer from the specification to the definition. */
4230 static inline void
4231 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4233 add_AT_die_ref (die, DW_AT_specification, targ_die);
4234 gcc_assert (!targ_die->die_definition);
4235 targ_die->die_definition = die;
4238 static inline dw_die_ref
4239 AT_ref (dw_attr_node *a)
4241 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4242 return a->dw_attr_val.v.val_die_ref.die;
4245 static inline int
4246 AT_ref_external (dw_attr_node *a)
4248 if (a && AT_class (a) == dw_val_class_die_ref)
4249 return a->dw_attr_val.v.val_die_ref.external;
4251 return 0;
4254 static inline void
4255 set_AT_ref_external (dw_attr_node *a, int i)
4257 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4258 a->dw_attr_val.v.val_die_ref.external = i;
4261 /* Add an FDE reference attribute value to a DIE. */
4263 static inline void
4264 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4266 dw_attr_node attr;
4268 attr.dw_attr = attr_kind;
4269 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4270 attr.dw_attr_val.val_entry = NULL;
4271 attr.dw_attr_val.v.val_fde_index = targ_fde;
4272 add_dwarf_attr (die, &attr);
4275 /* Add a location description attribute value to a DIE. */
4277 static inline void
4278 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4280 dw_attr_node attr;
4282 attr.dw_attr = attr_kind;
4283 attr.dw_attr_val.val_class = dw_val_class_loc;
4284 attr.dw_attr_val.val_entry = NULL;
4285 attr.dw_attr_val.v.val_loc = loc;
4286 add_dwarf_attr (die, &attr);
4289 static inline dw_loc_descr_ref
4290 AT_loc (dw_attr_node *a)
4292 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4293 return a->dw_attr_val.v.val_loc;
4296 static inline void
4297 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4299 dw_attr_node attr;
4301 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4302 return;
4304 attr.dw_attr = attr_kind;
4305 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4306 attr.dw_attr_val.val_entry = NULL;
4307 attr.dw_attr_val.v.val_loc_list = loc_list;
4308 add_dwarf_attr (die, &attr);
4309 have_location_lists = true;
4312 static inline dw_loc_list_ref
4313 AT_loc_list (dw_attr_node *a)
4315 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4316 return a->dw_attr_val.v.val_loc_list;
4319 static inline dw_loc_list_ref *
4320 AT_loc_list_ptr (dw_attr_node *a)
4322 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4323 return &a->dw_attr_val.v.val_loc_list;
4326 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4328 static hashval_t hash (addr_table_entry *);
4329 static bool equal (addr_table_entry *, addr_table_entry *);
4332 /* Table of entries into the .debug_addr section. */
4334 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4336 /* Hash an address_table_entry. */
4338 hashval_t
4339 addr_hasher::hash (addr_table_entry *a)
4341 inchash::hash hstate;
4342 switch (a->kind)
4344 case ate_kind_rtx:
4345 hstate.add_int (0);
4346 break;
4347 case ate_kind_rtx_dtprel:
4348 hstate.add_int (1);
4349 break;
4350 case ate_kind_label:
4351 return htab_hash_string (a->addr.label);
4352 default:
4353 gcc_unreachable ();
4355 inchash::add_rtx (a->addr.rtl, hstate);
4356 return hstate.end ();
4359 /* Determine equality for two address_table_entries. */
4361 bool
4362 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4364 if (a1->kind != a2->kind)
4365 return 0;
4366 switch (a1->kind)
4368 case ate_kind_rtx:
4369 case ate_kind_rtx_dtprel:
4370 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4371 case ate_kind_label:
4372 return strcmp (a1->addr.label, a2->addr.label) == 0;
4373 default:
4374 gcc_unreachable ();
4378 /* Initialize an addr_table_entry. */
4380 void
4381 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4383 e->kind = kind;
4384 switch (kind)
4386 case ate_kind_rtx:
4387 case ate_kind_rtx_dtprel:
4388 e->addr.rtl = (rtx) addr;
4389 break;
4390 case ate_kind_label:
4391 e->addr.label = (char *) addr;
4392 break;
4394 e->refcount = 0;
4395 e->index = NO_INDEX_ASSIGNED;
4398 /* Add attr to the address table entry to the table. Defer setting an
4399 index until output time. */
4401 static addr_table_entry *
4402 add_addr_table_entry (void *addr, enum ate_kind kind)
4404 addr_table_entry *node;
4405 addr_table_entry finder;
4407 gcc_assert (dwarf_split_debug_info);
4408 if (! addr_index_table)
4409 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4410 init_addr_table_entry (&finder, kind, addr);
4411 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4413 if (*slot == HTAB_EMPTY_ENTRY)
4415 node = ggc_cleared_alloc<addr_table_entry> ();
4416 init_addr_table_entry (node, kind, addr);
4417 *slot = node;
4419 else
4420 node = *slot;
4422 node->refcount++;
4423 return node;
4426 /* Remove an entry from the addr table by decrementing its refcount.
4427 Strictly, decrementing the refcount would be enough, but the
4428 assertion that the entry is actually in the table has found
4429 bugs. */
4431 static void
4432 remove_addr_table_entry (addr_table_entry *entry)
4434 gcc_assert (dwarf_split_debug_info && addr_index_table);
4435 /* After an index is assigned, the table is frozen. */
4436 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4437 entry->refcount--;
4440 /* Given a location list, remove all addresses it refers to from the
4441 address_table. */
4443 static void
4444 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4446 for (; descr; descr = descr->dw_loc_next)
4447 if (descr->dw_loc_oprnd1.val_entry != NULL)
4449 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4450 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4454 /* A helper function for dwarf2out_finish called through
4455 htab_traverse. Assign an addr_table_entry its index. All entries
4456 must be collected into the table when this function is called,
4457 because the indexing code relies on htab_traverse to traverse nodes
4458 in the same order for each run. */
4461 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4463 addr_table_entry *node = *h;
4465 /* Don't index unreferenced nodes. */
4466 if (node->refcount == 0)
4467 return 1;
4469 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4470 node->index = *index;
4471 *index += 1;
4473 return 1;
4476 /* Add an address constant attribute value to a DIE. When using
4477 dwarf_split_debug_info, address attributes in dies destined for the
4478 final executable should be direct references--setting the parameter
4479 force_direct ensures this behavior. */
4481 static inline void
4482 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4483 bool force_direct)
4485 dw_attr_node attr;
4487 attr.dw_attr = attr_kind;
4488 attr.dw_attr_val.val_class = dw_val_class_addr;
4489 attr.dw_attr_val.v.val_addr = addr;
4490 if (dwarf_split_debug_info && !force_direct)
4491 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4492 else
4493 attr.dw_attr_val.val_entry = NULL;
4494 add_dwarf_attr (die, &attr);
4497 /* Get the RTX from to an address DIE attribute. */
4499 static inline rtx
4500 AT_addr (dw_attr_node *a)
4502 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4503 return a->dw_attr_val.v.val_addr;
4506 /* Add a file attribute value to a DIE. */
4508 static inline void
4509 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4510 struct dwarf_file_data *fd)
4512 dw_attr_node attr;
4514 attr.dw_attr = attr_kind;
4515 attr.dw_attr_val.val_class = dw_val_class_file;
4516 attr.dw_attr_val.val_entry = NULL;
4517 attr.dw_attr_val.v.val_file = fd;
4518 add_dwarf_attr (die, &attr);
4521 /* Get the dwarf_file_data from a file DIE attribute. */
4523 static inline struct dwarf_file_data *
4524 AT_file (dw_attr_node *a)
4526 gcc_assert (a && AT_class (a) == dw_val_class_file);
4527 return a->dw_attr_val.v.val_file;
4530 /* Add a vms delta attribute value to a DIE. */
4532 static inline void
4533 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4534 const char *lbl1, const char *lbl2)
4536 dw_attr_node attr;
4538 attr.dw_attr = attr_kind;
4539 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4540 attr.dw_attr_val.val_entry = NULL;
4541 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4542 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4543 add_dwarf_attr (die, &attr);
4546 /* Add a label identifier attribute value to a DIE. */
4548 static inline void
4549 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4550 const char *lbl_id)
4552 dw_attr_node attr;
4554 attr.dw_attr = attr_kind;
4555 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4556 attr.dw_attr_val.val_entry = NULL;
4557 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4558 if (dwarf_split_debug_info)
4559 attr.dw_attr_val.val_entry
4560 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4561 ate_kind_label);
4562 add_dwarf_attr (die, &attr);
4565 /* Add a section offset attribute value to a DIE, an offset into the
4566 debug_line section. */
4568 static inline void
4569 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4570 const char *label)
4572 dw_attr_node attr;
4574 attr.dw_attr = attr_kind;
4575 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4576 attr.dw_attr_val.val_entry = NULL;
4577 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4578 add_dwarf_attr (die, &attr);
4581 /* Add a section offset attribute value to a DIE, an offset into the
4582 debug_macinfo section. */
4584 static inline void
4585 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4586 const char *label)
4588 dw_attr_node attr;
4590 attr.dw_attr = attr_kind;
4591 attr.dw_attr_val.val_class = dw_val_class_macptr;
4592 attr.dw_attr_val.val_entry = NULL;
4593 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4594 add_dwarf_attr (die, &attr);
4597 /* Add an offset attribute value to a DIE. */
4599 static inline void
4600 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4601 unsigned HOST_WIDE_INT offset)
4603 dw_attr_node attr;
4605 attr.dw_attr = attr_kind;
4606 attr.dw_attr_val.val_class = dw_val_class_offset;
4607 attr.dw_attr_val.val_entry = NULL;
4608 attr.dw_attr_val.v.val_offset = offset;
4609 add_dwarf_attr (die, &attr);
4612 /* Add a range_list attribute value to a DIE. When using
4613 dwarf_split_debug_info, address attributes in dies destined for the
4614 final executable should be direct references--setting the parameter
4615 force_direct ensures this behavior. */
4617 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4618 #define RELOCATED_OFFSET (NULL)
4620 static void
4621 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4622 long unsigned int offset, bool force_direct)
4624 dw_attr_node attr;
4626 attr.dw_attr = attr_kind;
4627 attr.dw_attr_val.val_class = dw_val_class_range_list;
4628 /* For the range_list attribute, use val_entry to store whether the
4629 offset should follow split-debug-info or normal semantics. This
4630 value is read in output_range_list_offset. */
4631 if (dwarf_split_debug_info && !force_direct)
4632 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4633 else
4634 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4635 attr.dw_attr_val.v.val_offset = offset;
4636 add_dwarf_attr (die, &attr);
4639 /* Return the start label of a delta attribute. */
4641 static inline const char *
4642 AT_vms_delta1 (dw_attr_node *a)
4644 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4645 return a->dw_attr_val.v.val_vms_delta.lbl1;
4648 /* Return the end label of a delta attribute. */
4650 static inline const char *
4651 AT_vms_delta2 (dw_attr_node *a)
4653 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4654 return a->dw_attr_val.v.val_vms_delta.lbl2;
4657 static inline const char *
4658 AT_lbl (dw_attr_node *a)
4660 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4661 || AT_class (a) == dw_val_class_lineptr
4662 || AT_class (a) == dw_val_class_macptr
4663 || AT_class (a) == dw_val_class_high_pc));
4664 return a->dw_attr_val.v.val_lbl_id;
4667 /* Get the attribute of type attr_kind. */
4669 static dw_attr_node *
4670 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4672 dw_attr_node *a;
4673 unsigned ix;
4674 dw_die_ref spec = NULL;
4676 if (! die)
4677 return NULL;
4679 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4680 if (a->dw_attr == attr_kind)
4681 return a;
4682 else if (a->dw_attr == DW_AT_specification
4683 || a->dw_attr == DW_AT_abstract_origin)
4684 spec = AT_ref (a);
4686 if (spec)
4687 return get_AT (spec, attr_kind);
4689 return NULL;
4692 /* Returns the parent of the declaration of DIE. */
4694 static dw_die_ref
4695 get_die_parent (dw_die_ref die)
4697 dw_die_ref t;
4699 if (!die)
4700 return NULL;
4702 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4703 || (t = get_AT_ref (die, DW_AT_specification)))
4704 die = t;
4706 return die->die_parent;
4709 /* Return the "low pc" attribute value, typically associated with a subprogram
4710 DIE. Return null if the "low pc" attribute is either not present, or if it
4711 cannot be represented as an assembler label identifier. */
4713 static inline const char *
4714 get_AT_low_pc (dw_die_ref die)
4716 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4718 return a ? AT_lbl (a) : NULL;
4721 /* Return the "high pc" attribute value, typically associated with a subprogram
4722 DIE. Return null if the "high pc" attribute is either not present, or if it
4723 cannot be represented as an assembler label identifier. */
4725 static inline const char *
4726 get_AT_hi_pc (dw_die_ref die)
4728 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4730 return a ? AT_lbl (a) : NULL;
4733 /* Return the value of the string attribute designated by ATTR_KIND, or
4734 NULL if it is not present. */
4736 static inline const char *
4737 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4739 dw_attr_node *a = get_AT (die, attr_kind);
4741 return a ? AT_string (a) : NULL;
4744 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4745 if it is not present. */
4747 static inline int
4748 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4750 dw_attr_node *a = get_AT (die, attr_kind);
4752 return a ? AT_flag (a) : 0;
4755 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4756 if it is not present. */
4758 static inline unsigned
4759 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4761 dw_attr_node *a = get_AT (die, attr_kind);
4763 return a ? AT_unsigned (a) : 0;
4766 static inline dw_die_ref
4767 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4769 dw_attr_node *a = get_AT (die, attr_kind);
4771 return a ? AT_ref (a) : NULL;
4774 static inline struct dwarf_file_data *
4775 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4777 dw_attr_node *a = get_AT (die, attr_kind);
4779 return a ? AT_file (a) : NULL;
4782 /* Return TRUE if the language is C++. */
4784 static inline bool
4785 is_cxx (void)
4787 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4789 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4790 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4793 /* Return TRUE if the language is Java. */
4795 static inline bool
4796 is_java (void)
4798 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4800 return lang == DW_LANG_Java;
4803 /* Return TRUE if the language is Fortran. */
4805 static inline bool
4806 is_fortran (void)
4808 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4810 return (lang == DW_LANG_Fortran77
4811 || lang == DW_LANG_Fortran90
4812 || lang == DW_LANG_Fortran95
4813 || lang == DW_LANG_Fortran03
4814 || lang == DW_LANG_Fortran08);
4817 /* Return TRUE if the language is Ada. */
4819 static inline bool
4820 is_ada (void)
4822 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4824 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4827 /* Remove the specified attribute if present. Return TRUE if removal
4828 was successful. */
4830 static bool
4831 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4833 dw_attr_node *a;
4834 unsigned ix;
4836 if (! die)
4837 return false;
4839 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4840 if (a->dw_attr == attr_kind)
4842 if (AT_class (a) == dw_val_class_str)
4843 if (a->dw_attr_val.v.val_str->refcount)
4844 a->dw_attr_val.v.val_str->refcount--;
4846 /* vec::ordered_remove should help reduce the number of abbrevs
4847 that are needed. */
4848 die->die_attr->ordered_remove (ix);
4849 return true;
4851 return false;
4854 /* Remove CHILD from its parent. PREV must have the property that
4855 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4857 static void
4858 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4860 gcc_assert (child->die_parent == prev->die_parent);
4861 gcc_assert (prev->die_sib == child);
4862 if (prev == child)
4864 gcc_assert (child->die_parent->die_child == child);
4865 prev = NULL;
4867 else
4868 prev->die_sib = child->die_sib;
4869 if (child->die_parent->die_child == child)
4870 child->die_parent->die_child = prev;
4871 child->die_sib = NULL;
4874 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4875 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4877 static void
4878 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4880 dw_die_ref parent = old_child->die_parent;
4882 gcc_assert (parent == prev->die_parent);
4883 gcc_assert (prev->die_sib == old_child);
4885 new_child->die_parent = parent;
4886 if (prev == old_child)
4888 gcc_assert (parent->die_child == old_child);
4889 new_child->die_sib = new_child;
4891 else
4893 prev->die_sib = new_child;
4894 new_child->die_sib = old_child->die_sib;
4896 if (old_child->die_parent->die_child == old_child)
4897 old_child->die_parent->die_child = new_child;
4898 old_child->die_sib = NULL;
4901 /* Move all children from OLD_PARENT to NEW_PARENT. */
4903 static void
4904 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4906 dw_die_ref c;
4907 new_parent->die_child = old_parent->die_child;
4908 old_parent->die_child = NULL;
4909 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4912 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4913 matches TAG. */
4915 static void
4916 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4918 dw_die_ref c;
4920 c = die->die_child;
4921 if (c) do {
4922 dw_die_ref prev = c;
4923 c = c->die_sib;
4924 while (c->die_tag == tag)
4926 remove_child_with_prev (c, prev);
4927 c->die_parent = NULL;
4928 /* Might have removed every child. */
4929 if (die->die_child == NULL)
4930 return;
4931 c = prev->die_sib;
4933 } while (c != die->die_child);
4936 /* Add a CHILD_DIE as the last child of DIE. */
4938 static void
4939 add_child_die (dw_die_ref die, dw_die_ref child_die)
4941 /* FIXME this should probably be an assert. */
4942 if (! die || ! child_die)
4943 return;
4944 gcc_assert (die != child_die);
4946 child_die->die_parent = die;
4947 if (die->die_child)
4949 child_die->die_sib = die->die_child->die_sib;
4950 die->die_child->die_sib = child_die;
4952 else
4953 child_die->die_sib = child_die;
4954 die->die_child = child_die;
4957 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4959 static void
4960 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4961 dw_die_ref after_die)
4963 gcc_assert (die
4964 && child_die
4965 && after_die
4966 && die->die_child
4967 && die != child_die);
4969 child_die->die_parent = die;
4970 child_die->die_sib = after_die->die_sib;
4971 after_die->die_sib = child_die;
4972 if (die->die_child == after_die)
4973 die->die_child = child_die;
4976 /* Unassociate CHILD from its parent, and make its parent be
4977 NEW_PARENT. */
4979 static void
4980 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4982 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4983 if (p->die_sib == child)
4985 remove_child_with_prev (child, p);
4986 break;
4988 add_child_die (new_parent, child);
4991 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4992 is the specification, to the end of PARENT's list of children.
4993 This is done by removing and re-adding it. */
4995 static void
4996 splice_child_die (dw_die_ref parent, dw_die_ref child)
4998 /* We want the declaration DIE from inside the class, not the
4999 specification DIE at toplevel. */
5000 if (child->die_parent != parent)
5002 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5004 if (tmp)
5005 child = tmp;
5008 gcc_assert (child->die_parent == parent
5009 || (child->die_parent
5010 == get_AT_ref (parent, DW_AT_specification)));
5012 reparent_child (child, parent);
5015 /* Create and return a new die with a parent of PARENT_DIE. If
5016 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5017 associated tree T must be supplied to determine parenthood
5018 later. */
5020 static inline dw_die_ref
5021 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5023 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5025 die->die_tag = tag_value;
5027 if (parent_die != NULL)
5028 add_child_die (parent_die, die);
5029 else
5031 limbo_die_node *limbo_node;
5033 /* No DIEs created after early dwarf should end up in limbo,
5034 because the limbo list should not persist past LTO
5035 streaming. */
5036 if (tag_value != DW_TAG_compile_unit
5037 /* These are allowed because they're generated while
5038 breaking out COMDAT units late. */
5039 && tag_value != DW_TAG_type_unit
5040 && !early_dwarf
5041 /* Allow nested functions to live in limbo because they will
5042 only temporarily live there, as decls_for_scope will fix
5043 them up. */
5044 && (TREE_CODE (t) != FUNCTION_DECL
5045 || !decl_function_context (t))
5046 /* Same as nested functions above but for types. Types that
5047 are local to a function will be fixed in
5048 decls_for_scope. */
5049 && (!RECORD_OR_UNION_TYPE_P (t)
5050 || !TYPE_CONTEXT (t)
5051 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5052 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5053 especially in the ltrans stage, but once we implement LTO
5054 dwarf streaming, we should remove this exception. */
5055 && !in_lto_p)
5057 fprintf (stderr, "symbol ended up in limbo too late:");
5058 debug_generic_stmt (t);
5059 gcc_unreachable ();
5062 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5063 limbo_node->die = die;
5064 limbo_node->created_for = t;
5065 limbo_node->next = limbo_die_list;
5066 limbo_die_list = limbo_node;
5069 return die;
5072 /* Return the DIE associated with the given type specifier. */
5074 static inline dw_die_ref
5075 lookup_type_die (tree type)
5077 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5078 if (die && die->removed)
5080 TYPE_SYMTAB_DIE (type) = NULL;
5081 return NULL;
5083 return die;
5086 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5087 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5088 anonymous type instead the one of the naming typedef. */
5090 static inline dw_die_ref
5091 strip_naming_typedef (tree type, dw_die_ref type_die)
5093 if (type
5094 && TREE_CODE (type) == RECORD_TYPE
5095 && type_die
5096 && type_die->die_tag == DW_TAG_typedef
5097 && is_naming_typedef_decl (TYPE_NAME (type)))
5098 type_die = get_AT_ref (type_die, DW_AT_type);
5099 return type_die;
5102 /* Like lookup_type_die, but if type is an anonymous type named by a
5103 typedef[1], return the DIE of the anonymous type instead the one of
5104 the naming typedef. This is because in gen_typedef_die, we did
5105 equate the anonymous struct named by the typedef with the DIE of
5106 the naming typedef. So by default, lookup_type_die on an anonymous
5107 struct yields the DIE of the naming typedef.
5109 [1]: Read the comment of is_naming_typedef_decl to learn about what
5110 a naming typedef is. */
5112 static inline dw_die_ref
5113 lookup_type_die_strip_naming_typedef (tree type)
5115 dw_die_ref die = lookup_type_die (type);
5116 return strip_naming_typedef (type, die);
5119 /* Equate a DIE to a given type specifier. */
5121 static inline void
5122 equate_type_number_to_die (tree type, dw_die_ref type_die)
5124 TYPE_SYMTAB_DIE (type) = type_die;
5127 /* Returns a hash value for X (which really is a die_struct). */
5129 inline hashval_t
5130 decl_die_hasher::hash (die_node *x)
5132 return (hashval_t) x->decl_id;
5135 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5137 inline bool
5138 decl_die_hasher::equal (die_node *x, tree y)
5140 return (x->decl_id == DECL_UID (y));
5143 /* Return the DIE associated with a given declaration. */
5145 static inline dw_die_ref
5146 lookup_decl_die (tree decl)
5148 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5149 NO_INSERT);
5150 if (!die)
5151 return NULL;
5152 if ((*die)->removed)
5154 decl_die_table->clear_slot (die);
5155 return NULL;
5157 return *die;
5160 /* Returns a hash value for X (which really is a var_loc_list). */
5162 inline hashval_t
5163 decl_loc_hasher::hash (var_loc_list *x)
5165 return (hashval_t) x->decl_id;
5168 /* Return nonzero if decl_id of var_loc_list X is the same as
5169 UID of decl *Y. */
5171 inline bool
5172 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5174 return (x->decl_id == DECL_UID (y));
5177 /* Return the var_loc list associated with a given declaration. */
5179 static inline var_loc_list *
5180 lookup_decl_loc (const_tree decl)
5182 if (!decl_loc_table)
5183 return NULL;
5184 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5187 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5189 inline hashval_t
5190 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5192 return (hashval_t) x->decl_id;
5195 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5196 UID of decl *Y. */
5198 inline bool
5199 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5201 return (x->decl_id == DECL_UID (y));
5204 /* Equate a DIE to a particular declaration. */
5206 static void
5207 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5209 unsigned int decl_id = DECL_UID (decl);
5211 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5212 decl_die->decl_id = decl_id;
5215 /* Return how many bits covers PIECE EXPR_LIST. */
5217 static HOST_WIDE_INT
5218 decl_piece_bitsize (rtx piece)
5220 int ret = (int) GET_MODE (piece);
5221 if (ret)
5222 return ret;
5223 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5224 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5225 return INTVAL (XEXP (XEXP (piece, 0), 0));
5228 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5230 static rtx *
5231 decl_piece_varloc_ptr (rtx piece)
5233 if ((int) GET_MODE (piece))
5234 return &XEXP (piece, 0);
5235 else
5236 return &XEXP (XEXP (piece, 0), 1);
5239 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5240 Next is the chain of following piece nodes. */
5242 static rtx_expr_list *
5243 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5245 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5246 return alloc_EXPR_LIST (bitsize, loc_note, next);
5247 else
5248 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5249 GEN_INT (bitsize),
5250 loc_note), next);
5253 /* Return rtx that should be stored into loc field for
5254 LOC_NOTE and BITPOS/BITSIZE. */
5256 static rtx
5257 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5258 HOST_WIDE_INT bitsize)
5260 if (bitsize != -1)
5262 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5263 if (bitpos != 0)
5264 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5266 return loc_note;
5269 /* This function either modifies location piece list *DEST in
5270 place (if SRC and INNER is NULL), or copies location piece list
5271 *SRC to *DEST while modifying it. Location BITPOS is modified
5272 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5273 not copied and if needed some padding around it is added.
5274 When modifying in place, DEST should point to EXPR_LIST where
5275 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5276 to the start of the whole list and INNER points to the EXPR_LIST
5277 where earlier pieces cover PIECE_BITPOS bits. */
5279 static void
5280 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5281 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5282 HOST_WIDE_INT bitsize, rtx loc_note)
5284 HOST_WIDE_INT diff;
5285 bool copy = inner != NULL;
5287 if (copy)
5289 /* First copy all nodes preceding the current bitpos. */
5290 while (src != inner)
5292 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5293 decl_piece_bitsize (*src), NULL_RTX);
5294 dest = &XEXP (*dest, 1);
5295 src = &XEXP (*src, 1);
5298 /* Add padding if needed. */
5299 if (bitpos != piece_bitpos)
5301 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5302 copy ? NULL_RTX : *dest);
5303 dest = &XEXP (*dest, 1);
5305 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5307 gcc_assert (!copy);
5308 /* A piece with correct bitpos and bitsize already exist,
5309 just update the location for it and return. */
5310 *decl_piece_varloc_ptr (*dest) = loc_note;
5311 return;
5313 /* Add the piece that changed. */
5314 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5315 dest = &XEXP (*dest, 1);
5316 /* Skip over pieces that overlap it. */
5317 diff = bitpos - piece_bitpos + bitsize;
5318 if (!copy)
5319 src = dest;
5320 while (diff > 0 && *src)
5322 rtx piece = *src;
5323 diff -= decl_piece_bitsize (piece);
5324 if (copy)
5325 src = &XEXP (piece, 1);
5326 else
5328 *src = XEXP (piece, 1);
5329 free_EXPR_LIST_node (piece);
5332 /* Add padding if needed. */
5333 if (diff < 0 && *src)
5335 if (!copy)
5336 dest = src;
5337 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5338 dest = &XEXP (*dest, 1);
5340 if (!copy)
5341 return;
5342 /* Finally copy all nodes following it. */
5343 while (*src)
5345 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5346 decl_piece_bitsize (*src), NULL_RTX);
5347 dest = &XEXP (*dest, 1);
5348 src = &XEXP (*src, 1);
5352 /* Add a variable location node to the linked list for DECL. */
5354 static struct var_loc_node *
5355 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5357 unsigned int decl_id;
5358 var_loc_list *temp;
5359 struct var_loc_node *loc = NULL;
5360 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5362 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5364 tree realdecl = DECL_DEBUG_EXPR (decl);
5365 if (handled_component_p (realdecl)
5366 || (TREE_CODE (realdecl) == MEM_REF
5367 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5369 HOST_WIDE_INT maxsize;
5370 bool reverse;
5371 tree innerdecl
5372 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5373 &reverse);
5374 if (!DECL_P (innerdecl)
5375 || DECL_IGNORED_P (innerdecl)
5376 || TREE_STATIC (innerdecl)
5377 || bitsize <= 0
5378 || bitpos + bitsize > 256
5379 || bitsize != maxsize)
5380 return NULL;
5381 decl = innerdecl;
5385 decl_id = DECL_UID (decl);
5386 var_loc_list **slot
5387 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5388 if (*slot == NULL)
5390 temp = ggc_cleared_alloc<var_loc_list> ();
5391 temp->decl_id = decl_id;
5392 *slot = temp;
5394 else
5395 temp = *slot;
5397 /* For PARM_DECLs try to keep around the original incoming value,
5398 even if that means we'll emit a zero-range .debug_loc entry. */
5399 if (temp->last
5400 && temp->first == temp->last
5401 && TREE_CODE (decl) == PARM_DECL
5402 && NOTE_P (temp->first->loc)
5403 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5404 && DECL_INCOMING_RTL (decl)
5405 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5406 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5407 == GET_CODE (DECL_INCOMING_RTL (decl))
5408 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5409 && (bitsize != -1
5410 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5411 NOTE_VAR_LOCATION_LOC (loc_note))
5412 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5413 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5415 loc = ggc_cleared_alloc<var_loc_node> ();
5416 temp->first->next = loc;
5417 temp->last = loc;
5418 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5420 else if (temp->last)
5422 struct var_loc_node *last = temp->last, *unused = NULL;
5423 rtx *piece_loc = NULL, last_loc_note;
5424 HOST_WIDE_INT piece_bitpos = 0;
5425 if (last->next)
5427 last = last->next;
5428 gcc_assert (last->next == NULL);
5430 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5432 piece_loc = &last->loc;
5435 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5436 if (piece_bitpos + cur_bitsize > bitpos)
5437 break;
5438 piece_bitpos += cur_bitsize;
5439 piece_loc = &XEXP (*piece_loc, 1);
5441 while (*piece_loc);
5443 /* TEMP->LAST here is either pointer to the last but one or
5444 last element in the chained list, LAST is pointer to the
5445 last element. */
5446 if (label && strcmp (last->label, label) == 0)
5448 /* For SRA optimized variables if there weren't any real
5449 insns since last note, just modify the last node. */
5450 if (piece_loc != NULL)
5452 adjust_piece_list (piece_loc, NULL, NULL,
5453 bitpos, piece_bitpos, bitsize, loc_note);
5454 return NULL;
5456 /* If the last note doesn't cover any instructions, remove it. */
5457 if (temp->last != last)
5459 temp->last->next = NULL;
5460 unused = last;
5461 last = temp->last;
5462 gcc_assert (strcmp (last->label, label) != 0);
5464 else
5466 gcc_assert (temp->first == temp->last
5467 || (temp->first->next == temp->last
5468 && TREE_CODE (decl) == PARM_DECL));
5469 memset (temp->last, '\0', sizeof (*temp->last));
5470 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5471 return temp->last;
5474 if (bitsize == -1 && NOTE_P (last->loc))
5475 last_loc_note = last->loc;
5476 else if (piece_loc != NULL
5477 && *piece_loc != NULL_RTX
5478 && piece_bitpos == bitpos
5479 && decl_piece_bitsize (*piece_loc) == bitsize)
5480 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5481 else
5482 last_loc_note = NULL_RTX;
5483 /* If the current location is the same as the end of the list,
5484 and either both or neither of the locations is uninitialized,
5485 we have nothing to do. */
5486 if (last_loc_note == NULL_RTX
5487 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5488 NOTE_VAR_LOCATION_LOC (loc_note)))
5489 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5490 != NOTE_VAR_LOCATION_STATUS (loc_note))
5491 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5492 == VAR_INIT_STATUS_UNINITIALIZED)
5493 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5494 == VAR_INIT_STATUS_UNINITIALIZED))))
5496 /* Add LOC to the end of list and update LAST. If the last
5497 element of the list has been removed above, reuse its
5498 memory for the new node, otherwise allocate a new one. */
5499 if (unused)
5501 loc = unused;
5502 memset (loc, '\0', sizeof (*loc));
5504 else
5505 loc = ggc_cleared_alloc<var_loc_node> ();
5506 if (bitsize == -1 || piece_loc == NULL)
5507 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5508 else
5509 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5510 bitpos, piece_bitpos, bitsize, loc_note);
5511 last->next = loc;
5512 /* Ensure TEMP->LAST will point either to the new last but one
5513 element of the chain, or to the last element in it. */
5514 if (last != temp->last)
5515 temp->last = last;
5517 else if (unused)
5518 ggc_free (unused);
5520 else
5522 loc = ggc_cleared_alloc<var_loc_node> ();
5523 temp->first = loc;
5524 temp->last = loc;
5525 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5527 return loc;
5530 /* Keep track of the number of spaces used to indent the
5531 output of the debugging routines that print the structure of
5532 the DIE internal representation. */
5533 static int print_indent;
5535 /* Indent the line the number of spaces given by print_indent. */
5537 static inline void
5538 print_spaces (FILE *outfile)
5540 fprintf (outfile, "%*s", print_indent, "");
5543 /* Print a type signature in hex. */
5545 static inline void
5546 print_signature (FILE *outfile, char *sig)
5548 int i;
5550 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5551 fprintf (outfile, "%02x", sig[i] & 0xff);
5554 static inline void
5555 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5557 if (discr_value->pos)
5558 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5559 else
5560 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5563 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5565 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5566 RECURSE, output location descriptor operations. */
5568 static void
5569 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5571 switch (val->val_class)
5573 case dw_val_class_addr:
5574 fprintf (outfile, "address");
5575 break;
5576 case dw_val_class_offset:
5577 fprintf (outfile, "offset");
5578 break;
5579 case dw_val_class_loc:
5580 fprintf (outfile, "location descriptor");
5581 if (val->v.val_loc == NULL)
5582 fprintf (outfile, " -> <null>\n");
5583 else if (recurse)
5585 fprintf (outfile, ":\n");
5586 print_indent += 4;
5587 print_loc_descr (val->v.val_loc, outfile);
5588 print_indent -= 4;
5590 else
5591 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5592 break;
5593 case dw_val_class_loc_list:
5594 fprintf (outfile, "location list -> label:%s",
5595 val->v.val_loc_list->ll_symbol);
5596 break;
5597 case dw_val_class_range_list:
5598 fprintf (outfile, "range list");
5599 break;
5600 case dw_val_class_const:
5601 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5602 break;
5603 case dw_val_class_unsigned_const:
5604 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5605 break;
5606 case dw_val_class_const_double:
5607 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5608 HOST_WIDE_INT_PRINT_UNSIGNED")",
5609 val->v.val_double.high,
5610 val->v.val_double.low);
5611 break;
5612 case dw_val_class_wide_int:
5614 int i = val->v.val_wide->get_len ();
5615 fprintf (outfile, "constant (");
5616 gcc_assert (i > 0);
5617 if (val->v.val_wide->elt (i - 1) == 0)
5618 fprintf (outfile, "0x");
5619 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5620 val->v.val_wide->elt (--i));
5621 while (--i >= 0)
5622 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5623 val->v.val_wide->elt (i));
5624 fprintf (outfile, ")");
5625 break;
5627 case dw_val_class_vec:
5628 fprintf (outfile, "floating-point or vector constant");
5629 break;
5630 case dw_val_class_flag:
5631 fprintf (outfile, "%u", val->v.val_flag);
5632 break;
5633 case dw_val_class_die_ref:
5634 if (val->v.val_die_ref.die != NULL)
5636 dw_die_ref die = val->v.val_die_ref.die;
5638 if (die->comdat_type_p)
5640 fprintf (outfile, "die -> signature: ");
5641 print_signature (outfile,
5642 die->die_id.die_type_node->signature);
5644 else if (die->die_id.die_symbol)
5645 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5646 else
5647 fprintf (outfile, "die -> %ld", die->die_offset);
5648 fprintf (outfile, " (%p)", (void *) die);
5650 else
5651 fprintf (outfile, "die -> <null>");
5652 break;
5653 case dw_val_class_vms_delta:
5654 fprintf (outfile, "delta: @slotcount(%s-%s)",
5655 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5656 break;
5657 case dw_val_class_lbl_id:
5658 case dw_val_class_lineptr:
5659 case dw_val_class_macptr:
5660 case dw_val_class_high_pc:
5661 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5662 break;
5663 case dw_val_class_str:
5664 if (val->v.val_str->str != NULL)
5665 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5666 else
5667 fprintf (outfile, "<null>");
5668 break;
5669 case dw_val_class_file:
5670 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5671 val->v.val_file->emitted_number);
5672 break;
5673 case dw_val_class_data8:
5675 int i;
5677 for (i = 0; i < 8; i++)
5678 fprintf (outfile, "%02x", val->v.val_data8[i]);
5679 break;
5681 case dw_val_class_discr_value:
5682 print_discr_value (outfile, &val->v.val_discr_value);
5683 break;
5684 case dw_val_class_discr_list:
5685 for (dw_discr_list_ref node = val->v.val_discr_list;
5686 node != NULL;
5687 node = node->dw_discr_next)
5689 if (node->dw_discr_range)
5691 fprintf (outfile, " .. ");
5692 print_discr_value (outfile, &node->dw_discr_lower_bound);
5693 print_discr_value (outfile, &node->dw_discr_upper_bound);
5695 else
5696 print_discr_value (outfile, &node->dw_discr_lower_bound);
5698 if (node->dw_discr_next != NULL)
5699 fprintf (outfile, " | ");
5701 default:
5702 break;
5706 /* Likewise, for a DIE attribute. */
5708 static void
5709 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5711 print_dw_val (&a->dw_attr_val, recurse, outfile);
5715 /* Print the list of operands in the LOC location description to OUTFILE. This
5716 routine is a debugging aid only. */
5718 static void
5719 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5721 dw_loc_descr_ref l = loc;
5723 if (loc == NULL)
5725 print_spaces (outfile);
5726 fprintf (outfile, "<null>\n");
5727 return;
5730 for (l = loc; l != NULL; l = l->dw_loc_next)
5732 print_spaces (outfile);
5733 fprintf (outfile, "(%p) %s",
5734 (void *) l,
5735 dwarf_stack_op_name (l->dw_loc_opc));
5736 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5738 fprintf (outfile, " ");
5739 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5741 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5743 fprintf (outfile, ", ");
5744 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5746 fprintf (outfile, "\n");
5750 /* Print the information associated with a given DIE, and its children.
5751 This routine is a debugging aid only. */
5753 static void
5754 print_die (dw_die_ref die, FILE *outfile)
5756 dw_attr_node *a;
5757 dw_die_ref c;
5758 unsigned ix;
5760 print_spaces (outfile);
5761 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5762 die->die_offset, dwarf_tag_name (die->die_tag),
5763 (void*) die);
5764 print_spaces (outfile);
5765 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5766 fprintf (outfile, " offset: %ld", die->die_offset);
5767 fprintf (outfile, " mark: %d\n", die->die_mark);
5769 if (die->comdat_type_p)
5771 print_spaces (outfile);
5772 fprintf (outfile, " signature: ");
5773 print_signature (outfile, die->die_id.die_type_node->signature);
5774 fprintf (outfile, "\n");
5777 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5779 print_spaces (outfile);
5780 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5782 print_attribute (a, true, outfile);
5783 fprintf (outfile, "\n");
5786 if (die->die_child != NULL)
5788 print_indent += 4;
5789 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5790 print_indent -= 4;
5792 if (print_indent == 0)
5793 fprintf (outfile, "\n");
5796 /* Print the list of operations in the LOC location description. */
5798 DEBUG_FUNCTION void
5799 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5801 print_loc_descr (loc, stderr);
5804 /* Print the information collected for a given DIE. */
5806 DEBUG_FUNCTION void
5807 debug_dwarf_die (dw_die_ref die)
5809 print_die (die, stderr);
5812 DEBUG_FUNCTION void
5813 debug (die_struct &ref)
5815 print_die (&ref, stderr);
5818 DEBUG_FUNCTION void
5819 debug (die_struct *ptr)
5821 if (ptr)
5822 debug (*ptr);
5823 else
5824 fprintf (stderr, "<nil>\n");
5828 /* Print all DWARF information collected for the compilation unit.
5829 This routine is a debugging aid only. */
5831 DEBUG_FUNCTION void
5832 debug_dwarf (void)
5834 print_indent = 0;
5835 print_die (comp_unit_die (), stderr);
5838 /* Sanity checks on DIEs. */
5840 static void
5841 check_die (dw_die_ref die)
5843 unsigned ix;
5844 dw_attr_node *a;
5845 bool inline_found = false;
5846 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5847 int n_decl_line = 0, n_decl_file = 0;
5848 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5850 switch (a->dw_attr)
5852 case DW_AT_inline:
5853 if (a->dw_attr_val.v.val_unsigned)
5854 inline_found = true;
5855 break;
5856 case DW_AT_location:
5857 ++n_location;
5858 break;
5859 case DW_AT_low_pc:
5860 ++n_low_pc;
5861 break;
5862 case DW_AT_high_pc:
5863 ++n_high_pc;
5864 break;
5865 case DW_AT_artificial:
5866 ++n_artificial;
5867 break;
5868 case DW_AT_decl_line:
5869 ++n_decl_line;
5870 break;
5871 case DW_AT_decl_file:
5872 ++n_decl_file;
5873 break;
5874 default:
5875 break;
5878 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5879 || n_decl_line > 1 || n_decl_file > 1)
5881 fprintf (stderr, "Duplicate attributes in DIE:\n");
5882 debug_dwarf_die (die);
5883 gcc_unreachable ();
5885 if (inline_found)
5887 /* A debugging information entry that is a member of an abstract
5888 instance tree [that has DW_AT_inline] should not contain any
5889 attributes which describe aspects of the subroutine which vary
5890 between distinct inlined expansions or distinct out-of-line
5891 expansions. */
5892 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5893 gcc_assert (a->dw_attr != DW_AT_low_pc
5894 && a->dw_attr != DW_AT_high_pc
5895 && a->dw_attr != DW_AT_location
5896 && a->dw_attr != DW_AT_frame_base
5897 && a->dw_attr != DW_AT_GNU_all_call_sites);
5901 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5902 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5903 DIE that marks the start of the DIEs for this include file. */
5905 static dw_die_ref
5906 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5908 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5909 dw_die_ref new_unit = gen_compile_unit_die (filename);
5911 new_unit->die_sib = old_unit;
5912 return new_unit;
5915 /* Close an include-file CU and reopen the enclosing one. */
5917 static dw_die_ref
5918 pop_compile_unit (dw_die_ref old_unit)
5920 dw_die_ref new_unit = old_unit->die_sib;
5922 old_unit->die_sib = NULL;
5923 return new_unit;
5926 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5927 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5928 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5930 /* Calculate the checksum of a location expression. */
5932 static inline void
5933 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5935 int tem;
5936 inchash::hash hstate;
5937 hashval_t hash;
5939 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5940 CHECKSUM (tem);
5941 hash_loc_operands (loc, hstate);
5942 hash = hstate.end();
5943 CHECKSUM (hash);
5946 /* Calculate the checksum of an attribute. */
5948 static void
5949 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5951 dw_loc_descr_ref loc;
5952 rtx r;
5954 CHECKSUM (at->dw_attr);
5956 /* We don't care that this was compiled with a different compiler
5957 snapshot; if the output is the same, that's what matters. */
5958 if (at->dw_attr == DW_AT_producer)
5959 return;
5961 switch (AT_class (at))
5963 case dw_val_class_const:
5964 CHECKSUM (at->dw_attr_val.v.val_int);
5965 break;
5966 case dw_val_class_unsigned_const:
5967 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5968 break;
5969 case dw_val_class_const_double:
5970 CHECKSUM (at->dw_attr_val.v.val_double);
5971 break;
5972 case dw_val_class_wide_int:
5973 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5974 get_full_len (*at->dw_attr_val.v.val_wide)
5975 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5976 break;
5977 case dw_val_class_vec:
5978 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5979 (at->dw_attr_val.v.val_vec.length
5980 * at->dw_attr_val.v.val_vec.elt_size));
5981 break;
5982 case dw_val_class_flag:
5983 CHECKSUM (at->dw_attr_val.v.val_flag);
5984 break;
5985 case dw_val_class_str:
5986 CHECKSUM_STRING (AT_string (at));
5987 break;
5989 case dw_val_class_addr:
5990 r = AT_addr (at);
5991 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5992 CHECKSUM_STRING (XSTR (r, 0));
5993 break;
5995 case dw_val_class_offset:
5996 CHECKSUM (at->dw_attr_val.v.val_offset);
5997 break;
5999 case dw_val_class_loc:
6000 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6001 loc_checksum (loc, ctx);
6002 break;
6004 case dw_val_class_die_ref:
6005 die_checksum (AT_ref (at), ctx, mark);
6006 break;
6008 case dw_val_class_fde_ref:
6009 case dw_val_class_vms_delta:
6010 case dw_val_class_lbl_id:
6011 case dw_val_class_lineptr:
6012 case dw_val_class_macptr:
6013 case dw_val_class_high_pc:
6014 break;
6016 case dw_val_class_file:
6017 CHECKSUM_STRING (AT_file (at)->filename);
6018 break;
6020 case dw_val_class_data8:
6021 CHECKSUM (at->dw_attr_val.v.val_data8);
6022 break;
6024 default:
6025 break;
6029 /* Calculate the checksum of a DIE. */
6031 static void
6032 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6034 dw_die_ref c;
6035 dw_attr_node *a;
6036 unsigned ix;
6038 /* To avoid infinite recursion. */
6039 if (die->die_mark)
6041 CHECKSUM (die->die_mark);
6042 return;
6044 die->die_mark = ++(*mark);
6046 CHECKSUM (die->die_tag);
6048 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6049 attr_checksum (a, ctx, mark);
6051 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6054 #undef CHECKSUM
6055 #undef CHECKSUM_BLOCK
6056 #undef CHECKSUM_STRING
6058 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6059 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6060 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6061 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6062 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6063 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6064 #define CHECKSUM_ATTR(FOO) \
6065 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6067 /* Calculate the checksum of a number in signed LEB128 format. */
6069 static void
6070 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6072 unsigned char byte;
6073 bool more;
6075 while (1)
6077 byte = (value & 0x7f);
6078 value >>= 7;
6079 more = !((value == 0 && (byte & 0x40) == 0)
6080 || (value == -1 && (byte & 0x40) != 0));
6081 if (more)
6082 byte |= 0x80;
6083 CHECKSUM (byte);
6084 if (!more)
6085 break;
6089 /* Calculate the checksum of a number in unsigned LEB128 format. */
6091 static void
6092 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6094 while (1)
6096 unsigned char byte = (value & 0x7f);
6097 value >>= 7;
6098 if (value != 0)
6099 /* More bytes to follow. */
6100 byte |= 0x80;
6101 CHECKSUM (byte);
6102 if (value == 0)
6103 break;
6107 /* Checksum the context of the DIE. This adds the names of any
6108 surrounding namespaces or structures to the checksum. */
6110 static void
6111 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6113 const char *name;
6114 dw_die_ref spec;
6115 int tag = die->die_tag;
6117 if (tag != DW_TAG_namespace
6118 && tag != DW_TAG_structure_type
6119 && tag != DW_TAG_class_type)
6120 return;
6122 name = get_AT_string (die, DW_AT_name);
6124 spec = get_AT_ref (die, DW_AT_specification);
6125 if (spec != NULL)
6126 die = spec;
6128 if (die->die_parent != NULL)
6129 checksum_die_context (die->die_parent, ctx);
6131 CHECKSUM_ULEB128 ('C');
6132 CHECKSUM_ULEB128 (tag);
6133 if (name != NULL)
6134 CHECKSUM_STRING (name);
6137 /* Calculate the checksum of a location expression. */
6139 static inline void
6140 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6142 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6143 were emitted as a DW_FORM_sdata instead of a location expression. */
6144 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6146 CHECKSUM_ULEB128 (DW_FORM_sdata);
6147 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6148 return;
6151 /* Otherwise, just checksum the raw location expression. */
6152 while (loc != NULL)
6154 inchash::hash hstate;
6155 hashval_t hash;
6157 CHECKSUM_ULEB128 (loc->dtprel);
6158 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6159 hash_loc_operands (loc, hstate);
6160 hash = hstate.end ();
6161 CHECKSUM (hash);
6162 loc = loc->dw_loc_next;
6166 /* Calculate the checksum of an attribute. */
6168 static void
6169 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6170 struct md5_ctx *ctx, int *mark)
6172 dw_loc_descr_ref loc;
6173 rtx r;
6175 if (AT_class (at) == dw_val_class_die_ref)
6177 dw_die_ref target_die = AT_ref (at);
6179 /* For pointer and reference types, we checksum only the (qualified)
6180 name of the target type (if there is a name). For friend entries,
6181 we checksum only the (qualified) name of the target type or function.
6182 This allows the checksum to remain the same whether the target type
6183 is complete or not. */
6184 if ((at->dw_attr == DW_AT_type
6185 && (tag == DW_TAG_pointer_type
6186 || tag == DW_TAG_reference_type
6187 || tag == DW_TAG_rvalue_reference_type
6188 || tag == DW_TAG_ptr_to_member_type))
6189 || (at->dw_attr == DW_AT_friend
6190 && tag == DW_TAG_friend))
6192 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6194 if (name_attr != NULL)
6196 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6198 if (decl == NULL)
6199 decl = target_die;
6200 CHECKSUM_ULEB128 ('N');
6201 CHECKSUM_ULEB128 (at->dw_attr);
6202 if (decl->die_parent != NULL)
6203 checksum_die_context (decl->die_parent, ctx);
6204 CHECKSUM_ULEB128 ('E');
6205 CHECKSUM_STRING (AT_string (name_attr));
6206 return;
6210 /* For all other references to another DIE, we check to see if the
6211 target DIE has already been visited. If it has, we emit a
6212 backward reference; if not, we descend recursively. */
6213 if (target_die->die_mark > 0)
6215 CHECKSUM_ULEB128 ('R');
6216 CHECKSUM_ULEB128 (at->dw_attr);
6217 CHECKSUM_ULEB128 (target_die->die_mark);
6219 else
6221 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6223 if (decl == NULL)
6224 decl = target_die;
6225 target_die->die_mark = ++(*mark);
6226 CHECKSUM_ULEB128 ('T');
6227 CHECKSUM_ULEB128 (at->dw_attr);
6228 if (decl->die_parent != NULL)
6229 checksum_die_context (decl->die_parent, ctx);
6230 die_checksum_ordered (target_die, ctx, mark);
6232 return;
6235 CHECKSUM_ULEB128 ('A');
6236 CHECKSUM_ULEB128 (at->dw_attr);
6238 switch (AT_class (at))
6240 case dw_val_class_const:
6241 CHECKSUM_ULEB128 (DW_FORM_sdata);
6242 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6243 break;
6245 case dw_val_class_unsigned_const:
6246 CHECKSUM_ULEB128 (DW_FORM_sdata);
6247 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6248 break;
6250 case dw_val_class_const_double:
6251 CHECKSUM_ULEB128 (DW_FORM_block);
6252 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6253 CHECKSUM (at->dw_attr_val.v.val_double);
6254 break;
6256 case dw_val_class_wide_int:
6257 CHECKSUM_ULEB128 (DW_FORM_block);
6258 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6259 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6260 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6261 get_full_len (*at->dw_attr_val.v.val_wide)
6262 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6263 break;
6265 case dw_val_class_vec:
6266 CHECKSUM_ULEB128 (DW_FORM_block);
6267 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6268 * at->dw_attr_val.v.val_vec.elt_size);
6269 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6270 (at->dw_attr_val.v.val_vec.length
6271 * at->dw_attr_val.v.val_vec.elt_size));
6272 break;
6274 case dw_val_class_flag:
6275 CHECKSUM_ULEB128 (DW_FORM_flag);
6276 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6277 break;
6279 case dw_val_class_str:
6280 CHECKSUM_ULEB128 (DW_FORM_string);
6281 CHECKSUM_STRING (AT_string (at));
6282 break;
6284 case dw_val_class_addr:
6285 r = AT_addr (at);
6286 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6287 CHECKSUM_ULEB128 (DW_FORM_string);
6288 CHECKSUM_STRING (XSTR (r, 0));
6289 break;
6291 case dw_val_class_offset:
6292 CHECKSUM_ULEB128 (DW_FORM_sdata);
6293 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6294 break;
6296 case dw_val_class_loc:
6297 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6298 loc_checksum_ordered (loc, ctx);
6299 break;
6301 case dw_val_class_fde_ref:
6302 case dw_val_class_lbl_id:
6303 case dw_val_class_lineptr:
6304 case dw_val_class_macptr:
6305 case dw_val_class_high_pc:
6306 break;
6308 case dw_val_class_file:
6309 CHECKSUM_ULEB128 (DW_FORM_string);
6310 CHECKSUM_STRING (AT_file (at)->filename);
6311 break;
6313 case dw_val_class_data8:
6314 CHECKSUM (at->dw_attr_val.v.val_data8);
6315 break;
6317 default:
6318 break;
6322 struct checksum_attributes
6324 dw_attr_node *at_name;
6325 dw_attr_node *at_type;
6326 dw_attr_node *at_friend;
6327 dw_attr_node *at_accessibility;
6328 dw_attr_node *at_address_class;
6329 dw_attr_node *at_allocated;
6330 dw_attr_node *at_artificial;
6331 dw_attr_node *at_associated;
6332 dw_attr_node *at_binary_scale;
6333 dw_attr_node *at_bit_offset;
6334 dw_attr_node *at_bit_size;
6335 dw_attr_node *at_bit_stride;
6336 dw_attr_node *at_byte_size;
6337 dw_attr_node *at_byte_stride;
6338 dw_attr_node *at_const_value;
6339 dw_attr_node *at_containing_type;
6340 dw_attr_node *at_count;
6341 dw_attr_node *at_data_location;
6342 dw_attr_node *at_data_member_location;
6343 dw_attr_node *at_decimal_scale;
6344 dw_attr_node *at_decimal_sign;
6345 dw_attr_node *at_default_value;
6346 dw_attr_node *at_digit_count;
6347 dw_attr_node *at_discr;
6348 dw_attr_node *at_discr_list;
6349 dw_attr_node *at_discr_value;
6350 dw_attr_node *at_encoding;
6351 dw_attr_node *at_endianity;
6352 dw_attr_node *at_explicit;
6353 dw_attr_node *at_is_optional;
6354 dw_attr_node *at_location;
6355 dw_attr_node *at_lower_bound;
6356 dw_attr_node *at_mutable;
6357 dw_attr_node *at_ordering;
6358 dw_attr_node *at_picture_string;
6359 dw_attr_node *at_prototyped;
6360 dw_attr_node *at_small;
6361 dw_attr_node *at_segment;
6362 dw_attr_node *at_string_length;
6363 dw_attr_node *at_string_length_bit_size;
6364 dw_attr_node *at_string_length_byte_size;
6365 dw_attr_node *at_threads_scaled;
6366 dw_attr_node *at_upper_bound;
6367 dw_attr_node *at_use_location;
6368 dw_attr_node *at_use_UTF8;
6369 dw_attr_node *at_variable_parameter;
6370 dw_attr_node *at_virtuality;
6371 dw_attr_node *at_visibility;
6372 dw_attr_node *at_vtable_elem_location;
6375 /* Collect the attributes that we will want to use for the checksum. */
6377 static void
6378 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6380 dw_attr_node *a;
6381 unsigned ix;
6383 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6385 switch (a->dw_attr)
6387 case DW_AT_name:
6388 attrs->at_name = a;
6389 break;
6390 case DW_AT_type:
6391 attrs->at_type = a;
6392 break;
6393 case DW_AT_friend:
6394 attrs->at_friend = a;
6395 break;
6396 case DW_AT_accessibility:
6397 attrs->at_accessibility = a;
6398 break;
6399 case DW_AT_address_class:
6400 attrs->at_address_class = a;
6401 break;
6402 case DW_AT_allocated:
6403 attrs->at_allocated = a;
6404 break;
6405 case DW_AT_artificial:
6406 attrs->at_artificial = a;
6407 break;
6408 case DW_AT_associated:
6409 attrs->at_associated = a;
6410 break;
6411 case DW_AT_binary_scale:
6412 attrs->at_binary_scale = a;
6413 break;
6414 case DW_AT_bit_offset:
6415 attrs->at_bit_offset = a;
6416 break;
6417 case DW_AT_bit_size:
6418 attrs->at_bit_size = a;
6419 break;
6420 case DW_AT_bit_stride:
6421 attrs->at_bit_stride = a;
6422 break;
6423 case DW_AT_byte_size:
6424 attrs->at_byte_size = a;
6425 break;
6426 case DW_AT_byte_stride:
6427 attrs->at_byte_stride = a;
6428 break;
6429 case DW_AT_const_value:
6430 attrs->at_const_value = a;
6431 break;
6432 case DW_AT_containing_type:
6433 attrs->at_containing_type = a;
6434 break;
6435 case DW_AT_count:
6436 attrs->at_count = a;
6437 break;
6438 case DW_AT_data_location:
6439 attrs->at_data_location = a;
6440 break;
6441 case DW_AT_data_member_location:
6442 attrs->at_data_member_location = a;
6443 break;
6444 case DW_AT_decimal_scale:
6445 attrs->at_decimal_scale = a;
6446 break;
6447 case DW_AT_decimal_sign:
6448 attrs->at_decimal_sign = a;
6449 break;
6450 case DW_AT_default_value:
6451 attrs->at_default_value = a;
6452 break;
6453 case DW_AT_digit_count:
6454 attrs->at_digit_count = a;
6455 break;
6456 case DW_AT_discr:
6457 attrs->at_discr = a;
6458 break;
6459 case DW_AT_discr_list:
6460 attrs->at_discr_list = a;
6461 break;
6462 case DW_AT_discr_value:
6463 attrs->at_discr_value = a;
6464 break;
6465 case DW_AT_encoding:
6466 attrs->at_encoding = a;
6467 break;
6468 case DW_AT_endianity:
6469 attrs->at_endianity = a;
6470 break;
6471 case DW_AT_explicit:
6472 attrs->at_explicit = a;
6473 break;
6474 case DW_AT_is_optional:
6475 attrs->at_is_optional = a;
6476 break;
6477 case DW_AT_location:
6478 attrs->at_location = a;
6479 break;
6480 case DW_AT_lower_bound:
6481 attrs->at_lower_bound = a;
6482 break;
6483 case DW_AT_mutable:
6484 attrs->at_mutable = a;
6485 break;
6486 case DW_AT_ordering:
6487 attrs->at_ordering = a;
6488 break;
6489 case DW_AT_picture_string:
6490 attrs->at_picture_string = a;
6491 break;
6492 case DW_AT_prototyped:
6493 attrs->at_prototyped = a;
6494 break;
6495 case DW_AT_small:
6496 attrs->at_small = a;
6497 break;
6498 case DW_AT_segment:
6499 attrs->at_segment = a;
6500 break;
6501 case DW_AT_string_length:
6502 attrs->at_string_length = a;
6503 break;
6504 case DW_AT_string_length_bit_size:
6505 attrs->at_string_length_bit_size = a;
6506 break;
6507 case DW_AT_string_length_byte_size:
6508 attrs->at_string_length_byte_size = a;
6509 break;
6510 case DW_AT_threads_scaled:
6511 attrs->at_threads_scaled = a;
6512 break;
6513 case DW_AT_upper_bound:
6514 attrs->at_upper_bound = a;
6515 break;
6516 case DW_AT_use_location:
6517 attrs->at_use_location = a;
6518 break;
6519 case DW_AT_use_UTF8:
6520 attrs->at_use_UTF8 = a;
6521 break;
6522 case DW_AT_variable_parameter:
6523 attrs->at_variable_parameter = a;
6524 break;
6525 case DW_AT_virtuality:
6526 attrs->at_virtuality = a;
6527 break;
6528 case DW_AT_visibility:
6529 attrs->at_visibility = a;
6530 break;
6531 case DW_AT_vtable_elem_location:
6532 attrs->at_vtable_elem_location = a;
6533 break;
6534 default:
6535 break;
6540 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6542 static void
6543 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6545 dw_die_ref c;
6546 dw_die_ref decl;
6547 struct checksum_attributes attrs;
6549 CHECKSUM_ULEB128 ('D');
6550 CHECKSUM_ULEB128 (die->die_tag);
6552 memset (&attrs, 0, sizeof (attrs));
6554 decl = get_AT_ref (die, DW_AT_specification);
6555 if (decl != NULL)
6556 collect_checksum_attributes (&attrs, decl);
6557 collect_checksum_attributes (&attrs, die);
6559 CHECKSUM_ATTR (attrs.at_name);
6560 CHECKSUM_ATTR (attrs.at_accessibility);
6561 CHECKSUM_ATTR (attrs.at_address_class);
6562 CHECKSUM_ATTR (attrs.at_allocated);
6563 CHECKSUM_ATTR (attrs.at_artificial);
6564 CHECKSUM_ATTR (attrs.at_associated);
6565 CHECKSUM_ATTR (attrs.at_binary_scale);
6566 CHECKSUM_ATTR (attrs.at_bit_offset);
6567 CHECKSUM_ATTR (attrs.at_bit_size);
6568 CHECKSUM_ATTR (attrs.at_bit_stride);
6569 CHECKSUM_ATTR (attrs.at_byte_size);
6570 CHECKSUM_ATTR (attrs.at_byte_stride);
6571 CHECKSUM_ATTR (attrs.at_const_value);
6572 CHECKSUM_ATTR (attrs.at_containing_type);
6573 CHECKSUM_ATTR (attrs.at_count);
6574 CHECKSUM_ATTR (attrs.at_data_location);
6575 CHECKSUM_ATTR (attrs.at_data_member_location);
6576 CHECKSUM_ATTR (attrs.at_decimal_scale);
6577 CHECKSUM_ATTR (attrs.at_decimal_sign);
6578 CHECKSUM_ATTR (attrs.at_default_value);
6579 CHECKSUM_ATTR (attrs.at_digit_count);
6580 CHECKSUM_ATTR (attrs.at_discr);
6581 CHECKSUM_ATTR (attrs.at_discr_list);
6582 CHECKSUM_ATTR (attrs.at_discr_value);
6583 CHECKSUM_ATTR (attrs.at_encoding);
6584 CHECKSUM_ATTR (attrs.at_endianity);
6585 CHECKSUM_ATTR (attrs.at_explicit);
6586 CHECKSUM_ATTR (attrs.at_is_optional);
6587 CHECKSUM_ATTR (attrs.at_location);
6588 CHECKSUM_ATTR (attrs.at_lower_bound);
6589 CHECKSUM_ATTR (attrs.at_mutable);
6590 CHECKSUM_ATTR (attrs.at_ordering);
6591 CHECKSUM_ATTR (attrs.at_picture_string);
6592 CHECKSUM_ATTR (attrs.at_prototyped);
6593 CHECKSUM_ATTR (attrs.at_small);
6594 CHECKSUM_ATTR (attrs.at_segment);
6595 CHECKSUM_ATTR (attrs.at_string_length);
6596 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6597 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6598 CHECKSUM_ATTR (attrs.at_threads_scaled);
6599 CHECKSUM_ATTR (attrs.at_upper_bound);
6600 CHECKSUM_ATTR (attrs.at_use_location);
6601 CHECKSUM_ATTR (attrs.at_use_UTF8);
6602 CHECKSUM_ATTR (attrs.at_variable_parameter);
6603 CHECKSUM_ATTR (attrs.at_virtuality);
6604 CHECKSUM_ATTR (attrs.at_visibility);
6605 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6606 CHECKSUM_ATTR (attrs.at_type);
6607 CHECKSUM_ATTR (attrs.at_friend);
6609 /* Checksum the child DIEs. */
6610 c = die->die_child;
6611 if (c) do {
6612 dw_attr_node *name_attr;
6614 c = c->die_sib;
6615 name_attr = get_AT (c, DW_AT_name);
6616 if (is_template_instantiation (c))
6618 /* Ignore instantiations of member type and function templates. */
6620 else if (name_attr != NULL
6621 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6623 /* Use a shallow checksum for named nested types and member
6624 functions. */
6625 CHECKSUM_ULEB128 ('S');
6626 CHECKSUM_ULEB128 (c->die_tag);
6627 CHECKSUM_STRING (AT_string (name_attr));
6629 else
6631 /* Use a deep checksum for other children. */
6632 /* Mark this DIE so it gets processed when unmarking. */
6633 if (c->die_mark == 0)
6634 c->die_mark = -1;
6635 die_checksum_ordered (c, ctx, mark);
6637 } while (c != die->die_child);
6639 CHECKSUM_ULEB128 (0);
6642 /* Add a type name and tag to a hash. */
6643 static void
6644 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6646 CHECKSUM_ULEB128 (tag);
6647 CHECKSUM_STRING (name);
6650 #undef CHECKSUM
6651 #undef CHECKSUM_STRING
6652 #undef CHECKSUM_ATTR
6653 #undef CHECKSUM_LEB128
6654 #undef CHECKSUM_ULEB128
6656 /* Generate the type signature for DIE. This is computed by generating an
6657 MD5 checksum over the DIE's tag, its relevant attributes, and its
6658 children. Attributes that are references to other DIEs are processed
6659 by recursion, using the MARK field to prevent infinite recursion.
6660 If the DIE is nested inside a namespace or another type, we also
6661 need to include that context in the signature. The lower 64 bits
6662 of the resulting MD5 checksum comprise the signature. */
6664 static void
6665 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6667 int mark;
6668 const char *name;
6669 unsigned char checksum[16];
6670 struct md5_ctx ctx;
6671 dw_die_ref decl;
6672 dw_die_ref parent;
6674 name = get_AT_string (die, DW_AT_name);
6675 decl = get_AT_ref (die, DW_AT_specification);
6676 parent = get_die_parent (die);
6678 /* First, compute a signature for just the type name (and its surrounding
6679 context, if any. This is stored in the type unit DIE for link-time
6680 ODR (one-definition rule) checking. */
6682 if (is_cxx () && name != NULL)
6684 md5_init_ctx (&ctx);
6686 /* Checksum the names of surrounding namespaces and structures. */
6687 if (parent != NULL)
6688 checksum_die_context (parent, &ctx);
6690 /* Checksum the current DIE. */
6691 die_odr_checksum (die->die_tag, name, &ctx);
6692 md5_finish_ctx (&ctx, checksum);
6694 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6697 /* Next, compute the complete type signature. */
6699 md5_init_ctx (&ctx);
6700 mark = 1;
6701 die->die_mark = mark;
6703 /* Checksum the names of surrounding namespaces and structures. */
6704 if (parent != NULL)
6705 checksum_die_context (parent, &ctx);
6707 /* Checksum the DIE and its children. */
6708 die_checksum_ordered (die, &ctx, &mark);
6709 unmark_all_dies (die);
6710 md5_finish_ctx (&ctx, checksum);
6712 /* Store the signature in the type node and link the type DIE and the
6713 type node together. */
6714 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6715 DWARF_TYPE_SIGNATURE_SIZE);
6716 die->comdat_type_p = true;
6717 die->die_id.die_type_node = type_node;
6718 type_node->type_die = die;
6720 /* If the DIE is a specification, link its declaration to the type node
6721 as well. */
6722 if (decl != NULL)
6724 decl->comdat_type_p = true;
6725 decl->die_id.die_type_node = type_node;
6729 /* Do the location expressions look same? */
6730 static inline int
6731 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6733 return loc1->dw_loc_opc == loc2->dw_loc_opc
6734 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6735 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6738 /* Do the values look the same? */
6739 static int
6740 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6742 dw_loc_descr_ref loc1, loc2;
6743 rtx r1, r2;
6745 if (v1->val_class != v2->val_class)
6746 return 0;
6748 switch (v1->val_class)
6750 case dw_val_class_const:
6751 return v1->v.val_int == v2->v.val_int;
6752 case dw_val_class_unsigned_const:
6753 return v1->v.val_unsigned == v2->v.val_unsigned;
6754 case dw_val_class_const_double:
6755 return v1->v.val_double.high == v2->v.val_double.high
6756 && v1->v.val_double.low == v2->v.val_double.low;
6757 case dw_val_class_wide_int:
6758 return *v1->v.val_wide == *v2->v.val_wide;
6759 case dw_val_class_vec:
6760 if (v1->v.val_vec.length != v2->v.val_vec.length
6761 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6762 return 0;
6763 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6764 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6765 return 0;
6766 return 1;
6767 case dw_val_class_flag:
6768 return v1->v.val_flag == v2->v.val_flag;
6769 case dw_val_class_str:
6770 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6772 case dw_val_class_addr:
6773 r1 = v1->v.val_addr;
6774 r2 = v2->v.val_addr;
6775 if (GET_CODE (r1) != GET_CODE (r2))
6776 return 0;
6777 return !rtx_equal_p (r1, r2);
6779 case dw_val_class_offset:
6780 return v1->v.val_offset == v2->v.val_offset;
6782 case dw_val_class_loc:
6783 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6784 loc1 && loc2;
6785 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6786 if (!same_loc_p (loc1, loc2, mark))
6787 return 0;
6788 return !loc1 && !loc2;
6790 case dw_val_class_die_ref:
6791 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6793 case dw_val_class_fde_ref:
6794 case dw_val_class_vms_delta:
6795 case dw_val_class_lbl_id:
6796 case dw_val_class_lineptr:
6797 case dw_val_class_macptr:
6798 case dw_val_class_high_pc:
6799 return 1;
6801 case dw_val_class_file:
6802 return v1->v.val_file == v2->v.val_file;
6804 case dw_val_class_data8:
6805 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6807 default:
6808 return 1;
6812 /* Do the attributes look the same? */
6814 static int
6815 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6817 if (at1->dw_attr != at2->dw_attr)
6818 return 0;
6820 /* We don't care that this was compiled with a different compiler
6821 snapshot; if the output is the same, that's what matters. */
6822 if (at1->dw_attr == DW_AT_producer)
6823 return 1;
6825 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6828 /* Do the dies look the same? */
6830 static int
6831 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6833 dw_die_ref c1, c2;
6834 dw_attr_node *a1;
6835 unsigned ix;
6837 /* To avoid infinite recursion. */
6838 if (die1->die_mark)
6839 return die1->die_mark == die2->die_mark;
6840 die1->die_mark = die2->die_mark = ++(*mark);
6842 if (die1->die_tag != die2->die_tag)
6843 return 0;
6845 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6846 return 0;
6848 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6849 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6850 return 0;
6852 c1 = die1->die_child;
6853 c2 = die2->die_child;
6854 if (! c1)
6856 if (c2)
6857 return 0;
6859 else
6860 for (;;)
6862 if (!same_die_p (c1, c2, mark))
6863 return 0;
6864 c1 = c1->die_sib;
6865 c2 = c2->die_sib;
6866 if (c1 == die1->die_child)
6868 if (c2 == die2->die_child)
6869 break;
6870 else
6871 return 0;
6875 return 1;
6878 /* Do the dies look the same? Wrapper around same_die_p. */
6880 static int
6881 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6883 int mark = 0;
6884 int ret = same_die_p (die1, die2, &mark);
6886 unmark_all_dies (die1);
6887 unmark_all_dies (die2);
6889 return ret;
6892 /* The prefix to attach to symbols on DIEs in the current comdat debug
6893 info section. */
6894 static const char *comdat_symbol_id;
6896 /* The index of the current symbol within the current comdat CU. */
6897 static unsigned int comdat_symbol_number;
6899 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6900 children, and set comdat_symbol_id accordingly. */
6902 static void
6903 compute_section_prefix (dw_die_ref unit_die)
6905 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6906 const char *base = die_name ? lbasename (die_name) : "anonymous";
6907 char *name = XALLOCAVEC (char, strlen (base) + 64);
6908 char *p;
6909 int i, mark;
6910 unsigned char checksum[16];
6911 struct md5_ctx ctx;
6913 /* Compute the checksum of the DIE, then append part of it as hex digits to
6914 the name filename of the unit. */
6916 md5_init_ctx (&ctx);
6917 mark = 0;
6918 die_checksum (unit_die, &ctx, &mark);
6919 unmark_all_dies (unit_die);
6920 md5_finish_ctx (&ctx, checksum);
6922 sprintf (name, "%s.", base);
6923 clean_symbol_name (name);
6925 p = name + strlen (name);
6926 for (i = 0; i < 4; i++)
6928 sprintf (p, "%.2x", checksum[i]);
6929 p += 2;
6932 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6933 comdat_symbol_number = 0;
6936 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6938 static int
6939 is_type_die (dw_die_ref die)
6941 switch (die->die_tag)
6943 case DW_TAG_array_type:
6944 case DW_TAG_class_type:
6945 case DW_TAG_interface_type:
6946 case DW_TAG_enumeration_type:
6947 case DW_TAG_pointer_type:
6948 case DW_TAG_reference_type:
6949 case DW_TAG_rvalue_reference_type:
6950 case DW_TAG_string_type:
6951 case DW_TAG_structure_type:
6952 case DW_TAG_subroutine_type:
6953 case DW_TAG_union_type:
6954 case DW_TAG_ptr_to_member_type:
6955 case DW_TAG_set_type:
6956 case DW_TAG_subrange_type:
6957 case DW_TAG_base_type:
6958 case DW_TAG_const_type:
6959 case DW_TAG_file_type:
6960 case DW_TAG_packed_type:
6961 case DW_TAG_volatile_type:
6962 case DW_TAG_typedef:
6963 return 1;
6964 default:
6965 return 0;
6969 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6970 Basically, we want to choose the bits that are likely to be shared between
6971 compilations (types) and leave out the bits that are specific to individual
6972 compilations (functions). */
6974 static int
6975 is_comdat_die (dw_die_ref c)
6977 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6978 we do for stabs. The advantage is a greater likelihood of sharing between
6979 objects that don't include headers in the same order (and therefore would
6980 put the base types in a different comdat). jason 8/28/00 */
6982 if (c->die_tag == DW_TAG_base_type)
6983 return 0;
6985 if (c->die_tag == DW_TAG_pointer_type
6986 || c->die_tag == DW_TAG_reference_type
6987 || c->die_tag == DW_TAG_rvalue_reference_type
6988 || c->die_tag == DW_TAG_const_type
6989 || c->die_tag == DW_TAG_volatile_type)
6991 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6993 return t ? is_comdat_die (t) : 0;
6996 return is_type_die (c);
6999 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7000 compilation unit. */
7002 static int
7003 is_symbol_die (dw_die_ref c)
7005 return (is_type_die (c)
7006 || is_declaration_die (c)
7007 || c->die_tag == DW_TAG_namespace
7008 || c->die_tag == DW_TAG_module);
7011 /* Returns true iff C is a compile-unit DIE. */
7013 static inline bool
7014 is_cu_die (dw_die_ref c)
7016 return c && c->die_tag == DW_TAG_compile_unit;
7019 /* Returns true iff C is a unit DIE of some sort. */
7021 static inline bool
7022 is_unit_die (dw_die_ref c)
7024 return c && (c->die_tag == DW_TAG_compile_unit
7025 || c->die_tag == DW_TAG_partial_unit
7026 || c->die_tag == DW_TAG_type_unit);
7029 /* Returns true iff C is a namespace DIE. */
7031 static inline bool
7032 is_namespace_die (dw_die_ref c)
7034 return c && c->die_tag == DW_TAG_namespace;
7037 /* Returns true iff C is a class or structure DIE. */
7039 static inline bool
7040 is_class_die (dw_die_ref c)
7042 return c && (c->die_tag == DW_TAG_class_type
7043 || c->die_tag == DW_TAG_structure_type);
7046 /* Return non-zero if this DIE is a template parameter. */
7048 static inline bool
7049 is_template_parameter (dw_die_ref die)
7051 switch (die->die_tag)
7053 case DW_TAG_template_type_param:
7054 case DW_TAG_template_value_param:
7055 case DW_TAG_GNU_template_template_param:
7056 case DW_TAG_GNU_template_parameter_pack:
7057 return true;
7058 default:
7059 return false;
7063 /* Return non-zero if this DIE represents a template instantiation. */
7065 static inline bool
7066 is_template_instantiation (dw_die_ref die)
7068 dw_die_ref c;
7070 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7071 return false;
7072 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7073 return false;
7076 static char *
7077 gen_internal_sym (const char *prefix)
7079 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7081 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7082 return xstrdup (buf);
7085 /* Assign symbols to all worthy DIEs under DIE. */
7087 static void
7088 assign_symbol_names (dw_die_ref die)
7090 dw_die_ref c;
7092 if (is_symbol_die (die) && !die->comdat_type_p)
7094 if (comdat_symbol_id)
7096 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7098 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7099 comdat_symbol_id, comdat_symbol_number++);
7100 die->die_id.die_symbol = xstrdup (p);
7102 else
7103 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7106 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7109 struct cu_hash_table_entry
7111 dw_die_ref cu;
7112 unsigned min_comdat_num, max_comdat_num;
7113 struct cu_hash_table_entry *next;
7116 /* Helpers to manipulate hash table of CUs. */
7118 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7120 typedef die_struct *compare_type;
7121 static inline hashval_t hash (const cu_hash_table_entry *);
7122 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7123 static inline void remove (cu_hash_table_entry *);
7126 inline hashval_t
7127 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7129 return htab_hash_string (entry->cu->die_id.die_symbol);
7132 inline bool
7133 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7134 const die_struct *entry2)
7136 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7139 inline void
7140 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7142 struct cu_hash_table_entry *next;
7144 while (entry)
7146 next = entry->next;
7147 free (entry);
7148 entry = next;
7152 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7154 /* Check whether we have already seen this CU and set up SYM_NUM
7155 accordingly. */
7156 static int
7157 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7159 struct cu_hash_table_entry dummy;
7160 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7162 dummy.max_comdat_num = 0;
7164 slot = htable->find_slot_with_hash (cu,
7165 htab_hash_string (cu->die_id.die_symbol),
7166 INSERT);
7167 entry = *slot;
7169 for (; entry; last = entry, entry = entry->next)
7171 if (same_die_p_wrap (cu, entry->cu))
7172 break;
7175 if (entry)
7177 *sym_num = entry->min_comdat_num;
7178 return 1;
7181 entry = XCNEW (struct cu_hash_table_entry);
7182 entry->cu = cu;
7183 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7184 entry->next = *slot;
7185 *slot = entry;
7187 return 0;
7190 /* Record SYM_NUM to record of CU in HTABLE. */
7191 static void
7192 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7193 unsigned int sym_num)
7195 struct cu_hash_table_entry **slot, *entry;
7197 slot = htable->find_slot_with_hash (cu,
7198 htab_hash_string (cu->die_id.die_symbol),
7199 NO_INSERT);
7200 entry = *slot;
7202 entry->max_comdat_num = sym_num;
7205 /* Traverse the DIE (which is always comp_unit_die), and set up
7206 additional compilation units for each of the include files we see
7207 bracketed by BINCL/EINCL. */
7209 static void
7210 break_out_includes (dw_die_ref die)
7212 dw_die_ref c;
7213 dw_die_ref unit = NULL;
7214 limbo_die_node *node, **pnode;
7216 c = die->die_child;
7217 if (c) do {
7218 dw_die_ref prev = c;
7219 c = c->die_sib;
7220 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7221 || (unit && is_comdat_die (c)))
7223 dw_die_ref next = c->die_sib;
7225 /* This DIE is for a secondary CU; remove it from the main one. */
7226 remove_child_with_prev (c, prev);
7228 if (c->die_tag == DW_TAG_GNU_BINCL)
7229 unit = push_new_compile_unit (unit, c);
7230 else if (c->die_tag == DW_TAG_GNU_EINCL)
7231 unit = pop_compile_unit (unit);
7232 else
7233 add_child_die (unit, c);
7234 c = next;
7235 if (c == die->die_child)
7236 break;
7238 } while (c != die->die_child);
7240 #if 0
7241 /* We can only use this in debugging, since the frontend doesn't check
7242 to make sure that we leave every include file we enter. */
7243 gcc_assert (!unit);
7244 #endif
7246 assign_symbol_names (die);
7247 cu_hash_type cu_hash_table (10);
7248 for (node = limbo_die_list, pnode = &limbo_die_list;
7249 node;
7250 node = node->next)
7252 int is_dupl;
7254 compute_section_prefix (node->die);
7255 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7256 &comdat_symbol_number);
7257 assign_symbol_names (node->die);
7258 if (is_dupl)
7259 *pnode = node->next;
7260 else
7262 pnode = &node->next;
7263 record_comdat_symbol_number (node->die, &cu_hash_table,
7264 comdat_symbol_number);
7269 /* Return non-zero if this DIE is a declaration. */
7271 static int
7272 is_declaration_die (dw_die_ref die)
7274 dw_attr_node *a;
7275 unsigned ix;
7277 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7278 if (a->dw_attr == DW_AT_declaration)
7279 return 1;
7281 return 0;
7284 /* Return non-zero if this DIE is nested inside a subprogram. */
7286 static int
7287 is_nested_in_subprogram (dw_die_ref die)
7289 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7291 if (decl == NULL)
7292 decl = die;
7293 return local_scope_p (decl);
7296 /* Return non-zero if this DIE contains a defining declaration of a
7297 subprogram. */
7299 static int
7300 contains_subprogram_definition (dw_die_ref die)
7302 dw_die_ref c;
7304 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7305 return 1;
7306 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7307 return 0;
7310 /* Return non-zero if this is a type DIE that should be moved to a
7311 COMDAT .debug_types section. */
7313 static int
7314 should_move_die_to_comdat (dw_die_ref die)
7316 switch (die->die_tag)
7318 case DW_TAG_class_type:
7319 case DW_TAG_structure_type:
7320 case DW_TAG_enumeration_type:
7321 case DW_TAG_union_type:
7322 /* Don't move declarations, inlined instances, types nested in a
7323 subprogram, or types that contain subprogram definitions. */
7324 if (is_declaration_die (die)
7325 || get_AT (die, DW_AT_abstract_origin)
7326 || is_nested_in_subprogram (die)
7327 || contains_subprogram_definition (die))
7328 return 0;
7329 return 1;
7330 case DW_TAG_array_type:
7331 case DW_TAG_interface_type:
7332 case DW_TAG_pointer_type:
7333 case DW_TAG_reference_type:
7334 case DW_TAG_rvalue_reference_type:
7335 case DW_TAG_string_type:
7336 case DW_TAG_subroutine_type:
7337 case DW_TAG_ptr_to_member_type:
7338 case DW_TAG_set_type:
7339 case DW_TAG_subrange_type:
7340 case DW_TAG_base_type:
7341 case DW_TAG_const_type:
7342 case DW_TAG_file_type:
7343 case DW_TAG_packed_type:
7344 case DW_TAG_volatile_type:
7345 case DW_TAG_typedef:
7346 default:
7347 return 0;
7351 /* Make a clone of DIE. */
7353 static dw_die_ref
7354 clone_die (dw_die_ref die)
7356 dw_die_ref clone;
7357 dw_attr_node *a;
7358 unsigned ix;
7360 clone = ggc_cleared_alloc<die_node> ();
7361 clone->die_tag = die->die_tag;
7363 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7364 add_dwarf_attr (clone, a);
7366 return clone;
7369 /* Make a clone of the tree rooted at DIE. */
7371 static dw_die_ref
7372 clone_tree (dw_die_ref die)
7374 dw_die_ref c;
7375 dw_die_ref clone = clone_die (die);
7377 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7379 return clone;
7382 /* Make a clone of DIE as a declaration. */
7384 static dw_die_ref
7385 clone_as_declaration (dw_die_ref die)
7387 dw_die_ref clone;
7388 dw_die_ref decl;
7389 dw_attr_node *a;
7390 unsigned ix;
7392 /* If the DIE is already a declaration, just clone it. */
7393 if (is_declaration_die (die))
7394 return clone_die (die);
7396 /* If the DIE is a specification, just clone its declaration DIE. */
7397 decl = get_AT_ref (die, DW_AT_specification);
7398 if (decl != NULL)
7400 clone = clone_die (decl);
7401 if (die->comdat_type_p)
7402 add_AT_die_ref (clone, DW_AT_signature, die);
7403 return clone;
7406 clone = ggc_cleared_alloc<die_node> ();
7407 clone->die_tag = die->die_tag;
7409 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7411 /* We don't want to copy over all attributes.
7412 For example we don't want DW_AT_byte_size because otherwise we will no
7413 longer have a declaration and GDB will treat it as a definition. */
7415 switch (a->dw_attr)
7417 case DW_AT_abstract_origin:
7418 case DW_AT_artificial:
7419 case DW_AT_containing_type:
7420 case DW_AT_external:
7421 case DW_AT_name:
7422 case DW_AT_type:
7423 case DW_AT_virtuality:
7424 case DW_AT_linkage_name:
7425 case DW_AT_MIPS_linkage_name:
7426 add_dwarf_attr (clone, a);
7427 break;
7428 case DW_AT_byte_size:
7429 default:
7430 break;
7434 if (die->comdat_type_p)
7435 add_AT_die_ref (clone, DW_AT_signature, die);
7437 add_AT_flag (clone, DW_AT_declaration, 1);
7438 return clone;
7442 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7444 struct decl_table_entry
7446 dw_die_ref orig;
7447 dw_die_ref copy;
7450 /* Helpers to manipulate hash table of copied declarations. */
7452 /* Hashtable helpers. */
7454 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7456 typedef die_struct *compare_type;
7457 static inline hashval_t hash (const decl_table_entry *);
7458 static inline bool equal (const decl_table_entry *, const die_struct *);
7461 inline hashval_t
7462 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7464 return htab_hash_pointer (entry->orig);
7467 inline bool
7468 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7469 const die_struct *entry2)
7471 return entry1->orig == entry2;
7474 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7476 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7477 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7478 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7479 to check if the ancestor has already been copied into UNIT. */
7481 static dw_die_ref
7482 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7483 decl_hash_type *decl_table)
7485 dw_die_ref parent = die->die_parent;
7486 dw_die_ref new_parent = unit;
7487 dw_die_ref copy;
7488 decl_table_entry **slot = NULL;
7489 struct decl_table_entry *entry = NULL;
7491 if (decl_table)
7493 /* Check if the entry has already been copied to UNIT. */
7494 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7495 INSERT);
7496 if (*slot != HTAB_EMPTY_ENTRY)
7498 entry = *slot;
7499 return entry->copy;
7502 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7503 entry = XCNEW (struct decl_table_entry);
7504 entry->orig = die;
7505 entry->copy = NULL;
7506 *slot = entry;
7509 if (parent != NULL)
7511 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7512 if (spec != NULL)
7513 parent = spec;
7514 if (!is_unit_die (parent))
7515 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7518 copy = clone_as_declaration (die);
7519 add_child_die (new_parent, copy);
7521 if (decl_table)
7523 /* Record the pointer to the copy. */
7524 entry->copy = copy;
7527 return copy;
7529 /* Copy the declaration context to the new type unit DIE. This includes
7530 any surrounding namespace or type declarations. If the DIE has an
7531 AT_specification attribute, it also includes attributes and children
7532 attached to the specification, and returns a pointer to the original
7533 parent of the declaration DIE. Returns NULL otherwise. */
7535 static dw_die_ref
7536 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7538 dw_die_ref decl;
7539 dw_die_ref new_decl;
7540 dw_die_ref orig_parent = NULL;
7542 decl = get_AT_ref (die, DW_AT_specification);
7543 if (decl == NULL)
7544 decl = die;
7545 else
7547 unsigned ix;
7548 dw_die_ref c;
7549 dw_attr_node *a;
7551 /* The original DIE will be changed to a declaration, and must
7552 be moved to be a child of the original declaration DIE. */
7553 orig_parent = decl->die_parent;
7555 /* Copy the type node pointer from the new DIE to the original
7556 declaration DIE so we can forward references later. */
7557 decl->comdat_type_p = true;
7558 decl->die_id.die_type_node = die->die_id.die_type_node;
7560 remove_AT (die, DW_AT_specification);
7562 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7564 if (a->dw_attr != DW_AT_name
7565 && a->dw_attr != DW_AT_declaration
7566 && a->dw_attr != DW_AT_external)
7567 add_dwarf_attr (die, a);
7570 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7573 if (decl->die_parent != NULL
7574 && !is_unit_die (decl->die_parent))
7576 new_decl = copy_ancestor_tree (unit, decl, NULL);
7577 if (new_decl != NULL)
7579 remove_AT (new_decl, DW_AT_signature);
7580 add_AT_specification (die, new_decl);
7584 return orig_parent;
7587 /* Generate the skeleton ancestor tree for the given NODE, then clone
7588 the DIE and add the clone into the tree. */
7590 static void
7591 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7593 if (node->new_die != NULL)
7594 return;
7596 node->new_die = clone_as_declaration (node->old_die);
7598 if (node->parent != NULL)
7600 generate_skeleton_ancestor_tree (node->parent);
7601 add_child_die (node->parent->new_die, node->new_die);
7605 /* Generate a skeleton tree of DIEs containing any declarations that are
7606 found in the original tree. We traverse the tree looking for declaration
7607 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7609 static void
7610 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7612 skeleton_chain_node node;
7613 dw_die_ref c;
7614 dw_die_ref first;
7615 dw_die_ref prev = NULL;
7616 dw_die_ref next = NULL;
7618 node.parent = parent;
7620 first = c = parent->old_die->die_child;
7621 if (c)
7622 next = c->die_sib;
7623 if (c) do {
7624 if (prev == NULL || prev->die_sib == c)
7625 prev = c;
7626 c = next;
7627 next = (c == first ? NULL : c->die_sib);
7628 node.old_die = c;
7629 node.new_die = NULL;
7630 if (is_declaration_die (c))
7632 if (is_template_instantiation (c))
7634 /* Instantiated templates do not need to be cloned into the
7635 type unit. Just move the DIE and its children back to
7636 the skeleton tree (in the main CU). */
7637 remove_child_with_prev (c, prev);
7638 add_child_die (parent->new_die, c);
7639 c = prev;
7641 else
7643 /* Clone the existing DIE, move the original to the skeleton
7644 tree (which is in the main CU), and put the clone, with
7645 all the original's children, where the original came from
7646 (which is about to be moved to the type unit). */
7647 dw_die_ref clone = clone_die (c);
7648 move_all_children (c, clone);
7650 /* If the original has a DW_AT_object_pointer attribute,
7651 it would now point to a child DIE just moved to the
7652 cloned tree, so we need to remove that attribute from
7653 the original. */
7654 remove_AT (c, DW_AT_object_pointer);
7656 replace_child (c, clone, prev);
7657 generate_skeleton_ancestor_tree (parent);
7658 add_child_die (parent->new_die, c);
7659 node.new_die = c;
7660 c = clone;
7663 generate_skeleton_bottom_up (&node);
7664 } while (next != NULL);
7667 /* Wrapper function for generate_skeleton_bottom_up. */
7669 static dw_die_ref
7670 generate_skeleton (dw_die_ref die)
7672 skeleton_chain_node node;
7674 node.old_die = die;
7675 node.new_die = NULL;
7676 node.parent = NULL;
7678 /* If this type definition is nested inside another type,
7679 and is not an instantiation of a template, always leave
7680 at least a declaration in its place. */
7681 if (die->die_parent != NULL
7682 && is_type_die (die->die_parent)
7683 && !is_template_instantiation (die))
7684 node.new_die = clone_as_declaration (die);
7686 generate_skeleton_bottom_up (&node);
7687 return node.new_die;
7690 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7691 declaration. The original DIE is moved to a new compile unit so that
7692 existing references to it follow it to the new location. If any of the
7693 original DIE's descendants is a declaration, we need to replace the
7694 original DIE with a skeleton tree and move the declarations back into the
7695 skeleton tree. */
7697 static dw_die_ref
7698 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7699 dw_die_ref prev)
7701 dw_die_ref skeleton, orig_parent;
7703 /* Copy the declaration context to the type unit DIE. If the returned
7704 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7705 that DIE. */
7706 orig_parent = copy_declaration_context (unit, child);
7708 skeleton = generate_skeleton (child);
7709 if (skeleton == NULL)
7710 remove_child_with_prev (child, prev);
7711 else
7713 skeleton->comdat_type_p = true;
7714 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7716 /* If the original DIE was a specification, we need to put
7717 the skeleton under the parent DIE of the declaration.
7718 This leaves the original declaration in the tree, but
7719 it will be pruned later since there are no longer any
7720 references to it. */
7721 if (orig_parent != NULL)
7723 remove_child_with_prev (child, prev);
7724 add_child_die (orig_parent, skeleton);
7726 else
7727 replace_child (child, skeleton, prev);
7730 return skeleton;
7733 static void
7734 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7735 comdat_type_node *type_node,
7736 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7738 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7739 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7740 DWARF procedure references in the DW_AT_location attribute. */
7742 static dw_die_ref
7743 copy_dwarf_procedure (dw_die_ref die,
7744 comdat_type_node *type_node,
7745 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7747 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7749 /* DWARF procedures are not supposed to have children... */
7750 gcc_assert (die->die_child == NULL);
7752 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7753 gcc_assert (vec_safe_length (die->die_attr) == 1
7754 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7756 /* Do not copy more than once DWARF procedures. */
7757 bool existed;
7758 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7759 if (existed)
7760 return die_copy;
7762 die_copy = clone_die (die);
7763 add_child_die (type_node->root_die, die_copy);
7764 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7765 return die_copy;
7768 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7769 procedures in DIE's attributes. */
7771 static void
7772 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7773 comdat_type_node *type_node,
7774 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7776 dw_attr_node *a;
7777 unsigned i;
7779 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7781 dw_loc_descr_ref loc;
7783 if (a->dw_attr_val.val_class != dw_val_class_loc)
7784 continue;
7786 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7788 switch (loc->dw_loc_opc)
7790 case DW_OP_call2:
7791 case DW_OP_call4:
7792 case DW_OP_call_ref:
7793 gcc_assert (loc->dw_loc_oprnd1.val_class
7794 == dw_val_class_die_ref);
7795 loc->dw_loc_oprnd1.v.val_die_ref.die
7796 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7797 type_node,
7798 copied_dwarf_procs);
7800 default:
7801 break;
7807 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7808 rewrite references to point to the copies.
7810 References are looked for in DIE's attributes and recursively in all its
7811 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7812 mapping from old DWARF procedures to their copy. It is used not to copy
7813 twice the same DWARF procedure under TYPE_NODE. */
7815 static void
7816 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7817 comdat_type_node *type_node,
7818 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7820 dw_die_ref c;
7822 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7823 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7824 type_node,
7825 copied_dwarf_procs));
7828 /* Traverse the DIE and set up additional .debug_types sections for each
7829 type worthy of being placed in a COMDAT section. */
7831 static void
7832 break_out_comdat_types (dw_die_ref die)
7834 dw_die_ref c;
7835 dw_die_ref first;
7836 dw_die_ref prev = NULL;
7837 dw_die_ref next = NULL;
7838 dw_die_ref unit = NULL;
7840 first = c = die->die_child;
7841 if (c)
7842 next = c->die_sib;
7843 if (c) do {
7844 if (prev == NULL || prev->die_sib == c)
7845 prev = c;
7846 c = next;
7847 next = (c == first ? NULL : c->die_sib);
7848 if (should_move_die_to_comdat (c))
7850 dw_die_ref replacement;
7851 comdat_type_node *type_node;
7853 /* Break out nested types into their own type units. */
7854 break_out_comdat_types (c);
7856 /* Create a new type unit DIE as the root for the new tree, and
7857 add it to the list of comdat types. */
7858 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7859 add_AT_unsigned (unit, DW_AT_language,
7860 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7861 type_node = ggc_cleared_alloc<comdat_type_node> ();
7862 type_node->root_die = unit;
7863 type_node->next = comdat_type_list;
7864 comdat_type_list = type_node;
7866 /* Generate the type signature. */
7867 generate_type_signature (c, type_node);
7869 /* Copy the declaration context, attributes, and children of the
7870 declaration into the new type unit DIE, then remove this DIE
7871 from the main CU (or replace it with a skeleton if necessary). */
7872 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7873 type_node->skeleton_die = replacement;
7875 /* Add the DIE to the new compunit. */
7876 add_child_die (unit, c);
7878 /* Types can reference DWARF procedures for type size or data location
7879 expressions. Calls in DWARF expressions cannot target procedures
7880 that are not in the same section. So we must copy DWARF procedures
7881 along with this type and then rewrite references to them. */
7882 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7883 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7885 if (replacement != NULL)
7886 c = replacement;
7888 else if (c->die_tag == DW_TAG_namespace
7889 || c->die_tag == DW_TAG_class_type
7890 || c->die_tag == DW_TAG_structure_type
7891 || c->die_tag == DW_TAG_union_type)
7893 /* Look for nested types that can be broken out. */
7894 break_out_comdat_types (c);
7896 } while (next != NULL);
7899 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7900 Enter all the cloned children into the hash table decl_table. */
7902 static dw_die_ref
7903 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7905 dw_die_ref c;
7906 dw_die_ref clone;
7907 struct decl_table_entry *entry;
7908 decl_table_entry **slot;
7910 if (die->die_tag == DW_TAG_subprogram)
7911 clone = clone_as_declaration (die);
7912 else
7913 clone = clone_die (die);
7915 slot = decl_table->find_slot_with_hash (die,
7916 htab_hash_pointer (die), INSERT);
7918 /* Assert that DIE isn't in the hash table yet. If it would be there
7919 before, the ancestors would be necessarily there as well, therefore
7920 clone_tree_partial wouldn't be called. */
7921 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7923 entry = XCNEW (struct decl_table_entry);
7924 entry->orig = die;
7925 entry->copy = clone;
7926 *slot = entry;
7928 if (die->die_tag != DW_TAG_subprogram)
7929 FOR_EACH_CHILD (die, c,
7930 add_child_die (clone, clone_tree_partial (c, decl_table)));
7932 return clone;
7935 /* Walk the DIE and its children, looking for references to incomplete
7936 or trivial types that are unmarked (i.e., that are not in the current
7937 type_unit). */
7939 static void
7940 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7942 dw_die_ref c;
7943 dw_attr_node *a;
7944 unsigned ix;
7946 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7948 if (AT_class (a) == dw_val_class_die_ref)
7950 dw_die_ref targ = AT_ref (a);
7951 decl_table_entry **slot;
7952 struct decl_table_entry *entry;
7954 if (targ->die_mark != 0 || targ->comdat_type_p)
7955 continue;
7957 slot = decl_table->find_slot_with_hash (targ,
7958 htab_hash_pointer (targ),
7959 INSERT);
7961 if (*slot != HTAB_EMPTY_ENTRY)
7963 /* TARG has already been copied, so we just need to
7964 modify the reference to point to the copy. */
7965 entry = *slot;
7966 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7968 else
7970 dw_die_ref parent = unit;
7971 dw_die_ref copy = clone_die (targ);
7973 /* Record in DECL_TABLE that TARG has been copied.
7974 Need to do this now, before the recursive call,
7975 because DECL_TABLE may be expanded and SLOT
7976 would no longer be a valid pointer. */
7977 entry = XCNEW (struct decl_table_entry);
7978 entry->orig = targ;
7979 entry->copy = copy;
7980 *slot = entry;
7982 /* If TARG is not a declaration DIE, we need to copy its
7983 children. */
7984 if (!is_declaration_die (targ))
7986 FOR_EACH_CHILD (
7987 targ, c,
7988 add_child_die (copy,
7989 clone_tree_partial (c, decl_table)));
7992 /* Make sure the cloned tree is marked as part of the
7993 type unit. */
7994 mark_dies (copy);
7996 /* If TARG has surrounding context, copy its ancestor tree
7997 into the new type unit. */
7998 if (targ->die_parent != NULL
7999 && !is_unit_die (targ->die_parent))
8000 parent = copy_ancestor_tree (unit, targ->die_parent,
8001 decl_table);
8003 add_child_die (parent, copy);
8004 a->dw_attr_val.v.val_die_ref.die = copy;
8006 /* Make sure the newly-copied DIE is walked. If it was
8007 installed in a previously-added context, it won't
8008 get visited otherwise. */
8009 if (parent != unit)
8011 /* Find the highest point of the newly-added tree,
8012 mark each node along the way, and walk from there. */
8013 parent->die_mark = 1;
8014 while (parent->die_parent
8015 && parent->die_parent->die_mark == 0)
8017 parent = parent->die_parent;
8018 parent->die_mark = 1;
8020 copy_decls_walk (unit, parent, decl_table);
8026 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8029 /* Copy declarations for "unworthy" types into the new comdat section.
8030 Incomplete types, modified types, and certain other types aren't broken
8031 out into comdat sections of their own, so they don't have a signature,
8032 and we need to copy the declaration into the same section so that we
8033 don't have an external reference. */
8035 static void
8036 copy_decls_for_unworthy_types (dw_die_ref unit)
8038 mark_dies (unit);
8039 decl_hash_type decl_table (10);
8040 copy_decls_walk (unit, unit, &decl_table);
8041 unmark_dies (unit);
8044 /* Traverse the DIE and add a sibling attribute if it may have the
8045 effect of speeding up access to siblings. To save some space,
8046 avoid generating sibling attributes for DIE's without children. */
8048 static void
8049 add_sibling_attributes (dw_die_ref die)
8051 dw_die_ref c;
8053 if (! die->die_child)
8054 return;
8056 if (die->die_parent && die != die->die_parent->die_child)
8057 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8059 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8062 /* Output all location lists for the DIE and its children. */
8064 static void
8065 output_location_lists (dw_die_ref die)
8067 dw_die_ref c;
8068 dw_attr_node *a;
8069 unsigned ix;
8071 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8072 if (AT_class (a) == dw_val_class_loc_list)
8073 output_loc_list (AT_loc_list (a));
8075 FOR_EACH_CHILD (die, c, output_location_lists (c));
8078 /* We want to limit the number of external references, because they are
8079 larger than local references: a relocation takes multiple words, and
8080 even a sig8 reference is always eight bytes, whereas a local reference
8081 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8082 So if we encounter multiple external references to the same type DIE, we
8083 make a local typedef stub for it and redirect all references there.
8085 This is the element of the hash table for keeping track of these
8086 references. */
8088 struct external_ref
8090 dw_die_ref type;
8091 dw_die_ref stub;
8092 unsigned n_refs;
8095 /* Hashtable helpers. */
8097 struct external_ref_hasher : free_ptr_hash <external_ref>
8099 static inline hashval_t hash (const external_ref *);
8100 static inline bool equal (const external_ref *, const external_ref *);
8103 inline hashval_t
8104 external_ref_hasher::hash (const external_ref *r)
8106 dw_die_ref die = r->type;
8107 hashval_t h = 0;
8109 /* We can't use the address of the DIE for hashing, because
8110 that will make the order of the stub DIEs non-deterministic. */
8111 if (! die->comdat_type_p)
8112 /* We have a symbol; use it to compute a hash. */
8113 h = htab_hash_string (die->die_id.die_symbol);
8114 else
8116 /* We have a type signature; use a subset of the bits as the hash.
8117 The 8-byte signature is at least as large as hashval_t. */
8118 comdat_type_node *type_node = die->die_id.die_type_node;
8119 memcpy (&h, type_node->signature, sizeof (h));
8121 return h;
8124 inline bool
8125 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8127 return r1->type == r2->type;
8130 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8132 /* Return a pointer to the external_ref for references to DIE. */
8134 static struct external_ref *
8135 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8137 struct external_ref ref, *ref_p;
8138 external_ref **slot;
8140 ref.type = die;
8141 slot = map->find_slot (&ref, INSERT);
8142 if (*slot != HTAB_EMPTY_ENTRY)
8143 return *slot;
8145 ref_p = XCNEW (struct external_ref);
8146 ref_p->type = die;
8147 *slot = ref_p;
8148 return ref_p;
8151 /* Subroutine of optimize_external_refs, below.
8153 If we see a type skeleton, record it as our stub. If we see external
8154 references, remember how many we've seen. */
8156 static void
8157 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8159 dw_die_ref c;
8160 dw_attr_node *a;
8161 unsigned ix;
8162 struct external_ref *ref_p;
8164 if (is_type_die (die)
8165 && (c = get_AT_ref (die, DW_AT_signature)))
8167 /* This is a local skeleton; use it for local references. */
8168 ref_p = lookup_external_ref (map, c);
8169 ref_p->stub = die;
8172 /* Scan the DIE references, and remember any that refer to DIEs from
8173 other CUs (i.e. those which are not marked). */
8174 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8175 if (AT_class (a) == dw_val_class_die_ref
8176 && (c = AT_ref (a))->die_mark == 0
8177 && is_type_die (c))
8179 ref_p = lookup_external_ref (map, c);
8180 ref_p->n_refs++;
8183 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8186 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8187 points to an external_ref, DATA is the CU we're processing. If we don't
8188 already have a local stub, and we have multiple refs, build a stub. */
8191 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8193 struct external_ref *ref_p = *slot;
8195 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8197 /* We have multiple references to this type, so build a small stub.
8198 Both of these forms are a bit dodgy from the perspective of the
8199 DWARF standard, since technically they should have names. */
8200 dw_die_ref cu = data;
8201 dw_die_ref type = ref_p->type;
8202 dw_die_ref stub = NULL;
8204 if (type->comdat_type_p)
8206 /* If we refer to this type via sig8, use AT_signature. */
8207 stub = new_die (type->die_tag, cu, NULL_TREE);
8208 add_AT_die_ref (stub, DW_AT_signature, type);
8210 else
8212 /* Otherwise, use a typedef with no name. */
8213 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8214 add_AT_die_ref (stub, DW_AT_type, type);
8217 stub->die_mark++;
8218 ref_p->stub = stub;
8220 return 1;
8223 /* DIE is a unit; look through all the DIE references to see if there are
8224 any external references to types, and if so, create local stubs for
8225 them which will be applied in build_abbrev_table. This is useful because
8226 references to local DIEs are smaller. */
8228 static external_ref_hash_type *
8229 optimize_external_refs (dw_die_ref die)
8231 external_ref_hash_type *map = new external_ref_hash_type (10);
8232 optimize_external_refs_1 (die, map);
8233 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8234 return map;
8237 /* The format of each DIE (and its attribute value pairs) is encoded in an
8238 abbreviation table. This routine builds the abbreviation table and assigns
8239 a unique abbreviation id for each abbreviation entry. The children of each
8240 die are visited recursively. */
8242 static void
8243 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8245 unsigned long abbrev_id;
8246 unsigned int n_alloc;
8247 dw_die_ref c;
8248 dw_attr_node *a;
8249 unsigned ix;
8251 /* Scan the DIE references, and replace any that refer to
8252 DIEs from other CUs (i.e. those which are not marked) with
8253 the local stubs we built in optimize_external_refs. */
8254 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8255 if (AT_class (a) == dw_val_class_die_ref
8256 && (c = AT_ref (a))->die_mark == 0)
8258 struct external_ref *ref_p;
8259 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8261 ref_p = lookup_external_ref (extern_map, c);
8262 if (ref_p->stub && ref_p->stub != die)
8263 change_AT_die_ref (a, ref_p->stub);
8264 else
8265 /* We aren't changing this reference, so mark it external. */
8266 set_AT_ref_external (a, 1);
8269 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8271 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8272 dw_attr_node *die_a, *abbrev_a;
8273 unsigned ix;
8274 bool ok = true;
8276 if (abbrev->die_tag != die->die_tag)
8277 continue;
8278 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8279 continue;
8281 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8282 continue;
8284 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8286 abbrev_a = &(*abbrev->die_attr)[ix];
8287 if ((abbrev_a->dw_attr != die_a->dw_attr)
8288 || (value_format (abbrev_a) != value_format (die_a)))
8290 ok = false;
8291 break;
8294 if (ok)
8295 break;
8298 if (abbrev_id >= abbrev_die_table_in_use)
8300 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8302 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8303 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8304 n_alloc);
8306 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8307 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8308 abbrev_die_table_allocated = n_alloc;
8311 ++abbrev_die_table_in_use;
8312 abbrev_die_table[abbrev_id] = die;
8315 die->die_abbrev = abbrev_id;
8316 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8319 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8321 static int
8322 constant_size (unsigned HOST_WIDE_INT value)
8324 int log;
8326 if (value == 0)
8327 log = 0;
8328 else
8329 log = floor_log2 (value);
8331 log = log / 8;
8332 log = 1 << (floor_log2 (log) + 1);
8334 return log;
8337 /* Return the size of a DIE as it is represented in the
8338 .debug_info section. */
8340 static unsigned long
8341 size_of_die (dw_die_ref die)
8343 unsigned long size = 0;
8344 dw_attr_node *a;
8345 unsigned ix;
8346 enum dwarf_form form;
8348 size += size_of_uleb128 (die->die_abbrev);
8349 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8351 switch (AT_class (a))
8353 case dw_val_class_addr:
8354 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8356 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8357 size += size_of_uleb128 (AT_index (a));
8359 else
8360 size += DWARF2_ADDR_SIZE;
8361 break;
8362 case dw_val_class_offset:
8363 size += DWARF_OFFSET_SIZE;
8364 break;
8365 case dw_val_class_loc:
8367 unsigned long lsize = size_of_locs (AT_loc (a));
8369 /* Block length. */
8370 if (dwarf_version >= 4)
8371 size += size_of_uleb128 (lsize);
8372 else
8373 size += constant_size (lsize);
8374 size += lsize;
8376 break;
8377 case dw_val_class_loc_list:
8378 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8380 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8381 size += size_of_uleb128 (AT_index (a));
8383 else
8384 size += DWARF_OFFSET_SIZE;
8385 break;
8386 case dw_val_class_range_list:
8387 size += DWARF_OFFSET_SIZE;
8388 break;
8389 case dw_val_class_const:
8390 size += size_of_sleb128 (AT_int (a));
8391 break;
8392 case dw_val_class_unsigned_const:
8394 int csize = constant_size (AT_unsigned (a));
8395 if (dwarf_version == 3
8396 && a->dw_attr == DW_AT_data_member_location
8397 && csize >= 4)
8398 size += size_of_uleb128 (AT_unsigned (a));
8399 else
8400 size += csize;
8402 break;
8403 case dw_val_class_const_double:
8404 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8405 if (HOST_BITS_PER_WIDE_INT >= 64)
8406 size++; /* block */
8407 break;
8408 case dw_val_class_wide_int:
8409 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8410 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8411 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8412 > 64)
8413 size++; /* block */
8414 break;
8415 case dw_val_class_vec:
8416 size += constant_size (a->dw_attr_val.v.val_vec.length
8417 * a->dw_attr_val.v.val_vec.elt_size)
8418 + a->dw_attr_val.v.val_vec.length
8419 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8420 break;
8421 case dw_val_class_flag:
8422 if (dwarf_version >= 4)
8423 /* Currently all add_AT_flag calls pass in 1 as last argument,
8424 so DW_FORM_flag_present can be used. If that ever changes,
8425 we'll need to use DW_FORM_flag and have some optimization
8426 in build_abbrev_table that will change those to
8427 DW_FORM_flag_present if it is set to 1 in all DIEs using
8428 the same abbrev entry. */
8429 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8430 else
8431 size += 1;
8432 break;
8433 case dw_val_class_die_ref:
8434 if (AT_ref_external (a))
8436 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8437 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8438 is sized by target address length, whereas in DWARF3
8439 it's always sized as an offset. */
8440 if (use_debug_types)
8441 size += DWARF_TYPE_SIGNATURE_SIZE;
8442 else if (dwarf_version == 2)
8443 size += DWARF2_ADDR_SIZE;
8444 else
8445 size += DWARF_OFFSET_SIZE;
8447 else
8448 size += DWARF_OFFSET_SIZE;
8449 break;
8450 case dw_val_class_fde_ref:
8451 size += DWARF_OFFSET_SIZE;
8452 break;
8453 case dw_val_class_lbl_id:
8454 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8456 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8457 size += size_of_uleb128 (AT_index (a));
8459 else
8460 size += DWARF2_ADDR_SIZE;
8461 break;
8462 case dw_val_class_lineptr:
8463 case dw_val_class_macptr:
8464 size += DWARF_OFFSET_SIZE;
8465 break;
8466 case dw_val_class_str:
8467 form = AT_string_form (a);
8468 if (form == DW_FORM_strp)
8469 size += DWARF_OFFSET_SIZE;
8470 else if (form == DW_FORM_GNU_str_index)
8471 size += size_of_uleb128 (AT_index (a));
8472 else
8473 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8474 break;
8475 case dw_val_class_file:
8476 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8477 break;
8478 case dw_val_class_data8:
8479 size += 8;
8480 break;
8481 case dw_val_class_vms_delta:
8482 size += DWARF_OFFSET_SIZE;
8483 break;
8484 case dw_val_class_high_pc:
8485 size += DWARF2_ADDR_SIZE;
8486 break;
8487 case dw_val_class_discr_value:
8488 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8489 break;
8490 case dw_val_class_discr_list:
8492 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8494 /* This is a block, so we have the block length and then its
8495 data. */
8496 size += constant_size (block_size) + block_size;
8498 break;
8499 default:
8500 gcc_unreachable ();
8504 return size;
8507 /* Size the debugging information associated with a given DIE. Visits the
8508 DIE's children recursively. Updates the global variable next_die_offset, on
8509 each time through. Uses the current value of next_die_offset to update the
8510 die_offset field in each DIE. */
8512 static void
8513 calc_die_sizes (dw_die_ref die)
8515 dw_die_ref c;
8517 gcc_assert (die->die_offset == 0
8518 || (unsigned long int) die->die_offset == next_die_offset);
8519 die->die_offset = next_die_offset;
8520 next_die_offset += size_of_die (die);
8522 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8524 if (die->die_child != NULL)
8525 /* Count the null byte used to terminate sibling lists. */
8526 next_die_offset += 1;
8529 /* Size just the base type children at the start of the CU.
8530 This is needed because build_abbrev needs to size locs
8531 and sizing of type based stack ops needs to know die_offset
8532 values for the base types. */
8534 static void
8535 calc_base_type_die_sizes (void)
8537 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8538 unsigned int i;
8539 dw_die_ref base_type;
8540 #if ENABLE_ASSERT_CHECKING
8541 dw_die_ref prev = comp_unit_die ()->die_child;
8542 #endif
8544 die_offset += size_of_die (comp_unit_die ());
8545 for (i = 0; base_types.iterate (i, &base_type); i++)
8547 #if ENABLE_ASSERT_CHECKING
8548 gcc_assert (base_type->die_offset == 0
8549 && prev->die_sib == base_type
8550 && base_type->die_child == NULL
8551 && base_type->die_abbrev);
8552 prev = base_type;
8553 #endif
8554 base_type->die_offset = die_offset;
8555 die_offset += size_of_die (base_type);
8559 /* Set the marks for a die and its children. We do this so
8560 that we know whether or not a reference needs to use FORM_ref_addr; only
8561 DIEs in the same CU will be marked. We used to clear out the offset
8562 and use that as the flag, but ran into ordering problems. */
8564 static void
8565 mark_dies (dw_die_ref die)
8567 dw_die_ref c;
8569 gcc_assert (!die->die_mark);
8571 die->die_mark = 1;
8572 FOR_EACH_CHILD (die, c, mark_dies (c));
8575 /* Clear the marks for a die and its children. */
8577 static void
8578 unmark_dies (dw_die_ref die)
8580 dw_die_ref c;
8582 if (! use_debug_types)
8583 gcc_assert (die->die_mark);
8585 die->die_mark = 0;
8586 FOR_EACH_CHILD (die, c, unmark_dies (c));
8589 /* Clear the marks for a die, its children and referred dies. */
8591 static void
8592 unmark_all_dies (dw_die_ref die)
8594 dw_die_ref c;
8595 dw_attr_node *a;
8596 unsigned ix;
8598 if (!die->die_mark)
8599 return;
8600 die->die_mark = 0;
8602 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8604 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8605 if (AT_class (a) == dw_val_class_die_ref)
8606 unmark_all_dies (AT_ref (a));
8609 /* Calculate if the entry should appear in the final output file. It may be
8610 from a pruned a type. */
8612 static bool
8613 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8615 /* By limiting gnu pubnames to definitions only, gold can generate a
8616 gdb index without entries for declarations, which don't include
8617 enough information to be useful. */
8618 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8619 return false;
8621 if (table == pubname_table)
8623 /* Enumerator names are part of the pubname table, but the
8624 parent DW_TAG_enumeration_type die may have been pruned.
8625 Don't output them if that is the case. */
8626 if (p->die->die_tag == DW_TAG_enumerator &&
8627 (p->die->die_parent == NULL
8628 || !p->die->die_parent->die_perennial_p))
8629 return false;
8631 /* Everything else in the pubname table is included. */
8632 return true;
8635 /* The pubtypes table shouldn't include types that have been
8636 pruned. */
8637 return (p->die->die_offset != 0
8638 || !flag_eliminate_unused_debug_types);
8641 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8642 generated for the compilation unit. */
8644 static unsigned long
8645 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8647 unsigned long size;
8648 unsigned i;
8649 pubname_entry *p;
8650 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8652 size = DWARF_PUBNAMES_HEADER_SIZE;
8653 FOR_EACH_VEC_ELT (*names, i, p)
8654 if (include_pubname_in_output (names, p))
8655 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8657 size += DWARF_OFFSET_SIZE;
8658 return size;
8661 /* Return the size of the information in the .debug_aranges section. */
8663 static unsigned long
8664 size_of_aranges (void)
8666 unsigned long size;
8668 size = DWARF_ARANGES_HEADER_SIZE;
8670 /* Count the address/length pair for this compilation unit. */
8671 if (text_section_used)
8672 size += 2 * DWARF2_ADDR_SIZE;
8673 if (cold_text_section_used)
8674 size += 2 * DWARF2_ADDR_SIZE;
8675 if (have_multiple_function_sections)
8677 unsigned fde_idx;
8678 dw_fde_ref fde;
8680 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8682 if (DECL_IGNORED_P (fde->decl))
8683 continue;
8684 if (!fde->in_std_section)
8685 size += 2 * DWARF2_ADDR_SIZE;
8686 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8687 size += 2 * DWARF2_ADDR_SIZE;
8691 /* Count the two zero words used to terminated the address range table. */
8692 size += 2 * DWARF2_ADDR_SIZE;
8693 return size;
8696 /* Select the encoding of an attribute value. */
8698 static enum dwarf_form
8699 value_format (dw_attr_node *a)
8701 switch (AT_class (a))
8703 case dw_val_class_addr:
8704 /* Only very few attributes allow DW_FORM_addr. */
8705 switch (a->dw_attr)
8707 case DW_AT_low_pc:
8708 case DW_AT_high_pc:
8709 case DW_AT_entry_pc:
8710 case DW_AT_trampoline:
8711 return (AT_index (a) == NOT_INDEXED
8712 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8713 default:
8714 break;
8716 switch (DWARF2_ADDR_SIZE)
8718 case 1:
8719 return DW_FORM_data1;
8720 case 2:
8721 return DW_FORM_data2;
8722 case 4:
8723 return DW_FORM_data4;
8724 case 8:
8725 return DW_FORM_data8;
8726 default:
8727 gcc_unreachable ();
8729 case dw_val_class_range_list:
8730 case dw_val_class_loc_list:
8731 if (dwarf_version >= 4)
8732 return DW_FORM_sec_offset;
8733 /* FALLTHRU */
8734 case dw_val_class_vms_delta:
8735 case dw_val_class_offset:
8736 switch (DWARF_OFFSET_SIZE)
8738 case 4:
8739 return DW_FORM_data4;
8740 case 8:
8741 return DW_FORM_data8;
8742 default:
8743 gcc_unreachable ();
8745 case dw_val_class_loc:
8746 if (dwarf_version >= 4)
8747 return DW_FORM_exprloc;
8748 switch (constant_size (size_of_locs (AT_loc (a))))
8750 case 1:
8751 return DW_FORM_block1;
8752 case 2:
8753 return DW_FORM_block2;
8754 case 4:
8755 return DW_FORM_block4;
8756 default:
8757 gcc_unreachable ();
8759 case dw_val_class_const:
8760 return DW_FORM_sdata;
8761 case dw_val_class_unsigned_const:
8762 switch (constant_size (AT_unsigned (a)))
8764 case 1:
8765 return DW_FORM_data1;
8766 case 2:
8767 return DW_FORM_data2;
8768 case 4:
8769 /* In DWARF3 DW_AT_data_member_location with
8770 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8771 constant, so we need to use DW_FORM_udata if we need
8772 a large constant. */
8773 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8774 return DW_FORM_udata;
8775 return DW_FORM_data4;
8776 case 8:
8777 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8778 return DW_FORM_udata;
8779 return DW_FORM_data8;
8780 default:
8781 gcc_unreachable ();
8783 case dw_val_class_const_double:
8784 switch (HOST_BITS_PER_WIDE_INT)
8786 case 8:
8787 return DW_FORM_data2;
8788 case 16:
8789 return DW_FORM_data4;
8790 case 32:
8791 return DW_FORM_data8;
8792 case 64:
8793 default:
8794 return DW_FORM_block1;
8796 case dw_val_class_wide_int:
8797 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8799 case 8:
8800 return DW_FORM_data1;
8801 case 16:
8802 return DW_FORM_data2;
8803 case 32:
8804 return DW_FORM_data4;
8805 case 64:
8806 return DW_FORM_data8;
8807 default:
8808 return DW_FORM_block1;
8810 case dw_val_class_vec:
8811 switch (constant_size (a->dw_attr_val.v.val_vec.length
8812 * a->dw_attr_val.v.val_vec.elt_size))
8814 case 1:
8815 return DW_FORM_block1;
8816 case 2:
8817 return DW_FORM_block2;
8818 case 4:
8819 return DW_FORM_block4;
8820 default:
8821 gcc_unreachable ();
8823 case dw_val_class_flag:
8824 if (dwarf_version >= 4)
8826 /* Currently all add_AT_flag calls pass in 1 as last argument,
8827 so DW_FORM_flag_present can be used. If that ever changes,
8828 we'll need to use DW_FORM_flag and have some optimization
8829 in build_abbrev_table that will change those to
8830 DW_FORM_flag_present if it is set to 1 in all DIEs using
8831 the same abbrev entry. */
8832 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8833 return DW_FORM_flag_present;
8835 return DW_FORM_flag;
8836 case dw_val_class_die_ref:
8837 if (AT_ref_external (a))
8838 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8839 else
8840 return DW_FORM_ref;
8841 case dw_val_class_fde_ref:
8842 return DW_FORM_data;
8843 case dw_val_class_lbl_id:
8844 return (AT_index (a) == NOT_INDEXED
8845 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8846 case dw_val_class_lineptr:
8847 case dw_val_class_macptr:
8848 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8849 case dw_val_class_str:
8850 return AT_string_form (a);
8851 case dw_val_class_file:
8852 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8854 case 1:
8855 return DW_FORM_data1;
8856 case 2:
8857 return DW_FORM_data2;
8858 case 4:
8859 return DW_FORM_data4;
8860 default:
8861 gcc_unreachable ();
8864 case dw_val_class_data8:
8865 return DW_FORM_data8;
8867 case dw_val_class_high_pc:
8868 switch (DWARF2_ADDR_SIZE)
8870 case 1:
8871 return DW_FORM_data1;
8872 case 2:
8873 return DW_FORM_data2;
8874 case 4:
8875 return DW_FORM_data4;
8876 case 8:
8877 return DW_FORM_data8;
8878 default:
8879 gcc_unreachable ();
8882 case dw_val_class_discr_value:
8883 return (a->dw_attr_val.v.val_discr_value.pos
8884 ? DW_FORM_udata
8885 : DW_FORM_sdata);
8886 case dw_val_class_discr_list:
8887 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8889 case 1:
8890 return DW_FORM_block1;
8891 case 2:
8892 return DW_FORM_block2;
8893 case 4:
8894 return DW_FORM_block4;
8895 default:
8896 gcc_unreachable ();
8899 default:
8900 gcc_unreachable ();
8904 /* Output the encoding of an attribute value. */
8906 static void
8907 output_value_format (dw_attr_node *a)
8909 enum dwarf_form form = value_format (a);
8911 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8914 /* Given a die and id, produce the appropriate abbreviations. */
8916 static void
8917 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8919 unsigned ix;
8920 dw_attr_node *a_attr;
8922 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8923 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8924 dwarf_tag_name (abbrev->die_tag));
8926 if (abbrev->die_child != NULL)
8927 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8928 else
8929 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8931 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8933 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8934 dwarf_attr_name (a_attr->dw_attr));
8935 output_value_format (a_attr);
8938 dw2_asm_output_data (1, 0, NULL);
8939 dw2_asm_output_data (1, 0, NULL);
8943 /* Output the .debug_abbrev section which defines the DIE abbreviation
8944 table. */
8946 static void
8947 output_abbrev_section (void)
8949 unsigned long abbrev_id;
8951 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8952 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8954 /* Terminate the table. */
8955 dw2_asm_output_data (1, 0, NULL);
8958 /* Output a symbol we can use to refer to this DIE from another CU. */
8960 static inline void
8961 output_die_symbol (dw_die_ref die)
8963 const char *sym = die->die_id.die_symbol;
8965 gcc_assert (!die->comdat_type_p);
8967 if (sym == 0)
8968 return;
8970 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8971 /* We make these global, not weak; if the target doesn't support
8972 .linkonce, it doesn't support combining the sections, so debugging
8973 will break. */
8974 targetm.asm_out.globalize_label (asm_out_file, sym);
8976 ASM_OUTPUT_LABEL (asm_out_file, sym);
8979 /* Return a new location list, given the begin and end range, and the
8980 expression. */
8982 static inline dw_loc_list_ref
8983 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8984 const char *section)
8986 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8988 retlist->begin = begin;
8989 retlist->begin_entry = NULL;
8990 retlist->end = end;
8991 retlist->expr = expr;
8992 retlist->section = section;
8994 return retlist;
8997 /* Generate a new internal symbol for this location list node, if it
8998 hasn't got one yet. */
9000 static inline void
9001 gen_llsym (dw_loc_list_ref list)
9003 gcc_assert (!list->ll_symbol);
9004 list->ll_symbol = gen_internal_sym ("LLST");
9007 /* Output the location list given to us. */
9009 static void
9010 output_loc_list (dw_loc_list_ref list_head)
9012 dw_loc_list_ref curr = list_head;
9014 if (list_head->emitted)
9015 return;
9016 list_head->emitted = true;
9018 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9020 /* Walk the location list, and output each range + expression. */
9021 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9023 unsigned long size;
9024 /* Don't output an entry that starts and ends at the same address. */
9025 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9026 continue;
9027 size = size_of_locs (curr->expr);
9028 /* If the expression is too large, drop it on the floor. We could
9029 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9030 in the expression, but >= 64KB expressions for a single value
9031 in a single range are unlikely very useful. */
9032 if (size > 0xffff)
9033 continue;
9034 if (dwarf_split_debug_info)
9036 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9037 "Location list start/length entry (%s)",
9038 list_head->ll_symbol);
9039 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9040 "Location list range start index (%s)",
9041 curr->begin);
9042 /* The length field is 4 bytes. If we ever need to support
9043 an 8-byte length, we can add a new DW_LLE code or fall back
9044 to DW_LLE_GNU_start_end_entry. */
9045 dw2_asm_output_delta (4, curr->end, curr->begin,
9046 "Location list range length (%s)",
9047 list_head->ll_symbol);
9049 else if (!have_multiple_function_sections)
9051 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9052 "Location list begin address (%s)",
9053 list_head->ll_symbol);
9054 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9055 "Location list end address (%s)",
9056 list_head->ll_symbol);
9058 else
9060 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9061 "Location list begin address (%s)",
9062 list_head->ll_symbol);
9063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9064 "Location list end address (%s)",
9065 list_head->ll_symbol);
9068 /* Output the block length for this list of location operations. */
9069 gcc_assert (size <= 0xffff);
9070 dw2_asm_output_data (2, size, "%s", "Location expression size");
9072 output_loc_sequence (curr->expr, -1);
9075 if (dwarf_split_debug_info)
9076 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9077 "Location list terminator (%s)",
9078 list_head->ll_symbol);
9079 else
9081 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9082 "Location list terminator begin (%s)",
9083 list_head->ll_symbol);
9084 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9085 "Location list terminator end (%s)",
9086 list_head->ll_symbol);
9090 /* Output a range_list offset into the debug_range section. Emit a
9091 relocated reference if val_entry is NULL, otherwise, emit an
9092 indirect reference. */
9094 static void
9095 output_range_list_offset (dw_attr_node *a)
9097 const char *name = dwarf_attr_name (a->dw_attr);
9099 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9101 char *p = strchr (ranges_section_label, '\0');
9102 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9103 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9104 debug_ranges_section, "%s", name);
9105 *p = '\0';
9107 else
9108 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9109 "%s (offset from %s)", name, ranges_section_label);
9112 /* Output the offset into the debug_loc section. */
9114 static void
9115 output_loc_list_offset (dw_attr_node *a)
9117 char *sym = AT_loc_list (a)->ll_symbol;
9119 gcc_assert (sym);
9120 if (dwarf_split_debug_info)
9121 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9122 "%s", dwarf_attr_name (a->dw_attr));
9123 else
9124 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9125 "%s", dwarf_attr_name (a->dw_attr));
9128 /* Output an attribute's index or value appropriately. */
9130 static void
9131 output_attr_index_or_value (dw_attr_node *a)
9133 const char *name = dwarf_attr_name (a->dw_attr);
9135 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9137 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9138 return;
9140 switch (AT_class (a))
9142 case dw_val_class_addr:
9143 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9144 break;
9145 case dw_val_class_high_pc:
9146 case dw_val_class_lbl_id:
9147 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9148 break;
9149 case dw_val_class_loc_list:
9150 output_loc_list_offset (a);
9151 break;
9152 default:
9153 gcc_unreachable ();
9157 /* Output a type signature. */
9159 static inline void
9160 output_signature (const char *sig, const char *name)
9162 int i;
9164 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9165 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9168 /* Output a discriminant value. */
9170 static inline void
9171 output_discr_value (dw_discr_value *discr_value, const char *name)
9173 if (discr_value->pos)
9174 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9175 else
9176 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9179 /* Output the DIE and its attributes. Called recursively to generate
9180 the definitions of each child DIE. */
9182 static void
9183 output_die (dw_die_ref die)
9185 dw_attr_node *a;
9186 dw_die_ref c;
9187 unsigned long size;
9188 unsigned ix;
9190 /* If someone in another CU might refer to us, set up a symbol for
9191 them to point to. */
9192 if (! die->comdat_type_p && die->die_id.die_symbol)
9193 output_die_symbol (die);
9195 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9196 (unsigned long)die->die_offset,
9197 dwarf_tag_name (die->die_tag));
9199 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9201 const char *name = dwarf_attr_name (a->dw_attr);
9203 switch (AT_class (a))
9205 case dw_val_class_addr:
9206 output_attr_index_or_value (a);
9207 break;
9209 case dw_val_class_offset:
9210 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9211 "%s", name);
9212 break;
9214 case dw_val_class_range_list:
9215 output_range_list_offset (a);
9216 break;
9218 case dw_val_class_loc:
9219 size = size_of_locs (AT_loc (a));
9221 /* Output the block length for this list of location operations. */
9222 if (dwarf_version >= 4)
9223 dw2_asm_output_data_uleb128 (size, "%s", name);
9224 else
9225 dw2_asm_output_data (constant_size (size), size, "%s", name);
9227 output_loc_sequence (AT_loc (a), -1);
9228 break;
9230 case dw_val_class_const:
9231 /* ??? It would be slightly more efficient to use a scheme like is
9232 used for unsigned constants below, but gdb 4.x does not sign
9233 extend. Gdb 5.x does sign extend. */
9234 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9235 break;
9237 case dw_val_class_unsigned_const:
9239 int csize = constant_size (AT_unsigned (a));
9240 if (dwarf_version == 3
9241 && a->dw_attr == DW_AT_data_member_location
9242 && csize >= 4)
9243 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9244 else
9245 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9247 break;
9249 case dw_val_class_const_double:
9251 unsigned HOST_WIDE_INT first, second;
9253 if (HOST_BITS_PER_WIDE_INT >= 64)
9254 dw2_asm_output_data (1,
9255 HOST_BITS_PER_DOUBLE_INT
9256 / HOST_BITS_PER_CHAR,
9257 NULL);
9259 if (WORDS_BIG_ENDIAN)
9261 first = a->dw_attr_val.v.val_double.high;
9262 second = a->dw_attr_val.v.val_double.low;
9264 else
9266 first = a->dw_attr_val.v.val_double.low;
9267 second = a->dw_attr_val.v.val_double.high;
9270 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9271 first, "%s", name);
9272 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9273 second, NULL);
9275 break;
9277 case dw_val_class_wide_int:
9279 int i;
9280 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9281 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9282 if (len * HOST_BITS_PER_WIDE_INT > 64)
9283 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9284 NULL);
9286 if (WORDS_BIG_ENDIAN)
9287 for (i = len - 1; i >= 0; --i)
9289 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9290 "%s", name);
9291 name = "";
9293 else
9294 for (i = 0; i < len; ++i)
9296 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9297 "%s", name);
9298 name = "";
9301 break;
9303 case dw_val_class_vec:
9305 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9306 unsigned int len = a->dw_attr_val.v.val_vec.length;
9307 unsigned int i;
9308 unsigned char *p;
9310 dw2_asm_output_data (constant_size (len * elt_size),
9311 len * elt_size, "%s", name);
9312 if (elt_size > sizeof (HOST_WIDE_INT))
9314 elt_size /= 2;
9315 len *= 2;
9317 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9318 i < len;
9319 i++, p += elt_size)
9320 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9321 "fp or vector constant word %u", i);
9322 break;
9325 case dw_val_class_flag:
9326 if (dwarf_version >= 4)
9328 /* Currently all add_AT_flag calls pass in 1 as last argument,
9329 so DW_FORM_flag_present can be used. If that ever changes,
9330 we'll need to use DW_FORM_flag and have some optimization
9331 in build_abbrev_table that will change those to
9332 DW_FORM_flag_present if it is set to 1 in all DIEs using
9333 the same abbrev entry. */
9334 gcc_assert (AT_flag (a) == 1);
9335 if (flag_debug_asm)
9336 fprintf (asm_out_file, "\t\t\t%s %s\n",
9337 ASM_COMMENT_START, name);
9338 break;
9340 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9341 break;
9343 case dw_val_class_loc_list:
9344 output_attr_index_or_value (a);
9345 break;
9347 case dw_val_class_die_ref:
9348 if (AT_ref_external (a))
9350 if (AT_ref (a)->comdat_type_p)
9352 comdat_type_node *type_node =
9353 AT_ref (a)->die_id.die_type_node;
9355 gcc_assert (type_node);
9356 output_signature (type_node->signature, name);
9358 else
9360 const char *sym = AT_ref (a)->die_id.die_symbol;
9361 int size;
9363 gcc_assert (sym);
9364 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9365 length, whereas in DWARF3 it's always sized as an
9366 offset. */
9367 if (dwarf_version == 2)
9368 size = DWARF2_ADDR_SIZE;
9369 else
9370 size = DWARF_OFFSET_SIZE;
9371 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9372 name);
9375 else
9377 gcc_assert (AT_ref (a)->die_offset);
9378 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9379 "%s", name);
9381 break;
9383 case dw_val_class_fde_ref:
9385 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
9387 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9388 a->dw_attr_val.v.val_fde_index * 2);
9389 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9390 "%s", name);
9392 break;
9394 case dw_val_class_vms_delta:
9395 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9396 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9397 AT_vms_delta2 (a), AT_vms_delta1 (a),
9398 "%s", name);
9399 #else
9400 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9401 AT_vms_delta2 (a), AT_vms_delta1 (a),
9402 "%s", name);
9403 #endif
9404 break;
9406 case dw_val_class_lbl_id:
9407 output_attr_index_or_value (a);
9408 break;
9410 case dw_val_class_lineptr:
9411 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9412 debug_line_section, "%s", name);
9413 break;
9415 case dw_val_class_macptr:
9416 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9417 debug_macinfo_section, "%s", name);
9418 break;
9420 case dw_val_class_str:
9421 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9422 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9423 a->dw_attr_val.v.val_str->label,
9424 debug_str_section,
9425 "%s: \"%s\"", name, AT_string (a));
9426 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9427 dw2_asm_output_data_uleb128 (AT_index (a),
9428 "%s: \"%s\"", name, AT_string (a));
9429 else
9430 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9431 break;
9433 case dw_val_class_file:
9435 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9437 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9438 a->dw_attr_val.v.val_file->filename);
9439 break;
9442 case dw_val_class_data8:
9444 int i;
9446 for (i = 0; i < 8; i++)
9447 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9448 i == 0 ? "%s" : NULL, name);
9449 break;
9452 case dw_val_class_high_pc:
9453 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9454 get_AT_low_pc (die), "DW_AT_high_pc");
9455 break;
9457 case dw_val_class_discr_value:
9458 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9459 break;
9461 case dw_val_class_discr_list:
9463 dw_discr_list_ref list = AT_discr_list (a);
9464 const int size = size_of_discr_list (list);
9466 /* This is a block, so output its length first. */
9467 dw2_asm_output_data (constant_size (size), size,
9468 "%s: block size", name);
9470 for (; list != NULL; list = list->dw_discr_next)
9472 /* One byte for the discriminant value descriptor, and then as
9473 many LEB128 numbers as required. */
9474 if (list->dw_discr_range)
9475 dw2_asm_output_data (1, DW_DSC_range,
9476 "%s: DW_DSC_range", name);
9477 else
9478 dw2_asm_output_data (1, DW_DSC_label,
9479 "%s: DW_DSC_label", name);
9481 output_discr_value (&list->dw_discr_lower_bound, name);
9482 if (list->dw_discr_range)
9483 output_discr_value (&list->dw_discr_upper_bound, name);
9485 break;
9488 default:
9489 gcc_unreachable ();
9493 FOR_EACH_CHILD (die, c, output_die (c));
9495 /* Add null byte to terminate sibling list. */
9496 if (die->die_child != NULL)
9497 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9498 (unsigned long) die->die_offset);
9501 /* Output the compilation unit that appears at the beginning of the
9502 .debug_info section, and precedes the DIE descriptions. */
9504 static void
9505 output_compilation_unit_header (void)
9507 /* We don't support actual DWARFv5 units yet, we just use some
9508 DWARFv5 draft DIE tags in DWARFv4 format. */
9509 int ver = dwarf_version < 5 ? dwarf_version : 4;
9511 if (!XCOFF_DEBUGGING_INFO)
9513 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9514 dw2_asm_output_data (4, 0xffffffff,
9515 "Initial length escape value indicating 64-bit DWARF extension");
9516 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9517 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9518 "Length of Compilation Unit Info");
9521 dw2_asm_output_data (2, ver, "DWARF version number");
9522 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9523 debug_abbrev_section,
9524 "Offset Into Abbrev. Section");
9525 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9528 /* Output the compilation unit DIE and its children. */
9530 static void
9531 output_comp_unit (dw_die_ref die, int output_if_empty)
9533 const char *secname, *oldsym;
9534 char *tmp;
9536 /* Unless we are outputting main CU, we may throw away empty ones. */
9537 if (!output_if_empty && die->die_child == NULL)
9538 return;
9540 /* Even if there are no children of this DIE, we must output the information
9541 about the compilation unit. Otherwise, on an empty translation unit, we
9542 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9543 will then complain when examining the file. First mark all the DIEs in
9544 this CU so we know which get local refs. */
9545 mark_dies (die);
9547 external_ref_hash_type *extern_map = optimize_external_refs (die);
9549 build_abbrev_table (die, extern_map);
9551 delete extern_map;
9553 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9554 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9555 calc_die_sizes (die);
9557 oldsym = die->die_id.die_symbol;
9558 if (oldsym)
9560 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9562 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9563 secname = tmp;
9564 die->die_id.die_symbol = NULL;
9565 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9567 else
9569 switch_to_section (debug_info_section);
9570 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9571 info_section_emitted = true;
9574 /* Output debugging information. */
9575 output_compilation_unit_header ();
9576 output_die (die);
9578 /* Leave the marks on the main CU, so we can check them in
9579 output_pubnames. */
9580 if (oldsym)
9582 unmark_dies (die);
9583 die->die_id.die_symbol = oldsym;
9587 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9588 and .debug_pubtypes. This is configured per-target, but can be
9589 overridden by the -gpubnames or -gno-pubnames options. */
9591 static inline bool
9592 want_pubnames (void)
9594 if (debug_info_level <= DINFO_LEVEL_TERSE)
9595 return false;
9596 if (debug_generate_pub_sections != -1)
9597 return debug_generate_pub_sections;
9598 return targetm.want_debug_pub_sections;
9601 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9603 static void
9604 add_AT_pubnames (dw_die_ref die)
9606 if (want_pubnames ())
9607 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9610 /* Add a string attribute value to a skeleton DIE. */
9612 static inline void
9613 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9614 const char *str)
9616 dw_attr_node attr;
9617 struct indirect_string_node *node;
9619 if (! skeleton_debug_str_hash)
9620 skeleton_debug_str_hash
9621 = hash_table<indirect_string_hasher>::create_ggc (10);
9623 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9624 find_string_form (node);
9625 if (node->form == DW_FORM_GNU_str_index)
9626 node->form = DW_FORM_strp;
9628 attr.dw_attr = attr_kind;
9629 attr.dw_attr_val.val_class = dw_val_class_str;
9630 attr.dw_attr_val.val_entry = NULL;
9631 attr.dw_attr_val.v.val_str = node;
9632 add_dwarf_attr (die, &attr);
9635 /* Helper function to generate top-level dies for skeleton debug_info and
9636 debug_types. */
9638 static void
9639 add_top_level_skeleton_die_attrs (dw_die_ref die)
9641 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9642 const char *comp_dir = comp_dir_string ();
9644 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9645 if (comp_dir != NULL)
9646 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9647 add_AT_pubnames (die);
9648 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9651 /* Output skeleton debug sections that point to the dwo file. */
9653 static void
9654 output_skeleton_debug_sections (dw_die_ref comp_unit)
9656 /* We don't support actual DWARFv5 units yet, we just use some
9657 DWARFv5 draft DIE tags in DWARFv4 format. */
9658 int ver = dwarf_version < 5 ? dwarf_version : 4;
9660 /* These attributes will be found in the full debug_info section. */
9661 remove_AT (comp_unit, DW_AT_producer);
9662 remove_AT (comp_unit, DW_AT_language);
9664 switch_to_section (debug_skeleton_info_section);
9665 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9667 /* Produce the skeleton compilation-unit header. This one differs enough from
9668 a normal CU header that it's better not to call output_compilation_unit
9669 header. */
9670 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9671 dw2_asm_output_data (4, 0xffffffff,
9672 "Initial length escape value indicating 64-bit DWARF extension");
9674 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9675 DWARF_COMPILE_UNIT_HEADER_SIZE
9676 - DWARF_INITIAL_LENGTH_SIZE
9677 + size_of_die (comp_unit),
9678 "Length of Compilation Unit Info");
9679 dw2_asm_output_data (2, ver, "DWARF version number");
9680 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9681 debug_abbrev_section,
9682 "Offset Into Abbrev. Section");
9683 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9685 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9686 output_die (comp_unit);
9688 /* Build the skeleton debug_abbrev section. */
9689 switch_to_section (debug_skeleton_abbrev_section);
9690 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9692 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9694 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9697 /* Output a comdat type unit DIE and its children. */
9699 static void
9700 output_comdat_type_unit (comdat_type_node *node)
9702 const char *secname;
9703 char *tmp;
9704 int i;
9705 #if defined (OBJECT_FORMAT_ELF)
9706 tree comdat_key;
9707 #endif
9709 /* First mark all the DIEs in this CU so we know which get local refs. */
9710 mark_dies (node->root_die);
9712 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9714 build_abbrev_table (node->root_die, extern_map);
9716 delete extern_map;
9717 extern_map = NULL;
9719 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9720 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9721 calc_die_sizes (node->root_die);
9723 #if defined (OBJECT_FORMAT_ELF)
9724 if (!dwarf_split_debug_info)
9725 secname = ".debug_types";
9726 else
9727 secname = ".debug_types.dwo";
9729 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9730 sprintf (tmp, "wt.");
9731 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9732 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9733 comdat_key = get_identifier (tmp);
9734 targetm.asm_out.named_section (secname,
9735 SECTION_DEBUG | SECTION_LINKONCE,
9736 comdat_key);
9737 #else
9738 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9739 sprintf (tmp, ".gnu.linkonce.wt.");
9740 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9741 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9742 secname = tmp;
9743 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9744 #endif
9746 /* Output debugging information. */
9747 output_compilation_unit_header ();
9748 output_signature (node->signature, "Type Signature");
9749 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9750 "Offset to Type DIE");
9751 output_die (node->root_die);
9753 unmark_dies (node->root_die);
9756 /* Return the DWARF2/3 pubname associated with a decl. */
9758 static const char *
9759 dwarf2_name (tree decl, int scope)
9761 if (DECL_NAMELESS (decl))
9762 return NULL;
9763 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9766 /* Add a new entry to .debug_pubnames if appropriate. */
9768 static void
9769 add_pubname_string (const char *str, dw_die_ref die)
9771 pubname_entry e;
9773 e.die = die;
9774 e.name = xstrdup (str);
9775 vec_safe_push (pubname_table, e);
9778 static void
9779 add_pubname (tree decl, dw_die_ref die)
9781 if (!want_pubnames ())
9782 return;
9784 /* Don't add items to the table when we expect that the consumer will have
9785 just read the enclosing die. For example, if the consumer is looking at a
9786 class_member, it will either be inside the class already, or will have just
9787 looked up the class to find the member. Either way, searching the class is
9788 faster than searching the index. */
9789 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9790 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9792 const char *name = dwarf2_name (decl, 1);
9794 if (name)
9795 add_pubname_string (name, die);
9799 /* Add an enumerator to the pubnames section. */
9801 static void
9802 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9804 pubname_entry e;
9806 gcc_assert (scope_name);
9807 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9808 e.die = die;
9809 vec_safe_push (pubname_table, e);
9812 /* Add a new entry to .debug_pubtypes if appropriate. */
9814 static void
9815 add_pubtype (tree decl, dw_die_ref die)
9817 pubname_entry e;
9819 if (!want_pubnames ())
9820 return;
9822 if ((TREE_PUBLIC (decl)
9823 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9824 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9826 tree scope = NULL;
9827 const char *scope_name = "";
9828 const char *sep = is_cxx () ? "::" : ".";
9829 const char *name;
9831 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9832 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9834 scope_name = lang_hooks.dwarf_name (scope, 1);
9835 if (scope_name != NULL && scope_name[0] != '\0')
9836 scope_name = concat (scope_name, sep, NULL);
9837 else
9838 scope_name = "";
9841 if (TYPE_P (decl))
9842 name = type_tag (decl);
9843 else
9844 name = lang_hooks.dwarf_name (decl, 1);
9846 /* If we don't have a name for the type, there's no point in adding
9847 it to the table. */
9848 if (name != NULL && name[0] != '\0')
9850 e.die = die;
9851 e.name = concat (scope_name, name, NULL);
9852 vec_safe_push (pubtype_table, e);
9855 /* Although it might be more consistent to add the pubinfo for the
9856 enumerators as their dies are created, they should only be added if the
9857 enum type meets the criteria above. So rather than re-check the parent
9858 enum type whenever an enumerator die is created, just output them all
9859 here. This isn't protected by the name conditional because anonymous
9860 enums don't have names. */
9861 if (die->die_tag == DW_TAG_enumeration_type)
9863 dw_die_ref c;
9865 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9870 /* Output a single entry in the pubnames table. */
9872 static void
9873 output_pubname (dw_offset die_offset, pubname_entry *entry)
9875 dw_die_ref die = entry->die;
9876 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9878 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9880 if (debug_generate_pub_sections == 2)
9882 /* This logic follows gdb's method for determining the value of the flag
9883 byte. */
9884 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9885 switch (die->die_tag)
9887 case DW_TAG_typedef:
9888 case DW_TAG_base_type:
9889 case DW_TAG_subrange_type:
9890 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9891 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9892 break;
9893 case DW_TAG_enumerator:
9894 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9895 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9896 if (!is_cxx () && !is_java ())
9897 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9898 break;
9899 case DW_TAG_subprogram:
9900 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9901 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9902 if (!is_ada ())
9903 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9904 break;
9905 case DW_TAG_constant:
9906 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9907 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9908 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9909 break;
9910 case DW_TAG_variable:
9911 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9912 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9913 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9914 break;
9915 case DW_TAG_namespace:
9916 case DW_TAG_imported_declaration:
9917 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9918 break;
9919 case DW_TAG_class_type:
9920 case DW_TAG_interface_type:
9921 case DW_TAG_structure_type:
9922 case DW_TAG_union_type:
9923 case DW_TAG_enumeration_type:
9924 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9925 if (!is_cxx () && !is_java ())
9926 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9927 break;
9928 default:
9929 /* An unusual tag. Leave the flag-byte empty. */
9930 break;
9932 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9933 "GDB-index flags");
9936 dw2_asm_output_nstring (entry->name, -1, "external name");
9940 /* Output the public names table used to speed up access to externally
9941 visible names; or the public types table used to find type definitions. */
9943 static void
9944 output_pubnames (vec<pubname_entry, va_gc> *names)
9946 unsigned i;
9947 unsigned long pubnames_length = size_of_pubnames (names);
9948 pubname_entry *pub;
9950 if (!XCOFF_DEBUGGING_INFO)
9952 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9953 dw2_asm_output_data (4, 0xffffffff,
9954 "Initial length escape value indicating 64-bit DWARF extension");
9955 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9956 "Pub Info Length");
9959 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9960 dw2_asm_output_data (2, 2, "DWARF Version");
9962 if (dwarf_split_debug_info)
9963 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9964 debug_skeleton_info_section,
9965 "Offset of Compilation Unit Info");
9966 else
9967 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9968 debug_info_section,
9969 "Offset of Compilation Unit Info");
9970 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9971 "Compilation Unit Length");
9973 FOR_EACH_VEC_ELT (*names, i, pub)
9975 if (include_pubname_in_output (names, pub))
9977 dw_offset die_offset = pub->die->die_offset;
9979 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9980 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9981 gcc_assert (pub->die->die_mark);
9983 /* If we're putting types in their own .debug_types sections,
9984 the .debug_pubtypes table will still point to the compile
9985 unit (not the type unit), so we want to use the offset of
9986 the skeleton DIE (if there is one). */
9987 if (pub->die->comdat_type_p && names == pubtype_table)
9989 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9991 if (type_node != NULL)
9992 die_offset = (type_node->skeleton_die != NULL
9993 ? type_node->skeleton_die->die_offset
9994 : comp_unit_die ()->die_offset);
9997 output_pubname (die_offset, pub);
10001 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10004 /* Output public names and types tables if necessary. */
10006 static void
10007 output_pubtables (void)
10009 if (!want_pubnames () || !info_section_emitted)
10010 return;
10012 switch_to_section (debug_pubnames_section);
10013 output_pubnames (pubname_table);
10014 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10015 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10016 simply won't look for the section. */
10017 switch_to_section (debug_pubtypes_section);
10018 output_pubnames (pubtype_table);
10022 /* Output the information that goes into the .debug_aranges table.
10023 Namely, define the beginning and ending address range of the
10024 text section generated for this compilation unit. */
10026 static void
10027 output_aranges (void)
10029 unsigned i;
10030 unsigned long aranges_length = size_of_aranges ();
10032 if (!XCOFF_DEBUGGING_INFO)
10034 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10035 dw2_asm_output_data (4, 0xffffffff,
10036 "Initial length escape value indicating 64-bit DWARF extension");
10037 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10038 "Length of Address Ranges Info");
10041 /* Version number for aranges is still 2, even up to DWARF5. */
10042 dw2_asm_output_data (2, 2, "DWARF Version");
10043 if (dwarf_split_debug_info)
10044 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10045 debug_skeleton_info_section,
10046 "Offset of Compilation Unit Info");
10047 else
10048 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10049 debug_info_section,
10050 "Offset of Compilation Unit Info");
10051 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10052 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10054 /* We need to align to twice the pointer size here. */
10055 if (DWARF_ARANGES_PAD_SIZE)
10057 /* Pad using a 2 byte words so that padding is correct for any
10058 pointer size. */
10059 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10060 2 * DWARF2_ADDR_SIZE);
10061 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10062 dw2_asm_output_data (2, 0, NULL);
10065 /* It is necessary not to output these entries if the sections were
10066 not used; if the sections were not used, the length will be 0 and
10067 the address may end up as 0 if the section is discarded by ld
10068 --gc-sections, leaving an invalid (0, 0) entry that can be
10069 confused with the terminator. */
10070 if (text_section_used)
10072 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10073 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10074 text_section_label, "Length");
10076 if (cold_text_section_used)
10078 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10079 "Address");
10080 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10081 cold_text_section_label, "Length");
10084 if (have_multiple_function_sections)
10086 unsigned fde_idx;
10087 dw_fde_ref fde;
10089 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10091 if (DECL_IGNORED_P (fde->decl))
10092 continue;
10093 if (!fde->in_std_section)
10095 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10096 "Address");
10097 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10098 fde->dw_fde_begin, "Length");
10100 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10102 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10103 "Address");
10104 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10105 fde->dw_fde_second_begin, "Length");
10110 /* Output the terminator words. */
10111 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10112 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10115 /* Add a new entry to .debug_ranges. Return the offset at which it
10116 was placed. */
10118 static unsigned int
10119 add_ranges_num (int num)
10121 unsigned int in_use = ranges_table_in_use;
10123 if (in_use == ranges_table_allocated)
10125 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10126 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10127 ranges_table_allocated);
10128 memset (ranges_table + ranges_table_in_use, 0,
10129 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10132 ranges_table[in_use].num = num;
10133 ranges_table_in_use = in_use + 1;
10135 return in_use * 2 * DWARF2_ADDR_SIZE;
10138 /* Add a new entry to .debug_ranges corresponding to a block, or a
10139 range terminator if BLOCK is NULL. */
10141 static unsigned int
10142 add_ranges (const_tree block)
10144 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10147 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10148 When using dwarf_split_debug_info, address attributes in dies destined
10149 for the final executable should be direct references--setting the
10150 parameter force_direct ensures this behavior. */
10152 static void
10153 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10154 bool *added, bool force_direct)
10156 unsigned int in_use = ranges_by_label_in_use;
10157 unsigned int offset;
10159 if (in_use == ranges_by_label_allocated)
10161 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10162 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10163 ranges_by_label_allocated);
10164 memset (ranges_by_label + ranges_by_label_in_use, 0,
10165 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10168 ranges_by_label[in_use].begin = begin;
10169 ranges_by_label[in_use].end = end;
10170 ranges_by_label_in_use = in_use + 1;
10172 offset = add_ranges_num (-(int)in_use - 1);
10173 if (!*added)
10175 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10176 *added = true;
10180 static void
10181 output_ranges (void)
10183 unsigned i;
10184 static const char *const start_fmt = "Offset %#x";
10185 const char *fmt = start_fmt;
10187 for (i = 0; i < ranges_table_in_use; i++)
10189 int block_num = ranges_table[i].num;
10191 if (block_num > 0)
10193 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10194 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10196 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10197 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10199 /* If all code is in the text section, then the compilation
10200 unit base address defaults to DW_AT_low_pc, which is the
10201 base of the text section. */
10202 if (!have_multiple_function_sections)
10204 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10205 text_section_label,
10206 fmt, i * 2 * DWARF2_ADDR_SIZE);
10207 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10208 text_section_label, NULL);
10211 /* Otherwise, the compilation unit base address is zero,
10212 which allows us to use absolute addresses, and not worry
10213 about whether the target supports cross-section
10214 arithmetic. */
10215 else
10217 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10218 fmt, i * 2 * DWARF2_ADDR_SIZE);
10219 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10222 fmt = NULL;
10225 /* Negative block_num stands for an index into ranges_by_label. */
10226 else if (block_num < 0)
10228 int lab_idx = - block_num - 1;
10230 if (!have_multiple_function_sections)
10232 gcc_unreachable ();
10233 #if 0
10234 /* If we ever use add_ranges_by_labels () for a single
10235 function section, all we have to do is to take out
10236 the #if 0 above. */
10237 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10238 ranges_by_label[lab_idx].begin,
10239 text_section_label,
10240 fmt, i * 2 * DWARF2_ADDR_SIZE);
10241 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10242 ranges_by_label[lab_idx].end,
10243 text_section_label, NULL);
10244 #endif
10246 else
10248 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10249 ranges_by_label[lab_idx].begin,
10250 fmt, i * 2 * DWARF2_ADDR_SIZE);
10251 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10252 ranges_by_label[lab_idx].end,
10253 NULL);
10256 else
10258 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10259 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10260 fmt = start_fmt;
10265 /* Data structure containing information about input files. */
10266 struct file_info
10268 const char *path; /* Complete file name. */
10269 const char *fname; /* File name part. */
10270 int length; /* Length of entire string. */
10271 struct dwarf_file_data * file_idx; /* Index in input file table. */
10272 int dir_idx; /* Index in directory table. */
10275 /* Data structure containing information about directories with source
10276 files. */
10277 struct dir_info
10279 const char *path; /* Path including directory name. */
10280 int length; /* Path length. */
10281 int prefix; /* Index of directory entry which is a prefix. */
10282 int count; /* Number of files in this directory. */
10283 int dir_idx; /* Index of directory used as base. */
10286 /* Callback function for file_info comparison. We sort by looking at
10287 the directories in the path. */
10289 static int
10290 file_info_cmp (const void *p1, const void *p2)
10292 const struct file_info *const s1 = (const struct file_info *) p1;
10293 const struct file_info *const s2 = (const struct file_info *) p2;
10294 const unsigned char *cp1;
10295 const unsigned char *cp2;
10297 /* Take care of file names without directories. We need to make sure that
10298 we return consistent values to qsort since some will get confused if
10299 we return the same value when identical operands are passed in opposite
10300 orders. So if neither has a directory, return 0 and otherwise return
10301 1 or -1 depending on which one has the directory. */
10302 if ((s1->path == s1->fname || s2->path == s2->fname))
10303 return (s2->path == s2->fname) - (s1->path == s1->fname);
10305 cp1 = (const unsigned char *) s1->path;
10306 cp2 = (const unsigned char *) s2->path;
10308 while (1)
10310 ++cp1;
10311 ++cp2;
10312 /* Reached the end of the first path? If so, handle like above. */
10313 if ((cp1 == (const unsigned char *) s1->fname)
10314 || (cp2 == (const unsigned char *) s2->fname))
10315 return ((cp2 == (const unsigned char *) s2->fname)
10316 - (cp1 == (const unsigned char *) s1->fname));
10318 /* Character of current path component the same? */
10319 else if (*cp1 != *cp2)
10320 return *cp1 - *cp2;
10324 struct file_name_acquire_data
10326 struct file_info *files;
10327 int used_files;
10328 int max_files;
10331 /* Traversal function for the hash table. */
10334 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10336 struct dwarf_file_data *d = *slot;
10337 struct file_info *fi;
10338 const char *f;
10340 gcc_assert (fnad->max_files >= d->emitted_number);
10342 if (! d->emitted_number)
10343 return 1;
10345 gcc_assert (fnad->max_files != fnad->used_files);
10347 fi = fnad->files + fnad->used_files++;
10349 /* Skip all leading "./". */
10350 f = d->filename;
10351 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10352 f += 2;
10354 /* Create a new array entry. */
10355 fi->path = f;
10356 fi->length = strlen (f);
10357 fi->file_idx = d;
10359 /* Search for the file name part. */
10360 f = strrchr (f, DIR_SEPARATOR);
10361 #if defined (DIR_SEPARATOR_2)
10363 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10365 if (g != NULL)
10367 if (f == NULL || f < g)
10368 f = g;
10371 #endif
10373 fi->fname = f == NULL ? fi->path : f + 1;
10374 return 1;
10377 /* Output the directory table and the file name table. We try to minimize
10378 the total amount of memory needed. A heuristic is used to avoid large
10379 slowdowns with many input files. */
10381 static void
10382 output_file_names (void)
10384 struct file_name_acquire_data fnad;
10385 int numfiles;
10386 struct file_info *files;
10387 struct dir_info *dirs;
10388 int *saved;
10389 int *savehere;
10390 int *backmap;
10391 int ndirs;
10392 int idx_offset;
10393 int i;
10395 if (!last_emitted_file)
10397 dw2_asm_output_data (1, 0, "End directory table");
10398 dw2_asm_output_data (1, 0, "End file name table");
10399 return;
10402 numfiles = last_emitted_file->emitted_number;
10404 /* Allocate the various arrays we need. */
10405 files = XALLOCAVEC (struct file_info, numfiles);
10406 dirs = XALLOCAVEC (struct dir_info, numfiles);
10408 fnad.files = files;
10409 fnad.used_files = 0;
10410 fnad.max_files = numfiles;
10411 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10412 gcc_assert (fnad.used_files == fnad.max_files);
10414 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10416 /* Find all the different directories used. */
10417 dirs[0].path = files[0].path;
10418 dirs[0].length = files[0].fname - files[0].path;
10419 dirs[0].prefix = -1;
10420 dirs[0].count = 1;
10421 dirs[0].dir_idx = 0;
10422 files[0].dir_idx = 0;
10423 ndirs = 1;
10425 for (i = 1; i < numfiles; i++)
10426 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10427 && memcmp (dirs[ndirs - 1].path, files[i].path,
10428 dirs[ndirs - 1].length) == 0)
10430 /* Same directory as last entry. */
10431 files[i].dir_idx = ndirs - 1;
10432 ++dirs[ndirs - 1].count;
10434 else
10436 int j;
10438 /* This is a new directory. */
10439 dirs[ndirs].path = files[i].path;
10440 dirs[ndirs].length = files[i].fname - files[i].path;
10441 dirs[ndirs].count = 1;
10442 dirs[ndirs].dir_idx = ndirs;
10443 files[i].dir_idx = ndirs;
10445 /* Search for a prefix. */
10446 dirs[ndirs].prefix = -1;
10447 for (j = 0; j < ndirs; j++)
10448 if (dirs[j].length < dirs[ndirs].length
10449 && dirs[j].length > 1
10450 && (dirs[ndirs].prefix == -1
10451 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10452 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10453 dirs[ndirs].prefix = j;
10455 ++ndirs;
10458 /* Now to the actual work. We have to find a subset of the directories which
10459 allow expressing the file name using references to the directory table
10460 with the least amount of characters. We do not do an exhaustive search
10461 where we would have to check out every combination of every single
10462 possible prefix. Instead we use a heuristic which provides nearly optimal
10463 results in most cases and never is much off. */
10464 saved = XALLOCAVEC (int, ndirs);
10465 savehere = XALLOCAVEC (int, ndirs);
10467 memset (saved, '\0', ndirs * sizeof (saved[0]));
10468 for (i = 0; i < ndirs; i++)
10470 int j;
10471 int total;
10473 /* We can always save some space for the current directory. But this
10474 does not mean it will be enough to justify adding the directory. */
10475 savehere[i] = dirs[i].length;
10476 total = (savehere[i] - saved[i]) * dirs[i].count;
10478 for (j = i + 1; j < ndirs; j++)
10480 savehere[j] = 0;
10481 if (saved[j] < dirs[i].length)
10483 /* Determine whether the dirs[i] path is a prefix of the
10484 dirs[j] path. */
10485 int k;
10487 k = dirs[j].prefix;
10488 while (k != -1 && k != (int) i)
10489 k = dirs[k].prefix;
10491 if (k == (int) i)
10493 /* Yes it is. We can possibly save some memory by
10494 writing the filenames in dirs[j] relative to
10495 dirs[i]. */
10496 savehere[j] = dirs[i].length;
10497 total += (savehere[j] - saved[j]) * dirs[j].count;
10502 /* Check whether we can save enough to justify adding the dirs[i]
10503 directory. */
10504 if (total > dirs[i].length + 1)
10506 /* It's worthwhile adding. */
10507 for (j = i; j < ndirs; j++)
10508 if (savehere[j] > 0)
10510 /* Remember how much we saved for this directory so far. */
10511 saved[j] = savehere[j];
10513 /* Remember the prefix directory. */
10514 dirs[j].dir_idx = i;
10519 /* Emit the directory name table. */
10520 idx_offset = dirs[0].length > 0 ? 1 : 0;
10521 for (i = 1 - idx_offset; i < ndirs; i++)
10522 dw2_asm_output_nstring (dirs[i].path,
10523 dirs[i].length
10524 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10525 "Directory Entry: %#x", i + idx_offset);
10527 dw2_asm_output_data (1, 0, "End directory table");
10529 /* We have to emit them in the order of emitted_number since that's
10530 used in the debug info generation. To do this efficiently we
10531 generate a back-mapping of the indices first. */
10532 backmap = XALLOCAVEC (int, numfiles);
10533 for (i = 0; i < numfiles; i++)
10534 backmap[files[i].file_idx->emitted_number - 1] = i;
10536 /* Now write all the file names. */
10537 for (i = 0; i < numfiles; i++)
10539 int file_idx = backmap[i];
10540 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10542 #ifdef VMS_DEBUGGING_INFO
10543 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10545 /* Setting these fields can lead to debugger miscomparisons,
10546 but VMS Debug requires them to be set correctly. */
10548 int ver;
10549 long long cdt;
10550 long siz;
10551 int maxfilelen = strlen (files[file_idx].path)
10552 + dirs[dir_idx].length
10553 + MAX_VMS_VERSION_LEN + 1;
10554 char *filebuf = XALLOCAVEC (char, maxfilelen);
10556 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10557 snprintf (filebuf, maxfilelen, "%s;%d",
10558 files[file_idx].path + dirs[dir_idx].length, ver);
10560 dw2_asm_output_nstring
10561 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10563 /* Include directory index. */
10564 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10566 /* Modification time. */
10567 dw2_asm_output_data_uleb128
10568 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10569 ? cdt : 0,
10570 NULL);
10572 /* File length in bytes. */
10573 dw2_asm_output_data_uleb128
10574 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10575 ? siz : 0,
10576 NULL);
10577 #else
10578 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10579 "File Entry: %#x", (unsigned) i + 1);
10581 /* Include directory index. */
10582 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10584 /* Modification time. */
10585 dw2_asm_output_data_uleb128 (0, NULL);
10587 /* File length in bytes. */
10588 dw2_asm_output_data_uleb128 (0, NULL);
10589 #endif /* VMS_DEBUGGING_INFO */
10592 dw2_asm_output_data (1, 0, "End file name table");
10596 /* Output one line number table into the .debug_line section. */
10598 static void
10599 output_one_line_info_table (dw_line_info_table *table)
10601 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10602 unsigned int current_line = 1;
10603 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10604 dw_line_info_entry *ent;
10605 size_t i;
10607 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10609 switch (ent->opcode)
10611 case LI_set_address:
10612 /* ??? Unfortunately, we have little choice here currently, and
10613 must always use the most general form. GCC does not know the
10614 address delta itself, so we can't use DW_LNS_advance_pc. Many
10615 ports do have length attributes which will give an upper bound
10616 on the address range. We could perhaps use length attributes
10617 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10618 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10620 /* This can handle any delta. This takes
10621 4+DWARF2_ADDR_SIZE bytes. */
10622 dw2_asm_output_data (1, 0, "set address %s", line_label);
10623 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10624 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10626 break;
10628 case LI_set_line:
10629 if (ent->val == current_line)
10631 /* We still need to start a new row, so output a copy insn. */
10632 dw2_asm_output_data (1, DW_LNS_copy,
10633 "copy line %u", current_line);
10635 else
10637 int line_offset = ent->val - current_line;
10638 int line_delta = line_offset - DWARF_LINE_BASE;
10640 current_line = ent->val;
10641 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10643 /* This can handle deltas from -10 to 234, using the current
10644 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10645 This takes 1 byte. */
10646 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10647 "line %u", current_line);
10649 else
10651 /* This can handle any delta. This takes at least 4 bytes,
10652 depending on the value being encoded. */
10653 dw2_asm_output_data (1, DW_LNS_advance_line,
10654 "advance to line %u", current_line);
10655 dw2_asm_output_data_sleb128 (line_offset, NULL);
10656 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10659 break;
10661 case LI_set_file:
10662 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10663 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10664 break;
10666 case LI_set_column:
10667 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10668 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10669 break;
10671 case LI_negate_stmt:
10672 current_is_stmt = !current_is_stmt;
10673 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10674 "is_stmt %d", current_is_stmt);
10675 break;
10677 case LI_set_prologue_end:
10678 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10679 "set prologue end");
10680 break;
10682 case LI_set_epilogue_begin:
10683 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10684 "set epilogue begin");
10685 break;
10687 case LI_set_discriminator:
10688 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10689 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10690 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10691 dw2_asm_output_data_uleb128 (ent->val, NULL);
10692 break;
10696 /* Emit debug info for the address of the end of the table. */
10697 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10698 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10699 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10700 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10702 dw2_asm_output_data (1, 0, "end sequence");
10703 dw2_asm_output_data_uleb128 (1, NULL);
10704 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10707 /* Output the source line number correspondence information. This
10708 information goes into the .debug_line section. */
10710 static void
10711 output_line_info (bool prologue_only)
10713 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
10714 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
10715 /* We don't support DWARFv5 line tables yet. */
10716 int ver = dwarf_version < 5 ? dwarf_version : 4;
10717 bool saw_one = false;
10718 int opc;
10720 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10721 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10722 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10723 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10725 if (!XCOFF_DEBUGGING_INFO)
10727 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10728 dw2_asm_output_data (4, 0xffffffff,
10729 "Initial length escape value indicating 64-bit DWARF extension");
10730 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10731 "Length of Source Line Info");
10734 ASM_OUTPUT_LABEL (asm_out_file, l1);
10736 dw2_asm_output_data (2, ver, "DWARF Version");
10737 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10738 ASM_OUTPUT_LABEL (asm_out_file, p1);
10740 /* Define the architecture-dependent minimum instruction length (in bytes).
10741 In this implementation of DWARF, this field is used for information
10742 purposes only. Since GCC generates assembly language, we have no
10743 a priori knowledge of how many instruction bytes are generated for each
10744 source line, and therefore can use only the DW_LNE_set_address and
10745 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10746 this as '1', which is "correct enough" for all architectures,
10747 and don't let the target override. */
10748 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10750 if (ver >= 4)
10751 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10752 "Maximum Operations Per Instruction");
10753 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10754 "Default is_stmt_start flag");
10755 dw2_asm_output_data (1, DWARF_LINE_BASE,
10756 "Line Base Value (Special Opcodes)");
10757 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10758 "Line Range Value (Special Opcodes)");
10759 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10760 "Special Opcode Base");
10762 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10764 int n_op_args;
10765 switch (opc)
10767 case DW_LNS_advance_pc:
10768 case DW_LNS_advance_line:
10769 case DW_LNS_set_file:
10770 case DW_LNS_set_column:
10771 case DW_LNS_fixed_advance_pc:
10772 case DW_LNS_set_isa:
10773 n_op_args = 1;
10774 break;
10775 default:
10776 n_op_args = 0;
10777 break;
10780 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10781 opc, n_op_args);
10784 /* Write out the information about the files we use. */
10785 output_file_names ();
10786 ASM_OUTPUT_LABEL (asm_out_file, p2);
10787 if (prologue_only)
10789 /* Output the marker for the end of the line number info. */
10790 ASM_OUTPUT_LABEL (asm_out_file, l2);
10791 return;
10794 if (separate_line_info)
10796 dw_line_info_table *table;
10797 size_t i;
10799 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10800 if (table->in_use)
10802 output_one_line_info_table (table);
10803 saw_one = true;
10806 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10808 output_one_line_info_table (cold_text_section_line_info);
10809 saw_one = true;
10812 /* ??? Some Darwin linkers crash on a .debug_line section with no
10813 sequences. Further, merely a DW_LNE_end_sequence entry is not
10814 sufficient -- the address column must also be initialized.
10815 Make sure to output at least one set_address/end_sequence pair,
10816 choosing .text since that section is always present. */
10817 if (text_section_line_info->in_use || !saw_one)
10818 output_one_line_info_table (text_section_line_info);
10820 /* Output the marker for the end of the line number info. */
10821 ASM_OUTPUT_LABEL (asm_out_file, l2);
10824 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10826 static inline bool
10827 need_endianity_attribute_p (bool reverse)
10829 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10832 /* Given a pointer to a tree node for some base type, return a pointer to
10833 a DIE that describes the given type. REVERSE is true if the type is
10834 to be interpreted in the reverse storage order wrt the target order.
10836 This routine must only be called for GCC type nodes that correspond to
10837 Dwarf base (fundamental) types. */
10839 static dw_die_ref
10840 base_type_die (tree type, bool reverse)
10842 dw_die_ref base_type_result;
10843 enum dwarf_type encoding;
10844 bool fpt_used = false;
10845 struct fixed_point_type_info fpt_info;
10846 tree type_bias = NULL_TREE;
10848 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10849 return 0;
10851 /* If this is a subtype that should not be emitted as a subrange type,
10852 use the base type. See subrange_type_for_debug_p. */
10853 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10854 type = TREE_TYPE (type);
10856 switch (TREE_CODE (type))
10858 case INTEGER_TYPE:
10859 if ((dwarf_version >= 4 || !dwarf_strict)
10860 && TYPE_NAME (type)
10861 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10862 && DECL_IS_BUILTIN (TYPE_NAME (type))
10863 && DECL_NAME (TYPE_NAME (type)))
10865 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10866 if (strcmp (name, "char16_t") == 0
10867 || strcmp (name, "char32_t") == 0)
10869 encoding = DW_ATE_UTF;
10870 break;
10873 if ((dwarf_version >= 3 || !dwarf_strict)
10874 && lang_hooks.types.get_fixed_point_type_info)
10876 memset (&fpt_info, 0, sizeof (fpt_info));
10877 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10879 fpt_used = true;
10880 encoding = ((TYPE_UNSIGNED (type))
10881 ? DW_ATE_unsigned_fixed
10882 : DW_ATE_signed_fixed);
10883 break;
10886 if (TYPE_STRING_FLAG (type))
10888 if (TYPE_UNSIGNED (type))
10889 encoding = DW_ATE_unsigned_char;
10890 else
10891 encoding = DW_ATE_signed_char;
10893 else if (TYPE_UNSIGNED (type))
10894 encoding = DW_ATE_unsigned;
10895 else
10896 encoding = DW_ATE_signed;
10898 if (!dwarf_strict
10899 && lang_hooks.types.get_type_bias)
10900 type_bias = lang_hooks.types.get_type_bias (type);
10901 break;
10903 case REAL_TYPE:
10904 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10906 if (dwarf_version >= 3 || !dwarf_strict)
10907 encoding = DW_ATE_decimal_float;
10908 else
10909 encoding = DW_ATE_lo_user;
10911 else
10912 encoding = DW_ATE_float;
10913 break;
10915 case FIXED_POINT_TYPE:
10916 if (!(dwarf_version >= 3 || !dwarf_strict))
10917 encoding = DW_ATE_lo_user;
10918 else if (TYPE_UNSIGNED (type))
10919 encoding = DW_ATE_unsigned_fixed;
10920 else
10921 encoding = DW_ATE_signed_fixed;
10922 break;
10924 /* Dwarf2 doesn't know anything about complex ints, so use
10925 a user defined type for it. */
10926 case COMPLEX_TYPE:
10927 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10928 encoding = DW_ATE_complex_float;
10929 else
10930 encoding = DW_ATE_lo_user;
10931 break;
10933 case BOOLEAN_TYPE:
10934 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10935 encoding = DW_ATE_boolean;
10936 break;
10938 default:
10939 /* No other TREE_CODEs are Dwarf fundamental types. */
10940 gcc_unreachable ();
10943 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10945 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10946 int_size_in_bytes (type));
10947 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10949 if (need_endianity_attribute_p (reverse))
10950 add_AT_unsigned (base_type_result, DW_AT_endianity,
10951 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10953 if (fpt_used)
10955 switch (fpt_info.scale_factor_kind)
10957 case fixed_point_scale_factor_binary:
10958 add_AT_int (base_type_result, DW_AT_binary_scale,
10959 fpt_info.scale_factor.binary);
10960 break;
10962 case fixed_point_scale_factor_decimal:
10963 add_AT_int (base_type_result, DW_AT_decimal_scale,
10964 fpt_info.scale_factor.decimal);
10965 break;
10967 case fixed_point_scale_factor_arbitrary:
10968 /* Arbitrary scale factors cannot be described in standard DWARF,
10969 yet. */
10970 if (!dwarf_strict)
10972 /* Describe the scale factor as a rational constant. */
10973 const dw_die_ref scale_factor
10974 = new_die (DW_TAG_constant, comp_unit_die (), type);
10976 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10977 fpt_info.scale_factor.arbitrary.numerator);
10978 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10979 fpt_info.scale_factor.arbitrary.denominator);
10981 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10983 break;
10985 default:
10986 gcc_unreachable ();
10990 if (type_bias)
10991 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10992 dw_scalar_form_constant
10993 | dw_scalar_form_exprloc
10994 | dw_scalar_form_reference,
10995 NULL);
10997 add_pubtype (type, base_type_result);
10999 return base_type_result;
11002 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11003 named 'auto' in its type: return true for it, false otherwise. */
11005 static inline bool
11006 is_cxx_auto (tree type)
11008 if (is_cxx ())
11010 tree name = TYPE_IDENTIFIER (type);
11011 if (name == get_identifier ("auto")
11012 || name == get_identifier ("decltype(auto)"))
11013 return true;
11015 return false;
11018 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11019 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11021 static inline int
11022 is_base_type (tree type)
11024 switch (TREE_CODE (type))
11026 case ERROR_MARK:
11027 case VOID_TYPE:
11028 case INTEGER_TYPE:
11029 case REAL_TYPE:
11030 case FIXED_POINT_TYPE:
11031 case COMPLEX_TYPE:
11032 case BOOLEAN_TYPE:
11033 case POINTER_BOUNDS_TYPE:
11034 return 1;
11036 case ARRAY_TYPE:
11037 case RECORD_TYPE:
11038 case UNION_TYPE:
11039 case QUAL_UNION_TYPE:
11040 case ENUMERAL_TYPE:
11041 case FUNCTION_TYPE:
11042 case METHOD_TYPE:
11043 case POINTER_TYPE:
11044 case REFERENCE_TYPE:
11045 case NULLPTR_TYPE:
11046 case OFFSET_TYPE:
11047 case LANG_TYPE:
11048 case VECTOR_TYPE:
11049 return 0;
11051 default:
11052 if (is_cxx_auto (type))
11053 return 0;
11054 gcc_unreachable ();
11057 return 0;
11060 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11061 node, return the size in bits for the type if it is a constant, or else
11062 return the alignment for the type if the type's size is not constant, or
11063 else return BITS_PER_WORD if the type actually turns out to be an
11064 ERROR_MARK node. */
11066 static inline unsigned HOST_WIDE_INT
11067 simple_type_size_in_bits (const_tree type)
11069 if (TREE_CODE (type) == ERROR_MARK)
11070 return BITS_PER_WORD;
11071 else if (TYPE_SIZE (type) == NULL_TREE)
11072 return 0;
11073 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11074 return tree_to_uhwi (TYPE_SIZE (type));
11075 else
11076 return TYPE_ALIGN (type);
11079 /* Similarly, but return an offset_int instead of UHWI. */
11081 static inline offset_int
11082 offset_int_type_size_in_bits (const_tree type)
11084 if (TREE_CODE (type) == ERROR_MARK)
11085 return BITS_PER_WORD;
11086 else if (TYPE_SIZE (type) == NULL_TREE)
11087 return 0;
11088 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11089 return wi::to_offset (TYPE_SIZE (type));
11090 else
11091 return TYPE_ALIGN (type);
11094 /* Given a pointer to a tree node for a subrange type, return a pointer
11095 to a DIE that describes the given type. */
11097 static dw_die_ref
11098 subrange_type_die (tree type, tree low, tree high, tree bias,
11099 dw_die_ref context_die)
11101 dw_die_ref subrange_die;
11102 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11104 if (context_die == NULL)
11105 context_die = comp_unit_die ();
11107 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11109 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11111 /* The size of the subrange type and its base type do not match,
11112 so we need to generate a size attribute for the subrange type. */
11113 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11116 if (low)
11117 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11118 if (high)
11119 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11120 if (bias && !dwarf_strict)
11121 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11122 dw_scalar_form_constant
11123 | dw_scalar_form_exprloc
11124 | dw_scalar_form_reference,
11125 NULL);
11127 return subrange_die;
11130 /* Returns the (const and/or volatile) cv_qualifiers associated with
11131 the decl node. This will normally be augmented with the
11132 cv_qualifiers of the underlying type in add_type_attribute. */
11134 static int
11135 decl_quals (const_tree decl)
11137 return ((TREE_READONLY (decl)
11138 /* The C++ front-end correctly marks reference-typed
11139 variables as readonly, but from a language (and debug
11140 info) standpoint they are not const-qualified. */
11141 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11142 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11143 | (TREE_THIS_VOLATILE (decl)
11144 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11147 /* Determine the TYPE whose qualifiers match the largest strict subset
11148 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11149 qualifiers outside QUAL_MASK. */
11151 static int
11152 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11154 tree t;
11155 int best_rank = 0, best_qual = 0, max_rank;
11157 type_quals &= qual_mask;
11158 max_rank = popcount_hwi (type_quals) - 1;
11160 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11161 t = TYPE_NEXT_VARIANT (t))
11163 int q = TYPE_QUALS (t) & qual_mask;
11165 if ((q & type_quals) == q && q != type_quals
11166 && check_base_type (t, type))
11168 int rank = popcount_hwi (q);
11170 if (rank > best_rank)
11172 best_rank = rank;
11173 best_qual = q;
11178 return best_qual;
11181 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11182 static const dwarf_qual_info_t dwarf_qual_info[] =
11184 { TYPE_QUAL_CONST, DW_TAG_const_type },
11185 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11186 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11187 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11189 static const unsigned int dwarf_qual_info_size
11190 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11192 /* If DIE is a qualified DIE of some base DIE with the same parent,
11193 return the base DIE, otherwise return NULL. Set MASK to the
11194 qualifiers added compared to the returned DIE. */
11196 static dw_die_ref
11197 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11199 unsigned int i;
11200 for (i = 0; i < dwarf_qual_info_size; i++)
11201 if (die->die_tag == dwarf_qual_info[i].t)
11202 break;
11203 if (i == dwarf_qual_info_size)
11204 return NULL;
11205 if (vec_safe_length (die->die_attr) != 1)
11206 return NULL;
11207 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11208 if (type == NULL || type->die_parent != die->die_parent)
11209 return NULL;
11210 *mask |= dwarf_qual_info[i].q;
11211 if (depth)
11213 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11214 if (ret)
11215 return ret;
11217 return type;
11220 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11221 entry that chains the modifiers specified by CV_QUALS in front of the
11222 given type. REVERSE is true if the type is to be interpreted in the
11223 reverse storage order wrt the target order. */
11225 static dw_die_ref
11226 modified_type_die (tree type, int cv_quals, bool reverse,
11227 dw_die_ref context_die)
11229 enum tree_code code = TREE_CODE (type);
11230 dw_die_ref mod_type_die;
11231 dw_die_ref sub_die = NULL;
11232 tree item_type = NULL;
11233 tree qualified_type;
11234 tree name, low, high;
11235 dw_die_ref mod_scope;
11236 /* Only these cv-qualifiers are currently handled. */
11237 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11238 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11240 if (code == ERROR_MARK)
11241 return NULL;
11243 if (lang_hooks.types.get_debug_type)
11245 tree debug_type = lang_hooks.types.get_debug_type (type);
11247 if (debug_type != NULL_TREE && debug_type != type)
11248 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11251 cv_quals &= cv_qual_mask;
11253 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11254 tag modifier (and not an attribute) old consumers won't be able
11255 to handle it. */
11256 if (dwarf_version < 3)
11257 cv_quals &= ~TYPE_QUAL_RESTRICT;
11259 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11260 if (dwarf_version < 5)
11261 cv_quals &= ~TYPE_QUAL_ATOMIC;
11263 /* See if we already have the appropriately qualified variant of
11264 this type. */
11265 qualified_type = get_qualified_type (type, cv_quals);
11267 if (qualified_type == sizetype
11268 && TYPE_NAME (qualified_type)
11269 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11271 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11273 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11274 && TYPE_PRECISION (t)
11275 == TYPE_PRECISION (qualified_type)
11276 && TYPE_UNSIGNED (t)
11277 == TYPE_UNSIGNED (qualified_type));
11278 qualified_type = t;
11281 /* If we do, then we can just use its DIE, if it exists. */
11282 if (qualified_type)
11284 mod_type_die = lookup_type_die (qualified_type);
11286 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11287 if (mod_type_die
11288 && (!need_endianity_attribute_p (reverse)
11289 || !is_base_type (type)
11290 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11291 return mod_type_die;
11294 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11296 /* Handle C typedef types. */
11297 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11298 && !DECL_ARTIFICIAL (name))
11300 tree dtype = TREE_TYPE (name);
11302 if (qualified_type == dtype)
11304 /* For a named type, use the typedef. */
11305 gen_type_die (qualified_type, context_die);
11306 return lookup_type_die (qualified_type);
11308 else
11310 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11311 dquals &= cv_qual_mask;
11312 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11313 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11314 /* cv-unqualified version of named type. Just use
11315 the unnamed type to which it refers. */
11316 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11317 reverse, context_die);
11318 /* Else cv-qualified version of named type; fall through. */
11322 mod_scope = scope_die_for (type, context_die);
11324 if (cv_quals)
11326 int sub_quals = 0, first_quals = 0;
11327 unsigned i;
11328 dw_die_ref first = NULL, last = NULL;
11330 /* Determine a lesser qualified type that most closely matches
11331 this one. Then generate DW_TAG_* entries for the remaining
11332 qualifiers. */
11333 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11334 cv_qual_mask);
11335 if (sub_quals && use_debug_types)
11337 bool needed = false;
11338 /* If emitting type units, make sure the order of qualifiers
11339 is canonical. Thus, start from unqualified type if
11340 an earlier qualifier is missing in sub_quals, but some later
11341 one is present there. */
11342 for (i = 0; i < dwarf_qual_info_size; i++)
11343 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11344 needed = true;
11345 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11347 sub_quals = 0;
11348 break;
11351 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11352 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11354 /* As not all intermediate qualified DIEs have corresponding
11355 tree types, ensure that qualified DIEs in the same scope
11356 as their DW_AT_type are emitted after their DW_AT_type,
11357 only with other qualified DIEs for the same type possibly
11358 in between them. Determine the range of such qualified
11359 DIEs now (first being the base type, last being corresponding
11360 last qualified DIE for it). */
11361 unsigned int count = 0;
11362 first = qualified_die_p (mod_type_die, &first_quals,
11363 dwarf_qual_info_size);
11364 if (first == NULL)
11365 first = mod_type_die;
11366 gcc_assert ((first_quals & ~sub_quals) == 0);
11367 for (count = 0, last = first;
11368 count < (1U << dwarf_qual_info_size);
11369 count++, last = last->die_sib)
11371 int quals = 0;
11372 if (last == mod_scope->die_child)
11373 break;
11374 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11375 != first)
11376 break;
11380 for (i = 0; i < dwarf_qual_info_size; i++)
11381 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11383 dw_die_ref d;
11384 if (first && first != last)
11386 for (d = first->die_sib; ; d = d->die_sib)
11388 int quals = 0;
11389 qualified_die_p (d, &quals, dwarf_qual_info_size);
11390 if (quals == (first_quals | dwarf_qual_info[i].q))
11391 break;
11392 if (d == last)
11394 d = NULL;
11395 break;
11398 if (d)
11400 mod_type_die = d;
11401 continue;
11404 if (first)
11406 d = ggc_cleared_alloc<die_node> ();
11407 d->die_tag = dwarf_qual_info[i].t;
11408 add_child_die_after (mod_scope, d, last);
11409 last = d;
11411 else
11412 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11413 if (mod_type_die)
11414 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11415 mod_type_die = d;
11416 first_quals |= dwarf_qual_info[i].q;
11419 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11421 dwarf_tag tag = DW_TAG_pointer_type;
11422 if (code == REFERENCE_TYPE)
11424 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11425 tag = DW_TAG_rvalue_reference_type;
11426 else
11427 tag = DW_TAG_reference_type;
11429 mod_type_die = new_die (tag, mod_scope, type);
11431 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11432 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11433 item_type = TREE_TYPE (type);
11435 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11436 if (!ADDR_SPACE_GENERIC_P (as))
11438 int action = targetm.addr_space.debug (as);
11439 if (action >= 0)
11441 /* Positive values indicate an address_class. */
11442 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11444 else
11446 /* Negative values indicate an (inverted) segment base reg. */
11447 dw_loc_descr_ref d
11448 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11449 add_AT_loc (mod_type_die, DW_AT_segment, d);
11453 else if (code == INTEGER_TYPE
11454 && TREE_TYPE (type) != NULL_TREE
11455 && subrange_type_for_debug_p (type, &low, &high))
11457 tree bias = NULL_TREE;
11458 if (lang_hooks.types.get_type_bias)
11459 bias = lang_hooks.types.get_type_bias (type);
11460 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11461 item_type = TREE_TYPE (type);
11463 else if (is_base_type (type))
11464 mod_type_die = base_type_die (type, reverse);
11465 else
11467 gen_type_die (type, context_die);
11469 /* We have to get the type_main_variant here (and pass that to the
11470 `lookup_type_die' routine) because the ..._TYPE node we have
11471 might simply be a *copy* of some original type node (where the
11472 copy was created to help us keep track of typedef names) and
11473 that copy might have a different TYPE_UID from the original
11474 ..._TYPE node. */
11475 if (TREE_CODE (type) != VECTOR_TYPE
11476 && TREE_CODE (type) != ARRAY_TYPE)
11477 return lookup_type_die (type_main_variant (type));
11478 else
11479 /* Vectors have the debugging information in the type,
11480 not the main variant. */
11481 return lookup_type_die (type);
11484 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11485 don't output a DW_TAG_typedef, since there isn't one in the
11486 user's program; just attach a DW_AT_name to the type.
11487 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11488 if the base type already has the same name. */
11489 if (name
11490 && ((TREE_CODE (name) != TYPE_DECL
11491 && (qualified_type == TYPE_MAIN_VARIANT (type)
11492 || (cv_quals == TYPE_UNQUALIFIED)))
11493 || (TREE_CODE (name) == TYPE_DECL
11494 && TREE_TYPE (name) == qualified_type
11495 && DECL_NAME (name))))
11497 if (TREE_CODE (name) == TYPE_DECL)
11498 /* Could just call add_name_and_src_coords_attributes here,
11499 but since this is a builtin type it doesn't have any
11500 useful source coordinates anyway. */
11501 name = DECL_NAME (name);
11502 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11504 /* This probably indicates a bug. */
11505 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11507 name = TYPE_IDENTIFIER (type);
11508 add_name_attribute (mod_type_die,
11509 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11512 if (qualified_type)
11513 equate_type_number_to_die (qualified_type, mod_type_die);
11515 if (item_type)
11516 /* We must do this after the equate_type_number_to_die call, in case
11517 this is a recursive type. This ensures that the modified_type_die
11518 recursion will terminate even if the type is recursive. Recursive
11519 types are possible in Ada. */
11520 sub_die = modified_type_die (item_type,
11521 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11522 reverse,
11523 context_die);
11525 if (sub_die != NULL)
11526 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11528 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11529 if (TYPE_ARTIFICIAL (type))
11530 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11532 return mod_type_die;
11535 /* Generate DIEs for the generic parameters of T.
11536 T must be either a generic type or a generic function.
11537 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11539 static void
11540 gen_generic_params_dies (tree t)
11542 tree parms, args;
11543 int parms_num, i;
11544 dw_die_ref die = NULL;
11545 int non_default;
11547 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11548 return;
11550 if (TYPE_P (t))
11551 die = lookup_type_die (t);
11552 else if (DECL_P (t))
11553 die = lookup_decl_die (t);
11555 gcc_assert (die);
11557 parms = lang_hooks.get_innermost_generic_parms (t);
11558 if (!parms)
11559 /* T has no generic parameter. It means T is neither a generic type
11560 or function. End of story. */
11561 return;
11563 parms_num = TREE_VEC_LENGTH (parms);
11564 args = lang_hooks.get_innermost_generic_args (t);
11565 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11566 non_default = int_cst_value (TREE_CHAIN (args));
11567 else
11568 non_default = TREE_VEC_LENGTH (args);
11569 for (i = 0; i < parms_num; i++)
11571 tree parm, arg, arg_pack_elems;
11572 dw_die_ref parm_die;
11574 parm = TREE_VEC_ELT (parms, i);
11575 arg = TREE_VEC_ELT (args, i);
11576 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11577 gcc_assert (parm && TREE_VALUE (parm) && arg);
11579 if (parm && TREE_VALUE (parm) && arg)
11581 /* If PARM represents a template parameter pack,
11582 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11583 by DW_TAG_template_*_parameter DIEs for the argument
11584 pack elements of ARG. Note that ARG would then be
11585 an argument pack. */
11586 if (arg_pack_elems)
11587 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11588 arg_pack_elems,
11589 die);
11590 else
11591 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11592 true /* emit name */, die);
11593 if (i >= non_default)
11594 add_AT_flag (parm_die, DW_AT_default_value, 1);
11599 /* Create and return a DIE for PARM which should be
11600 the representation of a generic type parameter.
11601 For instance, in the C++ front end, PARM would be a template parameter.
11602 ARG is the argument to PARM.
11603 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11604 name of the PARM.
11605 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11606 as a child node. */
11608 static dw_die_ref
11609 generic_parameter_die (tree parm, tree arg,
11610 bool emit_name_p,
11611 dw_die_ref parent_die)
11613 dw_die_ref tmpl_die = NULL;
11614 const char *name = NULL;
11616 if (!parm || !DECL_NAME (parm) || !arg)
11617 return NULL;
11619 /* We support non-type generic parameters and arguments,
11620 type generic parameters and arguments, as well as
11621 generic generic parameters (a.k.a. template template parameters in C++)
11622 and arguments. */
11623 if (TREE_CODE (parm) == PARM_DECL)
11624 /* PARM is a nontype generic parameter */
11625 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11626 else if (TREE_CODE (parm) == TYPE_DECL)
11627 /* PARM is a type generic parameter. */
11628 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11629 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11630 /* PARM is a generic generic parameter.
11631 Its DIE is a GNU extension. It shall have a
11632 DW_AT_name attribute to represent the name of the template template
11633 parameter, and a DW_AT_GNU_template_name attribute to represent the
11634 name of the template template argument. */
11635 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11636 parent_die, parm);
11637 else
11638 gcc_unreachable ();
11640 if (tmpl_die)
11642 tree tmpl_type;
11644 /* If PARM is a generic parameter pack, it means we are
11645 emitting debug info for a template argument pack element.
11646 In other terms, ARG is a template argument pack element.
11647 In that case, we don't emit any DW_AT_name attribute for
11648 the die. */
11649 if (emit_name_p)
11651 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11652 gcc_assert (name);
11653 add_AT_string (tmpl_die, DW_AT_name, name);
11656 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11658 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11659 TMPL_DIE should have a child DW_AT_type attribute that is set
11660 to the type of the argument to PARM, which is ARG.
11661 If PARM is a type generic parameter, TMPL_DIE should have a
11662 child DW_AT_type that is set to ARG. */
11663 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11664 add_type_attribute (tmpl_die, tmpl_type,
11665 (TREE_THIS_VOLATILE (tmpl_type)
11666 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11667 false, parent_die);
11669 else
11671 /* So TMPL_DIE is a DIE representing a
11672 a generic generic template parameter, a.k.a template template
11673 parameter in C++ and arg is a template. */
11675 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11676 to the name of the argument. */
11677 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11678 if (name)
11679 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11682 if (TREE_CODE (parm) == PARM_DECL)
11683 /* So PARM is a non-type generic parameter.
11684 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11685 attribute of TMPL_DIE which value represents the value
11686 of ARG.
11687 We must be careful here:
11688 The value of ARG might reference some function decls.
11689 We might currently be emitting debug info for a generic
11690 type and types are emitted before function decls, we don't
11691 know if the function decls referenced by ARG will actually be
11692 emitted after cgraph computations.
11693 So must defer the generation of the DW_AT_const_value to
11694 after cgraph is ready. */
11695 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11698 return tmpl_die;
11701 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11702 PARM_PACK must be a template parameter pack. The returned DIE
11703 will be child DIE of PARENT_DIE. */
11705 static dw_die_ref
11706 template_parameter_pack_die (tree parm_pack,
11707 tree parm_pack_args,
11708 dw_die_ref parent_die)
11710 dw_die_ref die;
11711 int j;
11713 gcc_assert (parent_die && parm_pack);
11715 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11716 add_name_and_src_coords_attributes (die, parm_pack);
11717 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11718 generic_parameter_die (parm_pack,
11719 TREE_VEC_ELT (parm_pack_args, j),
11720 false /* Don't emit DW_AT_name */,
11721 die);
11722 return die;
11725 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11726 an enumerated type. */
11728 static inline int
11729 type_is_enum (const_tree type)
11731 return TREE_CODE (type) == ENUMERAL_TYPE;
11734 /* Return the DBX register number described by a given RTL node. */
11736 static unsigned int
11737 dbx_reg_number (const_rtx rtl)
11739 unsigned regno = REGNO (rtl);
11741 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11743 #ifdef LEAF_REG_REMAP
11744 if (crtl->uses_only_leaf_regs)
11746 int leaf_reg = LEAF_REG_REMAP (regno);
11747 if (leaf_reg != -1)
11748 regno = (unsigned) leaf_reg;
11750 #endif
11752 regno = DBX_REGISTER_NUMBER (regno);
11753 gcc_assert (regno != INVALID_REGNUM);
11754 return regno;
11757 /* Optionally add a DW_OP_piece term to a location description expression.
11758 DW_OP_piece is only added if the location description expression already
11759 doesn't end with DW_OP_piece. */
11761 static void
11762 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11764 dw_loc_descr_ref loc;
11766 if (*list_head != NULL)
11768 /* Find the end of the chain. */
11769 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11772 if (loc->dw_loc_opc != DW_OP_piece)
11773 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11777 /* Return a location descriptor that designates a machine register or
11778 zero if there is none. */
11780 static dw_loc_descr_ref
11781 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11783 rtx regs;
11785 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11786 return 0;
11788 /* We only use "frame base" when we're sure we're talking about the
11789 post-prologue local stack frame. We do this by *not* running
11790 register elimination until this point, and recognizing the special
11791 argument pointer and soft frame pointer rtx's.
11792 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11793 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11794 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11796 dw_loc_descr_ref result = NULL;
11798 if (dwarf_version >= 4 || !dwarf_strict)
11800 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11801 initialized);
11802 if (result)
11803 add_loc_descr (&result,
11804 new_loc_descr (DW_OP_stack_value, 0, 0));
11806 return result;
11809 regs = targetm.dwarf_register_span (rtl);
11811 if (REG_NREGS (rtl) > 1 || regs)
11812 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11813 else
11815 unsigned int dbx_regnum = dbx_reg_number (rtl);
11816 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11817 return 0;
11818 return one_reg_loc_descriptor (dbx_regnum, initialized);
11822 /* Return a location descriptor that designates a machine register for
11823 a given hard register number. */
11825 static dw_loc_descr_ref
11826 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11828 dw_loc_descr_ref reg_loc_descr;
11830 if (regno <= 31)
11831 reg_loc_descr
11832 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11833 else
11834 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11836 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11837 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11839 return reg_loc_descr;
11842 /* Given an RTL of a register, return a location descriptor that
11843 designates a value that spans more than one register. */
11845 static dw_loc_descr_ref
11846 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11847 enum var_init_status initialized)
11849 int size, i;
11850 dw_loc_descr_ref loc_result = NULL;
11852 /* Simple, contiguous registers. */
11853 if (regs == NULL_RTX)
11855 unsigned reg = REGNO (rtl);
11856 int nregs;
11858 #ifdef LEAF_REG_REMAP
11859 if (crtl->uses_only_leaf_regs)
11861 int leaf_reg = LEAF_REG_REMAP (reg);
11862 if (leaf_reg != -1)
11863 reg = (unsigned) leaf_reg;
11865 #endif
11867 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11868 nregs = REG_NREGS (rtl);
11870 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11872 loc_result = NULL;
11873 while (nregs--)
11875 dw_loc_descr_ref t;
11877 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11878 VAR_INIT_STATUS_INITIALIZED);
11879 add_loc_descr (&loc_result, t);
11880 add_loc_descr_op_piece (&loc_result, size);
11881 ++reg;
11883 return loc_result;
11886 /* Now onto stupid register sets in non contiguous locations. */
11888 gcc_assert (GET_CODE (regs) == PARALLEL);
11890 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11891 loc_result = NULL;
11893 for (i = 0; i < XVECLEN (regs, 0); ++i)
11895 dw_loc_descr_ref t;
11897 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11898 VAR_INIT_STATUS_INITIALIZED);
11899 add_loc_descr (&loc_result, t);
11900 add_loc_descr_op_piece (&loc_result, size);
11903 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11904 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11905 return loc_result;
11908 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11910 /* Return a location descriptor that designates a constant i,
11911 as a compound operation from constant (i >> shift), constant shift
11912 and DW_OP_shl. */
11914 static dw_loc_descr_ref
11915 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11917 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11918 add_loc_descr (&ret, int_loc_descriptor (shift));
11919 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11920 return ret;
11923 /* Return a location descriptor that designates a constant. */
11925 static dw_loc_descr_ref
11926 int_loc_descriptor (HOST_WIDE_INT i)
11928 enum dwarf_location_atom op;
11930 /* Pick the smallest representation of a constant, rather than just
11931 defaulting to the LEB encoding. */
11932 if (i >= 0)
11934 int clz = clz_hwi (i);
11935 int ctz = ctz_hwi (i);
11936 if (i <= 31)
11937 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11938 else if (i <= 0xff)
11939 op = DW_OP_const1u;
11940 else if (i <= 0xffff)
11941 op = DW_OP_const2u;
11942 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11943 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11944 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11945 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11946 while DW_OP_const4u is 5 bytes. */
11947 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11948 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11949 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11950 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11951 while DW_OP_const4u is 5 bytes. */
11952 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11953 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11954 op = DW_OP_const4u;
11955 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11956 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11957 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11958 while DW_OP_constu of constant >= 0x100000000 takes at least
11959 6 bytes. */
11960 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11961 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11962 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11963 >= HOST_BITS_PER_WIDE_INT)
11964 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11965 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11966 while DW_OP_constu takes in this case at least 6 bytes. */
11967 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11968 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11969 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11970 && size_of_uleb128 (i) > 6)
11971 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11972 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11973 else
11974 op = DW_OP_constu;
11976 else
11978 if (i >= -0x80)
11979 op = DW_OP_const1s;
11980 else if (i >= -0x8000)
11981 op = DW_OP_const2s;
11982 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11984 if (size_of_int_loc_descriptor (i) < 5)
11986 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11987 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11988 return ret;
11990 op = DW_OP_const4s;
11992 else
11994 if (size_of_int_loc_descriptor (i)
11995 < (unsigned long) 1 + size_of_sleb128 (i))
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_consts;
12005 return new_loc_descr (op, i, 0);
12008 /* Likewise, for unsigned constants. */
12010 static dw_loc_descr_ref
12011 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12013 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12014 const unsigned HOST_WIDE_INT max_uint
12015 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12017 /* If possible, use the clever signed constants handling. */
12018 if (i <= max_int)
12019 return int_loc_descriptor ((HOST_WIDE_INT) i);
12021 /* Here, we are left with positive numbers that cannot be represented as
12022 HOST_WIDE_INT, i.e.:
12023 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12025 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12026 whereas may be better to output a negative integer: thanks to integer
12027 wrapping, we know that:
12028 x = x - 2 ** DWARF2_ADDR_SIZE
12029 = x - 2 * (max (HOST_WIDE_INT) + 1)
12030 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12031 small negative integers. Let's try that in cases it will clearly improve
12032 the encoding: there is no gain turning DW_OP_const4u into
12033 DW_OP_const4s. */
12034 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12035 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12036 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12038 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12040 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12041 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12042 const HOST_WIDE_INT second_shift
12043 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12045 /* So we finally have:
12046 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12047 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12048 return int_loc_descriptor (second_shift);
12051 /* Last chance: fallback to a simple constant operation. */
12052 return new_loc_descr
12053 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12054 ? DW_OP_const4u
12055 : DW_OP_const8u,
12056 i, 0);
12059 /* Generate and return a location description that computes the unsigned
12060 comparison of the two stack top entries (a OP b where b is the top-most
12061 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12062 LE_EXPR, GT_EXPR or GE_EXPR. */
12064 static dw_loc_descr_ref
12065 uint_comparison_loc_list (enum tree_code kind)
12067 enum dwarf_location_atom op, flip_op;
12068 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12070 switch (kind)
12072 case LT_EXPR:
12073 op = DW_OP_lt;
12074 break;
12075 case LE_EXPR:
12076 op = DW_OP_le;
12077 break;
12078 case GT_EXPR:
12079 op = DW_OP_gt;
12080 break;
12081 case GE_EXPR:
12082 op = DW_OP_ge;
12083 break;
12084 default:
12085 gcc_unreachable ();
12088 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12089 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12091 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12092 possible to perform unsigned comparisons: we just have to distinguish
12093 three cases:
12095 1. when a and b have the same sign (as signed integers); then we should
12096 return: a OP(signed) b;
12098 2. when a is a negative signed integer while b is a positive one, then a
12099 is a greater unsigned integer than b; likewise when a and b's roles
12100 are flipped.
12102 So first, compare the sign of the two operands. */
12103 ret = new_loc_descr (DW_OP_over, 0, 0);
12104 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12105 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12106 /* If they have different signs (i.e. they have different sign bits), then
12107 the stack top value has now the sign bit set and thus it's smaller than
12108 zero. */
12109 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12110 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12111 add_loc_descr (&ret, bra_node);
12113 /* We are in case 1. At this point, we know both operands have the same
12114 sign, to it's safe to use the built-in signed comparison. */
12115 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12116 add_loc_descr (&ret, jmp_node);
12118 /* We are in case 2. Here, we know both operands do not have the same sign,
12119 so we have to flip the signed comparison. */
12120 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12121 tmp = new_loc_descr (flip_op, 0, 0);
12122 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12123 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12124 add_loc_descr (&ret, tmp);
12126 /* This dummy operation is necessary to make the two branches join. */
12127 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12128 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12129 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12130 add_loc_descr (&ret, tmp);
12132 return ret;
12135 /* Likewise, but takes the location description lists (might be destructive on
12136 them). Return NULL if either is NULL or if concatenation fails. */
12138 static dw_loc_list_ref
12139 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12140 enum tree_code kind)
12142 if (left == NULL || right == NULL)
12143 return NULL;
12145 add_loc_list (&left, right);
12146 if (left == NULL)
12147 return NULL;
12149 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12150 return left;
12153 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12154 without actually allocating it. */
12156 static unsigned long
12157 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12159 return size_of_int_loc_descriptor (i >> shift)
12160 + size_of_int_loc_descriptor (shift)
12161 + 1;
12164 /* Return size_of_locs (int_loc_descriptor (i)) without
12165 actually allocating it. */
12167 static unsigned long
12168 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12170 unsigned long s;
12172 if (i >= 0)
12174 int clz, ctz;
12175 if (i <= 31)
12176 return 1;
12177 else if (i <= 0xff)
12178 return 2;
12179 else if (i <= 0xffff)
12180 return 3;
12181 clz = clz_hwi (i);
12182 ctz = ctz_hwi (i);
12183 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12184 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12185 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12186 - clz - 5);
12187 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12188 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12189 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12190 - clz - 8);
12191 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12192 return 5;
12193 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12194 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12195 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12196 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12197 - clz - 8);
12198 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12199 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12200 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12201 - clz - 16);
12202 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12203 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12204 && s > 6)
12205 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12206 - clz - 32);
12207 else
12208 return 1 + s;
12210 else
12212 if (i >= -0x80)
12213 return 2;
12214 else if (i >= -0x8000)
12215 return 3;
12216 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12218 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12220 s = size_of_int_loc_descriptor (-i) + 1;
12221 if (s < 5)
12222 return s;
12224 return 5;
12226 else
12228 unsigned long r = 1 + size_of_sleb128 (i);
12229 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12231 s = size_of_int_loc_descriptor (-i) + 1;
12232 if (s < r)
12233 return s;
12235 return r;
12240 /* Return loc description representing "address" of integer value.
12241 This can appear only as toplevel expression. */
12243 static dw_loc_descr_ref
12244 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12246 int litsize;
12247 dw_loc_descr_ref loc_result = NULL;
12249 if (!(dwarf_version >= 4 || !dwarf_strict))
12250 return NULL;
12252 litsize = size_of_int_loc_descriptor (i);
12253 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12254 is more compact. For DW_OP_stack_value we need:
12255 litsize + 1 (DW_OP_stack_value)
12256 and for DW_OP_implicit_value:
12257 1 (DW_OP_implicit_value) + 1 (length) + size. */
12258 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12260 loc_result = int_loc_descriptor (i);
12261 add_loc_descr (&loc_result,
12262 new_loc_descr (DW_OP_stack_value, 0, 0));
12263 return loc_result;
12266 loc_result = new_loc_descr (DW_OP_implicit_value,
12267 size, 0);
12268 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12269 loc_result->dw_loc_oprnd2.v.val_int = i;
12270 return loc_result;
12273 /* Return a location descriptor that designates a base+offset location. */
12275 static dw_loc_descr_ref
12276 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12277 enum var_init_status initialized)
12279 unsigned int regno;
12280 dw_loc_descr_ref result;
12281 dw_fde_ref fde = cfun->fde;
12283 /* We only use "frame base" when we're sure we're talking about the
12284 post-prologue local stack frame. We do this by *not* running
12285 register elimination until this point, and recognizing the special
12286 argument pointer and soft frame pointer rtx's. */
12287 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12289 rtx elim = (ira_use_lra_p
12290 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12291 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12293 if (elim != reg)
12295 if (GET_CODE (elim) == PLUS)
12297 offset += INTVAL (XEXP (elim, 1));
12298 elim = XEXP (elim, 0);
12300 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12301 && (elim == hard_frame_pointer_rtx
12302 || elim == stack_pointer_rtx))
12303 || elim == (frame_pointer_needed
12304 ? hard_frame_pointer_rtx
12305 : stack_pointer_rtx));
12307 /* If drap register is used to align stack, use frame
12308 pointer + offset to access stack variables. If stack
12309 is aligned without drap, use stack pointer + offset to
12310 access stack variables. */
12311 if (crtl->stack_realign_tried
12312 && reg == frame_pointer_rtx)
12314 int base_reg
12315 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12316 ? HARD_FRAME_POINTER_REGNUM
12317 : REGNO (elim));
12318 return new_reg_loc_descr (base_reg, offset);
12321 gcc_assert (frame_pointer_fb_offset_valid);
12322 offset += frame_pointer_fb_offset;
12323 return new_loc_descr (DW_OP_fbreg, offset, 0);
12327 regno = REGNO (reg);
12328 #ifdef LEAF_REG_REMAP
12329 if (crtl->uses_only_leaf_regs)
12331 int leaf_reg = LEAF_REG_REMAP (regno);
12332 if (leaf_reg != -1)
12333 regno = (unsigned) leaf_reg;
12335 #endif
12336 regno = DWARF_FRAME_REGNUM (regno);
12338 if (!optimize && fde
12339 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12341 /* Use cfa+offset to represent the location of arguments passed
12342 on the stack when drap is used to align stack.
12343 Only do this when not optimizing, for optimized code var-tracking
12344 is supposed to track where the arguments live and the register
12345 used as vdrap or drap in some spot might be used for something
12346 else in other part of the routine. */
12347 return new_loc_descr (DW_OP_fbreg, offset, 0);
12350 if (regno <= 31)
12351 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12352 offset, 0);
12353 else
12354 result = new_loc_descr (DW_OP_bregx, regno, offset);
12356 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12357 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12359 return result;
12362 /* Return true if this RTL expression describes a base+offset calculation. */
12364 static inline int
12365 is_based_loc (const_rtx rtl)
12367 return (GET_CODE (rtl) == PLUS
12368 && ((REG_P (XEXP (rtl, 0))
12369 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12370 && CONST_INT_P (XEXP (rtl, 1)))));
12373 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12374 failed. */
12376 static dw_loc_descr_ref
12377 tls_mem_loc_descriptor (rtx mem)
12379 tree base;
12380 dw_loc_descr_ref loc_result;
12382 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12383 return NULL;
12385 base = get_base_address (MEM_EXPR (mem));
12386 if (base == NULL
12387 || !VAR_P (base)
12388 || !DECL_THREAD_LOCAL_P (base))
12389 return NULL;
12391 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12392 if (loc_result == NULL)
12393 return NULL;
12395 if (MEM_OFFSET (mem))
12396 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12398 return loc_result;
12401 /* Output debug info about reason why we failed to expand expression as dwarf
12402 expression. */
12404 static void
12405 expansion_failed (tree expr, rtx rtl, char const *reason)
12407 if (dump_file && (dump_flags & TDF_DETAILS))
12409 fprintf (dump_file, "Failed to expand as dwarf: ");
12410 if (expr)
12411 print_generic_expr (dump_file, expr, dump_flags);
12412 if (rtl)
12414 fprintf (dump_file, "\n");
12415 print_rtl (dump_file, rtl);
12417 fprintf (dump_file, "\nReason: %s\n", reason);
12421 /* Helper function for const_ok_for_output. */
12423 static bool
12424 const_ok_for_output_1 (rtx rtl)
12426 if (GET_CODE (rtl) == UNSPEC)
12428 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12429 we can't express it in the debug info. */
12430 /* Don't complain about TLS UNSPECs, those are just too hard to
12431 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12432 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12433 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12434 if (flag_checking
12435 && (XVECLEN (rtl, 0) == 0
12436 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12437 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12438 inform (current_function_decl
12439 ? DECL_SOURCE_LOCATION (current_function_decl)
12440 : UNKNOWN_LOCATION,
12441 #if NUM_UNSPEC_VALUES > 0
12442 "non-delegitimized UNSPEC %s (%d) found in variable location",
12443 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12444 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12445 XINT (rtl, 1));
12446 #else
12447 "non-delegitimized UNSPEC %d found in variable location",
12448 XINT (rtl, 1));
12449 #endif
12450 expansion_failed (NULL_TREE, rtl,
12451 "UNSPEC hasn't been delegitimized.\n");
12452 return false;
12455 if (targetm.const_not_ok_for_debug_p (rtl))
12457 expansion_failed (NULL_TREE, rtl,
12458 "Expression rejected for debug by the backend.\n");
12459 return false;
12462 /* FIXME: Refer to PR60655. It is possible for simplification
12463 of rtl expressions in var tracking to produce such expressions.
12464 We should really identify / validate expressions
12465 enclosed in CONST that can be handled by assemblers on various
12466 targets and only handle legitimate cases here. */
12467 if (GET_CODE (rtl) != SYMBOL_REF)
12469 if (GET_CODE (rtl) == NOT)
12470 return false;
12471 return true;
12474 if (CONSTANT_POOL_ADDRESS_P (rtl))
12476 bool marked;
12477 get_pool_constant_mark (rtl, &marked);
12478 /* If all references to this pool constant were optimized away,
12479 it was not output and thus we can't represent it. */
12480 if (!marked)
12482 expansion_failed (NULL_TREE, rtl,
12483 "Constant was removed from constant pool.\n");
12484 return false;
12488 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12489 return false;
12491 /* Avoid references to external symbols in debug info, on several targets
12492 the linker might even refuse to link when linking a shared library,
12493 and in many other cases the relocations for .debug_info/.debug_loc are
12494 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12495 to be defined within the same shared library or executable are fine. */
12496 if (SYMBOL_REF_EXTERNAL_P (rtl))
12498 tree decl = SYMBOL_REF_DECL (rtl);
12500 if (decl == NULL || !targetm.binds_local_p (decl))
12502 expansion_failed (NULL_TREE, rtl,
12503 "Symbol not defined in current TU.\n");
12504 return false;
12508 return true;
12511 /* Return true if constant RTL can be emitted in DW_OP_addr or
12512 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12513 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12515 static bool
12516 const_ok_for_output (rtx rtl)
12518 if (GET_CODE (rtl) == SYMBOL_REF)
12519 return const_ok_for_output_1 (rtl);
12521 if (GET_CODE (rtl) == CONST)
12523 subrtx_var_iterator::array_type array;
12524 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12525 if (!const_ok_for_output_1 (*iter))
12526 return false;
12527 return true;
12530 return true;
12533 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12534 if possible, NULL otherwise. */
12536 static dw_die_ref
12537 base_type_for_mode (machine_mode mode, bool unsignedp)
12539 dw_die_ref type_die;
12540 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12542 if (type == NULL)
12543 return NULL;
12544 switch (TREE_CODE (type))
12546 case INTEGER_TYPE:
12547 case REAL_TYPE:
12548 break;
12549 default:
12550 return NULL;
12552 type_die = lookup_type_die (type);
12553 if (!type_die)
12554 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12555 comp_unit_die ());
12556 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12557 return NULL;
12558 return type_die;
12561 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12562 type matching MODE, or, if MODE is narrower than or as wide as
12563 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12564 possible. */
12566 static dw_loc_descr_ref
12567 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12569 machine_mode outer_mode = mode;
12570 dw_die_ref type_die;
12571 dw_loc_descr_ref cvt;
12573 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12575 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12576 return op;
12578 type_die = base_type_for_mode (outer_mode, 1);
12579 if (type_die == NULL)
12580 return NULL;
12581 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12582 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12583 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12584 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12585 add_loc_descr (&op, cvt);
12586 return op;
12589 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12591 static dw_loc_descr_ref
12592 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12593 dw_loc_descr_ref op1)
12595 dw_loc_descr_ref ret = op0;
12596 add_loc_descr (&ret, op1);
12597 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12598 if (STORE_FLAG_VALUE != 1)
12600 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12601 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12603 return ret;
12606 /* Return location descriptor for signed comparison OP RTL. */
12608 static dw_loc_descr_ref
12609 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12610 machine_mode mem_mode)
12612 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12613 dw_loc_descr_ref op0, op1;
12614 int shift;
12616 if (op_mode == VOIDmode)
12617 op_mode = GET_MODE (XEXP (rtl, 1));
12618 if (op_mode == VOIDmode)
12619 return NULL;
12621 if (dwarf_strict
12622 && (!SCALAR_INT_MODE_P (op_mode)
12623 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12624 return NULL;
12626 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12627 VAR_INIT_STATUS_INITIALIZED);
12628 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12629 VAR_INIT_STATUS_INITIALIZED);
12631 if (op0 == NULL || op1 == NULL)
12632 return NULL;
12634 if (!SCALAR_INT_MODE_P (op_mode)
12635 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12636 return compare_loc_descriptor (op, op0, op1);
12638 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12640 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12641 dw_loc_descr_ref cvt;
12643 if (type_die == NULL)
12644 return NULL;
12645 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12646 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12647 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12648 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12649 add_loc_descr (&op0, cvt);
12650 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12651 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12652 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12653 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12654 add_loc_descr (&op1, cvt);
12655 return compare_loc_descriptor (op, op0, op1);
12658 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12659 /* For eq/ne, if the operands are known to be zero-extended,
12660 there is no need to do the fancy shifting up. */
12661 if (op == DW_OP_eq || op == DW_OP_ne)
12663 dw_loc_descr_ref last0, last1;
12664 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12666 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12668 /* deref_size zero extends, and for constants we can check
12669 whether they are zero extended or not. */
12670 if (((last0->dw_loc_opc == DW_OP_deref_size
12671 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12672 || (CONST_INT_P (XEXP (rtl, 0))
12673 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12674 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12675 && ((last1->dw_loc_opc == DW_OP_deref_size
12676 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12677 || (CONST_INT_P (XEXP (rtl, 1))
12678 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12679 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12680 return compare_loc_descriptor (op, op0, op1);
12682 /* EQ/NE comparison against constant in narrower type than
12683 DWARF2_ADDR_SIZE can be performed either as
12684 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12685 DW_OP_{eq,ne}
12687 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12688 DW_OP_{eq,ne}. Pick whatever is shorter. */
12689 if (CONST_INT_P (XEXP (rtl, 1))
12690 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12691 && (size_of_int_loc_descriptor (shift) + 1
12692 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12693 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12694 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12695 & GET_MODE_MASK (op_mode))))
12697 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12698 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12699 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12700 & GET_MODE_MASK (op_mode));
12701 return compare_loc_descriptor (op, op0, op1);
12704 add_loc_descr (&op0, int_loc_descriptor (shift));
12705 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12706 if (CONST_INT_P (XEXP (rtl, 1)))
12707 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12708 else
12710 add_loc_descr (&op1, int_loc_descriptor (shift));
12711 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12713 return compare_loc_descriptor (op, op0, op1);
12716 /* Return location descriptor for unsigned comparison OP RTL. */
12718 static dw_loc_descr_ref
12719 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12720 machine_mode mem_mode)
12722 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12723 dw_loc_descr_ref op0, op1;
12725 if (op_mode == VOIDmode)
12726 op_mode = GET_MODE (XEXP (rtl, 1));
12727 if (op_mode == VOIDmode)
12728 return NULL;
12729 if (!SCALAR_INT_MODE_P (op_mode))
12730 return NULL;
12732 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12733 return NULL;
12735 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12736 VAR_INIT_STATUS_INITIALIZED);
12737 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12738 VAR_INIT_STATUS_INITIALIZED);
12740 if (op0 == NULL || op1 == NULL)
12741 return NULL;
12743 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12745 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12746 dw_loc_descr_ref last0, last1;
12747 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12749 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12751 if (CONST_INT_P (XEXP (rtl, 0)))
12752 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12753 /* deref_size zero extends, so no need to mask it again. */
12754 else if (last0->dw_loc_opc != DW_OP_deref_size
12755 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12757 add_loc_descr (&op0, int_loc_descriptor (mask));
12758 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12760 if (CONST_INT_P (XEXP (rtl, 1)))
12761 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12762 /* deref_size zero extends, so no need to mask it again. */
12763 else if (last1->dw_loc_opc != DW_OP_deref_size
12764 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12766 add_loc_descr (&op1, int_loc_descriptor (mask));
12767 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12770 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12772 HOST_WIDE_INT bias = 1;
12773 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12774 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12775 if (CONST_INT_P (XEXP (rtl, 1)))
12776 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12777 + INTVAL (XEXP (rtl, 1)));
12778 else
12779 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12780 bias, 0));
12782 return compare_loc_descriptor (op, op0, op1);
12785 /* Return location descriptor for {U,S}{MIN,MAX}. */
12787 static dw_loc_descr_ref
12788 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12789 machine_mode mem_mode)
12791 enum dwarf_location_atom op;
12792 dw_loc_descr_ref op0, op1, ret;
12793 dw_loc_descr_ref bra_node, drop_node;
12795 if (dwarf_strict
12796 && (!SCALAR_INT_MODE_P (mode)
12797 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12798 return NULL;
12800 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12801 VAR_INIT_STATUS_INITIALIZED);
12802 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12803 VAR_INIT_STATUS_INITIALIZED);
12805 if (op0 == NULL || op1 == NULL)
12806 return NULL;
12808 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12809 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12810 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12811 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12813 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12815 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12816 add_loc_descr (&op0, int_loc_descriptor (mask));
12817 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12818 add_loc_descr (&op1, int_loc_descriptor (mask));
12819 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12821 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12823 HOST_WIDE_INT bias = 1;
12824 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12825 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12826 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12829 else if (!SCALAR_INT_MODE_P (mode)
12830 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12832 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12833 add_loc_descr (&op0, int_loc_descriptor (shift));
12834 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12835 add_loc_descr (&op1, int_loc_descriptor (shift));
12836 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12838 else if (SCALAR_INT_MODE_P (mode)
12839 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12841 dw_die_ref type_die = base_type_for_mode (mode, 0);
12842 dw_loc_descr_ref cvt;
12843 if (type_die == NULL)
12844 return NULL;
12845 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12846 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12847 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12848 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12849 add_loc_descr (&op0, cvt);
12850 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12851 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12852 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12853 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12854 add_loc_descr (&op1, cvt);
12857 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12858 op = DW_OP_lt;
12859 else
12860 op = DW_OP_gt;
12861 ret = op0;
12862 add_loc_descr (&ret, op1);
12863 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12864 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12865 add_loc_descr (&ret, bra_node);
12866 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12867 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12868 add_loc_descr (&ret, drop_node);
12869 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12870 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12871 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12872 && SCALAR_INT_MODE_P (mode)
12873 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12874 ret = convert_descriptor_to_mode (mode, ret);
12875 return ret;
12878 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12879 but after converting arguments to type_die, afterwards
12880 convert back to unsigned. */
12882 static dw_loc_descr_ref
12883 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12884 machine_mode mode, machine_mode mem_mode)
12886 dw_loc_descr_ref cvt, op0, op1;
12888 if (type_die == NULL)
12889 return NULL;
12890 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12891 VAR_INIT_STATUS_INITIALIZED);
12892 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12893 VAR_INIT_STATUS_INITIALIZED);
12894 if (op0 == NULL || op1 == NULL)
12895 return NULL;
12896 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12897 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12898 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12899 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12900 add_loc_descr (&op0, cvt);
12901 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12902 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12903 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12904 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12905 add_loc_descr (&op1, cvt);
12906 add_loc_descr (&op0, op1);
12907 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12908 return convert_descriptor_to_mode (mode, op0);
12911 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12912 const0 is DW_OP_lit0 or corresponding typed constant,
12913 const1 is DW_OP_lit1 or corresponding typed constant
12914 and constMSB is constant with just the MSB bit set
12915 for the mode):
12916 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12917 L1: const0 DW_OP_swap
12918 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12919 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12920 L3: DW_OP_drop
12921 L4: DW_OP_nop
12923 CTZ is similar:
12924 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12925 L1: const0 DW_OP_swap
12926 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12927 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12928 L3: DW_OP_drop
12929 L4: DW_OP_nop
12931 FFS is similar:
12932 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12933 L1: const1 DW_OP_swap
12934 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12935 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12936 L3: DW_OP_drop
12937 L4: DW_OP_nop */
12939 static dw_loc_descr_ref
12940 clz_loc_descriptor (rtx rtl, machine_mode mode,
12941 machine_mode mem_mode)
12943 dw_loc_descr_ref op0, ret, tmp;
12944 HOST_WIDE_INT valv;
12945 dw_loc_descr_ref l1jump, l1label;
12946 dw_loc_descr_ref l2jump, l2label;
12947 dw_loc_descr_ref l3jump, l3label;
12948 dw_loc_descr_ref l4jump, l4label;
12949 rtx msb;
12951 if (!SCALAR_INT_MODE_P (mode)
12952 || GET_MODE (XEXP (rtl, 0)) != mode)
12953 return NULL;
12955 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12956 VAR_INIT_STATUS_INITIALIZED);
12957 if (op0 == NULL)
12958 return NULL;
12959 ret = op0;
12960 if (GET_CODE (rtl) == CLZ)
12962 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12963 valv = GET_MODE_BITSIZE (mode);
12965 else if (GET_CODE (rtl) == FFS)
12966 valv = 0;
12967 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12968 valv = GET_MODE_BITSIZE (mode);
12969 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12970 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12971 add_loc_descr (&ret, l1jump);
12972 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12973 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12974 VAR_INIT_STATUS_INITIALIZED);
12975 if (tmp == NULL)
12976 return NULL;
12977 add_loc_descr (&ret, tmp);
12978 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12979 add_loc_descr (&ret, l4jump);
12980 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12981 ? const1_rtx : const0_rtx,
12982 mode, mem_mode,
12983 VAR_INIT_STATUS_INITIALIZED);
12984 if (l1label == NULL)
12985 return NULL;
12986 add_loc_descr (&ret, l1label);
12987 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12988 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12989 add_loc_descr (&ret, l2label);
12990 if (GET_CODE (rtl) != CLZ)
12991 msb = const1_rtx;
12992 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12993 msb = GEN_INT (HOST_WIDE_INT_1U
12994 << (GET_MODE_BITSIZE (mode) - 1));
12995 else
12996 msb = immed_wide_int_const
12997 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12998 GET_MODE_PRECISION (mode)), mode);
12999 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13000 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13001 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13002 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13003 else
13004 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13005 VAR_INIT_STATUS_INITIALIZED);
13006 if (tmp == NULL)
13007 return NULL;
13008 add_loc_descr (&ret, tmp);
13009 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13010 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13011 add_loc_descr (&ret, l3jump);
13012 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13013 VAR_INIT_STATUS_INITIALIZED);
13014 if (tmp == NULL)
13015 return NULL;
13016 add_loc_descr (&ret, tmp);
13017 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13018 ? DW_OP_shl : DW_OP_shr, 0, 0));
13019 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13020 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13021 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13022 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13023 add_loc_descr (&ret, l2jump);
13024 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13025 add_loc_descr (&ret, l3label);
13026 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13027 add_loc_descr (&ret, l4label);
13028 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13029 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13030 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13031 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13032 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13033 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13034 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13035 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13036 return ret;
13039 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13040 const1 is DW_OP_lit1 or corresponding typed constant):
13041 const0 DW_OP_swap
13042 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13043 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13044 L2: DW_OP_drop
13046 PARITY is similar:
13047 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13048 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13049 L2: DW_OP_drop */
13051 static dw_loc_descr_ref
13052 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13053 machine_mode mem_mode)
13055 dw_loc_descr_ref op0, ret, tmp;
13056 dw_loc_descr_ref l1jump, l1label;
13057 dw_loc_descr_ref l2jump, l2label;
13059 if (!SCALAR_INT_MODE_P (mode)
13060 || GET_MODE (XEXP (rtl, 0)) != mode)
13061 return NULL;
13063 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13064 VAR_INIT_STATUS_INITIALIZED);
13065 if (op0 == NULL)
13066 return NULL;
13067 ret = op0;
13068 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13069 VAR_INIT_STATUS_INITIALIZED);
13070 if (tmp == NULL)
13071 return NULL;
13072 add_loc_descr (&ret, tmp);
13073 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13074 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13075 add_loc_descr (&ret, l1label);
13076 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13077 add_loc_descr (&ret, l2jump);
13078 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13079 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13080 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13081 VAR_INIT_STATUS_INITIALIZED);
13082 if (tmp == NULL)
13083 return NULL;
13084 add_loc_descr (&ret, tmp);
13085 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13086 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13087 ? DW_OP_plus : DW_OP_xor, 0, 0));
13088 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13089 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13090 VAR_INIT_STATUS_INITIALIZED);
13091 add_loc_descr (&ret, tmp);
13092 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13093 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13094 add_loc_descr (&ret, l1jump);
13095 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13096 add_loc_descr (&ret, l2label);
13097 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13098 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13099 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13100 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13101 return ret;
13104 /* BSWAP (constS is initial shift count, either 56 or 24):
13105 constS const0
13106 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13107 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13108 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13109 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13110 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13112 static dw_loc_descr_ref
13113 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13114 machine_mode mem_mode)
13116 dw_loc_descr_ref op0, ret, tmp;
13117 dw_loc_descr_ref l1jump, l1label;
13118 dw_loc_descr_ref l2jump, l2label;
13120 if (!SCALAR_INT_MODE_P (mode)
13121 || BITS_PER_UNIT != 8
13122 || (GET_MODE_BITSIZE (mode) != 32
13123 && GET_MODE_BITSIZE (mode) != 64))
13124 return NULL;
13126 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13127 VAR_INIT_STATUS_INITIALIZED);
13128 if (op0 == NULL)
13129 return NULL;
13131 ret = op0;
13132 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13133 mode, mem_mode,
13134 VAR_INIT_STATUS_INITIALIZED);
13135 if (tmp == NULL)
13136 return NULL;
13137 add_loc_descr (&ret, tmp);
13138 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13139 VAR_INIT_STATUS_INITIALIZED);
13140 if (tmp == NULL)
13141 return NULL;
13142 add_loc_descr (&ret, tmp);
13143 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13144 add_loc_descr (&ret, l1label);
13145 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13146 mode, mem_mode,
13147 VAR_INIT_STATUS_INITIALIZED);
13148 add_loc_descr (&ret, tmp);
13149 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13150 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13151 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13152 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13153 VAR_INIT_STATUS_INITIALIZED);
13154 if (tmp == NULL)
13155 return NULL;
13156 add_loc_descr (&ret, tmp);
13157 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13158 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13159 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13160 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13161 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13162 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13163 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13164 VAR_INIT_STATUS_INITIALIZED);
13165 add_loc_descr (&ret, tmp);
13166 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13167 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13168 add_loc_descr (&ret, l2jump);
13169 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13170 VAR_INIT_STATUS_INITIALIZED);
13171 add_loc_descr (&ret, tmp);
13172 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13173 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13174 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13175 add_loc_descr (&ret, l1jump);
13176 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13177 add_loc_descr (&ret, l2label);
13178 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13179 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13180 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13181 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13182 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13183 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13184 return ret;
13187 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13188 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13189 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13190 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13192 ROTATERT is similar:
13193 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13194 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13195 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13197 static dw_loc_descr_ref
13198 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13199 machine_mode mem_mode)
13201 rtx rtlop1 = XEXP (rtl, 1);
13202 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13203 int i;
13205 if (!SCALAR_INT_MODE_P (mode))
13206 return NULL;
13208 if (GET_MODE (rtlop1) != VOIDmode
13209 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13210 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13211 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13212 VAR_INIT_STATUS_INITIALIZED);
13213 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13214 VAR_INIT_STATUS_INITIALIZED);
13215 if (op0 == NULL || op1 == NULL)
13216 return NULL;
13217 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13218 for (i = 0; i < 2; i++)
13220 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13221 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13222 mode, mem_mode,
13223 VAR_INIT_STATUS_INITIALIZED);
13224 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13225 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13226 ? DW_OP_const4u
13227 : HOST_BITS_PER_WIDE_INT == 64
13228 ? DW_OP_const8u : DW_OP_constu,
13229 GET_MODE_MASK (mode), 0);
13230 else
13231 mask[i] = NULL;
13232 if (mask[i] == NULL)
13233 return NULL;
13234 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13236 ret = op0;
13237 add_loc_descr (&ret, op1);
13238 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13239 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13240 if (GET_CODE (rtl) == ROTATERT)
13242 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13243 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13244 GET_MODE_BITSIZE (mode), 0));
13246 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13247 if (mask[0] != NULL)
13248 add_loc_descr (&ret, mask[0]);
13249 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13250 if (mask[1] != NULL)
13252 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13253 add_loc_descr (&ret, mask[1]);
13254 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13256 if (GET_CODE (rtl) == ROTATE)
13258 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13259 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13260 GET_MODE_BITSIZE (mode), 0));
13262 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13263 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13264 return ret;
13267 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13268 for DEBUG_PARAMETER_REF RTL. */
13270 static dw_loc_descr_ref
13271 parameter_ref_descriptor (rtx rtl)
13273 dw_loc_descr_ref ret;
13274 dw_die_ref ref;
13276 if (dwarf_strict)
13277 return NULL;
13278 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13279 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13280 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13281 if (ref)
13283 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13284 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13285 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13287 else
13289 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13290 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13292 return ret;
13295 /* The following routine converts the RTL for a variable or parameter
13296 (resident in memory) into an equivalent Dwarf representation of a
13297 mechanism for getting the address of that same variable onto the top of a
13298 hypothetical "address evaluation" stack.
13300 When creating memory location descriptors, we are effectively transforming
13301 the RTL for a memory-resident object into its Dwarf postfix expression
13302 equivalent. This routine recursively descends an RTL tree, turning
13303 it into Dwarf postfix code as it goes.
13305 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13307 MEM_MODE is the mode of the memory reference, needed to handle some
13308 autoincrement addressing modes.
13310 Return 0 if we can't represent the location. */
13312 dw_loc_descr_ref
13313 mem_loc_descriptor (rtx rtl, machine_mode mode,
13314 machine_mode mem_mode,
13315 enum var_init_status initialized)
13317 dw_loc_descr_ref mem_loc_result = NULL;
13318 enum dwarf_location_atom op;
13319 dw_loc_descr_ref op0, op1;
13320 rtx inner = NULL_RTX;
13322 if (mode == VOIDmode)
13323 mode = GET_MODE (rtl);
13325 /* Note that for a dynamically sized array, the location we will generate a
13326 description of here will be the lowest numbered location which is
13327 actually within the array. That's *not* necessarily the same as the
13328 zeroth element of the array. */
13330 rtl = targetm.delegitimize_address (rtl);
13332 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13333 return NULL;
13335 switch (GET_CODE (rtl))
13337 case POST_INC:
13338 case POST_DEC:
13339 case POST_MODIFY:
13340 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13342 case SUBREG:
13343 /* The case of a subreg may arise when we have a local (register)
13344 variable or a formal (register) parameter which doesn't quite fill
13345 up an entire register. For now, just assume that it is
13346 legitimate to make the Dwarf info refer to the whole register which
13347 contains the given subreg. */
13348 if (!subreg_lowpart_p (rtl))
13349 break;
13350 inner = SUBREG_REG (rtl);
13351 /* FALLTHRU */
13352 case TRUNCATE:
13353 if (inner == NULL_RTX)
13354 inner = XEXP (rtl, 0);
13355 if (SCALAR_INT_MODE_P (mode)
13356 && SCALAR_INT_MODE_P (GET_MODE (inner))
13357 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13358 #ifdef POINTERS_EXTEND_UNSIGNED
13359 || (mode == Pmode && mem_mode != VOIDmode)
13360 #endif
13362 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13364 mem_loc_result = mem_loc_descriptor (inner,
13365 GET_MODE (inner),
13366 mem_mode, initialized);
13367 break;
13369 if (dwarf_strict)
13370 break;
13371 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13372 break;
13373 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13374 && (!SCALAR_INT_MODE_P (mode)
13375 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13376 break;
13377 else
13379 dw_die_ref type_die;
13380 dw_loc_descr_ref cvt;
13382 mem_loc_result = mem_loc_descriptor (inner,
13383 GET_MODE (inner),
13384 mem_mode, initialized);
13385 if (mem_loc_result == NULL)
13386 break;
13387 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13388 if (type_die == NULL)
13390 mem_loc_result = NULL;
13391 break;
13393 if (GET_MODE_SIZE (mode)
13394 != GET_MODE_SIZE (GET_MODE (inner)))
13395 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13396 else
13397 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13398 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13399 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13400 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13401 add_loc_descr (&mem_loc_result, cvt);
13402 if (SCALAR_INT_MODE_P (mode)
13403 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13405 /* Convert it to untyped afterwards. */
13406 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13407 add_loc_descr (&mem_loc_result, cvt);
13410 break;
13412 case REG:
13413 if (! SCALAR_INT_MODE_P (mode)
13414 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13415 && rtl != arg_pointer_rtx
13416 && rtl != frame_pointer_rtx
13417 #ifdef POINTERS_EXTEND_UNSIGNED
13418 && (mode != Pmode || mem_mode == VOIDmode)
13419 #endif
13422 dw_die_ref type_die;
13423 unsigned int dbx_regnum;
13425 if (dwarf_strict)
13426 break;
13427 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13428 break;
13429 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13430 if (type_die == NULL)
13431 break;
13433 dbx_regnum = dbx_reg_number (rtl);
13434 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13435 break;
13436 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13437 dbx_regnum, 0);
13438 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13439 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13440 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13441 break;
13443 /* Whenever a register number forms a part of the description of the
13444 method for calculating the (dynamic) address of a memory resident
13445 object, DWARF rules require the register number be referred to as
13446 a "base register". This distinction is not based in any way upon
13447 what category of register the hardware believes the given register
13448 belongs to. This is strictly DWARF terminology we're dealing with
13449 here. Note that in cases where the location of a memory-resident
13450 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13451 OP_CONST (0)) the actual DWARF location descriptor that we generate
13452 may just be OP_BASEREG (basereg). This may look deceptively like
13453 the object in question was allocated to a register (rather than in
13454 memory) so DWARF consumers need to be aware of the subtle
13455 distinction between OP_REG and OP_BASEREG. */
13456 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13457 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13458 else if (stack_realign_drap
13459 && crtl->drap_reg
13460 && crtl->args.internal_arg_pointer == rtl
13461 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13463 /* If RTL is internal_arg_pointer, which has been optimized
13464 out, use DRAP instead. */
13465 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13466 VAR_INIT_STATUS_INITIALIZED);
13468 break;
13470 case SIGN_EXTEND:
13471 case ZERO_EXTEND:
13472 if (!SCALAR_INT_MODE_P (mode))
13473 break;
13474 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13475 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13476 if (op0 == 0)
13477 break;
13478 else if (GET_CODE (rtl) == ZERO_EXTEND
13479 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13480 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13481 < HOST_BITS_PER_WIDE_INT
13482 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13483 to expand zero extend as two shifts instead of
13484 masking. */
13485 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13487 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13488 mem_loc_result = op0;
13489 add_loc_descr (&mem_loc_result,
13490 int_loc_descriptor (GET_MODE_MASK (imode)));
13491 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13493 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13495 int shift = DWARF2_ADDR_SIZE
13496 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13497 shift *= BITS_PER_UNIT;
13498 if (GET_CODE (rtl) == SIGN_EXTEND)
13499 op = DW_OP_shra;
13500 else
13501 op = DW_OP_shr;
13502 mem_loc_result = op0;
13503 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13504 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13505 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13506 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13508 else if (!dwarf_strict)
13510 dw_die_ref type_die1, type_die2;
13511 dw_loc_descr_ref cvt;
13513 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13514 GET_CODE (rtl) == ZERO_EXTEND);
13515 if (type_die1 == NULL)
13516 break;
13517 type_die2 = base_type_for_mode (mode, 1);
13518 if (type_die2 == NULL)
13519 break;
13520 mem_loc_result = op0;
13521 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13522 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13523 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13524 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13525 add_loc_descr (&mem_loc_result, cvt);
13526 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13527 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13528 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13529 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13530 add_loc_descr (&mem_loc_result, cvt);
13532 break;
13534 case MEM:
13536 rtx new_rtl = avoid_constant_pool_reference (rtl);
13537 if (new_rtl != rtl)
13539 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13540 initialized);
13541 if (mem_loc_result != NULL)
13542 return mem_loc_result;
13545 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13546 get_address_mode (rtl), mode,
13547 VAR_INIT_STATUS_INITIALIZED);
13548 if (mem_loc_result == NULL)
13549 mem_loc_result = tls_mem_loc_descriptor (rtl);
13550 if (mem_loc_result != NULL)
13552 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13553 || !SCALAR_INT_MODE_P(mode))
13555 dw_die_ref type_die;
13556 dw_loc_descr_ref deref;
13558 if (dwarf_strict)
13559 return NULL;
13560 type_die
13561 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13562 if (type_die == NULL)
13563 return NULL;
13564 deref = new_loc_descr (DW_OP_GNU_deref_type,
13565 GET_MODE_SIZE (mode), 0);
13566 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13567 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13568 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13569 add_loc_descr (&mem_loc_result, deref);
13571 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13572 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13573 else
13574 add_loc_descr (&mem_loc_result,
13575 new_loc_descr (DW_OP_deref_size,
13576 GET_MODE_SIZE (mode), 0));
13578 break;
13580 case LO_SUM:
13581 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13583 case LABEL_REF:
13584 /* Some ports can transform a symbol ref into a label ref, because
13585 the symbol ref is too far away and has to be dumped into a constant
13586 pool. */
13587 case CONST:
13588 case SYMBOL_REF:
13589 if (!SCALAR_INT_MODE_P (mode)
13590 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13591 #ifdef POINTERS_EXTEND_UNSIGNED
13592 && (mode != Pmode || mem_mode == VOIDmode)
13593 #endif
13595 break;
13596 if (GET_CODE (rtl) == SYMBOL_REF
13597 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13599 dw_loc_descr_ref temp;
13601 /* If this is not defined, we have no way to emit the data. */
13602 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13603 break;
13605 temp = new_addr_loc_descr (rtl, dtprel_true);
13607 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13608 add_loc_descr (&mem_loc_result, temp);
13610 break;
13613 if (!const_ok_for_output (rtl))
13615 if (GET_CODE (rtl) == CONST)
13616 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13617 initialized);
13618 break;
13621 symref:
13622 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13623 vec_safe_push (used_rtx_array, rtl);
13624 break;
13626 case CONCAT:
13627 case CONCATN:
13628 case VAR_LOCATION:
13629 case DEBUG_IMPLICIT_PTR:
13630 expansion_failed (NULL_TREE, rtl,
13631 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13632 return 0;
13634 case ENTRY_VALUE:
13635 if (dwarf_strict)
13636 return NULL;
13637 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13639 if (!SCALAR_INT_MODE_P (mode)
13640 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13641 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13642 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13643 else
13645 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13646 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13647 return NULL;
13648 op0 = one_reg_loc_descriptor (dbx_regnum,
13649 VAR_INIT_STATUS_INITIALIZED);
13652 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13653 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13655 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13656 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13657 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13658 return NULL;
13660 else
13661 gcc_unreachable ();
13662 if (op0 == NULL)
13663 return NULL;
13664 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13665 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13666 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13667 break;
13669 case DEBUG_PARAMETER_REF:
13670 mem_loc_result = parameter_ref_descriptor (rtl);
13671 break;
13673 case PRE_MODIFY:
13674 /* Extract the PLUS expression nested inside and fall into
13675 PLUS code below. */
13676 rtl = XEXP (rtl, 1);
13677 goto plus;
13679 case PRE_INC:
13680 case PRE_DEC:
13681 /* Turn these into a PLUS expression and fall into the PLUS code
13682 below. */
13683 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13684 gen_int_mode (GET_CODE (rtl) == PRE_INC
13685 ? GET_MODE_UNIT_SIZE (mem_mode)
13686 : -GET_MODE_UNIT_SIZE (mem_mode),
13687 mode));
13689 /* fall through */
13691 case PLUS:
13692 plus:
13693 if (is_based_loc (rtl)
13694 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13695 || XEXP (rtl, 0) == arg_pointer_rtx
13696 || XEXP (rtl, 0) == frame_pointer_rtx)
13697 && SCALAR_INT_MODE_P (mode))
13698 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13699 INTVAL (XEXP (rtl, 1)),
13700 VAR_INIT_STATUS_INITIALIZED);
13701 else
13703 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13704 VAR_INIT_STATUS_INITIALIZED);
13705 if (mem_loc_result == 0)
13706 break;
13708 if (CONST_INT_P (XEXP (rtl, 1))
13709 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13710 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13711 else
13713 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13714 VAR_INIT_STATUS_INITIALIZED);
13715 if (op1 == 0)
13716 return NULL;
13717 add_loc_descr (&mem_loc_result, op1);
13718 add_loc_descr (&mem_loc_result,
13719 new_loc_descr (DW_OP_plus, 0, 0));
13722 break;
13724 /* If a pseudo-reg is optimized away, it is possible for it to
13725 be replaced with a MEM containing a multiply or shift. */
13726 case MINUS:
13727 op = DW_OP_minus;
13728 goto do_binop;
13730 case MULT:
13731 op = DW_OP_mul;
13732 goto do_binop;
13734 case DIV:
13735 if (!dwarf_strict
13736 && SCALAR_INT_MODE_P (mode)
13737 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13739 mem_loc_result = typed_binop (DW_OP_div, rtl,
13740 base_type_for_mode (mode, 0),
13741 mode, mem_mode);
13742 break;
13744 op = DW_OP_div;
13745 goto do_binop;
13747 case UMOD:
13748 op = DW_OP_mod;
13749 goto do_binop;
13751 case ASHIFT:
13752 op = DW_OP_shl;
13753 goto do_shift;
13755 case ASHIFTRT:
13756 op = DW_OP_shra;
13757 goto do_shift;
13759 case LSHIFTRT:
13760 op = DW_OP_shr;
13761 goto do_shift;
13763 do_shift:
13764 if (!SCALAR_INT_MODE_P (mode))
13765 break;
13766 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13767 VAR_INIT_STATUS_INITIALIZED);
13769 rtx rtlop1 = XEXP (rtl, 1);
13770 if (GET_MODE (rtlop1) != VOIDmode
13771 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13772 < GET_MODE_BITSIZE (mode))
13773 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13774 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13775 VAR_INIT_STATUS_INITIALIZED);
13778 if (op0 == 0 || op1 == 0)
13779 break;
13781 mem_loc_result = op0;
13782 add_loc_descr (&mem_loc_result, op1);
13783 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13784 break;
13786 case AND:
13787 op = DW_OP_and;
13788 goto do_binop;
13790 case IOR:
13791 op = DW_OP_or;
13792 goto do_binop;
13794 case XOR:
13795 op = DW_OP_xor;
13796 goto do_binop;
13798 do_binop:
13799 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13800 VAR_INIT_STATUS_INITIALIZED);
13801 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13802 VAR_INIT_STATUS_INITIALIZED);
13804 if (op0 == 0 || op1 == 0)
13805 break;
13807 mem_loc_result = op0;
13808 add_loc_descr (&mem_loc_result, op1);
13809 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13810 break;
13812 case MOD:
13813 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13815 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13816 base_type_for_mode (mode, 0),
13817 mode, mem_mode);
13818 break;
13821 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13822 VAR_INIT_STATUS_INITIALIZED);
13823 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13824 VAR_INIT_STATUS_INITIALIZED);
13826 if (op0 == 0 || op1 == 0)
13827 break;
13829 mem_loc_result = op0;
13830 add_loc_descr (&mem_loc_result, op1);
13831 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13832 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13833 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13834 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13835 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13836 break;
13838 case UDIV:
13839 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13841 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13843 op = DW_OP_div;
13844 goto do_binop;
13846 mem_loc_result = typed_binop (DW_OP_div, rtl,
13847 base_type_for_mode (mode, 1),
13848 mode, mem_mode);
13850 break;
13852 case NOT:
13853 op = DW_OP_not;
13854 goto do_unop;
13856 case ABS:
13857 op = DW_OP_abs;
13858 goto do_unop;
13860 case NEG:
13861 op = DW_OP_neg;
13862 goto do_unop;
13864 do_unop:
13865 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13866 VAR_INIT_STATUS_INITIALIZED);
13868 if (op0 == 0)
13869 break;
13871 mem_loc_result = op0;
13872 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13873 break;
13875 case CONST_INT:
13876 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13877 #ifdef POINTERS_EXTEND_UNSIGNED
13878 || (mode == Pmode
13879 && mem_mode != VOIDmode
13880 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13881 #endif
13884 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13885 break;
13887 if (!dwarf_strict
13888 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13889 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13891 dw_die_ref type_die = base_type_for_mode (mode, 1);
13892 machine_mode amode;
13893 if (type_die == NULL)
13894 return NULL;
13895 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13896 MODE_INT, 0);
13897 if (INTVAL (rtl) >= 0
13898 && amode != BLKmode
13899 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13900 /* const DW_OP_GNU_convert <XXX> vs.
13901 DW_OP_GNU_const_type <XXX, 1, const>. */
13902 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13903 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13905 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13906 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13907 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13908 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13909 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13910 add_loc_descr (&mem_loc_result, op0);
13911 return mem_loc_result;
13913 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13914 INTVAL (rtl));
13915 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13916 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13917 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13918 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13919 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13920 else
13922 mem_loc_result->dw_loc_oprnd2.val_class
13923 = dw_val_class_const_double;
13924 mem_loc_result->dw_loc_oprnd2.v.val_double
13925 = double_int::from_shwi (INTVAL (rtl));
13928 break;
13930 case CONST_DOUBLE:
13931 if (!dwarf_strict)
13933 dw_die_ref type_die;
13935 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13936 CONST_DOUBLE rtx could represent either a large integer
13937 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13938 the value is always a floating point constant.
13940 When it is an integer, a CONST_DOUBLE is used whenever
13941 the constant requires 2 HWIs to be adequately represented.
13942 We output CONST_DOUBLEs as blocks. */
13943 if (mode == VOIDmode
13944 || (GET_MODE (rtl) == VOIDmode
13945 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13946 break;
13947 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13948 if (type_die == NULL)
13949 return NULL;
13950 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13951 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13952 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13953 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13954 #if TARGET_SUPPORTS_WIDE_INT == 0
13955 if (!SCALAR_FLOAT_MODE_P (mode))
13957 mem_loc_result->dw_loc_oprnd2.val_class
13958 = dw_val_class_const_double;
13959 mem_loc_result->dw_loc_oprnd2.v.val_double
13960 = rtx_to_double_int (rtl);
13962 else
13963 #endif
13965 unsigned int length = GET_MODE_SIZE (mode);
13966 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13968 insert_float (rtl, array);
13969 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13970 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13971 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13972 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13975 break;
13977 case CONST_WIDE_INT:
13978 if (!dwarf_strict)
13980 dw_die_ref type_die;
13982 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13983 if (type_die == NULL)
13984 return NULL;
13985 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13986 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13987 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13988 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13989 mem_loc_result->dw_loc_oprnd2.val_class
13990 = dw_val_class_wide_int;
13991 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13992 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13994 break;
13996 case EQ:
13997 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13998 break;
14000 case GE:
14001 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14002 break;
14004 case GT:
14005 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14006 break;
14008 case LE:
14009 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14010 break;
14012 case LT:
14013 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14014 break;
14016 case NE:
14017 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14018 break;
14020 case GEU:
14021 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14022 break;
14024 case GTU:
14025 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14026 break;
14028 case LEU:
14029 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14030 break;
14032 case LTU:
14033 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14034 break;
14036 case UMIN:
14037 case UMAX:
14038 if (!SCALAR_INT_MODE_P (mode))
14039 break;
14040 /* FALLTHRU */
14041 case SMIN:
14042 case SMAX:
14043 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14044 break;
14046 case ZERO_EXTRACT:
14047 case SIGN_EXTRACT:
14048 if (CONST_INT_P (XEXP (rtl, 1))
14049 && CONST_INT_P (XEXP (rtl, 2))
14050 && ((unsigned) INTVAL (XEXP (rtl, 1))
14051 + (unsigned) INTVAL (XEXP (rtl, 2))
14052 <= GET_MODE_BITSIZE (mode))
14053 && SCALAR_INT_MODE_P (mode)
14054 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14055 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14057 int shift, size;
14058 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14059 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14060 if (op0 == 0)
14061 break;
14062 if (GET_CODE (rtl) == SIGN_EXTRACT)
14063 op = DW_OP_shra;
14064 else
14065 op = DW_OP_shr;
14066 mem_loc_result = op0;
14067 size = INTVAL (XEXP (rtl, 1));
14068 shift = INTVAL (XEXP (rtl, 2));
14069 if (BITS_BIG_ENDIAN)
14070 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14071 - shift - size;
14072 if (shift + size != (int) DWARF2_ADDR_SIZE)
14074 add_loc_descr (&mem_loc_result,
14075 int_loc_descriptor (DWARF2_ADDR_SIZE
14076 - shift - size));
14077 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14079 if (size != (int) DWARF2_ADDR_SIZE)
14081 add_loc_descr (&mem_loc_result,
14082 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14083 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14086 break;
14088 case IF_THEN_ELSE:
14090 dw_loc_descr_ref op2, bra_node, drop_node;
14091 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14092 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14093 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14094 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14095 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14096 VAR_INIT_STATUS_INITIALIZED);
14097 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14098 VAR_INIT_STATUS_INITIALIZED);
14099 if (op0 == NULL || op1 == NULL || op2 == NULL)
14100 break;
14102 mem_loc_result = op1;
14103 add_loc_descr (&mem_loc_result, op2);
14104 add_loc_descr (&mem_loc_result, op0);
14105 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14106 add_loc_descr (&mem_loc_result, bra_node);
14107 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14108 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14109 add_loc_descr (&mem_loc_result, drop_node);
14110 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14111 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14113 break;
14115 case FLOAT_EXTEND:
14116 case FLOAT_TRUNCATE:
14117 case FLOAT:
14118 case UNSIGNED_FLOAT:
14119 case FIX:
14120 case UNSIGNED_FIX:
14121 if (!dwarf_strict)
14123 dw_die_ref type_die;
14124 dw_loc_descr_ref cvt;
14126 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14127 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14128 if (op0 == NULL)
14129 break;
14130 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14131 && (GET_CODE (rtl) == FLOAT
14132 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14133 <= DWARF2_ADDR_SIZE))
14135 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14136 GET_CODE (rtl) == UNSIGNED_FLOAT);
14137 if (type_die == NULL)
14138 break;
14139 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14140 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14141 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14142 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14143 add_loc_descr (&op0, cvt);
14145 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14146 if (type_die == NULL)
14147 break;
14148 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14149 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14150 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14151 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14152 add_loc_descr (&op0, cvt);
14153 if (SCALAR_INT_MODE_P (mode)
14154 && (GET_CODE (rtl) == FIX
14155 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14157 op0 = convert_descriptor_to_mode (mode, op0);
14158 if (op0 == NULL)
14159 break;
14161 mem_loc_result = op0;
14163 break;
14165 case CLZ:
14166 case CTZ:
14167 case FFS:
14168 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14169 break;
14171 case POPCOUNT:
14172 case PARITY:
14173 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14174 break;
14176 case BSWAP:
14177 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14178 break;
14180 case ROTATE:
14181 case ROTATERT:
14182 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14183 break;
14185 case COMPARE:
14186 /* In theory, we could implement the above. */
14187 /* DWARF cannot represent the unsigned compare operations
14188 natively. */
14189 case SS_MULT:
14190 case US_MULT:
14191 case SS_DIV:
14192 case US_DIV:
14193 case SS_PLUS:
14194 case US_PLUS:
14195 case SS_MINUS:
14196 case US_MINUS:
14197 case SS_NEG:
14198 case US_NEG:
14199 case SS_ABS:
14200 case SS_ASHIFT:
14201 case US_ASHIFT:
14202 case SS_TRUNCATE:
14203 case US_TRUNCATE:
14204 case UNORDERED:
14205 case ORDERED:
14206 case UNEQ:
14207 case UNGE:
14208 case UNGT:
14209 case UNLE:
14210 case UNLT:
14211 case LTGT:
14212 case FRACT_CONVERT:
14213 case UNSIGNED_FRACT_CONVERT:
14214 case SAT_FRACT:
14215 case UNSIGNED_SAT_FRACT:
14216 case SQRT:
14217 case ASM_OPERANDS:
14218 case VEC_MERGE:
14219 case VEC_SELECT:
14220 case VEC_CONCAT:
14221 case VEC_DUPLICATE:
14222 case UNSPEC:
14223 case HIGH:
14224 case FMA:
14225 case STRICT_LOW_PART:
14226 case CONST_VECTOR:
14227 case CONST_FIXED:
14228 case CLRSB:
14229 case CLOBBER:
14230 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14231 can't express it in the debug info. This can happen e.g. with some
14232 TLS UNSPECs. */
14233 break;
14235 case CONST_STRING:
14236 resolve_one_addr (&rtl);
14237 goto symref;
14239 default:
14240 if (flag_checking)
14242 print_rtl (stderr, rtl);
14243 gcc_unreachable ();
14245 break;
14248 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14249 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14251 return mem_loc_result;
14254 /* Return a descriptor that describes the concatenation of two locations.
14255 This is typically a complex variable. */
14257 static dw_loc_descr_ref
14258 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14260 dw_loc_descr_ref cc_loc_result = NULL;
14261 dw_loc_descr_ref x0_ref
14262 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14263 dw_loc_descr_ref x1_ref
14264 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14266 if (x0_ref == 0 || x1_ref == 0)
14267 return 0;
14269 cc_loc_result = x0_ref;
14270 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14272 add_loc_descr (&cc_loc_result, x1_ref);
14273 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14275 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14276 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14278 return cc_loc_result;
14281 /* Return a descriptor that describes the concatenation of N
14282 locations. */
14284 static dw_loc_descr_ref
14285 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14287 unsigned int i;
14288 dw_loc_descr_ref cc_loc_result = NULL;
14289 unsigned int n = XVECLEN (concatn, 0);
14291 for (i = 0; i < n; ++i)
14293 dw_loc_descr_ref ref;
14294 rtx x = XVECEXP (concatn, 0, i);
14296 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14297 if (ref == NULL)
14298 return NULL;
14300 add_loc_descr (&cc_loc_result, ref);
14301 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14304 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14305 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14307 return cc_loc_result;
14310 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14311 for DEBUG_IMPLICIT_PTR RTL. */
14313 static dw_loc_descr_ref
14314 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14316 dw_loc_descr_ref ret;
14317 dw_die_ref ref;
14319 if (dwarf_strict)
14320 return NULL;
14321 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14322 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14323 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14324 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14325 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14326 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14327 if (ref)
14329 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14330 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14331 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14333 else
14335 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14336 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14338 return ret;
14341 /* Output a proper Dwarf location descriptor for a variable or parameter
14342 which is either allocated in a register or in a memory location. For a
14343 register, we just generate an OP_REG and the register number. For a
14344 memory location we provide a Dwarf postfix expression describing how to
14345 generate the (dynamic) address of the object onto the address stack.
14347 MODE is mode of the decl if this loc_descriptor is going to be used in
14348 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14349 allowed, VOIDmode otherwise.
14351 If we don't know how to describe it, return 0. */
14353 static dw_loc_descr_ref
14354 loc_descriptor (rtx rtl, machine_mode mode,
14355 enum var_init_status initialized)
14357 dw_loc_descr_ref loc_result = NULL;
14359 switch (GET_CODE (rtl))
14361 case SUBREG:
14362 /* The case of a subreg may arise when we have a local (register)
14363 variable or a formal (register) parameter which doesn't quite fill
14364 up an entire register. For now, just assume that it is
14365 legitimate to make the Dwarf info refer to the whole register which
14366 contains the given subreg. */
14367 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14368 loc_result = loc_descriptor (SUBREG_REG (rtl),
14369 GET_MODE (SUBREG_REG (rtl)), initialized);
14370 else
14371 goto do_default;
14372 break;
14374 case REG:
14375 loc_result = reg_loc_descriptor (rtl, initialized);
14376 break;
14378 case MEM:
14379 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14380 GET_MODE (rtl), initialized);
14381 if (loc_result == NULL)
14382 loc_result = tls_mem_loc_descriptor (rtl);
14383 if (loc_result == NULL)
14385 rtx new_rtl = avoid_constant_pool_reference (rtl);
14386 if (new_rtl != rtl)
14387 loc_result = loc_descriptor (new_rtl, mode, initialized);
14389 break;
14391 case CONCAT:
14392 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14393 initialized);
14394 break;
14396 case CONCATN:
14397 loc_result = concatn_loc_descriptor (rtl, initialized);
14398 break;
14400 case VAR_LOCATION:
14401 /* Single part. */
14402 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14404 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14405 if (GET_CODE (loc) == EXPR_LIST)
14406 loc = XEXP (loc, 0);
14407 loc_result = loc_descriptor (loc, mode, initialized);
14408 break;
14411 rtl = XEXP (rtl, 1);
14412 /* FALLTHRU */
14414 case PARALLEL:
14416 rtvec par_elems = XVEC (rtl, 0);
14417 int num_elem = GET_NUM_ELEM (par_elems);
14418 machine_mode mode;
14419 int i;
14421 /* Create the first one, so we have something to add to. */
14422 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14423 VOIDmode, initialized);
14424 if (loc_result == NULL)
14425 return NULL;
14426 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14427 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14428 for (i = 1; i < num_elem; i++)
14430 dw_loc_descr_ref temp;
14432 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14433 VOIDmode, initialized);
14434 if (temp == NULL)
14435 return NULL;
14436 add_loc_descr (&loc_result, temp);
14437 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14438 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14441 break;
14443 case CONST_INT:
14444 if (mode != VOIDmode && mode != BLKmode)
14445 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14446 INTVAL (rtl));
14447 break;
14449 case CONST_DOUBLE:
14450 if (mode == VOIDmode)
14451 mode = GET_MODE (rtl);
14453 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14455 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14457 /* Note that a CONST_DOUBLE rtx could represent either an integer
14458 or a floating-point constant. A CONST_DOUBLE is used whenever
14459 the constant requires more than one word in order to be
14460 adequately represented. We output CONST_DOUBLEs as blocks. */
14461 loc_result = new_loc_descr (DW_OP_implicit_value,
14462 GET_MODE_SIZE (mode), 0);
14463 #if TARGET_SUPPORTS_WIDE_INT == 0
14464 if (!SCALAR_FLOAT_MODE_P (mode))
14466 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14467 loc_result->dw_loc_oprnd2.v.val_double
14468 = rtx_to_double_int (rtl);
14470 else
14471 #endif
14473 unsigned int length = GET_MODE_SIZE (mode);
14474 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14476 insert_float (rtl, array);
14477 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14478 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14479 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14480 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14483 break;
14485 case CONST_WIDE_INT:
14486 if (mode == VOIDmode)
14487 mode = GET_MODE (rtl);
14489 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14491 loc_result = new_loc_descr (DW_OP_implicit_value,
14492 GET_MODE_SIZE (mode), 0);
14493 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14494 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14495 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14497 break;
14499 case CONST_VECTOR:
14500 if (mode == VOIDmode)
14501 mode = GET_MODE (rtl);
14503 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14505 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14506 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14507 unsigned char *array
14508 = ggc_vec_alloc<unsigned char> (length * elt_size);
14509 unsigned int i;
14510 unsigned char *p;
14511 machine_mode imode = GET_MODE_INNER (mode);
14513 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14514 switch (GET_MODE_CLASS (mode))
14516 case MODE_VECTOR_INT:
14517 for (i = 0, p = array; i < length; i++, p += elt_size)
14519 rtx elt = CONST_VECTOR_ELT (rtl, i);
14520 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14522 break;
14524 case MODE_VECTOR_FLOAT:
14525 for (i = 0, p = array; i < length; i++, p += elt_size)
14527 rtx elt = CONST_VECTOR_ELT (rtl, i);
14528 insert_float (elt, p);
14530 break;
14532 default:
14533 gcc_unreachable ();
14536 loc_result = new_loc_descr (DW_OP_implicit_value,
14537 length * elt_size, 0);
14538 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14539 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14540 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14541 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14543 break;
14545 case CONST:
14546 if (mode == VOIDmode
14547 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14548 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14549 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14551 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14552 break;
14554 /* FALLTHROUGH */
14555 case SYMBOL_REF:
14556 if (!const_ok_for_output (rtl))
14557 break;
14558 /* FALLTHROUGH */
14559 case LABEL_REF:
14560 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14561 && (dwarf_version >= 4 || !dwarf_strict))
14563 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14564 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14565 vec_safe_push (used_rtx_array, rtl);
14567 break;
14569 case DEBUG_IMPLICIT_PTR:
14570 loc_result = implicit_ptr_descriptor (rtl, 0);
14571 break;
14573 case PLUS:
14574 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14575 && CONST_INT_P (XEXP (rtl, 1)))
14577 loc_result
14578 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14579 break;
14581 /* FALLTHRU */
14582 do_default:
14583 default:
14584 if ((SCALAR_INT_MODE_P (mode)
14585 && GET_MODE (rtl) == mode
14586 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14587 && dwarf_version >= 4)
14588 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14590 /* Value expression. */
14591 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14592 if (loc_result)
14593 add_loc_descr (&loc_result,
14594 new_loc_descr (DW_OP_stack_value, 0, 0));
14596 break;
14599 return loc_result;
14602 /* We need to figure out what section we should use as the base for the
14603 address ranges where a given location is valid.
14604 1. If this particular DECL has a section associated with it, use that.
14605 2. If this function has a section associated with it, use that.
14606 3. Otherwise, use the text section.
14607 XXX: If you split a variable across multiple sections, we won't notice. */
14609 static const char *
14610 secname_for_decl (const_tree decl)
14612 const char *secname;
14614 if (VAR_OR_FUNCTION_DECL_P (decl)
14615 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14616 && DECL_SECTION_NAME (decl))
14617 secname = DECL_SECTION_NAME (decl);
14618 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14619 secname = DECL_SECTION_NAME (current_function_decl);
14620 else if (cfun && in_cold_section_p)
14621 secname = crtl->subsections.cold_section_label;
14622 else
14623 secname = text_section_label;
14625 return secname;
14628 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14630 static bool
14631 decl_by_reference_p (tree decl)
14633 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14634 || VAR_P (decl))
14635 && DECL_BY_REFERENCE (decl));
14638 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14639 for VARLOC. */
14641 static dw_loc_descr_ref
14642 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14643 enum var_init_status initialized)
14645 int have_address = 0;
14646 dw_loc_descr_ref descr;
14647 machine_mode mode;
14649 if (want_address != 2)
14651 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14652 /* Single part. */
14653 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14655 varloc = PAT_VAR_LOCATION_LOC (varloc);
14656 if (GET_CODE (varloc) == EXPR_LIST)
14657 varloc = XEXP (varloc, 0);
14658 mode = GET_MODE (varloc);
14659 if (MEM_P (varloc))
14661 rtx addr = XEXP (varloc, 0);
14662 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14663 mode, initialized);
14664 if (descr)
14665 have_address = 1;
14666 else
14668 rtx x = avoid_constant_pool_reference (varloc);
14669 if (x != varloc)
14670 descr = mem_loc_descriptor (x, mode, VOIDmode,
14671 initialized);
14674 else
14675 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14677 else
14678 return 0;
14680 else
14682 if (GET_CODE (varloc) == VAR_LOCATION)
14683 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14684 else
14685 mode = DECL_MODE (loc);
14686 descr = loc_descriptor (varloc, mode, initialized);
14687 have_address = 1;
14690 if (!descr)
14691 return 0;
14693 if (want_address == 2 && !have_address
14694 && (dwarf_version >= 4 || !dwarf_strict))
14696 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14698 expansion_failed (loc, NULL_RTX,
14699 "DWARF address size mismatch");
14700 return 0;
14702 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14703 have_address = 1;
14705 /* Show if we can't fill the request for an address. */
14706 if (want_address && !have_address)
14708 expansion_failed (loc, NULL_RTX,
14709 "Want address and only have value");
14710 return 0;
14713 /* If we've got an address and don't want one, dereference. */
14714 if (!want_address && have_address)
14716 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14717 enum dwarf_location_atom op;
14719 if (size > DWARF2_ADDR_SIZE || size == -1)
14721 expansion_failed (loc, NULL_RTX,
14722 "DWARF address size mismatch");
14723 return 0;
14725 else if (size == DWARF2_ADDR_SIZE)
14726 op = DW_OP_deref;
14727 else
14728 op = DW_OP_deref_size;
14730 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14733 return descr;
14736 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14737 if it is not possible. */
14739 static dw_loc_descr_ref
14740 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14742 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14743 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14744 else if (dwarf_version >= 3 || !dwarf_strict)
14745 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14746 else
14747 return NULL;
14750 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14751 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14753 static dw_loc_descr_ref
14754 dw_sra_loc_expr (tree decl, rtx loc)
14756 rtx p;
14757 unsigned HOST_WIDE_INT padsize = 0;
14758 dw_loc_descr_ref descr, *descr_tail;
14759 unsigned HOST_WIDE_INT decl_size;
14760 rtx varloc;
14761 enum var_init_status initialized;
14763 if (DECL_SIZE (decl) == NULL
14764 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14765 return NULL;
14767 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14768 descr = NULL;
14769 descr_tail = &descr;
14771 for (p = loc; p; p = XEXP (p, 1))
14773 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14774 rtx loc_note = *decl_piece_varloc_ptr (p);
14775 dw_loc_descr_ref cur_descr;
14776 dw_loc_descr_ref *tail, last = NULL;
14777 unsigned HOST_WIDE_INT opsize = 0;
14779 if (loc_note == NULL_RTX
14780 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14782 padsize += bitsize;
14783 continue;
14785 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14786 varloc = NOTE_VAR_LOCATION (loc_note);
14787 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14788 if (cur_descr == NULL)
14790 padsize += bitsize;
14791 continue;
14794 /* Check that cur_descr either doesn't use
14795 DW_OP_*piece operations, or their sum is equal
14796 to bitsize. Otherwise we can't embed it. */
14797 for (tail = &cur_descr; *tail != NULL;
14798 tail = &(*tail)->dw_loc_next)
14799 if ((*tail)->dw_loc_opc == DW_OP_piece)
14801 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14802 * BITS_PER_UNIT;
14803 last = *tail;
14805 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14807 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14808 last = *tail;
14811 if (last != NULL && opsize != bitsize)
14813 padsize += bitsize;
14814 /* Discard the current piece of the descriptor and release any
14815 addr_table entries it uses. */
14816 remove_loc_list_addr_table_entries (cur_descr);
14817 continue;
14820 /* If there is a hole, add DW_OP_*piece after empty DWARF
14821 expression, which means that those bits are optimized out. */
14822 if (padsize)
14824 if (padsize > decl_size)
14826 remove_loc_list_addr_table_entries (cur_descr);
14827 goto discard_descr;
14829 decl_size -= padsize;
14830 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14831 if (*descr_tail == NULL)
14833 remove_loc_list_addr_table_entries (cur_descr);
14834 goto discard_descr;
14836 descr_tail = &(*descr_tail)->dw_loc_next;
14837 padsize = 0;
14839 *descr_tail = cur_descr;
14840 descr_tail = tail;
14841 if (bitsize > decl_size)
14842 goto discard_descr;
14843 decl_size -= bitsize;
14844 if (last == NULL)
14846 HOST_WIDE_INT offset = 0;
14847 if (GET_CODE (varloc) == VAR_LOCATION
14848 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14850 varloc = PAT_VAR_LOCATION_LOC (varloc);
14851 if (GET_CODE (varloc) == EXPR_LIST)
14852 varloc = XEXP (varloc, 0);
14856 if (GET_CODE (varloc) == CONST
14857 || GET_CODE (varloc) == SIGN_EXTEND
14858 || GET_CODE (varloc) == ZERO_EXTEND)
14859 varloc = XEXP (varloc, 0);
14860 else if (GET_CODE (varloc) == SUBREG)
14861 varloc = SUBREG_REG (varloc);
14862 else
14863 break;
14865 while (1);
14866 /* DW_OP_bit_size offset should be zero for register
14867 or implicit location descriptions and empty location
14868 descriptions, but for memory addresses needs big endian
14869 adjustment. */
14870 if (MEM_P (varloc))
14872 unsigned HOST_WIDE_INT memsize
14873 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14874 if (memsize != bitsize)
14876 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14877 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14878 goto discard_descr;
14879 if (memsize < bitsize)
14880 goto discard_descr;
14881 if (BITS_BIG_ENDIAN)
14882 offset = memsize - bitsize;
14886 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14887 if (*descr_tail == NULL)
14888 goto discard_descr;
14889 descr_tail = &(*descr_tail)->dw_loc_next;
14893 /* If there were any non-empty expressions, add padding till the end of
14894 the decl. */
14895 if (descr != NULL && decl_size != 0)
14897 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14898 if (*descr_tail == NULL)
14899 goto discard_descr;
14901 return descr;
14903 discard_descr:
14904 /* Discard the descriptor and release any addr_table entries it uses. */
14905 remove_loc_list_addr_table_entries (descr);
14906 return NULL;
14909 /* Return the dwarf representation of the location list LOC_LIST of
14910 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14911 function. */
14913 static dw_loc_list_ref
14914 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14916 const char *endname, *secname;
14917 rtx varloc;
14918 enum var_init_status initialized;
14919 struct var_loc_node *node;
14920 dw_loc_descr_ref descr;
14921 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14922 dw_loc_list_ref list = NULL;
14923 dw_loc_list_ref *listp = &list;
14925 /* Now that we know what section we are using for a base,
14926 actually construct the list of locations.
14927 The first location information is what is passed to the
14928 function that creates the location list, and the remaining
14929 locations just get added on to that list.
14930 Note that we only know the start address for a location
14931 (IE location changes), so to build the range, we use
14932 the range [current location start, next location start].
14933 This means we have to special case the last node, and generate
14934 a range of [last location start, end of function label]. */
14936 secname = secname_for_decl (decl);
14938 for (node = loc_list->first; node; node = node->next)
14939 if (GET_CODE (node->loc) == EXPR_LIST
14940 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14942 if (GET_CODE (node->loc) == EXPR_LIST)
14944 /* This requires DW_OP_{,bit_}piece, which is not usable
14945 inside DWARF expressions. */
14946 if (want_address != 2)
14947 continue;
14948 descr = dw_sra_loc_expr (decl, node->loc);
14949 if (descr == NULL)
14950 continue;
14952 else
14954 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14955 varloc = NOTE_VAR_LOCATION (node->loc);
14956 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14958 if (descr)
14960 bool range_across_switch = false;
14961 /* If section switch happens in between node->label
14962 and node->next->label (or end of function) and
14963 we can't emit it as a single entry list,
14964 emit two ranges, first one ending at the end
14965 of first partition and second one starting at the
14966 beginning of second partition. */
14967 if (node == loc_list->last_before_switch
14968 && (node != loc_list->first || loc_list->first->next)
14969 && current_function_decl)
14971 endname = cfun->fde->dw_fde_end;
14972 range_across_switch = true;
14974 /* The variable has a location between NODE->LABEL and
14975 NODE->NEXT->LABEL. */
14976 else if (node->next)
14977 endname = node->next->label;
14978 /* If the variable has a location at the last label
14979 it keeps its location until the end of function. */
14980 else if (!current_function_decl)
14981 endname = text_end_label;
14982 else
14984 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14985 current_function_funcdef_no);
14986 endname = ggc_strdup (label_id);
14989 *listp = new_loc_list (descr, node->label, endname, secname);
14990 if (TREE_CODE (decl) == PARM_DECL
14991 && node == loc_list->first
14992 && NOTE_P (node->loc)
14993 && strcmp (node->label, endname) == 0)
14994 (*listp)->force = true;
14995 listp = &(*listp)->dw_loc_next;
14997 if (range_across_switch)
14999 if (GET_CODE (node->loc) == EXPR_LIST)
15000 descr = dw_sra_loc_expr (decl, node->loc);
15001 else
15003 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15004 varloc = NOTE_VAR_LOCATION (node->loc);
15005 descr = dw_loc_list_1 (decl, varloc, want_address,
15006 initialized);
15008 gcc_assert (descr);
15009 /* The variable has a location between NODE->LABEL and
15010 NODE->NEXT->LABEL. */
15011 if (node->next)
15012 endname = node->next->label;
15013 else
15014 endname = cfun->fde->dw_fde_second_end;
15015 *listp = new_loc_list (descr,
15016 cfun->fde->dw_fde_second_begin,
15017 endname, secname);
15018 listp = &(*listp)->dw_loc_next;
15023 /* Try to avoid the overhead of a location list emitting a location
15024 expression instead, but only if we didn't have more than one
15025 location entry in the first place. If some entries were not
15026 representable, we don't want to pretend a single entry that was
15027 applies to the entire scope in which the variable is
15028 available. */
15029 if (list && loc_list->first->next)
15030 gen_llsym (list);
15032 return list;
15035 /* Return if the loc_list has only single element and thus can be represented
15036 as location description. */
15038 static bool
15039 single_element_loc_list_p (dw_loc_list_ref list)
15041 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15042 return !list->ll_symbol;
15045 /* To each location in list LIST add loc descr REF. */
15047 static void
15048 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15050 dw_loc_descr_ref copy;
15051 add_loc_descr (&list->expr, ref);
15052 list = list->dw_loc_next;
15053 while (list)
15055 copy = ggc_alloc<dw_loc_descr_node> ();
15056 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15057 add_loc_descr (&list->expr, copy);
15058 while (copy->dw_loc_next)
15060 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15061 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15062 copy->dw_loc_next = new_copy;
15063 copy = new_copy;
15065 list = list->dw_loc_next;
15069 /* Given two lists RET and LIST
15070 produce location list that is result of adding expression in LIST
15071 to expression in RET on each position in program.
15072 Might be destructive on both RET and LIST.
15074 TODO: We handle only simple cases of RET or LIST having at most one
15075 element. General case would inolve sorting the lists in program order
15076 and merging them that will need some additional work.
15077 Adding that will improve quality of debug info especially for SRA-ed
15078 structures. */
15080 static void
15081 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15083 if (!list)
15084 return;
15085 if (!*ret)
15087 *ret = list;
15088 return;
15090 if (!list->dw_loc_next)
15092 add_loc_descr_to_each (*ret, list->expr);
15093 return;
15095 if (!(*ret)->dw_loc_next)
15097 add_loc_descr_to_each (list, (*ret)->expr);
15098 *ret = list;
15099 return;
15101 expansion_failed (NULL_TREE, NULL_RTX,
15102 "Don't know how to merge two non-trivial"
15103 " location lists.\n");
15104 *ret = NULL;
15105 return;
15108 /* LOC is constant expression. Try a luck, look it up in constant
15109 pool and return its loc_descr of its address. */
15111 static dw_loc_descr_ref
15112 cst_pool_loc_descr (tree loc)
15114 /* Get an RTL for this, if something has been emitted. */
15115 rtx rtl = lookup_constant_def (loc);
15117 if (!rtl || !MEM_P (rtl))
15119 gcc_assert (!rtl);
15120 return 0;
15122 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15124 /* TODO: We might get more coverage if we was actually delaying expansion
15125 of all expressions till end of compilation when constant pools are fully
15126 populated. */
15127 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15129 expansion_failed (loc, NULL_RTX,
15130 "CST value in contant pool but not marked.");
15131 return 0;
15133 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15134 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15137 /* Return dw_loc_list representing address of addr_expr LOC
15138 by looking for inner INDIRECT_REF expression and turning
15139 it into simple arithmetics.
15141 See loc_list_from_tree for the meaning of CONTEXT. */
15143 static dw_loc_list_ref
15144 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15145 const loc_descr_context *context)
15147 tree obj, offset;
15148 HOST_WIDE_INT bitsize, bitpos, bytepos;
15149 machine_mode mode;
15150 int unsignedp, reversep, volatilep = 0;
15151 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15153 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15154 &bitsize, &bitpos, &offset, &mode,
15155 &unsignedp, &reversep, &volatilep);
15156 STRIP_NOPS (obj);
15157 if (bitpos % BITS_PER_UNIT)
15159 expansion_failed (loc, NULL_RTX, "bitfield access");
15160 return 0;
15162 if (!INDIRECT_REF_P (obj))
15164 expansion_failed (obj,
15165 NULL_RTX, "no indirect ref in inner refrence");
15166 return 0;
15168 if (!offset && !bitpos)
15169 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15170 context);
15171 else if (toplev
15172 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15173 && (dwarf_version >= 4 || !dwarf_strict))
15175 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15176 if (!list_ret)
15177 return 0;
15178 if (offset)
15180 /* Variable offset. */
15181 list_ret1 = loc_list_from_tree (offset, 0, context);
15182 if (list_ret1 == 0)
15183 return 0;
15184 add_loc_list (&list_ret, list_ret1);
15185 if (!list_ret)
15186 return 0;
15187 add_loc_descr_to_each (list_ret,
15188 new_loc_descr (DW_OP_plus, 0, 0));
15190 bytepos = bitpos / BITS_PER_UNIT;
15191 if (bytepos > 0)
15192 add_loc_descr_to_each (list_ret,
15193 new_loc_descr (DW_OP_plus_uconst,
15194 bytepos, 0));
15195 else if (bytepos < 0)
15196 loc_list_plus_const (list_ret, bytepos);
15197 add_loc_descr_to_each (list_ret,
15198 new_loc_descr (DW_OP_stack_value, 0, 0));
15200 return list_ret;
15203 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15204 all operations from LOC are nops, move to the last one. Insert in NOPS all
15205 operations that are skipped. */
15207 static void
15208 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15209 hash_set<dw_loc_descr_ref> &nops)
15211 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15213 nops.add (loc);
15214 loc = loc->dw_loc_next;
15218 /* Helper for loc_descr_without_nops: free the location description operation
15219 P. */
15221 bool
15222 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15224 ggc_free (loc);
15225 return true;
15228 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15229 finishes LOC. */
15231 static void
15232 loc_descr_without_nops (dw_loc_descr_ref &loc)
15234 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15235 return;
15237 /* Set of all DW_OP_nop operations we remove. */
15238 hash_set<dw_loc_descr_ref> nops;
15240 /* First, strip all prefix NOP operations in order to keep the head of the
15241 operations list. */
15242 loc_descr_to_next_no_nop (loc, nops);
15244 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15246 /* For control flow operations: strip "prefix" nops in destination
15247 labels. */
15248 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15249 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15250 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15251 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15253 /* Do the same for the operations that follow, then move to the next
15254 iteration. */
15255 if (cur->dw_loc_next != NULL)
15256 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15257 cur = cur->dw_loc_next;
15260 nops.traverse<void *, free_loc_descr> (NULL);
15264 struct dwarf_procedure_info;
15266 /* Helper structure for location descriptions generation. */
15267 struct loc_descr_context
15269 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15270 NULL_TREE if DW_OP_push_object_address in invalid for this location
15271 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15272 tree context_type;
15273 /* The ..._DECL node that should be translated as a
15274 DW_OP_push_object_address operation. */
15275 tree base_decl;
15276 /* Information about the DWARF procedure we are currently generating. NULL if
15277 we are not generating a DWARF procedure. */
15278 struct dwarf_procedure_info *dpi;
15281 /* DWARF procedures generation
15283 DWARF expressions (aka. location descriptions) are used to encode variable
15284 things such as sizes or offsets. Such computations can have redundant parts
15285 that can be factorized in order to reduce the size of the output debug
15286 information. This is the whole point of DWARF procedures.
15288 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15289 already factorized into functions ("size functions") in order to handle very
15290 big and complex types. Such functions are quite simple: they have integral
15291 arguments, they return an integral result and their body contains only a
15292 return statement with arithmetic expressions. This is the only kind of
15293 function we are interested in translating into DWARF procedures, here.
15295 DWARF expressions and DWARF procedure are executed using a stack, so we have
15296 to define some calling convention for them to interact. Let's say that:
15298 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15299 all arguments in reverse order (right-to-left) so that when the DWARF
15300 procedure execution starts, the first argument is the top of the stack.
15302 - Then, when returning, the DWARF procedure must have consumed all arguments
15303 on the stack, must have pushed the result and touched nothing else.
15305 - Each integral argument and the result are integral types can be hold in a
15306 single stack slot.
15308 - We call "frame offset" the number of stack slots that are "under DWARF
15309 procedure control": it includes the arguments slots, the temporaries and
15310 the result slot. Thus, it is equal to the number of arguments when the
15311 procedure execution starts and must be equal to one (the result) when it
15312 returns. */
15314 /* Helper structure used when generating operations for a DWARF procedure. */
15315 struct dwarf_procedure_info
15317 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15318 currently translated. */
15319 tree fndecl;
15320 /* The number of arguments FNDECL takes. */
15321 unsigned args_count;
15324 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15325 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15326 equate it to this DIE. */
15328 static dw_die_ref
15329 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15330 dw_die_ref parent_die)
15332 dw_die_ref dwarf_proc_die;
15334 if ((dwarf_version < 3 && dwarf_strict)
15335 || location == NULL)
15336 return NULL;
15338 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15339 if (fndecl)
15340 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15341 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15342 return dwarf_proc_die;
15345 /* Return whether TYPE is a supported type as a DWARF procedure argument
15346 type or return type (we handle only scalar types and pointer types that
15347 aren't wider than the DWARF expression evaluation stack. */
15349 static bool
15350 is_handled_procedure_type (tree type)
15352 return ((INTEGRAL_TYPE_P (type)
15353 || TREE_CODE (type) == OFFSET_TYPE
15354 || TREE_CODE (type) == POINTER_TYPE)
15355 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15358 /* Helper for resolve_args_picking: do the same but stop when coming across
15359 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15360 offset *before* evaluating the corresponding operation. */
15362 static bool
15363 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15364 struct dwarf_procedure_info *dpi,
15365 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15367 /* The "frame_offset" identifier is already used to name a macro... */
15368 unsigned frame_offset_ = initial_frame_offset;
15369 dw_loc_descr_ref l;
15371 for (l = loc; l != NULL;)
15373 bool existed;
15374 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15376 /* If we already met this node, there is nothing to compute anymore. */
15377 if (existed)
15379 /* Make sure that the stack size is consistent wherever the execution
15380 flow comes from. */
15381 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15382 break;
15384 l_frame_offset = frame_offset_;
15386 /* If needed, relocate the picking offset with respect to the frame
15387 offset. */
15388 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15390 /* frame_offset_ is the size of the current stack frame, including
15391 incoming arguments. Besides, the arguments are pushed
15392 right-to-left. Thus, in order to access the Nth argument from
15393 this operation node, the picking has to skip temporaries *plus*
15394 one stack slot per argument (0 for the first one, 1 for the second
15395 one, etc.).
15397 The targetted argument number (N) is already set as the operand,
15398 and the number of temporaries can be computed with:
15399 frame_offsets_ - dpi->args_count */
15400 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15402 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15403 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15404 return false;
15407 /* Update frame_offset according to the effect the current operation has
15408 on the stack. */
15409 switch (l->dw_loc_opc)
15411 case DW_OP_deref:
15412 case DW_OP_swap:
15413 case DW_OP_rot:
15414 case DW_OP_abs:
15415 case DW_OP_neg:
15416 case DW_OP_not:
15417 case DW_OP_plus_uconst:
15418 case DW_OP_skip:
15419 case DW_OP_reg0:
15420 case DW_OP_reg1:
15421 case DW_OP_reg2:
15422 case DW_OP_reg3:
15423 case DW_OP_reg4:
15424 case DW_OP_reg5:
15425 case DW_OP_reg6:
15426 case DW_OP_reg7:
15427 case DW_OP_reg8:
15428 case DW_OP_reg9:
15429 case DW_OP_reg10:
15430 case DW_OP_reg11:
15431 case DW_OP_reg12:
15432 case DW_OP_reg13:
15433 case DW_OP_reg14:
15434 case DW_OP_reg15:
15435 case DW_OP_reg16:
15436 case DW_OP_reg17:
15437 case DW_OP_reg18:
15438 case DW_OP_reg19:
15439 case DW_OP_reg20:
15440 case DW_OP_reg21:
15441 case DW_OP_reg22:
15442 case DW_OP_reg23:
15443 case DW_OP_reg24:
15444 case DW_OP_reg25:
15445 case DW_OP_reg26:
15446 case DW_OP_reg27:
15447 case DW_OP_reg28:
15448 case DW_OP_reg29:
15449 case DW_OP_reg30:
15450 case DW_OP_reg31:
15451 case DW_OP_bregx:
15452 case DW_OP_piece:
15453 case DW_OP_deref_size:
15454 case DW_OP_nop:
15455 case DW_OP_form_tls_address:
15456 case DW_OP_bit_piece:
15457 case DW_OP_implicit_value:
15458 case DW_OP_stack_value:
15459 break;
15461 case DW_OP_addr:
15462 case DW_OP_const1u:
15463 case DW_OP_const1s:
15464 case DW_OP_const2u:
15465 case DW_OP_const2s:
15466 case DW_OP_const4u:
15467 case DW_OP_const4s:
15468 case DW_OP_const8u:
15469 case DW_OP_const8s:
15470 case DW_OP_constu:
15471 case DW_OP_consts:
15472 case DW_OP_dup:
15473 case DW_OP_over:
15474 case DW_OP_pick:
15475 case DW_OP_lit0:
15476 case DW_OP_lit1:
15477 case DW_OP_lit2:
15478 case DW_OP_lit3:
15479 case DW_OP_lit4:
15480 case DW_OP_lit5:
15481 case DW_OP_lit6:
15482 case DW_OP_lit7:
15483 case DW_OP_lit8:
15484 case DW_OP_lit9:
15485 case DW_OP_lit10:
15486 case DW_OP_lit11:
15487 case DW_OP_lit12:
15488 case DW_OP_lit13:
15489 case DW_OP_lit14:
15490 case DW_OP_lit15:
15491 case DW_OP_lit16:
15492 case DW_OP_lit17:
15493 case DW_OP_lit18:
15494 case DW_OP_lit19:
15495 case DW_OP_lit20:
15496 case DW_OP_lit21:
15497 case DW_OP_lit22:
15498 case DW_OP_lit23:
15499 case DW_OP_lit24:
15500 case DW_OP_lit25:
15501 case DW_OP_lit26:
15502 case DW_OP_lit27:
15503 case DW_OP_lit28:
15504 case DW_OP_lit29:
15505 case DW_OP_lit30:
15506 case DW_OP_lit31:
15507 case DW_OP_breg0:
15508 case DW_OP_breg1:
15509 case DW_OP_breg2:
15510 case DW_OP_breg3:
15511 case DW_OP_breg4:
15512 case DW_OP_breg5:
15513 case DW_OP_breg6:
15514 case DW_OP_breg7:
15515 case DW_OP_breg8:
15516 case DW_OP_breg9:
15517 case DW_OP_breg10:
15518 case DW_OP_breg11:
15519 case DW_OP_breg12:
15520 case DW_OP_breg13:
15521 case DW_OP_breg14:
15522 case DW_OP_breg15:
15523 case DW_OP_breg16:
15524 case DW_OP_breg17:
15525 case DW_OP_breg18:
15526 case DW_OP_breg19:
15527 case DW_OP_breg20:
15528 case DW_OP_breg21:
15529 case DW_OP_breg22:
15530 case DW_OP_breg23:
15531 case DW_OP_breg24:
15532 case DW_OP_breg25:
15533 case DW_OP_breg26:
15534 case DW_OP_breg27:
15535 case DW_OP_breg28:
15536 case DW_OP_breg29:
15537 case DW_OP_breg30:
15538 case DW_OP_breg31:
15539 case DW_OP_fbreg:
15540 case DW_OP_push_object_address:
15541 case DW_OP_call_frame_cfa:
15542 ++frame_offset_;
15543 break;
15545 case DW_OP_drop:
15546 case DW_OP_xderef:
15547 case DW_OP_and:
15548 case DW_OP_div:
15549 case DW_OP_minus:
15550 case DW_OP_mod:
15551 case DW_OP_mul:
15552 case DW_OP_or:
15553 case DW_OP_plus:
15554 case DW_OP_shl:
15555 case DW_OP_shr:
15556 case DW_OP_shra:
15557 case DW_OP_xor:
15558 case DW_OP_bra:
15559 case DW_OP_eq:
15560 case DW_OP_ge:
15561 case DW_OP_gt:
15562 case DW_OP_le:
15563 case DW_OP_lt:
15564 case DW_OP_ne:
15565 case DW_OP_regx:
15566 case DW_OP_xderef_size:
15567 --frame_offset_;
15568 break;
15570 case DW_OP_call2:
15571 case DW_OP_call4:
15572 case DW_OP_call_ref:
15574 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15575 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15577 if (stack_usage == NULL)
15578 return false;
15579 frame_offset_ += *stack_usage;
15580 break;
15583 case DW_OP_GNU_push_tls_address:
15584 case DW_OP_GNU_uninit:
15585 case DW_OP_GNU_encoded_addr:
15586 case DW_OP_GNU_implicit_pointer:
15587 case DW_OP_GNU_entry_value:
15588 case DW_OP_GNU_const_type:
15589 case DW_OP_GNU_regval_type:
15590 case DW_OP_GNU_deref_type:
15591 case DW_OP_GNU_convert:
15592 case DW_OP_GNU_reinterpret:
15593 case DW_OP_GNU_parameter_ref:
15594 /* loc_list_from_tree will probably not output these operations for
15595 size functions, so assume they will not appear here. */
15596 /* Fall through... */
15598 default:
15599 gcc_unreachable ();
15602 /* Now, follow the control flow (except subroutine calls). */
15603 switch (l->dw_loc_opc)
15605 case DW_OP_bra:
15606 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15607 frame_offsets))
15608 return false;
15609 /* Fall through. */
15611 case DW_OP_skip:
15612 l = l->dw_loc_oprnd1.v.val_loc;
15613 break;
15615 case DW_OP_stack_value:
15616 return true;
15618 default:
15619 l = l->dw_loc_next;
15620 break;
15624 return true;
15627 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15628 operations) in order to resolve the operand of DW_OP_pick operations that
15629 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15630 offset *before* LOC is executed. Return if all relocations were
15631 successful. */
15633 static bool
15634 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15635 struct dwarf_procedure_info *dpi)
15637 /* Associate to all visited operations the frame offset *before* evaluating
15638 this operation. */
15639 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15641 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15642 frame_offsets);
15645 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15646 Return NULL if it is not possible. */
15648 static dw_die_ref
15649 function_to_dwarf_procedure (tree fndecl)
15651 struct loc_descr_context ctx;
15652 struct dwarf_procedure_info dpi;
15653 dw_die_ref dwarf_proc_die;
15654 tree tree_body = DECL_SAVED_TREE (fndecl);
15655 dw_loc_descr_ref loc_body, epilogue;
15657 tree cursor;
15658 unsigned i;
15660 /* Do not generate multiple DWARF procedures for the same function
15661 declaration. */
15662 dwarf_proc_die = lookup_decl_die (fndecl);
15663 if (dwarf_proc_die != NULL)
15664 return dwarf_proc_die;
15666 /* DWARF procedures are available starting with the DWARFv3 standard. */
15667 if (dwarf_version < 3 && dwarf_strict)
15668 return NULL;
15670 /* We handle only functions for which we still have a body, that return a
15671 supported type and that takes arguments with supported types. Note that
15672 there is no point translating functions that return nothing. */
15673 if (tree_body == NULL_TREE
15674 || DECL_RESULT (fndecl) == NULL_TREE
15675 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15676 return NULL;
15678 for (cursor = DECL_ARGUMENTS (fndecl);
15679 cursor != NULL_TREE;
15680 cursor = TREE_CHAIN (cursor))
15681 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15682 return NULL;
15684 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15685 if (TREE_CODE (tree_body) != RETURN_EXPR)
15686 return NULL;
15687 tree_body = TREE_OPERAND (tree_body, 0);
15688 if (TREE_CODE (tree_body) != MODIFY_EXPR
15689 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15690 return NULL;
15691 tree_body = TREE_OPERAND (tree_body, 1);
15693 /* Try to translate the body expression itself. Note that this will probably
15694 cause an infinite recursion if its call graph has a cycle. This is very
15695 unlikely for size functions, however, so don't bother with such things at
15696 the moment. */
15697 ctx.context_type = NULL_TREE;
15698 ctx.base_decl = NULL_TREE;
15699 ctx.dpi = &dpi;
15700 dpi.fndecl = fndecl;
15701 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15702 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15703 if (!loc_body)
15704 return NULL;
15706 /* After evaluating all operands in "loc_body", we should still have on the
15707 stack all arguments plus the desired function result (top of the stack).
15708 Generate code in order to keep only the result in our stack frame. */
15709 epilogue = NULL;
15710 for (i = 0; i < dpi.args_count; ++i)
15712 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15713 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15714 op_couple->dw_loc_next->dw_loc_next = epilogue;
15715 epilogue = op_couple;
15717 add_loc_descr (&loc_body, epilogue);
15718 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15719 return NULL;
15721 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15722 because they are considered useful. Now there is an epilogue, they are
15723 not anymore, so give it another try. */
15724 loc_descr_without_nops (loc_body);
15726 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15727 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15728 though, given that size functions do not come from source, so they should
15729 not have a dedicated DW_TAG_subprogram DIE. */
15730 dwarf_proc_die
15731 = new_dwarf_proc_die (loc_body, fndecl,
15732 get_context_die (DECL_CONTEXT (fndecl)));
15734 /* The called DWARF procedure consumes one stack slot per argument and
15735 returns one stack slot. */
15736 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15738 return dwarf_proc_die;
15742 /* Generate Dwarf location list representing LOC.
15743 If WANT_ADDRESS is false, expression computing LOC will be computed
15744 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15745 if WANT_ADDRESS is 2, expression computing address useable in location
15746 will be returned (i.e. DW_OP_reg can be used
15747 to refer to register values).
15749 CONTEXT provides information to customize the location descriptions
15750 generation. Its context_type field specifies what type is implicitly
15751 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15752 will not be generated.
15754 Its DPI field determines whether we are generating a DWARF expression for a
15755 DWARF procedure, so PARM_DECL references are processed specifically.
15757 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15758 and dpi fields were null. */
15760 static dw_loc_list_ref
15761 loc_list_from_tree_1 (tree loc, int want_address,
15762 const struct loc_descr_context *context)
15764 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15765 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15766 int have_address = 0;
15767 enum dwarf_location_atom op;
15769 /* ??? Most of the time we do not take proper care for sign/zero
15770 extending the values properly. Hopefully this won't be a real
15771 problem... */
15773 if (context != NULL
15774 && context->base_decl == loc
15775 && want_address == 0)
15777 if (dwarf_version >= 3 || !dwarf_strict)
15778 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15779 NULL, NULL, NULL);
15780 else
15781 return NULL;
15784 switch (TREE_CODE (loc))
15786 case ERROR_MARK:
15787 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15788 return 0;
15790 case PLACEHOLDER_EXPR:
15791 /* This case involves extracting fields from an object to determine the
15792 position of other fields. It is supposed to appear only as the first
15793 operand of COMPONENT_REF nodes and to reference precisely the type
15794 that the context allows. */
15795 if (context != NULL
15796 && TREE_TYPE (loc) == context->context_type
15797 && want_address >= 1)
15799 if (dwarf_version >= 3 || !dwarf_strict)
15801 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15802 have_address = 1;
15803 break;
15805 else
15806 return NULL;
15808 else
15809 expansion_failed (loc, NULL_RTX,
15810 "PLACEHOLDER_EXPR for an unexpected type");
15811 break;
15813 case CALL_EXPR:
15815 const int nargs = call_expr_nargs (loc);
15816 tree callee = get_callee_fndecl (loc);
15817 int i;
15818 dw_die_ref dwarf_proc;
15820 if (callee == NULL_TREE)
15821 goto call_expansion_failed;
15823 /* We handle only functions that return an integer. */
15824 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15825 goto call_expansion_failed;
15827 dwarf_proc = function_to_dwarf_procedure (callee);
15828 if (dwarf_proc == NULL)
15829 goto call_expansion_failed;
15831 /* Evaluate arguments right-to-left so that the first argument will
15832 be the top-most one on the stack. */
15833 for (i = nargs - 1; i >= 0; --i)
15835 dw_loc_descr_ref loc_descr
15836 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15837 context);
15839 if (loc_descr == NULL)
15840 goto call_expansion_failed;
15842 add_loc_descr (&ret, loc_descr);
15845 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15846 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15847 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15848 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15849 add_loc_descr (&ret, ret1);
15850 break;
15852 call_expansion_failed:
15853 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15854 /* There are no opcodes for these operations. */
15855 return 0;
15858 case PREINCREMENT_EXPR:
15859 case PREDECREMENT_EXPR:
15860 case POSTINCREMENT_EXPR:
15861 case POSTDECREMENT_EXPR:
15862 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15863 /* There are no opcodes for these operations. */
15864 return 0;
15866 case ADDR_EXPR:
15867 /* If we already want an address, see if there is INDIRECT_REF inside
15868 e.g. for &this->field. */
15869 if (want_address)
15871 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15872 (loc, want_address == 2, context);
15873 if (list_ret)
15874 have_address = 1;
15875 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15876 && (ret = cst_pool_loc_descr (loc)))
15877 have_address = 1;
15879 /* Otherwise, process the argument and look for the address. */
15880 if (!list_ret && !ret)
15881 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15882 else
15884 if (want_address)
15885 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15886 return NULL;
15888 break;
15890 case VAR_DECL:
15891 if (DECL_THREAD_LOCAL_P (loc))
15893 rtx rtl;
15894 enum dwarf_location_atom tls_op;
15895 enum dtprel_bool dtprel = dtprel_false;
15897 if (targetm.have_tls)
15899 /* If this is not defined, we have no way to emit the
15900 data. */
15901 if (!targetm.asm_out.output_dwarf_dtprel)
15902 return 0;
15904 /* The way DW_OP_GNU_push_tls_address is specified, we
15905 can only look up addresses of objects in the current
15906 module. We used DW_OP_addr as first op, but that's
15907 wrong, because DW_OP_addr is relocated by the debug
15908 info consumer, while DW_OP_GNU_push_tls_address
15909 operand shouldn't be. */
15910 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15911 return 0;
15912 dtprel = dtprel_true;
15913 tls_op = DW_OP_GNU_push_tls_address;
15915 else
15917 if (!targetm.emutls.debug_form_tls_address
15918 || !(dwarf_version >= 3 || !dwarf_strict))
15919 return 0;
15920 /* We stuffed the control variable into the DECL_VALUE_EXPR
15921 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15922 no longer appear in gimple code. We used the control
15923 variable in specific so that we could pick it up here. */
15924 loc = DECL_VALUE_EXPR (loc);
15925 tls_op = DW_OP_form_tls_address;
15928 rtl = rtl_for_decl_location (loc);
15929 if (rtl == NULL_RTX)
15930 return 0;
15932 if (!MEM_P (rtl))
15933 return 0;
15934 rtl = XEXP (rtl, 0);
15935 if (! CONSTANT_P (rtl))
15936 return 0;
15938 ret = new_addr_loc_descr (rtl, dtprel);
15939 ret1 = new_loc_descr (tls_op, 0, 0);
15940 add_loc_descr (&ret, ret1);
15942 have_address = 1;
15943 break;
15945 /* FALLTHRU */
15947 case PARM_DECL:
15948 if (context != NULL && context->dpi != NULL
15949 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15951 /* We are generating code for a DWARF procedure and we want to access
15952 one of its arguments: find the appropriate argument offset and let
15953 the resolve_args_picking pass compute the offset that complies
15954 with the stack frame size. */
15955 unsigned i = 0;
15956 tree cursor;
15958 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15959 cursor != NULL_TREE && cursor != loc;
15960 cursor = TREE_CHAIN (cursor), ++i)
15962 /* If we are translating a DWARF procedure, all referenced parameters
15963 must belong to the current function. */
15964 gcc_assert (cursor != NULL_TREE);
15966 ret = new_loc_descr (DW_OP_pick, i, 0);
15967 ret->frame_offset_rel = 1;
15968 break;
15970 /* FALLTHRU */
15972 case RESULT_DECL:
15973 if (DECL_HAS_VALUE_EXPR_P (loc))
15974 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15975 want_address, context);
15976 /* FALLTHRU */
15978 case FUNCTION_DECL:
15980 rtx rtl;
15981 var_loc_list *loc_list = lookup_decl_loc (loc);
15983 if (loc_list && loc_list->first)
15985 list_ret = dw_loc_list (loc_list, loc, want_address);
15986 have_address = want_address != 0;
15987 break;
15989 rtl = rtl_for_decl_location (loc);
15990 if (rtl == NULL_RTX)
15992 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15993 return 0;
15995 else if (CONST_INT_P (rtl))
15997 HOST_WIDE_INT val = INTVAL (rtl);
15998 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15999 val &= GET_MODE_MASK (DECL_MODE (loc));
16000 ret = int_loc_descriptor (val);
16002 else if (GET_CODE (rtl) == CONST_STRING)
16004 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16005 return 0;
16007 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16008 ret = new_addr_loc_descr (rtl, dtprel_false);
16009 else
16011 machine_mode mode, mem_mode;
16013 /* Certain constructs can only be represented at top-level. */
16014 if (want_address == 2)
16016 ret = loc_descriptor (rtl, VOIDmode,
16017 VAR_INIT_STATUS_INITIALIZED);
16018 have_address = 1;
16020 else
16022 mode = GET_MODE (rtl);
16023 mem_mode = VOIDmode;
16024 if (MEM_P (rtl))
16026 mem_mode = mode;
16027 mode = get_address_mode (rtl);
16028 rtl = XEXP (rtl, 0);
16029 have_address = 1;
16031 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16032 VAR_INIT_STATUS_INITIALIZED);
16034 if (!ret)
16035 expansion_failed (loc, rtl,
16036 "failed to produce loc descriptor for rtl");
16039 break;
16041 case MEM_REF:
16042 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16044 have_address = 1;
16045 goto do_plus;
16047 /* Fallthru. */
16048 case INDIRECT_REF:
16049 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16050 have_address = 1;
16051 break;
16053 case TARGET_MEM_REF:
16054 case SSA_NAME:
16055 case DEBUG_EXPR_DECL:
16056 return NULL;
16058 case COMPOUND_EXPR:
16059 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16060 context);
16062 CASE_CONVERT:
16063 case VIEW_CONVERT_EXPR:
16064 case SAVE_EXPR:
16065 case MODIFY_EXPR:
16066 case NON_LVALUE_EXPR:
16067 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16068 context);
16070 case COMPONENT_REF:
16071 case BIT_FIELD_REF:
16072 case ARRAY_REF:
16073 case ARRAY_RANGE_REF:
16074 case REALPART_EXPR:
16075 case IMAGPART_EXPR:
16077 tree obj, offset;
16078 HOST_WIDE_INT bitsize, bitpos, bytepos;
16079 machine_mode mode;
16080 int unsignedp, reversep, volatilep = 0;
16082 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16083 &unsignedp, &reversep, &volatilep);
16085 gcc_assert (obj != loc);
16087 list_ret = loc_list_from_tree_1 (obj,
16088 want_address == 2
16089 && !bitpos && !offset ? 2 : 1,
16090 context);
16091 /* TODO: We can extract value of the small expression via shifting even
16092 for nonzero bitpos. */
16093 if (list_ret == 0)
16094 return 0;
16095 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16097 expansion_failed (loc, NULL_RTX,
16098 "bitfield access");
16099 return 0;
16102 if (offset != NULL_TREE)
16104 /* Variable offset. */
16105 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16106 if (list_ret1 == 0)
16107 return 0;
16108 add_loc_list (&list_ret, list_ret1);
16109 if (!list_ret)
16110 return 0;
16111 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16114 bytepos = bitpos / BITS_PER_UNIT;
16115 if (bytepos > 0)
16116 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16117 else if (bytepos < 0)
16118 loc_list_plus_const (list_ret, bytepos);
16120 have_address = 1;
16121 break;
16124 case INTEGER_CST:
16125 if ((want_address || !tree_fits_shwi_p (loc))
16126 && (ret = cst_pool_loc_descr (loc)))
16127 have_address = 1;
16128 else if (want_address == 2
16129 && tree_fits_shwi_p (loc)
16130 && (ret = address_of_int_loc_descriptor
16131 (int_size_in_bytes (TREE_TYPE (loc)),
16132 tree_to_shwi (loc))))
16133 have_address = 1;
16134 else if (tree_fits_shwi_p (loc))
16135 ret = int_loc_descriptor (tree_to_shwi (loc));
16136 else if (tree_fits_uhwi_p (loc))
16137 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16138 else
16140 expansion_failed (loc, NULL_RTX,
16141 "Integer operand is not host integer");
16142 return 0;
16144 break;
16146 case CONSTRUCTOR:
16147 case REAL_CST:
16148 case STRING_CST:
16149 case COMPLEX_CST:
16150 if ((ret = cst_pool_loc_descr (loc)))
16151 have_address = 1;
16152 else if (TREE_CODE (loc) == CONSTRUCTOR)
16154 tree type = TREE_TYPE (loc);
16155 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16156 unsigned HOST_WIDE_INT offset = 0;
16157 unsigned HOST_WIDE_INT cnt;
16158 constructor_elt *ce;
16160 if (TREE_CODE (type) == RECORD_TYPE)
16162 /* This is very limited, but it's enough to output
16163 pointers to member functions, as long as the
16164 referenced function is defined in the current
16165 translation unit. */
16166 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16168 tree val = ce->value;
16170 tree field = ce->index;
16172 if (val)
16173 STRIP_NOPS (val);
16175 if (!field || DECL_BIT_FIELD (field))
16177 expansion_failed (loc, NULL_RTX,
16178 "bitfield in record type constructor");
16179 size = offset = (unsigned HOST_WIDE_INT)-1;
16180 ret = NULL;
16181 break;
16184 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16185 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16186 gcc_assert (pos + fieldsize <= size);
16187 if (pos < offset)
16189 expansion_failed (loc, NULL_RTX,
16190 "out-of-order fields in record constructor");
16191 size = offset = (unsigned HOST_WIDE_INT)-1;
16192 ret = NULL;
16193 break;
16195 if (pos > offset)
16197 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16198 add_loc_descr (&ret, ret1);
16199 offset = pos;
16201 if (val && fieldsize != 0)
16203 ret1 = loc_descriptor_from_tree (val, want_address, context);
16204 if (!ret1)
16206 expansion_failed (loc, NULL_RTX,
16207 "unsupported expression in field");
16208 size = offset = (unsigned HOST_WIDE_INT)-1;
16209 ret = NULL;
16210 break;
16212 add_loc_descr (&ret, ret1);
16214 if (fieldsize)
16216 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16217 add_loc_descr (&ret, ret1);
16218 offset = pos + fieldsize;
16222 if (offset != size)
16224 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16225 add_loc_descr (&ret, ret1);
16226 offset = size;
16229 have_address = !!want_address;
16231 else
16232 expansion_failed (loc, NULL_RTX,
16233 "constructor of non-record type");
16235 else
16236 /* We can construct small constants here using int_loc_descriptor. */
16237 expansion_failed (loc, NULL_RTX,
16238 "constructor or constant not in constant pool");
16239 break;
16241 case TRUTH_AND_EXPR:
16242 case TRUTH_ANDIF_EXPR:
16243 case BIT_AND_EXPR:
16244 op = DW_OP_and;
16245 goto do_binop;
16247 case TRUTH_XOR_EXPR:
16248 case BIT_XOR_EXPR:
16249 op = DW_OP_xor;
16250 goto do_binop;
16252 case TRUTH_OR_EXPR:
16253 case TRUTH_ORIF_EXPR:
16254 case BIT_IOR_EXPR:
16255 op = DW_OP_or;
16256 goto do_binop;
16258 case FLOOR_DIV_EXPR:
16259 case CEIL_DIV_EXPR:
16260 case ROUND_DIV_EXPR:
16261 case TRUNC_DIV_EXPR:
16262 case EXACT_DIV_EXPR:
16263 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16264 return 0;
16265 op = DW_OP_div;
16266 goto do_binop;
16268 case MINUS_EXPR:
16269 op = DW_OP_minus;
16270 goto do_binop;
16272 case FLOOR_MOD_EXPR:
16273 case CEIL_MOD_EXPR:
16274 case ROUND_MOD_EXPR:
16275 case TRUNC_MOD_EXPR:
16276 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16278 op = DW_OP_mod;
16279 goto do_binop;
16281 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16282 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16283 if (list_ret == 0 || list_ret1 == 0)
16284 return 0;
16286 add_loc_list (&list_ret, list_ret1);
16287 if (list_ret == 0)
16288 return 0;
16289 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16290 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16291 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16292 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16293 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16294 break;
16296 case MULT_EXPR:
16297 op = DW_OP_mul;
16298 goto do_binop;
16300 case LSHIFT_EXPR:
16301 op = DW_OP_shl;
16302 goto do_binop;
16304 case RSHIFT_EXPR:
16305 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16306 goto do_binop;
16308 case POINTER_PLUS_EXPR:
16309 case PLUS_EXPR:
16310 do_plus:
16311 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16313 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16314 smarter to encode their opposite. The DW_OP_plus_uconst operation
16315 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16316 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16317 bytes, Y being the size of the operation that pushes the opposite
16318 of the addend. So let's choose the smallest representation. */
16319 const tree tree_addend = TREE_OPERAND (loc, 1);
16320 offset_int wi_addend;
16321 HOST_WIDE_INT shwi_addend;
16322 dw_loc_descr_ref loc_naddend;
16324 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16325 if (list_ret == 0)
16326 return 0;
16328 /* Try to get the literal to push. It is the opposite of the addend,
16329 so as we rely on wrapping during DWARF evaluation, first decode
16330 the literal as a "DWARF-sized" signed number. */
16331 wi_addend = wi::to_offset (tree_addend);
16332 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16333 shwi_addend = wi_addend.to_shwi ();
16334 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16335 ? int_loc_descriptor (-shwi_addend)
16336 : NULL;
16338 if (loc_naddend != NULL
16339 && ((unsigned) size_of_uleb128 (shwi_addend)
16340 > size_of_loc_descr (loc_naddend)))
16342 add_loc_descr_to_each (list_ret, loc_naddend);
16343 add_loc_descr_to_each (list_ret,
16344 new_loc_descr (DW_OP_minus, 0, 0));
16346 else
16348 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16350 loc_naddend = loc_cur;
16351 loc_cur = loc_cur->dw_loc_next;
16352 ggc_free (loc_naddend);
16354 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16356 break;
16359 op = DW_OP_plus;
16360 goto do_binop;
16362 case LE_EXPR:
16363 op = DW_OP_le;
16364 goto do_comp_binop;
16366 case GE_EXPR:
16367 op = DW_OP_ge;
16368 goto do_comp_binop;
16370 case LT_EXPR:
16371 op = DW_OP_lt;
16372 goto do_comp_binop;
16374 case GT_EXPR:
16375 op = DW_OP_gt;
16376 goto do_comp_binop;
16378 do_comp_binop:
16379 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16381 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16382 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16383 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16384 TREE_CODE (loc));
16385 break;
16387 else
16388 goto do_binop;
16390 case EQ_EXPR:
16391 op = DW_OP_eq;
16392 goto do_binop;
16394 case NE_EXPR:
16395 op = DW_OP_ne;
16396 goto do_binop;
16398 do_binop:
16399 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16400 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16401 if (list_ret == 0 || list_ret1 == 0)
16402 return 0;
16404 add_loc_list (&list_ret, list_ret1);
16405 if (list_ret == 0)
16406 return 0;
16407 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16408 break;
16410 case TRUTH_NOT_EXPR:
16411 case BIT_NOT_EXPR:
16412 op = DW_OP_not;
16413 goto do_unop;
16415 case ABS_EXPR:
16416 op = DW_OP_abs;
16417 goto do_unop;
16419 case NEGATE_EXPR:
16420 op = DW_OP_neg;
16421 goto do_unop;
16423 do_unop:
16424 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16425 if (list_ret == 0)
16426 return 0;
16428 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16429 break;
16431 case MIN_EXPR:
16432 case MAX_EXPR:
16434 const enum tree_code code =
16435 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16437 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16438 build2 (code, integer_type_node,
16439 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16440 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16443 /* fall through */
16445 case COND_EXPR:
16447 dw_loc_descr_ref lhs
16448 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16449 dw_loc_list_ref rhs
16450 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16451 dw_loc_descr_ref bra_node, jump_node, tmp;
16453 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16454 if (list_ret == 0 || lhs == 0 || rhs == 0)
16455 return 0;
16457 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16458 add_loc_descr_to_each (list_ret, bra_node);
16460 add_loc_list (&list_ret, rhs);
16461 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16462 add_loc_descr_to_each (list_ret, jump_node);
16464 add_loc_descr_to_each (list_ret, lhs);
16465 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16466 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16468 /* ??? Need a node to point the skip at. Use a nop. */
16469 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16470 add_loc_descr_to_each (list_ret, tmp);
16471 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16472 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16474 break;
16476 case FIX_TRUNC_EXPR:
16477 return 0;
16479 default:
16480 /* Leave front-end specific codes as simply unknown. This comes
16481 up, for instance, with the C STMT_EXPR. */
16482 if ((unsigned int) TREE_CODE (loc)
16483 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16485 expansion_failed (loc, NULL_RTX,
16486 "language specific tree node");
16487 return 0;
16490 /* Otherwise this is a generic code; we should just lists all of
16491 these explicitly. We forgot one. */
16492 if (flag_checking)
16493 gcc_unreachable ();
16495 /* In a release build, we want to degrade gracefully: better to
16496 generate incomplete debugging information than to crash. */
16497 return NULL;
16500 if (!ret && !list_ret)
16501 return 0;
16503 if (want_address == 2 && !have_address
16504 && (dwarf_version >= 4 || !dwarf_strict))
16506 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16508 expansion_failed (loc, NULL_RTX,
16509 "DWARF address size mismatch");
16510 return 0;
16512 if (ret)
16513 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16514 else
16515 add_loc_descr_to_each (list_ret,
16516 new_loc_descr (DW_OP_stack_value, 0, 0));
16517 have_address = 1;
16519 /* Show if we can't fill the request for an address. */
16520 if (want_address && !have_address)
16522 expansion_failed (loc, NULL_RTX,
16523 "Want address and only have value");
16524 return 0;
16527 gcc_assert (!ret || !list_ret);
16529 /* If we've got an address and don't want one, dereference. */
16530 if (!want_address && have_address)
16532 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16534 if (size > DWARF2_ADDR_SIZE || size == -1)
16536 expansion_failed (loc, NULL_RTX,
16537 "DWARF address size mismatch");
16538 return 0;
16540 else if (size == DWARF2_ADDR_SIZE)
16541 op = DW_OP_deref;
16542 else
16543 op = DW_OP_deref_size;
16545 if (ret)
16546 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16547 else
16548 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16550 if (ret)
16551 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16553 return list_ret;
16556 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16557 expressions. */
16559 static dw_loc_list_ref
16560 loc_list_from_tree (tree loc, int want_address,
16561 const struct loc_descr_context *context)
16563 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16565 for (dw_loc_list_ref loc_cur = result;
16566 loc_cur != NULL; loc_cur =
16567 loc_cur->dw_loc_next)
16568 loc_descr_without_nops (loc_cur->expr);
16569 return result;
16572 /* Same as above but return only single location expression. */
16573 static dw_loc_descr_ref
16574 loc_descriptor_from_tree (tree loc, int want_address,
16575 const struct loc_descr_context *context)
16577 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16578 if (!ret)
16579 return NULL;
16580 if (ret->dw_loc_next)
16582 expansion_failed (loc, NULL_RTX,
16583 "Location list where only loc descriptor needed");
16584 return NULL;
16586 return ret->expr;
16589 /* Given a value, round it up to the lowest multiple of `boundary'
16590 which is not less than the value itself. */
16592 static inline HOST_WIDE_INT
16593 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16595 return (((value + boundary - 1) / boundary) * boundary);
16598 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16599 pointer to the declared type for the relevant field variable, or return
16600 `integer_type_node' if the given node turns out to be an
16601 ERROR_MARK node. */
16603 static inline tree
16604 field_type (const_tree decl)
16606 tree type;
16608 if (TREE_CODE (decl) == ERROR_MARK)
16609 return integer_type_node;
16611 type = DECL_BIT_FIELD_TYPE (decl);
16612 if (type == NULL_TREE)
16613 type = TREE_TYPE (decl);
16615 return type;
16618 /* Given a pointer to a tree node, return the alignment in bits for
16619 it, or else return BITS_PER_WORD if the node actually turns out to
16620 be an ERROR_MARK node. */
16622 static inline unsigned
16623 simple_type_align_in_bits (const_tree type)
16625 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16628 static inline unsigned
16629 simple_decl_align_in_bits (const_tree decl)
16631 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16634 /* Return the result of rounding T up to ALIGN. */
16636 static inline offset_int
16637 round_up_to_align (const offset_int &t, unsigned int align)
16639 return wi::udiv_trunc (t + align - 1, align) * align;
16642 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16643 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16644 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16645 if we fail to return the size in one of these two forms. */
16647 static dw_loc_descr_ref
16648 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16650 tree tree_size;
16651 struct loc_descr_context ctx;
16653 /* Return a constant integer in priority, if possible. */
16654 *cst_size = int_size_in_bytes (type);
16655 if (*cst_size != -1)
16656 return NULL;
16658 ctx.context_type = const_cast<tree> (type);
16659 ctx.base_decl = NULL_TREE;
16660 ctx.dpi = NULL;
16662 type = TYPE_MAIN_VARIANT (type);
16663 tree_size = TYPE_SIZE_UNIT (type);
16664 return ((tree_size != NULL_TREE)
16665 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16666 : NULL);
16669 /* Helper structure for RECORD_TYPE processing. */
16670 struct vlr_context
16672 /* Root RECORD_TYPE. It is needed to generate data member location
16673 descriptions in variable-length records (VLR), but also to cope with
16674 variants, which are composed of nested structures multiplexed with
16675 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16676 function processing a FIELD_DECL, it is required to be non null. */
16677 tree struct_type;
16678 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16679 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16680 this variant part as part of the root record (in storage units). For
16681 regular records, it must be NULL_TREE. */
16682 tree variant_part_offset;
16685 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16686 addressed byte of the "containing object" for the given FIELD_DECL. If
16687 possible, return a native constant through CST_OFFSET (in which case NULL is
16688 returned); otherwise return a DWARF expression that computes the offset.
16690 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16691 that offset is, either because the argument turns out to be a pointer to an
16692 ERROR_MARK node, or because the offset expression is too complex for us.
16694 CTX is required: see the comment for VLR_CONTEXT. */
16696 static dw_loc_descr_ref
16697 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16698 HOST_WIDE_INT *cst_offset)
16700 offset_int object_offset_in_bits;
16701 offset_int object_offset_in_bytes;
16702 offset_int bitpos_int;
16703 bool is_byte_offset_cst, is_bit_offset_cst;
16704 tree tree_result;
16705 dw_loc_list_ref loc_result;
16707 *cst_offset = 0;
16709 if (TREE_CODE (decl) == ERROR_MARK)
16710 return NULL;
16711 else
16712 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16714 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16715 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16717 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16718 case. */
16719 if (is_bit_offset_cst)
16720 return NULL;
16722 #ifdef PCC_BITFIELD_TYPE_MATTERS
16723 /* We used to handle only constant offsets in all cases. Now, we handle
16724 properly dynamic byte offsets only when PCC bitfield type doesn't
16725 matter. */
16726 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16728 tree type;
16729 tree field_size_tree;
16730 offset_int deepest_bitpos;
16731 offset_int field_size_in_bits;
16732 unsigned int type_align_in_bits;
16733 unsigned int decl_align_in_bits;
16734 offset_int type_size_in_bits;
16736 bitpos_int = wi::to_offset (bit_position (decl));
16737 type = field_type (decl);
16738 type_size_in_bits = offset_int_type_size_in_bits (type);
16739 type_align_in_bits = simple_type_align_in_bits (type);
16741 field_size_tree = DECL_SIZE (decl);
16743 /* The size could be unspecified if there was an error, or for
16744 a flexible array member. */
16745 if (!field_size_tree)
16746 field_size_tree = bitsize_zero_node;
16748 /* If the size of the field is not constant, use the type size. */
16749 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16750 field_size_in_bits = wi::to_offset (field_size_tree);
16751 else
16752 field_size_in_bits = type_size_in_bits;
16754 decl_align_in_bits = simple_decl_align_in_bits (decl);
16756 /* The GCC front-end doesn't make any attempt to keep track of the
16757 starting bit offset (relative to the start of the containing
16758 structure type) of the hypothetical "containing object" for a
16759 bit-field. Thus, when computing the byte offset value for the
16760 start of the "containing object" of a bit-field, we must deduce
16761 this information on our own. This can be rather tricky to do in
16762 some cases. For example, handling the following structure type
16763 definition when compiling for an i386/i486 target (which only
16764 aligns long long's to 32-bit boundaries) can be very tricky:
16766 struct S { int field1; long long field2:31; };
16768 Fortunately, there is a simple rule-of-thumb which can be used
16769 in such cases. When compiling for an i386/i486, GCC will
16770 allocate 8 bytes for the structure shown above. It decides to
16771 do this based upon one simple rule for bit-field allocation.
16772 GCC allocates each "containing object" for each bit-field at
16773 the first (i.e. lowest addressed) legitimate alignment boundary
16774 (based upon the required minimum alignment for the declared
16775 type of the field) which it can possibly use, subject to the
16776 condition that there is still enough available space remaining
16777 in the containing object (when allocated at the selected point)
16778 to fully accommodate all of the bits of the bit-field itself.
16780 This simple rule makes it obvious why GCC allocates 8 bytes for
16781 each object of the structure type shown above. When looking
16782 for a place to allocate the "containing object" for `field2',
16783 the compiler simply tries to allocate a 64-bit "containing
16784 object" at each successive 32-bit boundary (starting at zero)
16785 until it finds a place to allocate that 64- bit field such that
16786 at least 31 contiguous (and previously unallocated) bits remain
16787 within that selected 64 bit field. (As it turns out, for the
16788 example above, the compiler finds it is OK to allocate the
16789 "containing object" 64-bit field at bit-offset zero within the
16790 structure type.)
16792 Here we attempt to work backwards from the limited set of facts
16793 we're given, and we try to deduce from those facts, where GCC
16794 must have believed that the containing object started (within
16795 the structure type). The value we deduce is then used (by the
16796 callers of this routine) to generate DW_AT_location and
16797 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16798 the case of DW_AT_location, regular fields as well). */
16800 /* Figure out the bit-distance from the start of the structure to
16801 the "deepest" bit of the bit-field. */
16802 deepest_bitpos = bitpos_int + field_size_in_bits;
16804 /* This is the tricky part. Use some fancy footwork to deduce
16805 where the lowest addressed bit of the containing object must
16806 be. */
16807 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16809 /* Round up to type_align by default. This works best for
16810 bitfields. */
16811 object_offset_in_bits
16812 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16814 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16816 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16818 /* Round up to decl_align instead. */
16819 object_offset_in_bits
16820 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16823 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16825 tree_result = byte_position (decl);
16826 if (ctx->variant_part_offset != NULL_TREE)
16827 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16828 ctx->variant_part_offset, tree_result));
16830 /* If the byte offset is a constant, it's simplier to handle a native
16831 constant rather than a DWARF expression. */
16832 if (TREE_CODE (tree_result) == INTEGER_CST)
16834 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16835 return NULL;
16837 struct loc_descr_context loc_ctx = {
16838 ctx->struct_type, /* context_type */
16839 NULL_TREE, /* base_decl */
16840 NULL /* dpi */
16842 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16844 /* We want a DWARF expression: abort if we only have a location list with
16845 multiple elements. */
16846 if (!loc_result || !single_element_loc_list_p (loc_result))
16847 return NULL;
16848 else
16849 return loc_result->expr;
16852 /* The following routines define various Dwarf attributes and any data
16853 associated with them. */
16855 /* Add a location description attribute value to a DIE.
16857 This emits location attributes suitable for whole variables and
16858 whole parameters. Note that the location attributes for struct fields are
16859 generated by the routine `data_member_location_attribute' below. */
16861 static inline void
16862 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16863 dw_loc_list_ref descr)
16865 if (descr == 0)
16866 return;
16867 if (single_element_loc_list_p (descr))
16868 add_AT_loc (die, attr_kind, descr->expr);
16869 else
16870 add_AT_loc_list (die, attr_kind, descr);
16873 /* Add DW_AT_accessibility attribute to DIE if needed. */
16875 static void
16876 add_accessibility_attribute (dw_die_ref die, tree decl)
16878 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16879 children, otherwise the default is DW_ACCESS_public. In DWARF2
16880 the default has always been DW_ACCESS_public. */
16881 if (TREE_PROTECTED (decl))
16882 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16883 else if (TREE_PRIVATE (decl))
16885 if (dwarf_version == 2
16886 || die->die_parent == NULL
16887 || die->die_parent->die_tag != DW_TAG_class_type)
16888 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16890 else if (dwarf_version > 2
16891 && die->die_parent
16892 && die->die_parent->die_tag == DW_TAG_class_type)
16893 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16896 /* Attach the specialized form of location attribute used for data members of
16897 struct and union types. In the special case of a FIELD_DECL node which
16898 represents a bit-field, the "offset" part of this special location
16899 descriptor must indicate the distance in bytes from the lowest-addressed
16900 byte of the containing struct or union type to the lowest-addressed byte of
16901 the "containing object" for the bit-field. (See the `field_byte_offset'
16902 function above).
16904 For any given bit-field, the "containing object" is a hypothetical object
16905 (of some integral or enum type) within which the given bit-field lives. The
16906 type of this hypothetical "containing object" is always the same as the
16907 declared type of the individual bit-field itself (for GCC anyway... the
16908 DWARF spec doesn't actually mandate this). Note that it is the size (in
16909 bytes) of the hypothetical "containing object" which will be given in the
16910 DW_AT_byte_size attribute for this bit-field. (See the
16911 `byte_size_attribute' function below.) It is also used when calculating the
16912 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16913 function below.)
16915 CTX is required: see the comment for VLR_CONTEXT. */
16917 static void
16918 add_data_member_location_attribute (dw_die_ref die,
16919 tree decl,
16920 struct vlr_context *ctx)
16922 HOST_WIDE_INT offset;
16923 dw_loc_descr_ref loc_descr = 0;
16925 if (TREE_CODE (decl) == TREE_BINFO)
16927 /* We're working on the TAG_inheritance for a base class. */
16928 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16930 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16931 aren't at a fixed offset from all (sub)objects of the same
16932 type. We need to extract the appropriate offset from our
16933 vtable. The following dwarf expression means
16935 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16937 This is specific to the V3 ABI, of course. */
16939 dw_loc_descr_ref tmp;
16941 /* Make a copy of the object address. */
16942 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16943 add_loc_descr (&loc_descr, tmp);
16945 /* Extract the vtable address. */
16946 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16947 add_loc_descr (&loc_descr, tmp);
16949 /* Calculate the address of the offset. */
16950 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16951 gcc_assert (offset < 0);
16953 tmp = int_loc_descriptor (-offset);
16954 add_loc_descr (&loc_descr, tmp);
16955 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16956 add_loc_descr (&loc_descr, tmp);
16958 /* Extract the offset. */
16959 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16960 add_loc_descr (&loc_descr, tmp);
16962 /* Add it to the object address. */
16963 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16964 add_loc_descr (&loc_descr, tmp);
16966 else
16967 offset = tree_to_shwi (BINFO_OFFSET (decl));
16969 else
16971 loc_descr = field_byte_offset (decl, ctx, &offset);
16973 /* If loc_descr is available then we know the field offset is dynamic.
16974 However, GDB does not handle dynamic field offsets very well at the
16975 moment. */
16976 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16978 loc_descr = NULL;
16979 offset = 0;
16982 /* Data member location evalutation starts with the base address on the
16983 stack. Compute the field offset and add it to this base address. */
16984 else if (loc_descr != NULL)
16985 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16988 if (! loc_descr)
16990 if (dwarf_version > 2)
16992 /* Don't need to output a location expression, just the constant. */
16993 if (offset < 0)
16994 add_AT_int (die, DW_AT_data_member_location, offset);
16995 else
16996 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16997 return;
16999 else
17001 enum dwarf_location_atom op;
17003 /* The DWARF2 standard says that we should assume that the structure
17004 address is already on the stack, so we can specify a structure
17005 field address by using DW_OP_plus_uconst. */
17006 op = DW_OP_plus_uconst;
17007 loc_descr = new_loc_descr (op, offset, 0);
17011 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17014 /* Writes integer values to dw_vec_const array. */
17016 static void
17017 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17019 while (size != 0)
17021 *dest++ = val & 0xff;
17022 val >>= 8;
17023 --size;
17027 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17029 static HOST_WIDE_INT
17030 extract_int (const unsigned char *src, unsigned int size)
17032 HOST_WIDE_INT val = 0;
17034 src += size;
17035 while (size != 0)
17037 val <<= 8;
17038 val |= *--src & 0xff;
17039 --size;
17041 return val;
17044 /* Writes wide_int values to dw_vec_const array. */
17046 static void
17047 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17049 int i;
17051 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17053 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17054 return;
17057 /* We'd have to extend this code to support odd sizes. */
17058 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17060 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17062 if (WORDS_BIG_ENDIAN)
17063 for (i = n - 1; i >= 0; i--)
17065 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17066 dest += sizeof (HOST_WIDE_INT);
17068 else
17069 for (i = 0; i < n; i++)
17071 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17072 dest += sizeof (HOST_WIDE_INT);
17076 /* Writes floating point values to dw_vec_const array. */
17078 static void
17079 insert_float (const_rtx rtl, unsigned char *array)
17081 long val[4];
17082 int i;
17084 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17086 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17087 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17089 insert_int (val[i], 4, array);
17090 array += 4;
17094 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17095 does not have a "location" either in memory or in a register. These
17096 things can arise in GNU C when a constant is passed as an actual parameter
17097 to an inlined function. They can also arise in C++ where declared
17098 constants do not necessarily get memory "homes". */
17100 static bool
17101 add_const_value_attribute (dw_die_ref die, rtx rtl)
17103 switch (GET_CODE (rtl))
17105 case CONST_INT:
17107 HOST_WIDE_INT val = INTVAL (rtl);
17109 if (val < 0)
17110 add_AT_int (die, DW_AT_const_value, val);
17111 else
17112 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17114 return true;
17116 case CONST_WIDE_INT:
17118 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17119 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17120 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17121 wide_int w = wi::zext (w1, prec);
17122 add_AT_wide (die, DW_AT_const_value, w);
17124 return true;
17126 case CONST_DOUBLE:
17127 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17128 floating-point constant. A CONST_DOUBLE is used whenever the
17129 constant requires more than one word in order to be adequately
17130 represented. */
17132 machine_mode mode = GET_MODE (rtl);
17134 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17135 add_AT_double (die, DW_AT_const_value,
17136 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17137 else
17139 unsigned int length = GET_MODE_SIZE (mode);
17140 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17142 insert_float (rtl, array);
17143 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17146 return true;
17148 case CONST_VECTOR:
17150 machine_mode mode = GET_MODE (rtl);
17151 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17152 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17153 unsigned char *array
17154 = ggc_vec_alloc<unsigned char> (length * elt_size);
17155 unsigned int i;
17156 unsigned char *p;
17157 machine_mode imode = GET_MODE_INNER (mode);
17159 switch (GET_MODE_CLASS (mode))
17161 case MODE_VECTOR_INT:
17162 for (i = 0, p = array; i < length; i++, p += elt_size)
17164 rtx elt = CONST_VECTOR_ELT (rtl, i);
17165 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17167 break;
17169 case MODE_VECTOR_FLOAT:
17170 for (i = 0, p = array; i < length; i++, p += elt_size)
17172 rtx elt = CONST_VECTOR_ELT (rtl, i);
17173 insert_float (elt, p);
17175 break;
17177 default:
17178 gcc_unreachable ();
17181 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17183 return true;
17185 case CONST_STRING:
17186 if (dwarf_version >= 4 || !dwarf_strict)
17188 dw_loc_descr_ref loc_result;
17189 resolve_one_addr (&rtl);
17190 rtl_addr:
17191 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17192 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17193 add_AT_loc (die, DW_AT_location, loc_result);
17194 vec_safe_push (used_rtx_array, rtl);
17195 return true;
17197 return false;
17199 case CONST:
17200 if (CONSTANT_P (XEXP (rtl, 0)))
17201 return add_const_value_attribute (die, XEXP (rtl, 0));
17202 /* FALLTHROUGH */
17203 case SYMBOL_REF:
17204 if (!const_ok_for_output (rtl))
17205 return false;
17206 /* FALLTHROUGH */
17207 case LABEL_REF:
17208 if (dwarf_version >= 4 || !dwarf_strict)
17209 goto rtl_addr;
17210 return false;
17212 case PLUS:
17213 /* In cases where an inlined instance of an inline function is passed
17214 the address of an `auto' variable (which is local to the caller) we
17215 can get a situation where the DECL_RTL of the artificial local
17216 variable (for the inlining) which acts as a stand-in for the
17217 corresponding formal parameter (of the inline function) will look
17218 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17219 exactly a compile-time constant expression, but it isn't the address
17220 of the (artificial) local variable either. Rather, it represents the
17221 *value* which the artificial local variable always has during its
17222 lifetime. We currently have no way to represent such quasi-constant
17223 values in Dwarf, so for now we just punt and generate nothing. */
17224 return false;
17226 case HIGH:
17227 case CONST_FIXED:
17228 return false;
17230 case MEM:
17231 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17232 && MEM_READONLY_P (rtl)
17233 && GET_MODE (rtl) == BLKmode)
17235 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17236 return true;
17238 return false;
17240 default:
17241 /* No other kinds of rtx should be possible here. */
17242 gcc_unreachable ();
17244 return false;
17247 /* Determine whether the evaluation of EXPR references any variables
17248 or functions which aren't otherwise used (and therefore may not be
17249 output). */
17250 static tree
17251 reference_to_unused (tree * tp, int * walk_subtrees,
17252 void * data ATTRIBUTE_UNUSED)
17254 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17255 *walk_subtrees = 0;
17257 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17258 && ! TREE_ASM_WRITTEN (*tp))
17259 return *tp;
17260 /* ??? The C++ FE emits debug information for using decls, so
17261 putting gcc_unreachable here falls over. See PR31899. For now
17262 be conservative. */
17263 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
17264 return *tp;
17265 else if (VAR_P (*tp))
17267 varpool_node *node = varpool_node::get (*tp);
17268 if (!node || !node->definition)
17269 return *tp;
17271 else if (TREE_CODE (*tp) == FUNCTION_DECL
17272 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17274 /* The call graph machinery must have finished analyzing,
17275 optimizing and gimplifying the CU by now.
17276 So if *TP has no call graph node associated
17277 to it, it means *TP will not be emitted. */
17278 if (!cgraph_node::get (*tp))
17279 return *tp;
17281 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17282 return *tp;
17284 return NULL_TREE;
17287 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17288 for use in a later add_const_value_attribute call. */
17290 static rtx
17291 rtl_for_decl_init (tree init, tree type)
17293 rtx rtl = NULL_RTX;
17295 STRIP_NOPS (init);
17297 /* If a variable is initialized with a string constant without embedded
17298 zeros, build CONST_STRING. */
17299 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17301 tree enttype = TREE_TYPE (type);
17302 tree domain = TYPE_DOMAIN (type);
17303 machine_mode mode = TYPE_MODE (enttype);
17305 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17306 && domain
17307 && integer_zerop (TYPE_MIN_VALUE (domain))
17308 && compare_tree_int (TYPE_MAX_VALUE (domain),
17309 TREE_STRING_LENGTH (init) - 1) == 0
17310 && ((size_t) TREE_STRING_LENGTH (init)
17311 == strlen (TREE_STRING_POINTER (init)) + 1))
17313 rtl = gen_rtx_CONST_STRING (VOIDmode,
17314 ggc_strdup (TREE_STRING_POINTER (init)));
17315 rtl = gen_rtx_MEM (BLKmode, rtl);
17316 MEM_READONLY_P (rtl) = 1;
17319 /* Other aggregates, and complex values, could be represented using
17320 CONCAT: FIXME! */
17321 else if (AGGREGATE_TYPE_P (type)
17322 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17323 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17324 || TREE_CODE (type) == COMPLEX_TYPE)
17326 /* Vectors only work if their mode is supported by the target.
17327 FIXME: generic vectors ought to work too. */
17328 else if (TREE_CODE (type) == VECTOR_TYPE
17329 && !VECTOR_MODE_P (TYPE_MODE (type)))
17331 /* If the initializer is something that we know will expand into an
17332 immediate RTL constant, expand it now. We must be careful not to
17333 reference variables which won't be output. */
17334 else if (initializer_constant_valid_p (init, type)
17335 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17337 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17338 possible. */
17339 if (TREE_CODE (type) == VECTOR_TYPE)
17340 switch (TREE_CODE (init))
17342 case VECTOR_CST:
17343 break;
17344 case CONSTRUCTOR:
17345 if (TREE_CONSTANT (init))
17347 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17348 bool constant_p = true;
17349 tree value;
17350 unsigned HOST_WIDE_INT ix;
17352 /* Even when ctor is constant, it might contain non-*_CST
17353 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17354 belong into VECTOR_CST nodes. */
17355 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17356 if (!CONSTANT_CLASS_P (value))
17358 constant_p = false;
17359 break;
17362 if (constant_p)
17364 init = build_vector_from_ctor (type, elts);
17365 break;
17368 /* FALLTHRU */
17370 default:
17371 return NULL;
17374 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17376 /* If expand_expr returns a MEM, it wasn't immediate. */
17377 gcc_assert (!rtl || !MEM_P (rtl));
17380 return rtl;
17383 /* Generate RTL for the variable DECL to represent its location. */
17385 static rtx
17386 rtl_for_decl_location (tree decl)
17388 rtx rtl;
17390 /* Here we have to decide where we are going to say the parameter "lives"
17391 (as far as the debugger is concerned). We only have a couple of
17392 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17394 DECL_RTL normally indicates where the parameter lives during most of the
17395 activation of the function. If optimization is enabled however, this
17396 could be either NULL or else a pseudo-reg. Both of those cases indicate
17397 that the parameter doesn't really live anywhere (as far as the code
17398 generation parts of GCC are concerned) during most of the function's
17399 activation. That will happen (for example) if the parameter is never
17400 referenced within the function.
17402 We could just generate a location descriptor here for all non-NULL
17403 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17404 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17405 where DECL_RTL is NULL or is a pseudo-reg.
17407 Note however that we can only get away with using DECL_INCOMING_RTL as
17408 a backup substitute for DECL_RTL in certain limited cases. In cases
17409 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17410 we can be sure that the parameter was passed using the same type as it is
17411 declared to have within the function, and that its DECL_INCOMING_RTL
17412 points us to a place where a value of that type is passed.
17414 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17415 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17416 because in these cases DECL_INCOMING_RTL points us to a value of some
17417 type which is *different* from the type of the parameter itself. Thus,
17418 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17419 such cases, the debugger would end up (for example) trying to fetch a
17420 `float' from a place which actually contains the first part of a
17421 `double'. That would lead to really incorrect and confusing
17422 output at debug-time.
17424 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17425 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17426 are a couple of exceptions however. On little-endian machines we can
17427 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17428 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17429 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17430 when (on a little-endian machine) a non-prototyped function has a
17431 parameter declared to be of type `short' or `char'. In such cases,
17432 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17433 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17434 passed `int' value. If the debugger then uses that address to fetch
17435 a `short' or a `char' (on a little-endian machine) the result will be
17436 the correct data, so we allow for such exceptional cases below.
17438 Note that our goal here is to describe the place where the given formal
17439 parameter lives during most of the function's activation (i.e. between the
17440 end of the prologue and the start of the epilogue). We'll do that as best
17441 as we can. Note however that if the given formal parameter is modified
17442 sometime during the execution of the function, then a stack backtrace (at
17443 debug-time) will show the function as having been called with the *new*
17444 value rather than the value which was originally passed in. This happens
17445 rarely enough that it is not a major problem, but it *is* a problem, and
17446 I'd like to fix it.
17448 A future version of dwarf2out.c may generate two additional attributes for
17449 any given DW_TAG_formal_parameter DIE which will describe the "passed
17450 type" and the "passed location" for the given formal parameter in addition
17451 to the attributes we now generate to indicate the "declared type" and the
17452 "active location" for each parameter. This additional set of attributes
17453 could be used by debuggers for stack backtraces. Separately, note that
17454 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17455 This happens (for example) for inlined-instances of inline function formal
17456 parameters which are never referenced. This really shouldn't be
17457 happening. All PARM_DECL nodes should get valid non-NULL
17458 DECL_INCOMING_RTL values. FIXME. */
17460 /* Use DECL_RTL as the "location" unless we find something better. */
17461 rtl = DECL_RTL_IF_SET (decl);
17463 /* When generating abstract instances, ignore everything except
17464 constants, symbols living in memory, and symbols living in
17465 fixed registers. */
17466 if (! reload_completed)
17468 if (rtl
17469 && (CONSTANT_P (rtl)
17470 || (MEM_P (rtl)
17471 && CONSTANT_P (XEXP (rtl, 0)))
17472 || (REG_P (rtl)
17473 && VAR_P (decl)
17474 && TREE_STATIC (decl))))
17476 rtl = targetm.delegitimize_address (rtl);
17477 return rtl;
17479 rtl = NULL_RTX;
17481 else if (TREE_CODE (decl) == PARM_DECL)
17483 if (rtl == NULL_RTX
17484 || is_pseudo_reg (rtl)
17485 || (MEM_P (rtl)
17486 && is_pseudo_reg (XEXP (rtl, 0))
17487 && DECL_INCOMING_RTL (decl)
17488 && MEM_P (DECL_INCOMING_RTL (decl))
17489 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17491 tree declared_type = TREE_TYPE (decl);
17492 tree passed_type = DECL_ARG_TYPE (decl);
17493 machine_mode dmode = TYPE_MODE (declared_type);
17494 machine_mode pmode = TYPE_MODE (passed_type);
17496 /* This decl represents a formal parameter which was optimized out.
17497 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17498 all cases where (rtl == NULL_RTX) just below. */
17499 if (dmode == pmode)
17500 rtl = DECL_INCOMING_RTL (decl);
17501 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17502 && SCALAR_INT_MODE_P (dmode)
17503 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17504 && DECL_INCOMING_RTL (decl))
17506 rtx inc = DECL_INCOMING_RTL (decl);
17507 if (REG_P (inc))
17508 rtl = inc;
17509 else if (MEM_P (inc))
17511 if (BYTES_BIG_ENDIAN)
17512 rtl = adjust_address_nv (inc, dmode,
17513 GET_MODE_SIZE (pmode)
17514 - GET_MODE_SIZE (dmode));
17515 else
17516 rtl = inc;
17521 /* If the parm was passed in registers, but lives on the stack, then
17522 make a big endian correction if the mode of the type of the
17523 parameter is not the same as the mode of the rtl. */
17524 /* ??? This is the same series of checks that are made in dbxout.c before
17525 we reach the big endian correction code there. It isn't clear if all
17526 of these checks are necessary here, but keeping them all is the safe
17527 thing to do. */
17528 else if (MEM_P (rtl)
17529 && XEXP (rtl, 0) != const0_rtx
17530 && ! CONSTANT_P (XEXP (rtl, 0))
17531 /* Not passed in memory. */
17532 && !MEM_P (DECL_INCOMING_RTL (decl))
17533 /* Not passed by invisible reference. */
17534 && (!REG_P (XEXP (rtl, 0))
17535 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17536 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17537 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17538 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17539 #endif
17541 /* Big endian correction check. */
17542 && BYTES_BIG_ENDIAN
17543 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17544 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17545 < UNITS_PER_WORD))
17547 machine_mode addr_mode = get_address_mode (rtl);
17548 int offset = (UNITS_PER_WORD
17549 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17551 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17552 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17555 else if (VAR_P (decl)
17556 && rtl
17557 && MEM_P (rtl)
17558 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17559 && BYTES_BIG_ENDIAN)
17561 machine_mode addr_mode = get_address_mode (rtl);
17562 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17563 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17565 /* If a variable is declared "register" yet is smaller than
17566 a register, then if we store the variable to memory, it
17567 looks like we're storing a register-sized value, when in
17568 fact we are not. We need to adjust the offset of the
17569 storage location to reflect the actual value's bytes,
17570 else gdb will not be able to display it. */
17571 if (rsize > dsize)
17572 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17573 plus_constant (addr_mode, XEXP (rtl, 0),
17574 rsize - dsize));
17577 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17578 and will have been substituted directly into all expressions that use it.
17579 C does not have such a concept, but C++ and other languages do. */
17580 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
17581 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17583 if (rtl)
17584 rtl = targetm.delegitimize_address (rtl);
17586 /* If we don't look past the constant pool, we risk emitting a
17587 reference to a constant pool entry that isn't referenced from
17588 code, and thus is not emitted. */
17589 if (rtl)
17590 rtl = avoid_constant_pool_reference (rtl);
17592 /* Try harder to get a rtl. If this symbol ends up not being emitted
17593 in the current CU, resolve_addr will remove the expression referencing
17594 it. */
17595 if (rtl == NULL_RTX
17596 && VAR_P (decl)
17597 && !DECL_EXTERNAL (decl)
17598 && TREE_STATIC (decl)
17599 && DECL_NAME (decl)
17600 && !DECL_HARD_REGISTER (decl)
17601 && DECL_MODE (decl) != VOIDmode)
17603 rtl = make_decl_rtl_for_debug (decl);
17604 if (!MEM_P (rtl)
17605 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17606 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17607 rtl = NULL_RTX;
17610 return rtl;
17613 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17614 returned. If so, the decl for the COMMON block is returned, and the
17615 value is the offset into the common block for the symbol. */
17617 static tree
17618 fortran_common (tree decl, HOST_WIDE_INT *value)
17620 tree val_expr, cvar;
17621 machine_mode mode;
17622 HOST_WIDE_INT bitsize, bitpos;
17623 tree offset;
17624 int unsignedp, reversep, volatilep = 0;
17626 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17627 it does not have a value (the offset into the common area), or if it
17628 is thread local (as opposed to global) then it isn't common, and shouldn't
17629 be handled as such. */
17630 if (!VAR_P (decl)
17631 || !TREE_STATIC (decl)
17632 || !DECL_HAS_VALUE_EXPR_P (decl)
17633 || !is_fortran ())
17634 return NULL_TREE;
17636 val_expr = DECL_VALUE_EXPR (decl);
17637 if (TREE_CODE (val_expr) != COMPONENT_REF)
17638 return NULL_TREE;
17640 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17641 &unsignedp, &reversep, &volatilep);
17643 if (cvar == NULL_TREE
17644 || !VAR_P (cvar)
17645 || DECL_ARTIFICIAL (cvar)
17646 || !TREE_PUBLIC (cvar))
17647 return NULL_TREE;
17649 *value = 0;
17650 if (offset != NULL)
17652 if (!tree_fits_shwi_p (offset))
17653 return NULL_TREE;
17654 *value = tree_to_shwi (offset);
17656 if (bitpos != 0)
17657 *value += bitpos / BITS_PER_UNIT;
17659 return cvar;
17662 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17663 data attribute for a variable or a parameter. We generate the
17664 DW_AT_const_value attribute only in those cases where the given variable
17665 or parameter does not have a true "location" either in memory or in a
17666 register. This can happen (for example) when a constant is passed as an
17667 actual argument in a call to an inline function. (It's possible that
17668 these things can crop up in other ways also.) Note that one type of
17669 constant value which can be passed into an inlined function is a constant
17670 pointer. This can happen for example if an actual argument in an inlined
17671 function call evaluates to a compile-time constant address.
17673 CACHE_P is true if it is worth caching the location list for DECL,
17674 so that future calls can reuse it rather than regenerate it from scratch.
17675 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17676 since we will need to refer to them each time the function is inlined. */
17678 static bool
17679 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17681 rtx rtl;
17682 dw_loc_list_ref list;
17683 var_loc_list *loc_list;
17684 cached_dw_loc_list *cache;
17686 if (early_dwarf)
17687 return false;
17689 if (TREE_CODE (decl) == ERROR_MARK)
17690 return false;
17692 if (get_AT (die, DW_AT_location)
17693 || get_AT (die, DW_AT_const_value))
17694 return true;
17696 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
17697 || TREE_CODE (decl) == RESULT_DECL);
17699 /* Try to get some constant RTL for this decl, and use that as the value of
17700 the location. */
17702 rtl = rtl_for_decl_location (decl);
17703 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17704 && add_const_value_attribute (die, rtl))
17705 return true;
17707 /* See if we have single element location list that is equivalent to
17708 a constant value. That way we are better to use add_const_value_attribute
17709 rather than expanding constant value equivalent. */
17710 loc_list = lookup_decl_loc (decl);
17711 if (loc_list
17712 && loc_list->first
17713 && loc_list->first->next == NULL
17714 && NOTE_P (loc_list->first->loc)
17715 && NOTE_VAR_LOCATION (loc_list->first->loc)
17716 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17718 struct var_loc_node *node;
17720 node = loc_list->first;
17721 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17722 if (GET_CODE (rtl) == EXPR_LIST)
17723 rtl = XEXP (rtl, 0);
17724 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17725 && add_const_value_attribute (die, rtl))
17726 return true;
17728 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17729 list several times. See if we've already cached the contents. */
17730 list = NULL;
17731 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17732 cache_p = false;
17733 if (cache_p)
17735 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17736 if (cache)
17737 list = cache->loc_list;
17739 if (list == NULL)
17741 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17742 NULL);
17743 /* It is usually worth caching this result if the decl is from
17744 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17745 if (cache_p && list && list->dw_loc_next)
17747 cached_dw_loc_list **slot
17748 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17749 DECL_UID (decl),
17750 INSERT);
17751 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17752 cache->decl_id = DECL_UID (decl);
17753 cache->loc_list = list;
17754 *slot = cache;
17757 if (list)
17759 add_AT_location_description (die, DW_AT_location, list);
17760 return true;
17762 /* None of that worked, so it must not really have a location;
17763 try adding a constant value attribute from the DECL_INITIAL. */
17764 return tree_add_const_value_attribute_for_decl (die, decl);
17767 /* Helper function for tree_add_const_value_attribute. Natively encode
17768 initializer INIT into an array. Return true if successful. */
17770 static bool
17771 native_encode_initializer (tree init, unsigned char *array, int size)
17773 tree type;
17775 if (init == NULL_TREE)
17776 return false;
17778 STRIP_NOPS (init);
17779 switch (TREE_CODE (init))
17781 case STRING_CST:
17782 type = TREE_TYPE (init);
17783 if (TREE_CODE (type) == ARRAY_TYPE)
17785 tree enttype = TREE_TYPE (type);
17786 machine_mode mode = TYPE_MODE (enttype);
17788 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17789 return false;
17790 if (int_size_in_bytes (type) != size)
17791 return false;
17792 if (size > TREE_STRING_LENGTH (init))
17794 memcpy (array, TREE_STRING_POINTER (init),
17795 TREE_STRING_LENGTH (init));
17796 memset (array + TREE_STRING_LENGTH (init),
17797 '\0', size - TREE_STRING_LENGTH (init));
17799 else
17800 memcpy (array, TREE_STRING_POINTER (init), size);
17801 return true;
17803 return false;
17804 case CONSTRUCTOR:
17805 type = TREE_TYPE (init);
17806 if (int_size_in_bytes (type) != size)
17807 return false;
17808 if (TREE_CODE (type) == ARRAY_TYPE)
17810 HOST_WIDE_INT min_index;
17811 unsigned HOST_WIDE_INT cnt;
17812 int curpos = 0, fieldsize;
17813 constructor_elt *ce;
17815 if (TYPE_DOMAIN (type) == NULL_TREE
17816 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17817 return false;
17819 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17820 if (fieldsize <= 0)
17821 return false;
17823 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17824 memset (array, '\0', size);
17825 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17827 tree val = ce->value;
17828 tree index = ce->index;
17829 int pos = curpos;
17830 if (index && TREE_CODE (index) == RANGE_EXPR)
17831 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17832 * fieldsize;
17833 else if (index)
17834 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17836 if (val)
17838 STRIP_NOPS (val);
17839 if (!native_encode_initializer (val, array + pos, fieldsize))
17840 return false;
17842 curpos = pos + fieldsize;
17843 if (index && TREE_CODE (index) == RANGE_EXPR)
17845 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17846 - tree_to_shwi (TREE_OPERAND (index, 0));
17847 while (count-- > 0)
17849 if (val)
17850 memcpy (array + curpos, array + pos, fieldsize);
17851 curpos += fieldsize;
17854 gcc_assert (curpos <= size);
17856 return true;
17858 else if (TREE_CODE (type) == RECORD_TYPE
17859 || TREE_CODE (type) == UNION_TYPE)
17861 tree field = NULL_TREE;
17862 unsigned HOST_WIDE_INT cnt;
17863 constructor_elt *ce;
17865 if (int_size_in_bytes (type) != size)
17866 return false;
17868 if (TREE_CODE (type) == RECORD_TYPE)
17869 field = TYPE_FIELDS (type);
17871 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17873 tree val = ce->value;
17874 int pos, fieldsize;
17876 if (ce->index != 0)
17877 field = ce->index;
17879 if (val)
17880 STRIP_NOPS (val);
17882 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17883 return false;
17885 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17886 && TYPE_DOMAIN (TREE_TYPE (field))
17887 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17888 return false;
17889 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17890 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17891 return false;
17892 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17893 pos = int_byte_position (field);
17894 gcc_assert (pos + fieldsize <= size);
17895 if (val && fieldsize != 0
17896 && !native_encode_initializer (val, array + pos, fieldsize))
17897 return false;
17899 return true;
17901 return false;
17902 case VIEW_CONVERT_EXPR:
17903 case NON_LVALUE_EXPR:
17904 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17905 default:
17906 return native_encode_expr (init, array, size) == size;
17910 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17911 attribute is the const value T. */
17913 static bool
17914 tree_add_const_value_attribute (dw_die_ref die, tree t)
17916 tree init;
17917 tree type = TREE_TYPE (t);
17918 rtx rtl;
17920 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17921 return false;
17923 init = t;
17924 gcc_assert (!DECL_P (init));
17926 rtl = rtl_for_decl_init (init, type);
17927 if (rtl)
17928 return add_const_value_attribute (die, rtl);
17929 /* If the host and target are sane, try harder. */
17930 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17931 && initializer_constant_valid_p (init, type))
17933 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17934 if (size > 0 && (int) size == size)
17936 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17938 if (native_encode_initializer (init, array, size))
17940 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17941 return true;
17943 ggc_free (array);
17946 return false;
17949 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17950 attribute is the const value of T, where T is an integral constant
17951 variable with static storage duration
17952 (so it can't be a PARM_DECL or a RESULT_DECL). */
17954 static bool
17955 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17958 if (!decl
17959 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
17960 || (VAR_P (decl) && !TREE_STATIC (decl)))
17961 return false;
17963 if (TREE_READONLY (decl)
17964 && ! TREE_THIS_VOLATILE (decl)
17965 && DECL_INITIAL (decl))
17966 /* OK */;
17967 else
17968 return false;
17970 /* Don't add DW_AT_const_value if abstract origin already has one. */
17971 if (get_AT (var_die, DW_AT_const_value))
17972 return false;
17974 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17977 /* Convert the CFI instructions for the current function into a
17978 location list. This is used for DW_AT_frame_base when we targeting
17979 a dwarf2 consumer that does not support the dwarf3
17980 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17981 expressions. */
17983 static dw_loc_list_ref
17984 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17986 int ix;
17987 dw_fde_ref fde;
17988 dw_loc_list_ref list, *list_tail;
17989 dw_cfi_ref cfi;
17990 dw_cfa_location last_cfa, next_cfa;
17991 const char *start_label, *last_label, *section;
17992 dw_cfa_location remember;
17994 fde = cfun->fde;
17995 gcc_assert (fde != NULL);
17997 section = secname_for_decl (current_function_decl);
17998 list_tail = &list;
17999 list = NULL;
18001 memset (&next_cfa, 0, sizeof (next_cfa));
18002 next_cfa.reg = INVALID_REGNUM;
18003 remember = next_cfa;
18005 start_label = fde->dw_fde_begin;
18007 /* ??? Bald assumption that the CIE opcode list does not contain
18008 advance opcodes. */
18009 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18010 lookup_cfa_1 (cfi, &next_cfa, &remember);
18012 last_cfa = next_cfa;
18013 last_label = start_label;
18015 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18017 /* If the first partition contained no CFI adjustments, the
18018 CIE opcodes apply to the whole first partition. */
18019 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18020 fde->dw_fde_begin, fde->dw_fde_end, section);
18021 list_tail =&(*list_tail)->dw_loc_next;
18022 start_label = last_label = fde->dw_fde_second_begin;
18025 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18027 switch (cfi->dw_cfi_opc)
18029 case DW_CFA_set_loc:
18030 case DW_CFA_advance_loc1:
18031 case DW_CFA_advance_loc2:
18032 case DW_CFA_advance_loc4:
18033 if (!cfa_equal_p (&last_cfa, &next_cfa))
18035 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18036 start_label, last_label, section);
18038 list_tail = &(*list_tail)->dw_loc_next;
18039 last_cfa = next_cfa;
18040 start_label = last_label;
18042 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18043 break;
18045 case DW_CFA_advance_loc:
18046 /* The encoding is complex enough that we should never emit this. */
18047 gcc_unreachable ();
18049 default:
18050 lookup_cfa_1 (cfi, &next_cfa, &remember);
18051 break;
18053 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18055 if (!cfa_equal_p (&last_cfa, &next_cfa))
18057 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18058 start_label, last_label, section);
18060 list_tail = &(*list_tail)->dw_loc_next;
18061 last_cfa = next_cfa;
18062 start_label = last_label;
18064 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18065 start_label, fde->dw_fde_end, section);
18066 list_tail = &(*list_tail)->dw_loc_next;
18067 start_label = last_label = fde->dw_fde_second_begin;
18071 if (!cfa_equal_p (&last_cfa, &next_cfa))
18073 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18074 start_label, last_label, section);
18075 list_tail = &(*list_tail)->dw_loc_next;
18076 start_label = last_label;
18079 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18080 start_label,
18081 fde->dw_fde_second_begin
18082 ? fde->dw_fde_second_end : fde->dw_fde_end,
18083 section);
18085 if (list && list->dw_loc_next)
18086 gen_llsym (list);
18088 return list;
18091 /* Compute a displacement from the "steady-state frame pointer" to the
18092 frame base (often the same as the CFA), and store it in
18093 frame_pointer_fb_offset. OFFSET is added to the displacement
18094 before the latter is negated. */
18096 static void
18097 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18099 rtx reg, elim;
18101 #ifdef FRAME_POINTER_CFA_OFFSET
18102 reg = frame_pointer_rtx;
18103 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18104 #else
18105 reg = arg_pointer_rtx;
18106 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18107 #endif
18109 elim = (ira_use_lra_p
18110 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18111 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18112 if (GET_CODE (elim) == PLUS)
18114 offset += INTVAL (XEXP (elim, 1));
18115 elim = XEXP (elim, 0);
18118 frame_pointer_fb_offset = -offset;
18120 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18121 in which to eliminate. This is because it's stack pointer isn't
18122 directly accessible as a register within the ISA. To work around
18123 this, assume that while we cannot provide a proper value for
18124 frame_pointer_fb_offset, we won't need one either. */
18125 frame_pointer_fb_offset_valid
18126 = ((SUPPORTS_STACK_ALIGNMENT
18127 && (elim == hard_frame_pointer_rtx
18128 || elim == stack_pointer_rtx))
18129 || elim == (frame_pointer_needed
18130 ? hard_frame_pointer_rtx
18131 : stack_pointer_rtx));
18134 /* Generate a DW_AT_name attribute given some string value to be included as
18135 the value of the attribute. */
18137 static void
18138 add_name_attribute (dw_die_ref die, const char *name_string)
18140 if (name_string != NULL && *name_string != 0)
18142 if (demangle_name_func)
18143 name_string = (*demangle_name_func) (name_string);
18145 add_AT_string (die, DW_AT_name, name_string);
18149 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18150 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18151 of TYPE accordingly.
18153 ??? This is a temporary measure until after we're able to generate
18154 regular DWARF for the complex Ada type system. */
18156 static void
18157 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18158 dw_die_ref context_die)
18160 tree dtype;
18161 dw_die_ref dtype_die;
18163 if (!lang_hooks.types.descriptive_type)
18164 return;
18166 dtype = lang_hooks.types.descriptive_type (type);
18167 if (!dtype)
18168 return;
18170 dtype_die = lookup_type_die (dtype);
18171 if (!dtype_die)
18173 gen_type_die (dtype, context_die);
18174 dtype_die = lookup_type_die (dtype);
18175 gcc_assert (dtype_die);
18178 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18181 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18183 static const char *
18184 comp_dir_string (void)
18186 const char *wd;
18187 char *wd1;
18188 static const char *cached_wd = NULL;
18190 if (cached_wd != NULL)
18191 return cached_wd;
18193 wd = get_src_pwd ();
18194 if (wd == NULL)
18195 return NULL;
18197 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18199 int wdlen;
18201 wdlen = strlen (wd);
18202 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18203 strcpy (wd1, wd);
18204 wd1 [wdlen] = DIR_SEPARATOR;
18205 wd1 [wdlen + 1] = 0;
18206 wd = wd1;
18209 cached_wd = remap_debug_filename (wd);
18210 return cached_wd;
18213 /* Generate a DW_AT_comp_dir attribute for DIE. */
18215 static void
18216 add_comp_dir_attribute (dw_die_ref die)
18218 const char * wd = comp_dir_string ();
18219 if (wd != NULL)
18220 add_AT_string (die, DW_AT_comp_dir, wd);
18223 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18224 pointer computation, ...), output a representation for that bound according
18225 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18226 loc_list_from_tree for the meaning of CONTEXT. */
18228 static void
18229 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18230 int forms, const struct loc_descr_context *context)
18232 dw_die_ref context_die, decl_die;
18233 dw_loc_list_ref list;
18235 bool strip_conversions = true;
18237 while (strip_conversions)
18238 switch (TREE_CODE (value))
18240 case ERROR_MARK:
18241 case SAVE_EXPR:
18242 return;
18244 CASE_CONVERT:
18245 case VIEW_CONVERT_EXPR:
18246 value = TREE_OPERAND (value, 0);
18247 break;
18249 default:
18250 strip_conversions = false;
18251 break;
18254 /* If possible and permitted, output the attribute as a constant. */
18255 if ((forms & dw_scalar_form_constant) != 0
18256 && TREE_CODE (value) == INTEGER_CST)
18258 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18260 /* If HOST_WIDE_INT is big enough then represent the bound as
18261 a constant value. We need to choose a form based on
18262 whether the type is signed or unsigned. We cannot just
18263 call add_AT_unsigned if the value itself is positive
18264 (add_AT_unsigned might add the unsigned value encoded as
18265 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18266 bounds type and then sign extend any unsigned values found
18267 for signed types. This is needed only for
18268 DW_AT_{lower,upper}_bound, since for most other attributes,
18269 consumers will treat DW_FORM_data[1248] as unsigned values,
18270 regardless of the underlying type. */
18271 if (prec <= HOST_BITS_PER_WIDE_INT
18272 || tree_fits_uhwi_p (value))
18274 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18275 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18276 else
18277 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18279 else
18280 /* Otherwise represent the bound as an unsigned value with
18281 the precision of its type. The precision and signedness
18282 of the type will be necessary to re-interpret it
18283 unambiguously. */
18284 add_AT_wide (die, attr, value);
18285 return;
18288 /* Otherwise, if it's possible and permitted too, output a reference to
18289 another DIE. */
18290 if ((forms & dw_scalar_form_reference) != 0)
18292 tree decl = NULL_TREE;
18294 /* Some type attributes reference an outer type. For instance, the upper
18295 bound of an array may reference an embedding record (this happens in
18296 Ada). */
18297 if (TREE_CODE (value) == COMPONENT_REF
18298 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18299 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18300 decl = TREE_OPERAND (value, 1);
18302 else if (VAR_P (value)
18303 || TREE_CODE (value) == PARM_DECL
18304 || TREE_CODE (value) == RESULT_DECL)
18305 decl = value;
18307 if (decl != NULL_TREE)
18309 dw_die_ref decl_die = lookup_decl_die (decl);
18311 /* ??? Can this happen, or should the variable have been bound
18312 first? Probably it can, since I imagine that we try to create
18313 the types of parameters in the order in which they exist in
18314 the list, and won't have created a forward reference to a
18315 later parameter. */
18316 if (decl_die != NULL)
18318 add_AT_die_ref (die, attr, decl_die);
18319 return;
18324 /* Last chance: try to create a stack operation procedure to evaluate the
18325 value. Do nothing if even that is not possible or permitted. */
18326 if ((forms & dw_scalar_form_exprloc) == 0)
18327 return;
18329 list = loc_list_from_tree (value, 2, context);
18330 if (list == NULL || single_element_loc_list_p (list))
18332 /* If this attribute is not a reference nor constant, it is
18333 a DWARF expression rather than location description. For that
18334 loc_list_from_tree (value, 0, &context) is needed. */
18335 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18336 if (list2 && single_element_loc_list_p (list2))
18338 add_AT_loc (die, attr, list2->expr);
18339 return;
18343 /* If that failed to give a single element location list, fall back to
18344 outputting this as a reference... still if permitted. */
18345 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18346 return;
18348 if (current_function_decl == 0)
18349 context_die = comp_unit_die ();
18350 else
18351 context_die = lookup_decl_die (current_function_decl);
18353 decl_die = new_die (DW_TAG_variable, context_die, value);
18354 add_AT_flag (decl_die, DW_AT_artificial, 1);
18355 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18356 context_die);
18357 add_AT_location_description (decl_die, DW_AT_location, list);
18358 add_AT_die_ref (die, attr, decl_die);
18361 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18362 default. */
18364 static int
18365 lower_bound_default (void)
18367 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18369 case DW_LANG_C:
18370 case DW_LANG_C89:
18371 case DW_LANG_C99:
18372 case DW_LANG_C11:
18373 case DW_LANG_C_plus_plus:
18374 case DW_LANG_C_plus_plus_11:
18375 case DW_LANG_C_plus_plus_14:
18376 case DW_LANG_ObjC:
18377 case DW_LANG_ObjC_plus_plus:
18378 case DW_LANG_Java:
18379 return 0;
18380 case DW_LANG_Fortran77:
18381 case DW_LANG_Fortran90:
18382 case DW_LANG_Fortran95:
18383 case DW_LANG_Fortran03:
18384 case DW_LANG_Fortran08:
18385 return 1;
18386 case DW_LANG_UPC:
18387 case DW_LANG_D:
18388 case DW_LANG_Python:
18389 return dwarf_version >= 4 ? 0 : -1;
18390 case DW_LANG_Ada95:
18391 case DW_LANG_Ada83:
18392 case DW_LANG_Cobol74:
18393 case DW_LANG_Cobol85:
18394 case DW_LANG_Pascal83:
18395 case DW_LANG_Modula2:
18396 case DW_LANG_PLI:
18397 return dwarf_version >= 4 ? 1 : -1;
18398 default:
18399 return -1;
18403 /* Given a tree node describing an array bound (either lower or upper) output
18404 a representation for that bound. */
18406 static void
18407 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18408 tree bound, const struct loc_descr_context *context)
18410 int dflt;
18412 while (1)
18413 switch (TREE_CODE (bound))
18415 /* Strip all conversions. */
18416 CASE_CONVERT:
18417 case VIEW_CONVERT_EXPR:
18418 bound = TREE_OPERAND (bound, 0);
18419 break;
18421 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18422 are even omitted when they are the default. */
18423 case INTEGER_CST:
18424 /* If the value for this bound is the default one, we can even omit the
18425 attribute. */
18426 if (bound_attr == DW_AT_lower_bound
18427 && tree_fits_shwi_p (bound)
18428 && (dflt = lower_bound_default ()) != -1
18429 && tree_to_shwi (bound) == dflt)
18430 return;
18432 /* FALLTHRU */
18434 default:
18435 /* Because of the complex interaction there can be with other GNAT
18436 encodings, GDB isn't ready yet to handle proper DWARF description
18437 for self-referencial subrange bounds: let GNAT encodings do the
18438 magic in such a case. */
18439 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18440 && contains_placeholder_p (bound))
18441 return;
18443 add_scalar_info (subrange_die, bound_attr, bound,
18444 dw_scalar_form_constant
18445 | dw_scalar_form_exprloc
18446 | dw_scalar_form_reference,
18447 context);
18448 return;
18452 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18453 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18454 Note that the block of subscript information for an array type also
18455 includes information about the element type of the given array type.
18457 This function reuses previously set type and bound information if
18458 available. */
18460 static void
18461 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18463 unsigned dimension_number;
18464 tree lower, upper;
18465 dw_die_ref child = type_die->die_child;
18467 for (dimension_number = 0;
18468 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18469 type = TREE_TYPE (type), dimension_number++)
18471 tree domain = TYPE_DOMAIN (type);
18473 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18474 break;
18476 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18477 and (in GNU C only) variable bounds. Handle all three forms
18478 here. */
18480 /* Find and reuse a previously generated DW_TAG_subrange_type if
18481 available.
18483 For multi-dimensional arrays, as we iterate through the
18484 various dimensions in the enclosing for loop above, we also
18485 iterate through the DIE children and pick at each
18486 DW_TAG_subrange_type previously generated (if available).
18487 Each child DW_TAG_subrange_type DIE describes the range of
18488 the current dimension. At this point we should have as many
18489 DW_TAG_subrange_type's as we have dimensions in the
18490 array. */
18491 dw_die_ref subrange_die = NULL;
18492 if (child)
18493 while (1)
18495 child = child->die_sib;
18496 if (child->die_tag == DW_TAG_subrange_type)
18497 subrange_die = child;
18498 if (child == type_die->die_child)
18500 /* If we wrapped around, stop looking next time. */
18501 child = NULL;
18502 break;
18504 if (child->die_tag == DW_TAG_subrange_type)
18505 break;
18507 if (!subrange_die)
18508 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18510 if (domain)
18512 /* We have an array type with specified bounds. */
18513 lower = TYPE_MIN_VALUE (domain);
18514 upper = TYPE_MAX_VALUE (domain);
18516 /* Define the index type. */
18517 if (TREE_TYPE (domain)
18518 && !get_AT (subrange_die, DW_AT_type))
18520 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18521 TREE_TYPE field. We can't emit debug info for this
18522 because it is an unnamed integral type. */
18523 if (TREE_CODE (domain) == INTEGER_TYPE
18524 && TYPE_NAME (domain) == NULL_TREE
18525 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18526 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18528 else
18529 add_type_attribute (subrange_die, TREE_TYPE (domain),
18530 TYPE_UNQUALIFIED, false, type_die);
18533 /* ??? If upper is NULL, the array has unspecified length,
18534 but it does have a lower bound. This happens with Fortran
18535 dimension arr(N:*)
18536 Since the debugger is definitely going to need to know N
18537 to produce useful results, go ahead and output the lower
18538 bound solo, and hope the debugger can cope. */
18540 if (!get_AT (subrange_die, DW_AT_lower_bound))
18541 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18542 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18543 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18546 /* Otherwise we have an array type with an unspecified length. The
18547 DWARF-2 spec does not say how to handle this; let's just leave out the
18548 bounds. */
18552 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18554 static void
18555 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18557 dw_die_ref decl_die;
18558 HOST_WIDE_INT size;
18559 dw_loc_descr_ref size_expr = NULL;
18561 switch (TREE_CODE (tree_node))
18563 case ERROR_MARK:
18564 size = 0;
18565 break;
18566 case ENUMERAL_TYPE:
18567 case RECORD_TYPE:
18568 case UNION_TYPE:
18569 case QUAL_UNION_TYPE:
18570 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18571 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18573 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18574 return;
18576 size_expr = type_byte_size (tree_node, &size);
18577 break;
18578 case FIELD_DECL:
18579 /* For a data member of a struct or union, the DW_AT_byte_size is
18580 generally given as the number of bytes normally allocated for an
18581 object of the *declared* type of the member itself. This is true
18582 even for bit-fields. */
18583 size = int_size_in_bytes (field_type (tree_node));
18584 break;
18585 default:
18586 gcc_unreachable ();
18589 /* Support for dynamically-sized objects was introduced by DWARFv3.
18590 At the moment, GDB does not handle variable byte sizes very well,
18591 though. */
18592 if ((dwarf_version >= 3 || !dwarf_strict)
18593 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18594 && size_expr != NULL)
18595 add_AT_loc (die, DW_AT_byte_size, size_expr);
18597 /* Note that `size' might be -1 when we get to this point. If it is, that
18598 indicates that the byte size of the entity in question is variable and
18599 that we could not generate a DWARF expression that computes it. */
18600 if (size >= 0)
18601 add_AT_unsigned (die, DW_AT_byte_size, size);
18604 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18605 which specifies the distance in bits from the highest order bit of the
18606 "containing object" for the bit-field to the highest order bit of the
18607 bit-field itself.
18609 For any given bit-field, the "containing object" is a hypothetical object
18610 (of some integral or enum type) within which the given bit-field lives. The
18611 type of this hypothetical "containing object" is always the same as the
18612 declared type of the individual bit-field itself. The determination of the
18613 exact location of the "containing object" for a bit-field is rather
18614 complicated. It's handled by the `field_byte_offset' function (above).
18616 CTX is required: see the comment for VLR_CONTEXT.
18618 Note that it is the size (in bytes) of the hypothetical "containing object"
18619 which will be given in the DW_AT_byte_size attribute for this bit-field.
18620 (See `byte_size_attribute' above). */
18622 static inline void
18623 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18625 HOST_WIDE_INT object_offset_in_bytes;
18626 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18627 HOST_WIDE_INT bitpos_int;
18628 HOST_WIDE_INT highest_order_object_bit_offset;
18629 HOST_WIDE_INT highest_order_field_bit_offset;
18630 HOST_WIDE_INT bit_offset;
18632 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18634 /* Must be a field and a bit field. */
18635 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18637 /* We can't yet handle bit-fields whose offsets are variable, so if we
18638 encounter such things, just return without generating any attribute
18639 whatsoever. Likewise for variable or too large size. */
18640 if (! tree_fits_shwi_p (bit_position (decl))
18641 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18642 return;
18644 bitpos_int = int_bit_position (decl);
18646 /* Note that the bit offset is always the distance (in bits) from the
18647 highest-order bit of the "containing object" to the highest-order bit of
18648 the bit-field itself. Since the "high-order end" of any object or field
18649 is different on big-endian and little-endian machines, the computation
18650 below must take account of these differences. */
18651 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18652 highest_order_field_bit_offset = bitpos_int;
18654 if (! BYTES_BIG_ENDIAN)
18656 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18657 highest_order_object_bit_offset +=
18658 simple_type_size_in_bits (original_type);
18661 bit_offset
18662 = (! BYTES_BIG_ENDIAN
18663 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18664 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18666 if (bit_offset < 0)
18667 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18668 else
18669 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18672 /* For a FIELD_DECL node which represents a bit field, output an attribute
18673 which specifies the length in bits of the given field. */
18675 static inline void
18676 add_bit_size_attribute (dw_die_ref die, tree decl)
18678 /* Must be a field and a bit field. */
18679 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18680 && DECL_BIT_FIELD_TYPE (decl));
18682 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18683 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18686 /* If the compiled language is ANSI C, then add a 'prototyped'
18687 attribute, if arg types are given for the parameters of a function. */
18689 static inline void
18690 add_prototyped_attribute (dw_die_ref die, tree func_type)
18692 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18694 case DW_LANG_C:
18695 case DW_LANG_C89:
18696 case DW_LANG_C99:
18697 case DW_LANG_C11:
18698 case DW_LANG_ObjC:
18699 if (prototype_p (func_type))
18700 add_AT_flag (die, DW_AT_prototyped, 1);
18701 break;
18702 default:
18703 break;
18707 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18708 by looking in the type declaration, the object declaration equate table or
18709 the block mapping. */
18711 static inline dw_die_ref
18712 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18714 dw_die_ref origin_die = NULL;
18716 if (TREE_CODE (origin) != FUNCTION_DECL
18717 && TREE_CODE (origin) != BLOCK)
18719 /* We may have gotten separated from the block for the inlined
18720 function, if we're in an exception handler or some such; make
18721 sure that the abstract function has been written out.
18723 Doing this for nested functions is wrong, however; functions are
18724 distinct units, and our context might not even be inline. */
18725 tree fn = origin;
18727 if (TYPE_P (fn))
18728 fn = TYPE_STUB_DECL (fn);
18730 fn = decl_function_context (fn);
18731 if (fn)
18732 dwarf2out_abstract_function (fn);
18735 if (DECL_P (origin))
18736 origin_die = lookup_decl_die (origin);
18737 else if (TYPE_P (origin))
18738 origin_die = lookup_type_die (origin);
18739 else if (TREE_CODE (origin) == BLOCK)
18740 origin_die = BLOCK_DIE (origin);
18742 /* XXX: Functions that are never lowered don't always have correct block
18743 trees (in the case of java, they simply have no block tree, in some other
18744 languages). For these functions, there is nothing we can really do to
18745 output correct debug info for inlined functions in all cases. Rather
18746 than die, we'll just produce deficient debug info now, in that we will
18747 have variables without a proper abstract origin. In the future, when all
18748 functions are lowered, we should re-add a gcc_assert (origin_die)
18749 here. */
18751 if (origin_die)
18752 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18753 return origin_die;
18756 /* We do not currently support the pure_virtual attribute. */
18758 static inline void
18759 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18761 if (DECL_VINDEX (func_decl))
18763 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18765 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18766 add_AT_loc (die, DW_AT_vtable_elem_location,
18767 new_loc_descr (DW_OP_constu,
18768 tree_to_shwi (DECL_VINDEX (func_decl)),
18769 0));
18771 /* GNU extension: Record what type this method came from originally. */
18772 if (debug_info_level > DINFO_LEVEL_TERSE
18773 && DECL_CONTEXT (func_decl))
18774 add_AT_die_ref (die, DW_AT_containing_type,
18775 lookup_type_die (DECL_CONTEXT (func_decl)));
18779 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18780 given decl. This used to be a vendor extension until after DWARF 4
18781 standardized it. */
18783 static void
18784 add_linkage_attr (dw_die_ref die, tree decl)
18786 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18788 /* Mimic what assemble_name_raw does with a leading '*'. */
18789 if (name[0] == '*')
18790 name = &name[1];
18792 if (dwarf_version >= 4)
18793 add_AT_string (die, DW_AT_linkage_name, name);
18794 else
18795 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18798 /* Add source coordinate attributes for the given decl. */
18800 static void
18801 add_src_coords_attributes (dw_die_ref die, tree decl)
18803 expanded_location s;
18805 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18806 return;
18807 s = expand_location (DECL_SOURCE_LOCATION (decl));
18808 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18809 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18812 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18814 static void
18815 add_linkage_name_raw (dw_die_ref die, tree decl)
18817 /* Defer until we have an assembler name set. */
18818 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18820 limbo_die_node *asm_name;
18822 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18823 asm_name->die = die;
18824 asm_name->created_for = decl;
18825 asm_name->next = deferred_asm_name;
18826 deferred_asm_name = asm_name;
18828 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18829 add_linkage_attr (die, decl);
18832 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18834 static void
18835 add_linkage_name (dw_die_ref die, tree decl)
18837 if (debug_info_level > DINFO_LEVEL_NONE
18838 && VAR_OR_FUNCTION_DECL_P (decl)
18839 && TREE_PUBLIC (decl)
18840 && !(VAR_P (decl) && DECL_REGISTER (decl))
18841 && die->die_tag != DW_TAG_member)
18842 add_linkage_name_raw (die, decl);
18845 /* Add a DW_AT_name attribute and source coordinate attribute for the
18846 given decl, but only if it actually has a name. */
18848 static void
18849 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18851 tree decl_name;
18853 decl_name = DECL_NAME (decl);
18854 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18856 const char *name = dwarf2_name (decl, 0);
18857 if (name)
18858 add_name_attribute (die, name);
18859 if (! DECL_ARTIFICIAL (decl))
18860 add_src_coords_attributes (die, decl);
18862 add_linkage_name (die, decl);
18865 #ifdef VMS_DEBUGGING_INFO
18866 /* Get the function's name, as described by its RTL. This may be different
18867 from the DECL_NAME name used in the source file. */
18868 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18870 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18871 XEXP (DECL_RTL (decl), 0), false);
18872 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18874 #endif /* VMS_DEBUGGING_INFO */
18877 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18879 static void
18880 add_discr_value (dw_die_ref die, dw_discr_value *value)
18882 dw_attr_node attr;
18884 attr.dw_attr = DW_AT_discr_value;
18885 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18886 attr.dw_attr_val.val_entry = NULL;
18887 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18888 if (value->pos)
18889 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18890 else
18891 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18892 add_dwarf_attr (die, &attr);
18895 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18897 static void
18898 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18900 dw_attr_node attr;
18902 attr.dw_attr = DW_AT_discr_list;
18903 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18904 attr.dw_attr_val.val_entry = NULL;
18905 attr.dw_attr_val.v.val_discr_list = discr_list;
18906 add_dwarf_attr (die, &attr);
18909 static inline dw_discr_list_ref
18910 AT_discr_list (dw_attr_node *attr)
18912 return attr->dw_attr_val.v.val_discr_list;
18915 #ifdef VMS_DEBUGGING_INFO
18916 /* Output the debug main pointer die for VMS */
18918 void
18919 dwarf2out_vms_debug_main_pointer (void)
18921 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18922 dw_die_ref die;
18924 /* Allocate the VMS debug main subprogram die. */
18925 die = ggc_cleared_alloc<die_node> ();
18926 die->die_tag = DW_TAG_subprogram;
18927 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18928 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18929 current_function_funcdef_no);
18930 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18932 /* Make it the first child of comp_unit_die (). */
18933 die->die_parent = comp_unit_die ();
18934 if (comp_unit_die ()->die_child)
18936 die->die_sib = comp_unit_die ()->die_child->die_sib;
18937 comp_unit_die ()->die_child->die_sib = die;
18939 else
18941 die->die_sib = die;
18942 comp_unit_die ()->die_child = die;
18945 #endif /* VMS_DEBUGGING_INFO */
18947 /* Push a new declaration scope. */
18949 static void
18950 push_decl_scope (tree scope)
18952 vec_safe_push (decl_scope_table, scope);
18955 /* Pop a declaration scope. */
18957 static inline void
18958 pop_decl_scope (void)
18960 decl_scope_table->pop ();
18963 /* walk_tree helper function for uses_local_type, below. */
18965 static tree
18966 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18968 if (!TYPE_P (*tp))
18969 *walk_subtrees = 0;
18970 else
18972 tree name = TYPE_NAME (*tp);
18973 if (name && DECL_P (name) && decl_function_context (name))
18974 return *tp;
18976 return NULL_TREE;
18979 /* If TYPE involves a function-local type (including a local typedef to a
18980 non-local type), returns that type; otherwise returns NULL_TREE. */
18982 static tree
18983 uses_local_type (tree type)
18985 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18986 return used;
18989 /* Return the DIE for the scope that immediately contains this type.
18990 Non-named types that do not involve a function-local type get global
18991 scope. Named types nested in namespaces or other types get their
18992 containing scope. All other types (i.e. function-local named types) get
18993 the current active scope. */
18995 static dw_die_ref
18996 scope_die_for (tree t, dw_die_ref context_die)
18998 dw_die_ref scope_die = NULL;
18999 tree containing_scope;
19001 /* Non-types always go in the current scope. */
19002 gcc_assert (TYPE_P (t));
19004 /* Use the scope of the typedef, rather than the scope of the type
19005 it refers to. */
19006 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19007 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19008 else
19009 containing_scope = TYPE_CONTEXT (t);
19011 /* Use the containing namespace if there is one. */
19012 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19014 if (context_die == lookup_decl_die (containing_scope))
19015 /* OK */;
19016 else if (debug_info_level > DINFO_LEVEL_TERSE)
19017 context_die = get_context_die (containing_scope);
19018 else
19019 containing_scope = NULL_TREE;
19022 /* Ignore function type "scopes" from the C frontend. They mean that
19023 a tagged type is local to a parmlist of a function declarator, but
19024 that isn't useful to DWARF. */
19025 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19026 containing_scope = NULL_TREE;
19028 if (SCOPE_FILE_SCOPE_P (containing_scope))
19030 /* If T uses a local type keep it local as well, to avoid references
19031 to function-local DIEs from outside the function. */
19032 if (current_function_decl && uses_local_type (t))
19033 scope_die = context_die;
19034 else
19035 scope_die = comp_unit_die ();
19037 else if (TYPE_P (containing_scope))
19039 /* For types, we can just look up the appropriate DIE. */
19040 if (debug_info_level > DINFO_LEVEL_TERSE)
19041 scope_die = get_context_die (containing_scope);
19042 else
19044 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19045 if (scope_die == NULL)
19046 scope_die = comp_unit_die ();
19049 else
19050 scope_die = context_die;
19052 return scope_die;
19055 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19057 static inline int
19058 local_scope_p (dw_die_ref context_die)
19060 for (; context_die; context_die = context_die->die_parent)
19061 if (context_die->die_tag == DW_TAG_inlined_subroutine
19062 || context_die->die_tag == DW_TAG_subprogram)
19063 return 1;
19065 return 0;
19068 /* Returns nonzero if CONTEXT_DIE is a class. */
19070 static inline int
19071 class_scope_p (dw_die_ref context_die)
19073 return (context_die
19074 && (context_die->die_tag == DW_TAG_structure_type
19075 || context_die->die_tag == DW_TAG_class_type
19076 || context_die->die_tag == DW_TAG_interface_type
19077 || context_die->die_tag == DW_TAG_union_type));
19080 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19081 whether or not to treat a DIE in this context as a declaration. */
19083 static inline int
19084 class_or_namespace_scope_p (dw_die_ref context_die)
19086 return (class_scope_p (context_die)
19087 || (context_die && context_die->die_tag == DW_TAG_namespace));
19090 /* Many forms of DIEs require a "type description" attribute. This
19091 routine locates the proper "type descriptor" die for the type given
19092 by 'type' plus any additional qualifiers given by 'cv_quals', and
19093 adds a DW_AT_type attribute below the given die. */
19095 static void
19096 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19097 bool reverse, dw_die_ref context_die)
19099 enum tree_code code = TREE_CODE (type);
19100 dw_die_ref type_die = NULL;
19102 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19103 or fixed-point type, use the inner type. This is because we have no
19104 support for unnamed types in base_type_die. This can happen if this is
19105 an Ada subrange type. Correct solution is emit a subrange type die. */
19106 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19107 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19108 type = TREE_TYPE (type), code = TREE_CODE (type);
19110 if (code == ERROR_MARK
19111 /* Handle a special case. For functions whose return type is void, we
19112 generate *no* type attribute. (Note that no object may have type
19113 `void', so this only applies to function return types). */
19114 || code == VOID_TYPE)
19115 return;
19117 type_die = modified_type_die (type,
19118 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19119 reverse,
19120 context_die);
19122 if (type_die != NULL)
19123 add_AT_die_ref (object_die, DW_AT_type, type_die);
19126 /* Given an object die, add the calling convention attribute for the
19127 function call type. */
19128 static void
19129 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19131 enum dwarf_calling_convention value = DW_CC_normal;
19133 value = ((enum dwarf_calling_convention)
19134 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19136 if (is_fortran ()
19137 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19139 /* DWARF 2 doesn't provide a way to identify a program's source-level
19140 entry point. DW_AT_calling_convention attributes are only meant
19141 to describe functions' calling conventions. However, lacking a
19142 better way to signal the Fortran main program, we used this for
19143 a long time, following existing custom. Now, DWARF 4 has
19144 DW_AT_main_subprogram, which we add below, but some tools still
19145 rely on the old way, which we thus keep. */
19146 value = DW_CC_program;
19148 if (dwarf_version >= 4 || !dwarf_strict)
19149 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19152 /* Only add the attribute if the backend requests it, and
19153 is not DW_CC_normal. */
19154 if (value && (value != DW_CC_normal))
19155 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19158 /* Given a tree pointer to a struct, class, union, or enum type node, return
19159 a pointer to the (string) tag name for the given type, or zero if the type
19160 was declared without a tag. */
19162 static const char *
19163 type_tag (const_tree type)
19165 const char *name = 0;
19167 if (TYPE_NAME (type) != 0)
19169 tree t = 0;
19171 /* Find the IDENTIFIER_NODE for the type name. */
19172 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19173 && !TYPE_NAMELESS (type))
19174 t = TYPE_NAME (type);
19176 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19177 a TYPE_DECL node, regardless of whether or not a `typedef' was
19178 involved. */
19179 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19180 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19182 /* We want to be extra verbose. Don't call dwarf_name if
19183 DECL_NAME isn't set. The default hook for decl_printable_name
19184 doesn't like that, and in this context it's correct to return
19185 0, instead of "<anonymous>" or the like. */
19186 if (DECL_NAME (TYPE_NAME (type))
19187 && !DECL_NAMELESS (TYPE_NAME (type)))
19188 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19191 /* Now get the name as a string, or invent one. */
19192 if (!name && t != 0)
19193 name = IDENTIFIER_POINTER (t);
19196 return (name == 0 || *name == '\0') ? 0 : name;
19199 /* Return the type associated with a data member, make a special check
19200 for bit field types. */
19202 static inline tree
19203 member_declared_type (const_tree member)
19205 return (DECL_BIT_FIELD_TYPE (member)
19206 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19209 /* Get the decl's label, as described by its RTL. This may be different
19210 from the DECL_NAME name used in the source file. */
19212 #if 0
19213 static const char *
19214 decl_start_label (tree decl)
19216 rtx x;
19217 const char *fnname;
19219 x = DECL_RTL (decl);
19220 gcc_assert (MEM_P (x));
19222 x = XEXP (x, 0);
19223 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19225 fnname = XSTR (x, 0);
19226 return fnname;
19228 #endif
19230 /* For variable-length arrays that have been previously generated, but
19231 may be incomplete due to missing subscript info, fill the subscript
19232 info. Return TRUE if this is one of those cases. */
19233 static bool
19234 fill_variable_array_bounds (tree type)
19236 if (TREE_ASM_WRITTEN (type)
19237 && TREE_CODE (type) == ARRAY_TYPE
19238 && variably_modified_type_p (type, NULL))
19240 dw_die_ref array_die = lookup_type_die (type);
19241 if (!array_die)
19242 return false;
19243 add_subscript_info (array_die, type, !is_ada ());
19244 return true;
19246 return false;
19249 /* These routines generate the internal representation of the DIE's for
19250 the compilation unit. Debugging information is collected by walking
19251 the declaration trees passed in from dwarf2out_decl(). */
19253 static void
19254 gen_array_type_die (tree type, dw_die_ref context_die)
19256 dw_die_ref array_die;
19258 /* GNU compilers represent multidimensional array types as sequences of one
19259 dimensional array types whose element types are themselves array types.
19260 We sometimes squish that down to a single array_type DIE with multiple
19261 subscripts in the Dwarf debugging info. The draft Dwarf specification
19262 say that we are allowed to do this kind of compression in C, because
19263 there is no difference between an array of arrays and a multidimensional
19264 array. We don't do this for Ada to remain as close as possible to the
19265 actual representation, which is especially important against the language
19266 flexibilty wrt arrays of variable size. */
19268 bool collapse_nested_arrays = !is_ada ();
19270 if (fill_variable_array_bounds (type))
19271 return;
19273 dw_die_ref scope_die = scope_die_for (type, context_die);
19274 tree element_type;
19276 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19277 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19278 if (TYPE_STRING_FLAG (type)
19279 && TREE_CODE (type) == ARRAY_TYPE
19280 && is_fortran ()
19281 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19283 HOST_WIDE_INT size;
19285 array_die = new_die (DW_TAG_string_type, scope_die, type);
19286 add_name_attribute (array_die, type_tag (type));
19287 equate_type_number_to_die (type, array_die);
19288 size = int_size_in_bytes (type);
19289 if (size >= 0)
19290 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19291 else if (TYPE_DOMAIN (type) != NULL_TREE
19292 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19294 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19295 tree rszdecl = szdecl;
19296 HOST_WIDE_INT rsize = 0;
19298 size = int_size_in_bytes (TREE_TYPE (szdecl));
19299 if (!DECL_P (szdecl))
19301 if (TREE_CODE (szdecl) == INDIRECT_REF
19302 && DECL_P (TREE_OPERAND (szdecl, 0)))
19304 rszdecl = TREE_OPERAND (szdecl, 0);
19305 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19306 if (rsize <= 0)
19307 size = 0;
19309 else
19310 size = 0;
19312 if (size > 0)
19314 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19315 if (loc == NULL
19316 && early_dwarf
19317 && current_function_decl
19318 && DECL_CONTEXT (rszdecl) == current_function_decl)
19320 dw_die_ref ref = lookup_decl_die (rszdecl);
19321 dw_loc_descr_ref l = NULL;
19322 if (ref)
19324 l = new_loc_descr (DW_OP_call4, 0, 0);
19325 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19326 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19327 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19329 else if (TREE_CODE (rszdecl) == PARM_DECL
19330 && string_types)
19332 l = new_loc_descr (DW_OP_call4, 0, 0);
19333 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19334 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19335 string_types->safe_push (array_die);
19337 if (l && rszdecl != szdecl)
19339 if (rsize == DWARF2_ADDR_SIZE)
19340 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19341 0, 0));
19342 else
19343 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19344 rsize, 0));
19346 if (l)
19347 loc = new_loc_list (l, NULL, NULL, NULL);
19349 if (loc)
19351 add_AT_location_description (array_die, DW_AT_string_length,
19352 loc);
19353 if (size != DWARF2_ADDR_SIZE)
19354 add_AT_unsigned (array_die, dwarf_version >= 5
19355 ? DW_AT_string_length_byte_size
19356 : DW_AT_byte_size, size);
19360 return;
19363 array_die = new_die (DW_TAG_array_type, scope_die, type);
19364 add_name_attribute (array_die, type_tag (type));
19365 equate_type_number_to_die (type, array_die);
19367 if (TREE_CODE (type) == VECTOR_TYPE)
19368 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19370 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19371 if (is_fortran ()
19372 && TREE_CODE (type) == ARRAY_TYPE
19373 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19374 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19375 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19377 #if 0
19378 /* We default the array ordering. SDB will probably do
19379 the right things even if DW_AT_ordering is not present. It's not even
19380 an issue until we start to get into multidimensional arrays anyway. If
19381 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19382 then we'll have to put the DW_AT_ordering attribute back in. (But if
19383 and when we find out that we need to put these in, we will only do so
19384 for multidimensional arrays. */
19385 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19386 #endif
19388 if (TREE_CODE (type) == VECTOR_TYPE)
19390 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19391 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19392 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19393 add_bound_info (subrange_die, DW_AT_upper_bound,
19394 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19396 else
19397 add_subscript_info (array_die, type, collapse_nested_arrays);
19399 /* Add representation of the type of the elements of this array type and
19400 emit the corresponding DIE if we haven't done it already. */
19401 element_type = TREE_TYPE (type);
19402 if (collapse_nested_arrays)
19403 while (TREE_CODE (element_type) == ARRAY_TYPE)
19405 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19406 break;
19407 element_type = TREE_TYPE (element_type);
19410 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19411 TREE_CODE (type) == ARRAY_TYPE
19412 && TYPE_REVERSE_STORAGE_ORDER (type),
19413 context_die);
19415 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19416 if (TYPE_ARTIFICIAL (type))
19417 add_AT_flag (array_die, DW_AT_artificial, 1);
19419 if (get_AT (array_die, DW_AT_name))
19420 add_pubtype (type, array_die);
19423 /* After all arguments are created, adjust any DW_TAG_string_type
19424 DIEs DW_AT_string_length attributes. */
19426 static void
19427 adjust_string_types (void)
19429 dw_die_ref array_die;
19430 unsigned int i;
19431 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19433 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19434 if (a == NULL)
19435 continue;
19436 dw_loc_descr_ref loc = AT_loc (a);
19437 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19438 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19439 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19440 if (ref)
19442 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19443 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19444 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19446 else
19448 remove_AT (array_die, DW_AT_string_length);
19449 remove_AT (array_die, dwarf_version >= 5
19450 ? DW_AT_string_length_byte_size
19451 : DW_AT_byte_size);
19456 /* This routine generates DIE for array with hidden descriptor, details
19457 are filled into *info by a langhook. */
19459 static void
19460 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19461 dw_die_ref context_die)
19463 const dw_die_ref scope_die = scope_die_for (type, context_die);
19464 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19465 const struct loc_descr_context context = { type, info->base_decl, NULL };
19466 int dim;
19468 add_name_attribute (array_die, type_tag (type));
19469 equate_type_number_to_die (type, array_die);
19471 if (info->ndimensions > 1)
19472 switch (info->ordering)
19474 case array_descr_ordering_row_major:
19475 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19476 break;
19477 case array_descr_ordering_column_major:
19478 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19479 break;
19480 default:
19481 break;
19484 if (dwarf_version >= 3 || !dwarf_strict)
19486 if (info->data_location)
19487 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19488 dw_scalar_form_exprloc, &context);
19489 if (info->associated)
19490 add_scalar_info (array_die, DW_AT_associated, info->associated,
19491 dw_scalar_form_constant
19492 | dw_scalar_form_exprloc
19493 | dw_scalar_form_reference, &context);
19494 if (info->allocated)
19495 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19496 dw_scalar_form_constant
19497 | dw_scalar_form_exprloc
19498 | dw_scalar_form_reference, &context);
19499 if (info->stride)
19501 const enum dwarf_attribute attr
19502 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19503 const int forms
19504 = (info->stride_in_bits)
19505 ? dw_scalar_form_constant
19506 : (dw_scalar_form_constant
19507 | dw_scalar_form_exprloc
19508 | dw_scalar_form_reference);
19510 add_scalar_info (array_die, attr, info->stride, forms, &context);
19514 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19516 for (dim = 0; dim < info->ndimensions; dim++)
19518 dw_die_ref subrange_die
19519 = new_die (DW_TAG_subrange_type, array_die, NULL);
19521 if (info->dimen[dim].bounds_type)
19522 add_type_attribute (subrange_die,
19523 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19524 false, context_die);
19525 if (info->dimen[dim].lower_bound)
19526 add_bound_info (subrange_die, DW_AT_lower_bound,
19527 info->dimen[dim].lower_bound, &context);
19528 if (info->dimen[dim].upper_bound)
19529 add_bound_info (subrange_die, DW_AT_upper_bound,
19530 info->dimen[dim].upper_bound, &context);
19531 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19532 add_scalar_info (subrange_die, DW_AT_byte_stride,
19533 info->dimen[dim].stride,
19534 dw_scalar_form_constant
19535 | dw_scalar_form_exprloc
19536 | dw_scalar_form_reference,
19537 &context);
19540 gen_type_die (info->element_type, context_die);
19541 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19542 TREE_CODE (type) == ARRAY_TYPE
19543 && TYPE_REVERSE_STORAGE_ORDER (type),
19544 context_die);
19546 if (get_AT (array_die, DW_AT_name))
19547 add_pubtype (type, array_die);
19550 #if 0
19551 static void
19552 gen_entry_point_die (tree decl, dw_die_ref context_die)
19554 tree origin = decl_ultimate_origin (decl);
19555 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19557 if (origin != NULL)
19558 add_abstract_origin_attribute (decl_die, origin);
19559 else
19561 add_name_and_src_coords_attributes (decl_die, decl);
19562 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19563 TYPE_UNQUALIFIED, false, context_die);
19566 if (DECL_ABSTRACT_P (decl))
19567 equate_decl_number_to_die (decl, decl_die);
19568 else
19569 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19571 #endif
19573 /* Walk through the list of incomplete types again, trying once more to
19574 emit full debugging info for them. */
19576 static void
19577 retry_incomplete_types (void)
19579 set_early_dwarf s;
19580 int i;
19582 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19583 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19584 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19585 vec_safe_truncate (incomplete_types, 0);
19588 /* Determine what tag to use for a record type. */
19590 static enum dwarf_tag
19591 record_type_tag (tree type)
19593 if (! lang_hooks.types.classify_record)
19594 return DW_TAG_structure_type;
19596 switch (lang_hooks.types.classify_record (type))
19598 case RECORD_IS_STRUCT:
19599 return DW_TAG_structure_type;
19601 case RECORD_IS_CLASS:
19602 return DW_TAG_class_type;
19604 case RECORD_IS_INTERFACE:
19605 if (dwarf_version >= 3 || !dwarf_strict)
19606 return DW_TAG_interface_type;
19607 return DW_TAG_structure_type;
19609 default:
19610 gcc_unreachable ();
19614 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19615 include all of the information about the enumeration values also. Each
19616 enumerated type name/value is listed as a child of the enumerated type
19617 DIE. */
19619 static dw_die_ref
19620 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19622 dw_die_ref type_die = lookup_type_die (type);
19624 if (type_die == NULL)
19626 type_die = new_die (DW_TAG_enumeration_type,
19627 scope_die_for (type, context_die), type);
19628 equate_type_number_to_die (type, type_die);
19629 add_name_attribute (type_die, type_tag (type));
19630 if (dwarf_version >= 4 || !dwarf_strict)
19632 if (ENUM_IS_SCOPED (type))
19633 add_AT_flag (type_die, DW_AT_enum_class, 1);
19634 if (ENUM_IS_OPAQUE (type))
19635 add_AT_flag (type_die, DW_AT_declaration, 1);
19638 else if (! TYPE_SIZE (type))
19639 return type_die;
19640 else
19641 remove_AT (type_die, DW_AT_declaration);
19643 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19644 given enum type is incomplete, do not generate the DW_AT_byte_size
19645 attribute or the DW_AT_element_list attribute. */
19646 if (TYPE_SIZE (type))
19648 tree link;
19650 TREE_ASM_WRITTEN (type) = 1;
19651 add_byte_size_attribute (type_die, type);
19652 if (dwarf_version >= 3 || !dwarf_strict)
19654 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19655 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19656 context_die);
19658 if (TYPE_STUB_DECL (type) != NULL_TREE)
19660 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19661 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19664 /* If the first reference to this type was as the return type of an
19665 inline function, then it may not have a parent. Fix this now. */
19666 if (type_die->die_parent == NULL)
19667 add_child_die (scope_die_for (type, context_die), type_die);
19669 for (link = TYPE_VALUES (type);
19670 link != NULL; link = TREE_CHAIN (link))
19672 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19673 tree value = TREE_VALUE (link);
19675 add_name_attribute (enum_die,
19676 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19678 if (TREE_CODE (value) == CONST_DECL)
19679 value = DECL_INITIAL (value);
19681 if (simple_type_size_in_bits (TREE_TYPE (value))
19682 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19684 /* For constant forms created by add_AT_unsigned DWARF
19685 consumers (GDB, elfutils, etc.) always zero extend
19686 the value. Only when the actual value is negative
19687 do we need to use add_AT_int to generate a constant
19688 form that can represent negative values. */
19689 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19690 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19691 add_AT_unsigned (enum_die, DW_AT_const_value,
19692 (unsigned HOST_WIDE_INT) val);
19693 else
19694 add_AT_int (enum_die, DW_AT_const_value, val);
19696 else
19697 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19698 that here. TODO: This should be re-worked to use correct
19699 signed/unsigned double tags for all cases. */
19700 add_AT_wide (enum_die, DW_AT_const_value, value);
19703 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19704 if (TYPE_ARTIFICIAL (type))
19705 add_AT_flag (type_die, DW_AT_artificial, 1);
19707 else
19708 add_AT_flag (type_die, DW_AT_declaration, 1);
19710 add_pubtype (type, type_die);
19712 return type_die;
19715 /* Generate a DIE to represent either a real live formal parameter decl or to
19716 represent just the type of some formal parameter position in some function
19717 type.
19719 Note that this routine is a bit unusual because its argument may be a
19720 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19721 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19722 node. If it's the former then this function is being called to output a
19723 DIE to represent a formal parameter object (or some inlining thereof). If
19724 it's the latter, then this function is only being called to output a
19725 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19726 argument type of some subprogram type.
19727 If EMIT_NAME_P is true, name and source coordinate attributes
19728 are emitted. */
19730 static dw_die_ref
19731 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19732 dw_die_ref context_die)
19734 tree node_or_origin = node ? node : origin;
19735 tree ultimate_origin;
19736 dw_die_ref parm_die = NULL;
19738 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19740 parm_die = lookup_decl_die (node);
19742 /* If the contexts differ, we may not be talking about the same
19743 thing. */
19744 if (parm_die && parm_die->die_parent != context_die)
19746 if (!DECL_ABSTRACT_P (node))
19748 /* This can happen when creating an inlined instance, in
19749 which case we need to create a new DIE that will get
19750 annotated with DW_AT_abstract_origin. */
19751 parm_die = NULL;
19753 else
19755 /* FIXME: Reuse DIE even with a differing context.
19757 This can happen when calling
19758 dwarf2out_abstract_function to build debug info for
19759 the abstract instance of a function for which we have
19760 already generated a DIE in
19761 dwarf2out_early_global_decl.
19763 Once we remove dwarf2out_abstract_function, we should
19764 have a call to gcc_unreachable here. */
19768 if (parm_die && parm_die->die_parent == NULL)
19770 /* Check that parm_die already has the right attributes that
19771 we would have added below. If any attributes are
19772 missing, fall through to add them. */
19773 if (! DECL_ABSTRACT_P (node_or_origin)
19774 && !get_AT (parm_die, DW_AT_location)
19775 && !get_AT (parm_die, DW_AT_const_value))
19776 /* We are missing location info, and are about to add it. */
19778 else
19780 add_child_die (context_die, parm_die);
19781 return parm_die;
19786 /* If we have a previously generated DIE, use it, unless this is an
19787 concrete instance (origin != NULL), in which case we need a new
19788 DIE with a corresponding DW_AT_abstract_origin. */
19789 bool reusing_die;
19790 if (parm_die && origin == NULL)
19791 reusing_die = true;
19792 else
19794 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19795 reusing_die = false;
19798 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19800 case tcc_declaration:
19801 ultimate_origin = decl_ultimate_origin (node_or_origin);
19802 if (node || ultimate_origin)
19803 origin = ultimate_origin;
19805 if (reusing_die)
19806 goto add_location;
19808 if (origin != NULL)
19809 add_abstract_origin_attribute (parm_die, origin);
19810 else if (emit_name_p)
19811 add_name_and_src_coords_attributes (parm_die, node);
19812 if (origin == NULL
19813 || (! DECL_ABSTRACT_P (node_or_origin)
19814 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19815 decl_function_context
19816 (node_or_origin))))
19818 tree type = TREE_TYPE (node_or_origin);
19819 if (decl_by_reference_p (node_or_origin))
19820 add_type_attribute (parm_die, TREE_TYPE (type),
19821 TYPE_UNQUALIFIED,
19822 false, context_die);
19823 else
19824 add_type_attribute (parm_die, type,
19825 decl_quals (node_or_origin),
19826 false, context_die);
19828 if (origin == NULL && DECL_ARTIFICIAL (node))
19829 add_AT_flag (parm_die, DW_AT_artificial, 1);
19830 add_location:
19831 if (node && node != origin)
19832 equate_decl_number_to_die (node, parm_die);
19833 if (! DECL_ABSTRACT_P (node_or_origin))
19834 add_location_or_const_value_attribute (parm_die, node_or_origin,
19835 node == NULL);
19837 break;
19839 case tcc_type:
19840 /* We were called with some kind of a ..._TYPE node. */
19841 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19842 context_die);
19843 break;
19845 default:
19846 gcc_unreachable ();
19849 return parm_die;
19852 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19853 children DW_TAG_formal_parameter DIEs representing the arguments of the
19854 parameter pack.
19856 PARM_PACK must be a function parameter pack.
19857 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19858 must point to the subsequent arguments of the function PACK_ARG belongs to.
19859 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19860 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19861 following the last one for which a DIE was generated. */
19863 static dw_die_ref
19864 gen_formal_parameter_pack_die (tree parm_pack,
19865 tree pack_arg,
19866 dw_die_ref subr_die,
19867 tree *next_arg)
19869 tree arg;
19870 dw_die_ref parm_pack_die;
19872 gcc_assert (parm_pack
19873 && lang_hooks.function_parameter_pack_p (parm_pack)
19874 && subr_die);
19876 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19877 add_src_coords_attributes (parm_pack_die, parm_pack);
19879 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19881 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19882 parm_pack))
19883 break;
19884 gen_formal_parameter_die (arg, NULL,
19885 false /* Don't emit name attribute. */,
19886 parm_pack_die);
19888 if (next_arg)
19889 *next_arg = arg;
19890 return parm_pack_die;
19893 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19894 at the end of an (ANSI prototyped) formal parameters list. */
19896 static void
19897 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19899 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19902 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19903 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19904 parameters as specified in some function type specification (except for
19905 those which appear as part of a function *definition*). */
19907 static void
19908 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19910 tree link;
19911 tree formal_type = NULL;
19912 tree first_parm_type;
19913 tree arg;
19915 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19917 arg = DECL_ARGUMENTS (function_or_method_type);
19918 function_or_method_type = TREE_TYPE (function_or_method_type);
19920 else
19921 arg = NULL_TREE;
19923 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19925 /* Make our first pass over the list of formal parameter types and output a
19926 DW_TAG_formal_parameter DIE for each one. */
19927 for (link = first_parm_type; link; )
19929 dw_die_ref parm_die;
19931 formal_type = TREE_VALUE (link);
19932 if (formal_type == void_type_node)
19933 break;
19935 /* Output a (nameless) DIE to represent the formal parameter itself. */
19936 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19938 parm_die = gen_formal_parameter_die (formal_type, NULL,
19939 true /* Emit name attribute. */,
19940 context_die);
19941 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19942 && link == first_parm_type)
19944 add_AT_flag (parm_die, DW_AT_artificial, 1);
19945 if (dwarf_version >= 3 || !dwarf_strict)
19946 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19948 else if (arg && DECL_ARTIFICIAL (arg))
19949 add_AT_flag (parm_die, DW_AT_artificial, 1);
19952 link = TREE_CHAIN (link);
19953 if (arg)
19954 arg = DECL_CHAIN (arg);
19957 /* If this function type has an ellipsis, add a
19958 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19959 if (formal_type != void_type_node)
19960 gen_unspecified_parameters_die (function_or_method_type, context_die);
19962 /* Make our second (and final) pass over the list of formal parameter types
19963 and output DIEs to represent those types (as necessary). */
19964 for (link = TYPE_ARG_TYPES (function_or_method_type);
19965 link && TREE_VALUE (link);
19966 link = TREE_CHAIN (link))
19967 gen_type_die (TREE_VALUE (link), context_die);
19970 /* We want to generate the DIE for TYPE so that we can generate the
19971 die for MEMBER, which has been defined; we will need to refer back
19972 to the member declaration nested within TYPE. If we're trying to
19973 generate minimal debug info for TYPE, processing TYPE won't do the
19974 trick; we need to attach the member declaration by hand. */
19976 static void
19977 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19979 gen_type_die (type, context_die);
19981 /* If we're trying to avoid duplicate debug info, we may not have
19982 emitted the member decl for this function. Emit it now. */
19983 if (TYPE_STUB_DECL (type)
19984 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19985 && ! lookup_decl_die (member))
19987 dw_die_ref type_die;
19988 gcc_assert (!decl_ultimate_origin (member));
19990 push_decl_scope (type);
19991 type_die = lookup_type_die_strip_naming_typedef (type);
19992 if (TREE_CODE (member) == FUNCTION_DECL)
19993 gen_subprogram_die (member, type_die);
19994 else if (TREE_CODE (member) == FIELD_DECL)
19996 /* Ignore the nameless fields that are used to skip bits but handle
19997 C++ anonymous unions and structs. */
19998 if (DECL_NAME (member) != NULL_TREE
19999 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20000 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20002 struct vlr_context vlr_ctx = {
20003 DECL_CONTEXT (member), /* struct_type */
20004 NULL_TREE /* variant_part_offset */
20006 gen_type_die (member_declared_type (member), type_die);
20007 gen_field_die (member, &vlr_ctx, type_die);
20010 else
20011 gen_variable_die (member, NULL_TREE, type_die);
20013 pop_decl_scope ();
20017 /* Forward declare these functions, because they are mutually recursive
20018 with their set_block_* pairing functions. */
20019 static void set_decl_origin_self (tree);
20020 static void set_decl_abstract_flags (tree, vec<tree> &);
20022 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20023 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20024 that it points to the node itself, thus indicating that the node is its
20025 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20026 the given node is NULL, recursively descend the decl/block tree which
20027 it is the root of, and for each other ..._DECL or BLOCK node contained
20028 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20029 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20030 values to point to themselves. */
20032 static void
20033 set_block_origin_self (tree stmt)
20035 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20037 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20040 tree local_decl;
20042 for (local_decl = BLOCK_VARS (stmt);
20043 local_decl != NULL_TREE;
20044 local_decl = DECL_CHAIN (local_decl))
20045 /* Do not recurse on nested functions since the inlining status
20046 of parent and child can be different as per the DWARF spec. */
20047 if (TREE_CODE (local_decl) != FUNCTION_DECL
20048 && !DECL_EXTERNAL (local_decl))
20049 set_decl_origin_self (local_decl);
20053 tree subblock;
20055 for (subblock = BLOCK_SUBBLOCKS (stmt);
20056 subblock != NULL_TREE;
20057 subblock = BLOCK_CHAIN (subblock))
20058 set_block_origin_self (subblock); /* Recurse. */
20063 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20064 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20065 node to so that it points to the node itself, thus indicating that the
20066 node represents its own (abstract) origin. Additionally, if the
20067 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20068 the decl/block tree of which the given node is the root of, and for
20069 each other ..._DECL or BLOCK node contained therein whose
20070 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20071 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20072 point to themselves. */
20074 static void
20075 set_decl_origin_self (tree decl)
20077 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20079 DECL_ABSTRACT_ORIGIN (decl) = decl;
20080 if (TREE_CODE (decl) == FUNCTION_DECL)
20082 tree arg;
20084 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20085 DECL_ABSTRACT_ORIGIN (arg) = arg;
20086 if (DECL_INITIAL (decl) != NULL_TREE
20087 && DECL_INITIAL (decl) != error_mark_node)
20088 set_block_origin_self (DECL_INITIAL (decl));
20093 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20094 and if it wasn't 1 before, push it to abstract_vec vector.
20095 For all local decls and all local sub-blocks (recursively) do it
20096 too. */
20098 static void
20099 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20101 tree local_decl;
20102 tree subblock;
20103 unsigned int i;
20105 if (!BLOCK_ABSTRACT (stmt))
20107 abstract_vec.safe_push (stmt);
20108 BLOCK_ABSTRACT (stmt) = 1;
20111 for (local_decl = BLOCK_VARS (stmt);
20112 local_decl != NULL_TREE;
20113 local_decl = DECL_CHAIN (local_decl))
20114 if (! DECL_EXTERNAL (local_decl))
20115 set_decl_abstract_flags (local_decl, abstract_vec);
20117 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20119 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20120 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
20121 || TREE_CODE (local_decl) == PARM_DECL)
20122 set_decl_abstract_flags (local_decl, abstract_vec);
20125 for (subblock = BLOCK_SUBBLOCKS (stmt);
20126 subblock != NULL_TREE;
20127 subblock = BLOCK_CHAIN (subblock))
20128 set_block_abstract_flags (subblock, abstract_vec);
20131 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20132 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20133 In the case where the decl is a FUNCTION_DECL also set the abstract
20134 flags for all of the parameters, local vars, local
20135 blocks and sub-blocks (recursively). */
20137 static void
20138 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20140 if (!DECL_ABSTRACT_P (decl))
20142 abstract_vec.safe_push (decl);
20143 DECL_ABSTRACT_P (decl) = 1;
20146 if (TREE_CODE (decl) == FUNCTION_DECL)
20148 tree arg;
20150 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20151 if (!DECL_ABSTRACT_P (arg))
20153 abstract_vec.safe_push (arg);
20154 DECL_ABSTRACT_P (arg) = 1;
20156 if (DECL_INITIAL (decl) != NULL_TREE
20157 && DECL_INITIAL (decl) != error_mark_node)
20158 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20162 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20163 may later generate inlined and/or out-of-line instances of.
20165 FIXME: In the early-dwarf world, this function, and most of the
20166 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20167 the abstract instance. All we would need to do is annotate
20168 the early DIE with the appropriate DW_AT_inline in late
20169 dwarf (perhaps in gen_inlined_subroutine_die).
20171 However, we can't do this yet, because LTO streaming of DIEs
20172 has not been implemented yet. */
20174 static void
20175 dwarf2out_abstract_function (tree decl)
20177 dw_die_ref old_die;
20178 tree save_fn;
20179 tree context;
20180 hash_table<decl_loc_hasher> *old_decl_loc_table;
20181 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20182 int old_call_site_count, old_tail_call_site_count;
20183 struct call_arg_loc_node *old_call_arg_locations;
20185 /* Make sure we have the actual abstract inline, not a clone. */
20186 decl = DECL_ORIGIN (decl);
20188 old_die = lookup_decl_die (decl);
20189 if (old_die && get_AT (old_die, DW_AT_inline))
20190 /* We've already generated the abstract instance. */
20191 return;
20193 /* We can be called while recursively when seeing block defining inlined subroutine
20194 DIE. Be sure to not clobber the outer location table nor use it or we would
20195 get locations in abstract instantces. */
20196 old_decl_loc_table = decl_loc_table;
20197 decl_loc_table = NULL;
20198 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20199 cached_dw_loc_list_table = NULL;
20200 old_call_arg_locations = call_arg_locations;
20201 call_arg_locations = NULL;
20202 old_call_site_count = call_site_count;
20203 call_site_count = -1;
20204 old_tail_call_site_count = tail_call_site_count;
20205 tail_call_site_count = -1;
20207 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20208 we don't get confused by DECL_ABSTRACT_P. */
20209 if (debug_info_level > DINFO_LEVEL_TERSE)
20211 context = decl_class_context (decl);
20212 if (context)
20213 gen_type_die_for_member
20214 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20217 /* Pretend we've just finished compiling this function. */
20218 save_fn = current_function_decl;
20219 current_function_decl = decl;
20221 auto_vec<tree, 64> abstract_vec;
20222 set_decl_abstract_flags (decl, abstract_vec);
20223 dwarf2out_decl (decl);
20224 unsigned int i;
20225 tree t;
20226 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20227 if (TREE_CODE (t) == BLOCK)
20228 BLOCK_ABSTRACT (t) = 0;
20229 else
20230 DECL_ABSTRACT_P (t) = 0;
20232 current_function_decl = save_fn;
20233 decl_loc_table = old_decl_loc_table;
20234 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20235 call_arg_locations = old_call_arg_locations;
20236 call_site_count = old_call_site_count;
20237 tail_call_site_count = old_tail_call_site_count;
20240 /* Helper function of premark_used_types() which gets called through
20241 htab_traverse.
20243 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20244 marked as unused by prune_unused_types. */
20246 bool
20247 premark_used_types_helper (tree const &type, void *)
20249 dw_die_ref die;
20251 die = lookup_type_die (type);
20252 if (die != NULL)
20253 die->die_perennial_p = 1;
20254 return true;
20257 /* Helper function of premark_types_used_by_global_vars which gets called
20258 through htab_traverse.
20260 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20261 marked as unused by prune_unused_types. The DIE of the type is marked
20262 only if the global variable using the type will actually be emitted. */
20265 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20266 void *)
20268 struct types_used_by_vars_entry *entry;
20269 dw_die_ref die;
20271 entry = (struct types_used_by_vars_entry *) *slot;
20272 gcc_assert (entry->type != NULL
20273 && entry->var_decl != NULL);
20274 die = lookup_type_die (entry->type);
20275 if (die)
20277 /* Ask cgraph if the global variable really is to be emitted.
20278 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20279 varpool_node *node = varpool_node::get (entry->var_decl);
20280 if (node && node->definition)
20282 die->die_perennial_p = 1;
20283 /* Keep the parent DIEs as well. */
20284 while ((die = die->die_parent) && die->die_perennial_p == 0)
20285 die->die_perennial_p = 1;
20288 return 1;
20291 /* Mark all members of used_types_hash as perennial. */
20293 static void
20294 premark_used_types (struct function *fun)
20296 if (fun && fun->used_types_hash)
20297 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20300 /* Mark all members of types_used_by_vars_entry as perennial. */
20302 static void
20303 premark_types_used_by_global_vars (void)
20305 if (types_used_by_vars_hash)
20306 types_used_by_vars_hash
20307 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20310 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20311 for CA_LOC call arg loc node. */
20313 static dw_die_ref
20314 gen_call_site_die (tree decl, dw_die_ref subr_die,
20315 struct call_arg_loc_node *ca_loc)
20317 dw_die_ref stmt_die = NULL, die;
20318 tree block = ca_loc->block;
20320 while (block
20321 && block != DECL_INITIAL (decl)
20322 && TREE_CODE (block) == BLOCK)
20324 stmt_die = BLOCK_DIE (block);
20325 if (stmt_die)
20326 break;
20327 block = BLOCK_SUPERCONTEXT (block);
20329 if (stmt_die == NULL)
20330 stmt_die = subr_die;
20331 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20332 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20333 if (ca_loc->tail_call_p)
20334 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20335 if (ca_loc->symbol_ref)
20337 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20338 if (tdie)
20339 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20340 else
20341 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20343 return die;
20346 /* Generate a DIE to represent a declared function (either file-scope or
20347 block-local). */
20349 static void
20350 gen_subprogram_die (tree decl, dw_die_ref context_die)
20352 tree origin = decl_ultimate_origin (decl);
20353 dw_die_ref subr_die;
20354 dw_die_ref old_die = lookup_decl_die (decl);
20356 /* This function gets called multiple times for different stages of
20357 the debug process. For example, for func() in this code:
20359 namespace S
20361 void func() { ... }
20364 ...we get called 4 times. Twice in early debug and twice in
20365 late debug:
20367 Early debug
20368 -----------
20370 1. Once while generating func() within the namespace. This is
20371 the declaration. The declaration bit below is set, as the
20372 context is the namespace.
20374 A new DIE will be generated with DW_AT_declaration set.
20376 2. Once for func() itself. This is the specification. The
20377 declaration bit below is clear as the context is the CU.
20379 We will use the cached DIE from (1) to create a new DIE with
20380 DW_AT_specification pointing to the declaration in (1).
20382 Late debug via rest_of_handle_final()
20383 -------------------------------------
20385 3. Once generating func() within the namespace. This is also the
20386 declaration, as in (1), but this time we will early exit below
20387 as we have a cached DIE and a declaration needs no additional
20388 annotations (no locations), as the source declaration line
20389 info is enough.
20391 4. Once for func() itself. As in (2), this is the specification,
20392 but this time we will re-use the cached DIE, and just annotate
20393 it with the location information that should now be available.
20395 For something without namespaces, but with abstract instances, we
20396 are also called a multiple times:
20398 class Base
20400 public:
20401 Base (); // constructor declaration (1)
20404 Base::Base () { } // constructor specification (2)
20406 Early debug
20407 -----------
20409 1. Once for the Base() constructor by virtue of it being a
20410 member of the Base class. This is done via
20411 rest_of_type_compilation.
20413 This is a declaration, so a new DIE will be created with
20414 DW_AT_declaration.
20416 2. Once for the Base() constructor definition, but this time
20417 while generating the abstract instance of the base
20418 constructor (__base_ctor) which is being generated via early
20419 debug of reachable functions.
20421 Even though we have a cached version of the declaration (1),
20422 we will create a DW_AT_specification of the declaration DIE
20423 in (1).
20425 3. Once for the __base_ctor itself, but this time, we generate
20426 an DW_AT_abstract_origin version of the DW_AT_specification in
20427 (2).
20429 Late debug via rest_of_handle_final
20430 -----------------------------------
20432 4. One final time for the __base_ctor (which will have a cached
20433 DIE with DW_AT_abstract_origin created in (3). This time,
20434 we will just annotate the location information now
20435 available.
20437 int declaration = (current_function_decl != decl
20438 || class_or_namespace_scope_p (context_die));
20440 /* Now that the C++ front end lazily declares artificial member fns, we
20441 might need to retrofit the declaration into its class. */
20442 if (!declaration && !origin && !old_die
20443 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20444 && !class_or_namespace_scope_p (context_die)
20445 && debug_info_level > DINFO_LEVEL_TERSE)
20446 old_die = force_decl_die (decl);
20448 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20449 if (origin != NULL)
20451 gcc_assert (!declaration || local_scope_p (context_die));
20453 /* Fixup die_parent for the abstract instance of a nested
20454 inline function. */
20455 if (old_die && old_die->die_parent == NULL)
20456 add_child_die (context_die, old_die);
20458 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20460 /* If we have a DW_AT_abstract_origin we have a working
20461 cached version. */
20462 subr_die = old_die;
20464 else
20466 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20467 add_abstract_origin_attribute (subr_die, origin);
20468 /* This is where the actual code for a cloned function is.
20469 Let's emit linkage name attribute for it. This helps
20470 debuggers to e.g, set breakpoints into
20471 constructors/destructors when the user asks "break
20472 K::K". */
20473 add_linkage_name (subr_die, decl);
20476 /* A cached copy, possibly from early dwarf generation. Reuse as
20477 much as possible. */
20478 else if (old_die)
20480 /* A declaration that has been previously dumped needs no
20481 additional information. */
20482 if (declaration)
20483 return;
20485 if (!get_AT_flag (old_die, DW_AT_declaration)
20486 /* We can have a normal definition following an inline one in the
20487 case of redefinition of GNU C extern inlines.
20488 It seems reasonable to use AT_specification in this case. */
20489 && !get_AT (old_die, DW_AT_inline))
20491 /* Detect and ignore this case, where we are trying to output
20492 something we have already output. */
20493 if (get_AT (old_die, DW_AT_low_pc)
20494 || get_AT (old_die, DW_AT_ranges))
20495 return;
20497 /* If we have no location information, this must be a
20498 partially generated DIE from early dwarf generation.
20499 Fall through and generate it. */
20502 /* If the definition comes from the same place as the declaration,
20503 maybe use the old DIE. We always want the DIE for this function
20504 that has the *_pc attributes to be under comp_unit_die so the
20505 debugger can find it. We also need to do this for abstract
20506 instances of inlines, since the spec requires the out-of-line copy
20507 to have the same parent. For local class methods, this doesn't
20508 apply; we just use the old DIE. */
20509 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20510 struct dwarf_file_data * file_index = lookup_filename (s.file);
20511 if ((is_cu_die (old_die->die_parent)
20512 /* This condition fixes the inconsistency/ICE with the
20513 following Fortran test (or some derivative thereof) while
20514 building libgfortran:
20516 module some_m
20517 contains
20518 logical function funky (FLAG)
20519 funky = .true.
20520 end function
20521 end module
20523 || (old_die->die_parent
20524 && old_die->die_parent->die_tag == DW_TAG_module)
20525 || context_die == NULL)
20526 && (DECL_ARTIFICIAL (decl)
20527 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20528 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20529 == (unsigned) s.line))))
20531 subr_die = old_die;
20533 /* Clear out the declaration attribute, but leave the
20534 parameters so they can be augmented with location
20535 information later. Unless this was a declaration, in
20536 which case, wipe out the nameless parameters and recreate
20537 them further down. */
20538 if (remove_AT (subr_die, DW_AT_declaration))
20541 remove_AT (subr_die, DW_AT_object_pointer);
20542 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20545 /* Make a specification pointing to the previously built
20546 declaration. */
20547 else
20549 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20550 add_AT_specification (subr_die, old_die);
20551 add_pubname (decl, subr_die);
20552 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20553 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20554 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20555 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20557 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20558 emit the real type on the definition die. */
20559 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20561 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20562 if (die == auto_die || die == decltype_auto_die)
20563 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20564 TYPE_UNQUALIFIED, false, context_die);
20567 /* When we process the method declaration, we haven't seen
20568 the out-of-class defaulted definition yet, so we have to
20569 recheck now. */
20570 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20571 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20572 && !get_AT (subr_die, DW_AT_defaulted))
20573 switch (defaulted)
20575 case 2:
20576 add_AT_unsigned (subr_die, DW_AT_defaulted,
20577 DW_DEFAULTED_out_of_class);
20578 break;
20580 case 1: /* This must have been handled before. */
20581 default:
20582 gcc_unreachable ();
20586 /* Create a fresh DIE for anything else. */
20587 else
20589 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20591 if (TREE_PUBLIC (decl))
20592 add_AT_flag (subr_die, DW_AT_external, 1);
20594 add_name_and_src_coords_attributes (subr_die, decl);
20595 add_pubname (decl, subr_die);
20596 if (debug_info_level > DINFO_LEVEL_TERSE)
20598 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20599 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20600 TYPE_UNQUALIFIED, false, context_die);
20603 add_pure_or_virtual_attribute (subr_die, decl);
20604 if (DECL_ARTIFICIAL (decl))
20605 add_AT_flag (subr_die, DW_AT_artificial, 1);
20607 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20608 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20610 add_accessibility_attribute (subr_die, decl);
20613 /* Unless we have an existing non-declaration DIE, equate the new
20614 DIE. */
20615 if (!old_die || is_declaration_die (old_die))
20616 equate_decl_number_to_die (decl, subr_die);
20618 if (declaration)
20620 if (!old_die || !get_AT (old_die, DW_AT_inline))
20622 add_AT_flag (subr_die, DW_AT_declaration, 1);
20624 /* If this is an explicit function declaration then generate
20625 a DW_AT_explicit attribute. */
20626 if (lang_hooks.decls.function_decl_explicit_p (decl)
20627 && (dwarf_version >= 3 || !dwarf_strict))
20628 add_AT_flag (subr_die, DW_AT_explicit, 1);
20630 /* If this is a C++11 deleted special function member then generate
20631 a DW_AT_deleted attribute. */
20632 if (lang_hooks.decls.function_decl_deleted_p (decl)
20633 && (dwarf_version >= 5 || ! dwarf_strict))
20634 add_AT_flag (subr_die, DW_AT_deleted, 1);
20636 /* If this is a C++11 defaulted special function member then
20637 generate a DW_AT_GNU_defaulted attribute. */
20638 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20639 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20640 switch (defaulted)
20642 case 1:
20643 add_AT_unsigned (subr_die, DW_AT_defaulted,
20644 DW_DEFAULTED_in_class);
20645 break;
20647 /* It is likely that this will never hit, since we
20648 don't have the out-of-class definition yet when we
20649 process the class definition and the method
20650 declaration. We recheck elsewhere, but leave it
20651 here just in case. */
20652 case 2:
20653 add_AT_unsigned (subr_die, DW_AT_defaulted,
20654 DW_DEFAULTED_out_of_class);
20655 break;
20657 default:
20658 gcc_unreachable ();
20662 /* Tag abstract instances with DW_AT_inline. */
20663 else if (DECL_ABSTRACT_P (decl))
20665 if (DECL_DECLARED_INLINE_P (decl))
20667 if (cgraph_function_possibly_inlined_p (decl))
20668 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20669 else
20670 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20672 else
20674 if (cgraph_function_possibly_inlined_p (decl))
20675 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20676 else
20677 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20680 if (DECL_DECLARED_INLINE_P (decl)
20681 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20682 add_AT_flag (subr_die, DW_AT_artificial, 1);
20684 /* For non DECL_EXTERNALs, if range information is available, fill
20685 the DIE with it. */
20686 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20688 HOST_WIDE_INT cfa_fb_offset;
20690 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20692 if (!flag_reorder_blocks_and_partition)
20694 dw_fde_ref fde = fun->fde;
20695 if (fde->dw_fde_begin)
20697 /* We have already generated the labels. */
20698 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20699 fde->dw_fde_end, false);
20701 else
20703 /* Create start/end labels and add the range. */
20704 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20705 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20706 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20707 current_function_funcdef_no);
20708 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20709 current_function_funcdef_no);
20710 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20711 false);
20714 #if VMS_DEBUGGING_INFO
20715 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20716 Section 2.3 Prologue and Epilogue Attributes:
20717 When a breakpoint is set on entry to a function, it is generally
20718 desirable for execution to be suspended, not on the very first
20719 instruction of the function, but rather at a point after the
20720 function's frame has been set up, after any language defined local
20721 declaration processing has been completed, and before execution of
20722 the first statement of the function begins. Debuggers generally
20723 cannot properly determine where this point is. Similarly for a
20724 breakpoint set on exit from a function. The prologue and epilogue
20725 attributes allow a compiler to communicate the location(s) to use. */
20728 if (fde->dw_fde_vms_end_prologue)
20729 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20730 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20732 if (fde->dw_fde_vms_begin_epilogue)
20733 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20734 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20736 #endif
20739 else
20741 /* Generate pubnames entries for the split function code ranges. */
20742 dw_fde_ref fde = fun->fde;
20744 if (fde->dw_fde_second_begin)
20746 if (dwarf_version >= 3 || !dwarf_strict)
20748 /* We should use ranges for non-contiguous code section
20749 addresses. Use the actual code range for the initial
20750 section, since the HOT/COLD labels might precede an
20751 alignment offset. */
20752 bool range_list_added = false;
20753 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20754 fde->dw_fde_end, &range_list_added,
20755 false);
20756 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20757 fde->dw_fde_second_end,
20758 &range_list_added, false);
20759 if (range_list_added)
20760 add_ranges (NULL);
20762 else
20764 /* There is no real support in DW2 for this .. so we make
20765 a work-around. First, emit the pub name for the segment
20766 containing the function label. Then make and emit a
20767 simplified subprogram DIE for the second segment with the
20768 name pre-fixed by __hot/cold_sect_of_. We use the same
20769 linkage name for the second die so that gdb will find both
20770 sections when given "b foo". */
20771 const char *name = NULL;
20772 tree decl_name = DECL_NAME (decl);
20773 dw_die_ref seg_die;
20775 /* Do the 'primary' section. */
20776 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20777 fde->dw_fde_end, false);
20779 /* Build a minimal DIE for the secondary section. */
20780 seg_die = new_die (DW_TAG_subprogram,
20781 subr_die->die_parent, decl);
20783 if (TREE_PUBLIC (decl))
20784 add_AT_flag (seg_die, DW_AT_external, 1);
20786 if (decl_name != NULL
20787 && IDENTIFIER_POINTER (decl_name) != NULL)
20789 name = dwarf2_name (decl, 1);
20790 if (! DECL_ARTIFICIAL (decl))
20791 add_src_coords_attributes (seg_die, decl);
20793 add_linkage_name (seg_die, decl);
20795 gcc_assert (name != NULL);
20796 add_pure_or_virtual_attribute (seg_die, decl);
20797 if (DECL_ARTIFICIAL (decl))
20798 add_AT_flag (seg_die, DW_AT_artificial, 1);
20800 name = concat ("__second_sect_of_", name, NULL);
20801 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20802 fde->dw_fde_second_end, false);
20803 add_name_attribute (seg_die, name);
20804 if (want_pubnames ())
20805 add_pubname_string (name, seg_die);
20808 else
20809 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20810 false);
20813 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20815 /* We define the "frame base" as the function's CFA. This is more
20816 convenient for several reasons: (1) It's stable across the prologue
20817 and epilogue, which makes it better than just a frame pointer,
20818 (2) With dwarf3, there exists a one-byte encoding that allows us
20819 to reference the .debug_frame data by proxy, but failing that,
20820 (3) We can at least reuse the code inspection and interpretation
20821 code that determines the CFA position at various points in the
20822 function. */
20823 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20825 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20826 add_AT_loc (subr_die, DW_AT_frame_base, op);
20828 else
20830 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20831 if (list->dw_loc_next)
20832 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20833 else
20834 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20837 /* Compute a displacement from the "steady-state frame pointer" to
20838 the CFA. The former is what all stack slots and argument slots
20839 will reference in the rtl; the latter is what we've told the
20840 debugger about. We'll need to adjust all frame_base references
20841 by this displacement. */
20842 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20844 if (fun->static_chain_decl)
20846 /* DWARF requires here a location expression that computes the
20847 address of the enclosing subprogram's frame base. The machinery
20848 in tree-nested.c is supposed to store this specific address in the
20849 last field of the FRAME record. */
20850 const tree frame_type
20851 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20852 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20854 tree fb_expr
20855 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20856 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20857 fb_expr, fb_decl, NULL_TREE);
20859 add_AT_location_description (subr_die, DW_AT_static_link,
20860 loc_list_from_tree (fb_expr, 0, NULL));
20864 /* Generate child dies for template paramaters. */
20865 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20866 gen_generic_params_dies (decl);
20868 /* Now output descriptions of the arguments for this function. This gets
20869 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20870 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20871 `...' at the end of the formal parameter list. In order to find out if
20872 there was a trailing ellipsis or not, we must instead look at the type
20873 associated with the FUNCTION_DECL. This will be a node of type
20874 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20875 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20876 an ellipsis at the end. */
20878 /* In the case where we are describing a mere function declaration, all we
20879 need to do here (and all we *can* do here) is to describe the *types* of
20880 its formal parameters. */
20881 if (debug_info_level <= DINFO_LEVEL_TERSE)
20883 else if (declaration)
20884 gen_formal_types_die (decl, subr_die);
20885 else
20887 /* Generate DIEs to represent all known formal parameters. */
20888 tree parm = DECL_ARGUMENTS (decl);
20889 tree generic_decl = early_dwarf
20890 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20891 tree generic_decl_parm = generic_decl
20892 ? DECL_ARGUMENTS (generic_decl)
20893 : NULL;
20894 auto_vec<dw_die_ref> string_types_vec;
20895 if (string_types == NULL)
20896 string_types = &string_types_vec;
20898 /* Now we want to walk the list of parameters of the function and
20899 emit their relevant DIEs.
20901 We consider the case of DECL being an instance of a generic function
20902 as well as it being a normal function.
20904 If DECL is an instance of a generic function we walk the
20905 parameters of the generic function declaration _and_ the parameters of
20906 DECL itself. This is useful because we want to emit specific DIEs for
20907 function parameter packs and those are declared as part of the
20908 generic function declaration. In that particular case,
20909 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20910 That DIE has children DIEs representing the set of arguments
20911 of the pack. Note that the set of pack arguments can be empty.
20912 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20913 children DIE.
20915 Otherwise, we just consider the parameters of DECL. */
20916 while (generic_decl_parm || parm)
20918 if (generic_decl_parm
20919 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20920 gen_formal_parameter_pack_die (generic_decl_parm,
20921 parm, subr_die,
20922 &parm);
20923 else if (parm && !POINTER_BOUNDS_P (parm))
20925 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20927 if (parm == DECL_ARGUMENTS (decl)
20928 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20929 && parm_die
20930 && (dwarf_version >= 3 || !dwarf_strict))
20931 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20933 parm = DECL_CHAIN (parm);
20935 else if (parm)
20936 parm = DECL_CHAIN (parm);
20938 if (generic_decl_parm)
20939 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20942 /* Decide whether we need an unspecified_parameters DIE at the end.
20943 There are 2 more cases to do this for: 1) the ansi ... declaration -
20944 this is detectable when the end of the arg list is not a
20945 void_type_node 2) an unprototyped function declaration (not a
20946 definition). This just means that we have no info about the
20947 parameters at all. */
20948 if (early_dwarf)
20950 if (prototype_p (TREE_TYPE (decl)))
20952 /* This is the prototyped case, check for.... */
20953 if (stdarg_p (TREE_TYPE (decl)))
20954 gen_unspecified_parameters_die (decl, subr_die);
20956 else if (DECL_INITIAL (decl) == NULL_TREE)
20957 gen_unspecified_parameters_die (decl, subr_die);
20960 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
20961 have DIEs. */
20962 if (string_types == &string_types_vec)
20964 adjust_string_types ();
20965 string_types = NULL;
20969 if (subr_die != old_die)
20970 /* Add the calling convention attribute if requested. */
20971 add_calling_convention_attribute (subr_die, decl);
20973 /* Output Dwarf info for all of the stuff within the body of the function
20974 (if it has one - it may be just a declaration).
20976 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20977 a function. This BLOCK actually represents the outermost binding contour
20978 for the function, i.e. the contour in which the function's formal
20979 parameters and labels get declared. Curiously, it appears that the front
20980 end doesn't actually put the PARM_DECL nodes for the current function onto
20981 the BLOCK_VARS list for this outer scope, but are strung off of the
20982 DECL_ARGUMENTS list for the function instead.
20984 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20985 the LABEL_DECL nodes for the function however, and we output DWARF info
20986 for those in decls_for_scope. Just within the `outer_scope' there will be
20987 a BLOCK node representing the function's outermost pair of curly braces,
20988 and any blocks used for the base and member initializers of a C++
20989 constructor function. */
20990 tree outer_scope = DECL_INITIAL (decl);
20991 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20993 int call_site_note_count = 0;
20994 int tail_call_site_note_count = 0;
20996 /* Emit a DW_TAG_variable DIE for a named return value. */
20997 if (DECL_NAME (DECL_RESULT (decl)))
20998 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21000 /* The first time through decls_for_scope we will generate the
21001 DIEs for the locals. The second time, we fill in the
21002 location info. */
21003 decls_for_scope (outer_scope, subr_die);
21005 if (call_arg_locations && !dwarf_strict)
21007 struct call_arg_loc_node *ca_loc;
21008 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21010 dw_die_ref die = NULL;
21011 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21012 rtx arg, next_arg;
21014 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21015 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21016 : NULL_RTX);
21017 arg; arg = next_arg)
21019 dw_loc_descr_ref reg, val;
21020 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21021 dw_die_ref cdie, tdie = NULL;
21023 next_arg = XEXP (arg, 1);
21024 if (REG_P (XEXP (XEXP (arg, 0), 0))
21025 && next_arg
21026 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21027 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21028 && REGNO (XEXP (XEXP (arg, 0), 0))
21029 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21030 next_arg = XEXP (next_arg, 1);
21031 if (mode == VOIDmode)
21033 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21034 if (mode == VOIDmode)
21035 mode = GET_MODE (XEXP (arg, 0));
21037 if (mode == VOIDmode || mode == BLKmode)
21038 continue;
21039 /* Get dynamic information about call target only if we
21040 have no static information: we cannot generate both
21041 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21042 attributes. */
21043 if (ca_loc->symbol_ref == NULL_RTX)
21045 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21047 tloc = XEXP (XEXP (arg, 0), 1);
21048 continue;
21050 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21051 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21053 tlocc = XEXP (XEXP (arg, 0), 1);
21054 continue;
21057 reg = NULL;
21058 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21059 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21060 VAR_INIT_STATUS_INITIALIZED);
21061 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21063 rtx mem = XEXP (XEXP (arg, 0), 0);
21064 reg = mem_loc_descriptor (XEXP (mem, 0),
21065 get_address_mode (mem),
21066 GET_MODE (mem),
21067 VAR_INIT_STATUS_INITIALIZED);
21069 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21070 == DEBUG_PARAMETER_REF)
21072 tree tdecl
21073 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21074 tdie = lookup_decl_die (tdecl);
21075 if (tdie == NULL)
21076 continue;
21078 else
21079 continue;
21080 if (reg == NULL
21081 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21082 != DEBUG_PARAMETER_REF)
21083 continue;
21084 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21085 VOIDmode,
21086 VAR_INIT_STATUS_INITIALIZED);
21087 if (val == NULL)
21088 continue;
21089 if (die == NULL)
21090 die = gen_call_site_die (decl, subr_die, ca_loc);
21091 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21092 NULL_TREE);
21093 if (reg != NULL)
21094 add_AT_loc (cdie, DW_AT_location, reg);
21095 else if (tdie != NULL)
21096 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21097 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21098 if (next_arg != XEXP (arg, 1))
21100 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21101 if (mode == VOIDmode)
21102 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21103 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21104 0), 1),
21105 mode, VOIDmode,
21106 VAR_INIT_STATUS_INITIALIZED);
21107 if (val != NULL)
21108 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21111 if (die == NULL
21112 && (ca_loc->symbol_ref || tloc))
21113 die = gen_call_site_die (decl, subr_die, ca_loc);
21114 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21116 dw_loc_descr_ref tval = NULL;
21118 if (tloc != NULL_RTX)
21119 tval = mem_loc_descriptor (tloc,
21120 GET_MODE (tloc) == VOIDmode
21121 ? Pmode : GET_MODE (tloc),
21122 VOIDmode,
21123 VAR_INIT_STATUS_INITIALIZED);
21124 if (tval)
21125 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21126 else if (tlocc != NULL_RTX)
21128 tval = mem_loc_descriptor (tlocc,
21129 GET_MODE (tlocc) == VOIDmode
21130 ? Pmode : GET_MODE (tlocc),
21131 VOIDmode,
21132 VAR_INIT_STATUS_INITIALIZED);
21133 if (tval)
21134 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21135 tval);
21138 if (die != NULL)
21140 call_site_note_count++;
21141 if (ca_loc->tail_call_p)
21142 tail_call_site_note_count++;
21146 call_arg_locations = NULL;
21147 call_arg_loc_last = NULL;
21148 if (tail_call_site_count >= 0
21149 && tail_call_site_count == tail_call_site_note_count
21150 && !dwarf_strict)
21152 if (call_site_count >= 0
21153 && call_site_count == call_site_note_count)
21154 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21155 else
21156 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21158 call_site_count = -1;
21159 tail_call_site_count = -1;
21162 /* Mark used types after we have created DIEs for the functions scopes. */
21163 premark_used_types (DECL_STRUCT_FUNCTION (decl));
21166 /* Returns a hash value for X (which really is a die_struct). */
21168 hashval_t
21169 block_die_hasher::hash (die_struct *d)
21171 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21174 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21175 as decl_id and die_parent of die_struct Y. */
21177 bool
21178 block_die_hasher::equal (die_struct *x, die_struct *y)
21180 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21183 /* Return TRUE if DECL, which may have been previously generated as
21184 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21185 true if decl (or its origin) is either an extern declaration or a
21186 class/namespace scoped declaration.
21188 The declare_in_namespace support causes us to get two DIEs for one
21189 variable, both of which are declarations. We want to avoid
21190 considering one to be a specification, so we must test for
21191 DECLARATION and DW_AT_declaration. */
21192 static inline bool
21193 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21195 return (old_die && TREE_STATIC (decl) && !declaration
21196 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21199 /* Return true if DECL is a local static. */
21201 static inline bool
21202 local_function_static (tree decl)
21204 gcc_assert (VAR_P (decl));
21205 return TREE_STATIC (decl)
21206 && DECL_CONTEXT (decl)
21207 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21210 /* Generate a DIE to represent a declared data object.
21211 Either DECL or ORIGIN must be non-null. */
21213 static void
21214 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21216 HOST_WIDE_INT off = 0;
21217 tree com_decl;
21218 tree decl_or_origin = decl ? decl : origin;
21219 tree ultimate_origin;
21220 dw_die_ref var_die;
21221 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21222 dw_die_ref origin_die = NULL;
21223 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21224 || class_or_namespace_scope_p (context_die));
21225 bool specialization_p = false;
21227 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21228 if (decl || ultimate_origin)
21229 origin = ultimate_origin;
21230 com_decl = fortran_common (decl_or_origin, &off);
21232 /* Symbol in common gets emitted as a child of the common block, in the form
21233 of a data member. */
21234 if (com_decl)
21236 dw_die_ref com_die;
21237 dw_loc_list_ref loc;
21238 die_node com_die_arg;
21240 var_die = lookup_decl_die (decl_or_origin);
21241 if (var_die)
21243 if (get_AT (var_die, DW_AT_location) == NULL)
21245 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21246 if (loc)
21248 if (off)
21250 /* Optimize the common case. */
21251 if (single_element_loc_list_p (loc)
21252 && loc->expr->dw_loc_opc == DW_OP_addr
21253 && loc->expr->dw_loc_next == NULL
21254 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21255 == SYMBOL_REF)
21257 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21258 loc->expr->dw_loc_oprnd1.v.val_addr
21259 = plus_constant (GET_MODE (x), x , off);
21261 else
21262 loc_list_plus_const (loc, off);
21264 add_AT_location_description (var_die, DW_AT_location, loc);
21265 remove_AT (var_die, DW_AT_declaration);
21268 return;
21271 if (common_block_die_table == NULL)
21272 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21274 com_die_arg.decl_id = DECL_UID (com_decl);
21275 com_die_arg.die_parent = context_die;
21276 com_die = common_block_die_table->find (&com_die_arg);
21277 loc = loc_list_from_tree (com_decl, 2, NULL);
21278 if (com_die == NULL)
21280 const char *cnam
21281 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21282 die_node **slot;
21284 com_die = new_die (DW_TAG_common_block, context_die, decl);
21285 add_name_and_src_coords_attributes (com_die, com_decl);
21286 if (loc)
21288 add_AT_location_description (com_die, DW_AT_location, loc);
21289 /* Avoid sharing the same loc descriptor between
21290 DW_TAG_common_block and DW_TAG_variable. */
21291 loc = loc_list_from_tree (com_decl, 2, NULL);
21293 else if (DECL_EXTERNAL (decl_or_origin))
21294 add_AT_flag (com_die, DW_AT_declaration, 1);
21295 if (want_pubnames ())
21296 add_pubname_string (cnam, com_die); /* ??? needed? */
21297 com_die->decl_id = DECL_UID (com_decl);
21298 slot = common_block_die_table->find_slot (com_die, INSERT);
21299 *slot = com_die;
21301 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21303 add_AT_location_description (com_die, DW_AT_location, loc);
21304 loc = loc_list_from_tree (com_decl, 2, NULL);
21305 remove_AT (com_die, DW_AT_declaration);
21307 var_die = new_die (DW_TAG_variable, com_die, decl);
21308 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21309 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21310 decl_quals (decl_or_origin), false,
21311 context_die);
21312 add_AT_flag (var_die, DW_AT_external, 1);
21313 if (loc)
21315 if (off)
21317 /* Optimize the common case. */
21318 if (single_element_loc_list_p (loc)
21319 && loc->expr->dw_loc_opc == DW_OP_addr
21320 && loc->expr->dw_loc_next == NULL
21321 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21323 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21324 loc->expr->dw_loc_oprnd1.v.val_addr
21325 = plus_constant (GET_MODE (x), x, off);
21327 else
21328 loc_list_plus_const (loc, off);
21330 add_AT_location_description (var_die, DW_AT_location, loc);
21332 else if (DECL_EXTERNAL (decl_or_origin))
21333 add_AT_flag (var_die, DW_AT_declaration, 1);
21334 if (decl)
21335 equate_decl_number_to_die (decl, var_die);
21336 return;
21339 if (old_die)
21341 if (declaration)
21343 /* A declaration that has been previously dumped, needs no
21344 further annotations, since it doesn't need location on
21345 the second pass. */
21346 return;
21348 else if (decl_will_get_specification_p (old_die, decl, declaration)
21349 && !get_AT (old_die, DW_AT_specification))
21351 /* Fall-thru so we can make a new variable die along with a
21352 DW_AT_specification. */
21354 else if (origin && old_die->die_parent != context_die)
21356 /* If we will be creating an inlined instance, we need a
21357 new DIE that will get annotated with
21358 DW_AT_abstract_origin. Clear things so we can get a
21359 new DIE. */
21360 gcc_assert (!DECL_ABSTRACT_P (decl));
21361 old_die = NULL;
21363 else
21365 /* If a DIE was dumped early, it still needs location info.
21366 Skip to where we fill the location bits. */
21367 var_die = old_die;
21368 goto gen_variable_die_location;
21372 /* For static data members, the declaration in the class is supposed
21373 to have DW_TAG_member tag; the specification should still be
21374 DW_TAG_variable referencing the DW_TAG_member DIE. */
21375 if (declaration && class_scope_p (context_die))
21376 var_die = new_die (DW_TAG_member, context_die, decl);
21377 else
21378 var_die = new_die (DW_TAG_variable, context_die, decl);
21380 if (origin != NULL)
21381 origin_die = add_abstract_origin_attribute (var_die, origin);
21383 /* Loop unrolling can create multiple blocks that refer to the same
21384 static variable, so we must test for the DW_AT_declaration flag.
21386 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21387 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21388 sharing them.
21390 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21391 else if (decl_will_get_specification_p (old_die, decl, declaration))
21393 /* This is a definition of a C++ class level static. */
21394 add_AT_specification (var_die, old_die);
21395 specialization_p = true;
21396 if (DECL_NAME (decl))
21398 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21399 struct dwarf_file_data * file_index = lookup_filename (s.file);
21401 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21402 add_AT_file (var_die, DW_AT_decl_file, file_index);
21404 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21405 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21407 if (old_die->die_tag == DW_TAG_member)
21408 add_linkage_name (var_die, decl);
21411 else
21412 add_name_and_src_coords_attributes (var_die, decl);
21414 if ((origin == NULL && !specialization_p)
21415 || (origin != NULL
21416 && !DECL_ABSTRACT_P (decl_or_origin)
21417 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21418 decl_function_context
21419 (decl_or_origin))))
21421 tree type = TREE_TYPE (decl_or_origin);
21423 if (decl_by_reference_p (decl_or_origin))
21424 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21425 context_die);
21426 else
21427 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21428 context_die);
21431 if (origin == NULL && !specialization_p)
21433 if (TREE_PUBLIC (decl))
21434 add_AT_flag (var_die, DW_AT_external, 1);
21436 if (DECL_ARTIFICIAL (decl))
21437 add_AT_flag (var_die, DW_AT_artificial, 1);
21439 add_accessibility_attribute (var_die, decl);
21442 if (declaration)
21443 add_AT_flag (var_die, DW_AT_declaration, 1);
21445 if (decl && (DECL_ABSTRACT_P (decl)
21446 || !old_die || is_declaration_die (old_die)))
21447 equate_decl_number_to_die (decl, var_die);
21449 gen_variable_die_location:
21450 if (! declaration
21451 && (! DECL_ABSTRACT_P (decl_or_origin)
21452 /* Local static vars are shared between all clones/inlines,
21453 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21454 already set. */
21455 || (VAR_P (decl_or_origin)
21456 && TREE_STATIC (decl_or_origin)
21457 && DECL_RTL_SET_P (decl_or_origin)))
21458 /* When abstract origin already has DW_AT_location attribute, no need
21459 to add it again. */
21460 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21462 if (early_dwarf)
21463 add_pubname (decl_or_origin, var_die);
21464 else
21465 add_location_or_const_value_attribute (var_die, decl_or_origin,
21466 decl == NULL);
21468 else
21469 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21472 /* Generate a DIE to represent a named constant. */
21474 static void
21475 gen_const_die (tree decl, dw_die_ref context_die)
21477 dw_die_ref const_die;
21478 tree type = TREE_TYPE (decl);
21480 const_die = lookup_decl_die (decl);
21481 if (const_die)
21482 return;
21484 const_die = new_die (DW_TAG_constant, context_die, decl);
21485 equate_decl_number_to_die (decl, const_die);
21486 add_name_and_src_coords_attributes (const_die, decl);
21487 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21488 if (TREE_PUBLIC (decl))
21489 add_AT_flag (const_die, DW_AT_external, 1);
21490 if (DECL_ARTIFICIAL (decl))
21491 add_AT_flag (const_die, DW_AT_artificial, 1);
21492 tree_add_const_value_attribute_for_decl (const_die, decl);
21495 /* Generate a DIE to represent a label identifier. */
21497 static void
21498 gen_label_die (tree decl, dw_die_ref context_die)
21500 tree origin = decl_ultimate_origin (decl);
21501 dw_die_ref lbl_die = lookup_decl_die (decl);
21502 rtx insn;
21503 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21505 if (!lbl_die)
21507 lbl_die = new_die (DW_TAG_label, context_die, decl);
21508 equate_decl_number_to_die (decl, lbl_die);
21510 if (origin != NULL)
21511 add_abstract_origin_attribute (lbl_die, origin);
21512 else
21513 add_name_and_src_coords_attributes (lbl_die, decl);
21516 if (DECL_ABSTRACT_P (decl))
21517 equate_decl_number_to_die (decl, lbl_die);
21518 else
21520 insn = DECL_RTL_IF_SET (decl);
21522 /* Deleted labels are programmer specified labels which have been
21523 eliminated because of various optimizations. We still emit them
21524 here so that it is possible to put breakpoints on them. */
21525 if (insn
21526 && (LABEL_P (insn)
21527 || ((NOTE_P (insn)
21528 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21530 /* When optimization is enabled (via -O) some parts of the compiler
21531 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21532 represent source-level labels which were explicitly declared by
21533 the user. This really shouldn't be happening though, so catch
21534 it if it ever does happen. */
21535 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21537 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21538 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21540 else if (insn
21541 && NOTE_P (insn)
21542 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21543 && CODE_LABEL_NUMBER (insn) != -1)
21545 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21546 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21551 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21552 attributes to the DIE for a block STMT, to describe where the inlined
21553 function was called from. This is similar to add_src_coords_attributes. */
21555 static inline void
21556 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21558 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21560 if (dwarf_version >= 3 || !dwarf_strict)
21562 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21563 add_AT_unsigned (die, DW_AT_call_line, s.line);
21568 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21569 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21571 static inline void
21572 add_high_low_attributes (tree stmt, dw_die_ref die)
21574 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21576 if (BLOCK_FRAGMENT_CHAIN (stmt)
21577 && (dwarf_version >= 3 || !dwarf_strict))
21579 tree chain, superblock = NULL_TREE;
21580 dw_die_ref pdie;
21581 dw_attr_node *attr = NULL;
21583 if (inlined_function_outer_scope_p (stmt))
21585 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21586 BLOCK_NUMBER (stmt));
21587 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21590 /* Optimize duplicate .debug_ranges lists or even tails of
21591 lists. If this BLOCK has same ranges as its supercontext,
21592 lookup DW_AT_ranges attribute in the supercontext (and
21593 recursively so), verify that the ranges_table contains the
21594 right values and use it instead of adding a new .debug_range. */
21595 for (chain = stmt, pdie = die;
21596 BLOCK_SAME_RANGE (chain);
21597 chain = BLOCK_SUPERCONTEXT (chain))
21599 dw_attr_node *new_attr;
21601 pdie = pdie->die_parent;
21602 if (pdie == NULL)
21603 break;
21604 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21605 break;
21606 new_attr = get_AT (pdie, DW_AT_ranges);
21607 if (new_attr == NULL
21608 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21609 break;
21610 attr = new_attr;
21611 superblock = BLOCK_SUPERCONTEXT (chain);
21613 if (attr != NULL
21614 && (ranges_table[attr->dw_attr_val.v.val_offset
21615 / 2 / DWARF2_ADDR_SIZE].num
21616 == BLOCK_NUMBER (superblock))
21617 && BLOCK_FRAGMENT_CHAIN (superblock))
21619 unsigned long off = attr->dw_attr_val.v.val_offset
21620 / 2 / DWARF2_ADDR_SIZE;
21621 unsigned long supercnt = 0, thiscnt = 0;
21622 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21623 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21625 ++supercnt;
21626 gcc_checking_assert (ranges_table[off + supercnt].num
21627 == BLOCK_NUMBER (chain));
21629 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21630 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21631 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21632 ++thiscnt;
21633 gcc_assert (supercnt >= thiscnt);
21634 add_AT_range_list (die, DW_AT_ranges,
21635 ((off + supercnt - thiscnt)
21636 * 2 * DWARF2_ADDR_SIZE),
21637 false);
21638 return;
21641 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21643 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21646 add_ranges (chain);
21647 chain = BLOCK_FRAGMENT_CHAIN (chain);
21649 while (chain);
21650 add_ranges (NULL);
21652 else
21654 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21655 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21656 BLOCK_NUMBER (stmt));
21657 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21658 BLOCK_NUMBER (stmt));
21659 add_AT_low_high_pc (die, label, label_high, false);
21663 /* Generate a DIE for a lexical block. */
21665 static void
21666 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21668 dw_die_ref old_die = BLOCK_DIE (stmt);
21669 dw_die_ref stmt_die = NULL;
21670 if (!old_die)
21672 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21673 BLOCK_DIE (stmt) = stmt_die;
21676 if (BLOCK_ABSTRACT (stmt))
21678 if (old_die)
21680 /* This must have been generated early and it won't even
21681 need location information since it's a DW_AT_inline
21682 function. */
21683 if (flag_checking)
21684 for (dw_die_ref c = context_die; c; c = c->die_parent)
21685 if (c->die_tag == DW_TAG_inlined_subroutine
21686 || c->die_tag == DW_TAG_subprogram)
21688 gcc_assert (get_AT (c, DW_AT_inline));
21689 break;
21691 return;
21694 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21696 /* If this is an inlined instance, create a new lexical die for
21697 anything below to attach DW_AT_abstract_origin to. */
21698 if (old_die)
21700 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21701 BLOCK_DIE (stmt) = stmt_die;
21702 old_die = NULL;
21705 tree origin = block_ultimate_origin (stmt);
21706 if (origin != NULL_TREE && origin != stmt)
21707 add_abstract_origin_attribute (stmt_die, origin);
21710 if (old_die)
21711 stmt_die = old_die;
21713 /* A non abstract block whose blocks have already been reordered
21714 should have the instruction range for this block. If so, set the
21715 high/low attributes. */
21716 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21718 gcc_assert (stmt_die);
21719 add_high_low_attributes (stmt, stmt_die);
21722 decls_for_scope (stmt, stmt_die);
21725 /* Generate a DIE for an inlined subprogram. */
21727 static void
21728 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21730 tree decl;
21732 /* The instance of function that is effectively being inlined shall not
21733 be abstract. */
21734 gcc_assert (! BLOCK_ABSTRACT (stmt));
21736 decl = block_ultimate_origin (stmt);
21738 /* Make sure any inlined functions are known to be inlineable. */
21739 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21740 || cgraph_function_possibly_inlined_p (decl));
21742 /* Emit info for the abstract instance first, if we haven't yet. We
21743 must emit this even if the block is abstract, otherwise when we
21744 emit the block below (or elsewhere), we may end up trying to emit
21745 a die whose origin die hasn't been emitted, and crashing. */
21746 dwarf2out_abstract_function (decl);
21748 if (! BLOCK_ABSTRACT (stmt))
21750 dw_die_ref subr_die
21751 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21753 if (call_arg_locations)
21754 BLOCK_DIE (stmt) = subr_die;
21755 add_abstract_origin_attribute (subr_die, decl);
21756 if (TREE_ASM_WRITTEN (stmt))
21757 add_high_low_attributes (stmt, subr_die);
21758 add_call_src_coords_attributes (stmt, subr_die);
21760 decls_for_scope (stmt, subr_die);
21764 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21765 the comment for VLR_CONTEXT. */
21767 static void
21768 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21770 dw_die_ref decl_die;
21772 if (TREE_TYPE (decl) == error_mark_node)
21773 return;
21775 decl_die = new_die (DW_TAG_member, context_die, decl);
21776 add_name_and_src_coords_attributes (decl_die, decl);
21777 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21778 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21779 context_die);
21781 if (DECL_BIT_FIELD_TYPE (decl))
21783 add_byte_size_attribute (decl_die, decl);
21784 add_bit_size_attribute (decl_die, decl);
21785 add_bit_offset_attribute (decl_die, decl, ctx);
21788 /* If we have a variant part offset, then we are supposed to process a member
21789 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21790 trees. */
21791 gcc_assert (ctx->variant_part_offset == NULL_TREE
21792 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21793 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21794 add_data_member_location_attribute (decl_die, decl, ctx);
21796 if (DECL_ARTIFICIAL (decl))
21797 add_AT_flag (decl_die, DW_AT_artificial, 1);
21799 add_accessibility_attribute (decl_die, decl);
21801 /* Equate decl number to die, so that we can look up this decl later on. */
21802 equate_decl_number_to_die (decl, decl_die);
21805 #if 0
21806 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21807 Use modified_type_die instead.
21808 We keep this code here just in case these types of DIEs may be needed to
21809 represent certain things in other languages (e.g. Pascal) someday. */
21811 static void
21812 gen_pointer_type_die (tree type, dw_die_ref context_die)
21814 dw_die_ref ptr_die
21815 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21817 equate_type_number_to_die (type, ptr_die);
21818 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21819 context_die);
21820 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21823 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21824 Use modified_type_die instead.
21825 We keep this code here just in case these types of DIEs may be needed to
21826 represent certain things in other languages (e.g. Pascal) someday. */
21828 static void
21829 gen_reference_type_die (tree type, dw_die_ref context_die)
21831 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21833 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21834 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21835 else
21836 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21838 equate_type_number_to_die (type, ref_die);
21839 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21840 context_die);
21841 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21843 #endif
21845 /* Generate a DIE for a pointer to a member type. */
21847 static void
21848 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21850 dw_die_ref ptr_die
21851 = new_die (DW_TAG_ptr_to_member_type,
21852 scope_die_for (type, context_die), type);
21854 equate_type_number_to_die (type, ptr_die);
21855 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21856 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21857 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21858 context_die);
21861 static char *producer_string;
21863 /* Return a heap allocated producer string including command line options
21864 if -grecord-gcc-switches. */
21866 static char *
21867 gen_producer_string (void)
21869 size_t j;
21870 auto_vec<const char *> switches;
21871 const char *language_string = lang_hooks.name;
21872 char *producer, *tail;
21873 const char *p;
21874 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21875 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21877 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21878 switch (save_decoded_options[j].opt_index)
21880 case OPT_o:
21881 case OPT_d:
21882 case OPT_dumpbase:
21883 case OPT_dumpdir:
21884 case OPT_auxbase:
21885 case OPT_auxbase_strip:
21886 case OPT_quiet:
21887 case OPT_version:
21888 case OPT_v:
21889 case OPT_w:
21890 case OPT_L:
21891 case OPT_D:
21892 case OPT_I:
21893 case OPT_U:
21894 case OPT_SPECIAL_unknown:
21895 case OPT_SPECIAL_ignore:
21896 case OPT_SPECIAL_program_name:
21897 case OPT_SPECIAL_input_file:
21898 case OPT_grecord_gcc_switches:
21899 case OPT_gno_record_gcc_switches:
21900 case OPT__output_pch_:
21901 case OPT_fdiagnostics_show_location_:
21902 case OPT_fdiagnostics_show_option:
21903 case OPT_fdiagnostics_show_caret:
21904 case OPT_fdiagnostics_color_:
21905 case OPT_fverbose_asm:
21906 case OPT____:
21907 case OPT__sysroot_:
21908 case OPT_nostdinc:
21909 case OPT_nostdinc__:
21910 case OPT_fpreprocessed:
21911 case OPT_fltrans_output_list_:
21912 case OPT_fresolution_:
21913 case OPT_fdebug_prefix_map_:
21914 /* Ignore these. */
21915 continue;
21916 default:
21917 if (cl_options[save_decoded_options[j].opt_index].flags
21918 & CL_NO_DWARF_RECORD)
21919 continue;
21920 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21921 == '-');
21922 switch (save_decoded_options[j].canonical_option[0][1])
21924 case 'M':
21925 case 'i':
21926 case 'W':
21927 continue;
21928 case 'f':
21929 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21930 "dump", 4) == 0)
21931 continue;
21932 break;
21933 default:
21934 break;
21936 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21937 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21938 break;
21941 producer = XNEWVEC (char, plen + 1 + len + 1);
21942 tail = producer;
21943 sprintf (tail, "%s %s", language_string, version_string);
21944 tail += plen;
21946 FOR_EACH_VEC_ELT (switches, j, p)
21948 len = strlen (p);
21949 *tail = ' ';
21950 memcpy (tail + 1, p, len);
21951 tail += len + 1;
21954 *tail = '\0';
21955 return producer;
21958 /* Given a C and/or C++ language/version string return the "highest".
21959 C++ is assumed to be "higher" than C in this case. Used for merging
21960 LTO translation unit languages. */
21961 static const char *
21962 highest_c_language (const char *lang1, const char *lang2)
21964 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21965 return "GNU C++14";
21966 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21967 return "GNU C++11";
21968 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21969 return "GNU C++98";
21971 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21972 return "GNU C11";
21973 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21974 return "GNU C99";
21975 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21976 return "GNU C89";
21978 gcc_unreachable ();
21982 /* Generate the DIE for the compilation unit. */
21984 static dw_die_ref
21985 gen_compile_unit_die (const char *filename)
21987 dw_die_ref die;
21988 const char *language_string = lang_hooks.name;
21989 int language;
21991 die = new_die (DW_TAG_compile_unit, NULL, NULL);
21993 if (filename)
21995 add_name_attribute (die, filename);
21996 /* Don't add cwd for <built-in>. */
21997 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21998 add_comp_dir_attribute (die);
22001 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22003 /* If our producer is LTO try to figure out a common language to use
22004 from the global list of translation units. */
22005 if (strcmp (language_string, "GNU GIMPLE") == 0)
22007 unsigned i;
22008 tree t;
22009 const char *common_lang = NULL;
22011 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22013 if (!TRANSLATION_UNIT_LANGUAGE (t))
22014 continue;
22015 if (!common_lang)
22016 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22017 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22019 else if (strncmp (common_lang, "GNU C", 5) == 0
22020 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22021 /* Mixing C and C++ is ok, use C++ in that case. */
22022 common_lang = highest_c_language (common_lang,
22023 TRANSLATION_UNIT_LANGUAGE (t));
22024 else
22026 /* Fall back to C. */
22027 common_lang = NULL;
22028 break;
22032 if (common_lang)
22033 language_string = common_lang;
22036 language = DW_LANG_C;
22037 if (strncmp (language_string, "GNU C", 5) == 0
22038 && ISDIGIT (language_string[5]))
22040 language = DW_LANG_C89;
22041 if (dwarf_version >= 3 || !dwarf_strict)
22043 if (strcmp (language_string, "GNU C89") != 0)
22044 language = DW_LANG_C99;
22046 if (dwarf_version >= 5 /* || !dwarf_strict */)
22047 if (strcmp (language_string, "GNU C11") == 0)
22048 language = DW_LANG_C11;
22051 else if (strncmp (language_string, "GNU C++", 7) == 0)
22053 language = DW_LANG_C_plus_plus;
22054 if (dwarf_version >= 5 /* || !dwarf_strict */)
22056 if (strcmp (language_string, "GNU C++11") == 0)
22057 language = DW_LANG_C_plus_plus_11;
22058 else if (strcmp (language_string, "GNU C++14") == 0)
22059 language = DW_LANG_C_plus_plus_14;
22062 else if (strcmp (language_string, "GNU F77") == 0)
22063 language = DW_LANG_Fortran77;
22064 else if (strcmp (language_string, "GNU Pascal") == 0)
22065 language = DW_LANG_Pascal83;
22066 else if (dwarf_version >= 3 || !dwarf_strict)
22068 if (strcmp (language_string, "GNU Ada") == 0)
22069 language = DW_LANG_Ada95;
22070 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22072 language = DW_LANG_Fortran95;
22073 if (dwarf_version >= 5 /* || !dwarf_strict */)
22075 if (strcmp (language_string, "GNU Fortran2003") == 0)
22076 language = DW_LANG_Fortran03;
22077 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22078 language = DW_LANG_Fortran08;
22081 else if (strcmp (language_string, "GNU Java") == 0)
22082 language = DW_LANG_Java;
22083 else if (strcmp (language_string, "GNU Objective-C") == 0)
22084 language = DW_LANG_ObjC;
22085 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22086 language = DW_LANG_ObjC_plus_plus;
22087 else if (dwarf_version >= 5 || !dwarf_strict)
22089 if (strcmp (language_string, "GNU Go") == 0)
22090 language = DW_LANG_Go;
22093 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22094 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22095 language = DW_LANG_Fortran90;
22097 add_AT_unsigned (die, DW_AT_language, language);
22099 switch (language)
22101 case DW_LANG_Fortran77:
22102 case DW_LANG_Fortran90:
22103 case DW_LANG_Fortran95:
22104 case DW_LANG_Fortran03:
22105 case DW_LANG_Fortran08:
22106 /* Fortran has case insensitive identifiers and the front-end
22107 lowercases everything. */
22108 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22109 break;
22110 default:
22111 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22112 break;
22114 return die;
22117 /* Generate the DIE for a base class. */
22119 static void
22120 gen_inheritance_die (tree binfo, tree access, tree type,
22121 dw_die_ref context_die)
22123 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22124 struct vlr_context ctx = { type, NULL };
22126 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22127 context_die);
22128 add_data_member_location_attribute (die, binfo, &ctx);
22130 if (BINFO_VIRTUAL_P (binfo))
22131 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22133 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22134 children, otherwise the default is DW_ACCESS_public. In DWARF2
22135 the default has always been DW_ACCESS_private. */
22136 if (access == access_public_node)
22138 if (dwarf_version == 2
22139 || context_die->die_tag == DW_TAG_class_type)
22140 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22142 else if (access == access_protected_node)
22143 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22144 else if (dwarf_version > 2
22145 && context_die->die_tag != DW_TAG_class_type)
22146 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22149 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22150 structure. */
22151 static bool
22152 is_variant_part (tree decl)
22154 return (TREE_CODE (decl) == FIELD_DECL
22155 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22158 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22159 return the FIELD_DECL. Return NULL_TREE otherwise. */
22161 static tree
22162 analyze_discr_in_predicate (tree operand, tree struct_type)
22164 bool continue_stripping = true;
22165 while (continue_stripping)
22166 switch (TREE_CODE (operand))
22168 CASE_CONVERT:
22169 operand = TREE_OPERAND (operand, 0);
22170 break;
22171 default:
22172 continue_stripping = false;
22173 break;
22176 /* Match field access to members of struct_type only. */
22177 if (TREE_CODE (operand) == COMPONENT_REF
22178 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22179 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22180 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22181 return TREE_OPERAND (operand, 1);
22182 else
22183 return NULL_TREE;
22186 /* Check that SRC is a constant integer that can be represented as a native
22187 integer constant (either signed or unsigned). If so, store it into DEST and
22188 return true. Return false otherwise. */
22190 static bool
22191 get_discr_value (tree src, dw_discr_value *dest)
22193 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22195 if (TREE_CODE (src) != INTEGER_CST
22196 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22197 return false;
22199 dest->pos = is_unsigned;
22200 if (is_unsigned)
22201 dest->v.uval = tree_to_uhwi (src);
22202 else
22203 dest->v.sval = tree_to_shwi (src);
22205 return true;
22208 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22209 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22210 store NULL_TREE in DISCR_DECL. Otherwise:
22212 - store the discriminant field in STRUCT_TYPE that controls the variant
22213 part to *DISCR_DECL
22215 - put in *DISCR_LISTS_P an array where for each variant, the item
22216 represents the corresponding matching list of discriminant values.
22218 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22219 the above array.
22221 Note that when the array is allocated (i.e. when the analysis is
22222 successful), it is up to the caller to free the array. */
22224 static void
22225 analyze_variants_discr (tree variant_part_decl,
22226 tree struct_type,
22227 tree *discr_decl,
22228 dw_discr_list_ref **discr_lists_p,
22229 unsigned *discr_lists_length)
22231 tree variant_part_type = TREE_TYPE (variant_part_decl);
22232 tree variant;
22233 dw_discr_list_ref *discr_lists;
22234 unsigned i;
22236 /* Compute how many variants there are in this variant part. */
22237 *discr_lists_length = 0;
22238 for (variant = TYPE_FIELDS (variant_part_type);
22239 variant != NULL_TREE;
22240 variant = DECL_CHAIN (variant))
22241 ++*discr_lists_length;
22243 *discr_decl = NULL_TREE;
22244 *discr_lists_p
22245 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22246 sizeof (**discr_lists_p));
22247 discr_lists = *discr_lists_p;
22249 /* And then analyze all variants to extract discriminant information for all
22250 of them. This analysis is conservative: as soon as we detect something we
22251 do not support, abort everything and pretend we found nothing. */
22252 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22253 variant != NULL_TREE;
22254 variant = DECL_CHAIN (variant), ++i)
22256 tree match_expr = DECL_QUALIFIER (variant);
22258 /* Now, try to analyze the predicate and deduce a discriminant for
22259 it. */
22260 if (match_expr == boolean_true_node)
22261 /* Typically happens for the default variant: it matches all cases that
22262 previous variants rejected. Don't output any matching value for
22263 this one. */
22264 continue;
22266 /* The following loop tries to iterate over each discriminant
22267 possibility: single values or ranges. */
22268 while (match_expr != NULL_TREE)
22270 tree next_round_match_expr;
22271 tree candidate_discr = NULL_TREE;
22272 dw_discr_list_ref new_node = NULL;
22274 /* Possibilities are matched one after the other by nested
22275 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22276 continue with the rest at next iteration. */
22277 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22279 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22280 match_expr = TREE_OPERAND (match_expr, 1);
22282 else
22283 next_round_match_expr = NULL_TREE;
22285 if (match_expr == boolean_false_node)
22286 /* This sub-expression matches nothing: just wait for the next
22287 one. */
22290 else if (TREE_CODE (match_expr) == EQ_EXPR)
22292 /* We are matching: <discr_field> == <integer_cst>
22293 This sub-expression matches a single value. */
22294 tree integer_cst = TREE_OPERAND (match_expr, 1);
22296 candidate_discr
22297 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22298 struct_type);
22300 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22301 if (!get_discr_value (integer_cst,
22302 &new_node->dw_discr_lower_bound))
22303 goto abort;
22304 new_node->dw_discr_range = false;
22307 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22309 /* We are matching:
22310 <discr_field> > <integer_cst>
22311 && <discr_field> < <integer_cst>.
22312 This sub-expression matches the range of values between the
22313 two matched integer constants. Note that comparisons can be
22314 inclusive or exclusive. */
22315 tree candidate_discr_1, candidate_discr_2;
22316 tree lower_cst, upper_cst;
22317 bool lower_cst_included, upper_cst_included;
22318 tree lower_op = TREE_OPERAND (match_expr, 0);
22319 tree upper_op = TREE_OPERAND (match_expr, 1);
22321 /* When the comparison is exclusive, the integer constant is not
22322 the discriminant range bound we are looking for: we will have
22323 to increment or decrement it. */
22324 if (TREE_CODE (lower_op) == GE_EXPR)
22325 lower_cst_included = true;
22326 else if (TREE_CODE (lower_op) == GT_EXPR)
22327 lower_cst_included = false;
22328 else
22329 goto abort;
22331 if (TREE_CODE (upper_op) == LE_EXPR)
22332 upper_cst_included = true;
22333 else if (TREE_CODE (upper_op) == LT_EXPR)
22334 upper_cst_included = false;
22335 else
22336 goto abort;
22338 /* Extract the discriminant from the first operand and check it
22339 is consistant with the same analysis in the second
22340 operand. */
22341 candidate_discr_1
22342 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22343 struct_type);
22344 candidate_discr_2
22345 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22346 struct_type);
22347 if (candidate_discr_1 == candidate_discr_2)
22348 candidate_discr = candidate_discr_1;
22349 else
22350 goto abort;
22352 /* Extract bounds from both. */
22353 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22354 lower_cst = TREE_OPERAND (lower_op, 1);
22355 upper_cst = TREE_OPERAND (upper_op, 1);
22357 if (!lower_cst_included)
22358 lower_cst
22359 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22360 lower_cst,
22361 build_int_cst (TREE_TYPE (lower_cst), 1)));
22362 if (!upper_cst_included)
22363 upper_cst
22364 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22365 upper_cst,
22366 build_int_cst (TREE_TYPE (upper_cst), 1)));
22368 if (!get_discr_value (lower_cst,
22369 &new_node->dw_discr_lower_bound)
22370 || !get_discr_value (upper_cst,
22371 &new_node->dw_discr_upper_bound))
22372 goto abort;
22374 new_node->dw_discr_range = true;
22377 else
22378 /* Unsupported sub-expression: we cannot determine the set of
22379 matching discriminant values. Abort everything. */
22380 goto abort;
22382 /* If the discriminant info is not consistant with what we saw so
22383 far, consider the analysis failed and abort everything. */
22384 if (candidate_discr == NULL_TREE
22385 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22386 goto abort;
22387 else
22388 *discr_decl = candidate_discr;
22390 if (new_node != NULL)
22392 new_node->dw_discr_next = discr_lists[i];
22393 discr_lists[i] = new_node;
22395 match_expr = next_round_match_expr;
22399 /* If we reach this point, we could match everything we were interested
22400 in. */
22401 return;
22403 abort:
22404 /* Clean all data structure and return no result. */
22405 free (*discr_lists_p);
22406 *discr_lists_p = NULL;
22407 *discr_decl = NULL_TREE;
22410 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22411 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22412 under CONTEXT_DIE.
22414 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22415 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22416 this type, which are record types, represent the available variants and each
22417 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22418 values are inferred from these attributes.
22420 In trees, the offsets for the fields inside these sub-records are relative
22421 to the variant part itself, whereas the corresponding DIEs should have
22422 offset attributes that are relative to the embedding record base address.
22423 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22424 must be an expression that computes the offset of the variant part to
22425 describe in DWARF. */
22427 static void
22428 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22429 dw_die_ref context_die)
22431 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22432 tree variant_part_offset = vlr_ctx->variant_part_offset;
22433 struct loc_descr_context ctx = {
22434 vlr_ctx->struct_type, /* context_type */
22435 NULL_TREE, /* base_decl */
22436 NULL /* dpi */
22439 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22440 NULL_TREE if there is no such field. */
22441 tree discr_decl = NULL_TREE;
22442 dw_discr_list_ref *discr_lists;
22443 unsigned discr_lists_length = 0;
22444 unsigned i;
22446 dw_die_ref dwarf_proc_die = NULL;
22447 dw_die_ref variant_part_die
22448 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22450 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22452 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22453 &discr_decl, &discr_lists, &discr_lists_length);
22455 if (discr_decl != NULL_TREE)
22457 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22459 if (discr_die)
22460 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22461 else
22462 /* We have no DIE for the discriminant, so just discard all
22463 discrimimant information in the output. */
22464 discr_decl = NULL_TREE;
22467 /* If the offset for this variant part is more complex than a constant,
22468 create a DWARF procedure for it so that we will not have to generate DWARF
22469 expressions for it for each member. */
22470 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22471 && (dwarf_version >= 3 || !dwarf_strict))
22473 const tree dwarf_proc_fndecl
22474 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22475 build_function_type (TREE_TYPE (variant_part_offset),
22476 NULL_TREE));
22477 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22478 const dw_loc_descr_ref dwarf_proc_body
22479 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22481 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22482 dwarf_proc_fndecl, context_die);
22483 if (dwarf_proc_die != NULL)
22484 variant_part_offset = dwarf_proc_call;
22487 /* Output DIEs for all variants. */
22488 i = 0;
22489 for (tree variant = TYPE_FIELDS (variant_part_type);
22490 variant != NULL_TREE;
22491 variant = DECL_CHAIN (variant), ++i)
22493 tree variant_type = TREE_TYPE (variant);
22494 dw_die_ref variant_die;
22496 /* All variants (i.e. members of a variant part) are supposed to be
22497 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22498 under these records. */
22499 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22501 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22502 equate_decl_number_to_die (variant, variant_die);
22504 /* Output discriminant values this variant matches, if any. */
22505 if (discr_decl == NULL || discr_lists[i] == NULL)
22506 /* In the case we have discriminant information at all, this is
22507 probably the default variant: as the standard says, don't
22508 output any discriminant value/list attribute. */
22510 else if (discr_lists[i]->dw_discr_next == NULL
22511 && !discr_lists[i]->dw_discr_range)
22512 /* If there is only one accepted value, don't bother outputting a
22513 list. */
22514 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22515 else
22516 add_discr_list (variant_die, discr_lists[i]);
22518 for (tree member = TYPE_FIELDS (variant_type);
22519 member != NULL_TREE;
22520 member = DECL_CHAIN (member))
22522 struct vlr_context vlr_sub_ctx = {
22523 vlr_ctx->struct_type, /* struct_type */
22524 NULL /* variant_part_offset */
22526 if (is_variant_part (member))
22528 /* All offsets for fields inside variant parts are relative to
22529 the top-level embedding RECORD_TYPE's base address. On the
22530 other hand, offsets in GCC's types are relative to the
22531 nested-most variant part. So we have to sum offsets each time
22532 we recurse. */
22534 vlr_sub_ctx.variant_part_offset
22535 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22536 variant_part_offset, byte_position (member)));
22537 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22539 else
22541 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22542 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22547 free (discr_lists);
22550 /* Generate a DIE for a class member. */
22552 static void
22553 gen_member_die (tree type, dw_die_ref context_die)
22555 tree member;
22556 tree binfo = TYPE_BINFO (type);
22557 dw_die_ref child;
22559 /* If this is not an incomplete type, output descriptions of each of its
22560 members. Note that as we output the DIEs necessary to represent the
22561 members of this record or union type, we will also be trying to output
22562 DIEs to represent the *types* of those members. However the `type'
22563 function (above) will specifically avoid generating type DIEs for member
22564 types *within* the list of member DIEs for this (containing) type except
22565 for those types (of members) which are explicitly marked as also being
22566 members of this (containing) type themselves. The g++ front- end can
22567 force any given type to be treated as a member of some other (containing)
22568 type by setting the TYPE_CONTEXT of the given (member) type to point to
22569 the TREE node representing the appropriate (containing) type. */
22571 /* First output info about the base classes. */
22572 if (binfo)
22574 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22575 int i;
22576 tree base;
22578 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22579 gen_inheritance_die (base,
22580 (accesses ? (*accesses)[i] : access_public_node),
22581 type,
22582 context_die);
22585 /* Now output info about the data members and type members. */
22586 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22588 struct vlr_context vlr_ctx = { type, NULL_TREE };
22590 /* If we thought we were generating minimal debug info for TYPE
22591 and then changed our minds, some of the member declarations
22592 may have already been defined. Don't define them again, but
22593 do put them in the right order. */
22595 child = lookup_decl_die (member);
22596 if (child)
22597 splice_child_die (context_die, child);
22599 /* Do not generate standard DWARF for variant parts if we are generating
22600 the corresponding GNAT encodings: DIEs generated for both would
22601 conflict in our mappings. */
22602 else if (is_variant_part (member)
22603 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22605 vlr_ctx.variant_part_offset = byte_position (member);
22606 gen_variant_part (member, &vlr_ctx, context_die);
22608 else
22610 vlr_ctx.variant_part_offset = NULL_TREE;
22611 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22615 /* We do not keep type methods in type variants. */
22616 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22617 /* Now output info about the function members (if any). */
22618 if (TYPE_METHODS (type) != error_mark_node)
22619 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22621 /* Don't include clones in the member list. */
22622 if (DECL_ABSTRACT_ORIGIN (member))
22623 continue;
22624 /* Nor constructors for anonymous classes. */
22625 if (DECL_ARTIFICIAL (member)
22626 && dwarf2_name (member, 0) == NULL)
22627 continue;
22629 child = lookup_decl_die (member);
22630 if (child)
22631 splice_child_die (context_die, child);
22632 else
22633 gen_decl_die (member, NULL, NULL, context_die);
22637 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22638 is set, we pretend that the type was never defined, so we only get the
22639 member DIEs needed by later specification DIEs. */
22641 static void
22642 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22643 enum debug_info_usage usage)
22645 if (TREE_ASM_WRITTEN (type))
22647 /* Fill in the bound of variable-length fields in late dwarf if
22648 still incomplete. */
22649 if (!early_dwarf && variably_modified_type_p (type, NULL))
22650 for (tree member = TYPE_FIELDS (type);
22651 member;
22652 member = DECL_CHAIN (member))
22653 fill_variable_array_bounds (TREE_TYPE (member));
22654 return;
22657 dw_die_ref type_die = lookup_type_die (type);
22658 dw_die_ref scope_die = 0;
22659 int nested = 0;
22660 int complete = (TYPE_SIZE (type)
22661 && (! TYPE_STUB_DECL (type)
22662 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22663 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22664 complete = complete && should_emit_struct_debug (type, usage);
22666 if (type_die && ! complete)
22667 return;
22669 if (TYPE_CONTEXT (type) != NULL_TREE
22670 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22671 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22672 nested = 1;
22674 scope_die = scope_die_for (type, context_die);
22676 /* Generate child dies for template paramaters. */
22677 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22678 schedule_generic_params_dies_gen (type);
22680 if (! type_die || (nested && is_cu_die (scope_die)))
22681 /* First occurrence of type or toplevel definition of nested class. */
22683 dw_die_ref old_die = type_die;
22685 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22686 ? record_type_tag (type) : DW_TAG_union_type,
22687 scope_die, type);
22688 equate_type_number_to_die (type, type_die);
22689 if (old_die)
22690 add_AT_specification (type_die, old_die);
22691 else
22692 add_name_attribute (type_die, type_tag (type));
22694 else
22695 remove_AT (type_die, DW_AT_declaration);
22697 /* If this type has been completed, then give it a byte_size attribute and
22698 then give a list of members. */
22699 if (complete && !ns_decl)
22701 /* Prevent infinite recursion in cases where the type of some member of
22702 this type is expressed in terms of this type itself. */
22703 TREE_ASM_WRITTEN (type) = 1;
22704 add_byte_size_attribute (type_die, type);
22705 if (TYPE_STUB_DECL (type) != NULL_TREE)
22707 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22708 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22711 /* If the first reference to this type was as the return type of an
22712 inline function, then it may not have a parent. Fix this now. */
22713 if (type_die->die_parent == NULL)
22714 add_child_die (scope_die, type_die);
22716 push_decl_scope (type);
22717 gen_member_die (type, type_die);
22718 pop_decl_scope ();
22720 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22721 if (TYPE_ARTIFICIAL (type))
22722 add_AT_flag (type_die, DW_AT_artificial, 1);
22724 /* GNU extension: Record what type our vtable lives in. */
22725 if (TYPE_VFIELD (type))
22727 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22729 gen_type_die (vtype, context_die);
22730 add_AT_die_ref (type_die, DW_AT_containing_type,
22731 lookup_type_die (vtype));
22734 else
22736 add_AT_flag (type_die, DW_AT_declaration, 1);
22738 /* We don't need to do this for function-local types. */
22739 if (TYPE_STUB_DECL (type)
22740 && ! decl_function_context (TYPE_STUB_DECL (type)))
22741 vec_safe_push (incomplete_types, type);
22744 if (get_AT (type_die, DW_AT_name))
22745 add_pubtype (type, type_die);
22748 /* Generate a DIE for a subroutine _type_. */
22750 static void
22751 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22753 tree return_type = TREE_TYPE (type);
22754 dw_die_ref subr_die
22755 = new_die (DW_TAG_subroutine_type,
22756 scope_die_for (type, context_die), type);
22758 equate_type_number_to_die (type, subr_die);
22759 add_prototyped_attribute (subr_die, type);
22760 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22761 context_die);
22762 gen_formal_types_die (type, subr_die);
22764 if (get_AT (subr_die, DW_AT_name))
22765 add_pubtype (type, subr_die);
22768 /* Generate a DIE for a type definition. */
22770 static void
22771 gen_typedef_die (tree decl, dw_die_ref context_die)
22773 dw_die_ref type_die;
22774 tree origin;
22776 if (TREE_ASM_WRITTEN (decl))
22778 if (DECL_ORIGINAL_TYPE (decl))
22779 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22780 return;
22783 TREE_ASM_WRITTEN (decl) = 1;
22784 type_die = new_die (DW_TAG_typedef, context_die, decl);
22785 origin = decl_ultimate_origin (decl);
22786 if (origin != NULL)
22787 add_abstract_origin_attribute (type_die, origin);
22788 else
22790 tree type;
22792 add_name_and_src_coords_attributes (type_die, decl);
22793 if (DECL_ORIGINAL_TYPE (decl))
22795 type = DECL_ORIGINAL_TYPE (decl);
22797 if (type == error_mark_node)
22798 return;
22800 gcc_assert (type != TREE_TYPE (decl));
22801 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22803 else
22805 type = TREE_TYPE (decl);
22807 if (type == error_mark_node)
22808 return;
22810 if (is_naming_typedef_decl (TYPE_NAME (type)))
22812 /* Here, we are in the case of decl being a typedef naming
22813 an anonymous type, e.g:
22814 typedef struct {...} foo;
22815 In that case TREE_TYPE (decl) is not a typedef variant
22816 type and TYPE_NAME of the anonymous type is set to the
22817 TYPE_DECL of the typedef. This construct is emitted by
22818 the C++ FE.
22820 TYPE is the anonymous struct named by the typedef
22821 DECL. As we need the DW_AT_type attribute of the
22822 DW_TAG_typedef to point to the DIE of TYPE, let's
22823 generate that DIE right away. add_type_attribute
22824 called below will then pick (via lookup_type_die) that
22825 anonymous struct DIE. */
22826 if (!TREE_ASM_WRITTEN (type))
22827 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22829 /* This is a GNU Extension. We are adding a
22830 DW_AT_linkage_name attribute to the DIE of the
22831 anonymous struct TYPE. The value of that attribute
22832 is the name of the typedef decl naming the anonymous
22833 struct. This greatly eases the work of consumers of
22834 this debug info. */
22835 add_linkage_name_raw (lookup_type_die (type), decl);
22839 add_type_attribute (type_die, type, decl_quals (decl), false,
22840 context_die);
22842 if (is_naming_typedef_decl (decl))
22843 /* We want that all subsequent calls to lookup_type_die with
22844 TYPE in argument yield the DW_TAG_typedef we have just
22845 created. */
22846 equate_type_number_to_die (type, type_die);
22848 add_accessibility_attribute (type_die, decl);
22851 if (DECL_ABSTRACT_P (decl))
22852 equate_decl_number_to_die (decl, type_die);
22854 if (get_AT (type_die, DW_AT_name))
22855 add_pubtype (decl, type_die);
22858 /* Generate a DIE for a struct, class, enum or union type. */
22860 static void
22861 gen_tagged_type_die (tree type,
22862 dw_die_ref context_die,
22863 enum debug_info_usage usage)
22865 int need_pop;
22867 if (type == NULL_TREE
22868 || !is_tagged_type (type))
22869 return;
22871 if (TREE_ASM_WRITTEN (type))
22872 need_pop = 0;
22873 /* If this is a nested type whose containing class hasn't been written
22874 out yet, writing it out will cover this one, too. This does not apply
22875 to instantiations of member class templates; they need to be added to
22876 the containing class as they are generated. FIXME: This hurts the
22877 idea of combining type decls from multiple TUs, since we can't predict
22878 what set of template instantiations we'll get. */
22879 else if (TYPE_CONTEXT (type)
22880 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22881 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22883 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22885 if (TREE_ASM_WRITTEN (type))
22886 return;
22888 /* If that failed, attach ourselves to the stub. */
22889 push_decl_scope (TYPE_CONTEXT (type));
22890 context_die = lookup_type_die (TYPE_CONTEXT (type));
22891 need_pop = 1;
22893 else if (TYPE_CONTEXT (type) != NULL_TREE
22894 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22896 /* If this type is local to a function that hasn't been written
22897 out yet, use a NULL context for now; it will be fixed up in
22898 decls_for_scope. */
22899 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22900 /* A declaration DIE doesn't count; nested types need to go in the
22901 specification. */
22902 if (context_die && is_declaration_die (context_die))
22903 context_die = NULL;
22904 need_pop = 0;
22906 else
22908 context_die = declare_in_namespace (type, context_die);
22909 need_pop = 0;
22912 if (TREE_CODE (type) == ENUMERAL_TYPE)
22914 /* This might have been written out by the call to
22915 declare_in_namespace. */
22916 if (!TREE_ASM_WRITTEN (type))
22917 gen_enumeration_type_die (type, context_die);
22919 else
22920 gen_struct_or_union_type_die (type, context_die, usage);
22922 if (need_pop)
22923 pop_decl_scope ();
22925 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22926 it up if it is ever completed. gen_*_type_die will set it for us
22927 when appropriate. */
22930 /* Generate a type description DIE. */
22932 static void
22933 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22934 enum debug_info_usage usage)
22936 struct array_descr_info info;
22938 if (type == NULL_TREE || type == error_mark_node)
22939 return;
22941 if (flag_checking && type)
22942 verify_type (type);
22944 if (TYPE_NAME (type) != NULL_TREE
22945 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22946 && is_redundant_typedef (TYPE_NAME (type))
22947 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22948 /* The DECL of this type is a typedef we don't want to emit debug
22949 info for but we want debug info for its underlying typedef.
22950 This can happen for e.g, the injected-class-name of a C++
22951 type. */
22952 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22954 /* If TYPE is a typedef type variant, let's generate debug info
22955 for the parent typedef which TYPE is a type of. */
22956 if (typedef_variant_p (type))
22958 if (TREE_ASM_WRITTEN (type))
22959 return;
22961 /* Prevent broken recursion; we can't hand off to the same type. */
22962 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22964 /* Give typedefs the right scope. */
22965 context_die = scope_die_for (type, context_die);
22967 TREE_ASM_WRITTEN (type) = 1;
22969 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22970 return;
22973 /* If type is an anonymous tagged type named by a typedef, let's
22974 generate debug info for the typedef. */
22975 if (is_naming_typedef_decl (TYPE_NAME (type)))
22977 /* Use the DIE of the containing namespace as the parent DIE of
22978 the type description DIE we want to generate. */
22979 if (DECL_CONTEXT (TYPE_NAME (type))
22980 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22981 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22983 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22984 return;
22987 /* We are going to output a DIE to represent the unqualified version
22988 of this type (i.e. without any const or volatile qualifiers) so
22989 get the main variant (i.e. the unqualified version) of this type
22990 now. (Vectors and arrays are special because the debugging info is in the
22991 cloned type itself). */
22992 if (TREE_CODE (type) != VECTOR_TYPE
22993 && TREE_CODE (type) != ARRAY_TYPE)
22994 type = type_main_variant (type);
22996 /* If this is an array type with hidden descriptor, handle it first. */
22997 if (!TREE_ASM_WRITTEN (type)
22998 && lang_hooks.types.get_array_descr_info)
23000 memset (&info, 0, sizeof (info));
23001 if (lang_hooks.types.get_array_descr_info (type, &info))
23003 /* Fortran sometimes emits array types with no dimension. */
23004 gcc_assert (info.ndimensions >= 0
23005 && (info.ndimensions
23006 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23007 gen_descr_array_type_die (type, &info, context_die);
23008 TREE_ASM_WRITTEN (type) = 1;
23009 return;
23013 if (TREE_ASM_WRITTEN (type))
23015 /* Variable-length types may be incomplete even if
23016 TREE_ASM_WRITTEN. For such types, fall through to
23017 gen_array_type_die() and possibly fill in
23018 DW_AT_{upper,lower}_bound attributes. */
23019 if ((TREE_CODE (type) != ARRAY_TYPE
23020 && TREE_CODE (type) != RECORD_TYPE
23021 && TREE_CODE (type) != UNION_TYPE
23022 && TREE_CODE (type) != QUAL_UNION_TYPE)
23023 || !variably_modified_type_p (type, NULL))
23024 return;
23027 switch (TREE_CODE (type))
23029 case ERROR_MARK:
23030 break;
23032 case POINTER_TYPE:
23033 case REFERENCE_TYPE:
23034 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23035 ensures that the gen_type_die recursion will terminate even if the
23036 type is recursive. Recursive types are possible in Ada. */
23037 /* ??? We could perhaps do this for all types before the switch
23038 statement. */
23039 TREE_ASM_WRITTEN (type) = 1;
23041 /* For these types, all that is required is that we output a DIE (or a
23042 set of DIEs) to represent the "basis" type. */
23043 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23044 DINFO_USAGE_IND_USE);
23045 break;
23047 case OFFSET_TYPE:
23048 /* This code is used for C++ pointer-to-data-member types.
23049 Output a description of the relevant class type. */
23050 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23051 DINFO_USAGE_IND_USE);
23053 /* Output a description of the type of the object pointed to. */
23054 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23055 DINFO_USAGE_IND_USE);
23057 /* Now output a DIE to represent this pointer-to-data-member type
23058 itself. */
23059 gen_ptr_to_mbr_type_die (type, context_die);
23060 break;
23062 case FUNCTION_TYPE:
23063 /* Force out return type (in case it wasn't forced out already). */
23064 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23065 DINFO_USAGE_DIR_USE);
23066 gen_subroutine_type_die (type, context_die);
23067 break;
23069 case METHOD_TYPE:
23070 /* Force out return type (in case it wasn't forced out already). */
23071 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23072 DINFO_USAGE_DIR_USE);
23073 gen_subroutine_type_die (type, context_die);
23074 break;
23076 case ARRAY_TYPE:
23077 case VECTOR_TYPE:
23078 gen_array_type_die (type, context_die);
23079 break;
23081 case ENUMERAL_TYPE:
23082 case RECORD_TYPE:
23083 case UNION_TYPE:
23084 case QUAL_UNION_TYPE:
23085 gen_tagged_type_die (type, context_die, usage);
23086 return;
23088 case VOID_TYPE:
23089 case INTEGER_TYPE:
23090 case REAL_TYPE:
23091 case FIXED_POINT_TYPE:
23092 case COMPLEX_TYPE:
23093 case BOOLEAN_TYPE:
23094 case POINTER_BOUNDS_TYPE:
23095 /* No DIEs needed for fundamental types. */
23096 break;
23098 case NULLPTR_TYPE:
23099 case LANG_TYPE:
23100 /* Just use DW_TAG_unspecified_type. */
23102 dw_die_ref type_die = lookup_type_die (type);
23103 if (type_die == NULL)
23105 tree name = TYPE_IDENTIFIER (type);
23106 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23107 type);
23108 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23109 equate_type_number_to_die (type, type_die);
23112 break;
23114 default:
23115 if (is_cxx_auto (type))
23117 tree name = TYPE_IDENTIFIER (type);
23118 dw_die_ref *die = (name == get_identifier ("auto")
23119 ? &auto_die : &decltype_auto_die);
23120 if (!*die)
23122 *die = new_die (DW_TAG_unspecified_type,
23123 comp_unit_die (), NULL_TREE);
23124 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23126 equate_type_number_to_die (type, *die);
23127 break;
23129 gcc_unreachable ();
23132 TREE_ASM_WRITTEN (type) = 1;
23135 static void
23136 gen_type_die (tree type, dw_die_ref context_die)
23138 if (type != error_mark_node)
23140 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23141 if (flag_checking)
23143 dw_die_ref die = lookup_type_die (type);
23144 if (die)
23145 check_die (die);
23150 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23151 things which are local to the given block. */
23153 static void
23154 gen_block_die (tree stmt, dw_die_ref context_die)
23156 int must_output_die = 0;
23157 bool inlined_func;
23159 /* Ignore blocks that are NULL. */
23160 if (stmt == NULL_TREE)
23161 return;
23163 inlined_func = inlined_function_outer_scope_p (stmt);
23165 /* If the block is one fragment of a non-contiguous block, do not
23166 process the variables, since they will have been done by the
23167 origin block. Do process subblocks. */
23168 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23170 tree sub;
23172 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23173 gen_block_die (sub, context_die);
23175 return;
23178 /* Determine if we need to output any Dwarf DIEs at all to represent this
23179 block. */
23180 if (inlined_func)
23181 /* The outer scopes for inlinings *must* always be represented. We
23182 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23183 must_output_die = 1;
23184 else
23186 /* Determine if this block directly contains any "significant"
23187 local declarations which we will need to output DIEs for. */
23188 if (debug_info_level > DINFO_LEVEL_TERSE)
23189 /* We are not in terse mode so *any* local declaration counts
23190 as being a "significant" one. */
23191 must_output_die = ((BLOCK_VARS (stmt) != NULL
23192 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23193 && (TREE_USED (stmt)
23194 || TREE_ASM_WRITTEN (stmt)
23195 || BLOCK_ABSTRACT (stmt)));
23196 else if ((TREE_USED (stmt)
23197 || TREE_ASM_WRITTEN (stmt)
23198 || BLOCK_ABSTRACT (stmt))
23199 && !dwarf2out_ignore_block (stmt))
23200 must_output_die = 1;
23203 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23204 DIE for any block which contains no significant local declarations at
23205 all. Rather, in such cases we just call `decls_for_scope' so that any
23206 needed Dwarf info for any sub-blocks will get properly generated. Note
23207 that in terse mode, our definition of what constitutes a "significant"
23208 local declaration gets restricted to include only inlined function
23209 instances and local (nested) function definitions. */
23210 if (must_output_die)
23212 if (inlined_func)
23214 /* If STMT block is abstract, that means we have been called
23215 indirectly from dwarf2out_abstract_function.
23216 That function rightfully marks the descendent blocks (of
23217 the abstract function it is dealing with) as being abstract,
23218 precisely to prevent us from emitting any
23219 DW_TAG_inlined_subroutine DIE as a descendent
23220 of an abstract function instance. So in that case, we should
23221 not call gen_inlined_subroutine_die.
23223 Later though, when cgraph asks dwarf2out to emit info
23224 for the concrete instance of the function decl into which
23225 the concrete instance of STMT got inlined, the later will lead
23226 to the generation of a DW_TAG_inlined_subroutine DIE. */
23227 if (! BLOCK_ABSTRACT (stmt))
23228 gen_inlined_subroutine_die (stmt, context_die);
23230 else
23231 gen_lexical_block_die (stmt, context_die);
23233 else
23234 decls_for_scope (stmt, context_die);
23237 /* Process variable DECL (or variable with origin ORIGIN) within
23238 block STMT and add it to CONTEXT_DIE. */
23239 static void
23240 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23242 dw_die_ref die;
23243 tree decl_or_origin = decl ? decl : origin;
23245 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23246 die = lookup_decl_die (decl_or_origin);
23247 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
23249 if (TYPE_DECL_IS_STUB (decl_or_origin))
23250 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23251 else
23252 die = lookup_decl_die (decl_or_origin);
23253 /* Avoid re-creating the DIE late if it was optimized as unused early. */
23254 if (! die && ! early_dwarf)
23255 return;
23257 else
23258 die = NULL;
23260 if (die != NULL && die->die_parent == NULL)
23261 add_child_die (context_die, die);
23262 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23264 if (early_dwarf)
23265 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23266 stmt, context_die);
23268 else
23269 gen_decl_die (decl, origin, NULL, context_die);
23272 /* Generate all of the decls declared within a given scope and (recursively)
23273 all of its sub-blocks. */
23275 static void
23276 decls_for_scope (tree stmt, dw_die_ref context_die)
23278 tree decl;
23279 unsigned int i;
23280 tree subblocks;
23282 /* Ignore NULL blocks. */
23283 if (stmt == NULL_TREE)
23284 return;
23286 /* Output the DIEs to represent all of the data objects and typedefs
23287 declared directly within this block but not within any nested
23288 sub-blocks. Also, nested function and tag DIEs have been
23289 generated with a parent of NULL; fix that up now. We don't
23290 have to do this if we're at -g1. */
23291 if (debug_info_level > DINFO_LEVEL_TERSE)
23293 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23294 process_scope_var (stmt, decl, NULL_TREE, context_die);
23295 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23296 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23297 context_die);
23300 /* Even if we're at -g1, we need to process the subblocks in order to get
23301 inlined call information. */
23303 /* Output the DIEs to represent all sub-blocks (and the items declared
23304 therein) of this block. */
23305 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23306 subblocks != NULL;
23307 subblocks = BLOCK_CHAIN (subblocks))
23308 gen_block_die (subblocks, context_die);
23311 /* Is this a typedef we can avoid emitting? */
23313 bool
23314 is_redundant_typedef (const_tree decl)
23316 if (TYPE_DECL_IS_STUB (decl))
23317 return true;
23319 if (DECL_ARTIFICIAL (decl)
23320 && DECL_CONTEXT (decl)
23321 && is_tagged_type (DECL_CONTEXT (decl))
23322 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23323 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23324 /* Also ignore the artificial member typedef for the class name. */
23325 return true;
23327 return false;
23330 /* Return TRUE if TYPE is a typedef that names a type for linkage
23331 purposes. This kind of typedefs is produced by the C++ FE for
23332 constructs like:
23334 typedef struct {...} foo;
23336 In that case, there is no typedef variant type produced for foo.
23337 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23338 struct type. */
23340 static bool
23341 is_naming_typedef_decl (const_tree decl)
23343 if (decl == NULL_TREE
23344 || TREE_CODE (decl) != TYPE_DECL
23345 || DECL_NAMELESS (decl)
23346 || !is_tagged_type (TREE_TYPE (decl))
23347 || DECL_IS_BUILTIN (decl)
23348 || is_redundant_typedef (decl)
23349 /* It looks like Ada produces TYPE_DECLs that are very similar
23350 to C++ naming typedefs but that have different
23351 semantics. Let's be specific to c++ for now. */
23352 || !is_cxx ())
23353 return FALSE;
23355 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23356 && TYPE_NAME (TREE_TYPE (decl)) == decl
23357 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23358 != TYPE_NAME (TREE_TYPE (decl))));
23361 /* Looks up the DIE for a context. */
23363 static inline dw_die_ref
23364 lookup_context_die (tree context)
23366 if (context)
23368 /* Find die that represents this context. */
23369 if (TYPE_P (context))
23371 context = TYPE_MAIN_VARIANT (context);
23372 dw_die_ref ctx = lookup_type_die (context);
23373 if (!ctx)
23374 return NULL;
23375 return strip_naming_typedef (context, ctx);
23377 else
23378 return lookup_decl_die (context);
23380 return comp_unit_die ();
23383 /* Returns the DIE for a context. */
23385 static inline dw_die_ref
23386 get_context_die (tree context)
23388 if (context)
23390 /* Find die that represents this context. */
23391 if (TYPE_P (context))
23393 context = TYPE_MAIN_VARIANT (context);
23394 return strip_naming_typedef (context, force_type_die (context));
23396 else
23397 return force_decl_die (context);
23399 return comp_unit_die ();
23402 /* Returns the DIE for decl. A DIE will always be returned. */
23404 static dw_die_ref
23405 force_decl_die (tree decl)
23407 dw_die_ref decl_die;
23408 unsigned saved_external_flag;
23409 tree save_fn = NULL_TREE;
23410 decl_die = lookup_decl_die (decl);
23411 if (!decl_die)
23413 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23415 decl_die = lookup_decl_die (decl);
23416 if (decl_die)
23417 return decl_die;
23419 switch (TREE_CODE (decl))
23421 case FUNCTION_DECL:
23422 /* Clear current_function_decl, so that gen_subprogram_die thinks
23423 that this is a declaration. At this point, we just want to force
23424 declaration die. */
23425 save_fn = current_function_decl;
23426 current_function_decl = NULL_TREE;
23427 gen_subprogram_die (decl, context_die);
23428 current_function_decl = save_fn;
23429 break;
23431 case VAR_DECL:
23432 /* Set external flag to force declaration die. Restore it after
23433 gen_decl_die() call. */
23434 saved_external_flag = DECL_EXTERNAL (decl);
23435 DECL_EXTERNAL (decl) = 1;
23436 gen_decl_die (decl, NULL, NULL, context_die);
23437 DECL_EXTERNAL (decl) = saved_external_flag;
23438 break;
23440 case NAMESPACE_DECL:
23441 if (dwarf_version >= 3 || !dwarf_strict)
23442 dwarf2out_decl (decl);
23443 else
23444 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23445 decl_die = comp_unit_die ();
23446 break;
23448 case TRANSLATION_UNIT_DECL:
23449 decl_die = comp_unit_die ();
23450 break;
23452 default:
23453 gcc_unreachable ();
23456 /* We should be able to find the DIE now. */
23457 if (!decl_die)
23458 decl_die = lookup_decl_die (decl);
23459 gcc_assert (decl_die);
23462 return decl_die;
23465 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23466 always returned. */
23468 static dw_die_ref
23469 force_type_die (tree type)
23471 dw_die_ref type_die;
23473 type_die = lookup_type_die (type);
23474 if (!type_die)
23476 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23478 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23479 false, context_die);
23480 gcc_assert (type_die);
23482 return type_die;
23485 /* Force out any required namespaces to be able to output DECL,
23486 and return the new context_die for it, if it's changed. */
23488 static dw_die_ref
23489 setup_namespace_context (tree thing, dw_die_ref context_die)
23491 tree context = (DECL_P (thing)
23492 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23493 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23494 /* Force out the namespace. */
23495 context_die = force_decl_die (context);
23497 return context_die;
23500 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23501 type) within its namespace, if appropriate.
23503 For compatibility with older debuggers, namespace DIEs only contain
23504 declarations; all definitions are emitted at CU scope, with
23505 DW_AT_specification pointing to the declaration (like with class
23506 members). */
23508 static dw_die_ref
23509 declare_in_namespace (tree thing, dw_die_ref context_die)
23511 dw_die_ref ns_context;
23513 if (debug_info_level <= DINFO_LEVEL_TERSE)
23514 return context_die;
23516 /* External declarations in the local scope only need to be emitted
23517 once, not once in the namespace and once in the scope.
23519 This avoids declaring the `extern' below in the
23520 namespace DIE as well as in the innermost scope:
23522 namespace S
23524 int i=5;
23525 int foo()
23527 int i=8;
23528 extern int i;
23529 return i;
23533 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23534 return context_die;
23536 /* If this decl is from an inlined function, then don't try to emit it in its
23537 namespace, as we will get confused. It would have already been emitted
23538 when the abstract instance of the inline function was emitted anyways. */
23539 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23540 return context_die;
23542 ns_context = setup_namespace_context (thing, context_die);
23544 if (ns_context != context_die)
23546 if (is_fortran ())
23547 return ns_context;
23548 if (DECL_P (thing))
23549 gen_decl_die (thing, NULL, NULL, ns_context);
23550 else
23551 gen_type_die (thing, ns_context);
23553 return context_die;
23556 /* Generate a DIE for a namespace or namespace alias. */
23558 static void
23559 gen_namespace_die (tree decl, dw_die_ref context_die)
23561 dw_die_ref namespace_die;
23563 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23564 they are an alias of. */
23565 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23567 /* Output a real namespace or module. */
23568 context_die = setup_namespace_context (decl, comp_unit_die ());
23569 namespace_die = new_die (is_fortran ()
23570 ? DW_TAG_module : DW_TAG_namespace,
23571 context_die, decl);
23572 /* For Fortran modules defined in different CU don't add src coords. */
23573 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23575 const char *name = dwarf2_name (decl, 0);
23576 if (name)
23577 add_name_attribute (namespace_die, name);
23579 else
23580 add_name_and_src_coords_attributes (namespace_die, decl);
23581 if (DECL_EXTERNAL (decl))
23582 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23583 equate_decl_number_to_die (decl, namespace_die);
23585 else
23587 /* Output a namespace alias. */
23589 /* Force out the namespace we are an alias of, if necessary. */
23590 dw_die_ref origin_die
23591 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23593 if (DECL_FILE_SCOPE_P (decl)
23594 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23595 context_die = setup_namespace_context (decl, comp_unit_die ());
23596 /* Now create the namespace alias DIE. */
23597 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23598 add_name_and_src_coords_attributes (namespace_die, decl);
23599 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23600 equate_decl_number_to_die (decl, namespace_die);
23602 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23603 if (want_pubnames ())
23604 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23607 /* Generate Dwarf debug information for a decl described by DECL.
23608 The return value is currently only meaningful for PARM_DECLs,
23609 for all other decls it returns NULL.
23611 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23612 It can be NULL otherwise. */
23614 static dw_die_ref
23615 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23616 dw_die_ref context_die)
23618 tree decl_or_origin = decl ? decl : origin;
23619 tree class_origin = NULL, ultimate_origin;
23621 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23622 return NULL;
23624 /* Ignore pointer bounds decls. */
23625 if (DECL_P (decl_or_origin)
23626 && TREE_TYPE (decl_or_origin)
23627 && POINTER_BOUNDS_P (decl_or_origin))
23628 return NULL;
23630 switch (TREE_CODE (decl_or_origin))
23632 case ERROR_MARK:
23633 break;
23635 case CONST_DECL:
23636 if (!is_fortran () && !is_ada ())
23638 /* The individual enumerators of an enum type get output when we output
23639 the Dwarf representation of the relevant enum type itself. */
23640 break;
23643 /* Emit its type. */
23644 gen_type_die (TREE_TYPE (decl), context_die);
23646 /* And its containing namespace. */
23647 context_die = declare_in_namespace (decl, context_die);
23649 gen_const_die (decl, context_die);
23650 break;
23652 case FUNCTION_DECL:
23653 /* Don't output any DIEs to represent mere function declarations,
23654 unless they are class members or explicit block externs. */
23655 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23656 && DECL_FILE_SCOPE_P (decl_or_origin)
23657 && (current_function_decl == NULL_TREE
23658 || DECL_ARTIFICIAL (decl_or_origin)))
23659 break;
23661 #if 0
23662 /* FIXME */
23663 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23664 on local redeclarations of global functions. That seems broken. */
23665 if (current_function_decl != decl)
23666 /* This is only a declaration. */;
23667 #endif
23669 /* If we're emitting a clone, emit info for the abstract instance. */
23670 if (origin || DECL_ORIGIN (decl) != decl)
23671 dwarf2out_abstract_function (origin
23672 ? DECL_ORIGIN (origin)
23673 : DECL_ABSTRACT_ORIGIN (decl));
23675 /* If we're emitting an out-of-line copy of an inline function,
23676 emit info for the abstract instance and set up to refer to it. */
23677 else if (cgraph_function_possibly_inlined_p (decl)
23678 && ! DECL_ABSTRACT_P (decl)
23679 && ! class_or_namespace_scope_p (context_die)
23680 /* dwarf2out_abstract_function won't emit a die if this is just
23681 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23682 that case, because that works only if we have a die. */
23683 && DECL_INITIAL (decl) != NULL_TREE)
23685 dwarf2out_abstract_function (decl);
23686 set_decl_origin_self (decl);
23689 /* Otherwise we're emitting the primary DIE for this decl. */
23690 else if (debug_info_level > DINFO_LEVEL_TERSE)
23692 /* Before we describe the FUNCTION_DECL itself, make sure that we
23693 have its containing type. */
23694 if (!origin)
23695 origin = decl_class_context (decl);
23696 if (origin != NULL_TREE)
23697 gen_type_die (origin, context_die);
23699 /* And its return type. */
23700 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23702 /* And its virtual context. */
23703 if (DECL_VINDEX (decl) != NULL_TREE)
23704 gen_type_die (DECL_CONTEXT (decl), context_die);
23706 /* Make sure we have a member DIE for decl. */
23707 if (origin != NULL_TREE)
23708 gen_type_die_for_member (origin, decl, context_die);
23710 /* And its containing namespace. */
23711 context_die = declare_in_namespace (decl, context_die);
23714 /* Now output a DIE to represent the function itself. */
23715 if (decl)
23716 gen_subprogram_die (decl, context_die);
23717 break;
23719 case TYPE_DECL:
23720 /* If we are in terse mode, don't generate any DIEs to represent any
23721 actual typedefs. */
23722 if (debug_info_level <= DINFO_LEVEL_TERSE)
23723 break;
23725 /* In the special case of a TYPE_DECL node representing the declaration
23726 of some type tag, if the given TYPE_DECL is marked as having been
23727 instantiated from some other (original) TYPE_DECL node (e.g. one which
23728 was generated within the original definition of an inline function) we
23729 used to generate a special (abbreviated) DW_TAG_structure_type,
23730 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23731 should be actually referencing those DIEs, as variable DIEs with that
23732 type would be emitted already in the abstract origin, so it was always
23733 removed during unused type prunning. Don't add anything in this
23734 case. */
23735 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23736 break;
23738 if (is_redundant_typedef (decl))
23739 gen_type_die (TREE_TYPE (decl), context_die);
23740 else
23741 /* Output a DIE to represent the typedef itself. */
23742 gen_typedef_die (decl, context_die);
23743 break;
23745 case LABEL_DECL:
23746 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23747 gen_label_die (decl, context_die);
23748 break;
23750 case VAR_DECL:
23751 case RESULT_DECL:
23752 /* If we are in terse mode, don't generate any DIEs to represent any
23753 variable declarations or definitions. */
23754 if (debug_info_level <= DINFO_LEVEL_TERSE)
23755 break;
23757 /* Output any DIEs that are needed to specify the type of this data
23758 object. */
23759 if (decl_by_reference_p (decl_or_origin))
23760 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23761 else
23762 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23764 /* And its containing type. */
23765 class_origin = decl_class_context (decl_or_origin);
23766 if (class_origin != NULL_TREE)
23767 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23769 /* And its containing namespace. */
23770 context_die = declare_in_namespace (decl_or_origin, context_die);
23772 /* Now output the DIE to represent the data object itself. This gets
23773 complicated because of the possibility that the VAR_DECL really
23774 represents an inlined instance of a formal parameter for an inline
23775 function. */
23776 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23777 if (ultimate_origin != NULL_TREE
23778 && TREE_CODE (ultimate_origin) == PARM_DECL)
23779 gen_formal_parameter_die (decl, origin,
23780 true /* Emit name attribute. */,
23781 context_die);
23782 else
23783 gen_variable_die (decl, origin, context_die);
23784 break;
23786 case FIELD_DECL:
23787 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23788 /* Ignore the nameless fields that are used to skip bits but handle C++
23789 anonymous unions and structs. */
23790 if (DECL_NAME (decl) != NULL_TREE
23791 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23792 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23794 gen_type_die (member_declared_type (decl), context_die);
23795 gen_field_die (decl, ctx, context_die);
23797 break;
23799 case PARM_DECL:
23800 if (DECL_BY_REFERENCE (decl_or_origin))
23801 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23802 else
23803 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23804 return gen_formal_parameter_die (decl, origin,
23805 true /* Emit name attribute. */,
23806 context_die);
23808 case NAMESPACE_DECL:
23809 if (dwarf_version >= 3 || !dwarf_strict)
23810 gen_namespace_die (decl, context_die);
23811 break;
23813 case IMPORTED_DECL:
23814 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23815 DECL_CONTEXT (decl), context_die);
23816 break;
23818 case NAMELIST_DECL:
23819 gen_namelist_decl (DECL_NAME (decl), context_die,
23820 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23821 break;
23823 default:
23824 /* Probably some frontend-internal decl. Assume we don't care. */
23825 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23826 break;
23829 return NULL;
23832 /* Output initial debug information for global DECL. Called at the
23833 end of the parsing process.
23835 This is the initial debug generation process. As such, the DIEs
23836 generated may be incomplete. A later debug generation pass
23837 (dwarf2out_late_global_decl) will augment the information generated
23838 in this pass (e.g., with complete location info). */
23840 static void
23841 dwarf2out_early_global_decl (tree decl)
23843 set_early_dwarf s;
23845 /* gen_decl_die() will set DECL_ABSTRACT because
23846 cgraph_function_possibly_inlined_p() returns true. This is in
23847 turn will cause DW_AT_inline attributes to be set.
23849 This happens because at early dwarf generation, there is no
23850 cgraph information, causing cgraph_function_possibly_inlined_p()
23851 to return true. Trick cgraph_function_possibly_inlined_p()
23852 while we generate dwarf early. */
23853 bool save = symtab->global_info_ready;
23854 symtab->global_info_ready = true;
23856 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23857 other DECLs and they can point to template types or other things
23858 that dwarf2out can't handle when done via dwarf2out_decl. */
23859 if (TREE_CODE (decl) != TYPE_DECL
23860 && TREE_CODE (decl) != PARM_DECL)
23862 tree save_fndecl = current_function_decl;
23863 if (TREE_CODE (decl) == FUNCTION_DECL)
23865 /* No cfun means the symbol has no body, so there's nothing
23866 to emit. */
23867 if (!DECL_STRUCT_FUNCTION (decl))
23868 goto early_decl_exit;
23870 current_function_decl = decl;
23872 dwarf2out_decl (decl);
23873 if (TREE_CODE (decl) == FUNCTION_DECL)
23874 current_function_decl = save_fndecl;
23876 early_decl_exit:
23877 symtab->global_info_ready = save;
23880 /* Output debug information for global decl DECL. Called from
23881 toplev.c after compilation proper has finished. */
23883 static void
23884 dwarf2out_late_global_decl (tree decl)
23886 /* Fill-in any location information we were unable to determine
23887 on the first pass. */
23888 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
23890 dw_die_ref die = lookup_decl_die (decl);
23892 /* We have to generate early debug late for LTO. */
23893 if (! die && in_lto_p)
23895 dwarf2out_decl (decl);
23896 die = lookup_decl_die (decl);
23899 if (die)
23901 /* We get called during the early debug phase via the symtab
23902 code invoking late_global_decl for symbols that are optimized
23903 out. When the early phase is not finished, do not add
23904 locations. */
23905 if (! early_dwarf_finished)
23906 tree_add_const_value_attribute_for_decl (die, decl);
23907 else
23908 add_location_or_const_value_attribute (die, decl, false);
23913 /* Output debug information for type decl DECL. Called from toplev.c
23914 and from language front ends (to record built-in types). */
23915 static void
23916 dwarf2out_type_decl (tree decl, int local)
23918 if (!local)
23920 set_early_dwarf s;
23921 dwarf2out_decl (decl);
23925 /* Output debug information for imported module or decl DECL.
23926 NAME is non-NULL name in the lexical block if the decl has been renamed.
23927 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23928 that DECL belongs to.
23929 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23930 static void
23931 dwarf2out_imported_module_or_decl_1 (tree decl,
23932 tree name,
23933 tree lexical_block,
23934 dw_die_ref lexical_block_die)
23936 expanded_location xloc;
23937 dw_die_ref imported_die = NULL;
23938 dw_die_ref at_import_die;
23940 if (TREE_CODE (decl) == IMPORTED_DECL)
23942 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23943 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23944 gcc_assert (decl);
23946 else
23947 xloc = expand_location (input_location);
23949 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23951 at_import_die = force_type_die (TREE_TYPE (decl));
23952 /* For namespace N { typedef void T; } using N::T; base_type_die
23953 returns NULL, but DW_TAG_imported_declaration requires
23954 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23955 if (!at_import_die)
23957 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23958 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23959 at_import_die = lookup_type_die (TREE_TYPE (decl));
23960 gcc_assert (at_import_die);
23963 else
23965 at_import_die = lookup_decl_die (decl);
23966 if (!at_import_die)
23968 /* If we're trying to avoid duplicate debug info, we may not have
23969 emitted the member decl for this field. Emit it now. */
23970 if (TREE_CODE (decl) == FIELD_DECL)
23972 tree type = DECL_CONTEXT (decl);
23974 if (TYPE_CONTEXT (type)
23975 && TYPE_P (TYPE_CONTEXT (type))
23976 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23977 DINFO_USAGE_DIR_USE))
23978 return;
23979 gen_type_die_for_member (type, decl,
23980 get_context_die (TYPE_CONTEXT (type)));
23982 if (TREE_CODE (decl) == NAMELIST_DECL)
23983 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23984 get_context_die (DECL_CONTEXT (decl)),
23985 NULL_TREE);
23986 else
23987 at_import_die = force_decl_die (decl);
23991 if (TREE_CODE (decl) == NAMESPACE_DECL)
23993 if (dwarf_version >= 3 || !dwarf_strict)
23994 imported_die = new_die (DW_TAG_imported_module,
23995 lexical_block_die,
23996 lexical_block);
23997 else
23998 return;
24000 else
24001 imported_die = new_die (DW_TAG_imported_declaration,
24002 lexical_block_die,
24003 lexical_block);
24005 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
24006 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
24007 if (name)
24008 add_AT_string (imported_die, DW_AT_name,
24009 IDENTIFIER_POINTER (name));
24010 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
24013 /* Output debug information for imported module or decl DECL.
24014 NAME is non-NULL name in context if the decl has been renamed.
24015 CHILD is true if decl is one of the renamed decls as part of
24016 importing whole module. */
24018 static void
24019 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24020 bool child)
24022 /* dw_die_ref at_import_die; */
24023 dw_die_ref scope_die;
24025 if (debug_info_level <= DINFO_LEVEL_TERSE)
24026 return;
24028 gcc_assert (decl);
24030 set_early_dwarf s;
24032 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24033 We need decl DIE for reference and scope die. First, get DIE for the decl
24034 itself. */
24036 /* Get the scope die for decl context. Use comp_unit_die for global module
24037 or decl. If die is not found for non globals, force new die. */
24038 if (context
24039 && TYPE_P (context)
24040 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24041 return;
24043 scope_die = get_context_die (context);
24045 if (child)
24047 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
24048 there is nothing we can do, here. */
24049 if (dwarf_version < 3 && dwarf_strict)
24050 return;
24052 gcc_assert (scope_die->die_child);
24053 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24054 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24055 scope_die = scope_die->die_child;
24058 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24059 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24062 /* Output debug information for namelists. */
24064 static dw_die_ref
24065 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24067 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24068 tree value;
24069 unsigned i;
24071 if (debug_info_level <= DINFO_LEVEL_TERSE)
24072 return NULL;
24074 gcc_assert (scope_die != NULL);
24075 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24076 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24078 /* If there are no item_decls, we have a nondefining namelist, e.g.
24079 with USE association; hence, set DW_AT_declaration. */
24080 if (item_decls == NULL_TREE)
24082 add_AT_flag (nml_die, DW_AT_declaration, 1);
24083 return nml_die;
24086 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24088 nml_item_ref_die = lookup_decl_die (value);
24089 if (!nml_item_ref_die)
24090 nml_item_ref_die = force_decl_die (value);
24092 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24093 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24095 return nml_die;
24099 /* Write the debugging output for DECL and return the DIE. */
24101 static void
24102 dwarf2out_decl (tree decl)
24104 dw_die_ref context_die = comp_unit_die ();
24106 switch (TREE_CODE (decl))
24108 case ERROR_MARK:
24109 return;
24111 case FUNCTION_DECL:
24112 /* What we would really like to do here is to filter out all mere
24113 file-scope declarations of file-scope functions which are never
24114 referenced later within this translation unit (and keep all of ones
24115 that *are* referenced later on) but we aren't clairvoyant, so we have
24116 no idea which functions will be referenced in the future (i.e. later
24117 on within the current translation unit). So here we just ignore all
24118 file-scope function declarations which are not also definitions. If
24119 and when the debugger needs to know something about these functions,
24120 it will have to hunt around and find the DWARF information associated
24121 with the definition of the function.
24123 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24124 nodes represent definitions and which ones represent mere
24125 declarations. We have to check DECL_INITIAL instead. That's because
24126 the C front-end supports some weird semantics for "extern inline"
24127 function definitions. These can get inlined within the current
24128 translation unit (and thus, we need to generate Dwarf info for their
24129 abstract instances so that the Dwarf info for the concrete inlined
24130 instances can have something to refer to) but the compiler never
24131 generates any out-of-lines instances of such things (despite the fact
24132 that they *are* definitions).
24134 The important point is that the C front-end marks these "extern
24135 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24136 them anyway. Note that the C++ front-end also plays some similar games
24137 for inline function definitions appearing within include files which
24138 also contain `#pragma interface' pragmas.
24140 If we are called from dwarf2out_abstract_function output a DIE
24141 anyway. We can end up here this way with early inlining and LTO
24142 where the inlined function is output in a different LTRANS unit
24143 or not at all. */
24144 if (DECL_INITIAL (decl) == NULL_TREE
24145 && ! DECL_ABSTRACT_P (decl))
24146 return;
24148 /* If we're a nested function, initially use a parent of NULL; if we're
24149 a plain function, this will be fixed up in decls_for_scope. If
24150 we're a method, it will be ignored, since we already have a DIE. */
24151 if (decl_function_context (decl)
24152 /* But if we're in terse mode, we don't care about scope. */
24153 && debug_info_level > DINFO_LEVEL_TERSE)
24154 context_die = NULL;
24155 break;
24157 case VAR_DECL:
24158 /* For local statics lookup proper context die. */
24159 if (local_function_static (decl))
24160 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24162 /* If we are in terse mode, don't generate any DIEs to represent any
24163 variable declarations or definitions. */
24164 if (debug_info_level <= DINFO_LEVEL_TERSE)
24165 return;
24166 break;
24168 case CONST_DECL:
24169 if (debug_info_level <= DINFO_LEVEL_TERSE)
24170 return;
24171 if (!is_fortran () && !is_ada ())
24172 return;
24173 if (TREE_STATIC (decl) && decl_function_context (decl))
24174 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24175 break;
24177 case NAMESPACE_DECL:
24178 case IMPORTED_DECL:
24179 if (debug_info_level <= DINFO_LEVEL_TERSE)
24180 return;
24181 if (lookup_decl_die (decl) != NULL)
24182 return;
24183 break;
24185 case TYPE_DECL:
24186 /* Don't emit stubs for types unless they are needed by other DIEs. */
24187 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24188 return;
24190 /* Don't bother trying to generate any DIEs to represent any of the
24191 normal built-in types for the language we are compiling. */
24192 if (DECL_IS_BUILTIN (decl))
24193 return;
24195 /* If we are in terse mode, don't generate any DIEs for types. */
24196 if (debug_info_level <= DINFO_LEVEL_TERSE)
24197 return;
24199 /* If we're a function-scope tag, initially use a parent of NULL;
24200 this will be fixed up in decls_for_scope. */
24201 if (decl_function_context (decl))
24202 context_die = NULL;
24204 break;
24206 case NAMELIST_DECL:
24207 break;
24209 default:
24210 return;
24213 gen_decl_die (decl, NULL, NULL, context_die);
24215 if (flag_checking)
24217 dw_die_ref die = lookup_decl_die (decl);
24218 if (die)
24219 check_die (die);
24223 /* Write the debugging output for DECL. */
24225 static void
24226 dwarf2out_function_decl (tree decl)
24228 dwarf2out_decl (decl);
24229 call_arg_locations = NULL;
24230 call_arg_loc_last = NULL;
24231 call_site_count = -1;
24232 tail_call_site_count = -1;
24233 decl_loc_table->empty ();
24234 cached_dw_loc_list_table->empty ();
24237 /* Output a marker (i.e. a label) for the beginning of the generated code for
24238 a lexical block. */
24240 static void
24241 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24242 unsigned int blocknum)
24244 switch_to_section (current_function_section ());
24245 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24248 /* Output a marker (i.e. a label) for the end of the generated code for a
24249 lexical block. */
24251 static void
24252 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24254 switch_to_section (current_function_section ());
24255 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24258 /* Returns nonzero if it is appropriate not to emit any debugging
24259 information for BLOCK, because it doesn't contain any instructions.
24261 Don't allow this for blocks with nested functions or local classes
24262 as we would end up with orphans, and in the presence of scheduling
24263 we may end up calling them anyway. */
24265 static bool
24266 dwarf2out_ignore_block (const_tree block)
24268 tree decl;
24269 unsigned int i;
24271 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24272 if (TREE_CODE (decl) == FUNCTION_DECL
24273 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24274 return 0;
24275 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24277 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24278 if (TREE_CODE (decl) == FUNCTION_DECL
24279 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24280 return 0;
24283 return 1;
24286 /* Hash table routines for file_hash. */
24288 bool
24289 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24291 return filename_cmp (p1->filename, p2) == 0;
24294 hashval_t
24295 dwarf_file_hasher::hash (dwarf_file_data *p)
24297 return htab_hash_string (p->filename);
24300 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24301 dwarf2out.c) and return its "index". The index of each (known) filename is
24302 just a unique number which is associated with only that one filename. We
24303 need such numbers for the sake of generating labels (in the .debug_sfnames
24304 section) and references to those files numbers (in the .debug_srcinfo
24305 and .debug_macinfo sections). If the filename given as an argument is not
24306 found in our current list, add it to the list and assign it the next
24307 available unique index number. */
24309 static struct dwarf_file_data *
24310 lookup_filename (const char *file_name)
24312 struct dwarf_file_data * created;
24314 if (!file_name)
24315 return NULL;
24317 dwarf_file_data **slot
24318 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24319 INSERT);
24320 if (*slot)
24321 return *slot;
24323 created = ggc_alloc<dwarf_file_data> ();
24324 created->filename = file_name;
24325 created->emitted_number = 0;
24326 *slot = created;
24327 return created;
24330 /* If the assembler will construct the file table, then translate the compiler
24331 internal file table number into the assembler file table number, and emit
24332 a .file directive if we haven't already emitted one yet. The file table
24333 numbers are different because we prune debug info for unused variables and
24334 types, which may include filenames. */
24336 static int
24337 maybe_emit_file (struct dwarf_file_data * fd)
24339 if (! fd->emitted_number)
24341 if (last_emitted_file)
24342 fd->emitted_number = last_emitted_file->emitted_number + 1;
24343 else
24344 fd->emitted_number = 1;
24345 last_emitted_file = fd;
24347 if (DWARF2_ASM_LINE_DEBUG_INFO)
24349 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24350 output_quoted_string (asm_out_file,
24351 remap_debug_filename (fd->filename));
24352 fputc ('\n', asm_out_file);
24356 return fd->emitted_number;
24359 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24360 That generation should happen after function debug info has been
24361 generated. The value of the attribute is the constant value of ARG. */
24363 static void
24364 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24366 die_arg_entry entry;
24368 if (!die || !arg)
24369 return;
24371 gcc_assert (early_dwarf);
24373 if (!tmpl_value_parm_die_table)
24374 vec_alloc (tmpl_value_parm_die_table, 32);
24376 entry.die = die;
24377 entry.arg = arg;
24378 vec_safe_push (tmpl_value_parm_die_table, entry);
24381 /* Return TRUE if T is an instance of generic type, FALSE
24382 otherwise. */
24384 static bool
24385 generic_type_p (tree t)
24387 if (t == NULL_TREE || !TYPE_P (t))
24388 return false;
24389 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24392 /* Schedule the generation of the generic parameter dies for the
24393 instance of generic type T. The proper generation itself is later
24394 done by gen_scheduled_generic_parms_dies. */
24396 static void
24397 schedule_generic_params_dies_gen (tree t)
24399 if (!generic_type_p (t))
24400 return;
24402 gcc_assert (early_dwarf);
24404 if (!generic_type_instances)
24405 vec_alloc (generic_type_instances, 256);
24407 vec_safe_push (generic_type_instances, t);
24410 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24411 by append_entry_to_tmpl_value_parm_die_table. This function must
24412 be called after function DIEs have been generated. */
24414 static void
24415 gen_remaining_tmpl_value_param_die_attribute (void)
24417 if (tmpl_value_parm_die_table)
24419 unsigned i, j;
24420 die_arg_entry *e;
24422 /* We do this in two phases - first get the cases we can
24423 handle during early-finish, preserving those we cannot
24424 (containing symbolic constants where we don't yet know
24425 whether we are going to output the referenced symbols).
24426 For those we try again at late-finish. */
24427 j = 0;
24428 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24430 if (!tree_add_const_value_attribute (e->die, e->arg))
24432 dw_loc_descr_ref loc = NULL;
24433 if (! early_dwarf
24434 && (dwarf_version >= 5 || !dwarf_strict))
24435 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24436 if (loc)
24437 add_AT_loc (e->die, DW_AT_location, loc);
24438 else
24439 (*tmpl_value_parm_die_table)[j++] = *e;
24442 tmpl_value_parm_die_table->truncate (j);
24446 /* Generate generic parameters DIEs for instances of generic types
24447 that have been previously scheduled by
24448 schedule_generic_params_dies_gen. This function must be called
24449 after all the types of the CU have been laid out. */
24451 static void
24452 gen_scheduled_generic_parms_dies (void)
24454 unsigned i;
24455 tree t;
24457 if (!generic_type_instances)
24458 return;
24460 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24461 if (COMPLETE_TYPE_P (t))
24462 gen_generic_params_dies (t);
24464 generic_type_instances = NULL;
24468 /* Replace DW_AT_name for the decl with name. */
24470 static void
24471 dwarf2out_set_name (tree decl, tree name)
24473 dw_die_ref die;
24474 dw_attr_node *attr;
24475 const char *dname;
24477 die = TYPE_SYMTAB_DIE (decl);
24478 if (!die)
24479 return;
24481 dname = dwarf2_name (name, 0);
24482 if (!dname)
24483 return;
24485 attr = get_AT (die, DW_AT_name);
24486 if (attr)
24488 struct indirect_string_node *node;
24490 node = find_AT_string (dname);
24491 /* replace the string. */
24492 attr->dw_attr_val.v.val_str = node;
24495 else
24496 add_name_attribute (die, dname);
24499 /* True if before or during processing of the first function being emitted. */
24500 static bool in_first_function_p = true;
24501 /* True if loc_note during dwarf2out_var_location call might still be
24502 before first real instruction at address equal to .Ltext0. */
24503 static bool maybe_at_text_label_p = true;
24504 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24505 static unsigned int first_loclabel_num_not_at_text_label;
24507 /* Called by the final INSN scan whenever we see a var location. We
24508 use it to drop labels in the right places, and throw the location in
24509 our lookup table. */
24511 static void
24512 dwarf2out_var_location (rtx_insn *loc_note)
24514 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24515 struct var_loc_node *newloc;
24516 rtx_insn *next_real, *next_note;
24517 rtx_insn *call_insn = NULL;
24518 static const char *last_label;
24519 static const char *last_postcall_label;
24520 static bool last_in_cold_section_p;
24521 static rtx_insn *expected_next_loc_note;
24522 tree decl;
24523 bool var_loc_p;
24525 if (!NOTE_P (loc_note))
24527 if (CALL_P (loc_note))
24529 call_site_count++;
24530 if (SIBLING_CALL_P (loc_note))
24531 tail_call_site_count++;
24532 if (optimize == 0 && !flag_var_tracking)
24534 /* When the var-tracking pass is not running, there is no note
24535 for indirect calls whose target is compile-time known. In this
24536 case, process such calls specifically so that we generate call
24537 sites for them anyway. */
24538 rtx x = PATTERN (loc_note);
24539 if (GET_CODE (x) == PARALLEL)
24540 x = XVECEXP (x, 0, 0);
24541 if (GET_CODE (x) == SET)
24542 x = SET_SRC (x);
24543 if (GET_CODE (x) == CALL)
24544 x = XEXP (x, 0);
24545 if (!MEM_P (x)
24546 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24547 || !SYMBOL_REF_DECL (XEXP (x, 0))
24548 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24549 != FUNCTION_DECL))
24551 call_insn = loc_note;
24552 loc_note = NULL;
24553 var_loc_p = false;
24555 next_real = next_real_insn (call_insn);
24556 next_note = NULL;
24557 cached_next_real_insn = NULL;
24558 goto create_label;
24562 return;
24565 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24566 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24567 return;
24569 /* Optimize processing a large consecutive sequence of location
24570 notes so we don't spend too much time in next_real_insn. If the
24571 next insn is another location note, remember the next_real_insn
24572 calculation for next time. */
24573 next_real = cached_next_real_insn;
24574 if (next_real)
24576 if (expected_next_loc_note != loc_note)
24577 next_real = NULL;
24580 next_note = NEXT_INSN (loc_note);
24581 if (! next_note
24582 || next_note->deleted ()
24583 || ! NOTE_P (next_note)
24584 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24585 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24586 next_note = NULL;
24588 if (! next_real)
24589 next_real = next_real_insn (loc_note);
24591 if (next_note)
24593 expected_next_loc_note = next_note;
24594 cached_next_real_insn = next_real;
24596 else
24597 cached_next_real_insn = NULL;
24599 /* If there are no instructions which would be affected by this note,
24600 don't do anything. */
24601 if (var_loc_p
24602 && next_real == NULL_RTX
24603 && !NOTE_DURING_CALL_P (loc_note))
24604 return;
24606 create_label:
24608 if (next_real == NULL_RTX)
24609 next_real = get_last_insn ();
24611 /* If there were any real insns between note we processed last time
24612 and this note (or if it is the first note), clear
24613 last_{,postcall_}label so that they are not reused this time. */
24614 if (last_var_location_insn == NULL_RTX
24615 || last_var_location_insn != next_real
24616 || last_in_cold_section_p != in_cold_section_p)
24618 last_label = NULL;
24619 last_postcall_label = NULL;
24622 if (var_loc_p)
24624 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24625 newloc = add_var_loc_to_decl (decl, loc_note,
24626 NOTE_DURING_CALL_P (loc_note)
24627 ? last_postcall_label : last_label);
24628 if (newloc == NULL)
24629 return;
24631 else
24633 decl = NULL_TREE;
24634 newloc = NULL;
24637 /* If there were no real insns between note we processed last time
24638 and this note, use the label we emitted last time. Otherwise
24639 create a new label and emit it. */
24640 if (last_label == NULL)
24642 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24643 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24644 loclabel_num++;
24645 last_label = ggc_strdup (loclabel);
24646 /* See if loclabel might be equal to .Ltext0. If yes,
24647 bump first_loclabel_num_not_at_text_label. */
24648 if (!have_multiple_function_sections
24649 && in_first_function_p
24650 && maybe_at_text_label_p)
24652 static rtx_insn *last_start;
24653 rtx_insn *insn;
24654 for (insn = loc_note; insn; insn = previous_insn (insn))
24655 if (insn == last_start)
24656 break;
24657 else if (!NONDEBUG_INSN_P (insn))
24658 continue;
24659 else
24661 rtx body = PATTERN (insn);
24662 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24663 continue;
24664 /* Inline asm could occupy zero bytes. */
24665 else if (GET_CODE (body) == ASM_INPUT
24666 || asm_noperands (body) >= 0)
24667 continue;
24668 #ifdef HAVE_attr_length
24669 else if (get_attr_min_length (insn) == 0)
24670 continue;
24671 #endif
24672 else
24674 /* Assume insn has non-zero length. */
24675 maybe_at_text_label_p = false;
24676 break;
24679 if (maybe_at_text_label_p)
24681 last_start = loc_note;
24682 first_loclabel_num_not_at_text_label = loclabel_num;
24687 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24688 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24690 if (!var_loc_p)
24692 struct call_arg_loc_node *ca_loc
24693 = ggc_cleared_alloc<call_arg_loc_node> ();
24694 rtx_insn *prev
24695 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24697 ca_loc->call_arg_loc_note = loc_note;
24698 ca_loc->next = NULL;
24699 ca_loc->label = last_label;
24700 gcc_assert (prev
24701 && (CALL_P (prev)
24702 || (NONJUMP_INSN_P (prev)
24703 && GET_CODE (PATTERN (prev)) == SEQUENCE
24704 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24705 if (!CALL_P (prev))
24706 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24707 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24709 /* Look for a SYMBOL_REF in the "prev" instruction. */
24710 rtx x = get_call_rtx_from (PATTERN (prev));
24711 if (x)
24713 /* Try to get the call symbol, if any. */
24714 if (MEM_P (XEXP (x, 0)))
24715 x = XEXP (x, 0);
24716 /* First, look for a memory access to a symbol_ref. */
24717 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24718 && SYMBOL_REF_DECL (XEXP (x, 0))
24719 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24720 ca_loc->symbol_ref = XEXP (x, 0);
24721 /* Otherwise, look at a compile-time known user-level function
24722 declaration. */
24723 else if (MEM_P (x)
24724 && MEM_EXPR (x)
24725 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24726 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24729 ca_loc->block = insn_scope (prev);
24730 if (call_arg_locations)
24731 call_arg_loc_last->next = ca_loc;
24732 else
24733 call_arg_locations = ca_loc;
24734 call_arg_loc_last = ca_loc;
24736 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24737 newloc->label = last_label;
24738 else
24740 if (!last_postcall_label)
24742 sprintf (loclabel, "%s-1", last_label);
24743 last_postcall_label = ggc_strdup (loclabel);
24745 newloc->label = last_postcall_label;
24748 last_var_location_insn = next_real;
24749 last_in_cold_section_p = in_cold_section_p;
24752 /* Called from finalize_size_functions for size functions so that their body
24753 can be encoded in the debug info to describe the layout of variable-length
24754 structures. */
24756 static void
24757 dwarf2out_size_function (tree decl)
24759 function_to_dwarf_procedure (decl);
24762 /* Note in one location list that text section has changed. */
24765 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24767 var_loc_list *list = *slot;
24768 if (list->first)
24769 list->last_before_switch
24770 = list->last->next ? list->last->next : list->last;
24771 return 1;
24774 /* Note in all location lists that text section has changed. */
24776 static void
24777 var_location_switch_text_section (void)
24779 if (decl_loc_table == NULL)
24780 return;
24782 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24785 /* Create a new line number table. */
24787 static dw_line_info_table *
24788 new_line_info_table (void)
24790 dw_line_info_table *table;
24792 table = ggc_cleared_alloc<dw_line_info_table> ();
24793 table->file_num = 1;
24794 table->line_num = 1;
24795 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24797 return table;
24800 /* Lookup the "current" table into which we emit line info, so
24801 that we don't have to do it for every source line. */
24803 static void
24804 set_cur_line_info_table (section *sec)
24806 dw_line_info_table *table;
24808 if (sec == text_section)
24809 table = text_section_line_info;
24810 else if (sec == cold_text_section)
24812 table = cold_text_section_line_info;
24813 if (!table)
24815 cold_text_section_line_info = table = new_line_info_table ();
24816 table->end_label = cold_end_label;
24819 else
24821 const char *end_label;
24823 if (flag_reorder_blocks_and_partition)
24825 if (in_cold_section_p)
24826 end_label = crtl->subsections.cold_section_end_label;
24827 else
24828 end_label = crtl->subsections.hot_section_end_label;
24830 else
24832 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24833 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24834 current_function_funcdef_no);
24835 end_label = ggc_strdup (label);
24838 table = new_line_info_table ();
24839 table->end_label = end_label;
24841 vec_safe_push (separate_line_info, table);
24844 if (DWARF2_ASM_LINE_DEBUG_INFO)
24845 table->is_stmt = (cur_line_info_table
24846 ? cur_line_info_table->is_stmt
24847 : DWARF_LINE_DEFAULT_IS_STMT_START);
24848 cur_line_info_table = table;
24852 /* We need to reset the locations at the beginning of each
24853 function. We can't do this in the end_function hook, because the
24854 declarations that use the locations won't have been output when
24855 that hook is called. Also compute have_multiple_function_sections here. */
24857 static void
24858 dwarf2out_begin_function (tree fun)
24860 section *sec = function_section (fun);
24862 if (sec != text_section)
24863 have_multiple_function_sections = true;
24865 if (flag_reorder_blocks_and_partition && !cold_text_section)
24867 gcc_assert (current_function_decl == fun);
24868 cold_text_section = unlikely_text_section ();
24869 switch_to_section (cold_text_section);
24870 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24871 switch_to_section (sec);
24874 dwarf2out_note_section_used ();
24875 call_site_count = 0;
24876 tail_call_site_count = 0;
24878 set_cur_line_info_table (sec);
24881 /* Helper function of dwarf2out_end_function, called only after emitting
24882 the very first function into assembly. Check if some .debug_loc range
24883 might end with a .LVL* label that could be equal to .Ltext0.
24884 In that case we must force using absolute addresses in .debug_loc ranges,
24885 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24886 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24887 list terminator.
24888 Set have_multiple_function_sections to true in that case and
24889 terminate htab traversal. */
24892 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24894 var_loc_list *entry = *slot;
24895 struct var_loc_node *node;
24897 node = entry->first;
24898 if (node && node->next && node->next->label)
24900 unsigned int i;
24901 const char *label = node->next->label;
24902 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24904 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24906 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24907 if (strcmp (label, loclabel) == 0)
24909 have_multiple_function_sections = true;
24910 return 0;
24914 return 1;
24917 /* Hook called after emitting a function into assembly.
24918 This does something only for the very first function emitted. */
24920 static void
24921 dwarf2out_end_function (unsigned int)
24923 if (in_first_function_p
24924 && !have_multiple_function_sections
24925 && first_loclabel_num_not_at_text_label
24926 && decl_loc_table)
24927 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24928 in_first_function_p = false;
24929 maybe_at_text_label_p = false;
24932 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24933 front-ends register a translation unit even before dwarf2out_init is
24934 called. */
24935 static tree main_translation_unit = NULL_TREE;
24937 /* Hook called by front-ends after they built their main translation unit.
24938 Associate comp_unit_die to UNIT. */
24940 static void
24941 dwarf2out_register_main_translation_unit (tree unit)
24943 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24944 && main_translation_unit == NULL_TREE);
24945 main_translation_unit = unit;
24946 /* If dwarf2out_init has not been called yet, it will perform the association
24947 itself looking at main_translation_unit. */
24948 if (decl_die_table != NULL)
24949 equate_decl_number_to_die (unit, comp_unit_die ());
24952 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24954 static void
24955 push_dw_line_info_entry (dw_line_info_table *table,
24956 enum dw_line_info_opcode opcode, unsigned int val)
24958 dw_line_info_entry e;
24959 e.opcode = opcode;
24960 e.val = val;
24961 vec_safe_push (table->entries, e);
24964 /* Output a label to mark the beginning of a source code line entry
24965 and record information relating to this source line, in
24966 'line_info_table' for later output of the .debug_line section. */
24967 /* ??? The discriminator parameter ought to be unsigned. */
24969 static void
24970 dwarf2out_source_line (unsigned int line, const char *filename,
24971 int discriminator, bool is_stmt)
24973 unsigned int file_num;
24974 dw_line_info_table *table;
24976 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24977 return;
24979 /* The discriminator column was added in dwarf4. Simplify the below
24980 by simply removing it if we're not supposed to output it. */
24981 if (dwarf_version < 4 && dwarf_strict)
24982 discriminator = 0;
24984 table = cur_line_info_table;
24985 file_num = maybe_emit_file (lookup_filename (filename));
24987 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24988 the debugger has used the second (possibly duplicate) line number
24989 at the beginning of the function to mark the end of the prologue.
24990 We could eliminate any other duplicates within the function. For
24991 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24992 that second line number entry. */
24993 /* Recall that this end-of-prologue indication is *not* the same thing
24994 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24995 to which the hook corresponds, follows the last insn that was
24996 emitted by gen_prologue. What we need is to precede the first insn
24997 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24998 insn that corresponds to something the user wrote. These may be
24999 very different locations once scheduling is enabled. */
25001 if (0 && file_num == table->file_num
25002 && line == table->line_num
25003 && discriminator == table->discrim_num
25004 && is_stmt == table->is_stmt)
25005 return;
25007 switch_to_section (current_function_section ());
25009 /* If requested, emit something human-readable. */
25010 if (flag_debug_asm)
25011 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
25013 if (DWARF2_ASM_LINE_DEBUG_INFO)
25015 /* Emit the .loc directive understood by GNU as. */
25016 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25017 file_num, line, is_stmt, discriminator */
25018 fputs ("\t.loc ", asm_out_file);
25019 fprint_ul (asm_out_file, file_num);
25020 putc (' ', asm_out_file);
25021 fprint_ul (asm_out_file, line);
25022 putc (' ', asm_out_file);
25023 putc ('0', asm_out_file);
25025 if (is_stmt != table->is_stmt)
25027 fputs (" is_stmt ", asm_out_file);
25028 putc (is_stmt ? '1' : '0', asm_out_file);
25030 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25032 gcc_assert (discriminator > 0);
25033 fputs (" discriminator ", asm_out_file);
25034 fprint_ul (asm_out_file, (unsigned long) discriminator);
25036 putc ('\n', asm_out_file);
25038 else
25040 unsigned int label_num = ++line_info_label_num;
25042 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25044 push_dw_line_info_entry (table, LI_set_address, label_num);
25045 if (file_num != table->file_num)
25046 push_dw_line_info_entry (table, LI_set_file, file_num);
25047 if (discriminator != table->discrim_num)
25048 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25049 if (is_stmt != table->is_stmt)
25050 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25051 push_dw_line_info_entry (table, LI_set_line, line);
25054 table->file_num = file_num;
25055 table->line_num = line;
25056 table->discrim_num = discriminator;
25057 table->is_stmt = is_stmt;
25058 table->in_use = true;
25061 /* Record the beginning of a new source file. */
25063 static void
25064 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25066 if (flag_eliminate_dwarf2_dups)
25068 /* Record the beginning of the file for break_out_includes. */
25069 dw_die_ref bincl_die;
25071 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25072 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25075 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25077 macinfo_entry e;
25078 e.code = DW_MACINFO_start_file;
25079 e.lineno = lineno;
25080 e.info = ggc_strdup (filename);
25081 vec_safe_push (macinfo_table, e);
25085 /* Record the end of a source file. */
25087 static void
25088 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25090 if (flag_eliminate_dwarf2_dups)
25091 /* Record the end of the file for break_out_includes. */
25092 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25094 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25096 macinfo_entry e;
25097 e.code = DW_MACINFO_end_file;
25098 e.lineno = lineno;
25099 e.info = NULL;
25100 vec_safe_push (macinfo_table, e);
25104 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25105 the tail part of the directive line, i.e. the part which is past the
25106 initial whitespace, #, whitespace, directive-name, whitespace part. */
25108 static void
25109 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25110 const char *buffer ATTRIBUTE_UNUSED)
25112 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25114 macinfo_entry e;
25115 /* Insert a dummy first entry to be able to optimize the whole
25116 predefined macro block using DW_MACRO_GNU_transparent_include. */
25117 if (macinfo_table->is_empty () && lineno <= 1)
25119 e.code = 0;
25120 e.lineno = 0;
25121 e.info = NULL;
25122 vec_safe_push (macinfo_table, e);
25124 e.code = DW_MACINFO_define;
25125 e.lineno = lineno;
25126 e.info = ggc_strdup (buffer);
25127 vec_safe_push (macinfo_table, e);
25131 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25132 the tail part of the directive line, i.e. the part which is past the
25133 initial whitespace, #, whitespace, directive-name, whitespace part. */
25135 static void
25136 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25137 const char *buffer ATTRIBUTE_UNUSED)
25139 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25141 macinfo_entry e;
25142 /* Insert a dummy first entry to be able to optimize the whole
25143 predefined macro block using DW_MACRO_GNU_transparent_include. */
25144 if (macinfo_table->is_empty () && lineno <= 1)
25146 e.code = 0;
25147 e.lineno = 0;
25148 e.info = NULL;
25149 vec_safe_push (macinfo_table, e);
25151 e.code = DW_MACINFO_undef;
25152 e.lineno = lineno;
25153 e.info = ggc_strdup (buffer);
25154 vec_safe_push (macinfo_table, e);
25158 /* Helpers to manipulate hash table of CUs. */
25160 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25162 static inline hashval_t hash (const macinfo_entry *);
25163 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25166 inline hashval_t
25167 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25169 return htab_hash_string (entry->info);
25172 inline bool
25173 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25174 const macinfo_entry *entry2)
25176 return !strcmp (entry1->info, entry2->info);
25179 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25181 /* Output a single .debug_macinfo entry. */
25183 static void
25184 output_macinfo_op (macinfo_entry *ref)
25186 int file_num;
25187 size_t len;
25188 struct indirect_string_node *node;
25189 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25190 struct dwarf_file_data *fd;
25192 switch (ref->code)
25194 case DW_MACINFO_start_file:
25195 fd = lookup_filename (ref->info);
25196 file_num = maybe_emit_file (fd);
25197 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25198 dw2_asm_output_data_uleb128 (ref->lineno,
25199 "Included from line number %lu",
25200 (unsigned long) ref->lineno);
25201 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25202 break;
25203 case DW_MACINFO_end_file:
25204 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25205 break;
25206 case DW_MACINFO_define:
25207 case DW_MACINFO_undef:
25208 len = strlen (ref->info) + 1;
25209 if (!dwarf_strict
25210 && len > DWARF_OFFSET_SIZE
25211 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25212 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25214 ref->code = ref->code == DW_MACINFO_define
25215 ? DW_MACRO_GNU_define_indirect
25216 : DW_MACRO_GNU_undef_indirect;
25217 output_macinfo_op (ref);
25218 return;
25220 dw2_asm_output_data (1, ref->code,
25221 ref->code == DW_MACINFO_define
25222 ? "Define macro" : "Undefine macro");
25223 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25224 (unsigned long) ref->lineno);
25225 dw2_asm_output_nstring (ref->info, -1, "The macro");
25226 break;
25227 case DW_MACRO_GNU_define_indirect:
25228 case DW_MACRO_GNU_undef_indirect:
25229 node = find_AT_string (ref->info);
25230 gcc_assert (node
25231 && ((node->form == DW_FORM_strp)
25232 || (node->form == DW_FORM_GNU_str_index)));
25233 dw2_asm_output_data (1, ref->code,
25234 ref->code == DW_MACRO_GNU_define_indirect
25235 ? "Define macro indirect"
25236 : "Undefine macro indirect");
25237 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25238 (unsigned long) ref->lineno);
25239 if (node->form == DW_FORM_strp)
25240 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25241 debug_str_section, "The macro: \"%s\"",
25242 ref->info);
25243 else
25244 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25245 ref->info);
25246 break;
25247 case DW_MACRO_GNU_transparent_include:
25248 dw2_asm_output_data (1, ref->code, "Transparent include");
25249 ASM_GENERATE_INTERNAL_LABEL (label,
25250 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25251 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25252 break;
25253 default:
25254 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25255 ASM_COMMENT_START, (unsigned long) ref->code);
25256 break;
25260 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25261 other compilation unit .debug_macinfo sections. IDX is the first
25262 index of a define/undef, return the number of ops that should be
25263 emitted in a comdat .debug_macinfo section and emit
25264 a DW_MACRO_GNU_transparent_include entry referencing it.
25265 If the define/undef entry should be emitted normally, return 0. */
25267 static unsigned
25268 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25269 macinfo_hash_type **macinfo_htab)
25271 macinfo_entry *first, *second, *cur, *inc;
25272 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25273 unsigned char checksum[16];
25274 struct md5_ctx ctx;
25275 char *grp_name, *tail;
25276 const char *base;
25277 unsigned int i, count, encoded_filename_len, linebuf_len;
25278 macinfo_entry **slot;
25280 first = &(*macinfo_table)[idx];
25281 second = &(*macinfo_table)[idx + 1];
25283 /* Optimize only if there are at least two consecutive define/undef ops,
25284 and either all of them are before first DW_MACINFO_start_file
25285 with lineno {0,1} (i.e. predefined macro block), or all of them are
25286 in some included header file. */
25287 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25288 return 0;
25289 if (vec_safe_is_empty (files))
25291 if (first->lineno > 1 || second->lineno > 1)
25292 return 0;
25294 else if (first->lineno == 0)
25295 return 0;
25297 /* Find the last define/undef entry that can be grouped together
25298 with first and at the same time compute md5 checksum of their
25299 codes, linenumbers and strings. */
25300 md5_init_ctx (&ctx);
25301 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25302 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25303 break;
25304 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25305 break;
25306 else
25308 unsigned char code = cur->code;
25309 md5_process_bytes (&code, 1, &ctx);
25310 checksum_uleb128 (cur->lineno, &ctx);
25311 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25313 md5_finish_ctx (&ctx, checksum);
25314 count = i - idx;
25316 /* From the containing include filename (if any) pick up just
25317 usable characters from its basename. */
25318 if (vec_safe_is_empty (files))
25319 base = "";
25320 else
25321 base = lbasename (files->last ().info);
25322 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25323 if (ISIDNUM (base[i]) || base[i] == '.')
25324 encoded_filename_len++;
25325 /* Count . at the end. */
25326 if (encoded_filename_len)
25327 encoded_filename_len++;
25329 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25330 linebuf_len = strlen (linebuf);
25332 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25333 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25334 + 16 * 2 + 1);
25335 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25336 tail = grp_name + 4;
25337 if (encoded_filename_len)
25339 for (i = 0; base[i]; i++)
25340 if (ISIDNUM (base[i]) || base[i] == '.')
25341 *tail++ = base[i];
25342 *tail++ = '.';
25344 memcpy (tail, linebuf, linebuf_len);
25345 tail += linebuf_len;
25346 *tail++ = '.';
25347 for (i = 0; i < 16; i++)
25348 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25350 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25351 in the empty vector entry before the first define/undef. */
25352 inc = &(*macinfo_table)[idx - 1];
25353 inc->code = DW_MACRO_GNU_transparent_include;
25354 inc->lineno = 0;
25355 inc->info = ggc_strdup (grp_name);
25356 if (!*macinfo_htab)
25357 *macinfo_htab = new macinfo_hash_type (10);
25358 /* Avoid emitting duplicates. */
25359 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25360 if (*slot != NULL)
25362 inc->code = 0;
25363 inc->info = NULL;
25364 /* If such an entry has been used before, just emit
25365 a DW_MACRO_GNU_transparent_include op. */
25366 inc = *slot;
25367 output_macinfo_op (inc);
25368 /* And clear all macinfo_entry in the range to avoid emitting them
25369 in the second pass. */
25370 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25372 cur->code = 0;
25373 cur->info = NULL;
25376 else
25378 *slot = inc;
25379 inc->lineno = (*macinfo_htab)->elements ();
25380 output_macinfo_op (inc);
25382 return count;
25385 /* Save any strings needed by the macinfo table in the debug str
25386 table. All strings must be collected into the table by the time
25387 index_string is called. */
25389 static void
25390 save_macinfo_strings (void)
25392 unsigned len;
25393 unsigned i;
25394 macinfo_entry *ref;
25396 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25398 switch (ref->code)
25400 /* Match the logic in output_macinfo_op to decide on
25401 indirect strings. */
25402 case DW_MACINFO_define:
25403 case DW_MACINFO_undef:
25404 len = strlen (ref->info) + 1;
25405 if (!dwarf_strict
25406 && len > DWARF_OFFSET_SIZE
25407 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25408 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25409 set_indirect_string (find_AT_string (ref->info));
25410 break;
25411 case DW_MACRO_GNU_define_indirect:
25412 case DW_MACRO_GNU_undef_indirect:
25413 set_indirect_string (find_AT_string (ref->info));
25414 break;
25415 default:
25416 break;
25421 /* Output macinfo section(s). */
25423 static void
25424 output_macinfo (void)
25426 unsigned i;
25427 unsigned long length = vec_safe_length (macinfo_table);
25428 macinfo_entry *ref;
25429 vec<macinfo_entry, va_gc> *files = NULL;
25430 macinfo_hash_type *macinfo_htab = NULL;
25432 if (! length)
25433 return;
25435 /* output_macinfo* uses these interchangeably. */
25436 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25437 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25438 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25439 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25441 /* For .debug_macro emit the section header. */
25442 if (!dwarf_strict)
25444 dw2_asm_output_data (2, 4, "DWARF macro version number");
25445 if (DWARF_OFFSET_SIZE == 8)
25446 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25447 else
25448 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25449 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25450 (!dwarf_split_debug_info ? debug_line_section_label
25451 : debug_skeleton_line_section_label),
25452 debug_line_section, NULL);
25455 /* In the first loop, it emits the primary .debug_macinfo section
25456 and after each emitted op the macinfo_entry is cleared.
25457 If a longer range of define/undef ops can be optimized using
25458 DW_MACRO_GNU_transparent_include, the
25459 DW_MACRO_GNU_transparent_include op is emitted and kept in
25460 the vector before the first define/undef in the range and the
25461 whole range of define/undef ops is not emitted and kept. */
25462 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25464 switch (ref->code)
25466 case DW_MACINFO_start_file:
25467 vec_safe_push (files, *ref);
25468 break;
25469 case DW_MACINFO_end_file:
25470 if (!vec_safe_is_empty (files))
25471 files->pop ();
25472 break;
25473 case DW_MACINFO_define:
25474 case DW_MACINFO_undef:
25475 if (!dwarf_strict
25476 && HAVE_COMDAT_GROUP
25477 && vec_safe_length (files) != 1
25478 && i > 0
25479 && i + 1 < length
25480 && (*macinfo_table)[i - 1].code == 0)
25482 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25483 if (count)
25485 i += count - 1;
25486 continue;
25489 break;
25490 case 0:
25491 /* A dummy entry may be inserted at the beginning to be able
25492 to optimize the whole block of predefined macros. */
25493 if (i == 0)
25494 continue;
25495 default:
25496 break;
25498 output_macinfo_op (ref);
25499 ref->info = NULL;
25500 ref->code = 0;
25503 if (!macinfo_htab)
25504 return;
25506 delete macinfo_htab;
25507 macinfo_htab = NULL;
25509 /* If any DW_MACRO_GNU_transparent_include were used, on those
25510 DW_MACRO_GNU_transparent_include entries terminate the
25511 current chain and switch to a new comdat .debug_macinfo
25512 section and emit the define/undef entries within it. */
25513 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25514 switch (ref->code)
25516 case 0:
25517 continue;
25518 case DW_MACRO_GNU_transparent_include:
25520 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25521 tree comdat_key = get_identifier (ref->info);
25522 /* Terminate the previous .debug_macinfo section. */
25523 dw2_asm_output_data (1, 0, "End compilation unit");
25524 targetm.asm_out.named_section (debug_macinfo_section_name,
25525 SECTION_DEBUG
25526 | SECTION_LINKONCE,
25527 comdat_key);
25528 ASM_GENERATE_INTERNAL_LABEL (label,
25529 DEBUG_MACRO_SECTION_LABEL,
25530 ref->lineno);
25531 ASM_OUTPUT_LABEL (asm_out_file, label);
25532 ref->code = 0;
25533 ref->info = NULL;
25534 dw2_asm_output_data (2, 4, "DWARF macro version number");
25535 if (DWARF_OFFSET_SIZE == 8)
25536 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25537 else
25538 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25540 break;
25541 case DW_MACINFO_define:
25542 case DW_MACINFO_undef:
25543 output_macinfo_op (ref);
25544 ref->code = 0;
25545 ref->info = NULL;
25546 break;
25547 default:
25548 gcc_unreachable ();
25552 /* Initialize the various sections and labels for dwarf output. */
25554 static void
25555 init_sections_and_labels (void)
25557 if (!dwarf_split_debug_info)
25559 debug_info_section = get_section (DEBUG_INFO_SECTION,
25560 SECTION_DEBUG, NULL);
25561 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25562 SECTION_DEBUG, NULL);
25563 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25564 SECTION_DEBUG, NULL);
25565 debug_macinfo_section_name
25566 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
25567 debug_macinfo_section = get_section (debug_macinfo_section_name,
25568 SECTION_DEBUG, NULL);
25570 else
25572 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25573 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25574 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25575 SECTION_DEBUG | SECTION_EXCLUDE,
25576 NULL);
25577 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25578 SECTION_DEBUG, NULL);
25579 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25580 SECTION_DEBUG, NULL);
25581 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25582 SECTION_DEBUG, NULL);
25583 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25584 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25586 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25587 the main .o, but the skeleton_line goes into the split off dwo. */
25588 debug_skeleton_line_section
25589 = get_section (DEBUG_DWO_LINE_SECTION,
25590 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25591 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25592 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25593 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25594 SECTION_DEBUG | SECTION_EXCLUDE,
25595 NULL);
25596 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25597 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25598 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25599 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25600 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25601 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25602 debug_macinfo_section_name
25603 = dwarf_strict ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
25604 debug_macinfo_section = get_section (debug_macinfo_section_name,
25605 SECTION_DEBUG | SECTION_EXCLUDE,
25606 NULL);
25608 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25609 SECTION_DEBUG, NULL);
25610 debug_line_section = get_section (DEBUG_LINE_SECTION,
25611 SECTION_DEBUG, NULL);
25612 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25613 SECTION_DEBUG, NULL);
25614 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25615 SECTION_DEBUG, NULL);
25616 debug_str_section = get_section (DEBUG_STR_SECTION,
25617 DEBUG_STR_SECTION_FLAGS, NULL);
25618 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25619 SECTION_DEBUG, NULL);
25620 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25621 SECTION_DEBUG, NULL);
25623 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25624 DEBUG_ABBREV_SECTION_LABEL, 0);
25625 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25626 DEBUG_INFO_SECTION_LABEL, 0);
25627 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25628 DEBUG_LINE_SECTION_LABEL, 0);
25629 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25630 DEBUG_RANGES_SECTION_LABEL, 0);
25631 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25632 DEBUG_ADDR_SECTION_LABEL, 0);
25633 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25634 dwarf_strict
25635 ? DEBUG_MACINFO_SECTION_LABEL
25636 : DEBUG_MACRO_SECTION_LABEL, 0);
25637 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25640 /* Set up for Dwarf output at the start of compilation. */
25642 static void
25643 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25645 /* This option is currently broken, see (PR53118 and PR46102). */
25646 if (flag_eliminate_dwarf2_dups
25647 && strstr (lang_hooks.name, "C++"))
25649 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25650 flag_eliminate_dwarf2_dups = 0;
25653 /* Allocate the file_table. */
25654 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25656 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25657 /* Allocate the decl_die_table. */
25658 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25660 /* Allocate the decl_loc_table. */
25661 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25663 /* Allocate the cached_dw_loc_list_table. */
25664 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25666 /* Allocate the initial hunk of the decl_scope_table. */
25667 vec_alloc (decl_scope_table, 256);
25669 /* Allocate the initial hunk of the abbrev_die_table. */
25670 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25671 (ABBREV_DIE_TABLE_INCREMENT);
25672 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25673 /* Zero-th entry is allocated, but unused. */
25674 abbrev_die_table_in_use = 1;
25676 /* Allocate the dwarf_proc_stack_usage_map. */
25677 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25679 /* Allocate the pubtypes and pubnames vectors. */
25680 vec_alloc (pubname_table, 32);
25681 vec_alloc (pubtype_table, 32);
25683 vec_alloc (incomplete_types, 64);
25685 vec_alloc (used_rtx_array, 32);
25687 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25688 vec_alloc (macinfo_table, 64);
25689 #endif
25691 /* If front-ends already registered a main translation unit but we were not
25692 ready to perform the association, do this now. */
25693 if (main_translation_unit != NULL_TREE)
25694 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25697 /* Called before compile () starts outputtting functions, variables
25698 and toplevel asms into assembly. */
25700 static void
25701 dwarf2out_assembly_start (void)
25703 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25704 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25705 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25706 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25707 COLD_TEXT_SECTION_LABEL, 0);
25708 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25710 switch_to_section (text_section);
25711 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25712 #endif
25714 /* Make sure the line number table for .text always exists. */
25715 text_section_line_info = new_line_info_table ();
25716 text_section_line_info->end_label = text_end_label;
25718 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25719 cur_line_info_table = text_section_line_info;
25720 #endif
25722 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25723 && dwarf2out_do_cfi_asm ()
25724 && (!(flag_unwind_tables || flag_exceptions)
25725 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25726 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25729 /* A helper function for dwarf2out_finish called through
25730 htab_traverse. Assign a string its index. All strings must be
25731 collected into the table by the time index_string is called,
25732 because the indexing code relies on htab_traverse to traverse nodes
25733 in the same order for each run. */
25736 index_string (indirect_string_node **h, unsigned int *index)
25738 indirect_string_node *node = *h;
25740 find_string_form (node);
25741 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25743 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25744 node->index = *index;
25745 *index += 1;
25747 return 1;
25750 /* A helper function for output_indirect_strings called through
25751 htab_traverse. Output the offset to a string and update the
25752 current offset. */
25755 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25757 indirect_string_node *node = *h;
25759 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25761 /* Assert that this node has been assigned an index. */
25762 gcc_assert (node->index != NO_INDEX_ASSIGNED
25763 && node->index != NOT_INDEXED);
25764 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25765 "indexed string 0x%x: %s", node->index, node->str);
25766 *offset += strlen (node->str) + 1;
25768 return 1;
25771 /* A helper function for dwarf2out_finish called through
25772 htab_traverse. Output the indexed string. */
25775 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25777 struct indirect_string_node *node = *h;
25779 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25781 /* Assert that the strings are output in the same order as their
25782 indexes were assigned. */
25783 gcc_assert (*cur_idx == node->index);
25784 assemble_string (node->str, strlen (node->str) + 1);
25785 *cur_idx += 1;
25787 return 1;
25790 /* A helper function for dwarf2out_finish called through
25791 htab_traverse. Emit one queued .debug_str string. */
25794 output_indirect_string (indirect_string_node **h, void *)
25796 struct indirect_string_node *node = *h;
25798 node->form = find_string_form (node);
25799 if (node->form == DW_FORM_strp && node->refcount > 0)
25801 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25802 assemble_string (node->str, strlen (node->str) + 1);
25805 return 1;
25808 /* Output the indexed string table. */
25810 static void
25811 output_indirect_strings (void)
25813 switch_to_section (debug_str_section);
25814 if (!dwarf_split_debug_info)
25815 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25816 else
25818 unsigned int offset = 0;
25819 unsigned int cur_idx = 0;
25821 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25823 switch_to_section (debug_str_offsets_section);
25824 debug_str_hash->traverse_noresize
25825 <unsigned int *, output_index_string_offset> (&offset);
25826 switch_to_section (debug_str_dwo_section);
25827 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25828 (&cur_idx);
25832 /* Callback for htab_traverse to assign an index to an entry in the
25833 table, and to write that entry to the .debug_addr section. */
25836 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25838 addr_table_entry *entry = *slot;
25840 if (entry->refcount == 0)
25842 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25843 || entry->index == NOT_INDEXED);
25844 return 1;
25847 gcc_assert (entry->index == *cur_index);
25848 (*cur_index)++;
25850 switch (entry->kind)
25852 case ate_kind_rtx:
25853 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25854 "0x%x", entry->index);
25855 break;
25856 case ate_kind_rtx_dtprel:
25857 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25858 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25859 DWARF2_ADDR_SIZE,
25860 entry->addr.rtl);
25861 fputc ('\n', asm_out_file);
25862 break;
25863 case ate_kind_label:
25864 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25865 "0x%x", entry->index);
25866 break;
25867 default:
25868 gcc_unreachable ();
25870 return 1;
25873 /* Produce the .debug_addr section. */
25875 static void
25876 output_addr_table (void)
25878 unsigned int index = 0;
25879 if (addr_index_table == NULL || addr_index_table->size () == 0)
25880 return;
25882 switch_to_section (debug_addr_section);
25883 addr_index_table
25884 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25887 #if ENABLE_ASSERT_CHECKING
25888 /* Verify that all marks are clear. */
25890 static void
25891 verify_marks_clear (dw_die_ref die)
25893 dw_die_ref c;
25895 gcc_assert (! die->die_mark);
25896 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25898 #endif /* ENABLE_ASSERT_CHECKING */
25900 /* Clear the marks for a die and its children.
25901 Be cool if the mark isn't set. */
25903 static void
25904 prune_unmark_dies (dw_die_ref die)
25906 dw_die_ref c;
25908 if (die->die_mark)
25909 die->die_mark = 0;
25910 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25913 /* Given LOC that is referenced by a DIE we're marking as used, find all
25914 referenced DWARF procedures it references and mark them as used. */
25916 static void
25917 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25919 for (; loc != NULL; loc = loc->dw_loc_next)
25920 switch (loc->dw_loc_opc)
25922 case DW_OP_GNU_implicit_pointer:
25923 case DW_OP_GNU_convert:
25924 case DW_OP_GNU_reinterpret:
25925 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25926 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25927 break;
25928 case DW_OP_call2:
25929 case DW_OP_call4:
25930 case DW_OP_call_ref:
25931 case DW_OP_GNU_const_type:
25932 case DW_OP_GNU_parameter_ref:
25933 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25934 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25935 break;
25936 case DW_OP_GNU_regval_type:
25937 case DW_OP_GNU_deref_type:
25938 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25939 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25940 break;
25941 case DW_OP_GNU_entry_value:
25942 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25943 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25944 break;
25945 default:
25946 break;
25950 /* Given DIE that we're marking as used, find any other dies
25951 it references as attributes and mark them as used. */
25953 static void
25954 prune_unused_types_walk_attribs (dw_die_ref die)
25956 dw_attr_node *a;
25957 unsigned ix;
25959 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25961 switch (AT_class (a))
25963 /* Make sure DWARF procedures referenced by location descriptions will
25964 get emitted. */
25965 case dw_val_class_loc:
25966 prune_unused_types_walk_loc_descr (AT_loc (a));
25967 break;
25968 case dw_val_class_loc_list:
25969 for (dw_loc_list_ref list = AT_loc_list (a);
25970 list != NULL;
25971 list = list->dw_loc_next)
25972 prune_unused_types_walk_loc_descr (list->expr);
25973 break;
25975 case dw_val_class_die_ref:
25976 /* A reference to another DIE.
25977 Make sure that it will get emitted.
25978 If it was broken out into a comdat group, don't follow it. */
25979 if (! AT_ref (a)->comdat_type_p
25980 || a->dw_attr == DW_AT_specification)
25981 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25982 break;
25984 case dw_val_class_str:
25985 /* Set the string's refcount to 0 so that prune_unused_types_mark
25986 accounts properly for it. */
25987 a->dw_attr_val.v.val_str->refcount = 0;
25988 break;
25990 default:
25991 break;
25996 /* Mark the generic parameters and arguments children DIEs of DIE. */
25998 static void
25999 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
26001 dw_die_ref c;
26003 if (die == NULL || die->die_child == NULL)
26004 return;
26005 c = die->die_child;
26008 if (is_template_parameter (c))
26009 prune_unused_types_mark (c, 1);
26010 c = c->die_sib;
26011 } while (c && c != die->die_child);
26014 /* Mark DIE as being used. If DOKIDS is true, then walk down
26015 to DIE's children. */
26017 static void
26018 prune_unused_types_mark (dw_die_ref die, int dokids)
26020 dw_die_ref c;
26022 if (die->die_mark == 0)
26024 /* We haven't done this node yet. Mark it as used. */
26025 die->die_mark = 1;
26026 /* If this is the DIE of a generic type instantiation,
26027 mark the children DIEs that describe its generic parms and
26028 args. */
26029 prune_unused_types_mark_generic_parms_dies (die);
26031 /* We also have to mark its parents as used.
26032 (But we don't want to mark our parent's kids due to this,
26033 unless it is a class.) */
26034 if (die->die_parent)
26035 prune_unused_types_mark (die->die_parent,
26036 class_scope_p (die->die_parent));
26038 /* Mark any referenced nodes. */
26039 prune_unused_types_walk_attribs (die);
26041 /* If this node is a specification,
26042 also mark the definition, if it exists. */
26043 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26044 prune_unused_types_mark (die->die_definition, 1);
26047 if (dokids && die->die_mark != 2)
26049 /* We need to walk the children, but haven't done so yet.
26050 Remember that we've walked the kids. */
26051 die->die_mark = 2;
26053 /* If this is an array type, we need to make sure our
26054 kids get marked, even if they're types. If we're
26055 breaking out types into comdat sections, do this
26056 for all type definitions. */
26057 if (die->die_tag == DW_TAG_array_type
26058 || (use_debug_types
26059 && is_type_die (die) && ! is_declaration_die (die)))
26060 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26061 else
26062 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26066 /* For local classes, look if any static member functions were emitted
26067 and if so, mark them. */
26069 static void
26070 prune_unused_types_walk_local_classes (dw_die_ref die)
26072 dw_die_ref c;
26074 if (die->die_mark == 2)
26075 return;
26077 switch (die->die_tag)
26079 case DW_TAG_structure_type:
26080 case DW_TAG_union_type:
26081 case DW_TAG_class_type:
26082 break;
26084 case DW_TAG_subprogram:
26085 if (!get_AT_flag (die, DW_AT_declaration)
26086 || die->die_definition != NULL)
26087 prune_unused_types_mark (die, 1);
26088 return;
26090 default:
26091 return;
26094 /* Mark children. */
26095 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26098 /* Walk the tree DIE and mark types that we actually use. */
26100 static void
26101 prune_unused_types_walk (dw_die_ref die)
26103 dw_die_ref c;
26105 /* Don't do anything if this node is already marked and
26106 children have been marked as well. */
26107 if (die->die_mark == 2)
26108 return;
26110 switch (die->die_tag)
26112 case DW_TAG_structure_type:
26113 case DW_TAG_union_type:
26114 case DW_TAG_class_type:
26115 if (die->die_perennial_p)
26116 break;
26118 for (c = die->die_parent; c; c = c->die_parent)
26119 if (c->die_tag == DW_TAG_subprogram)
26120 break;
26122 /* Finding used static member functions inside of classes
26123 is needed just for local classes, because for other classes
26124 static member function DIEs with DW_AT_specification
26125 are emitted outside of the DW_TAG_*_type. If we ever change
26126 it, we'd need to call this even for non-local classes. */
26127 if (c)
26128 prune_unused_types_walk_local_classes (die);
26130 /* It's a type node --- don't mark it. */
26131 return;
26133 case DW_TAG_const_type:
26134 case DW_TAG_packed_type:
26135 case DW_TAG_pointer_type:
26136 case DW_TAG_reference_type:
26137 case DW_TAG_rvalue_reference_type:
26138 case DW_TAG_volatile_type:
26139 case DW_TAG_typedef:
26140 case DW_TAG_array_type:
26141 case DW_TAG_interface_type:
26142 case DW_TAG_friend:
26143 case DW_TAG_enumeration_type:
26144 case DW_TAG_subroutine_type:
26145 case DW_TAG_string_type:
26146 case DW_TAG_set_type:
26147 case DW_TAG_subrange_type:
26148 case DW_TAG_ptr_to_member_type:
26149 case DW_TAG_file_type:
26150 /* Type nodes are useful only when other DIEs reference them --- don't
26151 mark them. */
26152 /* FALLTHROUGH */
26154 case DW_TAG_dwarf_procedure:
26155 /* Likewise for DWARF procedures. */
26157 if (die->die_perennial_p)
26158 break;
26160 return;
26162 default:
26163 /* Mark everything else. */
26164 break;
26167 if (die->die_mark == 0)
26169 die->die_mark = 1;
26171 /* Now, mark any dies referenced from here. */
26172 prune_unused_types_walk_attribs (die);
26175 die->die_mark = 2;
26177 /* Mark children. */
26178 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26181 /* Increment the string counts on strings referred to from DIE's
26182 attributes. */
26184 static void
26185 prune_unused_types_update_strings (dw_die_ref die)
26187 dw_attr_node *a;
26188 unsigned ix;
26190 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26191 if (AT_class (a) == dw_val_class_str)
26193 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26194 s->refcount++;
26195 /* Avoid unnecessarily putting strings that are used less than
26196 twice in the hash table. */
26197 if (s->refcount
26198 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26200 indirect_string_node **slot
26201 = debug_str_hash->find_slot_with_hash (s->str,
26202 htab_hash_string (s->str),
26203 INSERT);
26204 gcc_assert (*slot == NULL);
26205 *slot = s;
26210 /* Mark DIE and its children as removed. */
26212 static void
26213 mark_removed (dw_die_ref die)
26215 dw_die_ref c;
26216 die->removed = true;
26217 FOR_EACH_CHILD (die, c, mark_removed (c));
26220 /* Remove from the tree DIE any dies that aren't marked. */
26222 static void
26223 prune_unused_types_prune (dw_die_ref die)
26225 dw_die_ref c;
26227 gcc_assert (die->die_mark);
26228 prune_unused_types_update_strings (die);
26230 if (! die->die_child)
26231 return;
26233 c = die->die_child;
26234 do {
26235 dw_die_ref prev = c, next;
26236 for (c = c->die_sib; ! c->die_mark; c = next)
26237 if (c == die->die_child)
26239 /* No marked children between 'prev' and the end of the list. */
26240 if (prev == c)
26241 /* No marked children at all. */
26242 die->die_child = NULL;
26243 else
26245 prev->die_sib = c->die_sib;
26246 die->die_child = prev;
26248 c->die_sib = NULL;
26249 mark_removed (c);
26250 return;
26252 else
26254 next = c->die_sib;
26255 c->die_sib = NULL;
26256 mark_removed (c);
26259 if (c != prev->die_sib)
26260 prev->die_sib = c;
26261 prune_unused_types_prune (c);
26262 } while (c != die->die_child);
26265 /* Remove dies representing declarations that we never use. */
26267 static void
26268 prune_unused_types (void)
26270 unsigned int i;
26271 limbo_die_node *node;
26272 comdat_type_node *ctnode;
26273 pubname_entry *pub;
26274 dw_die_ref base_type;
26276 #if ENABLE_ASSERT_CHECKING
26277 /* All the marks should already be clear. */
26278 verify_marks_clear (comp_unit_die ());
26279 for (node = limbo_die_list; node; node = node->next)
26280 verify_marks_clear (node->die);
26281 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26282 verify_marks_clear (ctnode->root_die);
26283 #endif /* ENABLE_ASSERT_CHECKING */
26285 /* Mark types that are used in global variables. */
26286 premark_types_used_by_global_vars ();
26288 /* Set the mark on nodes that are actually used. */
26289 prune_unused_types_walk (comp_unit_die ());
26290 for (node = limbo_die_list; node; node = node->next)
26291 prune_unused_types_walk (node->die);
26292 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26294 prune_unused_types_walk (ctnode->root_die);
26295 prune_unused_types_mark (ctnode->type_die, 1);
26298 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26299 are unusual in that they are pubnames that are the children of pubtypes.
26300 They should only be marked via their parent DW_TAG_enumeration_type die,
26301 not as roots in themselves. */
26302 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26303 if (pub->die->die_tag != DW_TAG_enumerator)
26304 prune_unused_types_mark (pub->die, 1);
26305 for (i = 0; base_types.iterate (i, &base_type); i++)
26306 prune_unused_types_mark (base_type, 1);
26308 if (debug_str_hash)
26309 debug_str_hash->empty ();
26310 if (skeleton_debug_str_hash)
26311 skeleton_debug_str_hash->empty ();
26312 prune_unused_types_prune (comp_unit_die ());
26313 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26315 node = *pnode;
26316 if (!node->die->die_mark)
26317 *pnode = node->next;
26318 else
26320 prune_unused_types_prune (node->die);
26321 pnode = &node->next;
26324 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26325 prune_unused_types_prune (ctnode->root_die);
26327 /* Leave the marks clear. */
26328 prune_unmark_dies (comp_unit_die ());
26329 for (node = limbo_die_list; node; node = node->next)
26330 prune_unmark_dies (node->die);
26331 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26332 prune_unmark_dies (ctnode->root_die);
26335 /* Set the parameter to true if there are any relative pathnames in
26336 the file table. */
26338 file_table_relative_p (dwarf_file_data **slot, bool *p)
26340 struct dwarf_file_data *d = *slot;
26341 if (!IS_ABSOLUTE_PATH (d->filename))
26343 *p = true;
26344 return 0;
26346 return 1;
26349 /* Helpers to manipulate hash table of comdat type units. */
26351 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26353 static inline hashval_t hash (const comdat_type_node *);
26354 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26357 inline hashval_t
26358 comdat_type_hasher::hash (const comdat_type_node *type_node)
26360 hashval_t h;
26361 memcpy (&h, type_node->signature, sizeof (h));
26362 return h;
26365 inline bool
26366 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26367 const comdat_type_node *type_node_2)
26369 return (! memcmp (type_node_1->signature, type_node_2->signature,
26370 DWARF_TYPE_SIGNATURE_SIZE));
26373 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26374 to the location it would have been added, should we know its
26375 DECL_ASSEMBLER_NAME when we added other attributes. This will
26376 probably improve compactness of debug info, removing equivalent
26377 abbrevs, and hide any differences caused by deferring the
26378 computation of the assembler name, triggered by e.g. PCH. */
26380 static inline void
26381 move_linkage_attr (dw_die_ref die)
26383 unsigned ix = vec_safe_length (die->die_attr);
26384 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26386 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26387 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26389 while (--ix > 0)
26391 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26393 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26394 break;
26397 if (ix != vec_safe_length (die->die_attr) - 1)
26399 die->die_attr->pop ();
26400 die->die_attr->quick_insert (ix, linkage);
26404 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26405 referenced from typed stack ops and count how often they are used. */
26407 static void
26408 mark_base_types (dw_loc_descr_ref loc)
26410 dw_die_ref base_type = NULL;
26412 for (; loc; loc = loc->dw_loc_next)
26414 switch (loc->dw_loc_opc)
26416 case DW_OP_GNU_regval_type:
26417 case DW_OP_GNU_deref_type:
26418 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26419 break;
26420 case DW_OP_GNU_convert:
26421 case DW_OP_GNU_reinterpret:
26422 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26423 continue;
26424 /* FALLTHRU */
26425 case DW_OP_GNU_const_type:
26426 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26427 break;
26428 case DW_OP_GNU_entry_value:
26429 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26430 continue;
26431 default:
26432 continue;
26434 gcc_assert (base_type->die_parent == comp_unit_die ());
26435 if (base_type->die_mark)
26436 base_type->die_mark++;
26437 else
26439 base_types.safe_push (base_type);
26440 base_type->die_mark = 1;
26445 /* Comparison function for sorting marked base types. */
26447 static int
26448 base_type_cmp (const void *x, const void *y)
26450 dw_die_ref dx = *(const dw_die_ref *) x;
26451 dw_die_ref dy = *(const dw_die_ref *) y;
26452 unsigned int byte_size1, byte_size2;
26453 unsigned int encoding1, encoding2;
26454 if (dx->die_mark > dy->die_mark)
26455 return -1;
26456 if (dx->die_mark < dy->die_mark)
26457 return 1;
26458 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26459 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26460 if (byte_size1 < byte_size2)
26461 return 1;
26462 if (byte_size1 > byte_size2)
26463 return -1;
26464 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26465 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26466 if (encoding1 < encoding2)
26467 return 1;
26468 if (encoding1 > encoding2)
26469 return -1;
26470 return 0;
26473 /* Move base types marked by mark_base_types as early as possible
26474 in the CU, sorted by decreasing usage count both to make the
26475 uleb128 references as small as possible and to make sure they
26476 will have die_offset already computed by calc_die_sizes when
26477 sizes of typed stack loc ops is computed. */
26479 static void
26480 move_marked_base_types (void)
26482 unsigned int i;
26483 dw_die_ref base_type, die, c;
26485 if (base_types.is_empty ())
26486 return;
26488 /* Sort by decreasing usage count, they will be added again in that
26489 order later on. */
26490 base_types.qsort (base_type_cmp);
26491 die = comp_unit_die ();
26492 c = die->die_child;
26495 dw_die_ref prev = c;
26496 c = c->die_sib;
26497 while (c->die_mark)
26499 remove_child_with_prev (c, prev);
26500 /* As base types got marked, there must be at least
26501 one node other than DW_TAG_base_type. */
26502 gcc_assert (die->die_child != NULL);
26503 c = prev->die_sib;
26506 while (c != die->die_child);
26507 gcc_assert (die->die_child);
26508 c = die->die_child;
26509 for (i = 0; base_types.iterate (i, &base_type); i++)
26511 base_type->die_mark = 0;
26512 base_type->die_sib = c->die_sib;
26513 c->die_sib = base_type;
26514 c = base_type;
26518 /* Helper function for resolve_addr, attempt to resolve
26519 one CONST_STRING, return true if successful. Similarly verify that
26520 SYMBOL_REFs refer to variables emitted in the current CU. */
26522 static bool
26523 resolve_one_addr (rtx *addr)
26525 rtx rtl = *addr;
26527 if (GET_CODE (rtl) == CONST_STRING)
26529 size_t len = strlen (XSTR (rtl, 0)) + 1;
26530 tree t = build_string (len, XSTR (rtl, 0));
26531 tree tlen = size_int (len - 1);
26532 TREE_TYPE (t)
26533 = build_array_type (char_type_node, build_index_type (tlen));
26534 rtl = lookup_constant_def (t);
26535 if (!rtl || !MEM_P (rtl))
26536 return false;
26537 rtl = XEXP (rtl, 0);
26538 if (GET_CODE (rtl) == SYMBOL_REF
26539 && SYMBOL_REF_DECL (rtl)
26540 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26541 return false;
26542 vec_safe_push (used_rtx_array, rtl);
26543 *addr = rtl;
26544 return true;
26547 if (GET_CODE (rtl) == SYMBOL_REF
26548 && SYMBOL_REF_DECL (rtl))
26550 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26552 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26553 return false;
26555 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26556 return false;
26559 if (GET_CODE (rtl) == CONST)
26561 subrtx_ptr_iterator::array_type array;
26562 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26563 if (!resolve_one_addr (*iter))
26564 return false;
26567 return true;
26570 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26571 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26572 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26574 static rtx
26575 string_cst_pool_decl (tree t)
26577 rtx rtl = output_constant_def (t, 1);
26578 unsigned char *array;
26579 dw_loc_descr_ref l;
26580 tree decl;
26581 size_t len;
26582 dw_die_ref ref;
26584 if (!rtl || !MEM_P (rtl))
26585 return NULL_RTX;
26586 rtl = XEXP (rtl, 0);
26587 if (GET_CODE (rtl) != SYMBOL_REF
26588 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26589 return NULL_RTX;
26591 decl = SYMBOL_REF_DECL (rtl);
26592 if (!lookup_decl_die (decl))
26594 len = TREE_STRING_LENGTH (t);
26595 vec_safe_push (used_rtx_array, rtl);
26596 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26597 array = ggc_vec_alloc<unsigned char> (len);
26598 memcpy (array, TREE_STRING_POINTER (t), len);
26599 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26600 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26601 l->dw_loc_oprnd2.v.val_vec.length = len;
26602 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26603 l->dw_loc_oprnd2.v.val_vec.array = array;
26604 add_AT_loc (ref, DW_AT_location, l);
26605 equate_decl_number_to_die (decl, ref);
26607 return rtl;
26610 /* Helper function of resolve_addr_in_expr. LOC is
26611 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26612 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26613 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26614 with DW_OP_GNU_implicit_pointer if possible
26615 and return true, if unsuccessful, return false. */
26617 static bool
26618 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26620 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26621 HOST_WIDE_INT offset = 0;
26622 dw_die_ref ref = NULL;
26623 tree decl;
26625 if (GET_CODE (rtl) == CONST
26626 && GET_CODE (XEXP (rtl, 0)) == PLUS
26627 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26629 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26630 rtl = XEXP (XEXP (rtl, 0), 0);
26632 if (GET_CODE (rtl) == CONST_STRING)
26634 size_t len = strlen (XSTR (rtl, 0)) + 1;
26635 tree t = build_string (len, XSTR (rtl, 0));
26636 tree tlen = size_int (len - 1);
26638 TREE_TYPE (t)
26639 = build_array_type (char_type_node, build_index_type (tlen));
26640 rtl = string_cst_pool_decl (t);
26641 if (!rtl)
26642 return false;
26644 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26646 decl = SYMBOL_REF_DECL (rtl);
26647 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
26649 ref = lookup_decl_die (decl);
26650 if (ref && (get_AT (ref, DW_AT_location)
26651 || get_AT (ref, DW_AT_const_value)))
26653 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26654 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26655 loc->dw_loc_oprnd1.val_entry = NULL;
26656 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26657 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26658 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26659 loc->dw_loc_oprnd2.v.val_int = offset;
26660 return true;
26664 return false;
26667 /* Helper function for resolve_addr, handle one location
26668 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26669 the location list couldn't be resolved. */
26671 static bool
26672 resolve_addr_in_expr (dw_loc_descr_ref loc)
26674 dw_loc_descr_ref keep = NULL;
26675 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26676 switch (loc->dw_loc_opc)
26678 case DW_OP_addr:
26679 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26681 if ((prev == NULL
26682 || prev->dw_loc_opc == DW_OP_piece
26683 || prev->dw_loc_opc == DW_OP_bit_piece)
26684 && loc->dw_loc_next
26685 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26686 && !dwarf_strict
26687 && optimize_one_addr_into_implicit_ptr (loc))
26688 break;
26689 return false;
26691 break;
26692 case DW_OP_GNU_addr_index:
26693 case DW_OP_GNU_const_index:
26694 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26695 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26697 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26698 if (!resolve_one_addr (&rtl))
26699 return false;
26700 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26701 loc->dw_loc_oprnd1.val_entry =
26702 add_addr_table_entry (rtl, ate_kind_rtx);
26704 break;
26705 case DW_OP_const4u:
26706 case DW_OP_const8u:
26707 if (loc->dtprel
26708 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26709 return false;
26710 break;
26711 case DW_OP_plus_uconst:
26712 if (size_of_loc_descr (loc)
26713 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26715 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26717 dw_loc_descr_ref repl
26718 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26719 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26720 add_loc_descr (&repl, loc->dw_loc_next);
26721 *loc = *repl;
26723 break;
26724 case DW_OP_implicit_value:
26725 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26726 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26727 return false;
26728 break;
26729 case DW_OP_GNU_implicit_pointer:
26730 case DW_OP_GNU_parameter_ref:
26731 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26733 dw_die_ref ref
26734 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26735 if (ref == NULL)
26736 return false;
26737 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26738 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26739 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26741 break;
26742 case DW_OP_GNU_const_type:
26743 case DW_OP_GNU_regval_type:
26744 case DW_OP_GNU_deref_type:
26745 case DW_OP_GNU_convert:
26746 case DW_OP_GNU_reinterpret:
26747 while (loc->dw_loc_next
26748 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26750 dw_die_ref base1, base2;
26751 unsigned enc1, enc2, size1, size2;
26752 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26753 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26754 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26755 else if (loc->dw_loc_oprnd1.val_class
26756 == dw_val_class_unsigned_const)
26757 break;
26758 else
26759 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26760 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26761 == dw_val_class_unsigned_const)
26762 break;
26763 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26764 gcc_assert (base1->die_tag == DW_TAG_base_type
26765 && base2->die_tag == DW_TAG_base_type);
26766 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26767 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26768 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26769 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26770 if (size1 == size2
26771 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26772 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26773 && loc != keep)
26774 || enc1 == enc2))
26776 /* Optimize away next DW_OP_GNU_convert after
26777 adjusting LOC's base type die reference. */
26778 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26779 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26780 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26781 else
26782 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26783 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26784 continue;
26786 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26787 point typed stack entry. */
26788 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26789 keep = loc->dw_loc_next;
26790 break;
26792 break;
26793 default:
26794 break;
26796 return true;
26799 /* Helper function of resolve_addr. DIE had DW_AT_location of
26800 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26801 and DW_OP_addr couldn't be resolved. resolve_addr has already
26802 removed the DW_AT_location attribute. This function attempts to
26803 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26804 to it or DW_AT_const_value attribute, if possible. */
26806 static void
26807 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26809 if (!VAR_P (decl)
26810 || lookup_decl_die (decl) != die
26811 || DECL_EXTERNAL (decl)
26812 || !TREE_STATIC (decl)
26813 || DECL_INITIAL (decl) == NULL_TREE
26814 || DECL_P (DECL_INITIAL (decl))
26815 || get_AT (die, DW_AT_const_value))
26816 return;
26818 tree init = DECL_INITIAL (decl);
26819 HOST_WIDE_INT offset = 0;
26820 /* For variables that have been optimized away and thus
26821 don't have a memory location, see if we can emit
26822 DW_AT_const_value instead. */
26823 if (tree_add_const_value_attribute (die, init))
26824 return;
26825 if (dwarf_strict)
26826 return;
26827 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26828 and ADDR_EXPR refers to a decl that has DW_AT_location or
26829 DW_AT_const_value (but isn't addressable, otherwise
26830 resolving the original DW_OP_addr wouldn't fail), see if
26831 we can add DW_OP_GNU_implicit_pointer. */
26832 STRIP_NOPS (init);
26833 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26834 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26836 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26837 init = TREE_OPERAND (init, 0);
26838 STRIP_NOPS (init);
26840 if (TREE_CODE (init) != ADDR_EXPR)
26841 return;
26842 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26843 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26844 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26845 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26846 && TREE_OPERAND (init, 0) != decl))
26848 dw_die_ref ref;
26849 dw_loc_descr_ref l;
26851 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26853 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26854 if (!rtl)
26855 return;
26856 decl = SYMBOL_REF_DECL (rtl);
26858 else
26859 decl = TREE_OPERAND (init, 0);
26860 ref = lookup_decl_die (decl);
26861 if (ref == NULL
26862 || (!get_AT (ref, DW_AT_location)
26863 && !get_AT (ref, DW_AT_const_value)))
26864 return;
26865 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26866 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26867 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26868 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26869 add_AT_loc (die, DW_AT_location, l);
26873 /* Return NULL if l is a DWARF expression, or first op that is not
26874 valid DWARF expression. */
26876 static dw_loc_descr_ref
26877 non_dwarf_expression (dw_loc_descr_ref l)
26879 while (l)
26881 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26882 return l;
26883 switch (l->dw_loc_opc)
26885 case DW_OP_regx:
26886 case DW_OP_implicit_value:
26887 case DW_OP_stack_value:
26888 case DW_OP_GNU_implicit_pointer:
26889 case DW_OP_GNU_parameter_ref:
26890 case DW_OP_piece:
26891 case DW_OP_bit_piece:
26892 return l;
26893 default:
26894 break;
26896 l = l->dw_loc_next;
26898 return NULL;
26901 /* Return adjusted copy of EXPR:
26902 If it is empty DWARF expression, return it.
26903 If it is valid non-empty DWARF expression,
26904 return copy of EXPR with copy of DEREF appended to it.
26905 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26906 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26907 and no DEREF.
26908 If it is DWARF expression followed by DW_OP_stack_value, return
26909 copy of the DWARF expression without anything appended.
26910 Otherwise, return NULL. */
26912 static dw_loc_descr_ref
26913 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26916 if (expr == NULL)
26917 return NULL;
26919 dw_loc_descr_ref l = non_dwarf_expression (expr);
26920 if (l && l->dw_loc_next)
26921 return NULL;
26923 if (l)
26925 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26926 deref = new_loc_descr ((enum dwarf_location_atom)
26927 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
26928 0, 0);
26929 else
26930 switch (l->dw_loc_opc)
26932 case DW_OP_regx:
26933 deref = new_loc_descr (DW_OP_bregx,
26934 l->dw_loc_oprnd1.v.val_unsigned, 0);
26935 break;
26936 case DW_OP_stack_value:
26937 deref = NULL;
26938 break;
26939 default:
26940 return NULL;
26943 else
26944 deref = new_loc_descr (deref->dw_loc_opc,
26945 deref->dw_loc_oprnd1.v.val_int, 0);
26947 dw_loc_descr_ref ret = NULL, *p = &ret;
26948 while (expr != l)
26950 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
26951 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
26952 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
26953 p = &(*p)->dw_loc_next;
26954 expr = expr->dw_loc_next;
26956 *p = deref;
26957 return ret;
26960 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
26961 or argument, adjust it if needed and return:
26962 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
26963 attribute if present should be removed
26964 0 keep the attribute as is if the referenced var or argument has
26965 only DWARF expression that covers all ranges
26966 1 if the attribute has been successfully adjusted. */
26968 static int
26969 optimize_string_length (dw_attr_node *a)
26971 dw_loc_descr_ref l = AT_loc (a), lv;
26972 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
26973 dw_attr_node *av = get_AT (die, DW_AT_location);
26974 dw_loc_list_ref d;
26975 bool non_dwarf_expr = false;
26977 if (av == NULL)
26978 return -1;
26979 switch (AT_class (av))
26981 case dw_val_class_loc_list:
26982 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
26983 if (d->expr && non_dwarf_expression (d->expr))
26984 non_dwarf_expr = true;
26985 break;
26986 case dw_val_class_loc:
26987 lv = AT_loc (av);
26988 if (lv == NULL)
26989 return -1;
26990 if (non_dwarf_expression (lv))
26991 non_dwarf_expr = true;
26992 break;
26993 default:
26994 return -1;
26997 /* If it is safe to keep DW_OP_call4 in, keep it. */
26998 if (!non_dwarf_expr
26999 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
27000 return 0;
27002 /* If not dereferencing the DW_OP_call4 afterwards, we can just
27003 copy over the DW_AT_location attribute from die to a. */
27004 if (l->dw_loc_next == NULL)
27006 a->dw_attr_val = av->dw_attr_val;
27007 return 1;
27010 dw_loc_list_ref list, *p;
27011 switch (AT_class (av))
27013 case dw_val_class_loc_list:
27014 p = &list;
27015 list = NULL;
27016 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27018 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
27019 if (lv)
27021 *p = new_loc_list (lv, d->begin, d->end, d->section);
27022 p = &(*p)->dw_loc_next;
27025 if (list == NULL)
27026 return -1;
27027 a->dw_attr_val.val_class = dw_val_class_loc_list;
27028 gen_llsym (list);
27029 *AT_loc_list_ptr (a) = list;
27030 return 1;
27031 case dw_val_class_loc:
27032 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
27033 if (lv == NULL)
27034 return -1;
27035 a->dw_attr_val.v.val_loc = lv;
27036 return 1;
27037 default:
27038 gcc_unreachable ();
27042 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27043 an address in .rodata section if the string literal is emitted there,
27044 or remove the containing location list or replace DW_AT_const_value
27045 with DW_AT_location and empty location expression, if it isn't found
27046 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27047 to something that has been emitted in the current CU. */
27049 static void
27050 resolve_addr (dw_die_ref die)
27052 dw_die_ref c;
27053 dw_attr_node *a;
27054 dw_loc_list_ref *curr, *start, loc;
27055 unsigned ix;
27056 bool remove_AT_byte_size = false;
27058 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27059 switch (AT_class (a))
27061 case dw_val_class_loc_list:
27062 start = curr = AT_loc_list_ptr (a);
27063 loc = *curr;
27064 gcc_assert (loc);
27065 /* The same list can be referenced more than once. See if we have
27066 already recorded the result from a previous pass. */
27067 if (loc->replaced)
27068 *curr = loc->dw_loc_next;
27069 else if (!loc->resolved_addr)
27071 /* As things stand, we do not expect or allow one die to
27072 reference a suffix of another die's location list chain.
27073 References must be identical or completely separate.
27074 There is therefore no need to cache the result of this
27075 pass on any list other than the first; doing so
27076 would lead to unnecessary writes. */
27077 while (*curr)
27079 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27080 if (!resolve_addr_in_expr ((*curr)->expr))
27082 dw_loc_list_ref next = (*curr)->dw_loc_next;
27083 dw_loc_descr_ref l = (*curr)->expr;
27085 if (next && (*curr)->ll_symbol)
27087 gcc_assert (!next->ll_symbol);
27088 next->ll_symbol = (*curr)->ll_symbol;
27090 if (dwarf_split_debug_info)
27091 remove_loc_list_addr_table_entries (l);
27092 *curr = next;
27094 else
27096 mark_base_types ((*curr)->expr);
27097 curr = &(*curr)->dw_loc_next;
27100 if (loc == *start)
27101 loc->resolved_addr = 1;
27102 else
27104 loc->replaced = 1;
27105 loc->dw_loc_next = *start;
27108 if (!*start)
27110 remove_AT (die, a->dw_attr);
27111 ix--;
27113 break;
27114 case dw_val_class_loc:
27116 dw_loc_descr_ref l = AT_loc (a);
27117 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27118 DW_AT_string_length is only a rough approximation; unfortunately
27119 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27120 needs a DWARF expression, while DW_AT_location of the referenced
27121 variable or argument might be any location description. */
27122 if (a->dw_attr == DW_AT_string_length
27123 && l
27124 && l->dw_loc_opc == DW_OP_call4
27125 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27126 && (l->dw_loc_next == NULL
27127 || (l->dw_loc_next->dw_loc_next == NULL
27128 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27129 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27131 switch (optimize_string_length (a))
27133 case -1:
27134 remove_AT (die, a->dw_attr);
27135 ix--;
27136 /* If we drop DW_AT_string_length, we need to drop also
27137 DW_AT_{string_length_,}byte_size. */
27138 remove_AT_byte_size = true;
27139 continue;
27140 default:
27141 break;
27142 case 1:
27143 /* Even if we keep the optimized DW_AT_string_length,
27144 it might have changed AT_class, so process it again. */
27145 ix--;
27146 continue;
27149 /* For -gdwarf-2 don't attempt to optimize
27150 DW_AT_data_member_location containing
27151 DW_OP_plus_uconst - older consumers might
27152 rely on it being that op instead of a more complex,
27153 but shorter, location description. */
27154 if ((dwarf_version > 2
27155 || a->dw_attr != DW_AT_data_member_location
27156 || l == NULL
27157 || l->dw_loc_opc != DW_OP_plus_uconst
27158 || l->dw_loc_next != NULL)
27159 && !resolve_addr_in_expr (l))
27161 if (dwarf_split_debug_info)
27162 remove_loc_list_addr_table_entries (l);
27163 if (l != NULL
27164 && l->dw_loc_next == NULL
27165 && l->dw_loc_opc == DW_OP_addr
27166 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27167 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27168 && a->dw_attr == DW_AT_location)
27170 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27171 remove_AT (die, a->dw_attr);
27172 ix--;
27173 optimize_location_into_implicit_ptr (die, decl);
27174 break;
27176 remove_AT (die, a->dw_attr);
27177 ix--;
27179 else
27180 mark_base_types (l);
27182 break;
27183 case dw_val_class_addr:
27184 if (a->dw_attr == DW_AT_const_value
27185 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27187 if (AT_index (a) != NOT_INDEXED)
27188 remove_addr_table_entry (a->dw_attr_val.val_entry);
27189 remove_AT (die, a->dw_attr);
27190 ix--;
27192 if (die->die_tag == DW_TAG_GNU_call_site
27193 && a->dw_attr == DW_AT_abstract_origin)
27195 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27196 dw_die_ref tdie = lookup_decl_die (tdecl);
27197 dw_die_ref cdie;
27198 if (tdie == NULL
27199 && DECL_EXTERNAL (tdecl)
27200 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27201 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27203 /* Creating a full DIE for tdecl is overly expensive and
27204 at this point even wrong when in the LTO phase
27205 as it can end up generating new type DIEs we didn't
27206 output and thus optimize_external_refs will crash. */
27207 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27208 add_AT_flag (tdie, DW_AT_external, 1);
27209 add_AT_flag (tdie, DW_AT_declaration, 1);
27210 add_linkage_attr (tdie, tdecl);
27211 add_name_and_src_coords_attributes (tdie, tdecl);
27212 equate_decl_number_to_die (tdecl, tdie);
27214 if (tdie)
27216 a->dw_attr_val.val_class = dw_val_class_die_ref;
27217 a->dw_attr_val.v.val_die_ref.die = tdie;
27218 a->dw_attr_val.v.val_die_ref.external = 0;
27220 else
27222 if (AT_index (a) != NOT_INDEXED)
27223 remove_addr_table_entry (a->dw_attr_val.val_entry);
27224 remove_AT (die, a->dw_attr);
27225 ix--;
27228 break;
27229 default:
27230 break;
27233 if (remove_AT_byte_size)
27234 remove_AT (die, dwarf_version >= 5
27235 ? DW_AT_string_length_byte_size
27236 : DW_AT_byte_size);
27238 FOR_EACH_CHILD (die, c, resolve_addr (c));
27241 /* Helper routines for optimize_location_lists.
27242 This pass tries to share identical local lists in .debug_loc
27243 section. */
27245 /* Iteratively hash operands of LOC opcode into HSTATE. */
27247 static void
27248 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27250 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27251 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27253 switch (loc->dw_loc_opc)
27255 case DW_OP_const4u:
27256 case DW_OP_const8u:
27257 if (loc->dtprel)
27258 goto hash_addr;
27259 /* FALLTHRU */
27260 case DW_OP_const1u:
27261 case DW_OP_const1s:
27262 case DW_OP_const2u:
27263 case DW_OP_const2s:
27264 case DW_OP_const4s:
27265 case DW_OP_const8s:
27266 case DW_OP_constu:
27267 case DW_OP_consts:
27268 case DW_OP_pick:
27269 case DW_OP_plus_uconst:
27270 case DW_OP_breg0:
27271 case DW_OP_breg1:
27272 case DW_OP_breg2:
27273 case DW_OP_breg3:
27274 case DW_OP_breg4:
27275 case DW_OP_breg5:
27276 case DW_OP_breg6:
27277 case DW_OP_breg7:
27278 case DW_OP_breg8:
27279 case DW_OP_breg9:
27280 case DW_OP_breg10:
27281 case DW_OP_breg11:
27282 case DW_OP_breg12:
27283 case DW_OP_breg13:
27284 case DW_OP_breg14:
27285 case DW_OP_breg15:
27286 case DW_OP_breg16:
27287 case DW_OP_breg17:
27288 case DW_OP_breg18:
27289 case DW_OP_breg19:
27290 case DW_OP_breg20:
27291 case DW_OP_breg21:
27292 case DW_OP_breg22:
27293 case DW_OP_breg23:
27294 case DW_OP_breg24:
27295 case DW_OP_breg25:
27296 case DW_OP_breg26:
27297 case DW_OP_breg27:
27298 case DW_OP_breg28:
27299 case DW_OP_breg29:
27300 case DW_OP_breg30:
27301 case DW_OP_breg31:
27302 case DW_OP_regx:
27303 case DW_OP_fbreg:
27304 case DW_OP_piece:
27305 case DW_OP_deref_size:
27306 case DW_OP_xderef_size:
27307 hstate.add_object (val1->v.val_int);
27308 break;
27309 case DW_OP_skip:
27310 case DW_OP_bra:
27312 int offset;
27314 gcc_assert (val1->val_class == dw_val_class_loc);
27315 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27316 hstate.add_object (offset);
27318 break;
27319 case DW_OP_implicit_value:
27320 hstate.add_object (val1->v.val_unsigned);
27321 switch (val2->val_class)
27323 case dw_val_class_const:
27324 hstate.add_object (val2->v.val_int);
27325 break;
27326 case dw_val_class_vec:
27328 unsigned int elt_size = val2->v.val_vec.elt_size;
27329 unsigned int len = val2->v.val_vec.length;
27331 hstate.add_int (elt_size);
27332 hstate.add_int (len);
27333 hstate.add (val2->v.val_vec.array, len * elt_size);
27335 break;
27336 case dw_val_class_const_double:
27337 hstate.add_object (val2->v.val_double.low);
27338 hstate.add_object (val2->v.val_double.high);
27339 break;
27340 case dw_val_class_wide_int:
27341 hstate.add (val2->v.val_wide->get_val (),
27342 get_full_len (*val2->v.val_wide)
27343 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27344 break;
27345 case dw_val_class_addr:
27346 inchash::add_rtx (val2->v.val_addr, hstate);
27347 break;
27348 default:
27349 gcc_unreachable ();
27351 break;
27352 case DW_OP_bregx:
27353 case DW_OP_bit_piece:
27354 hstate.add_object (val1->v.val_int);
27355 hstate.add_object (val2->v.val_int);
27356 break;
27357 case DW_OP_addr:
27358 hash_addr:
27359 if (loc->dtprel)
27361 unsigned char dtprel = 0xd1;
27362 hstate.add_object (dtprel);
27364 inchash::add_rtx (val1->v.val_addr, hstate);
27365 break;
27366 case DW_OP_GNU_addr_index:
27367 case DW_OP_GNU_const_index:
27369 if (loc->dtprel)
27371 unsigned char dtprel = 0xd1;
27372 hstate.add_object (dtprel);
27374 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27376 break;
27377 case DW_OP_GNU_implicit_pointer:
27378 hstate.add_int (val2->v.val_int);
27379 break;
27380 case DW_OP_GNU_entry_value:
27381 hstate.add_object (val1->v.val_loc);
27382 break;
27383 case DW_OP_GNU_regval_type:
27384 case DW_OP_GNU_deref_type:
27386 unsigned int byte_size
27387 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27388 unsigned int encoding
27389 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27390 hstate.add_object (val1->v.val_int);
27391 hstate.add_object (byte_size);
27392 hstate.add_object (encoding);
27394 break;
27395 case DW_OP_GNU_convert:
27396 case DW_OP_GNU_reinterpret:
27397 if (val1->val_class == dw_val_class_unsigned_const)
27399 hstate.add_object (val1->v.val_unsigned);
27400 break;
27402 /* FALLTHRU */
27403 case DW_OP_GNU_const_type:
27405 unsigned int byte_size
27406 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27407 unsigned int encoding
27408 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27409 hstate.add_object (byte_size);
27410 hstate.add_object (encoding);
27411 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27412 break;
27413 hstate.add_object (val2->val_class);
27414 switch (val2->val_class)
27416 case dw_val_class_const:
27417 hstate.add_object (val2->v.val_int);
27418 break;
27419 case dw_val_class_vec:
27421 unsigned int elt_size = val2->v.val_vec.elt_size;
27422 unsigned int len = val2->v.val_vec.length;
27424 hstate.add_object (elt_size);
27425 hstate.add_object (len);
27426 hstate.add (val2->v.val_vec.array, len * elt_size);
27428 break;
27429 case dw_val_class_const_double:
27430 hstate.add_object (val2->v.val_double.low);
27431 hstate.add_object (val2->v.val_double.high);
27432 break;
27433 case dw_val_class_wide_int:
27434 hstate.add (val2->v.val_wide->get_val (),
27435 get_full_len (*val2->v.val_wide)
27436 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27437 break;
27438 default:
27439 gcc_unreachable ();
27442 break;
27444 default:
27445 /* Other codes have no operands. */
27446 break;
27450 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27452 static inline void
27453 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27455 dw_loc_descr_ref l;
27456 bool sizes_computed = false;
27457 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27458 size_of_locs (loc);
27460 for (l = loc; l != NULL; l = l->dw_loc_next)
27462 enum dwarf_location_atom opc = l->dw_loc_opc;
27463 hstate.add_object (opc);
27464 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27466 size_of_locs (loc);
27467 sizes_computed = true;
27469 hash_loc_operands (l, hstate);
27473 /* Compute hash of the whole location list LIST_HEAD. */
27475 static inline void
27476 hash_loc_list (dw_loc_list_ref list_head)
27478 dw_loc_list_ref curr = list_head;
27479 inchash::hash hstate;
27481 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27483 hstate.add (curr->begin, strlen (curr->begin) + 1);
27484 hstate.add (curr->end, strlen (curr->end) + 1);
27485 if (curr->section)
27486 hstate.add (curr->section, strlen (curr->section) + 1);
27487 hash_locs (curr->expr, hstate);
27489 list_head->hash = hstate.end ();
27492 /* Return true if X and Y opcodes have the same operands. */
27494 static inline bool
27495 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27497 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27498 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27499 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27500 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27502 switch (x->dw_loc_opc)
27504 case DW_OP_const4u:
27505 case DW_OP_const8u:
27506 if (x->dtprel)
27507 goto hash_addr;
27508 /* FALLTHRU */
27509 case DW_OP_const1u:
27510 case DW_OP_const1s:
27511 case DW_OP_const2u:
27512 case DW_OP_const2s:
27513 case DW_OP_const4s:
27514 case DW_OP_const8s:
27515 case DW_OP_constu:
27516 case DW_OP_consts:
27517 case DW_OP_pick:
27518 case DW_OP_plus_uconst:
27519 case DW_OP_breg0:
27520 case DW_OP_breg1:
27521 case DW_OP_breg2:
27522 case DW_OP_breg3:
27523 case DW_OP_breg4:
27524 case DW_OP_breg5:
27525 case DW_OP_breg6:
27526 case DW_OP_breg7:
27527 case DW_OP_breg8:
27528 case DW_OP_breg9:
27529 case DW_OP_breg10:
27530 case DW_OP_breg11:
27531 case DW_OP_breg12:
27532 case DW_OP_breg13:
27533 case DW_OP_breg14:
27534 case DW_OP_breg15:
27535 case DW_OP_breg16:
27536 case DW_OP_breg17:
27537 case DW_OP_breg18:
27538 case DW_OP_breg19:
27539 case DW_OP_breg20:
27540 case DW_OP_breg21:
27541 case DW_OP_breg22:
27542 case DW_OP_breg23:
27543 case DW_OP_breg24:
27544 case DW_OP_breg25:
27545 case DW_OP_breg26:
27546 case DW_OP_breg27:
27547 case DW_OP_breg28:
27548 case DW_OP_breg29:
27549 case DW_OP_breg30:
27550 case DW_OP_breg31:
27551 case DW_OP_regx:
27552 case DW_OP_fbreg:
27553 case DW_OP_piece:
27554 case DW_OP_deref_size:
27555 case DW_OP_xderef_size:
27556 return valx1->v.val_int == valy1->v.val_int;
27557 case DW_OP_skip:
27558 case DW_OP_bra:
27559 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27560 can cause irrelevant differences in dw_loc_addr. */
27561 gcc_assert (valx1->val_class == dw_val_class_loc
27562 && valy1->val_class == dw_val_class_loc
27563 && (dwarf_split_debug_info
27564 || x->dw_loc_addr == y->dw_loc_addr));
27565 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27566 case DW_OP_implicit_value:
27567 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27568 || valx2->val_class != valy2->val_class)
27569 return false;
27570 switch (valx2->val_class)
27572 case dw_val_class_const:
27573 return valx2->v.val_int == valy2->v.val_int;
27574 case dw_val_class_vec:
27575 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27576 && valx2->v.val_vec.length == valy2->v.val_vec.length
27577 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27578 valx2->v.val_vec.elt_size
27579 * valx2->v.val_vec.length) == 0;
27580 case dw_val_class_const_double:
27581 return valx2->v.val_double.low == valy2->v.val_double.low
27582 && valx2->v.val_double.high == valy2->v.val_double.high;
27583 case dw_val_class_wide_int:
27584 return *valx2->v.val_wide == *valy2->v.val_wide;
27585 case dw_val_class_addr:
27586 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27587 default:
27588 gcc_unreachable ();
27590 case DW_OP_bregx:
27591 case DW_OP_bit_piece:
27592 return valx1->v.val_int == valy1->v.val_int
27593 && valx2->v.val_int == valy2->v.val_int;
27594 case DW_OP_addr:
27595 hash_addr:
27596 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27597 case DW_OP_GNU_addr_index:
27598 case DW_OP_GNU_const_index:
27600 rtx ax1 = valx1->val_entry->addr.rtl;
27601 rtx ay1 = valy1->val_entry->addr.rtl;
27602 return rtx_equal_p (ax1, ay1);
27604 case DW_OP_GNU_implicit_pointer:
27605 return valx1->val_class == dw_val_class_die_ref
27606 && valx1->val_class == valy1->val_class
27607 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27608 && valx2->v.val_int == valy2->v.val_int;
27609 case DW_OP_GNU_entry_value:
27610 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27611 case DW_OP_GNU_const_type:
27612 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27613 || valx2->val_class != valy2->val_class)
27614 return false;
27615 switch (valx2->val_class)
27617 case dw_val_class_const:
27618 return valx2->v.val_int == valy2->v.val_int;
27619 case dw_val_class_vec:
27620 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27621 && valx2->v.val_vec.length == valy2->v.val_vec.length
27622 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27623 valx2->v.val_vec.elt_size
27624 * valx2->v.val_vec.length) == 0;
27625 case dw_val_class_const_double:
27626 return valx2->v.val_double.low == valy2->v.val_double.low
27627 && valx2->v.val_double.high == valy2->v.val_double.high;
27628 case dw_val_class_wide_int:
27629 return *valx2->v.val_wide == *valy2->v.val_wide;
27630 default:
27631 gcc_unreachable ();
27633 case DW_OP_GNU_regval_type:
27634 case DW_OP_GNU_deref_type:
27635 return valx1->v.val_int == valy1->v.val_int
27636 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27637 case DW_OP_GNU_convert:
27638 case DW_OP_GNU_reinterpret:
27639 if (valx1->val_class != valy1->val_class)
27640 return false;
27641 if (valx1->val_class == dw_val_class_unsigned_const)
27642 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27643 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27644 case DW_OP_GNU_parameter_ref:
27645 return valx1->val_class == dw_val_class_die_ref
27646 && valx1->val_class == valy1->val_class
27647 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27648 default:
27649 /* Other codes have no operands. */
27650 return true;
27654 /* Return true if DWARF location expressions X and Y are the same. */
27656 static inline bool
27657 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27659 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27660 if (x->dw_loc_opc != y->dw_loc_opc
27661 || x->dtprel != y->dtprel
27662 || !compare_loc_operands (x, y))
27663 break;
27664 return x == NULL && y == NULL;
27667 /* Hashtable helpers. */
27669 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27671 static inline hashval_t hash (const dw_loc_list_struct *);
27672 static inline bool equal (const dw_loc_list_struct *,
27673 const dw_loc_list_struct *);
27676 /* Return precomputed hash of location list X. */
27678 inline hashval_t
27679 loc_list_hasher::hash (const dw_loc_list_struct *x)
27681 return x->hash;
27684 /* Return true if location lists A and B are the same. */
27686 inline bool
27687 loc_list_hasher::equal (const dw_loc_list_struct *a,
27688 const dw_loc_list_struct *b)
27690 if (a == b)
27691 return 1;
27692 if (a->hash != b->hash)
27693 return 0;
27694 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27695 if (strcmp (a->begin, b->begin) != 0
27696 || strcmp (a->end, b->end) != 0
27697 || (a->section == NULL) != (b->section == NULL)
27698 || (a->section && strcmp (a->section, b->section) != 0)
27699 || !compare_locs (a->expr, b->expr))
27700 break;
27701 return a == NULL && b == NULL;
27704 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27707 /* Recursively optimize location lists referenced from DIE
27708 children and share them whenever possible. */
27710 static void
27711 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27713 dw_die_ref c;
27714 dw_attr_node *a;
27715 unsigned ix;
27716 dw_loc_list_struct **slot;
27718 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27719 if (AT_class (a) == dw_val_class_loc_list)
27721 dw_loc_list_ref list = AT_loc_list (a);
27722 /* TODO: perform some optimizations here, before hashing
27723 it and storing into the hash table. */
27724 hash_loc_list (list);
27725 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27726 if (*slot == NULL)
27727 *slot = list;
27728 else
27729 a->dw_attr_val.v.val_loc_list = *slot;
27732 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27736 /* Recursively assign each location list a unique index into the debug_addr
27737 section. */
27739 static void
27740 index_location_lists (dw_die_ref die)
27742 dw_die_ref c;
27743 dw_attr_node *a;
27744 unsigned ix;
27746 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27747 if (AT_class (a) == dw_val_class_loc_list)
27749 dw_loc_list_ref list = AT_loc_list (a);
27750 dw_loc_list_ref curr;
27751 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27753 /* Don't index an entry that has already been indexed
27754 or won't be output. */
27755 if (curr->begin_entry != NULL
27756 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27757 continue;
27759 curr->begin_entry
27760 = add_addr_table_entry (xstrdup (curr->begin),
27761 ate_kind_label);
27765 FOR_EACH_CHILD (die, c, index_location_lists (c));
27768 /* Optimize location lists referenced from DIE
27769 children and share them whenever possible. */
27771 static void
27772 optimize_location_lists (dw_die_ref die)
27774 loc_list_hash_type htab (500);
27775 optimize_location_lists_1 (die, &htab);
27778 /* Traverse the limbo die list, and add parent/child links. The only
27779 dies without parents that should be here are concrete instances of
27780 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27781 For concrete instances, we can get the parent die from the abstract
27782 instance. */
27784 static void
27785 flush_limbo_die_list (void)
27787 limbo_die_node *node;
27789 /* get_context_die calls force_decl_die, which can put new DIEs on the
27790 limbo list in LTO mode when nested functions are put in a different
27791 partition than that of their parent function. */
27792 while ((node = limbo_die_list))
27794 dw_die_ref die = node->die;
27795 limbo_die_list = node->next;
27797 if (die->die_parent == NULL)
27799 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27801 if (origin && origin->die_parent)
27802 add_child_die (origin->die_parent, die);
27803 else if (is_cu_die (die))
27805 else if (seen_error ())
27806 /* It's OK to be confused by errors in the input. */
27807 add_child_die (comp_unit_die (), die);
27808 else
27810 /* In certain situations, the lexical block containing a
27811 nested function can be optimized away, which results
27812 in the nested function die being orphaned. Likewise
27813 with the return type of that nested function. Force
27814 this to be a child of the containing function.
27816 It may happen that even the containing function got fully
27817 inlined and optimized out. In that case we are lost and
27818 assign the empty child. This should not be big issue as
27819 the function is likely unreachable too. */
27820 gcc_assert (node->created_for);
27822 if (DECL_P (node->created_for))
27823 origin = get_context_die (DECL_CONTEXT (node->created_for));
27824 else if (TYPE_P (node->created_for))
27825 origin = scope_die_for (node->created_for, comp_unit_die ());
27826 else
27827 origin = comp_unit_die ();
27829 add_child_die (origin, die);
27835 /* Output stuff that dwarf requires at the end of every file,
27836 and generate the DWARF-2 debugging info. */
27838 static void
27839 dwarf2out_finish (const char *)
27841 comdat_type_node *ctnode;
27842 dw_die_ref main_comp_unit_die;
27844 /* Flush out any latecomers to the limbo party. */
27845 flush_limbo_die_list ();
27847 /* We shouldn't have any symbols with delayed asm names for
27848 DIEs generated after early finish. */
27849 gcc_assert (deferred_asm_name == NULL);
27851 gen_remaining_tmpl_value_param_die_attribute ();
27853 #if ENABLE_ASSERT_CHECKING
27855 dw_die_ref die = comp_unit_die (), c;
27856 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27858 #endif
27859 resolve_addr (comp_unit_die ());
27860 move_marked_base_types ();
27862 /* Initialize sections and labels used for actual assembler output. */
27863 init_sections_and_labels ();
27865 /* Traverse the DIE's and add sibling attributes to those DIE's that
27866 have children. */
27867 add_sibling_attributes (comp_unit_die ());
27868 limbo_die_node *node;
27869 for (node = cu_die_list; node; node = node->next)
27870 add_sibling_attributes (node->die);
27871 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27872 add_sibling_attributes (ctnode->root_die);
27874 /* When splitting DWARF info, we put some attributes in the
27875 skeleton compile_unit DIE that remains in the .o, while
27876 most attributes go in the DWO compile_unit_die. */
27877 if (dwarf_split_debug_info)
27879 limbo_die_node *cu;
27880 main_comp_unit_die = gen_compile_unit_die (NULL);
27881 cu = limbo_die_list;
27882 gcc_assert (cu->die == main_comp_unit_die);
27883 limbo_die_list = limbo_die_list->next;
27884 cu->next = cu_die_list;
27885 cu_die_list = cu;
27887 else
27888 main_comp_unit_die = comp_unit_die ();
27890 /* Output a terminator label for the .text section. */
27891 switch_to_section (text_section);
27892 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27893 if (cold_text_section)
27895 switch_to_section (cold_text_section);
27896 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27899 /* We can only use the low/high_pc attributes if all of the code was
27900 in .text. */
27901 if (!have_multiple_function_sections
27902 || (dwarf_version < 3 && dwarf_strict))
27904 /* Don't add if the CU has no associated code. */
27905 if (text_section_used)
27906 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27907 text_end_label, true);
27909 else
27911 unsigned fde_idx;
27912 dw_fde_ref fde;
27913 bool range_list_added = false;
27915 if (text_section_used)
27916 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27917 text_end_label, &range_list_added, true);
27918 if (cold_text_section_used)
27919 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27920 cold_end_label, &range_list_added, true);
27922 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27924 if (DECL_IGNORED_P (fde->decl))
27925 continue;
27926 if (!fde->in_std_section)
27927 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27928 fde->dw_fde_end, &range_list_added,
27929 true);
27930 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27931 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27932 fde->dw_fde_second_end, &range_list_added,
27933 true);
27936 if (range_list_added)
27938 /* We need to give .debug_loc and .debug_ranges an appropriate
27939 "base address". Use zero so that these addresses become
27940 absolute. Historically, we've emitted the unexpected
27941 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27942 Emit both to give time for other tools to adapt. */
27943 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27944 if (! dwarf_strict && dwarf_version < 4)
27945 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27947 add_ranges (NULL);
27951 if (debug_info_level >= DINFO_LEVEL_TERSE)
27952 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27953 debug_line_section_label);
27955 if (have_macinfo)
27956 add_AT_macptr (comp_unit_die (),
27957 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27958 macinfo_section_label);
27960 if (dwarf_split_debug_info)
27962 /* optimize_location_lists calculates the size of the lists,
27963 so index them first, and assign indices to the entries.
27964 Although optimize_location_lists will remove entries from
27965 the table, it only does so for duplicates, and therefore
27966 only reduces ref_counts to 1. */
27967 index_location_lists (comp_unit_die ());
27969 if (addr_index_table != NULL)
27971 unsigned int index = 0;
27972 addr_index_table
27973 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27974 (&index);
27978 if (have_location_lists)
27979 optimize_location_lists (comp_unit_die ());
27981 save_macinfo_strings ();
27983 if (dwarf_split_debug_info)
27985 unsigned int index = 0;
27987 /* Add attributes common to skeleton compile_units and
27988 type_units. Because these attributes include strings, it
27989 must be done before freezing the string table. Top-level
27990 skeleton die attrs are added when the skeleton type unit is
27991 created, so ensure it is created by this point. */
27992 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27993 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27996 /* Output all of the compilation units. We put the main one last so that
27997 the offsets are available to output_pubnames. */
27998 for (node = cu_die_list; node; node = node->next)
27999 output_comp_unit (node->die, 0);
28001 hash_table<comdat_type_hasher> comdat_type_table (100);
28002 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28004 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28006 /* Don't output duplicate types. */
28007 if (*slot != HTAB_EMPTY_ENTRY)
28008 continue;
28010 /* Add a pointer to the line table for the main compilation unit
28011 so that the debugger can make sense of DW_AT_decl_file
28012 attributes. */
28013 if (debug_info_level >= DINFO_LEVEL_TERSE)
28014 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28015 (!dwarf_split_debug_info
28016 ? debug_line_section_label
28017 : debug_skeleton_line_section_label));
28019 output_comdat_type_unit (ctnode);
28020 *slot = ctnode;
28023 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28024 both the main_cu and all skeleton TUs. Making this call unconditional
28025 would end up either adding a second copy of the AT_pubnames attribute, or
28026 requiring a special case in add_top_level_skeleton_die_attrs. */
28027 if (!dwarf_split_debug_info)
28028 add_AT_pubnames (comp_unit_die ());
28030 if (dwarf_split_debug_info)
28032 int mark;
28033 unsigned char checksum[16];
28034 struct md5_ctx ctx;
28036 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28037 md5_init_ctx (&ctx);
28038 mark = 0;
28039 die_checksum (comp_unit_die (), &ctx, &mark);
28040 unmark_all_dies (comp_unit_die ());
28041 md5_finish_ctx (&ctx, checksum);
28043 /* Use the first 8 bytes of the checksum as the dwo_id,
28044 and add it to both comp-unit DIEs. */
28045 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28046 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28048 /* Add the base offset of the ranges table to the skeleton
28049 comp-unit DIE. */
28050 if (ranges_table_in_use)
28051 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28052 ranges_section_label);
28054 switch_to_section (debug_addr_section);
28055 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28056 output_addr_table ();
28059 /* Output the main compilation unit if non-empty or if .debug_macinfo
28060 or .debug_macro will be emitted. */
28061 output_comp_unit (comp_unit_die (), have_macinfo);
28063 if (dwarf_split_debug_info && info_section_emitted)
28064 output_skeleton_debug_sections (main_comp_unit_die);
28066 /* Output the abbreviation table. */
28067 if (abbrev_die_table_in_use != 1)
28069 switch_to_section (debug_abbrev_section);
28070 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28071 output_abbrev_section ();
28074 /* Output location list section if necessary. */
28075 if (have_location_lists)
28077 /* Output the location lists info. */
28078 switch_to_section (debug_loc_section);
28079 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28080 output_location_lists (comp_unit_die ());
28083 output_pubtables ();
28085 /* Output the address range information if a CU (.debug_info section)
28086 was emitted. We output an empty table even if we had no functions
28087 to put in it. This because the consumer has no way to tell the
28088 difference between an empty table that we omitted and failure to
28089 generate a table that would have contained data. */
28090 if (info_section_emitted)
28092 switch_to_section (debug_aranges_section);
28093 output_aranges ();
28096 /* Output ranges section if necessary. */
28097 if (ranges_table_in_use)
28099 switch_to_section (debug_ranges_section);
28100 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28101 output_ranges ();
28104 /* Have to end the macro section. */
28105 if (have_macinfo)
28107 switch_to_section (debug_macinfo_section);
28108 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28109 output_macinfo ();
28110 dw2_asm_output_data (1, 0, "End compilation unit");
28113 /* Output the source line correspondence table. We must do this
28114 even if there is no line information. Otherwise, on an empty
28115 translation unit, we will generate a present, but empty,
28116 .debug_info section. IRIX 6.5 `nm' will then complain when
28117 examining the file. This is done late so that any filenames
28118 used by the debug_info section are marked as 'used'. */
28119 switch_to_section (debug_line_section);
28120 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28121 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28122 output_line_info (false);
28124 if (dwarf_split_debug_info && info_section_emitted)
28126 switch_to_section (debug_skeleton_line_section);
28127 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28128 output_line_info (true);
28131 /* If we emitted any indirect strings, output the string table too. */
28132 if (debug_str_hash || skeleton_debug_str_hash)
28133 output_indirect_strings ();
28136 /* Perform any cleanups needed after the early debug generation pass
28137 has run. */
28139 static void
28140 dwarf2out_early_finish (const char *filename)
28142 set_early_dwarf s;
28144 /* PCH might result in DW_AT_producer string being restored from the
28145 header compilation, so always fill it with empty string initially
28146 and overwrite only here. */
28147 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28148 producer_string = gen_producer_string ();
28149 producer->dw_attr_val.v.val_str->refcount--;
28150 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28152 /* Add the name for the main input file now. We delayed this from
28153 dwarf2out_init to avoid complications with PCH. */
28154 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28155 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
28156 add_comp_dir_attribute (comp_unit_die ());
28157 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
28159 bool p = false;
28160 file_table->traverse<bool *, file_table_relative_p> (&p);
28161 if (p)
28162 add_comp_dir_attribute (comp_unit_die ());
28165 /* With LTO early dwarf was really finished at compile-time, so make
28166 sure to adjust the phase after annotating the LTRANS CU DIE. */
28167 if (in_lto_p)
28169 early_dwarf_finished = true;
28170 return;
28173 /* Walk through the list of incomplete types again, trying once more to
28174 emit full debugging info for them. */
28175 retry_incomplete_types ();
28177 /* The point here is to flush out the limbo list so that it is empty
28178 and we don't need to stream it for LTO. */
28179 flush_limbo_die_list ();
28181 gen_scheduled_generic_parms_dies ();
28182 gen_remaining_tmpl_value_param_die_attribute ();
28184 /* Add DW_AT_linkage_name for all deferred DIEs. */
28185 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28187 tree decl = node->created_for;
28188 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28189 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28190 ended up in deferred_asm_name before we knew it was
28191 constant and never written to disk. */
28192 && DECL_ASSEMBLER_NAME (decl))
28194 add_linkage_attr (node->die, decl);
28195 move_linkage_attr (node->die);
28198 deferred_asm_name = NULL;
28200 if (flag_eliminate_unused_debug_types)
28201 prune_unused_types ();
28203 /* Generate separate COMDAT sections for type DIEs. */
28204 if (use_debug_types)
28206 break_out_comdat_types (comp_unit_die ());
28208 /* Each new type_unit DIE was added to the limbo die list when created.
28209 Since these have all been added to comdat_type_list, clear the
28210 limbo die list. */
28211 limbo_die_list = NULL;
28213 /* For each new comdat type unit, copy declarations for incomplete
28214 types to make the new unit self-contained (i.e., no direct
28215 references to the main compile unit). */
28216 for (comdat_type_node *ctnode = comdat_type_list;
28217 ctnode != NULL; ctnode = ctnode->next)
28218 copy_decls_for_unworthy_types (ctnode->root_die);
28219 copy_decls_for_unworthy_types (comp_unit_die ());
28221 /* In the process of copying declarations from one unit to another,
28222 we may have left some declarations behind that are no longer
28223 referenced. Prune them. */
28224 prune_unused_types ();
28227 /* Generate separate CUs for each of the include files we've seen.
28228 They will go into limbo_die_list and from there to cu_die_list. */
28229 if (flag_eliminate_dwarf2_dups)
28231 gcc_assert (limbo_die_list == NULL);
28232 break_out_includes (comp_unit_die ());
28233 limbo_die_node *cu;
28234 while ((cu = limbo_die_list))
28236 limbo_die_list = cu->next;
28237 cu->next = cu_die_list;
28238 cu_die_list = cu;
28242 /* The early debug phase is now finished. */
28243 early_dwarf_finished = true;
28246 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28247 within the same process. For use by toplev::finalize. */
28249 void
28250 dwarf2out_c_finalize (void)
28252 last_var_location_insn = NULL;
28253 cached_next_real_insn = NULL;
28254 used_rtx_array = NULL;
28255 incomplete_types = NULL;
28256 decl_scope_table = NULL;
28257 debug_info_section = NULL;
28258 debug_skeleton_info_section = NULL;
28259 debug_abbrev_section = NULL;
28260 debug_skeleton_abbrev_section = NULL;
28261 debug_aranges_section = NULL;
28262 debug_addr_section = NULL;
28263 debug_macinfo_section = NULL;
28264 debug_line_section = NULL;
28265 debug_skeleton_line_section = NULL;
28266 debug_loc_section = NULL;
28267 debug_pubnames_section = NULL;
28268 debug_pubtypes_section = NULL;
28269 debug_str_section = NULL;
28270 debug_str_dwo_section = NULL;
28271 debug_str_offsets_section = NULL;
28272 debug_ranges_section = NULL;
28273 debug_frame_section = NULL;
28274 fde_vec = NULL;
28275 debug_str_hash = NULL;
28276 skeleton_debug_str_hash = NULL;
28277 dw2_string_counter = 0;
28278 have_multiple_function_sections = false;
28279 text_section_used = false;
28280 cold_text_section_used = false;
28281 cold_text_section = NULL;
28282 current_unit_personality = NULL;
28284 next_die_offset = 0;
28285 single_comp_unit_die = NULL;
28286 comdat_type_list = NULL;
28287 limbo_die_list = NULL;
28288 file_table = NULL;
28289 decl_die_table = NULL;
28290 common_block_die_table = NULL;
28291 decl_loc_table = NULL;
28292 call_arg_locations = NULL;
28293 call_arg_loc_last = NULL;
28294 call_site_count = -1;
28295 tail_call_site_count = -1;
28296 cached_dw_loc_list_table = NULL;
28297 abbrev_die_table = NULL;
28298 abbrev_die_table_allocated = 0;
28299 abbrev_die_table_in_use = 0;
28300 delete dwarf_proc_stack_usage_map;
28301 dwarf_proc_stack_usage_map = NULL;
28302 line_info_label_num = 0;
28303 cur_line_info_table = NULL;
28304 text_section_line_info = NULL;
28305 cold_text_section_line_info = NULL;
28306 separate_line_info = NULL;
28307 info_section_emitted = false;
28308 pubname_table = NULL;
28309 pubtype_table = NULL;
28310 macinfo_table = NULL;
28311 ranges_table = NULL;
28312 ranges_table_allocated = 0;
28313 ranges_table_in_use = 0;
28314 ranges_by_label = 0;
28315 ranges_by_label_allocated = 0;
28316 ranges_by_label_in_use = 0;
28317 have_location_lists = false;
28318 loclabel_num = 0;
28319 poc_label_num = 0;
28320 last_emitted_file = NULL;
28321 label_num = 0;
28322 tmpl_value_parm_die_table = NULL;
28323 generic_type_instances = NULL;
28324 frame_pointer_fb_offset = 0;
28325 frame_pointer_fb_offset_valid = false;
28326 base_types.release ();
28327 XDELETEVEC (producer_string);
28328 producer_string = NULL;
28331 #include "gt-dwarf2out.h"