2016-04-27 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / dwarf2out.c
blob0bbff87a95ee1fc0881327557b7e08647f0f8619
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
208 struct GTY((for_user)) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
216 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
218 typedef const char *compare_type;
220 static hashval_t hash (indirect_string_node *);
221 static bool equal (indirect_string_node *, const char *);
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227 main object file, rather than the dwo, similar to the force_direct
228 parameter elsewhere but with additional complications:
230 1) The string is needed in both the main object file and the dwo.
231 That is, the comp_dir and dwo_name will appear in both places.
233 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234 DW_FORM_GNU_str_index.
236 3) GCC chooses the form to use late, depending on the size and
237 reference count.
239 Rather than forcing the all debug string handling functions and
240 callers to deal with these complications, simply use a separate,
241 special-cased string table for any attribute that should go in the
242 main object file. This limits the complexity to just the places
243 that need it. */
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
247 static GTY(()) int dw2_string_counter;
249 /* True if the compilation unit places functions in more than one section. */
250 static GTY(()) bool have_multiple_function_sections = false;
252 /* Whether the default text and cold text sections have been used at all. */
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
257 /* The default cold text section. */
258 static GTY(()) section *cold_text_section;
260 /* The DIE for C++14 'auto' in a function return type. */
261 static GTY(()) dw_die_ref auto_die;
263 /* The DIE for C++14 'decltype(auto)' in a function return type. */
264 static GTY(()) dw_die_ref decltype_auto_die;
266 /* Forward declarations for functions defined in this file. */
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. Some constants have a large uniform precision, so
372 we get the precision needed for the actual value of the number. */
374 static unsigned int
375 get_full_len (const wide_int &op)
377 int prec = wi::min_precision (op, UNSIGNED);
378 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379 / HOST_BITS_PER_WIDE_INT);
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 enum debug_struct_file criterion;
386 tree type_decl;
387 bool generic = lang_hooks.types.generic_p (type);
389 if (generic)
390 criterion = debug_struct_generic[usage];
391 else
392 criterion = debug_struct_ordinary[usage];
394 if (criterion == DINFO_STRUCT_FILE_NONE)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396 if (criterion == DINFO_STRUCT_FILE_ANY)
397 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401 if (type_decl != NULL)
403 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 /* Return a pointer to a copy of the section string name S with all
414 attributes stripped off, and an asterisk prepended (for assemble_name). */
416 static inline char *
417 stripattributes (const char *s)
419 char *stripped = XNEWVEC (char, strlen (s) + 2);
420 char *p = stripped;
422 *p++ = '*';
424 while (*s && *s != ',')
425 *p++ = *s++;
427 *p = '\0';
428 return stripped;
431 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
432 switch to the data section instead, and write out a synthetic start label
433 for collect2 the first time around. */
435 static void
436 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
438 if (eh_frame_section == 0)
440 int flags;
442 if (EH_TABLES_CAN_BE_READ_ONLY)
444 int fde_encoding;
445 int per_encoding;
446 int lsda_encoding;
448 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
449 /*global=*/0);
450 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
451 /*global=*/1);
452 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
453 /*global=*/0);
454 flags = ((! flag_pic
455 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
456 && (fde_encoding & 0x70) != DW_EH_PE_aligned
457 && (per_encoding & 0x70) != DW_EH_PE_absptr
458 && (per_encoding & 0x70) != DW_EH_PE_aligned
459 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
460 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
461 ? 0 : SECTION_WRITE);
463 else
464 flags = SECTION_WRITE;
466 #ifdef EH_FRAME_SECTION_NAME
467 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #else
469 eh_frame_section = ((flags == SECTION_WRITE)
470 ? data_section : readonly_data_section);
471 #endif /* EH_FRAME_SECTION_NAME */
474 switch_to_section (eh_frame_section);
476 #ifdef EH_FRAME_THROUGH_COLLECT2
477 /* We have no special eh_frame section. Emit special labels to guide
478 collect2. */
479 if (!back)
481 tree label = get_file_function_name ("F");
482 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
483 targetm.asm_out.globalize_label (asm_out_file,
484 IDENTIFIER_POINTER (label));
485 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
487 #endif
490 /* Switch [BACK] to the eh or debug frame table section, depending on
491 FOR_EH. */
493 static void
494 switch_to_frame_table_section (int for_eh, bool back)
496 if (for_eh)
497 switch_to_eh_frame_section (back);
498 else
500 if (!debug_frame_section)
501 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
502 SECTION_DEBUG, NULL);
503 switch_to_section (debug_frame_section);
507 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
509 enum dw_cfi_oprnd_type
510 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
512 switch (cfi)
514 case DW_CFA_nop:
515 case DW_CFA_GNU_window_save:
516 case DW_CFA_remember_state:
517 case DW_CFA_restore_state:
518 return dw_cfi_oprnd_unused;
520 case DW_CFA_set_loc:
521 case DW_CFA_advance_loc1:
522 case DW_CFA_advance_loc2:
523 case DW_CFA_advance_loc4:
524 case DW_CFA_MIPS_advance_loc8:
525 return dw_cfi_oprnd_addr;
527 case DW_CFA_offset:
528 case DW_CFA_offset_extended:
529 case DW_CFA_def_cfa:
530 case DW_CFA_offset_extended_sf:
531 case DW_CFA_def_cfa_sf:
532 case DW_CFA_restore:
533 case DW_CFA_restore_extended:
534 case DW_CFA_undefined:
535 case DW_CFA_same_value:
536 case DW_CFA_def_cfa_register:
537 case DW_CFA_register:
538 case DW_CFA_expression:
539 return dw_cfi_oprnd_reg_num;
541 case DW_CFA_def_cfa_offset:
542 case DW_CFA_GNU_args_size:
543 case DW_CFA_def_cfa_offset_sf:
544 return dw_cfi_oprnd_offset;
546 case DW_CFA_def_cfa_expression:
547 return dw_cfi_oprnd_loc;
549 default:
550 gcc_unreachable ();
554 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
556 enum dw_cfi_oprnd_type
557 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
559 switch (cfi)
561 case DW_CFA_def_cfa:
562 case DW_CFA_def_cfa_sf:
563 case DW_CFA_offset:
564 case DW_CFA_offset_extended_sf:
565 case DW_CFA_offset_extended:
566 return dw_cfi_oprnd_offset;
568 case DW_CFA_register:
569 return dw_cfi_oprnd_reg_num;
571 case DW_CFA_expression:
572 return dw_cfi_oprnd_loc;
574 default:
575 return dw_cfi_oprnd_unused;
579 /* Output one FDE. */
581 static void
582 output_fde (dw_fde_ref fde, bool for_eh, bool second,
583 char *section_start_label, int fde_encoding, char *augmentation,
584 bool any_lsda_needed, int lsda_encoding)
586 const char *begin, *end;
587 static unsigned int j;
588 char l1[20], l2[20];
590 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
591 /* empty */ 0);
592 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
593 for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
595 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
596 if (!XCOFF_DEBUGGING_INFO || for_eh)
598 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
599 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
600 " indicating 64-bit DWARF extension");
601 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
602 "FDE Length");
604 ASM_OUTPUT_LABEL (asm_out_file, l1);
606 if (for_eh)
607 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
608 else
609 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
610 debug_frame_section, "FDE CIE offset");
612 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
613 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
615 if (for_eh)
617 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
618 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
619 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
620 "FDE initial location");
621 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
622 end, begin, "FDE address range");
624 else
626 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
627 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
630 if (augmentation[0])
632 if (any_lsda_needed)
634 int size = size_of_encoded_value (lsda_encoding);
636 if (lsda_encoding == DW_EH_PE_aligned)
638 int offset = ( 4 /* Length */
639 + 4 /* CIE offset */
640 + 2 * size_of_encoded_value (fde_encoding)
641 + 1 /* Augmentation size */ );
642 int pad = -offset & (PTR_SIZE - 1);
644 size += pad;
645 gcc_assert (size_of_uleb128 (size) == 1);
648 dw2_asm_output_data_uleb128 (size, "Augmentation size");
650 if (fde->uses_eh_lsda)
652 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
653 fde->funcdef_number);
654 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
655 gen_rtx_SYMBOL_REF (Pmode, l1),
656 false,
657 "Language Specific Data Area");
659 else
661 if (lsda_encoding == DW_EH_PE_aligned)
662 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
663 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
664 "Language Specific Data Area (none)");
667 else
668 dw2_asm_output_data_uleb128 (0, "Augmentation size");
671 /* Loop through the Call Frame Instructions associated with this FDE. */
672 fde->dw_fde_current_label = begin;
674 size_t from, until, i;
676 from = 0;
677 until = vec_safe_length (fde->dw_fde_cfi);
679 if (fde->dw_fde_second_begin == NULL)
681 else if (!second)
682 until = fde->dw_fde_switch_cfi_index;
683 else
684 from = fde->dw_fde_switch_cfi_index;
686 for (i = from; i < until; i++)
687 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
690 /* If we are to emit a ref/link from function bodies to their frame tables,
691 do it now. This is typically performed to make sure that tables
692 associated with functions are dragged with them and not discarded in
693 garbage collecting links. We need to do this on a per function basis to
694 cope with -ffunction-sections. */
696 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
697 /* Switch to the function section, emit the ref to the tables, and
698 switch *back* into the table section. */
699 switch_to_section (function_section (fde->decl));
700 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
701 switch_to_frame_table_section (for_eh, true);
702 #endif
704 /* Pad the FDE out to an address sized boundary. */
705 ASM_OUTPUT_ALIGN (asm_out_file,
706 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
707 ASM_OUTPUT_LABEL (asm_out_file, l2);
709 j += 2;
712 /* Return true if frame description entry FDE is needed for EH. */
714 static bool
715 fde_needed_for_eh_p (dw_fde_ref fde)
717 if (flag_asynchronous_unwind_tables)
718 return true;
720 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
721 return true;
723 if (fde->uses_eh_lsda)
724 return true;
726 /* If exceptions are enabled, we have collected nothrow info. */
727 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
728 return false;
730 return true;
733 /* Output the call frame information used to record information
734 that relates to calculating the frame pointer, and records the
735 location of saved registers. */
737 static void
738 output_call_frame_info (int for_eh)
740 unsigned int i;
741 dw_fde_ref fde;
742 dw_cfi_ref cfi;
743 char l1[20], l2[20], section_start_label[20];
744 bool any_lsda_needed = false;
745 char augmentation[6];
746 int augmentation_size;
747 int fde_encoding = DW_EH_PE_absptr;
748 int per_encoding = DW_EH_PE_absptr;
749 int lsda_encoding = DW_EH_PE_absptr;
750 int return_reg;
751 rtx personality = NULL;
752 int dw_cie_version;
754 /* Don't emit a CIE if there won't be any FDEs. */
755 if (!fde_vec)
756 return;
758 /* Nothing to do if the assembler's doing it all. */
759 if (dwarf2out_do_cfi_asm ())
760 return;
762 /* If we don't have any functions we'll want to unwind out of, don't emit
763 any EH unwind information. If we make FDEs linkonce, we may have to
764 emit an empty label for an FDE that wouldn't otherwise be emitted. We
765 want to avoid having an FDE kept around when the function it refers to
766 is discarded. Example where this matters: a primary function template
767 in C++ requires EH information, an explicit specialization doesn't. */
768 if (for_eh)
770 bool any_eh_needed = false;
772 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
774 if (fde->uses_eh_lsda)
775 any_eh_needed = any_lsda_needed = true;
776 else if (fde_needed_for_eh_p (fde))
777 any_eh_needed = true;
778 else if (TARGET_USES_WEAK_UNWIND_INFO)
779 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
782 if (!any_eh_needed)
783 return;
786 /* We're going to be generating comments, so turn on app. */
787 if (flag_debug_asm)
788 app_enable ();
790 /* Switch to the proper frame section, first time. */
791 switch_to_frame_table_section (for_eh, false);
793 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
794 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
796 /* Output the CIE. */
797 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
798 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
799 if (!XCOFF_DEBUGGING_INFO || for_eh)
801 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
802 dw2_asm_output_data (4, 0xffffffff,
803 "Initial length escape value indicating 64-bit DWARF extension");
804 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
805 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 struct GTY((for_user)) addr_table_entry {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1277 /* Location lists are ranges + location descriptions for that range,
1278 so you can track variables that are in different places over
1279 their entire life. */
1280 typedef struct GTY(()) dw_loc_list_struct {
1281 dw_loc_list_ref dw_loc_next;
1282 const char *begin; /* Label and addr_entry for start of range */
1283 addr_table_entry *begin_entry;
1284 const char *end; /* Label for end of range */
1285 char *ll_symbol; /* Label for beginning of location list.
1286 Only on head of list */
1287 const char *section; /* Section this loclist is relative to */
1288 dw_loc_descr_ref expr;
1289 hashval_t hash;
1290 /* True if all addresses in this and subsequent lists are known to be
1291 resolved. */
1292 bool resolved_addr;
1293 /* True if this list has been replaced by dw_loc_next. */
1294 bool replaced;
1295 bool emitted;
1296 /* True if the range should be emitted even if begin and end
1297 are the same. */
1298 bool force;
1299 } dw_loc_list_node;
1301 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1302 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1304 /* Convert a DWARF stack opcode into its string name. */
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1309 const char *name = get_DW_OP_name (op);
1311 if (name != NULL)
1312 return name;
1314 return "OP_<unknown>";
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1327 descr->dw_loc_opc = op;
1328 #if CHECKING_P
1329 descr->dw_loc_frame_offset = -1;
1330 #endif
1331 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd1.val_entry = NULL;
1333 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1334 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd2.val_entry = NULL;
1336 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1338 return descr;
1341 /* Return a pointer to a newly allocated location description for
1342 REG and OFFSET. */
1344 static inline dw_loc_descr_ref
1345 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1347 if (reg <= 31)
1348 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1349 offset, 0);
1350 else
1351 return new_loc_descr (DW_OP_bregx, reg, offset);
1354 /* Add a location description term to a location description expression. */
1356 static inline void
1357 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1359 dw_loc_descr_ref *d;
1361 /* Find the end of the chain. */
1362 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1365 *d = descr;
1368 /* Compare two location operands for exact equality. */
1370 static bool
1371 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1373 if (a->val_class != b->val_class)
1374 return false;
1375 switch (a->val_class)
1377 case dw_val_class_none:
1378 return true;
1379 case dw_val_class_addr:
1380 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1382 case dw_val_class_offset:
1383 case dw_val_class_unsigned_const:
1384 case dw_val_class_const:
1385 case dw_val_class_range_list:
1386 case dw_val_class_lineptr:
1387 case dw_val_class_macptr:
1388 /* These are all HOST_WIDE_INT, signed or unsigned. */
1389 return a->v.val_unsigned == b->v.val_unsigned;
1391 case dw_val_class_loc:
1392 return a->v.val_loc == b->v.val_loc;
1393 case dw_val_class_loc_list:
1394 return a->v.val_loc_list == b->v.val_loc_list;
1395 case dw_val_class_die_ref:
1396 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1397 case dw_val_class_fde_ref:
1398 return a->v.val_fde_index == b->v.val_fde_index;
1399 case dw_val_class_lbl_id:
1400 case dw_val_class_high_pc:
1401 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402 case dw_val_class_str:
1403 return a->v.val_str == b->v.val_str;
1404 case dw_val_class_flag:
1405 return a->v.val_flag == b->v.val_flag;
1406 case dw_val_class_file:
1407 return a->v.val_file == b->v.val_file;
1408 case dw_val_class_decl_ref:
1409 return a->v.val_decl_ref == b->v.val_decl_ref;
1411 case dw_val_class_const_double:
1412 return (a->v.val_double.high == b->v.val_double.high
1413 && a->v.val_double.low == b->v.val_double.low);
1415 case dw_val_class_wide_int:
1416 return *a->v.val_wide == *b->v.val_wide;
1418 case dw_val_class_vec:
1420 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1421 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1423 return (a_len == b_len
1424 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1427 case dw_val_class_data8:
1428 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1430 case dw_val_class_vms_delta:
1431 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1432 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1434 case dw_val_class_discr_value:
1435 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1436 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1437 case dw_val_class_discr_list:
1438 /* It makes no sense comparing two discriminant value lists. */
1439 return false;
1441 gcc_unreachable ();
1444 /* Compare two location atoms for exact equality. */
1446 static bool
1447 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1449 if (a->dw_loc_opc != b->dw_loc_opc)
1450 return false;
1452 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1453 address size, but since we always allocate cleared storage it
1454 should be zero for other types of locations. */
1455 if (a->dtprel != b->dtprel)
1456 return false;
1458 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1459 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1462 /* Compare two complete location expressions for exact equality. */
1464 bool
1465 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1467 while (1)
1469 if (a == b)
1470 return true;
1471 if (a == NULL || b == NULL)
1472 return false;
1473 if (!loc_descr_equal_p_1 (a, b))
1474 return false;
1476 a = a->dw_loc_next;
1477 b = b->dw_loc_next;
1482 /* Add a constant OFFSET to a location expression. */
1484 static void
1485 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1487 dw_loc_descr_ref loc;
1488 HOST_WIDE_INT *p;
1490 gcc_assert (*list_head != NULL);
1492 if (!offset)
1493 return;
1495 /* Find the end of the chain. */
1496 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1499 p = NULL;
1500 if (loc->dw_loc_opc == DW_OP_fbreg
1501 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1502 p = &loc->dw_loc_oprnd1.v.val_int;
1503 else if (loc->dw_loc_opc == DW_OP_bregx)
1504 p = &loc->dw_loc_oprnd2.v.val_int;
1506 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1507 offset. Don't optimize if an signed integer overflow would happen. */
1508 if (p != NULL
1509 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1510 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1511 *p += offset;
1513 else if (offset > 0)
1514 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1516 else
1518 loc->dw_loc_next = int_loc_descriptor (-offset);
1519 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1523 /* Add a constant OFFSET to a location list. */
1525 static void
1526 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1528 dw_loc_list_ref d;
1529 for (d = list_head; d != NULL; d = d->dw_loc_next)
1530 loc_descr_plus_const (&d->expr, offset);
1533 #define DWARF_REF_SIZE \
1534 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1536 static unsigned long int get_base_type_offset (dw_die_ref);
1538 /* Return the size of a location descriptor. */
1540 static unsigned long
1541 size_of_loc_descr (dw_loc_descr_ref loc)
1543 unsigned long size = 1;
1545 switch (loc->dw_loc_opc)
1547 case DW_OP_addr:
1548 size += DWARF2_ADDR_SIZE;
1549 break;
1550 case DW_OP_GNU_addr_index:
1551 case DW_OP_GNU_const_index:
1552 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1553 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1554 break;
1555 case DW_OP_const1u:
1556 case DW_OP_const1s:
1557 size += 1;
1558 break;
1559 case DW_OP_const2u:
1560 case DW_OP_const2s:
1561 size += 2;
1562 break;
1563 case DW_OP_const4u:
1564 case DW_OP_const4s:
1565 size += 4;
1566 break;
1567 case DW_OP_const8u:
1568 case DW_OP_const8s:
1569 size += 8;
1570 break;
1571 case DW_OP_constu:
1572 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1573 break;
1574 case DW_OP_consts:
1575 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1576 break;
1577 case DW_OP_pick:
1578 size += 1;
1579 break;
1580 case DW_OP_plus_uconst:
1581 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1582 break;
1583 case DW_OP_skip:
1584 case DW_OP_bra:
1585 size += 2;
1586 break;
1587 case DW_OP_breg0:
1588 case DW_OP_breg1:
1589 case DW_OP_breg2:
1590 case DW_OP_breg3:
1591 case DW_OP_breg4:
1592 case DW_OP_breg5:
1593 case DW_OP_breg6:
1594 case DW_OP_breg7:
1595 case DW_OP_breg8:
1596 case DW_OP_breg9:
1597 case DW_OP_breg10:
1598 case DW_OP_breg11:
1599 case DW_OP_breg12:
1600 case DW_OP_breg13:
1601 case DW_OP_breg14:
1602 case DW_OP_breg15:
1603 case DW_OP_breg16:
1604 case DW_OP_breg17:
1605 case DW_OP_breg18:
1606 case DW_OP_breg19:
1607 case DW_OP_breg20:
1608 case DW_OP_breg21:
1609 case DW_OP_breg22:
1610 case DW_OP_breg23:
1611 case DW_OP_breg24:
1612 case DW_OP_breg25:
1613 case DW_OP_breg26:
1614 case DW_OP_breg27:
1615 case DW_OP_breg28:
1616 case DW_OP_breg29:
1617 case DW_OP_breg30:
1618 case DW_OP_breg31:
1619 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1620 break;
1621 case DW_OP_regx:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_fbreg:
1625 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1626 break;
1627 case DW_OP_bregx:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1630 break;
1631 case DW_OP_piece:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1633 break;
1634 case DW_OP_bit_piece:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1636 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1637 break;
1638 case DW_OP_deref_size:
1639 case DW_OP_xderef_size:
1640 size += 1;
1641 break;
1642 case DW_OP_call2:
1643 size += 2;
1644 break;
1645 case DW_OP_call4:
1646 size += 4;
1647 break;
1648 case DW_OP_call_ref:
1649 size += DWARF_REF_SIZE;
1650 break;
1651 case DW_OP_implicit_value:
1652 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1653 + loc->dw_loc_oprnd1.v.val_unsigned;
1654 break;
1655 case DW_OP_GNU_implicit_pointer:
1656 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1657 break;
1658 case DW_OP_GNU_entry_value:
1660 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1661 size += size_of_uleb128 (op_size) + op_size;
1662 break;
1664 case DW_OP_GNU_const_type:
1666 unsigned long o
1667 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1668 size += size_of_uleb128 (o) + 1;
1669 switch (loc->dw_loc_oprnd2.val_class)
1671 case dw_val_class_vec:
1672 size += loc->dw_loc_oprnd2.v.val_vec.length
1673 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1674 break;
1675 case dw_val_class_const:
1676 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1677 break;
1678 case dw_val_class_const_double:
1679 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1680 break;
1681 case dw_val_class_wide_int:
1682 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1683 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1684 break;
1685 default:
1686 gcc_unreachable ();
1688 break;
1690 case DW_OP_GNU_regval_type:
1692 unsigned long o
1693 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1694 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1695 + size_of_uleb128 (o);
1697 break;
1698 case DW_OP_GNU_deref_type:
1700 unsigned long o
1701 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1702 size += 1 + size_of_uleb128 (o);
1704 break;
1705 case DW_OP_GNU_convert:
1706 case DW_OP_GNU_reinterpret:
1707 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1708 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1709 else
1711 unsigned long o
1712 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1713 size += size_of_uleb128 (o);
1715 break;
1716 case DW_OP_GNU_parameter_ref:
1717 size += 4;
1718 break;
1719 default:
1720 break;
1723 return size;
1726 /* Return the size of a series of location descriptors. */
1728 unsigned long
1729 size_of_locs (dw_loc_descr_ref loc)
1731 dw_loc_descr_ref l;
1732 unsigned long size;
1734 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1735 field, to avoid writing to a PCH file. */
1736 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1738 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1739 break;
1740 size += size_of_loc_descr (l);
1742 if (! l)
1743 return size;
1745 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1747 l->dw_loc_addr = size;
1748 size += size_of_loc_descr (l);
1751 return size;
1754 /* Return the size of the value in a DW_AT_discr_value attribute. */
1756 static int
1757 size_of_discr_value (dw_discr_value *discr_value)
1759 if (discr_value->pos)
1760 return size_of_uleb128 (discr_value->v.uval);
1761 else
1762 return size_of_sleb128 (discr_value->v.sval);
1765 /* Return the size of the value in a DW_discr_list attribute. */
1767 static int
1768 size_of_discr_list (dw_discr_list_ref discr_list)
1770 int size = 0;
1772 for (dw_discr_list_ref list = discr_list;
1773 list != NULL;
1774 list = list->dw_discr_next)
1776 /* One byte for the discriminant value descriptor, and then one or two
1777 LEB128 numbers, depending on whether it's a single case label or a
1778 range label. */
1779 size += 1;
1780 size += size_of_discr_value (&list->dw_discr_lower_bound);
1781 if (list->dw_discr_range != 0)
1782 size += size_of_discr_value (&list->dw_discr_upper_bound);
1784 return size;
1787 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1788 static void get_ref_die_offset_label (char *, dw_die_ref);
1789 static unsigned long int get_ref_die_offset (dw_die_ref);
1791 /* Output location description stack opcode's operands (if any).
1792 The for_eh_or_skip parameter controls whether register numbers are
1793 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1794 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1795 info). This should be suppressed for the cases that have not been converted
1796 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1798 static void
1799 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1801 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1802 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1804 switch (loc->dw_loc_opc)
1806 #ifdef DWARF2_DEBUGGING_INFO
1807 case DW_OP_const2u:
1808 case DW_OP_const2s:
1809 dw2_asm_output_data (2, val1->v.val_int, NULL);
1810 break;
1811 case DW_OP_const4u:
1812 if (loc->dtprel)
1814 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1815 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1816 val1->v.val_addr);
1817 fputc ('\n', asm_out_file);
1818 break;
1820 /* FALLTHRU */
1821 case DW_OP_const4s:
1822 dw2_asm_output_data (4, val1->v.val_int, NULL);
1823 break;
1824 case DW_OP_const8u:
1825 if (loc->dtprel)
1827 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1828 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1829 val1->v.val_addr);
1830 fputc ('\n', asm_out_file);
1831 break;
1833 /* FALLTHRU */
1834 case DW_OP_const8s:
1835 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1836 dw2_asm_output_data (8, val1->v.val_int, NULL);
1837 break;
1838 case DW_OP_skip:
1839 case DW_OP_bra:
1841 int offset;
1843 gcc_assert (val1->val_class == dw_val_class_loc);
1844 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1846 dw2_asm_output_data (2, offset, NULL);
1848 break;
1849 case DW_OP_implicit_value:
1850 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1851 switch (val2->val_class)
1853 case dw_val_class_const:
1854 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1855 break;
1856 case dw_val_class_vec:
1858 unsigned int elt_size = val2->v.val_vec.elt_size;
1859 unsigned int len = val2->v.val_vec.length;
1860 unsigned int i;
1861 unsigned char *p;
1863 if (elt_size > sizeof (HOST_WIDE_INT))
1865 elt_size /= 2;
1866 len *= 2;
1868 for (i = 0, p = val2->v.val_vec.array;
1869 i < len;
1870 i++, p += elt_size)
1871 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1872 "fp or vector constant word %u", i);
1874 break;
1875 case dw_val_class_const_double:
1877 unsigned HOST_WIDE_INT first, second;
1879 if (WORDS_BIG_ENDIAN)
1881 first = val2->v.val_double.high;
1882 second = val2->v.val_double.low;
1884 else
1886 first = val2->v.val_double.low;
1887 second = val2->v.val_double.high;
1889 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1890 first, NULL);
1891 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1892 second, NULL);
1894 break;
1895 case dw_val_class_wide_int:
1897 int i;
1898 int len = get_full_len (*val2->v.val_wide);
1899 if (WORDS_BIG_ENDIAN)
1900 for (i = len - 1; i >= 0; --i)
1901 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1902 val2->v.val_wide->elt (i), NULL);
1903 else
1904 for (i = 0; i < len; ++i)
1905 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1906 val2->v.val_wide->elt (i), NULL);
1908 break;
1909 case dw_val_class_addr:
1910 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1911 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1912 break;
1913 default:
1914 gcc_unreachable ();
1916 break;
1917 #else
1918 case DW_OP_const2u:
1919 case DW_OP_const2s:
1920 case DW_OP_const4u:
1921 case DW_OP_const4s:
1922 case DW_OP_const8u:
1923 case DW_OP_const8s:
1924 case DW_OP_skip:
1925 case DW_OP_bra:
1926 case DW_OP_implicit_value:
1927 /* We currently don't make any attempt to make sure these are
1928 aligned properly like we do for the main unwind info, so
1929 don't support emitting things larger than a byte if we're
1930 only doing unwinding. */
1931 gcc_unreachable ();
1932 #endif
1933 case DW_OP_const1u:
1934 case DW_OP_const1s:
1935 dw2_asm_output_data (1, val1->v.val_int, NULL);
1936 break;
1937 case DW_OP_constu:
1938 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1939 break;
1940 case DW_OP_consts:
1941 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1942 break;
1943 case DW_OP_pick:
1944 dw2_asm_output_data (1, val1->v.val_int, NULL);
1945 break;
1946 case DW_OP_plus_uconst:
1947 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1948 break;
1949 case DW_OP_breg0:
1950 case DW_OP_breg1:
1951 case DW_OP_breg2:
1952 case DW_OP_breg3:
1953 case DW_OP_breg4:
1954 case DW_OP_breg5:
1955 case DW_OP_breg6:
1956 case DW_OP_breg7:
1957 case DW_OP_breg8:
1958 case DW_OP_breg9:
1959 case DW_OP_breg10:
1960 case DW_OP_breg11:
1961 case DW_OP_breg12:
1962 case DW_OP_breg13:
1963 case DW_OP_breg14:
1964 case DW_OP_breg15:
1965 case DW_OP_breg16:
1966 case DW_OP_breg17:
1967 case DW_OP_breg18:
1968 case DW_OP_breg19:
1969 case DW_OP_breg20:
1970 case DW_OP_breg21:
1971 case DW_OP_breg22:
1972 case DW_OP_breg23:
1973 case DW_OP_breg24:
1974 case DW_OP_breg25:
1975 case DW_OP_breg26:
1976 case DW_OP_breg27:
1977 case DW_OP_breg28:
1978 case DW_OP_breg29:
1979 case DW_OP_breg30:
1980 case DW_OP_breg31:
1981 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1982 break;
1983 case DW_OP_regx:
1985 unsigned r = val1->v.val_unsigned;
1986 if (for_eh_or_skip >= 0)
1987 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1988 gcc_assert (size_of_uleb128 (r)
1989 == size_of_uleb128 (val1->v.val_unsigned));
1990 dw2_asm_output_data_uleb128 (r, NULL);
1992 break;
1993 case DW_OP_fbreg:
1994 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1995 break;
1996 case DW_OP_bregx:
1998 unsigned r = val1->v.val_unsigned;
1999 if (for_eh_or_skip >= 0)
2000 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2001 gcc_assert (size_of_uleb128 (r)
2002 == size_of_uleb128 (val1->v.val_unsigned));
2003 dw2_asm_output_data_uleb128 (r, NULL);
2004 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2006 break;
2007 case DW_OP_piece:
2008 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2009 break;
2010 case DW_OP_bit_piece:
2011 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2012 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2013 break;
2014 case DW_OP_deref_size:
2015 case DW_OP_xderef_size:
2016 dw2_asm_output_data (1, val1->v.val_int, NULL);
2017 break;
2019 case DW_OP_addr:
2020 if (loc->dtprel)
2022 if (targetm.asm_out.output_dwarf_dtprel)
2024 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2025 DWARF2_ADDR_SIZE,
2026 val1->v.val_addr);
2027 fputc ('\n', asm_out_file);
2029 else
2030 gcc_unreachable ();
2032 else
2034 #ifdef DWARF2_DEBUGGING_INFO
2035 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2036 #else
2037 gcc_unreachable ();
2038 #endif
2040 break;
2042 case DW_OP_GNU_addr_index:
2043 case DW_OP_GNU_const_index:
2044 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2045 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2046 "(index into .debug_addr)");
2047 break;
2049 case DW_OP_call2:
2050 case DW_OP_call4:
2052 unsigned long die_offset
2053 = get_ref_die_offset (val1->v.val_die_ref.die);
2054 /* Make sure the offset has been computed and that we can encode it as
2055 an operand. */
2056 gcc_assert (die_offset > 0
2057 && die_offset <= (loc->dw_loc_opc == DW_OP_call2)
2058 ? 0xffff
2059 : 0xffffffff);
2060 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2061 die_offset, NULL);
2063 break;
2065 case DW_OP_GNU_implicit_pointer:
2067 char label[MAX_ARTIFICIAL_LABEL_BYTES
2068 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2069 gcc_assert (val1->val_class == dw_val_class_die_ref);
2070 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2071 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2072 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2074 break;
2076 case DW_OP_GNU_entry_value:
2077 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2078 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2079 break;
2081 case DW_OP_GNU_const_type:
2083 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2084 gcc_assert (o);
2085 dw2_asm_output_data_uleb128 (o, NULL);
2086 switch (val2->val_class)
2088 case dw_val_class_const:
2089 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2090 dw2_asm_output_data (1, l, NULL);
2091 dw2_asm_output_data (l, val2->v.val_int, NULL);
2092 break;
2093 case dw_val_class_vec:
2095 unsigned int elt_size = val2->v.val_vec.elt_size;
2096 unsigned int len = val2->v.val_vec.length;
2097 unsigned int i;
2098 unsigned char *p;
2100 l = len * elt_size;
2101 dw2_asm_output_data (1, l, NULL);
2102 if (elt_size > sizeof (HOST_WIDE_INT))
2104 elt_size /= 2;
2105 len *= 2;
2107 for (i = 0, p = val2->v.val_vec.array;
2108 i < len;
2109 i++, p += elt_size)
2110 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2111 "fp or vector constant word %u", i);
2113 break;
2114 case dw_val_class_const_double:
2116 unsigned HOST_WIDE_INT first, second;
2117 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2119 dw2_asm_output_data (1, 2 * l, NULL);
2120 if (WORDS_BIG_ENDIAN)
2122 first = val2->v.val_double.high;
2123 second = val2->v.val_double.low;
2125 else
2127 first = val2->v.val_double.low;
2128 second = val2->v.val_double.high;
2130 dw2_asm_output_data (l, first, NULL);
2131 dw2_asm_output_data (l, second, NULL);
2133 break;
2134 case dw_val_class_wide_int:
2136 int i;
2137 int len = get_full_len (*val2->v.val_wide);
2138 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2140 dw2_asm_output_data (1, len * l, NULL);
2141 if (WORDS_BIG_ENDIAN)
2142 for (i = len - 1; i >= 0; --i)
2143 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2144 else
2145 for (i = 0; i < len; ++i)
2146 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2148 break;
2149 default:
2150 gcc_unreachable ();
2153 break;
2154 case DW_OP_GNU_regval_type:
2156 unsigned r = val1->v.val_unsigned;
2157 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2158 gcc_assert (o);
2159 if (for_eh_or_skip >= 0)
2161 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2162 gcc_assert (size_of_uleb128 (r)
2163 == size_of_uleb128 (val1->v.val_unsigned));
2165 dw2_asm_output_data_uleb128 (r, NULL);
2166 dw2_asm_output_data_uleb128 (o, NULL);
2168 break;
2169 case DW_OP_GNU_deref_type:
2171 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2172 gcc_assert (o);
2173 dw2_asm_output_data (1, val1->v.val_int, NULL);
2174 dw2_asm_output_data_uleb128 (o, NULL);
2176 break;
2177 case DW_OP_GNU_convert:
2178 case DW_OP_GNU_reinterpret:
2179 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2180 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2181 else
2183 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2184 gcc_assert (o);
2185 dw2_asm_output_data_uleb128 (o, NULL);
2187 break;
2189 case DW_OP_GNU_parameter_ref:
2191 unsigned long o;
2192 gcc_assert (val1->val_class == dw_val_class_die_ref);
2193 o = get_ref_die_offset (val1->v.val_die_ref.die);
2194 dw2_asm_output_data (4, o, NULL);
2196 break;
2198 default:
2199 /* Other codes have no operands. */
2200 break;
2204 /* Output a sequence of location operations.
2205 The for_eh_or_skip parameter controls whether register numbers are
2206 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2207 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2208 info). This should be suppressed for the cases that have not been converted
2209 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2211 void
2212 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2214 for (; loc != NULL; loc = loc->dw_loc_next)
2216 enum dwarf_location_atom opc = loc->dw_loc_opc;
2217 /* Output the opcode. */
2218 if (for_eh_or_skip >= 0
2219 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2221 unsigned r = (opc - DW_OP_breg0);
2222 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2223 gcc_assert (r <= 31);
2224 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2226 else if (for_eh_or_skip >= 0
2227 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2229 unsigned r = (opc - DW_OP_reg0);
2230 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2231 gcc_assert (r <= 31);
2232 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2235 dw2_asm_output_data (1, opc,
2236 "%s", dwarf_stack_op_name (opc));
2238 /* Output the operand(s) (if any). */
2239 output_loc_operands (loc, for_eh_or_skip);
2243 /* Output location description stack opcode's operands (if any).
2244 The output is single bytes on a line, suitable for .cfi_escape. */
2246 static void
2247 output_loc_operands_raw (dw_loc_descr_ref loc)
2249 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2250 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2252 switch (loc->dw_loc_opc)
2254 case DW_OP_addr:
2255 case DW_OP_GNU_addr_index:
2256 case DW_OP_GNU_const_index:
2257 case DW_OP_implicit_value:
2258 /* We cannot output addresses in .cfi_escape, only bytes. */
2259 gcc_unreachable ();
2261 case DW_OP_const1u:
2262 case DW_OP_const1s:
2263 case DW_OP_pick:
2264 case DW_OP_deref_size:
2265 case DW_OP_xderef_size:
2266 fputc (',', asm_out_file);
2267 dw2_asm_output_data_raw (1, val1->v.val_int);
2268 break;
2270 case DW_OP_const2u:
2271 case DW_OP_const2s:
2272 fputc (',', asm_out_file);
2273 dw2_asm_output_data_raw (2, val1->v.val_int);
2274 break;
2276 case DW_OP_const4u:
2277 case DW_OP_const4s:
2278 fputc (',', asm_out_file);
2279 dw2_asm_output_data_raw (4, val1->v.val_int);
2280 break;
2282 case DW_OP_const8u:
2283 case DW_OP_const8s:
2284 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2285 fputc (',', asm_out_file);
2286 dw2_asm_output_data_raw (8, val1->v.val_int);
2287 break;
2289 case DW_OP_skip:
2290 case DW_OP_bra:
2292 int offset;
2294 gcc_assert (val1->val_class == dw_val_class_loc);
2295 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2297 fputc (',', asm_out_file);
2298 dw2_asm_output_data_raw (2, offset);
2300 break;
2302 case DW_OP_regx:
2304 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2305 gcc_assert (size_of_uleb128 (r)
2306 == size_of_uleb128 (val1->v.val_unsigned));
2307 fputc (',', asm_out_file);
2308 dw2_asm_output_data_uleb128_raw (r);
2310 break;
2312 case DW_OP_constu:
2313 case DW_OP_plus_uconst:
2314 case DW_OP_piece:
2315 fputc (',', asm_out_file);
2316 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2317 break;
2319 case DW_OP_bit_piece:
2320 fputc (',', asm_out_file);
2321 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2322 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2323 break;
2325 case DW_OP_consts:
2326 case DW_OP_breg0:
2327 case DW_OP_breg1:
2328 case DW_OP_breg2:
2329 case DW_OP_breg3:
2330 case DW_OP_breg4:
2331 case DW_OP_breg5:
2332 case DW_OP_breg6:
2333 case DW_OP_breg7:
2334 case DW_OP_breg8:
2335 case DW_OP_breg9:
2336 case DW_OP_breg10:
2337 case DW_OP_breg11:
2338 case DW_OP_breg12:
2339 case DW_OP_breg13:
2340 case DW_OP_breg14:
2341 case DW_OP_breg15:
2342 case DW_OP_breg16:
2343 case DW_OP_breg17:
2344 case DW_OP_breg18:
2345 case DW_OP_breg19:
2346 case DW_OP_breg20:
2347 case DW_OP_breg21:
2348 case DW_OP_breg22:
2349 case DW_OP_breg23:
2350 case DW_OP_breg24:
2351 case DW_OP_breg25:
2352 case DW_OP_breg26:
2353 case DW_OP_breg27:
2354 case DW_OP_breg28:
2355 case DW_OP_breg29:
2356 case DW_OP_breg30:
2357 case DW_OP_breg31:
2358 case DW_OP_fbreg:
2359 fputc (',', asm_out_file);
2360 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2361 break;
2363 case DW_OP_bregx:
2365 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2366 gcc_assert (size_of_uleb128 (r)
2367 == size_of_uleb128 (val1->v.val_unsigned));
2368 fputc (',', asm_out_file);
2369 dw2_asm_output_data_uleb128_raw (r);
2370 fputc (',', asm_out_file);
2371 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2373 break;
2375 case DW_OP_GNU_implicit_pointer:
2376 case DW_OP_GNU_entry_value:
2377 case DW_OP_GNU_const_type:
2378 case DW_OP_GNU_regval_type:
2379 case DW_OP_GNU_deref_type:
2380 case DW_OP_GNU_convert:
2381 case DW_OP_GNU_reinterpret:
2382 case DW_OP_GNU_parameter_ref:
2383 gcc_unreachable ();
2384 break;
2386 default:
2387 /* Other codes have no operands. */
2388 break;
2392 void
2393 output_loc_sequence_raw (dw_loc_descr_ref loc)
2395 while (1)
2397 enum dwarf_location_atom opc = loc->dw_loc_opc;
2398 /* Output the opcode. */
2399 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2401 unsigned r = (opc - DW_OP_breg0);
2402 r = DWARF2_FRAME_REG_OUT (r, 1);
2403 gcc_assert (r <= 31);
2404 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2406 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2408 unsigned r = (opc - DW_OP_reg0);
2409 r = DWARF2_FRAME_REG_OUT (r, 1);
2410 gcc_assert (r <= 31);
2411 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2413 /* Output the opcode. */
2414 fprintf (asm_out_file, "%#x", opc);
2415 output_loc_operands_raw (loc);
2417 if (!loc->dw_loc_next)
2418 break;
2419 loc = loc->dw_loc_next;
2421 fputc (',', asm_out_file);
2425 /* This function builds a dwarf location descriptor sequence from a
2426 dw_cfa_location, adding the given OFFSET to the result of the
2427 expression. */
2429 struct dw_loc_descr_node *
2430 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2432 struct dw_loc_descr_node *head, *tmp;
2434 offset += cfa->offset;
2436 if (cfa->indirect)
2438 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2439 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2440 head->dw_loc_oprnd1.val_entry = NULL;
2441 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2442 add_loc_descr (&head, tmp);
2443 if (offset != 0)
2445 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2446 add_loc_descr (&head, tmp);
2449 else
2450 head = new_reg_loc_descr (cfa->reg, offset);
2452 return head;
2455 /* This function builds a dwarf location descriptor sequence for
2456 the address at OFFSET from the CFA when stack is aligned to
2457 ALIGNMENT byte. */
2459 struct dw_loc_descr_node *
2460 build_cfa_aligned_loc (dw_cfa_location *cfa,
2461 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2463 struct dw_loc_descr_node *head;
2464 unsigned int dwarf_fp
2465 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2467 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2468 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2470 head = new_reg_loc_descr (dwarf_fp, 0);
2471 add_loc_descr (&head, int_loc_descriptor (alignment));
2472 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2473 loc_descr_plus_const (&head, offset);
2475 else
2476 head = new_reg_loc_descr (dwarf_fp, offset);
2477 return head;
2480 /* And now, the support for symbolic debugging information. */
2482 /* .debug_str support. */
2484 static void dwarf2out_init (const char *);
2485 static void dwarf2out_finish (const char *);
2486 static void dwarf2out_early_finish (void);
2487 static void dwarf2out_assembly_start (void);
2488 static void dwarf2out_define (unsigned int, const char *);
2489 static void dwarf2out_undef (unsigned int, const char *);
2490 static void dwarf2out_start_source_file (unsigned, const char *);
2491 static void dwarf2out_end_source_file (unsigned);
2492 static void dwarf2out_function_decl (tree);
2493 static void dwarf2out_begin_block (unsigned, unsigned);
2494 static void dwarf2out_end_block (unsigned, unsigned);
2495 static bool dwarf2out_ignore_block (const_tree);
2496 static void dwarf2out_early_global_decl (tree);
2497 static void dwarf2out_late_global_decl (tree);
2498 static void dwarf2out_type_decl (tree, int);
2499 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2500 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2501 dw_die_ref);
2502 static void dwarf2out_abstract_function (tree);
2503 static void dwarf2out_var_location (rtx_insn *);
2504 static void dwarf2out_size_function (tree);
2505 static void dwarf2out_begin_function (tree);
2506 static void dwarf2out_end_function (unsigned int);
2507 static void dwarf2out_register_main_translation_unit (tree unit);
2508 static void dwarf2out_set_name (tree, tree);
2510 /* The debug hooks structure. */
2512 const struct gcc_debug_hooks dwarf2_debug_hooks =
2514 dwarf2out_init,
2515 dwarf2out_finish,
2516 dwarf2out_early_finish,
2517 dwarf2out_assembly_start,
2518 dwarf2out_define,
2519 dwarf2out_undef,
2520 dwarf2out_start_source_file,
2521 dwarf2out_end_source_file,
2522 dwarf2out_begin_block,
2523 dwarf2out_end_block,
2524 dwarf2out_ignore_block,
2525 dwarf2out_source_line,
2526 dwarf2out_begin_prologue,
2527 #if VMS_DEBUGGING_INFO
2528 dwarf2out_vms_end_prologue,
2529 dwarf2out_vms_begin_epilogue,
2530 #else
2531 debug_nothing_int_charstar,
2532 debug_nothing_int_charstar,
2533 #endif
2534 dwarf2out_end_epilogue,
2535 dwarf2out_begin_function,
2536 dwarf2out_end_function, /* end_function */
2537 dwarf2out_register_main_translation_unit,
2538 dwarf2out_function_decl, /* function_decl */
2539 dwarf2out_early_global_decl,
2540 dwarf2out_late_global_decl,
2541 dwarf2out_type_decl, /* type_decl */
2542 dwarf2out_imported_module_or_decl,
2543 debug_nothing_tree, /* deferred_inline_function */
2544 /* The DWARF 2 backend tries to reduce debugging bloat by not
2545 emitting the abstract description of inline functions until
2546 something tries to reference them. */
2547 dwarf2out_abstract_function, /* outlining_inline_function */
2548 debug_nothing_rtx_code_label, /* label */
2549 debug_nothing_int, /* handle_pch */
2550 dwarf2out_var_location,
2551 dwarf2out_size_function, /* size_function */
2552 dwarf2out_switch_text_section,
2553 dwarf2out_set_name,
2554 1, /* start_end_main_source_file */
2555 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2558 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2560 dwarf2out_init,
2561 debug_nothing_charstar,
2562 debug_nothing_void,
2563 debug_nothing_void,
2564 debug_nothing_int_charstar,
2565 debug_nothing_int_charstar,
2566 debug_nothing_int_charstar,
2567 debug_nothing_int,
2568 debug_nothing_int_int, /* begin_block */
2569 debug_nothing_int_int, /* end_block */
2570 debug_true_const_tree, /* ignore_block */
2571 dwarf2out_source_line, /* source_line */
2572 debug_nothing_int_charstar, /* begin_prologue */
2573 debug_nothing_int_charstar, /* end_prologue */
2574 debug_nothing_int_charstar, /* begin_epilogue */
2575 debug_nothing_int_charstar, /* end_epilogue */
2576 debug_nothing_tree, /* begin_function */
2577 debug_nothing_int, /* end_function */
2578 debug_nothing_tree, /* register_main_translation_unit */
2579 debug_nothing_tree, /* function_decl */
2580 debug_nothing_tree, /* early_global_decl */
2581 debug_nothing_tree, /* late_global_decl */
2582 debug_nothing_tree_int, /* type_decl */
2583 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2584 debug_nothing_tree, /* deferred_inline_function */
2585 debug_nothing_tree, /* outlining_inline_function */
2586 debug_nothing_rtx_code_label, /* label */
2587 debug_nothing_int, /* handle_pch */
2588 debug_nothing_rtx_insn, /* var_location */
2589 debug_nothing_tree, /* size_function */
2590 debug_nothing_void, /* switch_text_section */
2591 debug_nothing_tree_tree, /* set_name */
2592 0, /* start_end_main_source_file */
2593 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2596 /* NOTE: In the comments in this file, many references are made to
2597 "Debugging Information Entries". This term is abbreviated as `DIE'
2598 throughout the remainder of this file. */
2600 /* An internal representation of the DWARF output is built, and then
2601 walked to generate the DWARF debugging info. The walk of the internal
2602 representation is done after the entire program has been compiled.
2603 The types below are used to describe the internal representation. */
2605 /* Whether to put type DIEs into their own section .debug_types instead
2606 of making them part of the .debug_info section. Only supported for
2607 Dwarf V4 or higher and the user didn't disable them through
2608 -fno-debug-types-section. It is more efficient to put them in a
2609 separate comdat sections since the linker will then be able to
2610 remove duplicates. But not all tools support .debug_types sections
2611 yet. */
2613 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2615 /* Various DIE's use offsets relative to the beginning of the
2616 .debug_info section to refer to each other. */
2618 typedef long int dw_offset;
2620 struct comdat_type_node;
2622 /* The entries in the line_info table more-or-less mirror the opcodes
2623 that are used in the real dwarf line table. Arrays of these entries
2624 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2625 supported. */
2627 enum dw_line_info_opcode {
2628 /* Emit DW_LNE_set_address; the operand is the label index. */
2629 LI_set_address,
2631 /* Emit a row to the matrix with the given line. This may be done
2632 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2633 special opcodes. */
2634 LI_set_line,
2636 /* Emit a DW_LNS_set_file. */
2637 LI_set_file,
2639 /* Emit a DW_LNS_set_column. */
2640 LI_set_column,
2642 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2643 LI_negate_stmt,
2645 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2646 LI_set_prologue_end,
2647 LI_set_epilogue_begin,
2649 /* Emit a DW_LNE_set_discriminator. */
2650 LI_set_discriminator
2653 typedef struct GTY(()) dw_line_info_struct {
2654 enum dw_line_info_opcode opcode;
2655 unsigned int val;
2656 } dw_line_info_entry;
2659 struct GTY(()) dw_line_info_table {
2660 /* The label that marks the end of this section. */
2661 const char *end_label;
2663 /* The values for the last row of the matrix, as collected in the table.
2664 These are used to minimize the changes to the next row. */
2665 unsigned int file_num;
2666 unsigned int line_num;
2667 unsigned int column_num;
2668 int discrim_num;
2669 bool is_stmt;
2670 bool in_use;
2672 vec<dw_line_info_entry, va_gc> *entries;
2676 /* Each DIE attribute has a field specifying the attribute kind,
2677 a link to the next attribute in the chain, and an attribute value.
2678 Attributes are typically linked below the DIE they modify. */
2680 typedef struct GTY(()) dw_attr_struct {
2681 enum dwarf_attribute dw_attr;
2682 dw_val_node dw_attr_val;
2684 dw_attr_node;
2687 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2688 The children of each node form a circular list linked by
2689 die_sib. die_child points to the node *before* the "first" child node. */
2691 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2692 union die_symbol_or_type_node
2694 const char * GTY ((tag ("0"))) die_symbol;
2695 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2697 GTY ((desc ("%0.comdat_type_p"))) die_id;
2698 vec<dw_attr_node, va_gc> *die_attr;
2699 dw_die_ref die_parent;
2700 dw_die_ref die_child;
2701 dw_die_ref die_sib;
2702 dw_die_ref die_definition; /* ref from a specification to its definition */
2703 dw_offset die_offset;
2704 unsigned long die_abbrev;
2705 int die_mark;
2706 unsigned int decl_id;
2707 enum dwarf_tag die_tag;
2708 /* Die is used and must not be pruned as unused. */
2709 BOOL_BITFIELD die_perennial_p : 1;
2710 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2711 /* Lots of spare bits. */
2713 die_node;
2715 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2716 static bool early_dwarf;
2717 struct set_early_dwarf {
2718 bool saved;
2719 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2720 ~set_early_dwarf () { early_dwarf = saved; }
2723 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2724 #define FOR_EACH_CHILD(die, c, expr) do { \
2725 c = die->die_child; \
2726 if (c) do { \
2727 c = c->die_sib; \
2728 expr; \
2729 } while (c != die->die_child); \
2730 } while (0)
2732 /* The pubname structure */
2734 typedef struct GTY(()) pubname_struct {
2735 dw_die_ref die;
2736 const char *name;
2738 pubname_entry;
2741 struct GTY(()) dw_ranges {
2742 /* If this is positive, it's a block number, otherwise it's a
2743 bitwise-negated index into dw_ranges_by_label. */
2744 int num;
2747 /* A structure to hold a macinfo entry. */
2749 typedef struct GTY(()) macinfo_struct {
2750 unsigned char code;
2751 unsigned HOST_WIDE_INT lineno;
2752 const char *info;
2754 macinfo_entry;
2757 struct GTY(()) dw_ranges_by_label {
2758 const char *begin;
2759 const char *end;
2762 /* The comdat type node structure. */
2763 struct GTY(()) comdat_type_node
2765 dw_die_ref root_die;
2766 dw_die_ref type_die;
2767 dw_die_ref skeleton_die;
2768 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2769 comdat_type_node *next;
2772 /* A list of DIEs for which we can't determine ancestry (parent_die
2773 field) just yet. Later in dwarf2out_finish we will fill in the
2774 missing bits. */
2775 typedef struct GTY(()) limbo_die_struct {
2776 dw_die_ref die;
2777 /* The tree for which this DIE was created. We use this to
2778 determine ancestry later. */
2779 tree created_for;
2780 struct limbo_die_struct *next;
2782 limbo_die_node;
2784 typedef struct skeleton_chain_struct
2786 dw_die_ref old_die;
2787 dw_die_ref new_die;
2788 struct skeleton_chain_struct *parent;
2790 skeleton_chain_node;
2792 /* Define a macro which returns nonzero for a TYPE_DECL which was
2793 implicitly generated for a type.
2795 Note that, unlike the C front-end (which generates a NULL named
2796 TYPE_DECL node for each complete tagged type, each array type,
2797 and each function type node created) the C++ front-end generates
2798 a _named_ TYPE_DECL node for each tagged type node created.
2799 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2800 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2801 front-end, but for each type, tagged or not. */
2803 #define TYPE_DECL_IS_STUB(decl) \
2804 (DECL_NAME (decl) == NULL_TREE \
2805 || (DECL_ARTIFICIAL (decl) \
2806 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2807 /* This is necessary for stub decls that \
2808 appear in nested inline functions. */ \
2809 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2810 && (decl_ultimate_origin (decl) \
2811 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2813 /* Information concerning the compilation unit's programming
2814 language, and compiler version. */
2816 /* Fixed size portion of the DWARF compilation unit header. */
2817 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2818 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2820 /* Fixed size portion of the DWARF comdat type unit header. */
2821 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2822 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2823 + DWARF_OFFSET_SIZE)
2825 /* Fixed size portion of public names info. */
2826 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2828 /* Fixed size portion of the address range info. */
2829 #define DWARF_ARANGES_HEADER_SIZE \
2830 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2831 DWARF2_ADDR_SIZE * 2) \
2832 - DWARF_INITIAL_LENGTH_SIZE)
2834 /* Size of padding portion in the address range info. It must be
2835 aligned to twice the pointer size. */
2836 #define DWARF_ARANGES_PAD_SIZE \
2837 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2838 DWARF2_ADDR_SIZE * 2) \
2839 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2841 /* Use assembler line directives if available. */
2842 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2843 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2844 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2845 #else
2846 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2847 #endif
2848 #endif
2850 /* Minimum line offset in a special line info. opcode.
2851 This value was chosen to give a reasonable range of values. */
2852 #define DWARF_LINE_BASE -10
2854 /* First special line opcode - leave room for the standard opcodes. */
2855 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2857 /* Range of line offsets in a special line info. opcode. */
2858 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2860 /* Flag that indicates the initial value of the is_stmt_start flag.
2861 In the present implementation, we do not mark any lines as
2862 the beginning of a source statement, because that information
2863 is not made available by the GCC front-end. */
2864 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2866 /* Maximum number of operations per instruction bundle. */
2867 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2868 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2869 #endif
2871 /* This location is used by calc_die_sizes() to keep track
2872 the offset of each DIE within the .debug_info section. */
2873 static unsigned long next_die_offset;
2875 /* Record the root of the DIE's built for the current compilation unit. */
2876 static GTY(()) dw_die_ref single_comp_unit_die;
2878 /* A list of type DIEs that have been separated into comdat sections. */
2879 static GTY(()) comdat_type_node *comdat_type_list;
2881 /* A list of DIEs with a NULL parent waiting to be relocated. */
2882 static GTY(()) limbo_die_node *limbo_die_list;
2884 /* A list of DIEs for which we may have to generate
2885 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2886 static GTY(()) limbo_die_node *deferred_asm_name;
2888 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2890 typedef const char *compare_type;
2892 static hashval_t hash (dwarf_file_data *);
2893 static bool equal (dwarf_file_data *, const char *);
2896 /* Filenames referenced by this compilation unit. */
2897 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2899 struct decl_die_hasher : ggc_ptr_hash<die_node>
2901 typedef tree compare_type;
2903 static hashval_t hash (die_node *);
2904 static bool equal (die_node *, tree);
2906 /* A hash table of references to DIE's that describe declarations.
2907 The key is a DECL_UID() which is a unique number identifying each decl. */
2908 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2910 struct block_die_hasher : ggc_ptr_hash<die_struct>
2912 static hashval_t hash (die_struct *);
2913 static bool equal (die_struct *, die_struct *);
2916 /* A hash table of references to DIE's that describe COMMON blocks.
2917 The key is DECL_UID() ^ die_parent. */
2918 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2920 typedef struct GTY(()) die_arg_entry_struct {
2921 dw_die_ref die;
2922 tree arg;
2923 } die_arg_entry;
2926 /* Node of the variable location list. */
2927 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2928 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2929 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2930 in mode of the EXPR_LIST node and first EXPR_LIST operand
2931 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2932 location or NULL for padding. For larger bitsizes,
2933 mode is 0 and first operand is a CONCAT with bitsize
2934 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2935 NULL as second operand. */
2936 rtx GTY (()) loc;
2937 const char * GTY (()) label;
2938 struct var_loc_node * GTY (()) next;
2941 /* Variable location list. */
2942 struct GTY ((for_user)) var_loc_list_def {
2943 struct var_loc_node * GTY (()) first;
2945 /* Pointer to the last but one or last element of the
2946 chained list. If the list is empty, both first and
2947 last are NULL, if the list contains just one node
2948 or the last node certainly is not redundant, it points
2949 to the last node, otherwise points to the last but one.
2950 Do not mark it for GC because it is marked through the chain. */
2951 struct var_loc_node * GTY ((skip ("%h"))) last;
2953 /* Pointer to the last element before section switch,
2954 if NULL, either sections weren't switched or first
2955 is after section switch. */
2956 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2958 /* DECL_UID of the variable decl. */
2959 unsigned int decl_id;
2961 typedef struct var_loc_list_def var_loc_list;
2963 /* Call argument location list. */
2964 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2965 rtx GTY (()) call_arg_loc_note;
2966 const char * GTY (()) label;
2967 tree GTY (()) block;
2968 bool tail_call_p;
2969 rtx GTY (()) symbol_ref;
2970 struct call_arg_loc_node * GTY (()) next;
2974 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2976 typedef const_tree compare_type;
2978 static hashval_t hash (var_loc_list *);
2979 static bool equal (var_loc_list *, const_tree);
2982 /* Table of decl location linked lists. */
2983 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2985 /* Head and tail of call_arg_loc chain. */
2986 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2987 static struct call_arg_loc_node *call_arg_loc_last;
2989 /* Number of call sites in the current function. */
2990 static int call_site_count = -1;
2991 /* Number of tail call sites in the current function. */
2992 static int tail_call_site_count = -1;
2994 /* A cached location list. */
2995 struct GTY ((for_user)) cached_dw_loc_list_def {
2996 /* The DECL_UID of the decl that this entry describes. */
2997 unsigned int decl_id;
2999 /* The cached location list. */
3000 dw_loc_list_ref loc_list;
3002 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3004 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
3007 typedef const_tree compare_type;
3009 static hashval_t hash (cached_dw_loc_list *);
3010 static bool equal (cached_dw_loc_list *, const_tree);
3013 /* Table of cached location lists. */
3014 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3016 /* A pointer to the base of a list of references to DIE's that
3017 are uniquely identified by their tag, presence/absence of
3018 children DIE's, and list of attribute/value pairs. */
3019 static GTY((length ("abbrev_die_table_allocated")))
3020 dw_die_ref *abbrev_die_table;
3022 /* Number of elements currently allocated for abbrev_die_table. */
3023 static GTY(()) unsigned abbrev_die_table_allocated;
3025 /* Number of elements in abbrev_die_table currently in use. */
3026 static GTY(()) unsigned abbrev_die_table_in_use;
3028 /* A hash map to remember the stack usage for DWARF procedures. The value
3029 stored is the stack size difference between before the DWARF procedure
3030 invokation and after it returned. In other words, for a DWARF procedure
3031 that consumes N stack slots and that pushes M ones, this stores M - N. */
3032 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3034 /* Size (in elements) of increments by which we may expand the
3035 abbrev_die_table. */
3036 #define ABBREV_DIE_TABLE_INCREMENT 256
3038 /* A global counter for generating labels for line number data. */
3039 static unsigned int line_info_label_num;
3041 /* The current table to which we should emit line number information
3042 for the current function. This will be set up at the beginning of
3043 assembly for the function. */
3044 static dw_line_info_table *cur_line_info_table;
3046 /* The two default tables of line number info. */
3047 static GTY(()) dw_line_info_table *text_section_line_info;
3048 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3050 /* The set of all non-default tables of line number info. */
3051 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3053 /* A flag to tell pubnames/types export if there is an info section to
3054 refer to. */
3055 static bool info_section_emitted;
3057 /* A pointer to the base of a table that contains a list of publicly
3058 accessible names. */
3059 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3061 /* A pointer to the base of a table that contains a list of publicly
3062 accessible types. */
3063 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3065 /* A pointer to the base of a table that contains a list of macro
3066 defines/undefines (and file start/end markers). */
3067 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3069 /* True if .debug_macinfo or .debug_macros section is going to be
3070 emitted. */
3071 #define have_macinfo \
3072 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3073 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3074 && !macinfo_table->is_empty ())
3076 /* Array of dies for which we should generate .debug_ranges info. */
3077 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3079 /* Number of elements currently allocated for ranges_table. */
3080 static GTY(()) unsigned ranges_table_allocated;
3082 /* Number of elements in ranges_table currently in use. */
3083 static GTY(()) unsigned ranges_table_in_use;
3085 /* Array of pairs of labels referenced in ranges_table. */
3086 static GTY ((length ("ranges_by_label_allocated")))
3087 dw_ranges_by_label *ranges_by_label;
3089 /* Number of elements currently allocated for ranges_by_label. */
3090 static GTY(()) unsigned ranges_by_label_allocated;
3092 /* Number of elements in ranges_by_label currently in use. */
3093 static GTY(()) unsigned ranges_by_label_in_use;
3095 /* Size (in elements) of increments by which we may expand the
3096 ranges_table. */
3097 #define RANGES_TABLE_INCREMENT 64
3099 /* Whether we have location lists that need outputting */
3100 static GTY(()) bool have_location_lists;
3102 /* Unique label counter. */
3103 static GTY(()) unsigned int loclabel_num;
3105 /* Unique label counter for point-of-call tables. */
3106 static GTY(()) unsigned int poc_label_num;
3108 /* The last file entry emitted by maybe_emit_file(). */
3109 static GTY(()) struct dwarf_file_data * last_emitted_file;
3111 /* Number of internal labels generated by gen_internal_sym(). */
3112 static GTY(()) int label_num;
3114 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3116 /* Instances of generic types for which we need to generate debug
3117 info that describe their generic parameters and arguments. That
3118 generation needs to happen once all types are properly laid out so
3119 we do it at the end of compilation. */
3120 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3122 /* Offset from the "steady-state frame pointer" to the frame base,
3123 within the current function. */
3124 static HOST_WIDE_INT frame_pointer_fb_offset;
3125 static bool frame_pointer_fb_offset_valid;
3127 static vec<dw_die_ref> base_types;
3129 /* Flags to represent a set of attribute classes for attributes that represent
3130 a scalar value (bounds, pointers, ...). */
3131 enum dw_scalar_form
3133 dw_scalar_form_constant = 0x01,
3134 dw_scalar_form_exprloc = 0x02,
3135 dw_scalar_form_reference = 0x04
3138 /* Forward declarations for functions defined in this file. */
3140 static int is_pseudo_reg (const_rtx);
3141 static tree type_main_variant (tree);
3142 static int is_tagged_type (const_tree);
3143 static const char *dwarf_tag_name (unsigned);
3144 static const char *dwarf_attr_name (unsigned);
3145 static const char *dwarf_form_name (unsigned);
3146 static tree decl_ultimate_origin (const_tree);
3147 static tree decl_class_context (tree);
3148 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3149 static inline enum dw_val_class AT_class (dw_attr_node *);
3150 static inline unsigned int AT_index (dw_attr_node *);
3151 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3152 static inline unsigned AT_flag (dw_attr_node *);
3153 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3154 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3155 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3156 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3157 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3158 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3159 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3160 unsigned int, unsigned char *);
3161 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3162 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3163 static inline const char *AT_string (dw_attr_node *);
3164 static enum dwarf_form AT_string_form (dw_attr_node *);
3165 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3166 static void add_AT_specification (dw_die_ref, dw_die_ref);
3167 static inline dw_die_ref AT_ref (dw_attr_node *);
3168 static inline int AT_ref_external (dw_attr_node *);
3169 static inline void set_AT_ref_external (dw_attr_node *, int);
3170 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3171 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3172 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3173 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3174 dw_loc_list_ref);
3175 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3176 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3177 static void remove_addr_table_entry (addr_table_entry *);
3178 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3179 static inline rtx AT_addr (dw_attr_node *);
3180 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3181 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3182 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3183 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3184 unsigned HOST_WIDE_INT);
3185 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3186 unsigned long, bool);
3187 static inline const char *AT_lbl (dw_attr_node *);
3188 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3189 static const char *get_AT_low_pc (dw_die_ref);
3190 static const char *get_AT_hi_pc (dw_die_ref);
3191 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3192 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3193 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3194 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3195 static bool is_cxx (void);
3196 static bool is_fortran (void);
3197 static bool is_ada (void);
3198 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3199 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3200 static void add_child_die (dw_die_ref, dw_die_ref);
3201 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3202 static dw_die_ref lookup_type_die (tree);
3203 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3204 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3205 static void equate_type_number_to_die (tree, dw_die_ref);
3206 static dw_die_ref lookup_decl_die (tree);
3207 static var_loc_list *lookup_decl_loc (const_tree);
3208 static void equate_decl_number_to_die (tree, dw_die_ref);
3209 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3210 static void print_spaces (FILE *);
3211 static void print_die (dw_die_ref, FILE *);
3212 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3213 static dw_die_ref pop_compile_unit (dw_die_ref);
3214 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3215 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3216 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3217 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3218 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3219 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3220 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3221 struct md5_ctx *, int *);
3222 struct checksum_attributes;
3223 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3224 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3225 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3226 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3227 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3228 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3229 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3230 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3231 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3232 static void compute_section_prefix (dw_die_ref);
3233 static int is_type_die (dw_die_ref);
3234 static int is_comdat_die (dw_die_ref);
3235 static int is_symbol_die (dw_die_ref);
3236 static inline bool is_template_instantiation (dw_die_ref);
3237 static void assign_symbol_names (dw_die_ref);
3238 static void break_out_includes (dw_die_ref);
3239 static int is_declaration_die (dw_die_ref);
3240 static int should_move_die_to_comdat (dw_die_ref);
3241 static dw_die_ref clone_as_declaration (dw_die_ref);
3242 static dw_die_ref clone_die (dw_die_ref);
3243 static dw_die_ref clone_tree (dw_die_ref);
3244 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3245 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3246 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3247 static dw_die_ref generate_skeleton (dw_die_ref);
3248 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3249 dw_die_ref,
3250 dw_die_ref);
3251 static void break_out_comdat_types (dw_die_ref);
3252 static void copy_decls_for_unworthy_types (dw_die_ref);
3254 static void add_sibling_attributes (dw_die_ref);
3255 static void output_location_lists (dw_die_ref);
3256 static int constant_size (unsigned HOST_WIDE_INT);
3257 static unsigned long size_of_die (dw_die_ref);
3258 static void calc_die_sizes (dw_die_ref);
3259 static void calc_base_type_die_sizes (void);
3260 static void mark_dies (dw_die_ref);
3261 static void unmark_dies (dw_die_ref);
3262 static void unmark_all_dies (dw_die_ref);
3263 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3264 static unsigned long size_of_aranges (void);
3265 static enum dwarf_form value_format (dw_attr_node *);
3266 static void output_value_format (dw_attr_node *);
3267 static void output_abbrev_section (void);
3268 static void output_die_abbrevs (unsigned long, dw_die_ref);
3269 static void output_die_symbol (dw_die_ref);
3270 static void output_die (dw_die_ref);
3271 static void output_compilation_unit_header (void);
3272 static void output_comp_unit (dw_die_ref, int);
3273 static void output_comdat_type_unit (comdat_type_node *);
3274 static const char *dwarf2_name (tree, int);
3275 static void add_pubname (tree, dw_die_ref);
3276 static void add_enumerator_pubname (const char *, dw_die_ref);
3277 static void add_pubname_string (const char *, dw_die_ref);
3278 static void add_pubtype (tree, dw_die_ref);
3279 static void output_pubnames (vec<pubname_entry, va_gc> *);
3280 static void output_aranges (void);
3281 static unsigned int add_ranges_num (int);
3282 static unsigned int add_ranges (const_tree);
3283 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3284 bool *, bool);
3285 static void output_ranges (void);
3286 static dw_line_info_table *new_line_info_table (void);
3287 static void output_line_info (bool);
3288 static void output_file_names (void);
3289 static dw_die_ref base_type_die (tree, bool);
3290 static int is_base_type (tree);
3291 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3292 static int decl_quals (const_tree);
3293 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3294 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3295 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3296 static int type_is_enum (const_tree);
3297 static unsigned int dbx_reg_number (const_rtx);
3298 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3299 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3300 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3301 enum var_init_status);
3302 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3303 enum var_init_status);
3304 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3305 enum var_init_status);
3306 static int is_based_loc (const_rtx);
3307 static bool resolve_one_addr (rtx *);
3308 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3309 enum var_init_status);
3310 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3311 enum var_init_status);
3312 struct loc_descr_context;
3313 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3314 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3315 static dw_loc_list_ref loc_list_from_tree (tree, int,
3316 const struct loc_descr_context *);
3317 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3318 const struct loc_descr_context *);
3319 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3320 static tree field_type (const_tree);
3321 static unsigned int simple_type_align_in_bits (const_tree);
3322 static unsigned int simple_decl_align_in_bits (const_tree);
3323 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3324 struct vlr_context;
3325 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3326 HOST_WIDE_INT *);
3327 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3328 dw_loc_list_ref);
3329 static void add_data_member_location_attribute (dw_die_ref, tree,
3330 struct vlr_context *);
3331 static bool add_const_value_attribute (dw_die_ref, rtx);
3332 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3333 static void insert_wide_int (const wide_int &, unsigned char *, int);
3334 static void insert_float (const_rtx, unsigned char *);
3335 static rtx rtl_for_decl_location (tree);
3336 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3337 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3338 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3339 static void add_name_attribute (dw_die_ref, const char *);
3340 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3341 static void add_comp_dir_attribute (dw_die_ref);
3342 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3343 const struct loc_descr_context *);
3344 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3345 const struct loc_descr_context *);
3346 static void add_subscript_info (dw_die_ref, tree, bool);
3347 static void add_byte_size_attribute (dw_die_ref, tree);
3348 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3349 struct vlr_context *);
3350 static void add_bit_size_attribute (dw_die_ref, tree);
3351 static void add_prototyped_attribute (dw_die_ref, tree);
3352 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3353 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3354 static void add_src_coords_attributes (dw_die_ref, tree);
3355 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3356 static void add_discr_value (dw_die_ref, dw_discr_value *);
3357 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3358 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3359 static void push_decl_scope (tree);
3360 static void pop_decl_scope (void);
3361 static dw_die_ref scope_die_for (tree, dw_die_ref);
3362 static inline int local_scope_p (dw_die_ref);
3363 static inline int class_scope_p (dw_die_ref);
3364 static inline int class_or_namespace_scope_p (dw_die_ref);
3365 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3366 static void add_calling_convention_attribute (dw_die_ref, tree);
3367 static const char *type_tag (const_tree);
3368 static tree member_declared_type (const_tree);
3369 #if 0
3370 static const char *decl_start_label (tree);
3371 #endif
3372 static void gen_array_type_die (tree, dw_die_ref);
3373 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3374 #if 0
3375 static void gen_entry_point_die (tree, dw_die_ref);
3376 #endif
3377 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3378 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3379 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3380 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3381 static void gen_formal_types_die (tree, dw_die_ref);
3382 static void gen_subprogram_die (tree, dw_die_ref);
3383 static void gen_variable_die (tree, tree, dw_die_ref);
3384 static void gen_const_die (tree, dw_die_ref);
3385 static void gen_label_die (tree, dw_die_ref);
3386 static void gen_lexical_block_die (tree, dw_die_ref);
3387 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3388 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3389 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3390 static dw_die_ref gen_compile_unit_die (const char *);
3391 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3392 static void gen_member_die (tree, dw_die_ref);
3393 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3394 enum debug_info_usage);
3395 static void gen_subroutine_type_die (tree, dw_die_ref);
3396 static void gen_typedef_die (tree, dw_die_ref);
3397 static void gen_type_die (tree, dw_die_ref);
3398 static void gen_block_die (tree, dw_die_ref);
3399 static void decls_for_scope (tree, dw_die_ref);
3400 static bool is_naming_typedef_decl (const_tree);
3401 static inline dw_die_ref get_context_die (tree);
3402 static void gen_namespace_die (tree, dw_die_ref);
3403 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3404 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3405 static dw_die_ref force_decl_die (tree);
3406 static dw_die_ref force_type_die (tree);
3407 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3408 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3409 static struct dwarf_file_data * lookup_filename (const char *);
3410 static void retry_incomplete_types (void);
3411 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3412 static void gen_generic_params_dies (tree);
3413 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3414 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3415 static void splice_child_die (dw_die_ref, dw_die_ref);
3416 static int file_info_cmp (const void *, const void *);
3417 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3418 const char *, const char *);
3419 static void output_loc_list (dw_loc_list_ref);
3420 static char *gen_internal_sym (const char *);
3421 static bool want_pubnames (void);
3423 static void prune_unmark_dies (dw_die_ref);
3424 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3425 static void prune_unused_types_mark (dw_die_ref, int);
3426 static void prune_unused_types_walk (dw_die_ref);
3427 static void prune_unused_types_walk_attribs (dw_die_ref);
3428 static void prune_unused_types_prune (dw_die_ref);
3429 static void prune_unused_types (void);
3430 static int maybe_emit_file (struct dwarf_file_data *fd);
3431 static inline const char *AT_vms_delta1 (dw_attr_node *);
3432 static inline const char *AT_vms_delta2 (dw_attr_node *);
3433 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3434 const char *, const char *);
3435 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3436 static void gen_remaining_tmpl_value_param_die_attribute (void);
3437 static bool generic_type_p (tree);
3438 static void schedule_generic_params_dies_gen (tree t);
3439 static void gen_scheduled_generic_parms_dies (void);
3441 static const char *comp_dir_string (void);
3443 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3445 /* enum for tracking thread-local variables whose address is really an offset
3446 relative to the TLS pointer, which will need link-time relocation, but will
3447 not need relocation by the DWARF consumer. */
3449 enum dtprel_bool
3451 dtprel_false = 0,
3452 dtprel_true = 1
3455 /* Return the operator to use for an address of a variable. For dtprel_true, we
3456 use DW_OP_const*. For regular variables, which need both link-time
3457 relocation and consumer-level relocation (e.g., to account for shared objects
3458 loaded at a random address), we use DW_OP_addr*. */
3460 static inline enum dwarf_location_atom
3461 dw_addr_op (enum dtprel_bool dtprel)
3463 if (dtprel == dtprel_true)
3464 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3465 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3466 else
3467 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3470 /* Return a pointer to a newly allocated address location description. If
3471 dwarf_split_debug_info is true, then record the address with the appropriate
3472 relocation. */
3473 static inline dw_loc_descr_ref
3474 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3476 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3478 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3479 ref->dw_loc_oprnd1.v.val_addr = addr;
3480 ref->dtprel = dtprel;
3481 if (dwarf_split_debug_info)
3482 ref->dw_loc_oprnd1.val_entry
3483 = add_addr_table_entry (addr,
3484 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3485 else
3486 ref->dw_loc_oprnd1.val_entry = NULL;
3488 return ref;
3491 /* Section names used to hold DWARF debugging information. */
3493 #ifndef DEBUG_INFO_SECTION
3494 #define DEBUG_INFO_SECTION ".debug_info"
3495 #endif
3496 #ifndef DEBUG_DWO_INFO_SECTION
3497 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3498 #endif
3499 #ifndef DEBUG_ABBREV_SECTION
3500 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3501 #endif
3502 #ifndef DEBUG_DWO_ABBREV_SECTION
3503 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3504 #endif
3505 #ifndef DEBUG_ARANGES_SECTION
3506 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3507 #endif
3508 #ifndef DEBUG_ADDR_SECTION
3509 #define DEBUG_ADDR_SECTION ".debug_addr"
3510 #endif
3511 #ifndef DEBUG_NORM_MACINFO_SECTION
3512 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3513 #endif
3514 #ifndef DEBUG_DWO_MACINFO_SECTION
3515 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3516 #endif
3517 #ifndef DEBUG_MACINFO_SECTION
3518 #define DEBUG_MACINFO_SECTION \
3519 (!dwarf_split_debug_info \
3520 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3521 #endif
3522 #ifndef DEBUG_NORM_MACRO_SECTION
3523 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3524 #endif
3525 #ifndef DEBUG_DWO_MACRO_SECTION
3526 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3527 #endif
3528 #ifndef DEBUG_MACRO_SECTION
3529 #define DEBUG_MACRO_SECTION \
3530 (!dwarf_split_debug_info \
3531 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3532 #endif
3533 #ifndef DEBUG_LINE_SECTION
3534 #define DEBUG_LINE_SECTION ".debug_line"
3535 #endif
3536 #ifndef DEBUG_DWO_LINE_SECTION
3537 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3538 #endif
3539 #ifndef DEBUG_LOC_SECTION
3540 #define DEBUG_LOC_SECTION ".debug_loc"
3541 #endif
3542 #ifndef DEBUG_DWO_LOC_SECTION
3543 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3544 #endif
3545 #ifndef DEBUG_PUBNAMES_SECTION
3546 #define DEBUG_PUBNAMES_SECTION \
3547 ((debug_generate_pub_sections == 2) \
3548 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3549 #endif
3550 #ifndef DEBUG_PUBTYPES_SECTION
3551 #define DEBUG_PUBTYPES_SECTION \
3552 ((debug_generate_pub_sections == 2) \
3553 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3554 #endif
3555 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3556 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3557 #ifndef DEBUG_STR_OFFSETS_SECTION
3558 #define DEBUG_STR_OFFSETS_SECTION \
3559 (!dwarf_split_debug_info \
3560 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3561 #endif
3562 #ifndef DEBUG_STR_DWO_SECTION
3563 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3564 #endif
3565 #ifndef DEBUG_STR_SECTION
3566 #define DEBUG_STR_SECTION ".debug_str"
3567 #endif
3568 #ifndef DEBUG_RANGES_SECTION
3569 #define DEBUG_RANGES_SECTION ".debug_ranges"
3570 #endif
3572 /* Standard ELF section names for compiled code and data. */
3573 #ifndef TEXT_SECTION_NAME
3574 #define TEXT_SECTION_NAME ".text"
3575 #endif
3577 /* Section flags for .debug_macinfo/.debug_macro section. */
3578 #define DEBUG_MACRO_SECTION_FLAGS \
3579 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3581 /* Section flags for .debug_str section. */
3582 #define DEBUG_STR_SECTION_FLAGS \
3583 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3584 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3585 : SECTION_DEBUG)
3587 /* Section flags for .debug_str.dwo section. */
3588 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3590 /* Labels we insert at beginning sections we can reference instead of
3591 the section names themselves. */
3593 #ifndef TEXT_SECTION_LABEL
3594 #define TEXT_SECTION_LABEL "Ltext"
3595 #endif
3596 #ifndef COLD_TEXT_SECTION_LABEL
3597 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3598 #endif
3599 #ifndef DEBUG_LINE_SECTION_LABEL
3600 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3601 #endif
3602 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3603 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3604 #endif
3605 #ifndef DEBUG_INFO_SECTION_LABEL
3606 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3607 #endif
3608 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3609 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3610 #endif
3611 #ifndef DEBUG_ABBREV_SECTION_LABEL
3612 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3613 #endif
3614 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3615 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3616 #endif
3617 #ifndef DEBUG_ADDR_SECTION_LABEL
3618 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3619 #endif
3620 #ifndef DEBUG_LOC_SECTION_LABEL
3621 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3622 #endif
3623 #ifndef DEBUG_RANGES_SECTION_LABEL
3624 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3625 #endif
3626 #ifndef DEBUG_MACINFO_SECTION_LABEL
3627 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3628 #endif
3629 #ifndef DEBUG_MACRO_SECTION_LABEL
3630 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3631 #endif
3632 #define SKELETON_COMP_DIE_ABBREV 1
3633 #define SKELETON_TYPE_DIE_ABBREV 2
3635 /* Definitions of defaults for formats and names of various special
3636 (artificial) labels which may be generated within this file (when the -g
3637 options is used and DWARF2_DEBUGGING_INFO is in effect.
3638 If necessary, these may be overridden from within the tm.h file, but
3639 typically, overriding these defaults is unnecessary. */
3641 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3642 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3643 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3644 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3645 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3646 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3647 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3648 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3649 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3650 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3651 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3652 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3653 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3654 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3656 #ifndef TEXT_END_LABEL
3657 #define TEXT_END_LABEL "Letext"
3658 #endif
3659 #ifndef COLD_END_LABEL
3660 #define COLD_END_LABEL "Letext_cold"
3661 #endif
3662 #ifndef BLOCK_BEGIN_LABEL
3663 #define BLOCK_BEGIN_LABEL "LBB"
3664 #endif
3665 #ifndef BLOCK_END_LABEL
3666 #define BLOCK_END_LABEL "LBE"
3667 #endif
3668 #ifndef LINE_CODE_LABEL
3669 #define LINE_CODE_LABEL "LM"
3670 #endif
3673 /* Return the root of the DIE's built for the current compilation unit. */
3674 static dw_die_ref
3675 comp_unit_die (void)
3677 if (!single_comp_unit_die)
3678 single_comp_unit_die = gen_compile_unit_die (NULL);
3679 return single_comp_unit_die;
3682 /* We allow a language front-end to designate a function that is to be
3683 called to "demangle" any name before it is put into a DIE. */
3685 static const char *(*demangle_name_func) (const char *);
3687 void
3688 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3690 demangle_name_func = func;
3693 /* Test if rtl node points to a pseudo register. */
3695 static inline int
3696 is_pseudo_reg (const_rtx rtl)
3698 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3699 || (GET_CODE (rtl) == SUBREG
3700 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3703 /* Return a reference to a type, with its const and volatile qualifiers
3704 removed. */
3706 static inline tree
3707 type_main_variant (tree type)
3709 type = TYPE_MAIN_VARIANT (type);
3711 /* ??? There really should be only one main variant among any group of
3712 variants of a given type (and all of the MAIN_VARIANT values for all
3713 members of the group should point to that one type) but sometimes the C
3714 front-end messes this up for array types, so we work around that bug
3715 here. */
3716 if (TREE_CODE (type) == ARRAY_TYPE)
3717 while (type != TYPE_MAIN_VARIANT (type))
3718 type = TYPE_MAIN_VARIANT (type);
3720 return type;
3723 /* Return nonzero if the given type node represents a tagged type. */
3725 static inline int
3726 is_tagged_type (const_tree type)
3728 enum tree_code code = TREE_CODE (type);
3730 return (code == RECORD_TYPE || code == UNION_TYPE
3731 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3734 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3736 static void
3737 get_ref_die_offset_label (char *label, dw_die_ref ref)
3739 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3742 /* Return die_offset of a DIE reference to a base type. */
3744 static unsigned long int
3745 get_base_type_offset (dw_die_ref ref)
3747 if (ref->die_offset)
3748 return ref->die_offset;
3749 if (comp_unit_die ()->die_abbrev)
3751 calc_base_type_die_sizes ();
3752 gcc_assert (ref->die_offset);
3754 return ref->die_offset;
3757 /* Return die_offset of a DIE reference other than base type. */
3759 static unsigned long int
3760 get_ref_die_offset (dw_die_ref ref)
3762 gcc_assert (ref->die_offset);
3763 return ref->die_offset;
3766 /* Convert a DIE tag into its string name. */
3768 static const char *
3769 dwarf_tag_name (unsigned int tag)
3771 const char *name = get_DW_TAG_name (tag);
3773 if (name != NULL)
3774 return name;
3776 return "DW_TAG_<unknown>";
3779 /* Convert a DWARF attribute code into its string name. */
3781 static const char *
3782 dwarf_attr_name (unsigned int attr)
3784 const char *name;
3786 switch (attr)
3788 #if VMS_DEBUGGING_INFO
3789 case DW_AT_HP_prologue:
3790 return "DW_AT_HP_prologue";
3791 #else
3792 case DW_AT_MIPS_loop_unroll_factor:
3793 return "DW_AT_MIPS_loop_unroll_factor";
3794 #endif
3796 #if VMS_DEBUGGING_INFO
3797 case DW_AT_HP_epilogue:
3798 return "DW_AT_HP_epilogue";
3799 #else
3800 case DW_AT_MIPS_stride:
3801 return "DW_AT_MIPS_stride";
3802 #endif
3805 name = get_DW_AT_name (attr);
3807 if (name != NULL)
3808 return name;
3810 return "DW_AT_<unknown>";
3813 /* Convert a DWARF value form code into its string name. */
3815 static const char *
3816 dwarf_form_name (unsigned int form)
3818 const char *name = get_DW_FORM_name (form);
3820 if (name != NULL)
3821 return name;
3823 return "DW_FORM_<unknown>";
3826 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3827 instance of an inlined instance of a decl which is local to an inline
3828 function, so we have to trace all of the way back through the origin chain
3829 to find out what sort of node actually served as the original seed for the
3830 given block. */
3832 static tree
3833 decl_ultimate_origin (const_tree decl)
3835 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3836 return NULL_TREE;
3838 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3839 we're trying to output the abstract instance of this function. */
3840 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3841 return NULL_TREE;
3843 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3844 most distant ancestor, this should never happen. */
3845 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3847 return DECL_ABSTRACT_ORIGIN (decl);
3850 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3851 of a virtual function may refer to a base class, so we check the 'this'
3852 parameter. */
3854 static tree
3855 decl_class_context (tree decl)
3857 tree context = NULL_TREE;
3859 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3860 context = DECL_CONTEXT (decl);
3861 else
3862 context = TYPE_MAIN_VARIANT
3863 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3865 if (context && !TYPE_P (context))
3866 context = NULL_TREE;
3868 return context;
3871 /* Add an attribute/value pair to a DIE. */
3873 static inline void
3874 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3876 /* Maybe this should be an assert? */
3877 if (die == NULL)
3878 return;
3880 vec_safe_reserve (die->die_attr, 1);
3881 vec_safe_push (die->die_attr, *attr);
3884 static inline enum dw_val_class
3885 AT_class (dw_attr_node *a)
3887 return a->dw_attr_val.val_class;
3890 /* Return the index for any attribute that will be referenced with a
3891 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3892 are stored in dw_attr_val.v.val_str for reference counting
3893 pruning. */
3895 static inline unsigned int
3896 AT_index (dw_attr_node *a)
3898 if (AT_class (a) == dw_val_class_str)
3899 return a->dw_attr_val.v.val_str->index;
3900 else if (a->dw_attr_val.val_entry != NULL)
3901 return a->dw_attr_val.val_entry->index;
3902 return NOT_INDEXED;
3905 /* Add a flag value attribute to a DIE. */
3907 static inline void
3908 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3910 dw_attr_node attr;
3912 attr.dw_attr = attr_kind;
3913 attr.dw_attr_val.val_class = dw_val_class_flag;
3914 attr.dw_attr_val.val_entry = NULL;
3915 attr.dw_attr_val.v.val_flag = flag;
3916 add_dwarf_attr (die, &attr);
3919 static inline unsigned
3920 AT_flag (dw_attr_node *a)
3922 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3923 return a->dw_attr_val.v.val_flag;
3926 /* Add a signed integer attribute value to a DIE. */
3928 static inline void
3929 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3931 dw_attr_node attr;
3933 attr.dw_attr = attr_kind;
3934 attr.dw_attr_val.val_class = dw_val_class_const;
3935 attr.dw_attr_val.val_entry = NULL;
3936 attr.dw_attr_val.v.val_int = int_val;
3937 add_dwarf_attr (die, &attr);
3940 static inline HOST_WIDE_INT
3941 AT_int (dw_attr_node *a)
3943 gcc_assert (a && AT_class (a) == dw_val_class_const);
3944 return a->dw_attr_val.v.val_int;
3947 /* Add an unsigned integer attribute value to a DIE. */
3949 static inline void
3950 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3951 unsigned HOST_WIDE_INT unsigned_val)
3953 dw_attr_node attr;
3955 attr.dw_attr = attr_kind;
3956 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3957 attr.dw_attr_val.val_entry = NULL;
3958 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3959 add_dwarf_attr (die, &attr);
3962 static inline unsigned HOST_WIDE_INT
3963 AT_unsigned (dw_attr_node *a)
3965 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3966 return a->dw_attr_val.v.val_unsigned;
3969 /* Add an unsigned wide integer attribute value to a DIE. */
3971 static inline void
3972 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3973 const wide_int& w)
3975 dw_attr_node attr;
3977 attr.dw_attr = attr_kind;
3978 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3979 attr.dw_attr_val.val_entry = NULL;
3980 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3981 *attr.dw_attr_val.v.val_wide = w;
3982 add_dwarf_attr (die, &attr);
3985 /* Add an unsigned double integer attribute value to a DIE. */
3987 static inline void
3988 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3989 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3991 dw_attr_node attr;
3993 attr.dw_attr = attr_kind;
3994 attr.dw_attr_val.val_class = dw_val_class_const_double;
3995 attr.dw_attr_val.val_entry = NULL;
3996 attr.dw_attr_val.v.val_double.high = high;
3997 attr.dw_attr_val.v.val_double.low = low;
3998 add_dwarf_attr (die, &attr);
4001 /* Add a floating point attribute value to a DIE and return it. */
4003 static inline void
4004 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4005 unsigned int length, unsigned int elt_size, unsigned char *array)
4007 dw_attr_node attr;
4009 attr.dw_attr = attr_kind;
4010 attr.dw_attr_val.val_class = dw_val_class_vec;
4011 attr.dw_attr_val.val_entry = NULL;
4012 attr.dw_attr_val.v.val_vec.length = length;
4013 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4014 attr.dw_attr_val.v.val_vec.array = array;
4015 add_dwarf_attr (die, &attr);
4018 /* Add an 8-byte data attribute value to a DIE. */
4020 static inline void
4021 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4022 unsigned char data8[8])
4024 dw_attr_node attr;
4026 attr.dw_attr = attr_kind;
4027 attr.dw_attr_val.val_class = dw_val_class_data8;
4028 attr.dw_attr_val.val_entry = NULL;
4029 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4030 add_dwarf_attr (die, &attr);
4033 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4034 dwarf_split_debug_info, address attributes in dies destined for the
4035 final executable have force_direct set to avoid using indexed
4036 references. */
4038 static inline void
4039 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4040 bool force_direct)
4042 dw_attr_node attr;
4043 char * lbl_id;
4045 lbl_id = xstrdup (lbl_low);
4046 attr.dw_attr = DW_AT_low_pc;
4047 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4048 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4049 if (dwarf_split_debug_info && !force_direct)
4050 attr.dw_attr_val.val_entry
4051 = add_addr_table_entry (lbl_id, ate_kind_label);
4052 else
4053 attr.dw_attr_val.val_entry = NULL;
4054 add_dwarf_attr (die, &attr);
4056 attr.dw_attr = DW_AT_high_pc;
4057 if (dwarf_version < 4)
4058 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4059 else
4060 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4061 lbl_id = xstrdup (lbl_high);
4062 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4063 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4064 && dwarf_split_debug_info && !force_direct)
4065 attr.dw_attr_val.val_entry
4066 = add_addr_table_entry (lbl_id, ate_kind_label);
4067 else
4068 attr.dw_attr_val.val_entry = NULL;
4069 add_dwarf_attr (die, &attr);
4072 /* Hash and equality functions for debug_str_hash. */
4074 hashval_t
4075 indirect_string_hasher::hash (indirect_string_node *x)
4077 return htab_hash_string (x->str);
4080 bool
4081 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4083 return strcmp (x1->str, x2) == 0;
4086 /* Add STR to the given string hash table. */
4088 static struct indirect_string_node *
4089 find_AT_string_in_table (const char *str,
4090 hash_table<indirect_string_hasher> *table)
4092 struct indirect_string_node *node;
4094 indirect_string_node **slot
4095 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4096 if (*slot == NULL)
4098 node = ggc_cleared_alloc<indirect_string_node> ();
4099 node->str = ggc_strdup (str);
4100 *slot = node;
4102 else
4103 node = *slot;
4105 node->refcount++;
4106 return node;
4109 /* Add STR to the indirect string hash table. */
4111 static struct indirect_string_node *
4112 find_AT_string (const char *str)
4114 if (! debug_str_hash)
4115 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4117 return find_AT_string_in_table (str, debug_str_hash);
4120 /* Add a string attribute value to a DIE. */
4122 static inline void
4123 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4125 dw_attr_node attr;
4126 struct indirect_string_node *node;
4128 node = find_AT_string (str);
4130 attr.dw_attr = attr_kind;
4131 attr.dw_attr_val.val_class = dw_val_class_str;
4132 attr.dw_attr_val.val_entry = NULL;
4133 attr.dw_attr_val.v.val_str = node;
4134 add_dwarf_attr (die, &attr);
4137 static inline const char *
4138 AT_string (dw_attr_node *a)
4140 gcc_assert (a && AT_class (a) == dw_val_class_str);
4141 return a->dw_attr_val.v.val_str->str;
4144 /* Call this function directly to bypass AT_string_form's logic to put
4145 the string inline in the die. */
4147 static void
4148 set_indirect_string (struct indirect_string_node *node)
4150 char label[32];
4151 /* Already indirect is a no op. */
4152 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4154 gcc_assert (node->label);
4155 return;
4157 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4158 ++dw2_string_counter;
4159 node->label = xstrdup (label);
4161 if (!dwarf_split_debug_info)
4163 node->form = DW_FORM_strp;
4164 node->index = NOT_INDEXED;
4166 else
4168 node->form = DW_FORM_GNU_str_index;
4169 node->index = NO_INDEX_ASSIGNED;
4173 /* Find out whether a string should be output inline in DIE
4174 or out-of-line in .debug_str section. */
4176 static enum dwarf_form
4177 find_string_form (struct indirect_string_node *node)
4179 unsigned int len;
4181 if (node->form)
4182 return node->form;
4184 len = strlen (node->str) + 1;
4186 /* If the string is shorter or equal to the size of the reference, it is
4187 always better to put it inline. */
4188 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4189 return node->form = DW_FORM_string;
4191 /* If we cannot expect the linker to merge strings in .debug_str
4192 section, only put it into .debug_str if it is worth even in this
4193 single module. */
4194 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4195 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4196 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4197 return node->form = DW_FORM_string;
4199 set_indirect_string (node);
4201 return node->form;
4204 /* Find out whether the string referenced from the attribute should be
4205 output inline in DIE or out-of-line in .debug_str section. */
4207 static enum dwarf_form
4208 AT_string_form (dw_attr_node *a)
4210 gcc_assert (a && AT_class (a) == dw_val_class_str);
4211 return find_string_form (a->dw_attr_val.v.val_str);
4214 /* Add a DIE reference attribute value to a DIE. */
4216 static inline void
4217 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4219 dw_attr_node attr;
4220 gcc_checking_assert (targ_die != NULL);
4222 /* With LTO we can end up trying to reference something we didn't create
4223 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4224 if (targ_die == NULL)
4225 return;
4227 attr.dw_attr = attr_kind;
4228 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4229 attr.dw_attr_val.val_entry = NULL;
4230 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4231 attr.dw_attr_val.v.val_die_ref.external = 0;
4232 add_dwarf_attr (die, &attr);
4235 /* Change DIE reference REF to point to NEW_DIE instead. */
4237 static inline void
4238 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4240 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4241 ref->dw_attr_val.v.val_die_ref.die = new_die;
4242 ref->dw_attr_val.v.val_die_ref.external = 0;
4245 /* Add an AT_specification attribute to a DIE, and also make the back
4246 pointer from the specification to the definition. */
4248 static inline void
4249 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4251 add_AT_die_ref (die, DW_AT_specification, targ_die);
4252 gcc_assert (!targ_die->die_definition);
4253 targ_die->die_definition = die;
4256 static inline dw_die_ref
4257 AT_ref (dw_attr_node *a)
4259 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4260 return a->dw_attr_val.v.val_die_ref.die;
4263 static inline int
4264 AT_ref_external (dw_attr_node *a)
4266 if (a && AT_class (a) == dw_val_class_die_ref)
4267 return a->dw_attr_val.v.val_die_ref.external;
4269 return 0;
4272 static inline void
4273 set_AT_ref_external (dw_attr_node *a, int i)
4275 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4276 a->dw_attr_val.v.val_die_ref.external = i;
4279 /* Add an FDE reference attribute value to a DIE. */
4281 static inline void
4282 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4284 dw_attr_node attr;
4286 attr.dw_attr = attr_kind;
4287 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4288 attr.dw_attr_val.val_entry = NULL;
4289 attr.dw_attr_val.v.val_fde_index = targ_fde;
4290 add_dwarf_attr (die, &attr);
4293 /* Add a location description attribute value to a DIE. */
4295 static inline void
4296 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4298 dw_attr_node attr;
4300 attr.dw_attr = attr_kind;
4301 attr.dw_attr_val.val_class = dw_val_class_loc;
4302 attr.dw_attr_val.val_entry = NULL;
4303 attr.dw_attr_val.v.val_loc = loc;
4304 add_dwarf_attr (die, &attr);
4307 static inline dw_loc_descr_ref
4308 AT_loc (dw_attr_node *a)
4310 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4311 return a->dw_attr_val.v.val_loc;
4314 static inline void
4315 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4317 dw_attr_node attr;
4319 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4320 return;
4322 attr.dw_attr = attr_kind;
4323 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4324 attr.dw_attr_val.val_entry = NULL;
4325 attr.dw_attr_val.v.val_loc_list = loc_list;
4326 add_dwarf_attr (die, &attr);
4327 have_location_lists = true;
4330 static inline dw_loc_list_ref
4331 AT_loc_list (dw_attr_node *a)
4333 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4334 return a->dw_attr_val.v.val_loc_list;
4337 static inline dw_loc_list_ref *
4338 AT_loc_list_ptr (dw_attr_node *a)
4340 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4341 return &a->dw_attr_val.v.val_loc_list;
4344 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4346 static hashval_t hash (addr_table_entry *);
4347 static bool equal (addr_table_entry *, addr_table_entry *);
4350 /* Table of entries into the .debug_addr section. */
4352 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4354 /* Hash an address_table_entry. */
4356 hashval_t
4357 addr_hasher::hash (addr_table_entry *a)
4359 inchash::hash hstate;
4360 switch (a->kind)
4362 case ate_kind_rtx:
4363 hstate.add_int (0);
4364 break;
4365 case ate_kind_rtx_dtprel:
4366 hstate.add_int (1);
4367 break;
4368 case ate_kind_label:
4369 return htab_hash_string (a->addr.label);
4370 default:
4371 gcc_unreachable ();
4373 inchash::add_rtx (a->addr.rtl, hstate);
4374 return hstate.end ();
4377 /* Determine equality for two address_table_entries. */
4379 bool
4380 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4382 if (a1->kind != a2->kind)
4383 return 0;
4384 switch (a1->kind)
4386 case ate_kind_rtx:
4387 case ate_kind_rtx_dtprel:
4388 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4389 case ate_kind_label:
4390 return strcmp (a1->addr.label, a2->addr.label) == 0;
4391 default:
4392 gcc_unreachable ();
4396 /* Initialize an addr_table_entry. */
4398 void
4399 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4401 e->kind = kind;
4402 switch (kind)
4404 case ate_kind_rtx:
4405 case ate_kind_rtx_dtprel:
4406 e->addr.rtl = (rtx) addr;
4407 break;
4408 case ate_kind_label:
4409 e->addr.label = (char *) addr;
4410 break;
4412 e->refcount = 0;
4413 e->index = NO_INDEX_ASSIGNED;
4416 /* Add attr to the address table entry to the table. Defer setting an
4417 index until output time. */
4419 static addr_table_entry *
4420 add_addr_table_entry (void *addr, enum ate_kind kind)
4422 addr_table_entry *node;
4423 addr_table_entry finder;
4425 gcc_assert (dwarf_split_debug_info);
4426 if (! addr_index_table)
4427 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4428 init_addr_table_entry (&finder, kind, addr);
4429 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4431 if (*slot == HTAB_EMPTY_ENTRY)
4433 node = ggc_cleared_alloc<addr_table_entry> ();
4434 init_addr_table_entry (node, kind, addr);
4435 *slot = node;
4437 else
4438 node = *slot;
4440 node->refcount++;
4441 return node;
4444 /* Remove an entry from the addr table by decrementing its refcount.
4445 Strictly, decrementing the refcount would be enough, but the
4446 assertion that the entry is actually in the table has found
4447 bugs. */
4449 static void
4450 remove_addr_table_entry (addr_table_entry *entry)
4452 gcc_assert (dwarf_split_debug_info && addr_index_table);
4453 /* After an index is assigned, the table is frozen. */
4454 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4455 entry->refcount--;
4458 /* Given a location list, remove all addresses it refers to from the
4459 address_table. */
4461 static void
4462 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4464 for (; descr; descr = descr->dw_loc_next)
4465 if (descr->dw_loc_oprnd1.val_entry != NULL)
4467 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4468 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4472 /* A helper function for dwarf2out_finish called through
4473 htab_traverse. Assign an addr_table_entry its index. All entries
4474 must be collected into the table when this function is called,
4475 because the indexing code relies on htab_traverse to traverse nodes
4476 in the same order for each run. */
4479 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4481 addr_table_entry *node = *h;
4483 /* Don't index unreferenced nodes. */
4484 if (node->refcount == 0)
4485 return 1;
4487 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4488 node->index = *index;
4489 *index += 1;
4491 return 1;
4494 /* Add an address constant attribute value to a DIE. When using
4495 dwarf_split_debug_info, address attributes in dies destined for the
4496 final executable should be direct references--setting the parameter
4497 force_direct ensures this behavior. */
4499 static inline void
4500 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4501 bool force_direct)
4503 dw_attr_node attr;
4505 attr.dw_attr = attr_kind;
4506 attr.dw_attr_val.val_class = dw_val_class_addr;
4507 attr.dw_attr_val.v.val_addr = addr;
4508 if (dwarf_split_debug_info && !force_direct)
4509 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4510 else
4511 attr.dw_attr_val.val_entry = NULL;
4512 add_dwarf_attr (die, &attr);
4515 /* Get the RTX from to an address DIE attribute. */
4517 static inline rtx
4518 AT_addr (dw_attr_node *a)
4520 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4521 return a->dw_attr_val.v.val_addr;
4524 /* Add a file attribute value to a DIE. */
4526 static inline void
4527 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4528 struct dwarf_file_data *fd)
4530 dw_attr_node attr;
4532 attr.dw_attr = attr_kind;
4533 attr.dw_attr_val.val_class = dw_val_class_file;
4534 attr.dw_attr_val.val_entry = NULL;
4535 attr.dw_attr_val.v.val_file = fd;
4536 add_dwarf_attr (die, &attr);
4539 /* Get the dwarf_file_data from a file DIE attribute. */
4541 static inline struct dwarf_file_data *
4542 AT_file (dw_attr_node *a)
4544 gcc_assert (a && AT_class (a) == dw_val_class_file);
4545 return a->dw_attr_val.v.val_file;
4548 /* Add a vms delta attribute value to a DIE. */
4550 static inline void
4551 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4552 const char *lbl1, const char *lbl2)
4554 dw_attr_node attr;
4556 attr.dw_attr = attr_kind;
4557 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4558 attr.dw_attr_val.val_entry = NULL;
4559 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4560 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4561 add_dwarf_attr (die, &attr);
4564 /* Add a label identifier attribute value to a DIE. */
4566 static inline void
4567 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4568 const char *lbl_id)
4570 dw_attr_node attr;
4572 attr.dw_attr = attr_kind;
4573 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4574 attr.dw_attr_val.val_entry = NULL;
4575 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4576 if (dwarf_split_debug_info)
4577 attr.dw_attr_val.val_entry
4578 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4579 ate_kind_label);
4580 add_dwarf_attr (die, &attr);
4583 /* Add a section offset attribute value to a DIE, an offset into the
4584 debug_line section. */
4586 static inline void
4587 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4588 const char *label)
4590 dw_attr_node attr;
4592 attr.dw_attr = attr_kind;
4593 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4594 attr.dw_attr_val.val_entry = NULL;
4595 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4596 add_dwarf_attr (die, &attr);
4599 /* Add a section offset attribute value to a DIE, an offset into the
4600 debug_macinfo section. */
4602 static inline void
4603 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4604 const char *label)
4606 dw_attr_node attr;
4608 attr.dw_attr = attr_kind;
4609 attr.dw_attr_val.val_class = dw_val_class_macptr;
4610 attr.dw_attr_val.val_entry = NULL;
4611 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4612 add_dwarf_attr (die, &attr);
4615 /* Add an offset attribute value to a DIE. */
4617 static inline void
4618 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4619 unsigned HOST_WIDE_INT offset)
4621 dw_attr_node attr;
4623 attr.dw_attr = attr_kind;
4624 attr.dw_attr_val.val_class = dw_val_class_offset;
4625 attr.dw_attr_val.val_entry = NULL;
4626 attr.dw_attr_val.v.val_offset = offset;
4627 add_dwarf_attr (die, &attr);
4630 /* Add a range_list attribute value to a DIE. When using
4631 dwarf_split_debug_info, address attributes in dies destined for the
4632 final executable should be direct references--setting the parameter
4633 force_direct ensures this behavior. */
4635 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4636 #define RELOCATED_OFFSET (NULL)
4638 static void
4639 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4640 long unsigned int offset, bool force_direct)
4642 dw_attr_node attr;
4644 attr.dw_attr = attr_kind;
4645 attr.dw_attr_val.val_class = dw_val_class_range_list;
4646 /* For the range_list attribute, use val_entry to store whether the
4647 offset should follow split-debug-info or normal semantics. This
4648 value is read in output_range_list_offset. */
4649 if (dwarf_split_debug_info && !force_direct)
4650 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4651 else
4652 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4653 attr.dw_attr_val.v.val_offset = offset;
4654 add_dwarf_attr (die, &attr);
4657 /* Return the start label of a delta attribute. */
4659 static inline const char *
4660 AT_vms_delta1 (dw_attr_node *a)
4662 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4663 return a->dw_attr_val.v.val_vms_delta.lbl1;
4666 /* Return the end label of a delta attribute. */
4668 static inline const char *
4669 AT_vms_delta2 (dw_attr_node *a)
4671 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4672 return a->dw_attr_val.v.val_vms_delta.lbl2;
4675 static inline const char *
4676 AT_lbl (dw_attr_node *a)
4678 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4679 || AT_class (a) == dw_val_class_lineptr
4680 || AT_class (a) == dw_val_class_macptr
4681 || AT_class (a) == dw_val_class_high_pc));
4682 return a->dw_attr_val.v.val_lbl_id;
4685 /* Get the attribute of type attr_kind. */
4687 static dw_attr_node *
4688 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4690 dw_attr_node *a;
4691 unsigned ix;
4692 dw_die_ref spec = NULL;
4694 if (! die)
4695 return NULL;
4697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4698 if (a->dw_attr == attr_kind)
4699 return a;
4700 else if (a->dw_attr == DW_AT_specification
4701 || a->dw_attr == DW_AT_abstract_origin)
4702 spec = AT_ref (a);
4704 if (spec)
4705 return get_AT (spec, attr_kind);
4707 return NULL;
4710 /* Returns the parent of the declaration of DIE. */
4712 static dw_die_ref
4713 get_die_parent (dw_die_ref die)
4715 dw_die_ref t;
4717 if (!die)
4718 return NULL;
4720 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4721 || (t = get_AT_ref (die, DW_AT_specification)))
4722 die = t;
4724 return die->die_parent;
4727 /* Return the "low pc" attribute value, typically associated with a subprogram
4728 DIE. Return null if the "low pc" attribute is either not present, or if it
4729 cannot be represented as an assembler label identifier. */
4731 static inline const char *
4732 get_AT_low_pc (dw_die_ref die)
4734 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4736 return a ? AT_lbl (a) : NULL;
4739 /* Return the "high pc" attribute value, typically associated with a subprogram
4740 DIE. Return null if the "high pc" attribute is either not present, or if it
4741 cannot be represented as an assembler label identifier. */
4743 static inline const char *
4744 get_AT_hi_pc (dw_die_ref die)
4746 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4748 return a ? AT_lbl (a) : NULL;
4751 /* Return the value of the string attribute designated by ATTR_KIND, or
4752 NULL if it is not present. */
4754 static inline const char *
4755 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4757 dw_attr_node *a = get_AT (die, attr_kind);
4759 return a ? AT_string (a) : NULL;
4762 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4763 if it is not present. */
4765 static inline int
4766 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4768 dw_attr_node *a = get_AT (die, attr_kind);
4770 return a ? AT_flag (a) : 0;
4773 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4774 if it is not present. */
4776 static inline unsigned
4777 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4779 dw_attr_node *a = get_AT (die, attr_kind);
4781 return a ? AT_unsigned (a) : 0;
4784 static inline dw_die_ref
4785 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4787 dw_attr_node *a = get_AT (die, attr_kind);
4789 return a ? AT_ref (a) : NULL;
4792 static inline struct dwarf_file_data *
4793 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4795 dw_attr_node *a = get_AT (die, attr_kind);
4797 return a ? AT_file (a) : NULL;
4800 /* Return TRUE if the language is C++. */
4802 static inline bool
4803 is_cxx (void)
4805 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4807 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4808 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4811 /* Return TRUE if the language is Java. */
4813 static inline bool
4814 is_java (void)
4816 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4818 return lang == DW_LANG_Java;
4821 /* Return TRUE if the language is Fortran. */
4823 static inline bool
4824 is_fortran (void)
4826 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4828 return (lang == DW_LANG_Fortran77
4829 || lang == DW_LANG_Fortran90
4830 || lang == DW_LANG_Fortran95
4831 || lang == DW_LANG_Fortran03
4832 || lang == DW_LANG_Fortran08);
4835 /* Return TRUE if the language is Ada. */
4837 static inline bool
4838 is_ada (void)
4840 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4842 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4845 /* Remove the specified attribute if present. Return TRUE if removal
4846 was successful. */
4848 static bool
4849 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4851 dw_attr_node *a;
4852 unsigned ix;
4854 if (! die)
4855 return false;
4857 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4858 if (a->dw_attr == attr_kind)
4860 if (AT_class (a) == dw_val_class_str)
4861 if (a->dw_attr_val.v.val_str->refcount)
4862 a->dw_attr_val.v.val_str->refcount--;
4864 /* vec::ordered_remove should help reduce the number of abbrevs
4865 that are needed. */
4866 die->die_attr->ordered_remove (ix);
4867 return true;
4869 return false;
4872 /* Remove CHILD from its parent. PREV must have the property that
4873 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4875 static void
4876 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4878 gcc_assert (child->die_parent == prev->die_parent);
4879 gcc_assert (prev->die_sib == child);
4880 if (prev == child)
4882 gcc_assert (child->die_parent->die_child == child);
4883 prev = NULL;
4885 else
4886 prev->die_sib = child->die_sib;
4887 if (child->die_parent->die_child == child)
4888 child->die_parent->die_child = prev;
4891 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4892 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4894 static void
4895 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4897 dw_die_ref parent = old_child->die_parent;
4899 gcc_assert (parent == prev->die_parent);
4900 gcc_assert (prev->die_sib == old_child);
4902 new_child->die_parent = parent;
4903 if (prev == old_child)
4905 gcc_assert (parent->die_child == old_child);
4906 new_child->die_sib = new_child;
4908 else
4910 prev->die_sib = new_child;
4911 new_child->die_sib = old_child->die_sib;
4913 if (old_child->die_parent->die_child == old_child)
4914 old_child->die_parent->die_child = new_child;
4917 /* Move all children from OLD_PARENT to NEW_PARENT. */
4919 static void
4920 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4922 dw_die_ref c;
4923 new_parent->die_child = old_parent->die_child;
4924 old_parent->die_child = NULL;
4925 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4928 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4929 matches TAG. */
4931 static void
4932 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4934 dw_die_ref c;
4936 c = die->die_child;
4937 if (c) do {
4938 dw_die_ref prev = c;
4939 c = c->die_sib;
4940 while (c->die_tag == tag)
4942 remove_child_with_prev (c, prev);
4943 c->die_parent = NULL;
4944 /* Might have removed every child. */
4945 if (c == c->die_sib)
4946 return;
4947 c = c->die_sib;
4949 } while (c != die->die_child);
4952 /* Add a CHILD_DIE as the last child of DIE. */
4954 static void
4955 add_child_die (dw_die_ref die, dw_die_ref child_die)
4957 /* FIXME this should probably be an assert. */
4958 if (! die || ! child_die)
4959 return;
4960 gcc_assert (die != child_die);
4962 child_die->die_parent = die;
4963 if (die->die_child)
4965 child_die->die_sib = die->die_child->die_sib;
4966 die->die_child->die_sib = child_die;
4968 else
4969 child_die->die_sib = child_die;
4970 die->die_child = child_die;
4973 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4975 static void
4976 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4977 dw_die_ref after_die)
4979 gcc_assert (die
4980 && child_die
4981 && after_die
4982 && die->die_child
4983 && die != child_die);
4985 child_die->die_parent = die;
4986 child_die->die_sib = after_die->die_sib;
4987 after_die->die_sib = child_die;
4988 if (die->die_child == after_die)
4989 die->die_child = child_die;
4992 /* Unassociate CHILD from its parent, and make its parent be
4993 NEW_PARENT. */
4995 static void
4996 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4998 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4999 if (p->die_sib == child)
5001 remove_child_with_prev (child, p);
5002 break;
5004 add_child_die (new_parent, child);
5007 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5008 is the specification, to the end of PARENT's list of children.
5009 This is done by removing and re-adding it. */
5011 static void
5012 splice_child_die (dw_die_ref parent, dw_die_ref child)
5014 /* We want the declaration DIE from inside the class, not the
5015 specification DIE at toplevel. */
5016 if (child->die_parent != parent)
5018 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5020 if (tmp)
5021 child = tmp;
5024 gcc_assert (child->die_parent == parent
5025 || (child->die_parent
5026 == get_AT_ref (parent, DW_AT_specification)));
5028 reparent_child (child, parent);
5031 /* Create and return a new die with a parent of PARENT_DIE. If
5032 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5033 associated tree T must be supplied to determine parenthood
5034 later. */
5036 static inline dw_die_ref
5037 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5039 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5041 die->die_tag = tag_value;
5043 if (parent_die != NULL)
5044 add_child_die (parent_die, die);
5045 else
5047 limbo_die_node *limbo_node;
5049 /* No DIEs created after early dwarf should end up in limbo,
5050 because the limbo list should not persist past LTO
5051 streaming. */
5052 if (tag_value != DW_TAG_compile_unit
5053 /* These are allowed because they're generated while
5054 breaking out COMDAT units late. */
5055 && tag_value != DW_TAG_type_unit
5056 && !early_dwarf
5057 /* Allow nested functions to live in limbo because they will
5058 only temporarily live there, as decls_for_scope will fix
5059 them up. */
5060 && (TREE_CODE (t) != FUNCTION_DECL
5061 || !decl_function_context (t))
5062 /* Same as nested functions above but for types. Types that
5063 are local to a function will be fixed in
5064 decls_for_scope. */
5065 && (!RECORD_OR_UNION_TYPE_P (t)
5066 || !TYPE_CONTEXT (t)
5067 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5068 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5069 especially in the ltrans stage, but once we implement LTO
5070 dwarf streaming, we should remove this exception. */
5071 && !in_lto_p)
5073 fprintf (stderr, "symbol ended up in limbo too late:");
5074 debug_generic_stmt (t);
5075 gcc_unreachable ();
5078 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5079 limbo_node->die = die;
5080 limbo_node->created_for = t;
5081 limbo_node->next = limbo_die_list;
5082 limbo_die_list = limbo_node;
5085 return die;
5088 /* Return the DIE associated with the given type specifier. */
5090 static inline dw_die_ref
5091 lookup_type_die (tree type)
5093 return TYPE_SYMTAB_DIE (type);
5096 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5097 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5098 anonymous type instead the one of the naming typedef. */
5100 static inline dw_die_ref
5101 strip_naming_typedef (tree type, dw_die_ref type_die)
5103 if (type
5104 && TREE_CODE (type) == RECORD_TYPE
5105 && type_die
5106 && type_die->die_tag == DW_TAG_typedef
5107 && is_naming_typedef_decl (TYPE_NAME (type)))
5108 type_die = get_AT_ref (type_die, DW_AT_type);
5109 return type_die;
5112 /* Like lookup_type_die, but if type is an anonymous type named by a
5113 typedef[1], return the DIE of the anonymous type instead the one of
5114 the naming typedef. This is because in gen_typedef_die, we did
5115 equate the anonymous struct named by the typedef with the DIE of
5116 the naming typedef. So by default, lookup_type_die on an anonymous
5117 struct yields the DIE of the naming typedef.
5119 [1]: Read the comment of is_naming_typedef_decl to learn about what
5120 a naming typedef is. */
5122 static inline dw_die_ref
5123 lookup_type_die_strip_naming_typedef (tree type)
5125 dw_die_ref die = lookup_type_die (type);
5126 return strip_naming_typedef (type, die);
5129 /* Equate a DIE to a given type specifier. */
5131 static inline void
5132 equate_type_number_to_die (tree type, dw_die_ref type_die)
5134 TYPE_SYMTAB_DIE (type) = type_die;
5137 /* Returns a hash value for X (which really is a die_struct). */
5139 inline hashval_t
5140 decl_die_hasher::hash (die_node *x)
5142 return (hashval_t) x->decl_id;
5145 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5147 inline bool
5148 decl_die_hasher::equal (die_node *x, tree y)
5150 return (x->decl_id == DECL_UID (y));
5153 /* Return the DIE associated with a given declaration. */
5155 static inline dw_die_ref
5156 lookup_decl_die (tree decl)
5158 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5161 /* Returns a hash value for X (which really is a var_loc_list). */
5163 inline hashval_t
5164 decl_loc_hasher::hash (var_loc_list *x)
5166 return (hashval_t) x->decl_id;
5169 /* Return nonzero if decl_id of var_loc_list X is the same as
5170 UID of decl *Y. */
5172 inline bool
5173 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5175 return (x->decl_id == DECL_UID (y));
5178 /* Return the var_loc list associated with a given declaration. */
5180 static inline var_loc_list *
5181 lookup_decl_loc (const_tree decl)
5183 if (!decl_loc_table)
5184 return NULL;
5185 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5188 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5190 inline hashval_t
5191 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5193 return (hashval_t) x->decl_id;
5196 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5197 UID of decl *Y. */
5199 inline bool
5200 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5202 return (x->decl_id == DECL_UID (y));
5205 /* Equate a DIE to a particular declaration. */
5207 static void
5208 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5210 unsigned int decl_id = DECL_UID (decl);
5212 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5213 decl_die->decl_id = decl_id;
5216 /* Return how many bits covers PIECE EXPR_LIST. */
5218 static HOST_WIDE_INT
5219 decl_piece_bitsize (rtx piece)
5221 int ret = (int) GET_MODE (piece);
5222 if (ret)
5223 return ret;
5224 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5225 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5226 return INTVAL (XEXP (XEXP (piece, 0), 0));
5229 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5231 static rtx *
5232 decl_piece_varloc_ptr (rtx piece)
5234 if ((int) GET_MODE (piece))
5235 return &XEXP (piece, 0);
5236 else
5237 return &XEXP (XEXP (piece, 0), 1);
5240 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5241 Next is the chain of following piece nodes. */
5243 static rtx_expr_list *
5244 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5246 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5247 return alloc_EXPR_LIST (bitsize, loc_note, next);
5248 else
5249 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5250 GEN_INT (bitsize),
5251 loc_note), next);
5254 /* Return rtx that should be stored into loc field for
5255 LOC_NOTE and BITPOS/BITSIZE. */
5257 static rtx
5258 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5259 HOST_WIDE_INT bitsize)
5261 if (bitsize != -1)
5263 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5264 if (bitpos != 0)
5265 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5267 return loc_note;
5270 /* This function either modifies location piece list *DEST in
5271 place (if SRC and INNER is NULL), or copies location piece list
5272 *SRC to *DEST while modifying it. Location BITPOS is modified
5273 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5274 not copied and if needed some padding around it is added.
5275 When modifying in place, DEST should point to EXPR_LIST where
5276 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5277 to the start of the whole list and INNER points to the EXPR_LIST
5278 where earlier pieces cover PIECE_BITPOS bits. */
5280 static void
5281 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5282 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5283 HOST_WIDE_INT bitsize, rtx loc_note)
5285 HOST_WIDE_INT diff;
5286 bool copy = inner != NULL;
5288 if (copy)
5290 /* First copy all nodes preceding the current bitpos. */
5291 while (src != inner)
5293 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5294 decl_piece_bitsize (*src), NULL_RTX);
5295 dest = &XEXP (*dest, 1);
5296 src = &XEXP (*src, 1);
5299 /* Add padding if needed. */
5300 if (bitpos != piece_bitpos)
5302 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5303 copy ? NULL_RTX : *dest);
5304 dest = &XEXP (*dest, 1);
5306 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5308 gcc_assert (!copy);
5309 /* A piece with correct bitpos and bitsize already exist,
5310 just update the location for it and return. */
5311 *decl_piece_varloc_ptr (*dest) = loc_note;
5312 return;
5314 /* Add the piece that changed. */
5315 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5316 dest = &XEXP (*dest, 1);
5317 /* Skip over pieces that overlap it. */
5318 diff = bitpos - piece_bitpos + bitsize;
5319 if (!copy)
5320 src = dest;
5321 while (diff > 0 && *src)
5323 rtx piece = *src;
5324 diff -= decl_piece_bitsize (piece);
5325 if (copy)
5326 src = &XEXP (piece, 1);
5327 else
5329 *src = XEXP (piece, 1);
5330 free_EXPR_LIST_node (piece);
5333 /* Add padding if needed. */
5334 if (diff < 0 && *src)
5336 if (!copy)
5337 dest = src;
5338 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5339 dest = &XEXP (*dest, 1);
5341 if (!copy)
5342 return;
5343 /* Finally copy all nodes following it. */
5344 while (*src)
5346 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5347 decl_piece_bitsize (*src), NULL_RTX);
5348 dest = &XEXP (*dest, 1);
5349 src = &XEXP (*src, 1);
5353 /* Add a variable location node to the linked list for DECL. */
5355 static struct var_loc_node *
5356 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5358 unsigned int decl_id;
5359 var_loc_list *temp;
5360 struct var_loc_node *loc = NULL;
5361 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5363 if (TREE_CODE (decl) == VAR_DECL
5364 && DECL_HAS_DEBUG_EXPR_P (decl))
5366 tree realdecl = DECL_DEBUG_EXPR (decl);
5367 if (handled_component_p (realdecl)
5368 || (TREE_CODE (realdecl) == MEM_REF
5369 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5371 HOST_WIDE_INT maxsize;
5372 bool reverse;
5373 tree innerdecl
5374 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5375 &reverse);
5376 if (!DECL_P (innerdecl)
5377 || DECL_IGNORED_P (innerdecl)
5378 || TREE_STATIC (innerdecl)
5379 || bitsize <= 0
5380 || bitpos + bitsize > 256
5381 || bitsize != maxsize)
5382 return NULL;
5383 decl = innerdecl;
5387 decl_id = DECL_UID (decl);
5388 var_loc_list **slot
5389 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5390 if (*slot == NULL)
5392 temp = ggc_cleared_alloc<var_loc_list> ();
5393 temp->decl_id = decl_id;
5394 *slot = temp;
5396 else
5397 temp = *slot;
5399 /* For PARM_DECLs try to keep around the original incoming value,
5400 even if that means we'll emit a zero-range .debug_loc entry. */
5401 if (temp->last
5402 && temp->first == temp->last
5403 && TREE_CODE (decl) == PARM_DECL
5404 && NOTE_P (temp->first->loc)
5405 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5406 && DECL_INCOMING_RTL (decl)
5407 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5408 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5409 == GET_CODE (DECL_INCOMING_RTL (decl))
5410 && prev_real_insn (temp->first->loc) == NULL_RTX
5411 && (bitsize != -1
5412 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5413 NOTE_VAR_LOCATION_LOC (loc_note))
5414 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5415 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5417 loc = ggc_cleared_alloc<var_loc_node> ();
5418 temp->first->next = loc;
5419 temp->last = loc;
5420 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5422 else if (temp->last)
5424 struct var_loc_node *last = temp->last, *unused = NULL;
5425 rtx *piece_loc = NULL, last_loc_note;
5426 HOST_WIDE_INT piece_bitpos = 0;
5427 if (last->next)
5429 last = last->next;
5430 gcc_assert (last->next == NULL);
5432 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5434 piece_loc = &last->loc;
5437 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5438 if (piece_bitpos + cur_bitsize > bitpos)
5439 break;
5440 piece_bitpos += cur_bitsize;
5441 piece_loc = &XEXP (*piece_loc, 1);
5443 while (*piece_loc);
5445 /* TEMP->LAST here is either pointer to the last but one or
5446 last element in the chained list, LAST is pointer to the
5447 last element. */
5448 if (label && strcmp (last->label, label) == 0)
5450 /* For SRA optimized variables if there weren't any real
5451 insns since last note, just modify the last node. */
5452 if (piece_loc != NULL)
5454 adjust_piece_list (piece_loc, NULL, NULL,
5455 bitpos, piece_bitpos, bitsize, loc_note);
5456 return NULL;
5458 /* If the last note doesn't cover any instructions, remove it. */
5459 if (temp->last != last)
5461 temp->last->next = NULL;
5462 unused = last;
5463 last = temp->last;
5464 gcc_assert (strcmp (last->label, label) != 0);
5466 else
5468 gcc_assert (temp->first == temp->last
5469 || (temp->first->next == temp->last
5470 && TREE_CODE (decl) == PARM_DECL));
5471 memset (temp->last, '\0', sizeof (*temp->last));
5472 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5473 return temp->last;
5476 if (bitsize == -1 && NOTE_P (last->loc))
5477 last_loc_note = last->loc;
5478 else if (piece_loc != NULL
5479 && *piece_loc != NULL_RTX
5480 && piece_bitpos == bitpos
5481 && decl_piece_bitsize (*piece_loc) == bitsize)
5482 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5483 else
5484 last_loc_note = NULL_RTX;
5485 /* If the current location is the same as the end of the list,
5486 and either both or neither of the locations is uninitialized,
5487 we have nothing to do. */
5488 if (last_loc_note == NULL_RTX
5489 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5490 NOTE_VAR_LOCATION_LOC (loc_note)))
5491 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5492 != NOTE_VAR_LOCATION_STATUS (loc_note))
5493 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5494 == VAR_INIT_STATUS_UNINITIALIZED)
5495 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5496 == VAR_INIT_STATUS_UNINITIALIZED))))
5498 /* Add LOC to the end of list and update LAST. If the last
5499 element of the list has been removed above, reuse its
5500 memory for the new node, otherwise allocate a new one. */
5501 if (unused)
5503 loc = unused;
5504 memset (loc, '\0', sizeof (*loc));
5506 else
5507 loc = ggc_cleared_alloc<var_loc_node> ();
5508 if (bitsize == -1 || piece_loc == NULL)
5509 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5510 else
5511 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5512 bitpos, piece_bitpos, bitsize, loc_note);
5513 last->next = loc;
5514 /* Ensure TEMP->LAST will point either to the new last but one
5515 element of the chain, or to the last element in it. */
5516 if (last != temp->last)
5517 temp->last = last;
5519 else if (unused)
5520 ggc_free (unused);
5522 else
5524 loc = ggc_cleared_alloc<var_loc_node> ();
5525 temp->first = loc;
5526 temp->last = loc;
5527 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5529 return loc;
5532 /* Keep track of the number of spaces used to indent the
5533 output of the debugging routines that print the structure of
5534 the DIE internal representation. */
5535 static int print_indent;
5537 /* Indent the line the number of spaces given by print_indent. */
5539 static inline void
5540 print_spaces (FILE *outfile)
5542 fprintf (outfile, "%*s", print_indent, "");
5545 /* Print a type signature in hex. */
5547 static inline void
5548 print_signature (FILE *outfile, char *sig)
5550 int i;
5552 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5553 fprintf (outfile, "%02x", sig[i] & 0xff);
5556 static inline void
5557 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5559 if (discr_value->pos)
5560 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5561 else
5562 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5565 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5567 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5568 RECURSE, output location descriptor operations. */
5570 static void
5571 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5573 switch (val->val_class)
5575 case dw_val_class_addr:
5576 fprintf (outfile, "address");
5577 break;
5578 case dw_val_class_offset:
5579 fprintf (outfile, "offset");
5580 break;
5581 case dw_val_class_loc:
5582 fprintf (outfile, "location descriptor");
5583 if (val->v.val_loc == NULL)
5584 fprintf (outfile, " -> <null>\n");
5585 else if (recurse)
5587 fprintf (outfile, ":\n");
5588 print_indent += 4;
5589 print_loc_descr (val->v.val_loc, outfile);
5590 print_indent -= 4;
5592 else
5593 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5594 break;
5595 case dw_val_class_loc_list:
5596 fprintf (outfile, "location list -> label:%s",
5597 val->v.val_loc_list->ll_symbol);
5598 break;
5599 case dw_val_class_range_list:
5600 fprintf (outfile, "range list");
5601 break;
5602 case dw_val_class_const:
5603 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5604 break;
5605 case dw_val_class_unsigned_const:
5606 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5607 break;
5608 case dw_val_class_const_double:
5609 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5610 HOST_WIDE_INT_PRINT_UNSIGNED")",
5611 val->v.val_double.high,
5612 val->v.val_double.low);
5613 break;
5614 case dw_val_class_wide_int:
5616 int i = val->v.val_wide->get_len ();
5617 fprintf (outfile, "constant (");
5618 gcc_assert (i > 0);
5619 if (val->v.val_wide->elt (i - 1) == 0)
5620 fprintf (outfile, "0x");
5621 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5622 val->v.val_wide->elt (--i));
5623 while (--i >= 0)
5624 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5625 val->v.val_wide->elt (i));
5626 fprintf (outfile, ")");
5627 break;
5629 case dw_val_class_vec:
5630 fprintf (outfile, "floating-point or vector constant");
5631 break;
5632 case dw_val_class_flag:
5633 fprintf (outfile, "%u", val->v.val_flag);
5634 break;
5635 case dw_val_class_die_ref:
5636 if (val->v.val_die_ref.die != NULL)
5638 dw_die_ref die = val->v.val_die_ref.die;
5640 if (die->comdat_type_p)
5642 fprintf (outfile, "die -> signature: ");
5643 print_signature (outfile,
5644 die->die_id.die_type_node->signature);
5646 else if (die->die_id.die_symbol)
5647 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5648 else
5649 fprintf (outfile, "die -> %ld", die->die_offset);
5650 fprintf (outfile, " (%p)", (void *) die);
5652 else
5653 fprintf (outfile, "die -> <null>");
5654 break;
5655 case dw_val_class_vms_delta:
5656 fprintf (outfile, "delta: @slotcount(%s-%s)",
5657 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5658 break;
5659 case dw_val_class_lbl_id:
5660 case dw_val_class_lineptr:
5661 case dw_val_class_macptr:
5662 case dw_val_class_high_pc:
5663 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5664 break;
5665 case dw_val_class_str:
5666 if (val->v.val_str->str != NULL)
5667 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5668 else
5669 fprintf (outfile, "<null>");
5670 break;
5671 case dw_val_class_file:
5672 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5673 val->v.val_file->emitted_number);
5674 break;
5675 case dw_val_class_data8:
5677 int i;
5679 for (i = 0; i < 8; i++)
5680 fprintf (outfile, "%02x", val->v.val_data8[i]);
5681 break;
5683 case dw_val_class_discr_value:
5684 print_discr_value (outfile, &val->v.val_discr_value);
5685 break;
5686 case dw_val_class_discr_list:
5687 for (dw_discr_list_ref node = val->v.val_discr_list;
5688 node != NULL;
5689 node = node->dw_discr_next)
5691 if (node->dw_discr_range)
5693 fprintf (outfile, " .. ");
5694 print_discr_value (outfile, &node->dw_discr_lower_bound);
5695 print_discr_value (outfile, &node->dw_discr_upper_bound);
5697 else
5698 print_discr_value (outfile, &node->dw_discr_lower_bound);
5700 if (node->dw_discr_next != NULL)
5701 fprintf (outfile, " | ");
5703 default:
5704 break;
5708 /* Likewise, for a DIE attribute. */
5710 static void
5711 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5713 print_dw_val (&a->dw_attr_val, recurse, outfile);
5717 /* Print the list of operands in the LOC location description to OUTFILE. This
5718 routine is a debugging aid only. */
5720 static void
5721 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5723 dw_loc_descr_ref l = loc;
5725 if (loc == NULL)
5727 print_spaces (outfile);
5728 fprintf (outfile, "<null>\n");
5729 return;
5732 for (l = loc; l != NULL; l = l->dw_loc_next)
5734 print_spaces (outfile);
5735 fprintf (outfile, "(%p) %s",
5736 (void *) l,
5737 dwarf_stack_op_name (l->dw_loc_opc));
5738 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5740 fprintf (outfile, " ");
5741 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5743 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5745 fprintf (outfile, ", ");
5746 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5748 fprintf (outfile, "\n");
5752 /* Print the information associated with a given DIE, and its children.
5753 This routine is a debugging aid only. */
5755 static void
5756 print_die (dw_die_ref die, FILE *outfile)
5758 dw_attr_node *a;
5759 dw_die_ref c;
5760 unsigned ix;
5762 print_spaces (outfile);
5763 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5764 die->die_offset, dwarf_tag_name (die->die_tag),
5765 (void*) die);
5766 print_spaces (outfile);
5767 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5768 fprintf (outfile, " offset: %ld", die->die_offset);
5769 fprintf (outfile, " mark: %d\n", die->die_mark);
5771 if (die->comdat_type_p)
5773 print_spaces (outfile);
5774 fprintf (outfile, " signature: ");
5775 print_signature (outfile, die->die_id.die_type_node->signature);
5776 fprintf (outfile, "\n");
5779 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5781 print_spaces (outfile);
5782 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5784 print_attribute (a, true, outfile);
5785 fprintf (outfile, "\n");
5788 if (die->die_child != NULL)
5790 print_indent += 4;
5791 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5792 print_indent -= 4;
5794 if (print_indent == 0)
5795 fprintf (outfile, "\n");
5798 /* Print the list of operations in the LOC location description. */
5800 DEBUG_FUNCTION void
5801 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5803 print_loc_descr (loc, stderr);
5806 /* Print the information collected for a given DIE. */
5808 DEBUG_FUNCTION void
5809 debug_dwarf_die (dw_die_ref die)
5811 print_die (die, stderr);
5814 DEBUG_FUNCTION void
5815 debug (die_struct &ref)
5817 print_die (&ref, stderr);
5820 DEBUG_FUNCTION void
5821 debug (die_struct *ptr)
5823 if (ptr)
5824 debug (*ptr);
5825 else
5826 fprintf (stderr, "<nil>\n");
5830 /* Print all DWARF information collected for the compilation unit.
5831 This routine is a debugging aid only. */
5833 DEBUG_FUNCTION void
5834 debug_dwarf (void)
5836 print_indent = 0;
5837 print_die (comp_unit_die (), stderr);
5840 /* Sanity checks on DIEs. */
5842 static void
5843 check_die (dw_die_ref die)
5845 unsigned ix;
5846 dw_attr_node *a;
5847 bool inline_found = false;
5848 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5849 int n_decl_line = 0, n_decl_file = 0;
5850 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5852 switch (a->dw_attr)
5854 case DW_AT_inline:
5855 if (a->dw_attr_val.v.val_unsigned)
5856 inline_found = true;
5857 break;
5858 case DW_AT_location:
5859 ++n_location;
5860 break;
5861 case DW_AT_low_pc:
5862 ++n_low_pc;
5863 break;
5864 case DW_AT_high_pc:
5865 ++n_high_pc;
5866 break;
5867 case DW_AT_artificial:
5868 ++n_artificial;
5869 break;
5870 case DW_AT_decl_line:
5871 ++n_decl_line;
5872 break;
5873 case DW_AT_decl_file:
5874 ++n_decl_file;
5875 break;
5876 default:
5877 break;
5880 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5881 || n_decl_line > 1 || n_decl_file > 1)
5883 fprintf (stderr, "Duplicate attributes in DIE:\n");
5884 debug_dwarf_die (die);
5885 gcc_unreachable ();
5887 if (inline_found)
5889 /* A debugging information entry that is a member of an abstract
5890 instance tree [that has DW_AT_inline] should not contain any
5891 attributes which describe aspects of the subroutine which vary
5892 between distinct inlined expansions or distinct out-of-line
5893 expansions. */
5894 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5895 gcc_assert (a->dw_attr != DW_AT_low_pc
5896 && a->dw_attr != DW_AT_high_pc
5897 && a->dw_attr != DW_AT_location
5898 && a->dw_attr != DW_AT_frame_base
5899 && a->dw_attr != DW_AT_GNU_all_call_sites);
5903 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5904 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5905 DIE that marks the start of the DIEs for this include file. */
5907 static dw_die_ref
5908 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5910 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5911 dw_die_ref new_unit = gen_compile_unit_die (filename);
5913 new_unit->die_sib = old_unit;
5914 return new_unit;
5917 /* Close an include-file CU and reopen the enclosing one. */
5919 static dw_die_ref
5920 pop_compile_unit (dw_die_ref old_unit)
5922 dw_die_ref new_unit = old_unit->die_sib;
5924 old_unit->die_sib = NULL;
5925 return new_unit;
5928 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5929 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5930 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5932 /* Calculate the checksum of a location expression. */
5934 static inline void
5935 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5937 int tem;
5938 inchash::hash hstate;
5939 hashval_t hash;
5941 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5942 CHECKSUM (tem);
5943 hash_loc_operands (loc, hstate);
5944 hash = hstate.end();
5945 CHECKSUM (hash);
5948 /* Calculate the checksum of an attribute. */
5950 static void
5951 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5953 dw_loc_descr_ref loc;
5954 rtx r;
5956 CHECKSUM (at->dw_attr);
5958 /* We don't care that this was compiled with a different compiler
5959 snapshot; if the output is the same, that's what matters. */
5960 if (at->dw_attr == DW_AT_producer)
5961 return;
5963 switch (AT_class (at))
5965 case dw_val_class_const:
5966 CHECKSUM (at->dw_attr_val.v.val_int);
5967 break;
5968 case dw_val_class_unsigned_const:
5969 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5970 break;
5971 case dw_val_class_const_double:
5972 CHECKSUM (at->dw_attr_val.v.val_double);
5973 break;
5974 case dw_val_class_wide_int:
5975 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5976 get_full_len (*at->dw_attr_val.v.val_wide)
5977 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5978 break;
5979 case dw_val_class_vec:
5980 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5981 (at->dw_attr_val.v.val_vec.length
5982 * at->dw_attr_val.v.val_vec.elt_size));
5983 break;
5984 case dw_val_class_flag:
5985 CHECKSUM (at->dw_attr_val.v.val_flag);
5986 break;
5987 case dw_val_class_str:
5988 CHECKSUM_STRING (AT_string (at));
5989 break;
5991 case dw_val_class_addr:
5992 r = AT_addr (at);
5993 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5994 CHECKSUM_STRING (XSTR (r, 0));
5995 break;
5997 case dw_val_class_offset:
5998 CHECKSUM (at->dw_attr_val.v.val_offset);
5999 break;
6001 case dw_val_class_loc:
6002 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6003 loc_checksum (loc, ctx);
6004 break;
6006 case dw_val_class_die_ref:
6007 die_checksum (AT_ref (at), ctx, mark);
6008 break;
6010 case dw_val_class_fde_ref:
6011 case dw_val_class_vms_delta:
6012 case dw_val_class_lbl_id:
6013 case dw_val_class_lineptr:
6014 case dw_val_class_macptr:
6015 case dw_val_class_high_pc:
6016 break;
6018 case dw_val_class_file:
6019 CHECKSUM_STRING (AT_file (at)->filename);
6020 break;
6022 case dw_val_class_data8:
6023 CHECKSUM (at->dw_attr_val.v.val_data8);
6024 break;
6026 default:
6027 break;
6031 /* Calculate the checksum of a DIE. */
6033 static void
6034 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6036 dw_die_ref c;
6037 dw_attr_node *a;
6038 unsigned ix;
6040 /* To avoid infinite recursion. */
6041 if (die->die_mark)
6043 CHECKSUM (die->die_mark);
6044 return;
6046 die->die_mark = ++(*mark);
6048 CHECKSUM (die->die_tag);
6050 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6051 attr_checksum (a, ctx, mark);
6053 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6056 #undef CHECKSUM
6057 #undef CHECKSUM_BLOCK
6058 #undef CHECKSUM_STRING
6060 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6061 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6062 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6063 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6064 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6065 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6066 #define CHECKSUM_ATTR(FOO) \
6067 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6069 /* Calculate the checksum of a number in signed LEB128 format. */
6071 static void
6072 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6074 unsigned char byte;
6075 bool more;
6077 while (1)
6079 byte = (value & 0x7f);
6080 value >>= 7;
6081 more = !((value == 0 && (byte & 0x40) == 0)
6082 || (value == -1 && (byte & 0x40) != 0));
6083 if (more)
6084 byte |= 0x80;
6085 CHECKSUM (byte);
6086 if (!more)
6087 break;
6091 /* Calculate the checksum of a number in unsigned LEB128 format. */
6093 static void
6094 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6096 while (1)
6098 unsigned char byte = (value & 0x7f);
6099 value >>= 7;
6100 if (value != 0)
6101 /* More bytes to follow. */
6102 byte |= 0x80;
6103 CHECKSUM (byte);
6104 if (value == 0)
6105 break;
6109 /* Checksum the context of the DIE. This adds the names of any
6110 surrounding namespaces or structures to the checksum. */
6112 static void
6113 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6115 const char *name;
6116 dw_die_ref spec;
6117 int tag = die->die_tag;
6119 if (tag != DW_TAG_namespace
6120 && tag != DW_TAG_structure_type
6121 && tag != DW_TAG_class_type)
6122 return;
6124 name = get_AT_string (die, DW_AT_name);
6126 spec = get_AT_ref (die, DW_AT_specification);
6127 if (spec != NULL)
6128 die = spec;
6130 if (die->die_parent != NULL)
6131 checksum_die_context (die->die_parent, ctx);
6133 CHECKSUM_ULEB128 ('C');
6134 CHECKSUM_ULEB128 (tag);
6135 if (name != NULL)
6136 CHECKSUM_STRING (name);
6139 /* Calculate the checksum of a location expression. */
6141 static inline void
6142 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6144 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6145 were emitted as a DW_FORM_sdata instead of a location expression. */
6146 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6148 CHECKSUM_ULEB128 (DW_FORM_sdata);
6149 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6150 return;
6153 /* Otherwise, just checksum the raw location expression. */
6154 while (loc != NULL)
6156 inchash::hash hstate;
6157 hashval_t hash;
6159 CHECKSUM_ULEB128 (loc->dtprel);
6160 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6161 hash_loc_operands (loc, hstate);
6162 hash = hstate.end ();
6163 CHECKSUM (hash);
6164 loc = loc->dw_loc_next;
6168 /* Calculate the checksum of an attribute. */
6170 static void
6171 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6172 struct md5_ctx *ctx, int *mark)
6174 dw_loc_descr_ref loc;
6175 rtx r;
6177 if (AT_class (at) == dw_val_class_die_ref)
6179 dw_die_ref target_die = AT_ref (at);
6181 /* For pointer and reference types, we checksum only the (qualified)
6182 name of the target type (if there is a name). For friend entries,
6183 we checksum only the (qualified) name of the target type or function.
6184 This allows the checksum to remain the same whether the target type
6185 is complete or not. */
6186 if ((at->dw_attr == DW_AT_type
6187 && (tag == DW_TAG_pointer_type
6188 || tag == DW_TAG_reference_type
6189 || tag == DW_TAG_rvalue_reference_type
6190 || tag == DW_TAG_ptr_to_member_type))
6191 || (at->dw_attr == DW_AT_friend
6192 && tag == DW_TAG_friend))
6194 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6196 if (name_attr != NULL)
6198 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6200 if (decl == NULL)
6201 decl = target_die;
6202 CHECKSUM_ULEB128 ('N');
6203 CHECKSUM_ULEB128 (at->dw_attr);
6204 if (decl->die_parent != NULL)
6205 checksum_die_context (decl->die_parent, ctx);
6206 CHECKSUM_ULEB128 ('E');
6207 CHECKSUM_STRING (AT_string (name_attr));
6208 return;
6212 /* For all other references to another DIE, we check to see if the
6213 target DIE has already been visited. If it has, we emit a
6214 backward reference; if not, we descend recursively. */
6215 if (target_die->die_mark > 0)
6217 CHECKSUM_ULEB128 ('R');
6218 CHECKSUM_ULEB128 (at->dw_attr);
6219 CHECKSUM_ULEB128 (target_die->die_mark);
6221 else
6223 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6225 if (decl == NULL)
6226 decl = target_die;
6227 target_die->die_mark = ++(*mark);
6228 CHECKSUM_ULEB128 ('T');
6229 CHECKSUM_ULEB128 (at->dw_attr);
6230 if (decl->die_parent != NULL)
6231 checksum_die_context (decl->die_parent, ctx);
6232 die_checksum_ordered (target_die, ctx, mark);
6234 return;
6237 CHECKSUM_ULEB128 ('A');
6238 CHECKSUM_ULEB128 (at->dw_attr);
6240 switch (AT_class (at))
6242 case dw_val_class_const:
6243 CHECKSUM_ULEB128 (DW_FORM_sdata);
6244 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6245 break;
6247 case dw_val_class_unsigned_const:
6248 CHECKSUM_ULEB128 (DW_FORM_sdata);
6249 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6250 break;
6252 case dw_val_class_const_double:
6253 CHECKSUM_ULEB128 (DW_FORM_block);
6254 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6255 CHECKSUM (at->dw_attr_val.v.val_double);
6256 break;
6258 case dw_val_class_wide_int:
6259 CHECKSUM_ULEB128 (DW_FORM_block);
6260 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6261 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6262 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6263 get_full_len (*at->dw_attr_val.v.val_wide)
6264 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6265 break;
6267 case dw_val_class_vec:
6268 CHECKSUM_ULEB128 (DW_FORM_block);
6269 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6270 * at->dw_attr_val.v.val_vec.elt_size);
6271 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6272 (at->dw_attr_val.v.val_vec.length
6273 * at->dw_attr_val.v.val_vec.elt_size));
6274 break;
6276 case dw_val_class_flag:
6277 CHECKSUM_ULEB128 (DW_FORM_flag);
6278 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6279 break;
6281 case dw_val_class_str:
6282 CHECKSUM_ULEB128 (DW_FORM_string);
6283 CHECKSUM_STRING (AT_string (at));
6284 break;
6286 case dw_val_class_addr:
6287 r = AT_addr (at);
6288 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6289 CHECKSUM_ULEB128 (DW_FORM_string);
6290 CHECKSUM_STRING (XSTR (r, 0));
6291 break;
6293 case dw_val_class_offset:
6294 CHECKSUM_ULEB128 (DW_FORM_sdata);
6295 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6296 break;
6298 case dw_val_class_loc:
6299 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6300 loc_checksum_ordered (loc, ctx);
6301 break;
6303 case dw_val_class_fde_ref:
6304 case dw_val_class_lbl_id:
6305 case dw_val_class_lineptr:
6306 case dw_val_class_macptr:
6307 case dw_val_class_high_pc:
6308 break;
6310 case dw_val_class_file:
6311 CHECKSUM_ULEB128 (DW_FORM_string);
6312 CHECKSUM_STRING (AT_file (at)->filename);
6313 break;
6315 case dw_val_class_data8:
6316 CHECKSUM (at->dw_attr_val.v.val_data8);
6317 break;
6319 default:
6320 break;
6324 struct checksum_attributes
6326 dw_attr_node *at_name;
6327 dw_attr_node *at_type;
6328 dw_attr_node *at_friend;
6329 dw_attr_node *at_accessibility;
6330 dw_attr_node *at_address_class;
6331 dw_attr_node *at_allocated;
6332 dw_attr_node *at_artificial;
6333 dw_attr_node *at_associated;
6334 dw_attr_node *at_binary_scale;
6335 dw_attr_node *at_bit_offset;
6336 dw_attr_node *at_bit_size;
6337 dw_attr_node *at_bit_stride;
6338 dw_attr_node *at_byte_size;
6339 dw_attr_node *at_byte_stride;
6340 dw_attr_node *at_const_value;
6341 dw_attr_node *at_containing_type;
6342 dw_attr_node *at_count;
6343 dw_attr_node *at_data_location;
6344 dw_attr_node *at_data_member_location;
6345 dw_attr_node *at_decimal_scale;
6346 dw_attr_node *at_decimal_sign;
6347 dw_attr_node *at_default_value;
6348 dw_attr_node *at_digit_count;
6349 dw_attr_node *at_discr;
6350 dw_attr_node *at_discr_list;
6351 dw_attr_node *at_discr_value;
6352 dw_attr_node *at_encoding;
6353 dw_attr_node *at_endianity;
6354 dw_attr_node *at_explicit;
6355 dw_attr_node *at_is_optional;
6356 dw_attr_node *at_location;
6357 dw_attr_node *at_lower_bound;
6358 dw_attr_node *at_mutable;
6359 dw_attr_node *at_ordering;
6360 dw_attr_node *at_picture_string;
6361 dw_attr_node *at_prototyped;
6362 dw_attr_node *at_small;
6363 dw_attr_node *at_segment;
6364 dw_attr_node *at_string_length;
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_threads_scaled:
6505 attrs->at_threads_scaled = a;
6506 break;
6507 case DW_AT_upper_bound:
6508 attrs->at_upper_bound = a;
6509 break;
6510 case DW_AT_use_location:
6511 attrs->at_use_location = a;
6512 break;
6513 case DW_AT_use_UTF8:
6514 attrs->at_use_UTF8 = a;
6515 break;
6516 case DW_AT_variable_parameter:
6517 attrs->at_variable_parameter = a;
6518 break;
6519 case DW_AT_virtuality:
6520 attrs->at_virtuality = a;
6521 break;
6522 case DW_AT_visibility:
6523 attrs->at_visibility = a;
6524 break;
6525 case DW_AT_vtable_elem_location:
6526 attrs->at_vtable_elem_location = a;
6527 break;
6528 default:
6529 break;
6534 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6536 static void
6537 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6539 dw_die_ref c;
6540 dw_die_ref decl;
6541 struct checksum_attributes attrs;
6543 CHECKSUM_ULEB128 ('D');
6544 CHECKSUM_ULEB128 (die->die_tag);
6546 memset (&attrs, 0, sizeof (attrs));
6548 decl = get_AT_ref (die, DW_AT_specification);
6549 if (decl != NULL)
6550 collect_checksum_attributes (&attrs, decl);
6551 collect_checksum_attributes (&attrs, die);
6553 CHECKSUM_ATTR (attrs.at_name);
6554 CHECKSUM_ATTR (attrs.at_accessibility);
6555 CHECKSUM_ATTR (attrs.at_address_class);
6556 CHECKSUM_ATTR (attrs.at_allocated);
6557 CHECKSUM_ATTR (attrs.at_artificial);
6558 CHECKSUM_ATTR (attrs.at_associated);
6559 CHECKSUM_ATTR (attrs.at_binary_scale);
6560 CHECKSUM_ATTR (attrs.at_bit_offset);
6561 CHECKSUM_ATTR (attrs.at_bit_size);
6562 CHECKSUM_ATTR (attrs.at_bit_stride);
6563 CHECKSUM_ATTR (attrs.at_byte_size);
6564 CHECKSUM_ATTR (attrs.at_byte_stride);
6565 CHECKSUM_ATTR (attrs.at_const_value);
6566 CHECKSUM_ATTR (attrs.at_containing_type);
6567 CHECKSUM_ATTR (attrs.at_count);
6568 CHECKSUM_ATTR (attrs.at_data_location);
6569 CHECKSUM_ATTR (attrs.at_data_member_location);
6570 CHECKSUM_ATTR (attrs.at_decimal_scale);
6571 CHECKSUM_ATTR (attrs.at_decimal_sign);
6572 CHECKSUM_ATTR (attrs.at_default_value);
6573 CHECKSUM_ATTR (attrs.at_digit_count);
6574 CHECKSUM_ATTR (attrs.at_discr);
6575 CHECKSUM_ATTR (attrs.at_discr_list);
6576 CHECKSUM_ATTR (attrs.at_discr_value);
6577 CHECKSUM_ATTR (attrs.at_encoding);
6578 CHECKSUM_ATTR (attrs.at_endianity);
6579 CHECKSUM_ATTR (attrs.at_explicit);
6580 CHECKSUM_ATTR (attrs.at_is_optional);
6581 CHECKSUM_ATTR (attrs.at_location);
6582 CHECKSUM_ATTR (attrs.at_lower_bound);
6583 CHECKSUM_ATTR (attrs.at_mutable);
6584 CHECKSUM_ATTR (attrs.at_ordering);
6585 CHECKSUM_ATTR (attrs.at_picture_string);
6586 CHECKSUM_ATTR (attrs.at_prototyped);
6587 CHECKSUM_ATTR (attrs.at_small);
6588 CHECKSUM_ATTR (attrs.at_segment);
6589 CHECKSUM_ATTR (attrs.at_string_length);
6590 CHECKSUM_ATTR (attrs.at_threads_scaled);
6591 CHECKSUM_ATTR (attrs.at_upper_bound);
6592 CHECKSUM_ATTR (attrs.at_use_location);
6593 CHECKSUM_ATTR (attrs.at_use_UTF8);
6594 CHECKSUM_ATTR (attrs.at_variable_parameter);
6595 CHECKSUM_ATTR (attrs.at_virtuality);
6596 CHECKSUM_ATTR (attrs.at_visibility);
6597 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6598 CHECKSUM_ATTR (attrs.at_type);
6599 CHECKSUM_ATTR (attrs.at_friend);
6601 /* Checksum the child DIEs. */
6602 c = die->die_child;
6603 if (c) do {
6604 dw_attr_node *name_attr;
6606 c = c->die_sib;
6607 name_attr = get_AT (c, DW_AT_name);
6608 if (is_template_instantiation (c))
6610 /* Ignore instantiations of member type and function templates. */
6612 else if (name_attr != NULL
6613 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6615 /* Use a shallow checksum for named nested types and member
6616 functions. */
6617 CHECKSUM_ULEB128 ('S');
6618 CHECKSUM_ULEB128 (c->die_tag);
6619 CHECKSUM_STRING (AT_string (name_attr));
6621 else
6623 /* Use a deep checksum for other children. */
6624 /* Mark this DIE so it gets processed when unmarking. */
6625 if (c->die_mark == 0)
6626 c->die_mark = -1;
6627 die_checksum_ordered (c, ctx, mark);
6629 } while (c != die->die_child);
6631 CHECKSUM_ULEB128 (0);
6634 /* Add a type name and tag to a hash. */
6635 static void
6636 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6638 CHECKSUM_ULEB128 (tag);
6639 CHECKSUM_STRING (name);
6642 #undef CHECKSUM
6643 #undef CHECKSUM_STRING
6644 #undef CHECKSUM_ATTR
6645 #undef CHECKSUM_LEB128
6646 #undef CHECKSUM_ULEB128
6648 /* Generate the type signature for DIE. This is computed by generating an
6649 MD5 checksum over the DIE's tag, its relevant attributes, and its
6650 children. Attributes that are references to other DIEs are processed
6651 by recursion, using the MARK field to prevent infinite recursion.
6652 If the DIE is nested inside a namespace or another type, we also
6653 need to include that context in the signature. The lower 64 bits
6654 of the resulting MD5 checksum comprise the signature. */
6656 static void
6657 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6659 int mark;
6660 const char *name;
6661 unsigned char checksum[16];
6662 struct md5_ctx ctx;
6663 dw_die_ref decl;
6664 dw_die_ref parent;
6666 name = get_AT_string (die, DW_AT_name);
6667 decl = get_AT_ref (die, DW_AT_specification);
6668 parent = get_die_parent (die);
6670 /* First, compute a signature for just the type name (and its surrounding
6671 context, if any. This is stored in the type unit DIE for link-time
6672 ODR (one-definition rule) checking. */
6674 if (is_cxx () && name != NULL)
6676 md5_init_ctx (&ctx);
6678 /* Checksum the names of surrounding namespaces and structures. */
6679 if (parent != NULL)
6680 checksum_die_context (parent, &ctx);
6682 /* Checksum the current DIE. */
6683 die_odr_checksum (die->die_tag, name, &ctx);
6684 md5_finish_ctx (&ctx, checksum);
6686 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6689 /* Next, compute the complete type signature. */
6691 md5_init_ctx (&ctx);
6692 mark = 1;
6693 die->die_mark = mark;
6695 /* Checksum the names of surrounding namespaces and structures. */
6696 if (parent != NULL)
6697 checksum_die_context (parent, &ctx);
6699 /* Checksum the DIE and its children. */
6700 die_checksum_ordered (die, &ctx, &mark);
6701 unmark_all_dies (die);
6702 md5_finish_ctx (&ctx, checksum);
6704 /* Store the signature in the type node and link the type DIE and the
6705 type node together. */
6706 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6707 DWARF_TYPE_SIGNATURE_SIZE);
6708 die->comdat_type_p = true;
6709 die->die_id.die_type_node = type_node;
6710 type_node->type_die = die;
6712 /* If the DIE is a specification, link its declaration to the type node
6713 as well. */
6714 if (decl != NULL)
6716 decl->comdat_type_p = true;
6717 decl->die_id.die_type_node = type_node;
6721 /* Do the location expressions look same? */
6722 static inline int
6723 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6725 return loc1->dw_loc_opc == loc2->dw_loc_opc
6726 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6727 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6730 /* Do the values look the same? */
6731 static int
6732 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6734 dw_loc_descr_ref loc1, loc2;
6735 rtx r1, r2;
6737 if (v1->val_class != v2->val_class)
6738 return 0;
6740 switch (v1->val_class)
6742 case dw_val_class_const:
6743 return v1->v.val_int == v2->v.val_int;
6744 case dw_val_class_unsigned_const:
6745 return v1->v.val_unsigned == v2->v.val_unsigned;
6746 case dw_val_class_const_double:
6747 return v1->v.val_double.high == v2->v.val_double.high
6748 && v1->v.val_double.low == v2->v.val_double.low;
6749 case dw_val_class_wide_int:
6750 return *v1->v.val_wide == *v2->v.val_wide;
6751 case dw_val_class_vec:
6752 if (v1->v.val_vec.length != v2->v.val_vec.length
6753 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6754 return 0;
6755 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6756 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6757 return 0;
6758 return 1;
6759 case dw_val_class_flag:
6760 return v1->v.val_flag == v2->v.val_flag;
6761 case dw_val_class_str:
6762 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6764 case dw_val_class_addr:
6765 r1 = v1->v.val_addr;
6766 r2 = v2->v.val_addr;
6767 if (GET_CODE (r1) != GET_CODE (r2))
6768 return 0;
6769 return !rtx_equal_p (r1, r2);
6771 case dw_val_class_offset:
6772 return v1->v.val_offset == v2->v.val_offset;
6774 case dw_val_class_loc:
6775 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6776 loc1 && loc2;
6777 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6778 if (!same_loc_p (loc1, loc2, mark))
6779 return 0;
6780 return !loc1 && !loc2;
6782 case dw_val_class_die_ref:
6783 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6785 case dw_val_class_fde_ref:
6786 case dw_val_class_vms_delta:
6787 case dw_val_class_lbl_id:
6788 case dw_val_class_lineptr:
6789 case dw_val_class_macptr:
6790 case dw_val_class_high_pc:
6791 return 1;
6793 case dw_val_class_file:
6794 return v1->v.val_file == v2->v.val_file;
6796 case dw_val_class_data8:
6797 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6799 default:
6800 return 1;
6804 /* Do the attributes look the same? */
6806 static int
6807 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6809 if (at1->dw_attr != at2->dw_attr)
6810 return 0;
6812 /* We don't care that this was compiled with a different compiler
6813 snapshot; if the output is the same, that's what matters. */
6814 if (at1->dw_attr == DW_AT_producer)
6815 return 1;
6817 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6820 /* Do the dies look the same? */
6822 static int
6823 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6825 dw_die_ref c1, c2;
6826 dw_attr_node *a1;
6827 unsigned ix;
6829 /* To avoid infinite recursion. */
6830 if (die1->die_mark)
6831 return die1->die_mark == die2->die_mark;
6832 die1->die_mark = die2->die_mark = ++(*mark);
6834 if (die1->die_tag != die2->die_tag)
6835 return 0;
6837 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6838 return 0;
6840 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6841 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6842 return 0;
6844 c1 = die1->die_child;
6845 c2 = die2->die_child;
6846 if (! c1)
6848 if (c2)
6849 return 0;
6851 else
6852 for (;;)
6854 if (!same_die_p (c1, c2, mark))
6855 return 0;
6856 c1 = c1->die_sib;
6857 c2 = c2->die_sib;
6858 if (c1 == die1->die_child)
6860 if (c2 == die2->die_child)
6861 break;
6862 else
6863 return 0;
6867 return 1;
6870 /* Do the dies look the same? Wrapper around same_die_p. */
6872 static int
6873 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6875 int mark = 0;
6876 int ret = same_die_p (die1, die2, &mark);
6878 unmark_all_dies (die1);
6879 unmark_all_dies (die2);
6881 return ret;
6884 /* The prefix to attach to symbols on DIEs in the current comdat debug
6885 info section. */
6886 static const char *comdat_symbol_id;
6888 /* The index of the current symbol within the current comdat CU. */
6889 static unsigned int comdat_symbol_number;
6891 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6892 children, and set comdat_symbol_id accordingly. */
6894 static void
6895 compute_section_prefix (dw_die_ref unit_die)
6897 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6898 const char *base = die_name ? lbasename (die_name) : "anonymous";
6899 char *name = XALLOCAVEC (char, strlen (base) + 64);
6900 char *p;
6901 int i, mark;
6902 unsigned char checksum[16];
6903 struct md5_ctx ctx;
6905 /* Compute the checksum of the DIE, then append part of it as hex digits to
6906 the name filename of the unit. */
6908 md5_init_ctx (&ctx);
6909 mark = 0;
6910 die_checksum (unit_die, &ctx, &mark);
6911 unmark_all_dies (unit_die);
6912 md5_finish_ctx (&ctx, checksum);
6914 sprintf (name, "%s.", base);
6915 clean_symbol_name (name);
6917 p = name + strlen (name);
6918 for (i = 0; i < 4; i++)
6920 sprintf (p, "%.2x", checksum[i]);
6921 p += 2;
6924 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6925 comdat_symbol_number = 0;
6928 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6930 static int
6931 is_type_die (dw_die_ref die)
6933 switch (die->die_tag)
6935 case DW_TAG_array_type:
6936 case DW_TAG_class_type:
6937 case DW_TAG_interface_type:
6938 case DW_TAG_enumeration_type:
6939 case DW_TAG_pointer_type:
6940 case DW_TAG_reference_type:
6941 case DW_TAG_rvalue_reference_type:
6942 case DW_TAG_string_type:
6943 case DW_TAG_structure_type:
6944 case DW_TAG_subroutine_type:
6945 case DW_TAG_union_type:
6946 case DW_TAG_ptr_to_member_type:
6947 case DW_TAG_set_type:
6948 case DW_TAG_subrange_type:
6949 case DW_TAG_base_type:
6950 case DW_TAG_const_type:
6951 case DW_TAG_file_type:
6952 case DW_TAG_packed_type:
6953 case DW_TAG_volatile_type:
6954 case DW_TAG_typedef:
6955 return 1;
6956 default:
6957 return 0;
6961 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6962 Basically, we want to choose the bits that are likely to be shared between
6963 compilations (types) and leave out the bits that are specific to individual
6964 compilations (functions). */
6966 static int
6967 is_comdat_die (dw_die_ref c)
6969 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6970 we do for stabs. The advantage is a greater likelihood of sharing between
6971 objects that don't include headers in the same order (and therefore would
6972 put the base types in a different comdat). jason 8/28/00 */
6974 if (c->die_tag == DW_TAG_base_type)
6975 return 0;
6977 if (c->die_tag == DW_TAG_pointer_type
6978 || c->die_tag == DW_TAG_reference_type
6979 || c->die_tag == DW_TAG_rvalue_reference_type
6980 || c->die_tag == DW_TAG_const_type
6981 || c->die_tag == DW_TAG_volatile_type)
6983 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6985 return t ? is_comdat_die (t) : 0;
6988 return is_type_die (c);
6991 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6992 compilation unit. */
6994 static int
6995 is_symbol_die (dw_die_ref c)
6997 return (is_type_die (c)
6998 || is_declaration_die (c)
6999 || c->die_tag == DW_TAG_namespace
7000 || c->die_tag == DW_TAG_module);
7003 /* Returns true iff C is a compile-unit DIE. */
7005 static inline bool
7006 is_cu_die (dw_die_ref c)
7008 return c && c->die_tag == DW_TAG_compile_unit;
7011 /* Returns true iff C is a unit DIE of some sort. */
7013 static inline bool
7014 is_unit_die (dw_die_ref c)
7016 return c && (c->die_tag == DW_TAG_compile_unit
7017 || c->die_tag == DW_TAG_partial_unit
7018 || c->die_tag == DW_TAG_type_unit);
7021 /* Returns true iff C is a namespace DIE. */
7023 static inline bool
7024 is_namespace_die (dw_die_ref c)
7026 return c && c->die_tag == DW_TAG_namespace;
7029 /* Returns true iff C is a class or structure DIE. */
7031 static inline bool
7032 is_class_die (dw_die_ref c)
7034 return c && (c->die_tag == DW_TAG_class_type
7035 || c->die_tag == DW_TAG_structure_type);
7038 /* Return non-zero if this DIE is a template parameter. */
7040 static inline bool
7041 is_template_parameter (dw_die_ref die)
7043 switch (die->die_tag)
7045 case DW_TAG_template_type_param:
7046 case DW_TAG_template_value_param:
7047 case DW_TAG_GNU_template_template_param:
7048 case DW_TAG_GNU_template_parameter_pack:
7049 return true;
7050 default:
7051 return false;
7055 /* Return non-zero if this DIE represents a template instantiation. */
7057 static inline bool
7058 is_template_instantiation (dw_die_ref die)
7060 dw_die_ref c;
7062 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7063 return false;
7064 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7065 return false;
7068 static char *
7069 gen_internal_sym (const char *prefix)
7071 char buf[256];
7073 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7074 return xstrdup (buf);
7077 /* Assign symbols to all worthy DIEs under DIE. */
7079 static void
7080 assign_symbol_names (dw_die_ref die)
7082 dw_die_ref c;
7084 if (is_symbol_die (die) && !die->comdat_type_p)
7086 if (comdat_symbol_id)
7088 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7090 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7091 comdat_symbol_id, comdat_symbol_number++);
7092 die->die_id.die_symbol = xstrdup (p);
7094 else
7095 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7098 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7101 struct cu_hash_table_entry
7103 dw_die_ref cu;
7104 unsigned min_comdat_num, max_comdat_num;
7105 struct cu_hash_table_entry *next;
7108 /* Helpers to manipulate hash table of CUs. */
7110 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7112 typedef die_struct *compare_type;
7113 static inline hashval_t hash (const cu_hash_table_entry *);
7114 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7115 static inline void remove (cu_hash_table_entry *);
7118 inline hashval_t
7119 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7121 return htab_hash_string (entry->cu->die_id.die_symbol);
7124 inline bool
7125 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7126 const die_struct *entry2)
7128 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7131 inline void
7132 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7134 struct cu_hash_table_entry *next;
7136 while (entry)
7138 next = entry->next;
7139 free (entry);
7140 entry = next;
7144 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7146 /* Check whether we have already seen this CU and set up SYM_NUM
7147 accordingly. */
7148 static int
7149 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7151 struct cu_hash_table_entry dummy;
7152 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7154 dummy.max_comdat_num = 0;
7156 slot = htable->find_slot_with_hash (cu,
7157 htab_hash_string (cu->die_id.die_symbol),
7158 INSERT);
7159 entry = *slot;
7161 for (; entry; last = entry, entry = entry->next)
7163 if (same_die_p_wrap (cu, entry->cu))
7164 break;
7167 if (entry)
7169 *sym_num = entry->min_comdat_num;
7170 return 1;
7173 entry = XCNEW (struct cu_hash_table_entry);
7174 entry->cu = cu;
7175 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7176 entry->next = *slot;
7177 *slot = entry;
7179 return 0;
7182 /* Record SYM_NUM to record of CU in HTABLE. */
7183 static void
7184 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7185 unsigned int sym_num)
7187 struct cu_hash_table_entry **slot, *entry;
7189 slot = htable->find_slot_with_hash (cu,
7190 htab_hash_string (cu->die_id.die_symbol),
7191 NO_INSERT);
7192 entry = *slot;
7194 entry->max_comdat_num = sym_num;
7197 /* Traverse the DIE (which is always comp_unit_die), and set up
7198 additional compilation units for each of the include files we see
7199 bracketed by BINCL/EINCL. */
7201 static void
7202 break_out_includes (dw_die_ref die)
7204 dw_die_ref c;
7205 dw_die_ref unit = NULL;
7206 limbo_die_node *node, **pnode;
7208 c = die->die_child;
7209 if (c) do {
7210 dw_die_ref prev = c;
7211 c = c->die_sib;
7212 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7213 || (unit && is_comdat_die (c)))
7215 dw_die_ref next = c->die_sib;
7217 /* This DIE is for a secondary CU; remove it from the main one. */
7218 remove_child_with_prev (c, prev);
7220 if (c->die_tag == DW_TAG_GNU_BINCL)
7221 unit = push_new_compile_unit (unit, c);
7222 else if (c->die_tag == DW_TAG_GNU_EINCL)
7223 unit = pop_compile_unit (unit);
7224 else
7225 add_child_die (unit, c);
7226 c = next;
7227 if (c == die->die_child)
7228 break;
7230 } while (c != die->die_child);
7232 #if 0
7233 /* We can only use this in debugging, since the frontend doesn't check
7234 to make sure that we leave every include file we enter. */
7235 gcc_assert (!unit);
7236 #endif
7238 assign_symbol_names (die);
7239 cu_hash_type cu_hash_table (10);
7240 for (node = limbo_die_list, pnode = &limbo_die_list;
7241 node;
7242 node = node->next)
7244 int is_dupl;
7246 compute_section_prefix (node->die);
7247 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7248 &comdat_symbol_number);
7249 assign_symbol_names (node->die);
7250 if (is_dupl)
7251 *pnode = node->next;
7252 else
7254 pnode = &node->next;
7255 record_comdat_symbol_number (node->die, &cu_hash_table,
7256 comdat_symbol_number);
7261 /* Return non-zero if this DIE is a declaration. */
7263 static int
7264 is_declaration_die (dw_die_ref die)
7266 dw_attr_node *a;
7267 unsigned ix;
7269 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7270 if (a->dw_attr == DW_AT_declaration)
7271 return 1;
7273 return 0;
7276 /* Return non-zero if this DIE is nested inside a subprogram. */
7278 static int
7279 is_nested_in_subprogram (dw_die_ref die)
7281 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7283 if (decl == NULL)
7284 decl = die;
7285 return local_scope_p (decl);
7288 /* Return non-zero if this DIE contains a defining declaration of a
7289 subprogram. */
7291 static int
7292 contains_subprogram_definition (dw_die_ref die)
7294 dw_die_ref c;
7296 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7297 return 1;
7298 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7299 return 0;
7302 /* Return non-zero if this is a type DIE that should be moved to a
7303 COMDAT .debug_types section. */
7305 static int
7306 should_move_die_to_comdat (dw_die_ref die)
7308 switch (die->die_tag)
7310 case DW_TAG_class_type:
7311 case DW_TAG_structure_type:
7312 case DW_TAG_enumeration_type:
7313 case DW_TAG_union_type:
7314 /* Don't move declarations, inlined instances, types nested in a
7315 subprogram, or types that contain subprogram definitions. */
7316 if (is_declaration_die (die)
7317 || get_AT (die, DW_AT_abstract_origin)
7318 || is_nested_in_subprogram (die)
7319 || contains_subprogram_definition (die))
7320 return 0;
7321 return 1;
7322 case DW_TAG_array_type:
7323 case DW_TAG_interface_type:
7324 case DW_TAG_pointer_type:
7325 case DW_TAG_reference_type:
7326 case DW_TAG_rvalue_reference_type:
7327 case DW_TAG_string_type:
7328 case DW_TAG_subroutine_type:
7329 case DW_TAG_ptr_to_member_type:
7330 case DW_TAG_set_type:
7331 case DW_TAG_subrange_type:
7332 case DW_TAG_base_type:
7333 case DW_TAG_const_type:
7334 case DW_TAG_file_type:
7335 case DW_TAG_packed_type:
7336 case DW_TAG_volatile_type:
7337 case DW_TAG_typedef:
7338 default:
7339 return 0;
7343 /* Make a clone of DIE. */
7345 static dw_die_ref
7346 clone_die (dw_die_ref die)
7348 dw_die_ref clone;
7349 dw_attr_node *a;
7350 unsigned ix;
7352 clone = ggc_cleared_alloc<die_node> ();
7353 clone->die_tag = die->die_tag;
7355 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7356 add_dwarf_attr (clone, a);
7358 return clone;
7361 /* Make a clone of the tree rooted at DIE. */
7363 static dw_die_ref
7364 clone_tree (dw_die_ref die)
7366 dw_die_ref c;
7367 dw_die_ref clone = clone_die (die);
7369 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7371 return clone;
7374 /* Make a clone of DIE as a declaration. */
7376 static dw_die_ref
7377 clone_as_declaration (dw_die_ref die)
7379 dw_die_ref clone;
7380 dw_die_ref decl;
7381 dw_attr_node *a;
7382 unsigned ix;
7384 /* If the DIE is already a declaration, just clone it. */
7385 if (is_declaration_die (die))
7386 return clone_die (die);
7388 /* If the DIE is a specification, just clone its declaration DIE. */
7389 decl = get_AT_ref (die, DW_AT_specification);
7390 if (decl != NULL)
7392 clone = clone_die (decl);
7393 if (die->comdat_type_p)
7394 add_AT_die_ref (clone, DW_AT_signature, die);
7395 return clone;
7398 clone = ggc_cleared_alloc<die_node> ();
7399 clone->die_tag = die->die_tag;
7401 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7403 /* We don't want to copy over all attributes.
7404 For example we don't want DW_AT_byte_size because otherwise we will no
7405 longer have a declaration and GDB will treat it as a definition. */
7407 switch (a->dw_attr)
7409 case DW_AT_abstract_origin:
7410 case DW_AT_artificial:
7411 case DW_AT_containing_type:
7412 case DW_AT_external:
7413 case DW_AT_name:
7414 case DW_AT_type:
7415 case DW_AT_virtuality:
7416 case DW_AT_linkage_name:
7417 case DW_AT_MIPS_linkage_name:
7418 add_dwarf_attr (clone, a);
7419 break;
7420 case DW_AT_byte_size:
7421 default:
7422 break;
7426 if (die->comdat_type_p)
7427 add_AT_die_ref (clone, DW_AT_signature, die);
7429 add_AT_flag (clone, DW_AT_declaration, 1);
7430 return clone;
7434 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7436 struct decl_table_entry
7438 dw_die_ref orig;
7439 dw_die_ref copy;
7442 /* Helpers to manipulate hash table of copied declarations. */
7444 /* Hashtable helpers. */
7446 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7448 typedef die_struct *compare_type;
7449 static inline hashval_t hash (const decl_table_entry *);
7450 static inline bool equal (const decl_table_entry *, const die_struct *);
7453 inline hashval_t
7454 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7456 return htab_hash_pointer (entry->orig);
7459 inline bool
7460 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7461 const die_struct *entry2)
7463 return entry1->orig == entry2;
7466 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7468 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7469 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7470 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7471 to check if the ancestor has already been copied into UNIT. */
7473 static dw_die_ref
7474 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7475 decl_hash_type *decl_table)
7477 dw_die_ref parent = die->die_parent;
7478 dw_die_ref new_parent = unit;
7479 dw_die_ref copy;
7480 decl_table_entry **slot = NULL;
7481 struct decl_table_entry *entry = NULL;
7483 if (decl_table)
7485 /* Check if the entry has already been copied to UNIT. */
7486 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7487 INSERT);
7488 if (*slot != HTAB_EMPTY_ENTRY)
7490 entry = *slot;
7491 return entry->copy;
7494 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7495 entry = XCNEW (struct decl_table_entry);
7496 entry->orig = die;
7497 entry->copy = NULL;
7498 *slot = entry;
7501 if (parent != NULL)
7503 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7504 if (spec != NULL)
7505 parent = spec;
7506 if (!is_unit_die (parent))
7507 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7510 copy = clone_as_declaration (die);
7511 add_child_die (new_parent, copy);
7513 if (decl_table)
7515 /* Record the pointer to the copy. */
7516 entry->copy = copy;
7519 return copy;
7521 /* Copy the declaration context to the new type unit DIE. This includes
7522 any surrounding namespace or type declarations. If the DIE has an
7523 AT_specification attribute, it also includes attributes and children
7524 attached to the specification, and returns a pointer to the original
7525 parent of the declaration DIE. Returns NULL otherwise. */
7527 static dw_die_ref
7528 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7530 dw_die_ref decl;
7531 dw_die_ref new_decl;
7532 dw_die_ref orig_parent = NULL;
7534 decl = get_AT_ref (die, DW_AT_specification);
7535 if (decl == NULL)
7536 decl = die;
7537 else
7539 unsigned ix;
7540 dw_die_ref c;
7541 dw_attr_node *a;
7543 /* The original DIE will be changed to a declaration, and must
7544 be moved to be a child of the original declaration DIE. */
7545 orig_parent = decl->die_parent;
7547 /* Copy the type node pointer from the new DIE to the original
7548 declaration DIE so we can forward references later. */
7549 decl->comdat_type_p = true;
7550 decl->die_id.die_type_node = die->die_id.die_type_node;
7552 remove_AT (die, DW_AT_specification);
7554 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7556 if (a->dw_attr != DW_AT_name
7557 && a->dw_attr != DW_AT_declaration
7558 && a->dw_attr != DW_AT_external)
7559 add_dwarf_attr (die, a);
7562 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7565 if (decl->die_parent != NULL
7566 && !is_unit_die (decl->die_parent))
7568 new_decl = copy_ancestor_tree (unit, decl, NULL);
7569 if (new_decl != NULL)
7571 remove_AT (new_decl, DW_AT_signature);
7572 add_AT_specification (die, new_decl);
7576 return orig_parent;
7579 /* Generate the skeleton ancestor tree for the given NODE, then clone
7580 the DIE and add the clone into the tree. */
7582 static void
7583 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7585 if (node->new_die != NULL)
7586 return;
7588 node->new_die = clone_as_declaration (node->old_die);
7590 if (node->parent != NULL)
7592 generate_skeleton_ancestor_tree (node->parent);
7593 add_child_die (node->parent->new_die, node->new_die);
7597 /* Generate a skeleton tree of DIEs containing any declarations that are
7598 found in the original tree. We traverse the tree looking for declaration
7599 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7601 static void
7602 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7604 skeleton_chain_node node;
7605 dw_die_ref c;
7606 dw_die_ref first;
7607 dw_die_ref prev = NULL;
7608 dw_die_ref next = NULL;
7610 node.parent = parent;
7612 first = c = parent->old_die->die_child;
7613 if (c)
7614 next = c->die_sib;
7615 if (c) do {
7616 if (prev == NULL || prev->die_sib == c)
7617 prev = c;
7618 c = next;
7619 next = (c == first ? NULL : c->die_sib);
7620 node.old_die = c;
7621 node.new_die = NULL;
7622 if (is_declaration_die (c))
7624 if (is_template_instantiation (c))
7626 /* Instantiated templates do not need to be cloned into the
7627 type unit. Just move the DIE and its children back to
7628 the skeleton tree (in the main CU). */
7629 remove_child_with_prev (c, prev);
7630 add_child_die (parent->new_die, c);
7631 c = prev;
7633 else
7635 /* Clone the existing DIE, move the original to the skeleton
7636 tree (which is in the main CU), and put the clone, with
7637 all the original's children, where the original came from
7638 (which is about to be moved to the type unit). */
7639 dw_die_ref clone = clone_die (c);
7640 move_all_children (c, clone);
7642 /* If the original has a DW_AT_object_pointer attribute,
7643 it would now point to a child DIE just moved to the
7644 cloned tree, so we need to remove that attribute from
7645 the original. */
7646 remove_AT (c, DW_AT_object_pointer);
7648 replace_child (c, clone, prev);
7649 generate_skeleton_ancestor_tree (parent);
7650 add_child_die (parent->new_die, c);
7651 node.new_die = c;
7652 c = clone;
7655 generate_skeleton_bottom_up (&node);
7656 } while (next != NULL);
7659 /* Wrapper function for generate_skeleton_bottom_up. */
7661 static dw_die_ref
7662 generate_skeleton (dw_die_ref die)
7664 skeleton_chain_node node;
7666 node.old_die = die;
7667 node.new_die = NULL;
7668 node.parent = NULL;
7670 /* If this type definition is nested inside another type,
7671 and is not an instantiation of a template, always leave
7672 at least a declaration in its place. */
7673 if (die->die_parent != NULL
7674 && is_type_die (die->die_parent)
7675 && !is_template_instantiation (die))
7676 node.new_die = clone_as_declaration (die);
7678 generate_skeleton_bottom_up (&node);
7679 return node.new_die;
7682 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7683 declaration. The original DIE is moved to a new compile unit so that
7684 existing references to it follow it to the new location. If any of the
7685 original DIE's descendants is a declaration, we need to replace the
7686 original DIE with a skeleton tree and move the declarations back into the
7687 skeleton tree. */
7689 static dw_die_ref
7690 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7691 dw_die_ref prev)
7693 dw_die_ref skeleton, orig_parent;
7695 /* Copy the declaration context to the type unit DIE. If the returned
7696 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7697 that DIE. */
7698 orig_parent = copy_declaration_context (unit, child);
7700 skeleton = generate_skeleton (child);
7701 if (skeleton == NULL)
7702 remove_child_with_prev (child, prev);
7703 else
7705 skeleton->comdat_type_p = true;
7706 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7708 /* If the original DIE was a specification, we need to put
7709 the skeleton under the parent DIE of the declaration.
7710 This leaves the original declaration in the tree, but
7711 it will be pruned later since there are no longer any
7712 references to it. */
7713 if (orig_parent != NULL)
7715 remove_child_with_prev (child, prev);
7716 add_child_die (orig_parent, skeleton);
7718 else
7719 replace_child (child, skeleton, prev);
7722 return skeleton;
7725 static void
7726 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7727 comdat_type_node *type_node,
7728 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7730 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7731 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7732 DWARF procedure references in the DW_AT_location attribute. */
7734 static dw_die_ref
7735 copy_dwarf_procedure (dw_die_ref die,
7736 comdat_type_node *type_node,
7737 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7739 /* We do this for COMDAT section, which is DWARFv4 specific, so
7740 DWARF procedure are always DW_TAG_dwarf_procedure DIEs (unlike
7741 DW_TAG_variable in DWARFv3). */
7742 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7744 /* DWARF procedures are not supposed to have children... */
7745 gcc_assert (die->die_child == NULL);
7747 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7748 gcc_assert (vec_safe_length (die->die_attr) == 1
7749 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7751 /* Do not copy more than once DWARF procedures. */
7752 bool existed;
7753 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7754 if (existed)
7755 return die_copy;
7757 die_copy = clone_die (die);
7758 add_child_die (type_node->root_die, die_copy);
7759 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7760 return die_copy;
7763 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7764 procedures in DIE's attributes. */
7766 static void
7767 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7768 comdat_type_node *type_node,
7769 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7771 dw_attr_node *a;
7772 unsigned i;
7774 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7776 dw_loc_descr_ref loc;
7778 if (a->dw_attr_val.val_class != dw_val_class_loc)
7779 continue;
7781 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7783 switch (loc->dw_loc_opc)
7785 case DW_OP_call2:
7786 case DW_OP_call4:
7787 case DW_OP_call_ref:
7788 gcc_assert (loc->dw_loc_oprnd1.val_class
7789 == dw_val_class_die_ref);
7790 loc->dw_loc_oprnd1.v.val_die_ref.die
7791 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7792 type_node,
7793 copied_dwarf_procs);
7795 default:
7796 break;
7802 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7803 rewrite references to point to the copies.
7805 References are looked for in DIE's attributes and recursively in all its
7806 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7807 mapping from old DWARF procedures to their copy. It is used not to copy
7808 twice the same DWARF procedure under TYPE_NODE. */
7810 static void
7811 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7812 comdat_type_node *type_node,
7813 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7815 dw_die_ref c;
7817 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7818 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7819 type_node,
7820 copied_dwarf_procs));
7823 /* Traverse the DIE and set up additional .debug_types sections for each
7824 type worthy of being placed in a COMDAT section. */
7826 static void
7827 break_out_comdat_types (dw_die_ref die)
7829 dw_die_ref c;
7830 dw_die_ref first;
7831 dw_die_ref prev = NULL;
7832 dw_die_ref next = NULL;
7833 dw_die_ref unit = NULL;
7835 first = c = die->die_child;
7836 if (c)
7837 next = c->die_sib;
7838 if (c) do {
7839 if (prev == NULL || prev->die_sib == c)
7840 prev = c;
7841 c = next;
7842 next = (c == first ? NULL : c->die_sib);
7843 if (should_move_die_to_comdat (c))
7845 dw_die_ref replacement;
7846 comdat_type_node *type_node;
7848 /* Break out nested types into their own type units. */
7849 break_out_comdat_types (c);
7851 /* Create a new type unit DIE as the root for the new tree, and
7852 add it to the list of comdat types. */
7853 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7854 add_AT_unsigned (unit, DW_AT_language,
7855 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7856 type_node = ggc_cleared_alloc<comdat_type_node> ();
7857 type_node->root_die = unit;
7858 type_node->next = comdat_type_list;
7859 comdat_type_list = type_node;
7861 /* Generate the type signature. */
7862 generate_type_signature (c, type_node);
7864 /* Copy the declaration context, attributes, and children of the
7865 declaration into the new type unit DIE, then remove this DIE
7866 from the main CU (or replace it with a skeleton if necessary). */
7867 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7868 type_node->skeleton_die = replacement;
7870 /* Add the DIE to the new compunit. */
7871 add_child_die (unit, c);
7873 /* Types can reference DWARF procedures for type size or data location
7874 expressions. Calls in DWARF expressions cannot target procedures
7875 that are not in the same section. So we must copy DWARF procedures
7876 along with this type and then rewrite references to them. */
7877 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7878 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7880 if (replacement != NULL)
7881 c = replacement;
7883 else if (c->die_tag == DW_TAG_namespace
7884 || c->die_tag == DW_TAG_class_type
7885 || c->die_tag == DW_TAG_structure_type
7886 || c->die_tag == DW_TAG_union_type)
7888 /* Look for nested types that can be broken out. */
7889 break_out_comdat_types (c);
7891 } while (next != NULL);
7894 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7895 Enter all the cloned children into the hash table decl_table. */
7897 static dw_die_ref
7898 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7900 dw_die_ref c;
7901 dw_die_ref clone;
7902 struct decl_table_entry *entry;
7903 decl_table_entry **slot;
7905 if (die->die_tag == DW_TAG_subprogram)
7906 clone = clone_as_declaration (die);
7907 else
7908 clone = clone_die (die);
7910 slot = decl_table->find_slot_with_hash (die,
7911 htab_hash_pointer (die), INSERT);
7913 /* Assert that DIE isn't in the hash table yet. If it would be there
7914 before, the ancestors would be necessarily there as well, therefore
7915 clone_tree_partial wouldn't be called. */
7916 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7918 entry = XCNEW (struct decl_table_entry);
7919 entry->orig = die;
7920 entry->copy = clone;
7921 *slot = entry;
7923 if (die->die_tag != DW_TAG_subprogram)
7924 FOR_EACH_CHILD (die, c,
7925 add_child_die (clone, clone_tree_partial (c, decl_table)));
7927 return clone;
7930 /* Walk the DIE and its children, looking for references to incomplete
7931 or trivial types that are unmarked (i.e., that are not in the current
7932 type_unit). */
7934 static void
7935 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7937 dw_die_ref c;
7938 dw_attr_node *a;
7939 unsigned ix;
7941 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7943 if (AT_class (a) == dw_val_class_die_ref)
7945 dw_die_ref targ = AT_ref (a);
7946 decl_table_entry **slot;
7947 struct decl_table_entry *entry;
7949 if (targ->die_mark != 0 || targ->comdat_type_p)
7950 continue;
7952 slot = decl_table->find_slot_with_hash (targ,
7953 htab_hash_pointer (targ),
7954 INSERT);
7956 if (*slot != HTAB_EMPTY_ENTRY)
7958 /* TARG has already been copied, so we just need to
7959 modify the reference to point to the copy. */
7960 entry = *slot;
7961 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7963 else
7965 dw_die_ref parent = unit;
7966 dw_die_ref copy = clone_die (targ);
7968 /* Record in DECL_TABLE that TARG has been copied.
7969 Need to do this now, before the recursive call,
7970 because DECL_TABLE may be expanded and SLOT
7971 would no longer be a valid pointer. */
7972 entry = XCNEW (struct decl_table_entry);
7973 entry->orig = targ;
7974 entry->copy = copy;
7975 *slot = entry;
7977 /* If TARG is not a declaration DIE, we need to copy its
7978 children. */
7979 if (!is_declaration_die (targ))
7981 FOR_EACH_CHILD (
7982 targ, c,
7983 add_child_die (copy,
7984 clone_tree_partial (c, decl_table)));
7987 /* Make sure the cloned tree is marked as part of the
7988 type unit. */
7989 mark_dies (copy);
7991 /* If TARG has surrounding context, copy its ancestor tree
7992 into the new type unit. */
7993 if (targ->die_parent != NULL
7994 && !is_unit_die (targ->die_parent))
7995 parent = copy_ancestor_tree (unit, targ->die_parent,
7996 decl_table);
7998 add_child_die (parent, copy);
7999 a->dw_attr_val.v.val_die_ref.die = copy;
8001 /* Make sure the newly-copied DIE is walked. If it was
8002 installed in a previously-added context, it won't
8003 get visited otherwise. */
8004 if (parent != unit)
8006 /* Find the highest point of the newly-added tree,
8007 mark each node along the way, and walk from there. */
8008 parent->die_mark = 1;
8009 while (parent->die_parent
8010 && parent->die_parent->die_mark == 0)
8012 parent = parent->die_parent;
8013 parent->die_mark = 1;
8015 copy_decls_walk (unit, parent, decl_table);
8021 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8024 /* Copy declarations for "unworthy" types into the new comdat section.
8025 Incomplete types, modified types, and certain other types aren't broken
8026 out into comdat sections of their own, so they don't have a signature,
8027 and we need to copy the declaration into the same section so that we
8028 don't have an external reference. */
8030 static void
8031 copy_decls_for_unworthy_types (dw_die_ref unit)
8033 mark_dies (unit);
8034 decl_hash_type decl_table (10);
8035 copy_decls_walk (unit, unit, &decl_table);
8036 unmark_dies (unit);
8039 /* Traverse the DIE and add a sibling attribute if it may have the
8040 effect of speeding up access to siblings. To save some space,
8041 avoid generating sibling attributes for DIE's without children. */
8043 static void
8044 add_sibling_attributes (dw_die_ref die)
8046 dw_die_ref c;
8048 if (! die->die_child)
8049 return;
8051 if (die->die_parent && die != die->die_parent->die_child)
8052 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8054 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8057 /* Output all location lists for the DIE and its children. */
8059 static void
8060 output_location_lists (dw_die_ref die)
8062 dw_die_ref c;
8063 dw_attr_node *a;
8064 unsigned ix;
8066 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8067 if (AT_class (a) == dw_val_class_loc_list)
8068 output_loc_list (AT_loc_list (a));
8070 FOR_EACH_CHILD (die, c, output_location_lists (c));
8073 /* We want to limit the number of external references, because they are
8074 larger than local references: a relocation takes multiple words, and
8075 even a sig8 reference is always eight bytes, whereas a local reference
8076 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8077 So if we encounter multiple external references to the same type DIE, we
8078 make a local typedef stub for it and redirect all references there.
8080 This is the element of the hash table for keeping track of these
8081 references. */
8083 struct external_ref
8085 dw_die_ref type;
8086 dw_die_ref stub;
8087 unsigned n_refs;
8090 /* Hashtable helpers. */
8092 struct external_ref_hasher : free_ptr_hash <external_ref>
8094 static inline hashval_t hash (const external_ref *);
8095 static inline bool equal (const external_ref *, const external_ref *);
8098 inline hashval_t
8099 external_ref_hasher::hash (const external_ref *r)
8101 dw_die_ref die = r->type;
8102 hashval_t h = 0;
8104 /* We can't use the address of the DIE for hashing, because
8105 that will make the order of the stub DIEs non-deterministic. */
8106 if (! die->comdat_type_p)
8107 /* We have a symbol; use it to compute a hash. */
8108 h = htab_hash_string (die->die_id.die_symbol);
8109 else
8111 /* We have a type signature; use a subset of the bits as the hash.
8112 The 8-byte signature is at least as large as hashval_t. */
8113 comdat_type_node *type_node = die->die_id.die_type_node;
8114 memcpy (&h, type_node->signature, sizeof (h));
8116 return h;
8119 inline bool
8120 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8122 return r1->type == r2->type;
8125 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8127 /* Return a pointer to the external_ref for references to DIE. */
8129 static struct external_ref *
8130 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8132 struct external_ref ref, *ref_p;
8133 external_ref **slot;
8135 ref.type = die;
8136 slot = map->find_slot (&ref, INSERT);
8137 if (*slot != HTAB_EMPTY_ENTRY)
8138 return *slot;
8140 ref_p = XCNEW (struct external_ref);
8141 ref_p->type = die;
8142 *slot = ref_p;
8143 return ref_p;
8146 /* Subroutine of optimize_external_refs, below.
8148 If we see a type skeleton, record it as our stub. If we see external
8149 references, remember how many we've seen. */
8151 static void
8152 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8154 dw_die_ref c;
8155 dw_attr_node *a;
8156 unsigned ix;
8157 struct external_ref *ref_p;
8159 if (is_type_die (die)
8160 && (c = get_AT_ref (die, DW_AT_signature)))
8162 /* This is a local skeleton; use it for local references. */
8163 ref_p = lookup_external_ref (map, c);
8164 ref_p->stub = die;
8167 /* Scan the DIE references, and remember any that refer to DIEs from
8168 other CUs (i.e. those which are not marked). */
8169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8170 if (AT_class (a) == dw_val_class_die_ref
8171 && (c = AT_ref (a))->die_mark == 0
8172 && is_type_die (c))
8174 ref_p = lookup_external_ref (map, c);
8175 ref_p->n_refs++;
8178 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8181 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8182 points to an external_ref, DATA is the CU we're processing. If we don't
8183 already have a local stub, and we have multiple refs, build a stub. */
8186 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8188 struct external_ref *ref_p = *slot;
8190 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8192 /* We have multiple references to this type, so build a small stub.
8193 Both of these forms are a bit dodgy from the perspective of the
8194 DWARF standard, since technically they should have names. */
8195 dw_die_ref cu = data;
8196 dw_die_ref type = ref_p->type;
8197 dw_die_ref stub = NULL;
8199 if (type->comdat_type_p)
8201 /* If we refer to this type via sig8, use AT_signature. */
8202 stub = new_die (type->die_tag, cu, NULL_TREE);
8203 add_AT_die_ref (stub, DW_AT_signature, type);
8205 else
8207 /* Otherwise, use a typedef with no name. */
8208 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8209 add_AT_die_ref (stub, DW_AT_type, type);
8212 stub->die_mark++;
8213 ref_p->stub = stub;
8215 return 1;
8218 /* DIE is a unit; look through all the DIE references to see if there are
8219 any external references to types, and if so, create local stubs for
8220 them which will be applied in build_abbrev_table. This is useful because
8221 references to local DIEs are smaller. */
8223 static external_ref_hash_type *
8224 optimize_external_refs (dw_die_ref die)
8226 external_ref_hash_type *map = new external_ref_hash_type (10);
8227 optimize_external_refs_1 (die, map);
8228 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8229 return map;
8232 /* The format of each DIE (and its attribute value pairs) is encoded in an
8233 abbreviation table. This routine builds the abbreviation table and assigns
8234 a unique abbreviation id for each abbreviation entry. The children of each
8235 die are visited recursively. */
8237 static void
8238 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8240 unsigned long abbrev_id;
8241 unsigned int n_alloc;
8242 dw_die_ref c;
8243 dw_attr_node *a;
8244 unsigned ix;
8246 /* Scan the DIE references, and replace any that refer to
8247 DIEs from other CUs (i.e. those which are not marked) with
8248 the local stubs we built in optimize_external_refs. */
8249 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8250 if (AT_class (a) == dw_val_class_die_ref
8251 && (c = AT_ref (a))->die_mark == 0)
8253 struct external_ref *ref_p;
8254 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8256 ref_p = lookup_external_ref (extern_map, c);
8257 if (ref_p->stub && ref_p->stub != die)
8258 change_AT_die_ref (a, ref_p->stub);
8259 else
8260 /* We aren't changing this reference, so mark it external. */
8261 set_AT_ref_external (a, 1);
8264 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8266 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8267 dw_attr_node *die_a, *abbrev_a;
8268 unsigned ix;
8269 bool ok = true;
8271 if (abbrev->die_tag != die->die_tag)
8272 continue;
8273 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8274 continue;
8276 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8277 continue;
8279 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8281 abbrev_a = &(*abbrev->die_attr)[ix];
8282 if ((abbrev_a->dw_attr != die_a->dw_attr)
8283 || (value_format (abbrev_a) != value_format (die_a)))
8285 ok = false;
8286 break;
8289 if (ok)
8290 break;
8293 if (abbrev_id >= abbrev_die_table_in_use)
8295 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8297 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8298 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8299 n_alloc);
8301 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8302 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8303 abbrev_die_table_allocated = n_alloc;
8306 ++abbrev_die_table_in_use;
8307 abbrev_die_table[abbrev_id] = die;
8310 die->die_abbrev = abbrev_id;
8311 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8314 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8316 static int
8317 constant_size (unsigned HOST_WIDE_INT value)
8319 int log;
8321 if (value == 0)
8322 log = 0;
8323 else
8324 log = floor_log2 (value);
8326 log = log / 8;
8327 log = 1 << (floor_log2 (log) + 1);
8329 return log;
8332 /* Return the size of a DIE as it is represented in the
8333 .debug_info section. */
8335 static unsigned long
8336 size_of_die (dw_die_ref die)
8338 unsigned long size = 0;
8339 dw_attr_node *a;
8340 unsigned ix;
8341 enum dwarf_form form;
8343 size += size_of_uleb128 (die->die_abbrev);
8344 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8346 switch (AT_class (a))
8348 case dw_val_class_addr:
8349 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8351 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8352 size += size_of_uleb128 (AT_index (a));
8354 else
8355 size += DWARF2_ADDR_SIZE;
8356 break;
8357 case dw_val_class_offset:
8358 size += DWARF_OFFSET_SIZE;
8359 break;
8360 case dw_val_class_loc:
8362 unsigned long lsize = size_of_locs (AT_loc (a));
8364 /* Block length. */
8365 if (dwarf_version >= 4)
8366 size += size_of_uleb128 (lsize);
8367 else
8368 size += constant_size (lsize);
8369 size += lsize;
8371 break;
8372 case dw_val_class_loc_list:
8373 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8375 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8376 size += size_of_uleb128 (AT_index (a));
8378 else
8379 size += DWARF_OFFSET_SIZE;
8380 break;
8381 case dw_val_class_range_list:
8382 size += DWARF_OFFSET_SIZE;
8383 break;
8384 case dw_val_class_const:
8385 size += size_of_sleb128 (AT_int (a));
8386 break;
8387 case dw_val_class_unsigned_const:
8389 int csize = constant_size (AT_unsigned (a));
8390 if (dwarf_version == 3
8391 && a->dw_attr == DW_AT_data_member_location
8392 && csize >= 4)
8393 size += size_of_uleb128 (AT_unsigned (a));
8394 else
8395 size += csize;
8397 break;
8398 case dw_val_class_const_double:
8399 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8400 if (HOST_BITS_PER_WIDE_INT >= 64)
8401 size++; /* block */
8402 break;
8403 case dw_val_class_wide_int:
8404 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8405 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8406 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8407 > 64)
8408 size++; /* block */
8409 break;
8410 case dw_val_class_vec:
8411 size += constant_size (a->dw_attr_val.v.val_vec.length
8412 * a->dw_attr_val.v.val_vec.elt_size)
8413 + a->dw_attr_val.v.val_vec.length
8414 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8415 break;
8416 case dw_val_class_flag:
8417 if (dwarf_version >= 4)
8418 /* Currently all add_AT_flag calls pass in 1 as last argument,
8419 so DW_FORM_flag_present can be used. If that ever changes,
8420 we'll need to use DW_FORM_flag and have some optimization
8421 in build_abbrev_table that will change those to
8422 DW_FORM_flag_present if it is set to 1 in all DIEs using
8423 the same abbrev entry. */
8424 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8425 else
8426 size += 1;
8427 break;
8428 case dw_val_class_die_ref:
8429 if (AT_ref_external (a))
8431 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8432 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8433 is sized by target address length, whereas in DWARF3
8434 it's always sized as an offset. */
8435 if (use_debug_types)
8436 size += DWARF_TYPE_SIGNATURE_SIZE;
8437 else if (dwarf_version == 2)
8438 size += DWARF2_ADDR_SIZE;
8439 else
8440 size += DWARF_OFFSET_SIZE;
8442 else
8443 size += DWARF_OFFSET_SIZE;
8444 break;
8445 case dw_val_class_fde_ref:
8446 size += DWARF_OFFSET_SIZE;
8447 break;
8448 case dw_val_class_lbl_id:
8449 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8451 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8452 size += size_of_uleb128 (AT_index (a));
8454 else
8455 size += DWARF2_ADDR_SIZE;
8456 break;
8457 case dw_val_class_lineptr:
8458 case dw_val_class_macptr:
8459 size += DWARF_OFFSET_SIZE;
8460 break;
8461 case dw_val_class_str:
8462 form = AT_string_form (a);
8463 if (form == DW_FORM_strp)
8464 size += DWARF_OFFSET_SIZE;
8465 else if (form == DW_FORM_GNU_str_index)
8466 size += size_of_uleb128 (AT_index (a));
8467 else
8468 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8469 break;
8470 case dw_val_class_file:
8471 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8472 break;
8473 case dw_val_class_data8:
8474 size += 8;
8475 break;
8476 case dw_val_class_vms_delta:
8477 size += DWARF_OFFSET_SIZE;
8478 break;
8479 case dw_val_class_high_pc:
8480 size += DWARF2_ADDR_SIZE;
8481 break;
8482 case dw_val_class_discr_value:
8483 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8484 break;
8485 case dw_val_class_discr_list:
8487 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8489 /* This is a block, so we have the block length and then its
8490 data. */
8491 size += constant_size (block_size) + block_size;
8493 break;
8494 default:
8495 gcc_unreachable ();
8499 return size;
8502 /* Size the debugging information associated with a given DIE. Visits the
8503 DIE's children recursively. Updates the global variable next_die_offset, on
8504 each time through. Uses the current value of next_die_offset to update the
8505 die_offset field in each DIE. */
8507 static void
8508 calc_die_sizes (dw_die_ref die)
8510 dw_die_ref c;
8512 gcc_assert (die->die_offset == 0
8513 || (unsigned long int) die->die_offset == next_die_offset);
8514 die->die_offset = next_die_offset;
8515 next_die_offset += size_of_die (die);
8517 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8519 if (die->die_child != NULL)
8520 /* Count the null byte used to terminate sibling lists. */
8521 next_die_offset += 1;
8524 /* Size just the base type children at the start of the CU.
8525 This is needed because build_abbrev needs to size locs
8526 and sizing of type based stack ops needs to know die_offset
8527 values for the base types. */
8529 static void
8530 calc_base_type_die_sizes (void)
8532 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8533 unsigned int i;
8534 dw_die_ref base_type;
8535 #if ENABLE_ASSERT_CHECKING
8536 dw_die_ref prev = comp_unit_die ()->die_child;
8537 #endif
8539 die_offset += size_of_die (comp_unit_die ());
8540 for (i = 0; base_types.iterate (i, &base_type); i++)
8542 #if ENABLE_ASSERT_CHECKING
8543 gcc_assert (base_type->die_offset == 0
8544 && prev->die_sib == base_type
8545 && base_type->die_child == NULL
8546 && base_type->die_abbrev);
8547 prev = base_type;
8548 #endif
8549 base_type->die_offset = die_offset;
8550 die_offset += size_of_die (base_type);
8554 /* Set the marks for a die and its children. We do this so
8555 that we know whether or not a reference needs to use FORM_ref_addr; only
8556 DIEs in the same CU will be marked. We used to clear out the offset
8557 and use that as the flag, but ran into ordering problems. */
8559 static void
8560 mark_dies (dw_die_ref die)
8562 dw_die_ref c;
8564 gcc_assert (!die->die_mark);
8566 die->die_mark = 1;
8567 FOR_EACH_CHILD (die, c, mark_dies (c));
8570 /* Clear the marks for a die and its children. */
8572 static void
8573 unmark_dies (dw_die_ref die)
8575 dw_die_ref c;
8577 if (! use_debug_types)
8578 gcc_assert (die->die_mark);
8580 die->die_mark = 0;
8581 FOR_EACH_CHILD (die, c, unmark_dies (c));
8584 /* Clear the marks for a die, its children and referred dies. */
8586 static void
8587 unmark_all_dies (dw_die_ref die)
8589 dw_die_ref c;
8590 dw_attr_node *a;
8591 unsigned ix;
8593 if (!die->die_mark)
8594 return;
8595 die->die_mark = 0;
8597 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8599 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8600 if (AT_class (a) == dw_val_class_die_ref)
8601 unmark_all_dies (AT_ref (a));
8604 /* Calculate if the entry should appear in the final output file. It may be
8605 from a pruned a type. */
8607 static bool
8608 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8610 /* By limiting gnu pubnames to definitions only, gold can generate a
8611 gdb index without entries for declarations, which don't include
8612 enough information to be useful. */
8613 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8614 return false;
8616 if (table == pubname_table)
8618 /* Enumerator names are part of the pubname table, but the
8619 parent DW_TAG_enumeration_type die may have been pruned.
8620 Don't output them if that is the case. */
8621 if (p->die->die_tag == DW_TAG_enumerator &&
8622 (p->die->die_parent == NULL
8623 || !p->die->die_parent->die_perennial_p))
8624 return false;
8626 /* Everything else in the pubname table is included. */
8627 return true;
8630 /* The pubtypes table shouldn't include types that have been
8631 pruned. */
8632 return (p->die->die_offset != 0
8633 || !flag_eliminate_unused_debug_types);
8636 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8637 generated for the compilation unit. */
8639 static unsigned long
8640 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8642 unsigned long size;
8643 unsigned i;
8644 pubname_entry *p;
8645 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8647 size = DWARF_PUBNAMES_HEADER_SIZE;
8648 FOR_EACH_VEC_ELT (*names, i, p)
8649 if (include_pubname_in_output (names, p))
8650 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8652 size += DWARF_OFFSET_SIZE;
8653 return size;
8656 /* Return the size of the information in the .debug_aranges section. */
8658 static unsigned long
8659 size_of_aranges (void)
8661 unsigned long size;
8663 size = DWARF_ARANGES_HEADER_SIZE;
8665 /* Count the address/length pair for this compilation unit. */
8666 if (text_section_used)
8667 size += 2 * DWARF2_ADDR_SIZE;
8668 if (cold_text_section_used)
8669 size += 2 * DWARF2_ADDR_SIZE;
8670 if (have_multiple_function_sections)
8672 unsigned fde_idx;
8673 dw_fde_ref fde;
8675 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8677 if (DECL_IGNORED_P (fde->decl))
8678 continue;
8679 if (!fde->in_std_section)
8680 size += 2 * DWARF2_ADDR_SIZE;
8681 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8682 size += 2 * DWARF2_ADDR_SIZE;
8686 /* Count the two zero words used to terminated the address range table. */
8687 size += 2 * DWARF2_ADDR_SIZE;
8688 return size;
8691 /* Select the encoding of an attribute value. */
8693 static enum dwarf_form
8694 value_format (dw_attr_node *a)
8696 switch (AT_class (a))
8698 case dw_val_class_addr:
8699 /* Only very few attributes allow DW_FORM_addr. */
8700 switch (a->dw_attr)
8702 case DW_AT_low_pc:
8703 case DW_AT_high_pc:
8704 case DW_AT_entry_pc:
8705 case DW_AT_trampoline:
8706 return (AT_index (a) == NOT_INDEXED
8707 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8708 default:
8709 break;
8711 switch (DWARF2_ADDR_SIZE)
8713 case 1:
8714 return DW_FORM_data1;
8715 case 2:
8716 return DW_FORM_data2;
8717 case 4:
8718 return DW_FORM_data4;
8719 case 8:
8720 return DW_FORM_data8;
8721 default:
8722 gcc_unreachable ();
8724 case dw_val_class_range_list:
8725 case dw_val_class_loc_list:
8726 if (dwarf_version >= 4)
8727 return DW_FORM_sec_offset;
8728 /* FALLTHRU */
8729 case dw_val_class_vms_delta:
8730 case dw_val_class_offset:
8731 switch (DWARF_OFFSET_SIZE)
8733 case 4:
8734 return DW_FORM_data4;
8735 case 8:
8736 return DW_FORM_data8;
8737 default:
8738 gcc_unreachable ();
8740 case dw_val_class_loc:
8741 if (dwarf_version >= 4)
8742 return DW_FORM_exprloc;
8743 switch (constant_size (size_of_locs (AT_loc (a))))
8745 case 1:
8746 return DW_FORM_block1;
8747 case 2:
8748 return DW_FORM_block2;
8749 case 4:
8750 return DW_FORM_block4;
8751 default:
8752 gcc_unreachable ();
8754 case dw_val_class_const:
8755 return DW_FORM_sdata;
8756 case dw_val_class_unsigned_const:
8757 switch (constant_size (AT_unsigned (a)))
8759 case 1:
8760 return DW_FORM_data1;
8761 case 2:
8762 return DW_FORM_data2;
8763 case 4:
8764 /* In DWARF3 DW_AT_data_member_location with
8765 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8766 constant, so we need to use DW_FORM_udata if we need
8767 a large constant. */
8768 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8769 return DW_FORM_udata;
8770 return DW_FORM_data4;
8771 case 8:
8772 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8773 return DW_FORM_udata;
8774 return DW_FORM_data8;
8775 default:
8776 gcc_unreachable ();
8778 case dw_val_class_const_double:
8779 switch (HOST_BITS_PER_WIDE_INT)
8781 case 8:
8782 return DW_FORM_data2;
8783 case 16:
8784 return DW_FORM_data4;
8785 case 32:
8786 return DW_FORM_data8;
8787 case 64:
8788 default:
8789 return DW_FORM_block1;
8791 case dw_val_class_wide_int:
8792 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8794 case 8:
8795 return DW_FORM_data1;
8796 case 16:
8797 return DW_FORM_data2;
8798 case 32:
8799 return DW_FORM_data4;
8800 case 64:
8801 return DW_FORM_data8;
8802 default:
8803 return DW_FORM_block1;
8805 case dw_val_class_vec:
8806 switch (constant_size (a->dw_attr_val.v.val_vec.length
8807 * a->dw_attr_val.v.val_vec.elt_size))
8809 case 1:
8810 return DW_FORM_block1;
8811 case 2:
8812 return DW_FORM_block2;
8813 case 4:
8814 return DW_FORM_block4;
8815 default:
8816 gcc_unreachable ();
8818 case dw_val_class_flag:
8819 if (dwarf_version >= 4)
8821 /* Currently all add_AT_flag calls pass in 1 as last argument,
8822 so DW_FORM_flag_present can be used. If that ever changes,
8823 we'll need to use DW_FORM_flag and have some optimization
8824 in build_abbrev_table that will change those to
8825 DW_FORM_flag_present if it is set to 1 in all DIEs using
8826 the same abbrev entry. */
8827 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8828 return DW_FORM_flag_present;
8830 return DW_FORM_flag;
8831 case dw_val_class_die_ref:
8832 if (AT_ref_external (a))
8833 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8834 else
8835 return DW_FORM_ref;
8836 case dw_val_class_fde_ref:
8837 return DW_FORM_data;
8838 case dw_val_class_lbl_id:
8839 return (AT_index (a) == NOT_INDEXED
8840 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8841 case dw_val_class_lineptr:
8842 case dw_val_class_macptr:
8843 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8844 case dw_val_class_str:
8845 return AT_string_form (a);
8846 case dw_val_class_file:
8847 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8849 case 1:
8850 return DW_FORM_data1;
8851 case 2:
8852 return DW_FORM_data2;
8853 case 4:
8854 return DW_FORM_data4;
8855 default:
8856 gcc_unreachable ();
8859 case dw_val_class_data8:
8860 return DW_FORM_data8;
8862 case dw_val_class_high_pc:
8863 switch (DWARF2_ADDR_SIZE)
8865 case 1:
8866 return DW_FORM_data1;
8867 case 2:
8868 return DW_FORM_data2;
8869 case 4:
8870 return DW_FORM_data4;
8871 case 8:
8872 return DW_FORM_data8;
8873 default:
8874 gcc_unreachable ();
8877 case dw_val_class_discr_value:
8878 return (a->dw_attr_val.v.val_discr_value.pos
8879 ? DW_FORM_udata
8880 : DW_FORM_sdata);
8881 case dw_val_class_discr_list:
8882 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8884 case 1:
8885 return DW_FORM_block1;
8886 case 2:
8887 return DW_FORM_block2;
8888 case 4:
8889 return DW_FORM_block4;
8890 default:
8891 gcc_unreachable ();
8894 default:
8895 gcc_unreachable ();
8899 /* Output the encoding of an attribute value. */
8901 static void
8902 output_value_format (dw_attr_node *a)
8904 enum dwarf_form form = value_format (a);
8906 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8909 /* Given a die and id, produce the appropriate abbreviations. */
8911 static void
8912 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8914 unsigned ix;
8915 dw_attr_node *a_attr;
8917 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8918 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8919 dwarf_tag_name (abbrev->die_tag));
8921 if (abbrev->die_child != NULL)
8922 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8923 else
8924 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8926 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8928 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8929 dwarf_attr_name (a_attr->dw_attr));
8930 output_value_format (a_attr);
8933 dw2_asm_output_data (1, 0, NULL);
8934 dw2_asm_output_data (1, 0, NULL);
8938 /* Output the .debug_abbrev section which defines the DIE abbreviation
8939 table. */
8941 static void
8942 output_abbrev_section (void)
8944 unsigned long abbrev_id;
8946 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8947 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8949 /* Terminate the table. */
8950 dw2_asm_output_data (1, 0, NULL);
8953 /* Output a symbol we can use to refer to this DIE from another CU. */
8955 static inline void
8956 output_die_symbol (dw_die_ref die)
8958 const char *sym = die->die_id.die_symbol;
8960 gcc_assert (!die->comdat_type_p);
8962 if (sym == 0)
8963 return;
8965 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8966 /* We make these global, not weak; if the target doesn't support
8967 .linkonce, it doesn't support combining the sections, so debugging
8968 will break. */
8969 targetm.asm_out.globalize_label (asm_out_file, sym);
8971 ASM_OUTPUT_LABEL (asm_out_file, sym);
8974 /* Return a new location list, given the begin and end range, and the
8975 expression. */
8977 static inline dw_loc_list_ref
8978 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8979 const char *section)
8981 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8983 retlist->begin = begin;
8984 retlist->begin_entry = NULL;
8985 retlist->end = end;
8986 retlist->expr = expr;
8987 retlist->section = section;
8989 return retlist;
8992 /* Generate a new internal symbol for this location list node, if it
8993 hasn't got one yet. */
8995 static inline void
8996 gen_llsym (dw_loc_list_ref list)
8998 gcc_assert (!list->ll_symbol);
8999 list->ll_symbol = gen_internal_sym ("LLST");
9002 /* Output the location list given to us. */
9004 static void
9005 output_loc_list (dw_loc_list_ref list_head)
9007 dw_loc_list_ref curr = list_head;
9009 if (list_head->emitted)
9010 return;
9011 list_head->emitted = true;
9013 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9015 /* Walk the location list, and output each range + expression. */
9016 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9018 unsigned long size;
9019 /* Don't output an entry that starts and ends at the same address. */
9020 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9021 continue;
9022 size = size_of_locs (curr->expr);
9023 /* If the expression is too large, drop it on the floor. We could
9024 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9025 in the expression, but >= 64KB expressions for a single value
9026 in a single range are unlikely very useful. */
9027 if (size > 0xffff)
9028 continue;
9029 if (dwarf_split_debug_info)
9031 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9032 "Location list start/length entry (%s)",
9033 list_head->ll_symbol);
9034 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9035 "Location list range start index (%s)",
9036 curr->begin);
9037 /* The length field is 4 bytes. If we ever need to support
9038 an 8-byte length, we can add a new DW_LLE code or fall back
9039 to DW_LLE_GNU_start_end_entry. */
9040 dw2_asm_output_delta (4, curr->end, curr->begin,
9041 "Location list range length (%s)",
9042 list_head->ll_symbol);
9044 else if (!have_multiple_function_sections)
9046 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9047 "Location list begin address (%s)",
9048 list_head->ll_symbol);
9049 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9050 "Location list end address (%s)",
9051 list_head->ll_symbol);
9053 else
9055 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9056 "Location list begin address (%s)",
9057 list_head->ll_symbol);
9058 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9059 "Location list end address (%s)",
9060 list_head->ll_symbol);
9063 /* Output the block length for this list of location operations. */
9064 gcc_assert (size <= 0xffff);
9065 dw2_asm_output_data (2, size, "%s", "Location expression size");
9067 output_loc_sequence (curr->expr, -1);
9070 if (dwarf_split_debug_info)
9071 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9072 "Location list terminator (%s)",
9073 list_head->ll_symbol);
9074 else
9076 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9077 "Location list terminator begin (%s)",
9078 list_head->ll_symbol);
9079 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9080 "Location list terminator end (%s)",
9081 list_head->ll_symbol);
9085 /* Output a range_list offset into the debug_range section. Emit a
9086 relocated reference if val_entry is NULL, otherwise, emit an
9087 indirect reference. */
9089 static void
9090 output_range_list_offset (dw_attr_node *a)
9092 const char *name = dwarf_attr_name (a->dw_attr);
9094 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9096 char *p = strchr (ranges_section_label, '\0');
9097 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9098 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9099 debug_ranges_section, "%s", name);
9100 *p = '\0';
9102 else
9103 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9104 "%s (offset from %s)", name, ranges_section_label);
9107 /* Output the offset into the debug_loc section. */
9109 static void
9110 output_loc_list_offset (dw_attr_node *a)
9112 char *sym = AT_loc_list (a)->ll_symbol;
9114 gcc_assert (sym);
9115 if (dwarf_split_debug_info)
9116 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9117 "%s", dwarf_attr_name (a->dw_attr));
9118 else
9119 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9120 "%s", dwarf_attr_name (a->dw_attr));
9123 /* Output an attribute's index or value appropriately. */
9125 static void
9126 output_attr_index_or_value (dw_attr_node *a)
9128 const char *name = dwarf_attr_name (a->dw_attr);
9130 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9132 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9133 return;
9135 switch (AT_class (a))
9137 case dw_val_class_addr:
9138 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9139 break;
9140 case dw_val_class_high_pc:
9141 case dw_val_class_lbl_id:
9142 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9143 break;
9144 case dw_val_class_loc_list:
9145 output_loc_list_offset (a);
9146 break;
9147 default:
9148 gcc_unreachable ();
9152 /* Output a type signature. */
9154 static inline void
9155 output_signature (const char *sig, const char *name)
9157 int i;
9159 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9160 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9163 /* Output a discriminant value. */
9165 static inline void
9166 output_discr_value (dw_discr_value *discr_value, const char *name)
9168 if (discr_value->pos)
9169 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9170 else
9171 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9174 /* Output the DIE and its attributes. Called recursively to generate
9175 the definitions of each child DIE. */
9177 static void
9178 output_die (dw_die_ref die)
9180 dw_attr_node *a;
9181 dw_die_ref c;
9182 unsigned long size;
9183 unsigned ix;
9185 /* If someone in another CU might refer to us, set up a symbol for
9186 them to point to. */
9187 if (! die->comdat_type_p && die->die_id.die_symbol)
9188 output_die_symbol (die);
9190 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9191 (unsigned long)die->die_offset,
9192 dwarf_tag_name (die->die_tag));
9194 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9196 const char *name = dwarf_attr_name (a->dw_attr);
9198 switch (AT_class (a))
9200 case dw_val_class_addr:
9201 output_attr_index_or_value (a);
9202 break;
9204 case dw_val_class_offset:
9205 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9206 "%s", name);
9207 break;
9209 case dw_val_class_range_list:
9210 output_range_list_offset (a);
9211 break;
9213 case dw_val_class_loc:
9214 size = size_of_locs (AT_loc (a));
9216 /* Output the block length for this list of location operations. */
9217 if (dwarf_version >= 4)
9218 dw2_asm_output_data_uleb128 (size, "%s", name);
9219 else
9220 dw2_asm_output_data (constant_size (size), size, "%s", name);
9222 output_loc_sequence (AT_loc (a), -1);
9223 break;
9225 case dw_val_class_const:
9226 /* ??? It would be slightly more efficient to use a scheme like is
9227 used for unsigned constants below, but gdb 4.x does not sign
9228 extend. Gdb 5.x does sign extend. */
9229 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9230 break;
9232 case dw_val_class_unsigned_const:
9234 int csize = constant_size (AT_unsigned (a));
9235 if (dwarf_version == 3
9236 && a->dw_attr == DW_AT_data_member_location
9237 && csize >= 4)
9238 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9239 else
9240 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9242 break;
9244 case dw_val_class_const_double:
9246 unsigned HOST_WIDE_INT first, second;
9248 if (HOST_BITS_PER_WIDE_INT >= 64)
9249 dw2_asm_output_data (1,
9250 HOST_BITS_PER_DOUBLE_INT
9251 / HOST_BITS_PER_CHAR,
9252 NULL);
9254 if (WORDS_BIG_ENDIAN)
9256 first = a->dw_attr_val.v.val_double.high;
9257 second = a->dw_attr_val.v.val_double.low;
9259 else
9261 first = a->dw_attr_val.v.val_double.low;
9262 second = a->dw_attr_val.v.val_double.high;
9265 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9266 first, "%s", name);
9267 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9268 second, NULL);
9270 break;
9272 case dw_val_class_wide_int:
9274 int i;
9275 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9276 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9277 if (len * HOST_BITS_PER_WIDE_INT > 64)
9278 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9279 NULL);
9281 if (WORDS_BIG_ENDIAN)
9282 for (i = len - 1; i >= 0; --i)
9284 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9285 "%s", name);
9286 name = "";
9288 else
9289 for (i = 0; i < len; ++i)
9291 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9292 "%s", name);
9293 name = "";
9296 break;
9298 case dw_val_class_vec:
9300 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9301 unsigned int len = a->dw_attr_val.v.val_vec.length;
9302 unsigned int i;
9303 unsigned char *p;
9305 dw2_asm_output_data (constant_size (len * elt_size),
9306 len * elt_size, "%s", name);
9307 if (elt_size > sizeof (HOST_WIDE_INT))
9309 elt_size /= 2;
9310 len *= 2;
9312 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9313 i < len;
9314 i++, p += elt_size)
9315 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9316 "fp or vector constant word %u", i);
9317 break;
9320 case dw_val_class_flag:
9321 if (dwarf_version >= 4)
9323 /* Currently all add_AT_flag calls pass in 1 as last argument,
9324 so DW_FORM_flag_present can be used. If that ever changes,
9325 we'll need to use DW_FORM_flag and have some optimization
9326 in build_abbrev_table that will change those to
9327 DW_FORM_flag_present if it is set to 1 in all DIEs using
9328 the same abbrev entry. */
9329 gcc_assert (AT_flag (a) == 1);
9330 if (flag_debug_asm)
9331 fprintf (asm_out_file, "\t\t\t%s %s\n",
9332 ASM_COMMENT_START, name);
9333 break;
9335 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9336 break;
9338 case dw_val_class_loc_list:
9339 output_attr_index_or_value (a);
9340 break;
9342 case dw_val_class_die_ref:
9343 if (AT_ref_external (a))
9345 if (AT_ref (a)->comdat_type_p)
9347 comdat_type_node *type_node =
9348 AT_ref (a)->die_id.die_type_node;
9350 gcc_assert (type_node);
9351 output_signature (type_node->signature, name);
9353 else
9355 const char *sym = AT_ref (a)->die_id.die_symbol;
9356 int size;
9358 gcc_assert (sym);
9359 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9360 length, whereas in DWARF3 it's always sized as an
9361 offset. */
9362 if (dwarf_version == 2)
9363 size = DWARF2_ADDR_SIZE;
9364 else
9365 size = DWARF_OFFSET_SIZE;
9366 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9367 name);
9370 else
9372 gcc_assert (AT_ref (a)->die_offset);
9373 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9374 "%s", name);
9376 break;
9378 case dw_val_class_fde_ref:
9380 char l1[20];
9382 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9383 a->dw_attr_val.v.val_fde_index * 2);
9384 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9385 "%s", name);
9387 break;
9389 case dw_val_class_vms_delta:
9390 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9391 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9392 AT_vms_delta2 (a), AT_vms_delta1 (a),
9393 "%s", name);
9394 #else
9395 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9396 AT_vms_delta2 (a), AT_vms_delta1 (a),
9397 "%s", name);
9398 #endif
9399 break;
9401 case dw_val_class_lbl_id:
9402 output_attr_index_or_value (a);
9403 break;
9405 case dw_val_class_lineptr:
9406 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9407 debug_line_section, "%s", name);
9408 break;
9410 case dw_val_class_macptr:
9411 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9412 debug_macinfo_section, "%s", name);
9413 break;
9415 case dw_val_class_str:
9416 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9417 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9418 a->dw_attr_val.v.val_str->label,
9419 debug_str_section,
9420 "%s: \"%s\"", name, AT_string (a));
9421 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9422 dw2_asm_output_data_uleb128 (AT_index (a),
9423 "%s: \"%s\"", name, AT_string (a));
9424 else
9425 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9426 break;
9428 case dw_val_class_file:
9430 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9432 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9433 a->dw_attr_val.v.val_file->filename);
9434 break;
9437 case dw_val_class_data8:
9439 int i;
9441 for (i = 0; i < 8; i++)
9442 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9443 i == 0 ? "%s" : NULL, name);
9444 break;
9447 case dw_val_class_high_pc:
9448 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9449 get_AT_low_pc (die), "DW_AT_high_pc");
9450 break;
9452 case dw_val_class_discr_value:
9453 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9454 break;
9456 case dw_val_class_discr_list:
9458 dw_discr_list_ref list = AT_discr_list (a);
9459 const int size = size_of_discr_list (list);
9461 /* This is a block, so output its length first. */
9462 dw2_asm_output_data (constant_size (size), size,
9463 "%s: block size", name);
9465 for (; list != NULL; list = list->dw_discr_next)
9467 /* One byte for the discriminant value descriptor, and then as
9468 many LEB128 numbers as required. */
9469 if (list->dw_discr_range)
9470 dw2_asm_output_data (1, DW_DSC_range,
9471 "%s: DW_DSC_range", name);
9472 else
9473 dw2_asm_output_data (1, DW_DSC_label,
9474 "%s: DW_DSC_label", name);
9476 output_discr_value (&list->dw_discr_lower_bound, name);
9477 if (list->dw_discr_range)
9478 output_discr_value (&list->dw_discr_upper_bound, name);
9480 break;
9483 default:
9484 gcc_unreachable ();
9488 FOR_EACH_CHILD (die, c, output_die (c));
9490 /* Add null byte to terminate sibling list. */
9491 if (die->die_child != NULL)
9492 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9493 (unsigned long) die->die_offset);
9496 /* Output the compilation unit that appears at the beginning of the
9497 .debug_info section, and precedes the DIE descriptions. */
9499 static void
9500 output_compilation_unit_header (void)
9502 /* We don't support actual DWARFv5 units yet, we just use some
9503 DWARFv5 draft DIE tags in DWARFv4 format. */
9504 int ver = dwarf_version < 5 ? dwarf_version : 4;
9506 if (!XCOFF_DEBUGGING_INFO)
9508 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9509 dw2_asm_output_data (4, 0xffffffff,
9510 "Initial length escape value indicating 64-bit DWARF extension");
9511 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9512 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9513 "Length of Compilation Unit Info");
9516 dw2_asm_output_data (2, ver, "DWARF version number");
9517 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9518 debug_abbrev_section,
9519 "Offset Into Abbrev. Section");
9520 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9523 /* Output the compilation unit DIE and its children. */
9525 static void
9526 output_comp_unit (dw_die_ref die, int output_if_empty)
9528 const char *secname, *oldsym;
9529 char *tmp;
9531 /* Unless we are outputting main CU, we may throw away empty ones. */
9532 if (!output_if_empty && die->die_child == NULL)
9533 return;
9535 /* Even if there are no children of this DIE, we must output the information
9536 about the compilation unit. Otherwise, on an empty translation unit, we
9537 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9538 will then complain when examining the file. First mark all the DIEs in
9539 this CU so we know which get local refs. */
9540 mark_dies (die);
9542 external_ref_hash_type *extern_map = optimize_external_refs (die);
9544 build_abbrev_table (die, extern_map);
9546 delete extern_map;
9548 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9549 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9550 calc_die_sizes (die);
9552 oldsym = die->die_id.die_symbol;
9553 if (oldsym)
9555 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9557 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9558 secname = tmp;
9559 die->die_id.die_symbol = NULL;
9560 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9562 else
9564 switch_to_section (debug_info_section);
9565 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9566 info_section_emitted = true;
9569 /* Output debugging information. */
9570 output_compilation_unit_header ();
9571 output_die (die);
9573 /* Leave the marks on the main CU, so we can check them in
9574 output_pubnames. */
9575 if (oldsym)
9577 unmark_dies (die);
9578 die->die_id.die_symbol = oldsym;
9582 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9583 and .debug_pubtypes. This is configured per-target, but can be
9584 overridden by the -gpubnames or -gno-pubnames options. */
9586 static inline bool
9587 want_pubnames (void)
9589 if (debug_info_level <= DINFO_LEVEL_TERSE)
9590 return false;
9591 if (debug_generate_pub_sections != -1)
9592 return debug_generate_pub_sections;
9593 return targetm.want_debug_pub_sections;
9596 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9598 static void
9599 add_AT_pubnames (dw_die_ref die)
9601 if (want_pubnames ())
9602 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9605 /* Add a string attribute value to a skeleton DIE. */
9607 static inline void
9608 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9609 const char *str)
9611 dw_attr_node attr;
9612 struct indirect_string_node *node;
9614 if (! skeleton_debug_str_hash)
9615 skeleton_debug_str_hash
9616 = hash_table<indirect_string_hasher>::create_ggc (10);
9618 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9619 find_string_form (node);
9620 if (node->form == DW_FORM_GNU_str_index)
9621 node->form = DW_FORM_strp;
9623 attr.dw_attr = attr_kind;
9624 attr.dw_attr_val.val_class = dw_val_class_str;
9625 attr.dw_attr_val.val_entry = NULL;
9626 attr.dw_attr_val.v.val_str = node;
9627 add_dwarf_attr (die, &attr);
9630 /* Helper function to generate top-level dies for skeleton debug_info and
9631 debug_types. */
9633 static void
9634 add_top_level_skeleton_die_attrs (dw_die_ref die)
9636 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9637 const char *comp_dir = comp_dir_string ();
9639 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9640 if (comp_dir != NULL)
9641 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9642 add_AT_pubnames (die);
9643 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9646 /* Output skeleton debug sections that point to the dwo file. */
9648 static void
9649 output_skeleton_debug_sections (dw_die_ref comp_unit)
9651 /* We don't support actual DWARFv5 units yet, we just use some
9652 DWARFv5 draft DIE tags in DWARFv4 format. */
9653 int ver = dwarf_version < 5 ? dwarf_version : 4;
9655 /* These attributes will be found in the full debug_info section. */
9656 remove_AT (comp_unit, DW_AT_producer);
9657 remove_AT (comp_unit, DW_AT_language);
9659 switch_to_section (debug_skeleton_info_section);
9660 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9662 /* Produce the skeleton compilation-unit header. This one differs enough from
9663 a normal CU header that it's better not to call output_compilation_unit
9664 header. */
9665 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9666 dw2_asm_output_data (4, 0xffffffff,
9667 "Initial length escape value indicating 64-bit DWARF extension");
9669 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9670 DWARF_COMPILE_UNIT_HEADER_SIZE
9671 - DWARF_INITIAL_LENGTH_SIZE
9672 + size_of_die (comp_unit),
9673 "Length of Compilation Unit Info");
9674 dw2_asm_output_data (2, ver, "DWARF version number");
9675 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9676 debug_abbrev_section,
9677 "Offset Into Abbrev. Section");
9678 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9680 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9681 output_die (comp_unit);
9683 /* Build the skeleton debug_abbrev section. */
9684 switch_to_section (debug_skeleton_abbrev_section);
9685 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9687 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9689 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9692 /* Output a comdat type unit DIE and its children. */
9694 static void
9695 output_comdat_type_unit (comdat_type_node *node)
9697 const char *secname;
9698 char *tmp;
9699 int i;
9700 #if defined (OBJECT_FORMAT_ELF)
9701 tree comdat_key;
9702 #endif
9704 /* First mark all the DIEs in this CU so we know which get local refs. */
9705 mark_dies (node->root_die);
9707 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9709 build_abbrev_table (node->root_die, extern_map);
9711 delete extern_map;
9712 extern_map = NULL;
9714 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9715 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9716 calc_die_sizes (node->root_die);
9718 #if defined (OBJECT_FORMAT_ELF)
9719 if (!dwarf_split_debug_info)
9720 secname = ".debug_types";
9721 else
9722 secname = ".debug_types.dwo";
9724 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9725 sprintf (tmp, "wt.");
9726 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9727 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9728 comdat_key = get_identifier (tmp);
9729 targetm.asm_out.named_section (secname,
9730 SECTION_DEBUG | SECTION_LINKONCE,
9731 comdat_key);
9732 #else
9733 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9734 sprintf (tmp, ".gnu.linkonce.wt.");
9735 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9736 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9737 secname = tmp;
9738 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9739 #endif
9741 /* Output debugging information. */
9742 output_compilation_unit_header ();
9743 output_signature (node->signature, "Type Signature");
9744 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9745 "Offset to Type DIE");
9746 output_die (node->root_die);
9748 unmark_dies (node->root_die);
9751 /* Return the DWARF2/3 pubname associated with a decl. */
9753 static const char *
9754 dwarf2_name (tree decl, int scope)
9756 if (DECL_NAMELESS (decl))
9757 return NULL;
9758 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9761 /* Add a new entry to .debug_pubnames if appropriate. */
9763 static void
9764 add_pubname_string (const char *str, dw_die_ref die)
9766 pubname_entry e;
9768 e.die = die;
9769 e.name = xstrdup (str);
9770 vec_safe_push (pubname_table, e);
9773 static void
9774 add_pubname (tree decl, dw_die_ref die)
9776 if (!want_pubnames ())
9777 return;
9779 /* Don't add items to the table when we expect that the consumer will have
9780 just read the enclosing die. For example, if the consumer is looking at a
9781 class_member, it will either be inside the class already, or will have just
9782 looked up the class to find the member. Either way, searching the class is
9783 faster than searching the index. */
9784 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9785 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9787 const char *name = dwarf2_name (decl, 1);
9789 if (name)
9790 add_pubname_string (name, die);
9794 /* Add an enumerator to the pubnames section. */
9796 static void
9797 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9799 pubname_entry e;
9801 gcc_assert (scope_name);
9802 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9803 e.die = die;
9804 vec_safe_push (pubname_table, e);
9807 /* Add a new entry to .debug_pubtypes if appropriate. */
9809 static void
9810 add_pubtype (tree decl, dw_die_ref die)
9812 pubname_entry e;
9814 if (!want_pubnames ())
9815 return;
9817 if ((TREE_PUBLIC (decl)
9818 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9819 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9821 tree scope = NULL;
9822 const char *scope_name = "";
9823 const char *sep = is_cxx () ? "::" : ".";
9824 const char *name;
9826 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9827 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9829 scope_name = lang_hooks.dwarf_name (scope, 1);
9830 if (scope_name != NULL && scope_name[0] != '\0')
9831 scope_name = concat (scope_name, sep, NULL);
9832 else
9833 scope_name = "";
9836 if (TYPE_P (decl))
9837 name = type_tag (decl);
9838 else
9839 name = lang_hooks.dwarf_name (decl, 1);
9841 /* If we don't have a name for the type, there's no point in adding
9842 it to the table. */
9843 if (name != NULL && name[0] != '\0')
9845 e.die = die;
9846 e.name = concat (scope_name, name, NULL);
9847 vec_safe_push (pubtype_table, e);
9850 /* Although it might be more consistent to add the pubinfo for the
9851 enumerators as their dies are created, they should only be added if the
9852 enum type meets the criteria above. So rather than re-check the parent
9853 enum type whenever an enumerator die is created, just output them all
9854 here. This isn't protected by the name conditional because anonymous
9855 enums don't have names. */
9856 if (die->die_tag == DW_TAG_enumeration_type)
9858 dw_die_ref c;
9860 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9865 /* Output a single entry in the pubnames table. */
9867 static void
9868 output_pubname (dw_offset die_offset, pubname_entry *entry)
9870 dw_die_ref die = entry->die;
9871 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9873 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9875 if (debug_generate_pub_sections == 2)
9877 /* This logic follows gdb's method for determining the value of the flag
9878 byte. */
9879 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9880 switch (die->die_tag)
9882 case DW_TAG_typedef:
9883 case DW_TAG_base_type:
9884 case DW_TAG_subrange_type:
9885 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9886 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9887 break;
9888 case DW_TAG_enumerator:
9889 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9890 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9891 if (!is_cxx () && !is_java ())
9892 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9893 break;
9894 case DW_TAG_subprogram:
9895 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9896 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9897 if (!is_ada ())
9898 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9899 break;
9900 case DW_TAG_constant:
9901 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9902 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9903 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9904 break;
9905 case DW_TAG_variable:
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_namespace:
9911 case DW_TAG_imported_declaration:
9912 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9913 break;
9914 case DW_TAG_class_type:
9915 case DW_TAG_interface_type:
9916 case DW_TAG_structure_type:
9917 case DW_TAG_union_type:
9918 case DW_TAG_enumeration_type:
9919 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9920 if (!is_cxx () && !is_java ())
9921 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9922 break;
9923 default:
9924 /* An unusual tag. Leave the flag-byte empty. */
9925 break;
9927 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9928 "GDB-index flags");
9931 dw2_asm_output_nstring (entry->name, -1, "external name");
9935 /* Output the public names table used to speed up access to externally
9936 visible names; or the public types table used to find type definitions. */
9938 static void
9939 output_pubnames (vec<pubname_entry, va_gc> *names)
9941 unsigned i;
9942 unsigned long pubnames_length = size_of_pubnames (names);
9943 pubname_entry *pub;
9945 if (!XCOFF_DEBUGGING_INFO)
9947 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9948 dw2_asm_output_data (4, 0xffffffff,
9949 "Initial length escape value indicating 64-bit DWARF extension");
9950 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9951 "Pub Info Length");
9954 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9955 dw2_asm_output_data (2, 2, "DWARF Version");
9957 if (dwarf_split_debug_info)
9958 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9959 debug_skeleton_info_section,
9960 "Offset of Compilation Unit Info");
9961 else
9962 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9963 debug_info_section,
9964 "Offset of Compilation Unit Info");
9965 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9966 "Compilation Unit Length");
9968 FOR_EACH_VEC_ELT (*names, i, pub)
9970 if (include_pubname_in_output (names, pub))
9972 dw_offset die_offset = pub->die->die_offset;
9974 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9975 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9976 gcc_assert (pub->die->die_mark);
9978 /* If we're putting types in their own .debug_types sections,
9979 the .debug_pubtypes table will still point to the compile
9980 unit (not the type unit), so we want to use the offset of
9981 the skeleton DIE (if there is one). */
9982 if (pub->die->comdat_type_p && names == pubtype_table)
9984 comdat_type_node *type_node = pub->die->die_id.die_type_node;
9986 if (type_node != NULL)
9987 die_offset = (type_node->skeleton_die != NULL
9988 ? type_node->skeleton_die->die_offset
9989 : comp_unit_die ()->die_offset);
9992 output_pubname (die_offset, pub);
9996 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9999 /* Output public names and types tables if necessary. */
10001 static void
10002 output_pubtables (void)
10004 if (!want_pubnames () || !info_section_emitted)
10005 return;
10007 switch_to_section (debug_pubnames_section);
10008 output_pubnames (pubname_table);
10009 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10010 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10011 simply won't look for the section. */
10012 switch_to_section (debug_pubtypes_section);
10013 output_pubnames (pubtype_table);
10017 /* Output the information that goes into the .debug_aranges table.
10018 Namely, define the beginning and ending address range of the
10019 text section generated for this compilation unit. */
10021 static void
10022 output_aranges (void)
10024 unsigned i;
10025 unsigned long aranges_length = size_of_aranges ();
10027 if (!XCOFF_DEBUGGING_INFO)
10029 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10030 dw2_asm_output_data (4, 0xffffffff,
10031 "Initial length escape value indicating 64-bit DWARF extension");
10032 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10033 "Length of Address Ranges Info");
10036 /* Version number for aranges is still 2, even up to DWARF5. */
10037 dw2_asm_output_data (2, 2, "DWARF Version");
10038 if (dwarf_split_debug_info)
10039 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10040 debug_skeleton_info_section,
10041 "Offset of Compilation Unit Info");
10042 else
10043 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10044 debug_info_section,
10045 "Offset of Compilation Unit Info");
10046 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10047 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10049 /* We need to align to twice the pointer size here. */
10050 if (DWARF_ARANGES_PAD_SIZE)
10052 /* Pad using a 2 byte words so that padding is correct for any
10053 pointer size. */
10054 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10055 2 * DWARF2_ADDR_SIZE);
10056 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10057 dw2_asm_output_data (2, 0, NULL);
10060 /* It is necessary not to output these entries if the sections were
10061 not used; if the sections were not used, the length will be 0 and
10062 the address may end up as 0 if the section is discarded by ld
10063 --gc-sections, leaving an invalid (0, 0) entry that can be
10064 confused with the terminator. */
10065 if (text_section_used)
10067 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10068 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10069 text_section_label, "Length");
10071 if (cold_text_section_used)
10073 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10074 "Address");
10075 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10076 cold_text_section_label, "Length");
10079 if (have_multiple_function_sections)
10081 unsigned fde_idx;
10082 dw_fde_ref fde;
10084 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10086 if (DECL_IGNORED_P (fde->decl))
10087 continue;
10088 if (!fde->in_std_section)
10090 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10091 "Address");
10092 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10093 fde->dw_fde_begin, "Length");
10095 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10097 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10098 "Address");
10099 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10100 fde->dw_fde_second_begin, "Length");
10105 /* Output the terminator words. */
10106 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10107 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10110 /* Add a new entry to .debug_ranges. Return the offset at which it
10111 was placed. */
10113 static unsigned int
10114 add_ranges_num (int num)
10116 unsigned int in_use = ranges_table_in_use;
10118 if (in_use == ranges_table_allocated)
10120 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10121 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10122 ranges_table_allocated);
10123 memset (ranges_table + ranges_table_in_use, 0,
10124 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10127 ranges_table[in_use].num = num;
10128 ranges_table_in_use = in_use + 1;
10130 return in_use * 2 * DWARF2_ADDR_SIZE;
10133 /* Add a new entry to .debug_ranges corresponding to a block, or a
10134 range terminator if BLOCK is NULL. */
10136 static unsigned int
10137 add_ranges (const_tree block)
10139 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10142 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10143 When using dwarf_split_debug_info, address attributes in dies destined
10144 for the final executable should be direct references--setting the
10145 parameter force_direct ensures this behavior. */
10147 static void
10148 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10149 bool *added, bool force_direct)
10151 unsigned int in_use = ranges_by_label_in_use;
10152 unsigned int offset;
10154 if (in_use == ranges_by_label_allocated)
10156 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10157 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10158 ranges_by_label_allocated);
10159 memset (ranges_by_label + ranges_by_label_in_use, 0,
10160 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10163 ranges_by_label[in_use].begin = begin;
10164 ranges_by_label[in_use].end = end;
10165 ranges_by_label_in_use = in_use + 1;
10167 offset = add_ranges_num (-(int)in_use - 1);
10168 if (!*added)
10170 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10171 *added = true;
10175 static void
10176 output_ranges (void)
10178 unsigned i;
10179 static const char *const start_fmt = "Offset %#x";
10180 const char *fmt = start_fmt;
10182 for (i = 0; i < ranges_table_in_use; i++)
10184 int block_num = ranges_table[i].num;
10186 if (block_num > 0)
10188 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10189 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10191 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10192 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10194 /* If all code is in the text section, then the compilation
10195 unit base address defaults to DW_AT_low_pc, which is the
10196 base of the text section. */
10197 if (!have_multiple_function_sections)
10199 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10200 text_section_label,
10201 fmt, i * 2 * DWARF2_ADDR_SIZE);
10202 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10203 text_section_label, NULL);
10206 /* Otherwise, the compilation unit base address is zero,
10207 which allows us to use absolute addresses, and not worry
10208 about whether the target supports cross-section
10209 arithmetic. */
10210 else
10212 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10213 fmt, i * 2 * DWARF2_ADDR_SIZE);
10214 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10217 fmt = NULL;
10220 /* Negative block_num stands for an index into ranges_by_label. */
10221 else if (block_num < 0)
10223 int lab_idx = - block_num - 1;
10225 if (!have_multiple_function_sections)
10227 gcc_unreachable ();
10228 #if 0
10229 /* If we ever use add_ranges_by_labels () for a single
10230 function section, all we have to do is to take out
10231 the #if 0 above. */
10232 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10233 ranges_by_label[lab_idx].begin,
10234 text_section_label,
10235 fmt, i * 2 * DWARF2_ADDR_SIZE);
10236 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10237 ranges_by_label[lab_idx].end,
10238 text_section_label, NULL);
10239 #endif
10241 else
10243 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10244 ranges_by_label[lab_idx].begin,
10245 fmt, i * 2 * DWARF2_ADDR_SIZE);
10246 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10247 ranges_by_label[lab_idx].end,
10248 NULL);
10251 else
10253 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10254 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10255 fmt = start_fmt;
10260 /* Data structure containing information about input files. */
10261 struct file_info
10263 const char *path; /* Complete file name. */
10264 const char *fname; /* File name part. */
10265 int length; /* Length of entire string. */
10266 struct dwarf_file_data * file_idx; /* Index in input file table. */
10267 int dir_idx; /* Index in directory table. */
10270 /* Data structure containing information about directories with source
10271 files. */
10272 struct dir_info
10274 const char *path; /* Path including directory name. */
10275 int length; /* Path length. */
10276 int prefix; /* Index of directory entry which is a prefix. */
10277 int count; /* Number of files in this directory. */
10278 int dir_idx; /* Index of directory used as base. */
10281 /* Callback function for file_info comparison. We sort by looking at
10282 the directories in the path. */
10284 static int
10285 file_info_cmp (const void *p1, const void *p2)
10287 const struct file_info *const s1 = (const struct file_info *) p1;
10288 const struct file_info *const s2 = (const struct file_info *) p2;
10289 const unsigned char *cp1;
10290 const unsigned char *cp2;
10292 /* Take care of file names without directories. We need to make sure that
10293 we return consistent values to qsort since some will get confused if
10294 we return the same value when identical operands are passed in opposite
10295 orders. So if neither has a directory, return 0 and otherwise return
10296 1 or -1 depending on which one has the directory. */
10297 if ((s1->path == s1->fname || s2->path == s2->fname))
10298 return (s2->path == s2->fname) - (s1->path == s1->fname);
10300 cp1 = (const unsigned char *) s1->path;
10301 cp2 = (const unsigned char *) s2->path;
10303 while (1)
10305 ++cp1;
10306 ++cp2;
10307 /* Reached the end of the first path? If so, handle like above. */
10308 if ((cp1 == (const unsigned char *) s1->fname)
10309 || (cp2 == (const unsigned char *) s2->fname))
10310 return ((cp2 == (const unsigned char *) s2->fname)
10311 - (cp1 == (const unsigned char *) s1->fname));
10313 /* Character of current path component the same? */
10314 else if (*cp1 != *cp2)
10315 return *cp1 - *cp2;
10319 struct file_name_acquire_data
10321 struct file_info *files;
10322 int used_files;
10323 int max_files;
10326 /* Traversal function for the hash table. */
10329 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10331 struct dwarf_file_data *d = *slot;
10332 struct file_info *fi;
10333 const char *f;
10335 gcc_assert (fnad->max_files >= d->emitted_number);
10337 if (! d->emitted_number)
10338 return 1;
10340 gcc_assert (fnad->max_files != fnad->used_files);
10342 fi = fnad->files + fnad->used_files++;
10344 /* Skip all leading "./". */
10345 f = d->filename;
10346 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10347 f += 2;
10349 /* Create a new array entry. */
10350 fi->path = f;
10351 fi->length = strlen (f);
10352 fi->file_idx = d;
10354 /* Search for the file name part. */
10355 f = strrchr (f, DIR_SEPARATOR);
10356 #if defined (DIR_SEPARATOR_2)
10358 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10360 if (g != NULL)
10362 if (f == NULL || f < g)
10363 f = g;
10366 #endif
10368 fi->fname = f == NULL ? fi->path : f + 1;
10369 return 1;
10372 /* Output the directory table and the file name table. We try to minimize
10373 the total amount of memory needed. A heuristic is used to avoid large
10374 slowdowns with many input files. */
10376 static void
10377 output_file_names (void)
10379 struct file_name_acquire_data fnad;
10380 int numfiles;
10381 struct file_info *files;
10382 struct dir_info *dirs;
10383 int *saved;
10384 int *savehere;
10385 int *backmap;
10386 int ndirs;
10387 int idx_offset;
10388 int i;
10390 if (!last_emitted_file)
10392 dw2_asm_output_data (1, 0, "End directory table");
10393 dw2_asm_output_data (1, 0, "End file name table");
10394 return;
10397 numfiles = last_emitted_file->emitted_number;
10399 /* Allocate the various arrays we need. */
10400 files = XALLOCAVEC (struct file_info, numfiles);
10401 dirs = XALLOCAVEC (struct dir_info, numfiles);
10403 fnad.files = files;
10404 fnad.used_files = 0;
10405 fnad.max_files = numfiles;
10406 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10407 gcc_assert (fnad.used_files == fnad.max_files);
10409 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10411 /* Find all the different directories used. */
10412 dirs[0].path = files[0].path;
10413 dirs[0].length = files[0].fname - files[0].path;
10414 dirs[0].prefix = -1;
10415 dirs[0].count = 1;
10416 dirs[0].dir_idx = 0;
10417 files[0].dir_idx = 0;
10418 ndirs = 1;
10420 for (i = 1; i < numfiles; i++)
10421 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10422 && memcmp (dirs[ndirs - 1].path, files[i].path,
10423 dirs[ndirs - 1].length) == 0)
10425 /* Same directory as last entry. */
10426 files[i].dir_idx = ndirs - 1;
10427 ++dirs[ndirs - 1].count;
10429 else
10431 int j;
10433 /* This is a new directory. */
10434 dirs[ndirs].path = files[i].path;
10435 dirs[ndirs].length = files[i].fname - files[i].path;
10436 dirs[ndirs].count = 1;
10437 dirs[ndirs].dir_idx = ndirs;
10438 files[i].dir_idx = ndirs;
10440 /* Search for a prefix. */
10441 dirs[ndirs].prefix = -1;
10442 for (j = 0; j < ndirs; j++)
10443 if (dirs[j].length < dirs[ndirs].length
10444 && dirs[j].length > 1
10445 && (dirs[ndirs].prefix == -1
10446 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10447 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10448 dirs[ndirs].prefix = j;
10450 ++ndirs;
10453 /* Now to the actual work. We have to find a subset of the directories which
10454 allow expressing the file name using references to the directory table
10455 with the least amount of characters. We do not do an exhaustive search
10456 where we would have to check out every combination of every single
10457 possible prefix. Instead we use a heuristic which provides nearly optimal
10458 results in most cases and never is much off. */
10459 saved = XALLOCAVEC (int, ndirs);
10460 savehere = XALLOCAVEC (int, ndirs);
10462 memset (saved, '\0', ndirs * sizeof (saved[0]));
10463 for (i = 0; i < ndirs; i++)
10465 int j;
10466 int total;
10468 /* We can always save some space for the current directory. But this
10469 does not mean it will be enough to justify adding the directory. */
10470 savehere[i] = dirs[i].length;
10471 total = (savehere[i] - saved[i]) * dirs[i].count;
10473 for (j = i + 1; j < ndirs; j++)
10475 savehere[j] = 0;
10476 if (saved[j] < dirs[i].length)
10478 /* Determine whether the dirs[i] path is a prefix of the
10479 dirs[j] path. */
10480 int k;
10482 k = dirs[j].prefix;
10483 while (k != -1 && k != (int) i)
10484 k = dirs[k].prefix;
10486 if (k == (int) i)
10488 /* Yes it is. We can possibly save some memory by
10489 writing the filenames in dirs[j] relative to
10490 dirs[i]. */
10491 savehere[j] = dirs[i].length;
10492 total += (savehere[j] - saved[j]) * dirs[j].count;
10497 /* Check whether we can save enough to justify adding the dirs[i]
10498 directory. */
10499 if (total > dirs[i].length + 1)
10501 /* It's worthwhile adding. */
10502 for (j = i; j < ndirs; j++)
10503 if (savehere[j] > 0)
10505 /* Remember how much we saved for this directory so far. */
10506 saved[j] = savehere[j];
10508 /* Remember the prefix directory. */
10509 dirs[j].dir_idx = i;
10514 /* Emit the directory name table. */
10515 idx_offset = dirs[0].length > 0 ? 1 : 0;
10516 for (i = 1 - idx_offset; i < ndirs; i++)
10517 dw2_asm_output_nstring (dirs[i].path,
10518 dirs[i].length
10519 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10520 "Directory Entry: %#x", i + idx_offset);
10522 dw2_asm_output_data (1, 0, "End directory table");
10524 /* We have to emit them in the order of emitted_number since that's
10525 used in the debug info generation. To do this efficiently we
10526 generate a back-mapping of the indices first. */
10527 backmap = XALLOCAVEC (int, numfiles);
10528 for (i = 0; i < numfiles; i++)
10529 backmap[files[i].file_idx->emitted_number - 1] = i;
10531 /* Now write all the file names. */
10532 for (i = 0; i < numfiles; i++)
10534 int file_idx = backmap[i];
10535 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10537 #ifdef VMS_DEBUGGING_INFO
10538 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10540 /* Setting these fields can lead to debugger miscomparisons,
10541 but VMS Debug requires them to be set correctly. */
10543 int ver;
10544 long long cdt;
10545 long siz;
10546 int maxfilelen = strlen (files[file_idx].path)
10547 + dirs[dir_idx].length
10548 + MAX_VMS_VERSION_LEN + 1;
10549 char *filebuf = XALLOCAVEC (char, maxfilelen);
10551 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10552 snprintf (filebuf, maxfilelen, "%s;%d",
10553 files[file_idx].path + dirs[dir_idx].length, ver);
10555 dw2_asm_output_nstring
10556 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10558 /* Include directory index. */
10559 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10561 /* Modification time. */
10562 dw2_asm_output_data_uleb128
10563 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10564 ? cdt : 0,
10565 NULL);
10567 /* File length in bytes. */
10568 dw2_asm_output_data_uleb128
10569 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10570 ? siz : 0,
10571 NULL);
10572 #else
10573 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10574 "File Entry: %#x", (unsigned) i + 1);
10576 /* Include directory index. */
10577 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10579 /* Modification time. */
10580 dw2_asm_output_data_uleb128 (0, NULL);
10582 /* File length in bytes. */
10583 dw2_asm_output_data_uleb128 (0, NULL);
10584 #endif /* VMS_DEBUGGING_INFO */
10587 dw2_asm_output_data (1, 0, "End file name table");
10591 /* Output one line number table into the .debug_line section. */
10593 static void
10594 output_one_line_info_table (dw_line_info_table *table)
10596 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10597 unsigned int current_line = 1;
10598 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10599 dw_line_info_entry *ent;
10600 size_t i;
10602 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10604 switch (ent->opcode)
10606 case LI_set_address:
10607 /* ??? Unfortunately, we have little choice here currently, and
10608 must always use the most general form. GCC does not know the
10609 address delta itself, so we can't use DW_LNS_advance_pc. Many
10610 ports do have length attributes which will give an upper bound
10611 on the address range. We could perhaps use length attributes
10612 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10613 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10615 /* This can handle any delta. This takes
10616 4+DWARF2_ADDR_SIZE bytes. */
10617 dw2_asm_output_data (1, 0, "set address %s", line_label);
10618 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10619 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10620 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10621 break;
10623 case LI_set_line:
10624 if (ent->val == current_line)
10626 /* We still need to start a new row, so output a copy insn. */
10627 dw2_asm_output_data (1, DW_LNS_copy,
10628 "copy line %u", current_line);
10630 else
10632 int line_offset = ent->val - current_line;
10633 int line_delta = line_offset - DWARF_LINE_BASE;
10635 current_line = ent->val;
10636 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10638 /* This can handle deltas from -10 to 234, using the current
10639 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10640 This takes 1 byte. */
10641 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10642 "line %u", current_line);
10644 else
10646 /* This can handle any delta. This takes at least 4 bytes,
10647 depending on the value being encoded. */
10648 dw2_asm_output_data (1, DW_LNS_advance_line,
10649 "advance to line %u", current_line);
10650 dw2_asm_output_data_sleb128 (line_offset, NULL);
10651 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10654 break;
10656 case LI_set_file:
10657 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10658 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10659 break;
10661 case LI_set_column:
10662 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10663 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10664 break;
10666 case LI_negate_stmt:
10667 current_is_stmt = !current_is_stmt;
10668 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10669 "is_stmt %d", current_is_stmt);
10670 break;
10672 case LI_set_prologue_end:
10673 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10674 "set prologue end");
10675 break;
10677 case LI_set_epilogue_begin:
10678 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10679 "set epilogue begin");
10680 break;
10682 case LI_set_discriminator:
10683 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10684 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10685 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10686 dw2_asm_output_data_uleb128 (ent->val, NULL);
10687 break;
10691 /* Emit debug info for the address of the end of the table. */
10692 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10693 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10694 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10695 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10697 dw2_asm_output_data (1, 0, "end sequence");
10698 dw2_asm_output_data_uleb128 (1, NULL);
10699 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10702 /* Output the source line number correspondence information. This
10703 information goes into the .debug_line section. */
10705 static void
10706 output_line_info (bool prologue_only)
10708 char l1[20], l2[20], p1[20], p2[20];
10709 /* We don't support DWARFv5 line tables yet. */
10710 int ver = dwarf_version < 5 ? dwarf_version : 4;
10711 bool saw_one = false;
10712 int opc;
10714 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10715 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10716 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10717 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10719 if (!XCOFF_DEBUGGING_INFO)
10721 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10722 dw2_asm_output_data (4, 0xffffffff,
10723 "Initial length escape value indicating 64-bit DWARF extension");
10724 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10725 "Length of Source Line Info");
10728 ASM_OUTPUT_LABEL (asm_out_file, l1);
10730 dw2_asm_output_data (2, ver, "DWARF Version");
10731 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10732 ASM_OUTPUT_LABEL (asm_out_file, p1);
10734 /* Define the architecture-dependent minimum instruction length (in bytes).
10735 In this implementation of DWARF, this field is used for information
10736 purposes only. Since GCC generates assembly language, we have no
10737 a priori knowledge of how many instruction bytes are generated for each
10738 source line, and therefore can use only the DW_LNE_set_address and
10739 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10740 this as '1', which is "correct enough" for all architectures,
10741 and don't let the target override. */
10742 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10744 if (ver >= 4)
10745 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10746 "Maximum Operations Per Instruction");
10747 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10748 "Default is_stmt_start flag");
10749 dw2_asm_output_data (1, DWARF_LINE_BASE,
10750 "Line Base Value (Special Opcodes)");
10751 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10752 "Line Range Value (Special Opcodes)");
10753 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10754 "Special Opcode Base");
10756 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10758 int n_op_args;
10759 switch (opc)
10761 case DW_LNS_advance_pc:
10762 case DW_LNS_advance_line:
10763 case DW_LNS_set_file:
10764 case DW_LNS_set_column:
10765 case DW_LNS_fixed_advance_pc:
10766 case DW_LNS_set_isa:
10767 n_op_args = 1;
10768 break;
10769 default:
10770 n_op_args = 0;
10771 break;
10774 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10775 opc, n_op_args);
10778 /* Write out the information about the files we use. */
10779 output_file_names ();
10780 ASM_OUTPUT_LABEL (asm_out_file, p2);
10781 if (prologue_only)
10783 /* Output the marker for the end of the line number info. */
10784 ASM_OUTPUT_LABEL (asm_out_file, l2);
10785 return;
10788 if (separate_line_info)
10790 dw_line_info_table *table;
10791 size_t i;
10793 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10794 if (table->in_use)
10796 output_one_line_info_table (table);
10797 saw_one = true;
10800 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10802 output_one_line_info_table (cold_text_section_line_info);
10803 saw_one = true;
10806 /* ??? Some Darwin linkers crash on a .debug_line section with no
10807 sequences. Further, merely a DW_LNE_end_sequence entry is not
10808 sufficient -- the address column must also be initialized.
10809 Make sure to output at least one set_address/end_sequence pair,
10810 choosing .text since that section is always present. */
10811 if (text_section_line_info->in_use || !saw_one)
10812 output_one_line_info_table (text_section_line_info);
10814 /* Output the marker for the end of the line number info. */
10815 ASM_OUTPUT_LABEL (asm_out_file, l2);
10818 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10820 static inline bool
10821 need_endianity_attribute_p (bool reverse)
10823 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10826 /* Given a pointer to a tree node for some base type, return a pointer to
10827 a DIE that describes the given type. REVERSE is true if the type is
10828 to be interpreted in the reverse storage order wrt the target order.
10830 This routine must only be called for GCC type nodes that correspond to
10831 Dwarf base (fundamental) types. */
10833 static dw_die_ref
10834 base_type_die (tree type, bool reverse)
10836 dw_die_ref base_type_result;
10837 enum dwarf_type encoding;
10838 bool fpt_used = false;
10839 struct fixed_point_type_info fpt_info;
10840 tree type_bias = NULL_TREE;
10842 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10843 return 0;
10845 /* If this is a subtype that should not be emitted as a subrange type,
10846 use the base type. See subrange_type_for_debug_p. */
10847 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10848 type = TREE_TYPE (type);
10850 switch (TREE_CODE (type))
10852 case INTEGER_TYPE:
10853 if ((dwarf_version >= 4 || !dwarf_strict)
10854 && TYPE_NAME (type)
10855 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10856 && DECL_IS_BUILTIN (TYPE_NAME (type))
10857 && DECL_NAME (TYPE_NAME (type)))
10859 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10860 if (strcmp (name, "char16_t") == 0
10861 || strcmp (name, "char32_t") == 0)
10863 encoding = DW_ATE_UTF;
10864 break;
10867 if ((dwarf_version >= 3 || !dwarf_strict)
10868 && lang_hooks.types.get_fixed_point_type_info)
10870 memset (&fpt_info, 0, sizeof (fpt_info));
10871 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10873 fpt_used = true;
10874 encoding = ((TYPE_UNSIGNED (type))
10875 ? DW_ATE_unsigned_fixed
10876 : DW_ATE_signed_fixed);
10877 break;
10880 if (TYPE_STRING_FLAG (type))
10882 if (TYPE_UNSIGNED (type))
10883 encoding = DW_ATE_unsigned_char;
10884 else
10885 encoding = DW_ATE_signed_char;
10887 else if (TYPE_UNSIGNED (type))
10888 encoding = DW_ATE_unsigned;
10889 else
10890 encoding = DW_ATE_signed;
10892 if (!dwarf_strict
10893 && lang_hooks.types.get_type_bias)
10894 type_bias = lang_hooks.types.get_type_bias (type);
10895 break;
10897 case REAL_TYPE:
10898 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10900 if (dwarf_version >= 3 || !dwarf_strict)
10901 encoding = DW_ATE_decimal_float;
10902 else
10903 encoding = DW_ATE_lo_user;
10905 else
10906 encoding = DW_ATE_float;
10907 break;
10909 case FIXED_POINT_TYPE:
10910 if (!(dwarf_version >= 3 || !dwarf_strict))
10911 encoding = DW_ATE_lo_user;
10912 else if (TYPE_UNSIGNED (type))
10913 encoding = DW_ATE_unsigned_fixed;
10914 else
10915 encoding = DW_ATE_signed_fixed;
10916 break;
10918 /* Dwarf2 doesn't know anything about complex ints, so use
10919 a user defined type for it. */
10920 case COMPLEX_TYPE:
10921 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10922 encoding = DW_ATE_complex_float;
10923 else
10924 encoding = DW_ATE_lo_user;
10925 break;
10927 case BOOLEAN_TYPE:
10928 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10929 encoding = DW_ATE_boolean;
10930 break;
10932 default:
10933 /* No other TREE_CODEs are Dwarf fundamental types. */
10934 gcc_unreachable ();
10937 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10939 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10940 int_size_in_bytes (type));
10941 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10943 if (need_endianity_attribute_p (reverse))
10944 add_AT_unsigned (base_type_result, DW_AT_endianity,
10945 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10947 if (fpt_used)
10949 switch (fpt_info.scale_factor_kind)
10951 case fixed_point_scale_factor_binary:
10952 add_AT_int (base_type_result, DW_AT_binary_scale,
10953 fpt_info.scale_factor.binary);
10954 break;
10956 case fixed_point_scale_factor_decimal:
10957 add_AT_int (base_type_result, DW_AT_decimal_scale,
10958 fpt_info.scale_factor.decimal);
10959 break;
10961 case fixed_point_scale_factor_arbitrary:
10962 /* Arbitrary scale factors cannot be described in standard DWARF,
10963 yet. */
10964 if (!dwarf_strict)
10966 /* Describe the scale factor as a rational constant. */
10967 const dw_die_ref scale_factor
10968 = new_die (DW_TAG_constant, comp_unit_die (), type);
10970 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
10971 fpt_info.scale_factor.arbitrary.numerator);
10972 add_AT_int (scale_factor, DW_AT_GNU_denominator,
10973 fpt_info.scale_factor.arbitrary.denominator);
10975 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
10977 break;
10979 default:
10980 gcc_unreachable ();
10984 if (type_bias)
10985 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
10986 dw_scalar_form_constant
10987 | dw_scalar_form_exprloc
10988 | dw_scalar_form_reference,
10989 NULL);
10991 add_pubtype (type, base_type_result);
10993 return base_type_result;
10996 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10997 named 'auto' in its type: return true for it, false otherwise. */
10999 static inline bool
11000 is_cxx_auto (tree type)
11002 if (is_cxx ())
11004 tree name = TYPE_IDENTIFIER (type);
11005 if (name == get_identifier ("auto")
11006 || name == get_identifier ("decltype(auto)"))
11007 return true;
11009 return false;
11012 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11013 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11015 static inline int
11016 is_base_type (tree type)
11018 switch (TREE_CODE (type))
11020 case ERROR_MARK:
11021 case VOID_TYPE:
11022 case INTEGER_TYPE:
11023 case REAL_TYPE:
11024 case FIXED_POINT_TYPE:
11025 case COMPLEX_TYPE:
11026 case BOOLEAN_TYPE:
11027 case POINTER_BOUNDS_TYPE:
11028 return 1;
11030 case ARRAY_TYPE:
11031 case RECORD_TYPE:
11032 case UNION_TYPE:
11033 case QUAL_UNION_TYPE:
11034 case ENUMERAL_TYPE:
11035 case FUNCTION_TYPE:
11036 case METHOD_TYPE:
11037 case POINTER_TYPE:
11038 case REFERENCE_TYPE:
11039 case NULLPTR_TYPE:
11040 case OFFSET_TYPE:
11041 case LANG_TYPE:
11042 case VECTOR_TYPE:
11043 return 0;
11045 default:
11046 if (is_cxx_auto (type))
11047 return 0;
11048 gcc_unreachable ();
11051 return 0;
11054 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11055 node, return the size in bits for the type if it is a constant, or else
11056 return the alignment for the type if the type's size is not constant, or
11057 else return BITS_PER_WORD if the type actually turns out to be an
11058 ERROR_MARK node. */
11060 static inline unsigned HOST_WIDE_INT
11061 simple_type_size_in_bits (const_tree type)
11063 if (TREE_CODE (type) == ERROR_MARK)
11064 return BITS_PER_WORD;
11065 else if (TYPE_SIZE (type) == NULL_TREE)
11066 return 0;
11067 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11068 return tree_to_uhwi (TYPE_SIZE (type));
11069 else
11070 return TYPE_ALIGN (type);
11073 /* Similarly, but return an offset_int instead of UHWI. */
11075 static inline offset_int
11076 offset_int_type_size_in_bits (const_tree type)
11078 if (TREE_CODE (type) == ERROR_MARK)
11079 return BITS_PER_WORD;
11080 else if (TYPE_SIZE (type) == NULL_TREE)
11081 return 0;
11082 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11083 return wi::to_offset (TYPE_SIZE (type));
11084 else
11085 return TYPE_ALIGN (type);
11088 /* Given a pointer to a tree node for a subrange type, return a pointer
11089 to a DIE that describes the given type. */
11091 static dw_die_ref
11092 subrange_type_die (tree type, tree low, tree high, tree bias,
11093 dw_die_ref context_die)
11095 dw_die_ref subrange_die;
11096 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11098 if (context_die == NULL)
11099 context_die = comp_unit_die ();
11101 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11103 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11105 /* The size of the subrange type and its base type do not match,
11106 so we need to generate a size attribute for the subrange type. */
11107 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11110 if (low)
11111 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11112 if (high)
11113 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11114 if (bias && !dwarf_strict)
11115 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11116 dw_scalar_form_constant
11117 | dw_scalar_form_exprloc
11118 | dw_scalar_form_reference,
11119 NULL);
11121 return subrange_die;
11124 /* Returns the (const and/or volatile) cv_qualifiers associated with
11125 the decl node. This will normally be augmented with the
11126 cv_qualifiers of the underlying type in add_type_attribute. */
11128 static int
11129 decl_quals (const_tree decl)
11131 return ((TREE_READONLY (decl)
11132 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11133 | (TREE_THIS_VOLATILE (decl)
11134 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11137 /* Determine the TYPE whose qualifiers match the largest strict subset
11138 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11139 qualifiers outside QUAL_MASK. */
11141 static int
11142 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11144 tree t;
11145 int best_rank = 0, best_qual = 0, max_rank;
11147 type_quals &= qual_mask;
11148 max_rank = popcount_hwi (type_quals) - 1;
11150 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11151 t = TYPE_NEXT_VARIANT (t))
11153 int q = TYPE_QUALS (t) & qual_mask;
11155 if ((q & type_quals) == q && q != type_quals
11156 && check_base_type (t, type))
11158 int rank = popcount_hwi (q);
11160 if (rank > best_rank)
11162 best_rank = rank;
11163 best_qual = q;
11168 return best_qual;
11171 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11172 static const dwarf_qual_info_t dwarf_qual_info[] =
11174 { TYPE_QUAL_CONST, DW_TAG_const_type },
11175 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11176 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11177 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11179 static const unsigned int dwarf_qual_info_size
11180 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11182 /* If DIE is a qualified DIE of some base DIE with the same parent,
11183 return the base DIE, otherwise return NULL. Set MASK to the
11184 qualifiers added compared to the returned DIE. */
11186 static dw_die_ref
11187 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11189 unsigned int i;
11190 for (i = 0; i < dwarf_qual_info_size; i++)
11191 if (die->die_tag == dwarf_qual_info[i].t)
11192 break;
11193 if (i == dwarf_qual_info_size)
11194 return NULL;
11195 if (vec_safe_length (die->die_attr) != 1)
11196 return NULL;
11197 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11198 if (type == NULL || type->die_parent != die->die_parent)
11199 return NULL;
11200 *mask |= dwarf_qual_info[i].q;
11201 if (depth)
11203 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11204 if (ret)
11205 return ret;
11207 return type;
11210 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11211 entry that chains the modifiers specified by CV_QUALS in front of the
11212 given type. REVERSE is true if the type is to be interpreted in the
11213 reverse storage order wrt the target order. */
11215 static dw_die_ref
11216 modified_type_die (tree type, int cv_quals, bool reverse,
11217 dw_die_ref context_die)
11219 enum tree_code code = TREE_CODE (type);
11220 dw_die_ref mod_type_die;
11221 dw_die_ref sub_die = NULL;
11222 tree item_type = NULL;
11223 tree qualified_type;
11224 tree name, low, high;
11225 dw_die_ref mod_scope;
11226 /* Only these cv-qualifiers are currently handled. */
11227 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11228 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11230 if (code == ERROR_MARK)
11231 return NULL;
11233 if (lang_hooks.types.get_debug_type)
11235 tree debug_type = lang_hooks.types.get_debug_type (type);
11237 if (debug_type != NULL_TREE && debug_type != type)
11238 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11241 cv_quals &= cv_qual_mask;
11243 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11244 tag modifier (and not an attribute) old consumers won't be able
11245 to handle it. */
11246 if (dwarf_version < 3)
11247 cv_quals &= ~TYPE_QUAL_RESTRICT;
11249 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11250 if (dwarf_version < 5)
11251 cv_quals &= ~TYPE_QUAL_ATOMIC;
11253 /* See if we already have the appropriately qualified variant of
11254 this type. */
11255 qualified_type = get_qualified_type (type, cv_quals);
11257 if (qualified_type == sizetype
11258 && TYPE_NAME (qualified_type)
11259 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11261 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11263 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11264 && TYPE_PRECISION (t)
11265 == TYPE_PRECISION (qualified_type)
11266 && TYPE_UNSIGNED (t)
11267 == TYPE_UNSIGNED (qualified_type));
11268 qualified_type = t;
11271 /* If we do, then we can just use its DIE, if it exists. */
11272 if (qualified_type)
11274 mod_type_die = lookup_type_die (qualified_type);
11276 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11277 if (mod_type_die
11278 && (!need_endianity_attribute_p (reverse)
11279 || !is_base_type (type)
11280 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11281 return mod_type_die;
11284 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11286 /* Handle C typedef types. */
11287 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11288 && !DECL_ARTIFICIAL (name))
11290 tree dtype = TREE_TYPE (name);
11292 if (qualified_type == dtype)
11294 /* For a named type, use the typedef. */
11295 gen_type_die (qualified_type, context_die);
11296 return lookup_type_die (qualified_type);
11298 else
11300 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11301 dquals &= cv_qual_mask;
11302 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11303 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11304 /* cv-unqualified version of named type. Just use
11305 the unnamed type to which it refers. */
11306 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11307 reverse, context_die);
11308 /* Else cv-qualified version of named type; fall through. */
11312 mod_scope = scope_die_for (type, context_die);
11314 if (cv_quals)
11316 int sub_quals = 0, first_quals = 0;
11317 unsigned i;
11318 dw_die_ref first = NULL, last = NULL;
11320 /* Determine a lesser qualified type that most closely matches
11321 this one. Then generate DW_TAG_* entries for the remaining
11322 qualifiers. */
11323 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11324 cv_qual_mask);
11325 if (sub_quals && use_debug_types)
11327 bool needed = false;
11328 /* If emitting type units, make sure the order of qualifiers
11329 is canonical. Thus, start from unqualified type if
11330 an earlier qualifier is missing in sub_quals, but some later
11331 one is present there. */
11332 for (i = 0; i < dwarf_qual_info_size; i++)
11333 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11334 needed = true;
11335 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11337 sub_quals = 0;
11338 break;
11341 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11342 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11344 /* As not all intermediate qualified DIEs have corresponding
11345 tree types, ensure that qualified DIEs in the same scope
11346 as their DW_AT_type are emitted after their DW_AT_type,
11347 only with other qualified DIEs for the same type possibly
11348 in between them. Determine the range of such qualified
11349 DIEs now (first being the base type, last being corresponding
11350 last qualified DIE for it). */
11351 unsigned int count = 0;
11352 first = qualified_die_p (mod_type_die, &first_quals,
11353 dwarf_qual_info_size);
11354 if (first == NULL)
11355 first = mod_type_die;
11356 gcc_assert ((first_quals & ~sub_quals) == 0);
11357 for (count = 0, last = first;
11358 count < (1U << dwarf_qual_info_size);
11359 count++, last = last->die_sib)
11361 int quals = 0;
11362 if (last == mod_scope->die_child)
11363 break;
11364 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11365 != first)
11366 break;
11370 for (i = 0; i < dwarf_qual_info_size; i++)
11371 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11373 dw_die_ref d;
11374 if (first && first != last)
11376 for (d = first->die_sib; ; d = d->die_sib)
11378 int quals = 0;
11379 qualified_die_p (d, &quals, dwarf_qual_info_size);
11380 if (quals == (first_quals | dwarf_qual_info[i].q))
11381 break;
11382 if (d == last)
11384 d = NULL;
11385 break;
11388 if (d)
11390 mod_type_die = d;
11391 continue;
11394 if (first)
11396 d = ggc_cleared_alloc<die_node> ();
11397 d->die_tag = dwarf_qual_info[i].t;
11398 add_child_die_after (mod_scope, d, last);
11399 last = d;
11401 else
11402 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11403 if (mod_type_die)
11404 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11405 mod_type_die = d;
11406 first_quals |= dwarf_qual_info[i].q;
11409 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11411 dwarf_tag tag = DW_TAG_pointer_type;
11412 if (code == REFERENCE_TYPE)
11414 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11415 tag = DW_TAG_rvalue_reference_type;
11416 else
11417 tag = DW_TAG_reference_type;
11419 mod_type_die = new_die (tag, mod_scope, type);
11421 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11422 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11423 item_type = TREE_TYPE (type);
11425 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11426 if (!ADDR_SPACE_GENERIC_P (as))
11428 int action = targetm.addr_space.debug (as);
11429 if (action >= 0)
11431 /* Positive values indicate an address_class. */
11432 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11434 else
11436 /* Negative values indicate an (inverted) segment base reg. */
11437 dw_loc_descr_ref d
11438 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11439 add_AT_loc (mod_type_die, DW_AT_segment, d);
11443 else if (code == INTEGER_TYPE
11444 && TREE_TYPE (type) != NULL_TREE
11445 && subrange_type_for_debug_p (type, &low, &high))
11447 tree bias = NULL_TREE;
11448 if (lang_hooks.types.get_type_bias)
11449 bias = lang_hooks.types.get_type_bias (type);
11450 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11451 item_type = TREE_TYPE (type);
11453 else if (is_base_type (type))
11454 mod_type_die = base_type_die (type, reverse);
11455 else
11457 gen_type_die (type, context_die);
11459 /* We have to get the type_main_variant here (and pass that to the
11460 `lookup_type_die' routine) because the ..._TYPE node we have
11461 might simply be a *copy* of some original type node (where the
11462 copy was created to help us keep track of typedef names) and
11463 that copy might have a different TYPE_UID from the original
11464 ..._TYPE node. */
11465 if (TREE_CODE (type) != VECTOR_TYPE)
11466 return lookup_type_die (type_main_variant (type));
11467 else
11468 /* Vectors have the debugging information in the type,
11469 not the main variant. */
11470 return lookup_type_die (type);
11473 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11474 don't output a DW_TAG_typedef, since there isn't one in the
11475 user's program; just attach a DW_AT_name to the type.
11476 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11477 if the base type already has the same name. */
11478 if (name
11479 && ((TREE_CODE (name) != TYPE_DECL
11480 && (qualified_type == TYPE_MAIN_VARIANT (type)
11481 || (cv_quals == TYPE_UNQUALIFIED)))
11482 || (TREE_CODE (name) == TYPE_DECL
11483 && TREE_TYPE (name) == qualified_type
11484 && DECL_NAME (name))))
11486 if (TREE_CODE (name) == TYPE_DECL)
11487 /* Could just call add_name_and_src_coords_attributes here,
11488 but since this is a builtin type it doesn't have any
11489 useful source coordinates anyway. */
11490 name = DECL_NAME (name);
11491 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11493 /* This probably indicates a bug. */
11494 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11496 name = TYPE_IDENTIFIER (type);
11497 add_name_attribute (mod_type_die,
11498 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11501 if (qualified_type)
11502 equate_type_number_to_die (qualified_type, mod_type_die);
11504 if (item_type)
11505 /* We must do this after the equate_type_number_to_die call, in case
11506 this is a recursive type. This ensures that the modified_type_die
11507 recursion will terminate even if the type is recursive. Recursive
11508 types are possible in Ada. */
11509 sub_die = modified_type_die (item_type,
11510 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11511 reverse,
11512 context_die);
11514 if (sub_die != NULL)
11515 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11517 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11518 if (TYPE_ARTIFICIAL (type))
11519 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11521 return mod_type_die;
11524 /* Generate DIEs for the generic parameters of T.
11525 T must be either a generic type or a generic function.
11526 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11528 static void
11529 gen_generic_params_dies (tree t)
11531 tree parms, args;
11532 int parms_num, i;
11533 dw_die_ref die = NULL;
11534 int non_default;
11536 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11537 return;
11539 if (TYPE_P (t))
11540 die = lookup_type_die (t);
11541 else if (DECL_P (t))
11542 die = lookup_decl_die (t);
11544 gcc_assert (die);
11546 parms = lang_hooks.get_innermost_generic_parms (t);
11547 if (!parms)
11548 /* T has no generic parameter. It means T is neither a generic type
11549 or function. End of story. */
11550 return;
11552 parms_num = TREE_VEC_LENGTH (parms);
11553 args = lang_hooks.get_innermost_generic_args (t);
11554 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11555 non_default = int_cst_value (TREE_CHAIN (args));
11556 else
11557 non_default = TREE_VEC_LENGTH (args);
11558 for (i = 0; i < parms_num; i++)
11560 tree parm, arg, arg_pack_elems;
11561 dw_die_ref parm_die;
11563 parm = TREE_VEC_ELT (parms, i);
11564 arg = TREE_VEC_ELT (args, i);
11565 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11566 gcc_assert (parm && TREE_VALUE (parm) && arg);
11568 if (parm && TREE_VALUE (parm) && arg)
11570 /* If PARM represents a template parameter pack,
11571 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11572 by DW_TAG_template_*_parameter DIEs for the argument
11573 pack elements of ARG. Note that ARG would then be
11574 an argument pack. */
11575 if (arg_pack_elems)
11576 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11577 arg_pack_elems,
11578 die);
11579 else
11580 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11581 true /* emit name */, die);
11582 if (i >= non_default)
11583 add_AT_flag (parm_die, DW_AT_default_value, 1);
11588 /* Create and return a DIE for PARM which should be
11589 the representation of a generic type parameter.
11590 For instance, in the C++ front end, PARM would be a template parameter.
11591 ARG is the argument to PARM.
11592 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11593 name of the PARM.
11594 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11595 as a child node. */
11597 static dw_die_ref
11598 generic_parameter_die (tree parm, tree arg,
11599 bool emit_name_p,
11600 dw_die_ref parent_die)
11602 dw_die_ref tmpl_die = NULL;
11603 const char *name = NULL;
11605 if (!parm || !DECL_NAME (parm) || !arg)
11606 return NULL;
11608 /* We support non-type generic parameters and arguments,
11609 type generic parameters and arguments, as well as
11610 generic generic parameters (a.k.a. template template parameters in C++)
11611 and arguments. */
11612 if (TREE_CODE (parm) == PARM_DECL)
11613 /* PARM is a nontype generic parameter */
11614 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11615 else if (TREE_CODE (parm) == TYPE_DECL)
11616 /* PARM is a type generic parameter. */
11617 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11618 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11619 /* PARM is a generic generic parameter.
11620 Its DIE is a GNU extension. It shall have a
11621 DW_AT_name attribute to represent the name of the template template
11622 parameter, and a DW_AT_GNU_template_name attribute to represent the
11623 name of the template template argument. */
11624 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11625 parent_die, parm);
11626 else
11627 gcc_unreachable ();
11629 if (tmpl_die)
11631 tree tmpl_type;
11633 /* If PARM is a generic parameter pack, it means we are
11634 emitting debug info for a template argument pack element.
11635 In other terms, ARG is a template argument pack element.
11636 In that case, we don't emit any DW_AT_name attribute for
11637 the die. */
11638 if (emit_name_p)
11640 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11641 gcc_assert (name);
11642 add_AT_string (tmpl_die, DW_AT_name, name);
11645 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11647 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11648 TMPL_DIE should have a child DW_AT_type attribute that is set
11649 to the type of the argument to PARM, which is ARG.
11650 If PARM is a type generic parameter, TMPL_DIE should have a
11651 child DW_AT_type that is set to ARG. */
11652 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11653 add_type_attribute (tmpl_die, tmpl_type,
11654 (TREE_THIS_VOLATILE (tmpl_type)
11655 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11656 false, parent_die);
11658 else
11660 /* So TMPL_DIE is a DIE representing a
11661 a generic generic template parameter, a.k.a template template
11662 parameter in C++ and arg is a template. */
11664 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11665 to the name of the argument. */
11666 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11667 if (name)
11668 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11671 if (TREE_CODE (parm) == PARM_DECL)
11672 /* So PARM is a non-type generic parameter.
11673 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11674 attribute of TMPL_DIE which value represents the value
11675 of ARG.
11676 We must be careful here:
11677 The value of ARG might reference some function decls.
11678 We might currently be emitting debug info for a generic
11679 type and types are emitted before function decls, we don't
11680 know if the function decls referenced by ARG will actually be
11681 emitted after cgraph computations.
11682 So must defer the generation of the DW_AT_const_value to
11683 after cgraph is ready. */
11684 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11687 return tmpl_die;
11690 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11691 PARM_PACK must be a template parameter pack. The returned DIE
11692 will be child DIE of PARENT_DIE. */
11694 static dw_die_ref
11695 template_parameter_pack_die (tree parm_pack,
11696 tree parm_pack_args,
11697 dw_die_ref parent_die)
11699 dw_die_ref die;
11700 int j;
11702 gcc_assert (parent_die && parm_pack);
11704 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11705 add_name_and_src_coords_attributes (die, parm_pack);
11706 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11707 generic_parameter_die (parm_pack,
11708 TREE_VEC_ELT (parm_pack_args, j),
11709 false /* Don't emit DW_AT_name */,
11710 die);
11711 return die;
11714 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11715 an enumerated type. */
11717 static inline int
11718 type_is_enum (const_tree type)
11720 return TREE_CODE (type) == ENUMERAL_TYPE;
11723 /* Return the DBX register number described by a given RTL node. */
11725 static unsigned int
11726 dbx_reg_number (const_rtx rtl)
11728 unsigned regno = REGNO (rtl);
11730 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11732 #ifdef LEAF_REG_REMAP
11733 if (crtl->uses_only_leaf_regs)
11735 int leaf_reg = LEAF_REG_REMAP (regno);
11736 if (leaf_reg != -1)
11737 regno = (unsigned) leaf_reg;
11739 #endif
11741 regno = DBX_REGISTER_NUMBER (regno);
11742 gcc_assert (regno != INVALID_REGNUM);
11743 return regno;
11746 /* Optionally add a DW_OP_piece term to a location description expression.
11747 DW_OP_piece is only added if the location description expression already
11748 doesn't end with DW_OP_piece. */
11750 static void
11751 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11753 dw_loc_descr_ref loc;
11755 if (*list_head != NULL)
11757 /* Find the end of the chain. */
11758 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11761 if (loc->dw_loc_opc != DW_OP_piece)
11762 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11766 /* Return a location descriptor that designates a machine register or
11767 zero if there is none. */
11769 static dw_loc_descr_ref
11770 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11772 rtx regs;
11774 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11775 return 0;
11777 /* We only use "frame base" when we're sure we're talking about the
11778 post-prologue local stack frame. We do this by *not* running
11779 register elimination until this point, and recognizing the special
11780 argument pointer and soft frame pointer rtx's.
11781 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11782 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11783 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11785 dw_loc_descr_ref result = NULL;
11787 if (dwarf_version >= 4 || !dwarf_strict)
11789 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11790 initialized);
11791 if (result)
11792 add_loc_descr (&result,
11793 new_loc_descr (DW_OP_stack_value, 0, 0));
11795 return result;
11798 regs = targetm.dwarf_register_span (rtl);
11800 if (REG_NREGS (rtl) > 1 || regs)
11801 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11802 else
11804 unsigned int dbx_regnum = dbx_reg_number (rtl);
11805 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11806 return 0;
11807 return one_reg_loc_descriptor (dbx_regnum, initialized);
11811 /* Return a location descriptor that designates a machine register for
11812 a given hard register number. */
11814 static dw_loc_descr_ref
11815 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11817 dw_loc_descr_ref reg_loc_descr;
11819 if (regno <= 31)
11820 reg_loc_descr
11821 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11822 else
11823 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11825 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11826 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11828 return reg_loc_descr;
11831 /* Given an RTL of a register, return a location descriptor that
11832 designates a value that spans more than one register. */
11834 static dw_loc_descr_ref
11835 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11836 enum var_init_status initialized)
11838 int size, i;
11839 dw_loc_descr_ref loc_result = NULL;
11841 /* Simple, contiguous registers. */
11842 if (regs == NULL_RTX)
11844 unsigned reg = REGNO (rtl);
11845 int nregs;
11847 #ifdef LEAF_REG_REMAP
11848 if (crtl->uses_only_leaf_regs)
11850 int leaf_reg = LEAF_REG_REMAP (reg);
11851 if (leaf_reg != -1)
11852 reg = (unsigned) leaf_reg;
11854 #endif
11856 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11857 nregs = REG_NREGS (rtl);
11859 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11861 loc_result = NULL;
11862 while (nregs--)
11864 dw_loc_descr_ref t;
11866 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11867 VAR_INIT_STATUS_INITIALIZED);
11868 add_loc_descr (&loc_result, t);
11869 add_loc_descr_op_piece (&loc_result, size);
11870 ++reg;
11872 return loc_result;
11875 /* Now onto stupid register sets in non contiguous locations. */
11877 gcc_assert (GET_CODE (regs) == PARALLEL);
11879 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11880 loc_result = NULL;
11882 for (i = 0; i < XVECLEN (regs, 0); ++i)
11884 dw_loc_descr_ref t;
11886 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11887 VAR_INIT_STATUS_INITIALIZED);
11888 add_loc_descr (&loc_result, t);
11889 add_loc_descr_op_piece (&loc_result, size);
11892 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11893 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11894 return loc_result;
11897 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11899 /* Return a location descriptor that designates a constant i,
11900 as a compound operation from constant (i >> shift), constant shift
11901 and DW_OP_shl. */
11903 static dw_loc_descr_ref
11904 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11906 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11907 add_loc_descr (&ret, int_loc_descriptor (shift));
11908 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11909 return ret;
11912 /* Return a location descriptor that designates a constant. */
11914 static dw_loc_descr_ref
11915 int_loc_descriptor (HOST_WIDE_INT i)
11917 enum dwarf_location_atom op;
11919 /* Pick the smallest representation of a constant, rather than just
11920 defaulting to the LEB encoding. */
11921 if (i >= 0)
11923 int clz = clz_hwi (i);
11924 int ctz = ctz_hwi (i);
11925 if (i <= 31)
11926 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11927 else if (i <= 0xff)
11928 op = DW_OP_const1u;
11929 else if (i <= 0xffff)
11930 op = DW_OP_const2u;
11931 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11932 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11933 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11934 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11935 while DW_OP_const4u is 5 bytes. */
11936 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11937 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11938 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11939 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11940 while DW_OP_const4u is 5 bytes. */
11941 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11942 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11943 op = DW_OP_const4u;
11944 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11945 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11946 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11947 while DW_OP_constu of constant >= 0x100000000 takes at least
11948 6 bytes. */
11949 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11950 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11951 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11952 >= HOST_BITS_PER_WIDE_INT)
11953 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11954 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11955 while DW_OP_constu takes in this case at least 6 bytes. */
11956 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11957 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11958 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11959 && size_of_uleb128 (i) > 6)
11960 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11961 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11962 else
11963 op = DW_OP_constu;
11965 else
11967 if (i >= -0x80)
11968 op = DW_OP_const1s;
11969 else if (i >= -0x8000)
11970 op = DW_OP_const2s;
11971 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11973 if (size_of_int_loc_descriptor (i) < 5)
11975 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11976 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11977 return ret;
11979 op = DW_OP_const4s;
11981 else
11983 if (size_of_int_loc_descriptor (i)
11984 < (unsigned long) 1 + size_of_sleb128 (i))
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_consts;
11994 return new_loc_descr (op, i, 0);
11997 /* Likewise, for unsigned constants. */
11999 static dw_loc_descr_ref
12000 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12002 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12003 const unsigned HOST_WIDE_INT max_uint
12004 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12006 /* If possible, use the clever signed constants handling. */
12007 if (i <= max_int)
12008 return int_loc_descriptor ((HOST_WIDE_INT) i);
12010 /* Here, we are left with positive numbers that cannot be represented as
12011 HOST_WIDE_INT, i.e.:
12012 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12014 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12015 whereas may be better to output a negative integer: thanks to integer
12016 wrapping, we know that:
12017 x = x - 2 ** DWARF2_ADDR_SIZE
12018 = x - 2 * (max (HOST_WIDE_INT) + 1)
12019 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12020 small negative integers. Let's try that in cases it will clearly improve
12021 the encoding: there is no gain turning DW_OP_const4u into
12022 DW_OP_const4s. */
12023 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12024 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12025 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12027 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12029 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12030 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12031 const HOST_WIDE_INT second_shift
12032 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12034 /* So we finally have:
12035 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12036 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12037 return int_loc_descriptor (second_shift);
12040 /* Last chance: fallback to a simple constant operation. */
12041 return new_loc_descr
12042 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12043 ? DW_OP_const4u
12044 : DW_OP_const8u,
12045 i, 0);
12048 /* Generate and return a location description that computes the unsigned
12049 comparison of the two stack top entries (a OP b where b is the top-most
12050 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12051 LE_EXPR, GT_EXPR or GE_EXPR. */
12053 static dw_loc_descr_ref
12054 uint_comparison_loc_list (enum tree_code kind)
12056 enum dwarf_location_atom op, flip_op;
12057 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12059 switch (kind)
12061 case LT_EXPR:
12062 op = DW_OP_lt;
12063 break;
12064 case LE_EXPR:
12065 op = DW_OP_le;
12066 break;
12067 case GT_EXPR:
12068 op = DW_OP_gt;
12069 break;
12070 case GE_EXPR:
12071 op = DW_OP_ge;
12072 break;
12073 default:
12074 gcc_unreachable ();
12077 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12078 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12080 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12081 possible to perform unsigned comparisons: we just have to distinguish
12082 three cases:
12084 1. when a and b have the same sign (as signed integers); then we should
12085 return: a OP(signed) b;
12087 2. when a is a negative signed integer while b is a positive one, then a
12088 is a greater unsigned integer than b; likewise when a and b's roles
12089 are flipped.
12091 So first, compare the sign of the two operands. */
12092 ret = new_loc_descr (DW_OP_over, 0, 0);
12093 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12094 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12095 /* If they have different signs (i.e. they have different sign bits), then
12096 the stack top value has now the sign bit set and thus it's smaller than
12097 zero. */
12098 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12099 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12100 add_loc_descr (&ret, bra_node);
12102 /* We are in case 1. At this point, we know both operands have the same
12103 sign, to it's safe to use the built-in signed comparison. */
12104 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12105 add_loc_descr (&ret, jmp_node);
12107 /* We are in case 2. Here, we know both operands do not have the same sign,
12108 so we have to flip the signed comparison. */
12109 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12110 tmp = new_loc_descr (flip_op, 0, 0);
12111 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12112 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12113 add_loc_descr (&ret, tmp);
12115 /* This dummy operation is necessary to make the two branches join. */
12116 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12117 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12118 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12119 add_loc_descr (&ret, tmp);
12121 return ret;
12124 /* Likewise, but takes the location description lists (might be destructive on
12125 them). Return NULL if either is NULL or if concatenation fails. */
12127 static dw_loc_list_ref
12128 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12129 enum tree_code kind)
12131 if (left == NULL || right == NULL)
12132 return NULL;
12134 add_loc_list (&left, right);
12135 if (left == NULL)
12136 return NULL;
12138 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12139 return left;
12142 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12143 without actually allocating it. */
12145 static unsigned long
12146 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12148 return size_of_int_loc_descriptor (i >> shift)
12149 + size_of_int_loc_descriptor (shift)
12150 + 1;
12153 /* Return size_of_locs (int_loc_descriptor (i)) without
12154 actually allocating it. */
12156 static unsigned long
12157 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12159 unsigned long s;
12161 if (i >= 0)
12163 int clz, ctz;
12164 if (i <= 31)
12165 return 1;
12166 else if (i <= 0xff)
12167 return 2;
12168 else if (i <= 0xffff)
12169 return 3;
12170 clz = clz_hwi (i);
12171 ctz = ctz_hwi (i);
12172 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12173 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12174 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12175 - clz - 5);
12176 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12177 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12178 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12179 - clz - 8);
12180 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12181 return 5;
12182 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12183 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12184 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12185 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12186 - clz - 8);
12187 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12188 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12189 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12190 - clz - 16);
12191 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12192 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12193 && s > 6)
12194 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12195 - clz - 32);
12196 else
12197 return 1 + s;
12199 else
12201 if (i >= -0x80)
12202 return 2;
12203 else if (i >= -0x8000)
12204 return 3;
12205 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12207 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12209 s = size_of_int_loc_descriptor (-i) + 1;
12210 if (s < 5)
12211 return s;
12213 return 5;
12215 else
12217 unsigned long r = 1 + size_of_sleb128 (i);
12218 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12220 s = size_of_int_loc_descriptor (-i) + 1;
12221 if (s < r)
12222 return s;
12224 return r;
12229 /* Return loc description representing "address" of integer value.
12230 This can appear only as toplevel expression. */
12232 static dw_loc_descr_ref
12233 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12235 int litsize;
12236 dw_loc_descr_ref loc_result = NULL;
12238 if (!(dwarf_version >= 4 || !dwarf_strict))
12239 return NULL;
12241 litsize = size_of_int_loc_descriptor (i);
12242 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12243 is more compact. For DW_OP_stack_value we need:
12244 litsize + 1 (DW_OP_stack_value)
12245 and for DW_OP_implicit_value:
12246 1 (DW_OP_implicit_value) + 1 (length) + size. */
12247 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12249 loc_result = int_loc_descriptor (i);
12250 add_loc_descr (&loc_result,
12251 new_loc_descr (DW_OP_stack_value, 0, 0));
12252 return loc_result;
12255 loc_result = new_loc_descr (DW_OP_implicit_value,
12256 size, 0);
12257 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12258 loc_result->dw_loc_oprnd2.v.val_int = i;
12259 return loc_result;
12262 /* Return a location descriptor that designates a base+offset location. */
12264 static dw_loc_descr_ref
12265 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12266 enum var_init_status initialized)
12268 unsigned int regno;
12269 dw_loc_descr_ref result;
12270 dw_fde_ref fde = cfun->fde;
12272 /* We only use "frame base" when we're sure we're talking about the
12273 post-prologue local stack frame. We do this by *not* running
12274 register elimination until this point, and recognizing the special
12275 argument pointer and soft frame pointer rtx's. */
12276 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12278 rtx elim = (ira_use_lra_p
12279 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12280 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12282 if (elim != reg)
12284 if (GET_CODE (elim) == PLUS)
12286 offset += INTVAL (XEXP (elim, 1));
12287 elim = XEXP (elim, 0);
12289 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12290 && (elim == hard_frame_pointer_rtx
12291 || elim == stack_pointer_rtx))
12292 || elim == (frame_pointer_needed
12293 ? hard_frame_pointer_rtx
12294 : stack_pointer_rtx));
12296 /* If drap register is used to align stack, use frame
12297 pointer + offset to access stack variables. If stack
12298 is aligned without drap, use stack pointer + offset to
12299 access stack variables. */
12300 if (crtl->stack_realign_tried
12301 && reg == frame_pointer_rtx)
12303 int base_reg
12304 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12305 ? HARD_FRAME_POINTER_REGNUM
12306 : REGNO (elim));
12307 return new_reg_loc_descr (base_reg, offset);
12310 gcc_assert (frame_pointer_fb_offset_valid);
12311 offset += frame_pointer_fb_offset;
12312 return new_loc_descr (DW_OP_fbreg, offset, 0);
12316 regno = REGNO (reg);
12317 #ifdef LEAF_REG_REMAP
12318 if (crtl->uses_only_leaf_regs)
12320 int leaf_reg = LEAF_REG_REMAP (regno);
12321 if (leaf_reg != -1)
12322 regno = (unsigned) leaf_reg;
12324 #endif
12325 regno = DWARF_FRAME_REGNUM (regno);
12327 if (!optimize && fde
12328 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12330 /* Use cfa+offset to represent the location of arguments passed
12331 on the stack when drap is used to align stack.
12332 Only do this when not optimizing, for optimized code var-tracking
12333 is supposed to track where the arguments live and the register
12334 used as vdrap or drap in some spot might be used for something
12335 else in other part of the routine. */
12336 return new_loc_descr (DW_OP_fbreg, offset, 0);
12339 if (regno <= 31)
12340 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12341 offset, 0);
12342 else
12343 result = new_loc_descr (DW_OP_bregx, regno, offset);
12345 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12346 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12348 return result;
12351 /* Return true if this RTL expression describes a base+offset calculation. */
12353 static inline int
12354 is_based_loc (const_rtx rtl)
12356 return (GET_CODE (rtl) == PLUS
12357 && ((REG_P (XEXP (rtl, 0))
12358 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12359 && CONST_INT_P (XEXP (rtl, 1)))));
12362 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12363 failed. */
12365 static dw_loc_descr_ref
12366 tls_mem_loc_descriptor (rtx mem)
12368 tree base;
12369 dw_loc_descr_ref loc_result;
12371 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12372 return NULL;
12374 base = get_base_address (MEM_EXPR (mem));
12375 if (base == NULL
12376 || TREE_CODE (base) != VAR_DECL
12377 || !DECL_THREAD_LOCAL_P (base))
12378 return NULL;
12380 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12381 if (loc_result == NULL)
12382 return NULL;
12384 if (MEM_OFFSET (mem))
12385 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12387 return loc_result;
12390 /* Output debug info about reason why we failed to expand expression as dwarf
12391 expression. */
12393 static void
12394 expansion_failed (tree expr, rtx rtl, char const *reason)
12396 if (dump_file && (dump_flags & TDF_DETAILS))
12398 fprintf (dump_file, "Failed to expand as dwarf: ");
12399 if (expr)
12400 print_generic_expr (dump_file, expr, dump_flags);
12401 if (rtl)
12403 fprintf (dump_file, "\n");
12404 print_rtl (dump_file, rtl);
12406 fprintf (dump_file, "\nReason: %s\n", reason);
12410 /* Helper function for const_ok_for_output. */
12412 static bool
12413 const_ok_for_output_1 (rtx rtl)
12415 if (GET_CODE (rtl) == UNSPEC)
12417 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12418 we can't express it in the debug info. */
12419 /* Don't complain about TLS UNSPECs, those are just too hard to
12420 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12421 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12422 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12423 if (flag_checking
12424 && (XVECLEN (rtl, 0) == 0
12425 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12426 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12427 inform (current_function_decl
12428 ? DECL_SOURCE_LOCATION (current_function_decl)
12429 : UNKNOWN_LOCATION,
12430 #if NUM_UNSPEC_VALUES > 0
12431 "non-delegitimized UNSPEC %s (%d) found in variable location",
12432 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12433 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12434 XINT (rtl, 1));
12435 #else
12436 "non-delegitimized UNSPEC %d found in variable location",
12437 XINT (rtl, 1));
12438 #endif
12439 expansion_failed (NULL_TREE, rtl,
12440 "UNSPEC hasn't been delegitimized.\n");
12441 return false;
12444 if (targetm.const_not_ok_for_debug_p (rtl))
12446 expansion_failed (NULL_TREE, rtl,
12447 "Expression rejected for debug by the backend.\n");
12448 return false;
12451 /* FIXME: Refer to PR60655. It is possible for simplification
12452 of rtl expressions in var tracking to produce such expressions.
12453 We should really identify / validate expressions
12454 enclosed in CONST that can be handled by assemblers on various
12455 targets and only handle legitimate cases here. */
12456 if (GET_CODE (rtl) != SYMBOL_REF)
12458 if (GET_CODE (rtl) == NOT)
12459 return false;
12460 return true;
12463 if (CONSTANT_POOL_ADDRESS_P (rtl))
12465 bool marked;
12466 get_pool_constant_mark (rtl, &marked);
12467 /* If all references to this pool constant were optimized away,
12468 it was not output and thus we can't represent it. */
12469 if (!marked)
12471 expansion_failed (NULL_TREE, rtl,
12472 "Constant was removed from constant pool.\n");
12473 return false;
12477 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12478 return false;
12480 /* Avoid references to external symbols in debug info, on several targets
12481 the linker might even refuse to link when linking a shared library,
12482 and in many other cases the relocations for .debug_info/.debug_loc are
12483 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12484 to be defined within the same shared library or executable are fine. */
12485 if (SYMBOL_REF_EXTERNAL_P (rtl))
12487 tree decl = SYMBOL_REF_DECL (rtl);
12489 if (decl == NULL || !targetm.binds_local_p (decl))
12491 expansion_failed (NULL_TREE, rtl,
12492 "Symbol not defined in current TU.\n");
12493 return false;
12497 return true;
12500 /* Return true if constant RTL can be emitted in DW_OP_addr or
12501 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12502 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12504 static bool
12505 const_ok_for_output (rtx rtl)
12507 if (GET_CODE (rtl) == SYMBOL_REF)
12508 return const_ok_for_output_1 (rtl);
12510 if (GET_CODE (rtl) == CONST)
12512 subrtx_var_iterator::array_type array;
12513 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12514 if (!const_ok_for_output_1 (*iter))
12515 return false;
12516 return true;
12519 return true;
12522 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12523 if possible, NULL otherwise. */
12525 static dw_die_ref
12526 base_type_for_mode (machine_mode mode, bool unsignedp)
12528 dw_die_ref type_die;
12529 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12531 if (type == NULL)
12532 return NULL;
12533 switch (TREE_CODE (type))
12535 case INTEGER_TYPE:
12536 case REAL_TYPE:
12537 break;
12538 default:
12539 return NULL;
12541 type_die = lookup_type_die (type);
12542 if (!type_die)
12543 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12544 comp_unit_die ());
12545 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12546 return NULL;
12547 return type_die;
12550 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12551 type matching MODE, or, if MODE is narrower than or as wide as
12552 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12553 possible. */
12555 static dw_loc_descr_ref
12556 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12558 machine_mode outer_mode = mode;
12559 dw_die_ref type_die;
12560 dw_loc_descr_ref cvt;
12562 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12564 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12565 return op;
12567 type_die = base_type_for_mode (outer_mode, 1);
12568 if (type_die == NULL)
12569 return NULL;
12570 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12571 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12572 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12573 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12574 add_loc_descr (&op, cvt);
12575 return op;
12578 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12580 static dw_loc_descr_ref
12581 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12582 dw_loc_descr_ref op1)
12584 dw_loc_descr_ref ret = op0;
12585 add_loc_descr (&ret, op1);
12586 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12587 if (STORE_FLAG_VALUE != 1)
12589 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12590 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12592 return ret;
12595 /* Return location descriptor for signed comparison OP RTL. */
12597 static dw_loc_descr_ref
12598 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12599 machine_mode mem_mode)
12601 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12602 dw_loc_descr_ref op0, op1;
12603 int shift;
12605 if (op_mode == VOIDmode)
12606 op_mode = GET_MODE (XEXP (rtl, 1));
12607 if (op_mode == VOIDmode)
12608 return NULL;
12610 if (dwarf_strict
12611 && (GET_MODE_CLASS (op_mode) != MODE_INT
12612 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12613 return NULL;
12615 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12616 VAR_INIT_STATUS_INITIALIZED);
12617 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12618 VAR_INIT_STATUS_INITIALIZED);
12620 if (op0 == NULL || op1 == NULL)
12621 return NULL;
12623 if (GET_MODE_CLASS (op_mode) != MODE_INT
12624 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12625 return compare_loc_descriptor (op, op0, op1);
12627 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12629 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12630 dw_loc_descr_ref cvt;
12632 if (type_die == NULL)
12633 return NULL;
12634 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12635 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12636 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12637 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12638 add_loc_descr (&op0, cvt);
12639 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12640 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12641 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12642 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12643 add_loc_descr (&op1, cvt);
12644 return compare_loc_descriptor (op, op0, op1);
12647 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12648 /* For eq/ne, if the operands are known to be zero-extended,
12649 there is no need to do the fancy shifting up. */
12650 if (op == DW_OP_eq || op == DW_OP_ne)
12652 dw_loc_descr_ref last0, last1;
12653 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12655 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12657 /* deref_size zero extends, and for constants we can check
12658 whether they are zero extended or not. */
12659 if (((last0->dw_loc_opc == DW_OP_deref_size
12660 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12661 || (CONST_INT_P (XEXP (rtl, 0))
12662 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12663 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12664 && ((last1->dw_loc_opc == DW_OP_deref_size
12665 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12666 || (CONST_INT_P (XEXP (rtl, 1))
12667 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12668 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12669 return compare_loc_descriptor (op, op0, op1);
12671 /* EQ/NE comparison against constant in narrower type than
12672 DWARF2_ADDR_SIZE can be performed either as
12673 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12674 DW_OP_{eq,ne}
12676 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12677 DW_OP_{eq,ne}. Pick whatever is shorter. */
12678 if (CONST_INT_P (XEXP (rtl, 1))
12679 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12680 && (size_of_int_loc_descriptor (shift) + 1
12681 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12682 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12683 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12684 & GET_MODE_MASK (op_mode))))
12686 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12687 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12688 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12689 & GET_MODE_MASK (op_mode));
12690 return compare_loc_descriptor (op, op0, op1);
12693 add_loc_descr (&op0, int_loc_descriptor (shift));
12694 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12695 if (CONST_INT_P (XEXP (rtl, 1)))
12696 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12697 else
12699 add_loc_descr (&op1, int_loc_descriptor (shift));
12700 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12702 return compare_loc_descriptor (op, op0, op1);
12705 /* Return location descriptor for unsigned comparison OP RTL. */
12707 static dw_loc_descr_ref
12708 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12709 machine_mode mem_mode)
12711 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12712 dw_loc_descr_ref op0, op1;
12714 if (op_mode == VOIDmode)
12715 op_mode = GET_MODE (XEXP (rtl, 1));
12716 if (op_mode == VOIDmode)
12717 return NULL;
12718 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12719 return NULL;
12721 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12722 return NULL;
12724 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12725 VAR_INIT_STATUS_INITIALIZED);
12726 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12727 VAR_INIT_STATUS_INITIALIZED);
12729 if (op0 == NULL || op1 == NULL)
12730 return NULL;
12732 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12734 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12735 dw_loc_descr_ref last0, last1;
12736 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12738 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12740 if (CONST_INT_P (XEXP (rtl, 0)))
12741 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12742 /* deref_size zero extends, so no need to mask it again. */
12743 else if (last0->dw_loc_opc != DW_OP_deref_size
12744 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12746 add_loc_descr (&op0, int_loc_descriptor (mask));
12747 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12749 if (CONST_INT_P (XEXP (rtl, 1)))
12750 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12751 /* deref_size zero extends, so no need to mask it again. */
12752 else if (last1->dw_loc_opc != DW_OP_deref_size
12753 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12755 add_loc_descr (&op1, int_loc_descriptor (mask));
12756 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12759 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12761 HOST_WIDE_INT bias = 1;
12762 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12763 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12764 if (CONST_INT_P (XEXP (rtl, 1)))
12765 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12766 + INTVAL (XEXP (rtl, 1)));
12767 else
12768 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12769 bias, 0));
12771 return compare_loc_descriptor (op, op0, op1);
12774 /* Return location descriptor for {U,S}{MIN,MAX}. */
12776 static dw_loc_descr_ref
12777 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12778 machine_mode mem_mode)
12780 enum dwarf_location_atom op;
12781 dw_loc_descr_ref op0, op1, ret;
12782 dw_loc_descr_ref bra_node, drop_node;
12784 if (dwarf_strict
12785 && (GET_MODE_CLASS (mode) != MODE_INT
12786 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12787 return NULL;
12789 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12790 VAR_INIT_STATUS_INITIALIZED);
12791 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12792 VAR_INIT_STATUS_INITIALIZED);
12794 if (op0 == NULL || op1 == NULL)
12795 return NULL;
12797 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12798 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12799 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12800 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12802 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12804 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12805 add_loc_descr (&op0, int_loc_descriptor (mask));
12806 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12807 add_loc_descr (&op1, int_loc_descriptor (mask));
12808 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12810 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12812 HOST_WIDE_INT bias = 1;
12813 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12814 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12815 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12818 else if (GET_MODE_CLASS (mode) == MODE_INT
12819 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12821 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12822 add_loc_descr (&op0, int_loc_descriptor (shift));
12823 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12824 add_loc_descr (&op1, int_loc_descriptor (shift));
12825 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12827 else if (GET_MODE_CLASS (mode) == MODE_INT
12828 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12830 dw_die_ref type_die = base_type_for_mode (mode, 0);
12831 dw_loc_descr_ref cvt;
12832 if (type_die == NULL)
12833 return NULL;
12834 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12835 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12836 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12837 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12838 add_loc_descr (&op0, cvt);
12839 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12840 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12841 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12842 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12843 add_loc_descr (&op1, cvt);
12846 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12847 op = DW_OP_lt;
12848 else
12849 op = DW_OP_gt;
12850 ret = op0;
12851 add_loc_descr (&ret, op1);
12852 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12853 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12854 add_loc_descr (&ret, bra_node);
12855 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12856 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12857 add_loc_descr (&ret, drop_node);
12858 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12859 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12860 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12861 && GET_MODE_CLASS (mode) == MODE_INT
12862 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12863 ret = convert_descriptor_to_mode (mode, ret);
12864 return ret;
12867 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12868 but after converting arguments to type_die, afterwards
12869 convert back to unsigned. */
12871 static dw_loc_descr_ref
12872 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12873 machine_mode mode, machine_mode mem_mode)
12875 dw_loc_descr_ref cvt, op0, op1;
12877 if (type_die == NULL)
12878 return NULL;
12879 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12880 VAR_INIT_STATUS_INITIALIZED);
12881 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12882 VAR_INIT_STATUS_INITIALIZED);
12883 if (op0 == NULL || op1 == NULL)
12884 return NULL;
12885 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12886 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12887 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12888 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12889 add_loc_descr (&op0, cvt);
12890 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12891 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12892 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12893 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12894 add_loc_descr (&op1, cvt);
12895 add_loc_descr (&op0, op1);
12896 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12897 return convert_descriptor_to_mode (mode, op0);
12900 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12901 const0 is DW_OP_lit0 or corresponding typed constant,
12902 const1 is DW_OP_lit1 or corresponding typed constant
12903 and constMSB is constant with just the MSB bit set
12904 for the mode):
12905 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12906 L1: const0 DW_OP_swap
12907 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12908 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12909 L3: DW_OP_drop
12910 L4: DW_OP_nop
12912 CTZ is similar:
12913 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12914 L1: const0 DW_OP_swap
12915 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12916 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12917 L3: DW_OP_drop
12918 L4: DW_OP_nop
12920 FFS is similar:
12921 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12922 L1: const1 DW_OP_swap
12923 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12924 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12925 L3: DW_OP_drop
12926 L4: DW_OP_nop */
12928 static dw_loc_descr_ref
12929 clz_loc_descriptor (rtx rtl, machine_mode mode,
12930 machine_mode mem_mode)
12932 dw_loc_descr_ref op0, ret, tmp;
12933 HOST_WIDE_INT valv;
12934 dw_loc_descr_ref l1jump, l1label;
12935 dw_loc_descr_ref l2jump, l2label;
12936 dw_loc_descr_ref l3jump, l3label;
12937 dw_loc_descr_ref l4jump, l4label;
12938 rtx msb;
12940 if (GET_MODE_CLASS (mode) != MODE_INT
12941 || GET_MODE (XEXP (rtl, 0)) != mode)
12942 return NULL;
12944 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12945 VAR_INIT_STATUS_INITIALIZED);
12946 if (op0 == NULL)
12947 return NULL;
12948 ret = op0;
12949 if (GET_CODE (rtl) == CLZ)
12951 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12952 valv = GET_MODE_BITSIZE (mode);
12954 else if (GET_CODE (rtl) == FFS)
12955 valv = 0;
12956 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12957 valv = GET_MODE_BITSIZE (mode);
12958 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12959 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12960 add_loc_descr (&ret, l1jump);
12961 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12962 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12963 VAR_INIT_STATUS_INITIALIZED);
12964 if (tmp == NULL)
12965 return NULL;
12966 add_loc_descr (&ret, tmp);
12967 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12968 add_loc_descr (&ret, l4jump);
12969 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12970 ? const1_rtx : const0_rtx,
12971 mode, mem_mode,
12972 VAR_INIT_STATUS_INITIALIZED);
12973 if (l1label == NULL)
12974 return NULL;
12975 add_loc_descr (&ret, l1label);
12976 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12977 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12978 add_loc_descr (&ret, l2label);
12979 if (GET_CODE (rtl) != CLZ)
12980 msb = const1_rtx;
12981 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12982 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12983 << (GET_MODE_BITSIZE (mode) - 1));
12984 else
12985 msb = immed_wide_int_const
12986 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12987 GET_MODE_PRECISION (mode)), mode);
12988 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12989 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12990 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12991 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12992 else
12993 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12994 VAR_INIT_STATUS_INITIALIZED);
12995 if (tmp == NULL)
12996 return NULL;
12997 add_loc_descr (&ret, tmp);
12998 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12999 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13000 add_loc_descr (&ret, l3jump);
13001 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13002 VAR_INIT_STATUS_INITIALIZED);
13003 if (tmp == NULL)
13004 return NULL;
13005 add_loc_descr (&ret, tmp);
13006 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13007 ? DW_OP_shl : DW_OP_shr, 0, 0));
13008 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13009 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13010 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13011 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13012 add_loc_descr (&ret, l2jump);
13013 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13014 add_loc_descr (&ret, l3label);
13015 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13016 add_loc_descr (&ret, l4label);
13017 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13018 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13019 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13020 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13021 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13022 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13023 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13024 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13025 return ret;
13028 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13029 const1 is DW_OP_lit1 or corresponding typed constant):
13030 const0 DW_OP_swap
13031 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13032 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13033 L2: DW_OP_drop
13035 PARITY is similar:
13036 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13037 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13038 L2: DW_OP_drop */
13040 static dw_loc_descr_ref
13041 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13042 machine_mode mem_mode)
13044 dw_loc_descr_ref op0, ret, tmp;
13045 dw_loc_descr_ref l1jump, l1label;
13046 dw_loc_descr_ref l2jump, l2label;
13048 if (GET_MODE_CLASS (mode) != MODE_INT
13049 || GET_MODE (XEXP (rtl, 0)) != mode)
13050 return NULL;
13052 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13053 VAR_INIT_STATUS_INITIALIZED);
13054 if (op0 == NULL)
13055 return NULL;
13056 ret = op0;
13057 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13058 VAR_INIT_STATUS_INITIALIZED);
13059 if (tmp == NULL)
13060 return NULL;
13061 add_loc_descr (&ret, tmp);
13062 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13063 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13064 add_loc_descr (&ret, l1label);
13065 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13066 add_loc_descr (&ret, l2jump);
13067 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13068 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13069 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13070 VAR_INIT_STATUS_INITIALIZED);
13071 if (tmp == NULL)
13072 return NULL;
13073 add_loc_descr (&ret, tmp);
13074 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13075 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13076 ? DW_OP_plus : DW_OP_xor, 0, 0));
13077 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13078 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13079 VAR_INIT_STATUS_INITIALIZED);
13080 add_loc_descr (&ret, tmp);
13081 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13082 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13083 add_loc_descr (&ret, l1jump);
13084 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13085 add_loc_descr (&ret, l2label);
13086 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13087 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13088 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13089 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13090 return ret;
13093 /* BSWAP (constS is initial shift count, either 56 or 24):
13094 constS const0
13095 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13096 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13097 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13098 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13099 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13101 static dw_loc_descr_ref
13102 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13103 machine_mode mem_mode)
13105 dw_loc_descr_ref op0, ret, tmp;
13106 dw_loc_descr_ref l1jump, l1label;
13107 dw_loc_descr_ref l2jump, l2label;
13109 if (GET_MODE_CLASS (mode) != MODE_INT
13110 || BITS_PER_UNIT != 8
13111 || (GET_MODE_BITSIZE (mode) != 32
13112 && GET_MODE_BITSIZE (mode) != 64))
13113 return NULL;
13115 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13116 VAR_INIT_STATUS_INITIALIZED);
13117 if (op0 == NULL)
13118 return NULL;
13120 ret = op0;
13121 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13122 mode, mem_mode,
13123 VAR_INIT_STATUS_INITIALIZED);
13124 if (tmp == NULL)
13125 return NULL;
13126 add_loc_descr (&ret, tmp);
13127 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13128 VAR_INIT_STATUS_INITIALIZED);
13129 if (tmp == NULL)
13130 return NULL;
13131 add_loc_descr (&ret, tmp);
13132 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13133 add_loc_descr (&ret, l1label);
13134 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13135 mode, mem_mode,
13136 VAR_INIT_STATUS_INITIALIZED);
13137 add_loc_descr (&ret, tmp);
13138 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13139 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13140 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13141 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13142 VAR_INIT_STATUS_INITIALIZED);
13143 if (tmp == NULL)
13144 return NULL;
13145 add_loc_descr (&ret, tmp);
13146 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13147 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13148 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13149 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13150 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13151 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13152 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13153 VAR_INIT_STATUS_INITIALIZED);
13154 add_loc_descr (&ret, tmp);
13155 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13156 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13157 add_loc_descr (&ret, l2jump);
13158 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13159 VAR_INIT_STATUS_INITIALIZED);
13160 add_loc_descr (&ret, tmp);
13161 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13162 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13163 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13164 add_loc_descr (&ret, l1jump);
13165 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13166 add_loc_descr (&ret, l2label);
13167 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13168 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13169 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13170 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13171 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13172 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13173 return ret;
13176 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13177 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13178 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13179 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13181 ROTATERT is similar:
13182 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13183 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13184 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13186 static dw_loc_descr_ref
13187 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13188 machine_mode mem_mode)
13190 rtx rtlop1 = XEXP (rtl, 1);
13191 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13192 int i;
13194 if (GET_MODE_CLASS (mode) != MODE_INT)
13195 return NULL;
13197 if (GET_MODE (rtlop1) != VOIDmode
13198 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13199 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13200 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13201 VAR_INIT_STATUS_INITIALIZED);
13202 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13203 VAR_INIT_STATUS_INITIALIZED);
13204 if (op0 == NULL || op1 == NULL)
13205 return NULL;
13206 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13207 for (i = 0; i < 2; i++)
13209 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13210 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13211 mode, mem_mode,
13212 VAR_INIT_STATUS_INITIALIZED);
13213 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13214 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13215 ? DW_OP_const4u
13216 : HOST_BITS_PER_WIDE_INT == 64
13217 ? DW_OP_const8u : DW_OP_constu,
13218 GET_MODE_MASK (mode), 0);
13219 else
13220 mask[i] = NULL;
13221 if (mask[i] == NULL)
13222 return NULL;
13223 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13225 ret = op0;
13226 add_loc_descr (&ret, op1);
13227 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13228 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13229 if (GET_CODE (rtl) == ROTATERT)
13231 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13232 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13233 GET_MODE_BITSIZE (mode), 0));
13235 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13236 if (mask[0] != NULL)
13237 add_loc_descr (&ret, mask[0]);
13238 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13239 if (mask[1] != NULL)
13241 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13242 add_loc_descr (&ret, mask[1]);
13243 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13245 if (GET_CODE (rtl) == ROTATE)
13247 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13248 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13249 GET_MODE_BITSIZE (mode), 0));
13251 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13252 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13253 return ret;
13256 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13257 for DEBUG_PARAMETER_REF RTL. */
13259 static dw_loc_descr_ref
13260 parameter_ref_descriptor (rtx rtl)
13262 dw_loc_descr_ref ret;
13263 dw_die_ref ref;
13265 if (dwarf_strict)
13266 return NULL;
13267 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13268 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13269 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13270 if (ref)
13272 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13273 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13274 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13276 else
13278 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13279 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13281 return ret;
13284 /* The following routine converts the RTL for a variable or parameter
13285 (resident in memory) into an equivalent Dwarf representation of a
13286 mechanism for getting the address of that same variable onto the top of a
13287 hypothetical "address evaluation" stack.
13289 When creating memory location descriptors, we are effectively transforming
13290 the RTL for a memory-resident object into its Dwarf postfix expression
13291 equivalent. This routine recursively descends an RTL tree, turning
13292 it into Dwarf postfix code as it goes.
13294 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13296 MEM_MODE is the mode of the memory reference, needed to handle some
13297 autoincrement addressing modes.
13299 Return 0 if we can't represent the location. */
13301 dw_loc_descr_ref
13302 mem_loc_descriptor (rtx rtl, machine_mode mode,
13303 machine_mode mem_mode,
13304 enum var_init_status initialized)
13306 dw_loc_descr_ref mem_loc_result = NULL;
13307 enum dwarf_location_atom op;
13308 dw_loc_descr_ref op0, op1;
13309 rtx inner = NULL_RTX;
13311 if (mode == VOIDmode)
13312 mode = GET_MODE (rtl);
13314 /* Note that for a dynamically sized array, the location we will generate a
13315 description of here will be the lowest numbered location which is
13316 actually within the array. That's *not* necessarily the same as the
13317 zeroth element of the array. */
13319 rtl = targetm.delegitimize_address (rtl);
13321 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13322 return NULL;
13324 switch (GET_CODE (rtl))
13326 case POST_INC:
13327 case POST_DEC:
13328 case POST_MODIFY:
13329 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13331 case SUBREG:
13332 /* The case of a subreg may arise when we have a local (register)
13333 variable or a formal (register) parameter which doesn't quite fill
13334 up an entire register. For now, just assume that it is
13335 legitimate to make the Dwarf info refer to the whole register which
13336 contains the given subreg. */
13337 if (!subreg_lowpart_p (rtl))
13338 break;
13339 inner = SUBREG_REG (rtl);
13340 case TRUNCATE:
13341 if (inner == NULL_RTX)
13342 inner = XEXP (rtl, 0);
13343 if (GET_MODE_CLASS (mode) == MODE_INT
13344 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
13345 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13346 #ifdef POINTERS_EXTEND_UNSIGNED
13347 || (mode == Pmode && mem_mode != VOIDmode)
13348 #endif
13350 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13352 mem_loc_result = mem_loc_descriptor (inner,
13353 GET_MODE (inner),
13354 mem_mode, initialized);
13355 break;
13357 if (dwarf_strict)
13358 break;
13359 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13360 break;
13361 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13362 && (GET_MODE_CLASS (mode) != MODE_INT
13363 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
13364 break;
13365 else
13367 dw_die_ref type_die;
13368 dw_loc_descr_ref cvt;
13370 mem_loc_result = mem_loc_descriptor (inner,
13371 GET_MODE (inner),
13372 mem_mode, initialized);
13373 if (mem_loc_result == NULL)
13374 break;
13375 type_die = base_type_for_mode (mode,
13376 GET_MODE_CLASS (mode) == MODE_INT);
13377 if (type_die == NULL)
13379 mem_loc_result = NULL;
13380 break;
13382 if (GET_MODE_SIZE (mode)
13383 != GET_MODE_SIZE (GET_MODE (inner)))
13384 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13385 else
13386 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13387 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13388 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13389 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13390 add_loc_descr (&mem_loc_result, cvt);
13391 if (GET_MODE_CLASS (mode) == MODE_INT
13392 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13394 /* Convert it to untyped afterwards. */
13395 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13396 add_loc_descr (&mem_loc_result, cvt);
13399 break;
13401 case REG:
13402 if (GET_MODE_CLASS (mode) != MODE_INT
13403 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13404 && rtl != arg_pointer_rtx
13405 && rtl != frame_pointer_rtx
13406 #ifdef POINTERS_EXTEND_UNSIGNED
13407 && (mode != Pmode || mem_mode == VOIDmode)
13408 #endif
13411 dw_die_ref type_die;
13412 unsigned int dbx_regnum;
13414 if (dwarf_strict)
13415 break;
13416 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13417 break;
13418 type_die = base_type_for_mode (mode,
13419 GET_MODE_CLASS (mode) == MODE_INT);
13420 if (type_die == NULL)
13421 break;
13423 dbx_regnum = dbx_reg_number (rtl);
13424 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13425 break;
13426 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13427 dbx_regnum, 0);
13428 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13429 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13430 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13431 break;
13433 /* Whenever a register number forms a part of the description of the
13434 method for calculating the (dynamic) address of a memory resident
13435 object, DWARF rules require the register number be referred to as
13436 a "base register". This distinction is not based in any way upon
13437 what category of register the hardware believes the given register
13438 belongs to. This is strictly DWARF terminology we're dealing with
13439 here. Note that in cases where the location of a memory-resident
13440 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13441 OP_CONST (0)) the actual DWARF location descriptor that we generate
13442 may just be OP_BASEREG (basereg). This may look deceptively like
13443 the object in question was allocated to a register (rather than in
13444 memory) so DWARF consumers need to be aware of the subtle
13445 distinction between OP_REG and OP_BASEREG. */
13446 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13447 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13448 else if (stack_realign_drap
13449 && crtl->drap_reg
13450 && crtl->args.internal_arg_pointer == rtl
13451 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13453 /* If RTL is internal_arg_pointer, which has been optimized
13454 out, use DRAP instead. */
13455 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13456 VAR_INIT_STATUS_INITIALIZED);
13458 break;
13460 case SIGN_EXTEND:
13461 case ZERO_EXTEND:
13462 if (GET_MODE_CLASS (mode) != MODE_INT)
13463 break;
13464 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13465 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13466 if (op0 == 0)
13467 break;
13468 else if (GET_CODE (rtl) == ZERO_EXTEND
13469 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13470 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13471 < HOST_BITS_PER_WIDE_INT
13472 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13473 to expand zero extend as two shifts instead of
13474 masking. */
13475 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13477 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13478 mem_loc_result = op0;
13479 add_loc_descr (&mem_loc_result,
13480 int_loc_descriptor (GET_MODE_MASK (imode)));
13481 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13483 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13485 int shift = DWARF2_ADDR_SIZE
13486 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13487 shift *= BITS_PER_UNIT;
13488 if (GET_CODE (rtl) == SIGN_EXTEND)
13489 op = DW_OP_shra;
13490 else
13491 op = DW_OP_shr;
13492 mem_loc_result = op0;
13493 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13494 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13495 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13496 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13498 else if (!dwarf_strict)
13500 dw_die_ref type_die1, type_die2;
13501 dw_loc_descr_ref cvt;
13503 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13504 GET_CODE (rtl) == ZERO_EXTEND);
13505 if (type_die1 == NULL)
13506 break;
13507 type_die2 = base_type_for_mode (mode, 1);
13508 if (type_die2 == NULL)
13509 break;
13510 mem_loc_result = op0;
13511 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13512 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13513 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13514 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13515 add_loc_descr (&mem_loc_result, cvt);
13516 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13517 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13518 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13519 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13520 add_loc_descr (&mem_loc_result, cvt);
13522 break;
13524 case MEM:
13526 rtx new_rtl = avoid_constant_pool_reference (rtl);
13527 if (new_rtl != rtl)
13529 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13530 initialized);
13531 if (mem_loc_result != NULL)
13532 return mem_loc_result;
13535 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13536 get_address_mode (rtl), mode,
13537 VAR_INIT_STATUS_INITIALIZED);
13538 if (mem_loc_result == NULL)
13539 mem_loc_result = tls_mem_loc_descriptor (rtl);
13540 if (mem_loc_result != NULL)
13542 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13543 || GET_MODE_CLASS (mode) != MODE_INT)
13545 dw_die_ref type_die;
13546 dw_loc_descr_ref deref;
13548 if (dwarf_strict)
13549 return NULL;
13550 type_die
13551 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
13552 if (type_die == NULL)
13553 return NULL;
13554 deref = new_loc_descr (DW_OP_GNU_deref_type,
13555 GET_MODE_SIZE (mode), 0);
13556 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13557 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13558 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13559 add_loc_descr (&mem_loc_result, deref);
13561 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13562 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13563 else
13564 add_loc_descr (&mem_loc_result,
13565 new_loc_descr (DW_OP_deref_size,
13566 GET_MODE_SIZE (mode), 0));
13568 break;
13570 case LO_SUM:
13571 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13573 case LABEL_REF:
13574 /* Some ports can transform a symbol ref into a label ref, because
13575 the symbol ref is too far away and has to be dumped into a constant
13576 pool. */
13577 case CONST:
13578 case SYMBOL_REF:
13579 if ((GET_MODE_CLASS (mode) != MODE_INT
13580 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
13581 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13582 #ifdef POINTERS_EXTEND_UNSIGNED
13583 && (mode != Pmode || mem_mode == VOIDmode)
13584 #endif
13586 break;
13587 if (GET_CODE (rtl) == SYMBOL_REF
13588 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13590 dw_loc_descr_ref temp;
13592 /* If this is not defined, we have no way to emit the data. */
13593 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13594 break;
13596 temp = new_addr_loc_descr (rtl, dtprel_true);
13598 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13599 add_loc_descr (&mem_loc_result, temp);
13601 break;
13604 if (!const_ok_for_output (rtl))
13606 if (GET_CODE (rtl) == CONST)
13607 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13608 initialized);
13609 break;
13612 symref:
13613 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13614 vec_safe_push (used_rtx_array, rtl);
13615 break;
13617 case CONCAT:
13618 case CONCATN:
13619 case VAR_LOCATION:
13620 case DEBUG_IMPLICIT_PTR:
13621 expansion_failed (NULL_TREE, rtl,
13622 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13623 return 0;
13625 case ENTRY_VALUE:
13626 if (dwarf_strict)
13627 return NULL;
13628 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13630 if (GET_MODE_CLASS (mode) != MODE_INT
13631 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13632 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13633 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13634 else
13636 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13637 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13638 return NULL;
13639 op0 = one_reg_loc_descriptor (dbx_regnum,
13640 VAR_INIT_STATUS_INITIALIZED);
13643 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13644 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13646 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13647 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13648 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13649 return NULL;
13651 else
13652 gcc_unreachable ();
13653 if (op0 == NULL)
13654 return NULL;
13655 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13656 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13657 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13658 break;
13660 case DEBUG_PARAMETER_REF:
13661 mem_loc_result = parameter_ref_descriptor (rtl);
13662 break;
13664 case PRE_MODIFY:
13665 /* Extract the PLUS expression nested inside and fall into
13666 PLUS code below. */
13667 rtl = XEXP (rtl, 1);
13668 goto plus;
13670 case PRE_INC:
13671 case PRE_DEC:
13672 /* Turn these into a PLUS expression and fall into the PLUS code
13673 below. */
13674 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13675 gen_int_mode (GET_CODE (rtl) == PRE_INC
13676 ? GET_MODE_UNIT_SIZE (mem_mode)
13677 : -GET_MODE_UNIT_SIZE (mem_mode),
13678 mode));
13680 /* ... fall through ... */
13682 case PLUS:
13683 plus:
13684 if (is_based_loc (rtl)
13685 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13686 || XEXP (rtl, 0) == arg_pointer_rtx
13687 || XEXP (rtl, 0) == frame_pointer_rtx)
13688 && GET_MODE_CLASS (mode) == MODE_INT)
13689 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13690 INTVAL (XEXP (rtl, 1)),
13691 VAR_INIT_STATUS_INITIALIZED);
13692 else
13694 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13695 VAR_INIT_STATUS_INITIALIZED);
13696 if (mem_loc_result == 0)
13697 break;
13699 if (CONST_INT_P (XEXP (rtl, 1))
13700 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13701 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13702 else
13704 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13705 VAR_INIT_STATUS_INITIALIZED);
13706 if (op1 == 0)
13707 return NULL;
13708 add_loc_descr (&mem_loc_result, op1);
13709 add_loc_descr (&mem_loc_result,
13710 new_loc_descr (DW_OP_plus, 0, 0));
13713 break;
13715 /* If a pseudo-reg is optimized away, it is possible for it to
13716 be replaced with a MEM containing a multiply or shift. */
13717 case MINUS:
13718 op = DW_OP_minus;
13719 goto do_binop;
13721 case MULT:
13722 op = DW_OP_mul;
13723 goto do_binop;
13725 case DIV:
13726 if (!dwarf_strict
13727 && GET_MODE_CLASS (mode) == MODE_INT
13728 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13730 mem_loc_result = typed_binop (DW_OP_div, rtl,
13731 base_type_for_mode (mode, 0),
13732 mode, mem_mode);
13733 break;
13735 op = DW_OP_div;
13736 goto do_binop;
13738 case UMOD:
13739 op = DW_OP_mod;
13740 goto do_binop;
13742 case ASHIFT:
13743 op = DW_OP_shl;
13744 goto do_shift;
13746 case ASHIFTRT:
13747 op = DW_OP_shra;
13748 goto do_shift;
13750 case LSHIFTRT:
13751 op = DW_OP_shr;
13752 goto do_shift;
13754 do_shift:
13755 if (GET_MODE_CLASS (mode) != MODE_INT)
13756 break;
13757 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13758 VAR_INIT_STATUS_INITIALIZED);
13760 rtx rtlop1 = XEXP (rtl, 1);
13761 if (GET_MODE (rtlop1) != VOIDmode
13762 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13763 < GET_MODE_BITSIZE (mode))
13764 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13765 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13766 VAR_INIT_STATUS_INITIALIZED);
13769 if (op0 == 0 || op1 == 0)
13770 break;
13772 mem_loc_result = op0;
13773 add_loc_descr (&mem_loc_result, op1);
13774 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13775 break;
13777 case AND:
13778 op = DW_OP_and;
13779 goto do_binop;
13781 case IOR:
13782 op = DW_OP_or;
13783 goto do_binop;
13785 case XOR:
13786 op = DW_OP_xor;
13787 goto do_binop;
13789 do_binop:
13790 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13791 VAR_INIT_STATUS_INITIALIZED);
13792 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13793 VAR_INIT_STATUS_INITIALIZED);
13795 if (op0 == 0 || op1 == 0)
13796 break;
13798 mem_loc_result = op0;
13799 add_loc_descr (&mem_loc_result, op1);
13800 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13801 break;
13803 case MOD:
13804 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13806 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13807 base_type_for_mode (mode, 0),
13808 mode, mem_mode);
13809 break;
13812 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13813 VAR_INIT_STATUS_INITIALIZED);
13814 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13815 VAR_INIT_STATUS_INITIALIZED);
13817 if (op0 == 0 || op1 == 0)
13818 break;
13820 mem_loc_result = op0;
13821 add_loc_descr (&mem_loc_result, op1);
13822 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13823 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13824 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13825 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13826 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13827 break;
13829 case UDIV:
13830 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13832 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13834 op = DW_OP_div;
13835 goto do_binop;
13837 mem_loc_result = typed_binop (DW_OP_div, rtl,
13838 base_type_for_mode (mode, 1),
13839 mode, mem_mode);
13841 break;
13843 case NOT:
13844 op = DW_OP_not;
13845 goto do_unop;
13847 case ABS:
13848 op = DW_OP_abs;
13849 goto do_unop;
13851 case NEG:
13852 op = DW_OP_neg;
13853 goto do_unop;
13855 do_unop:
13856 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13857 VAR_INIT_STATUS_INITIALIZED);
13859 if (op0 == 0)
13860 break;
13862 mem_loc_result = op0;
13863 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13864 break;
13866 case CONST_INT:
13867 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13868 #ifdef POINTERS_EXTEND_UNSIGNED
13869 || (mode == Pmode
13870 && mem_mode != VOIDmode
13871 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13872 #endif
13875 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13876 break;
13878 if (!dwarf_strict
13879 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13880 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13882 dw_die_ref type_die = base_type_for_mode (mode, 1);
13883 machine_mode amode;
13884 if (type_die == NULL)
13885 return NULL;
13886 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13887 MODE_INT, 0);
13888 if (INTVAL (rtl) >= 0
13889 && amode != BLKmode
13890 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13891 /* const DW_OP_GNU_convert <XXX> vs.
13892 DW_OP_GNU_const_type <XXX, 1, const>. */
13893 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13894 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13896 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13897 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13898 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13899 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13900 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13901 add_loc_descr (&mem_loc_result, op0);
13902 return mem_loc_result;
13904 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13905 INTVAL (rtl));
13906 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13907 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13908 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13909 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13910 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13911 else
13913 mem_loc_result->dw_loc_oprnd2.val_class
13914 = dw_val_class_const_double;
13915 mem_loc_result->dw_loc_oprnd2.v.val_double
13916 = double_int::from_shwi (INTVAL (rtl));
13919 break;
13921 case CONST_DOUBLE:
13922 if (!dwarf_strict)
13924 dw_die_ref type_die;
13926 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13927 CONST_DOUBLE rtx could represent either a large integer
13928 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13929 the value is always a floating point constant.
13931 When it is an integer, a CONST_DOUBLE is used whenever
13932 the constant requires 2 HWIs to be adequately represented.
13933 We output CONST_DOUBLEs as blocks. */
13934 if (mode == VOIDmode
13935 || (GET_MODE (rtl) == VOIDmode
13936 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13937 break;
13938 type_die = base_type_for_mode (mode,
13939 GET_MODE_CLASS (mode) == MODE_INT);
13940 if (type_die == NULL)
13941 return NULL;
13942 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13943 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13944 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13945 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13946 #if TARGET_SUPPORTS_WIDE_INT == 0
13947 if (!SCALAR_FLOAT_MODE_P (mode))
13949 mem_loc_result->dw_loc_oprnd2.val_class
13950 = dw_val_class_const_double;
13951 mem_loc_result->dw_loc_oprnd2.v.val_double
13952 = rtx_to_double_int (rtl);
13954 else
13955 #endif
13957 unsigned int length = GET_MODE_SIZE (mode);
13958 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13960 insert_float (rtl, array);
13961 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13962 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13963 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13964 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13967 break;
13969 case CONST_WIDE_INT:
13970 if (!dwarf_strict)
13972 dw_die_ref type_die;
13974 type_die = base_type_for_mode (mode,
13975 GET_MODE_CLASS (mode) == MODE_INT);
13976 if (type_die == NULL)
13977 return NULL;
13978 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13979 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13980 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13981 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13982 mem_loc_result->dw_loc_oprnd2.val_class
13983 = dw_val_class_wide_int;
13984 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13985 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13987 break;
13989 case EQ:
13990 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13991 break;
13993 case GE:
13994 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13995 break;
13997 case GT:
13998 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13999 break;
14001 case LE:
14002 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14003 break;
14005 case LT:
14006 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14007 break;
14009 case NE:
14010 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14011 break;
14013 case GEU:
14014 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14015 break;
14017 case GTU:
14018 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14019 break;
14021 case LEU:
14022 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14023 break;
14025 case LTU:
14026 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14027 break;
14029 case UMIN:
14030 case UMAX:
14031 if (GET_MODE_CLASS (mode) != MODE_INT)
14032 break;
14033 /* FALLTHRU */
14034 case SMIN:
14035 case SMAX:
14036 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14037 break;
14039 case ZERO_EXTRACT:
14040 case SIGN_EXTRACT:
14041 if (CONST_INT_P (XEXP (rtl, 1))
14042 && CONST_INT_P (XEXP (rtl, 2))
14043 && ((unsigned) INTVAL (XEXP (rtl, 1))
14044 + (unsigned) INTVAL (XEXP (rtl, 2))
14045 <= GET_MODE_BITSIZE (mode))
14046 && GET_MODE_CLASS (mode) == MODE_INT
14047 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14048 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14050 int shift, size;
14051 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14052 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14053 if (op0 == 0)
14054 break;
14055 if (GET_CODE (rtl) == SIGN_EXTRACT)
14056 op = DW_OP_shra;
14057 else
14058 op = DW_OP_shr;
14059 mem_loc_result = op0;
14060 size = INTVAL (XEXP (rtl, 1));
14061 shift = INTVAL (XEXP (rtl, 2));
14062 if (BITS_BIG_ENDIAN)
14063 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14064 - shift - size;
14065 if (shift + size != (int) DWARF2_ADDR_SIZE)
14067 add_loc_descr (&mem_loc_result,
14068 int_loc_descriptor (DWARF2_ADDR_SIZE
14069 - shift - size));
14070 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14072 if (size != (int) DWARF2_ADDR_SIZE)
14074 add_loc_descr (&mem_loc_result,
14075 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14076 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14079 break;
14081 case IF_THEN_ELSE:
14083 dw_loc_descr_ref op2, bra_node, drop_node;
14084 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14085 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14086 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14087 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14088 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14089 VAR_INIT_STATUS_INITIALIZED);
14090 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14091 VAR_INIT_STATUS_INITIALIZED);
14092 if (op0 == NULL || op1 == NULL || op2 == NULL)
14093 break;
14095 mem_loc_result = op1;
14096 add_loc_descr (&mem_loc_result, op2);
14097 add_loc_descr (&mem_loc_result, op0);
14098 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14099 add_loc_descr (&mem_loc_result, bra_node);
14100 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14101 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14102 add_loc_descr (&mem_loc_result, drop_node);
14103 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14104 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14106 break;
14108 case FLOAT_EXTEND:
14109 case FLOAT_TRUNCATE:
14110 case FLOAT:
14111 case UNSIGNED_FLOAT:
14112 case FIX:
14113 case UNSIGNED_FIX:
14114 if (!dwarf_strict)
14116 dw_die_ref type_die;
14117 dw_loc_descr_ref cvt;
14119 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14120 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14121 if (op0 == NULL)
14122 break;
14123 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14124 && (GET_CODE (rtl) == FLOAT
14125 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14126 <= DWARF2_ADDR_SIZE))
14128 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14129 GET_CODE (rtl) == UNSIGNED_FLOAT);
14130 if (type_die == NULL)
14131 break;
14132 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14133 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14134 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14135 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14136 add_loc_descr (&op0, cvt);
14138 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14139 if (type_die == NULL)
14140 break;
14141 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14142 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14143 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14144 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14145 add_loc_descr (&op0, cvt);
14146 if (GET_MODE_CLASS (mode) == MODE_INT
14147 && (GET_CODE (rtl) == FIX
14148 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14150 op0 = convert_descriptor_to_mode (mode, op0);
14151 if (op0 == NULL)
14152 break;
14154 mem_loc_result = op0;
14156 break;
14158 case CLZ:
14159 case CTZ:
14160 case FFS:
14161 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14162 break;
14164 case POPCOUNT:
14165 case PARITY:
14166 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14167 break;
14169 case BSWAP:
14170 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14171 break;
14173 case ROTATE:
14174 case ROTATERT:
14175 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14176 break;
14178 case COMPARE:
14179 /* In theory, we could implement the above. */
14180 /* DWARF cannot represent the unsigned compare operations
14181 natively. */
14182 case SS_MULT:
14183 case US_MULT:
14184 case SS_DIV:
14185 case US_DIV:
14186 case SS_PLUS:
14187 case US_PLUS:
14188 case SS_MINUS:
14189 case US_MINUS:
14190 case SS_NEG:
14191 case US_NEG:
14192 case SS_ABS:
14193 case SS_ASHIFT:
14194 case US_ASHIFT:
14195 case SS_TRUNCATE:
14196 case US_TRUNCATE:
14197 case UNORDERED:
14198 case ORDERED:
14199 case UNEQ:
14200 case UNGE:
14201 case UNGT:
14202 case UNLE:
14203 case UNLT:
14204 case LTGT:
14205 case FRACT_CONVERT:
14206 case UNSIGNED_FRACT_CONVERT:
14207 case SAT_FRACT:
14208 case UNSIGNED_SAT_FRACT:
14209 case SQRT:
14210 case ASM_OPERANDS:
14211 case VEC_MERGE:
14212 case VEC_SELECT:
14213 case VEC_CONCAT:
14214 case VEC_DUPLICATE:
14215 case UNSPEC:
14216 case HIGH:
14217 case FMA:
14218 case STRICT_LOW_PART:
14219 case CONST_VECTOR:
14220 case CONST_FIXED:
14221 case CLRSB:
14222 case CLOBBER:
14223 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14224 can't express it in the debug info. This can happen e.g. with some
14225 TLS UNSPECs. */
14226 break;
14228 case CONST_STRING:
14229 resolve_one_addr (&rtl);
14230 goto symref;
14232 default:
14233 if (flag_checking)
14235 print_rtl (stderr, rtl);
14236 gcc_unreachable ();
14238 break;
14241 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14242 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14244 return mem_loc_result;
14247 /* Return a descriptor that describes the concatenation of two locations.
14248 This is typically a complex variable. */
14250 static dw_loc_descr_ref
14251 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14253 dw_loc_descr_ref cc_loc_result = NULL;
14254 dw_loc_descr_ref x0_ref
14255 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14256 dw_loc_descr_ref x1_ref
14257 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14259 if (x0_ref == 0 || x1_ref == 0)
14260 return 0;
14262 cc_loc_result = x0_ref;
14263 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14265 add_loc_descr (&cc_loc_result, x1_ref);
14266 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14268 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14269 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14271 return cc_loc_result;
14274 /* Return a descriptor that describes the concatenation of N
14275 locations. */
14277 static dw_loc_descr_ref
14278 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14280 unsigned int i;
14281 dw_loc_descr_ref cc_loc_result = NULL;
14282 unsigned int n = XVECLEN (concatn, 0);
14284 for (i = 0; i < n; ++i)
14286 dw_loc_descr_ref ref;
14287 rtx x = XVECEXP (concatn, 0, i);
14289 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14290 if (ref == NULL)
14291 return NULL;
14293 add_loc_descr (&cc_loc_result, ref);
14294 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14297 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14298 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14300 return cc_loc_result;
14303 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14304 for DEBUG_IMPLICIT_PTR RTL. */
14306 static dw_loc_descr_ref
14307 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14309 dw_loc_descr_ref ret;
14310 dw_die_ref ref;
14312 if (dwarf_strict)
14313 return NULL;
14314 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14315 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14316 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14317 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14318 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14319 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14320 if (ref)
14322 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14323 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14324 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14326 else
14328 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14329 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14331 return ret;
14334 /* Output a proper Dwarf location descriptor for a variable or parameter
14335 which is either allocated in a register or in a memory location. For a
14336 register, we just generate an OP_REG and the register number. For a
14337 memory location we provide a Dwarf postfix expression describing how to
14338 generate the (dynamic) address of the object onto the address stack.
14340 MODE is mode of the decl if this loc_descriptor is going to be used in
14341 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14342 allowed, VOIDmode otherwise.
14344 If we don't know how to describe it, return 0. */
14346 static dw_loc_descr_ref
14347 loc_descriptor (rtx rtl, machine_mode mode,
14348 enum var_init_status initialized)
14350 dw_loc_descr_ref loc_result = NULL;
14352 switch (GET_CODE (rtl))
14354 case SUBREG:
14355 /* The case of a subreg may arise when we have a local (register)
14356 variable or a formal (register) parameter which doesn't quite fill
14357 up an entire register. For now, just assume that it is
14358 legitimate to make the Dwarf info refer to the whole register which
14359 contains the given subreg. */
14360 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14361 loc_result = loc_descriptor (SUBREG_REG (rtl),
14362 GET_MODE (SUBREG_REG (rtl)), initialized);
14363 else
14364 goto do_default;
14365 break;
14367 case REG:
14368 loc_result = reg_loc_descriptor (rtl, initialized);
14369 break;
14371 case MEM:
14372 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14373 GET_MODE (rtl), initialized);
14374 if (loc_result == NULL)
14375 loc_result = tls_mem_loc_descriptor (rtl);
14376 if (loc_result == NULL)
14378 rtx new_rtl = avoid_constant_pool_reference (rtl);
14379 if (new_rtl != rtl)
14380 loc_result = loc_descriptor (new_rtl, mode, initialized);
14382 break;
14384 case CONCAT:
14385 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14386 initialized);
14387 break;
14389 case CONCATN:
14390 loc_result = concatn_loc_descriptor (rtl, initialized);
14391 break;
14393 case VAR_LOCATION:
14394 /* Single part. */
14395 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14397 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14398 if (GET_CODE (loc) == EXPR_LIST)
14399 loc = XEXP (loc, 0);
14400 loc_result = loc_descriptor (loc, mode, initialized);
14401 break;
14404 rtl = XEXP (rtl, 1);
14405 /* FALLTHRU */
14407 case PARALLEL:
14409 rtvec par_elems = XVEC (rtl, 0);
14410 int num_elem = GET_NUM_ELEM (par_elems);
14411 machine_mode mode;
14412 int i;
14414 /* Create the first one, so we have something to add to. */
14415 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14416 VOIDmode, initialized);
14417 if (loc_result == NULL)
14418 return NULL;
14419 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14420 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14421 for (i = 1; i < num_elem; i++)
14423 dw_loc_descr_ref temp;
14425 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14426 VOIDmode, initialized);
14427 if (temp == NULL)
14428 return NULL;
14429 add_loc_descr (&loc_result, temp);
14430 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14431 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14434 break;
14436 case CONST_INT:
14437 if (mode != VOIDmode && mode != BLKmode)
14438 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14439 INTVAL (rtl));
14440 break;
14442 case CONST_DOUBLE:
14443 if (mode == VOIDmode)
14444 mode = GET_MODE (rtl);
14446 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14448 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14450 /* Note that a CONST_DOUBLE rtx could represent either an integer
14451 or a floating-point constant. A CONST_DOUBLE is used whenever
14452 the constant requires more than one word in order to be
14453 adequately represented. We output CONST_DOUBLEs as blocks. */
14454 loc_result = new_loc_descr (DW_OP_implicit_value,
14455 GET_MODE_SIZE (mode), 0);
14456 #if TARGET_SUPPORTS_WIDE_INT == 0
14457 if (!SCALAR_FLOAT_MODE_P (mode))
14459 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14460 loc_result->dw_loc_oprnd2.v.val_double
14461 = rtx_to_double_int (rtl);
14463 else
14464 #endif
14466 unsigned int length = GET_MODE_SIZE (mode);
14467 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14469 insert_float (rtl, array);
14470 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14471 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14472 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14473 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14476 break;
14478 case CONST_WIDE_INT:
14479 if (mode == VOIDmode)
14480 mode = GET_MODE (rtl);
14482 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14484 loc_result = new_loc_descr (DW_OP_implicit_value,
14485 GET_MODE_SIZE (mode), 0);
14486 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14487 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14488 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14490 break;
14492 case CONST_VECTOR:
14493 if (mode == VOIDmode)
14494 mode = GET_MODE (rtl);
14496 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14498 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14499 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14500 unsigned char *array
14501 = ggc_vec_alloc<unsigned char> (length * elt_size);
14502 unsigned int i;
14503 unsigned char *p;
14504 machine_mode imode = GET_MODE_INNER (mode);
14506 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14507 switch (GET_MODE_CLASS (mode))
14509 case MODE_VECTOR_INT:
14510 for (i = 0, p = array; i < length; i++, p += elt_size)
14512 rtx elt = CONST_VECTOR_ELT (rtl, i);
14513 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14515 break;
14517 case MODE_VECTOR_FLOAT:
14518 for (i = 0, p = array; i < length; i++, p += elt_size)
14520 rtx elt = CONST_VECTOR_ELT (rtl, i);
14521 insert_float (elt, p);
14523 break;
14525 default:
14526 gcc_unreachable ();
14529 loc_result = new_loc_descr (DW_OP_implicit_value,
14530 length * elt_size, 0);
14531 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14532 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14533 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14534 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14536 break;
14538 case CONST:
14539 if (mode == VOIDmode
14540 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14541 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14542 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14544 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14545 break;
14547 /* FALLTHROUGH */
14548 case SYMBOL_REF:
14549 if (!const_ok_for_output (rtl))
14550 break;
14551 case LABEL_REF:
14552 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14553 && (dwarf_version >= 4 || !dwarf_strict))
14555 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14556 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14557 vec_safe_push (used_rtx_array, rtl);
14559 break;
14561 case DEBUG_IMPLICIT_PTR:
14562 loc_result = implicit_ptr_descriptor (rtl, 0);
14563 break;
14565 case PLUS:
14566 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14567 && CONST_INT_P (XEXP (rtl, 1)))
14569 loc_result
14570 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14571 break;
14573 /* FALLTHRU */
14574 do_default:
14575 default:
14576 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14577 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14578 && dwarf_version >= 4)
14579 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14581 /* Value expression. */
14582 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14583 if (loc_result)
14584 add_loc_descr (&loc_result,
14585 new_loc_descr (DW_OP_stack_value, 0, 0));
14587 break;
14590 return loc_result;
14593 /* We need to figure out what section we should use as the base for the
14594 address ranges where a given location is valid.
14595 1. If this particular DECL has a section associated with it, use that.
14596 2. If this function has a section associated with it, use that.
14597 3. Otherwise, use the text section.
14598 XXX: If you split a variable across multiple sections, we won't notice. */
14600 static const char *
14601 secname_for_decl (const_tree decl)
14603 const char *secname;
14605 if (VAR_OR_FUNCTION_DECL_P (decl)
14606 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14607 && DECL_SECTION_NAME (decl))
14608 secname = DECL_SECTION_NAME (decl);
14609 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14610 secname = DECL_SECTION_NAME (current_function_decl);
14611 else if (cfun && in_cold_section_p)
14612 secname = crtl->subsections.cold_section_label;
14613 else
14614 secname = text_section_label;
14616 return secname;
14619 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14621 static bool
14622 decl_by_reference_p (tree decl)
14624 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14625 || TREE_CODE (decl) == VAR_DECL)
14626 && DECL_BY_REFERENCE (decl));
14629 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14630 for VARLOC. */
14632 static dw_loc_descr_ref
14633 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14634 enum var_init_status initialized)
14636 int have_address = 0;
14637 dw_loc_descr_ref descr;
14638 machine_mode mode;
14640 if (want_address != 2)
14642 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14643 /* Single part. */
14644 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14646 varloc = PAT_VAR_LOCATION_LOC (varloc);
14647 if (GET_CODE (varloc) == EXPR_LIST)
14648 varloc = XEXP (varloc, 0);
14649 mode = GET_MODE (varloc);
14650 if (MEM_P (varloc))
14652 rtx addr = XEXP (varloc, 0);
14653 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14654 mode, initialized);
14655 if (descr)
14656 have_address = 1;
14657 else
14659 rtx x = avoid_constant_pool_reference (varloc);
14660 if (x != varloc)
14661 descr = mem_loc_descriptor (x, mode, VOIDmode,
14662 initialized);
14665 else
14666 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14668 else
14669 return 0;
14671 else
14673 if (GET_CODE (varloc) == VAR_LOCATION)
14674 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14675 else
14676 mode = DECL_MODE (loc);
14677 descr = loc_descriptor (varloc, mode, initialized);
14678 have_address = 1;
14681 if (!descr)
14682 return 0;
14684 if (want_address == 2 && !have_address
14685 && (dwarf_version >= 4 || !dwarf_strict))
14687 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14689 expansion_failed (loc, NULL_RTX,
14690 "DWARF address size mismatch");
14691 return 0;
14693 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14694 have_address = 1;
14696 /* Show if we can't fill the request for an address. */
14697 if (want_address && !have_address)
14699 expansion_failed (loc, NULL_RTX,
14700 "Want address and only have value");
14701 return 0;
14704 /* If we've got an address and don't want one, dereference. */
14705 if (!want_address && have_address)
14707 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14708 enum dwarf_location_atom op;
14710 if (size > DWARF2_ADDR_SIZE || size == -1)
14712 expansion_failed (loc, NULL_RTX,
14713 "DWARF address size mismatch");
14714 return 0;
14716 else if (size == DWARF2_ADDR_SIZE)
14717 op = DW_OP_deref;
14718 else
14719 op = DW_OP_deref_size;
14721 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14724 return descr;
14727 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14728 if it is not possible. */
14730 static dw_loc_descr_ref
14731 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14733 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14734 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14735 else if (dwarf_version >= 3 || !dwarf_strict)
14736 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14737 else
14738 return NULL;
14741 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14742 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14744 static dw_loc_descr_ref
14745 dw_sra_loc_expr (tree decl, rtx loc)
14747 rtx p;
14748 unsigned HOST_WIDE_INT padsize = 0;
14749 dw_loc_descr_ref descr, *descr_tail;
14750 unsigned HOST_WIDE_INT decl_size;
14751 rtx varloc;
14752 enum var_init_status initialized;
14754 if (DECL_SIZE (decl) == NULL
14755 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14756 return NULL;
14758 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14759 descr = NULL;
14760 descr_tail = &descr;
14762 for (p = loc; p; p = XEXP (p, 1))
14764 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14765 rtx loc_note = *decl_piece_varloc_ptr (p);
14766 dw_loc_descr_ref cur_descr;
14767 dw_loc_descr_ref *tail, last = NULL;
14768 unsigned HOST_WIDE_INT opsize = 0;
14770 if (loc_note == NULL_RTX
14771 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14773 padsize += bitsize;
14774 continue;
14776 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14777 varloc = NOTE_VAR_LOCATION (loc_note);
14778 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14779 if (cur_descr == NULL)
14781 padsize += bitsize;
14782 continue;
14785 /* Check that cur_descr either doesn't use
14786 DW_OP_*piece operations, or their sum is equal
14787 to bitsize. Otherwise we can't embed it. */
14788 for (tail = &cur_descr; *tail != NULL;
14789 tail = &(*tail)->dw_loc_next)
14790 if ((*tail)->dw_loc_opc == DW_OP_piece)
14792 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14793 * BITS_PER_UNIT;
14794 last = *tail;
14796 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14798 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14799 last = *tail;
14802 if (last != NULL && opsize != bitsize)
14804 padsize += bitsize;
14805 /* Discard the current piece of the descriptor and release any
14806 addr_table entries it uses. */
14807 remove_loc_list_addr_table_entries (cur_descr);
14808 continue;
14811 /* If there is a hole, add DW_OP_*piece after empty DWARF
14812 expression, which means that those bits are optimized out. */
14813 if (padsize)
14815 if (padsize > decl_size)
14817 remove_loc_list_addr_table_entries (cur_descr);
14818 goto discard_descr;
14820 decl_size -= padsize;
14821 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14822 if (*descr_tail == NULL)
14824 remove_loc_list_addr_table_entries (cur_descr);
14825 goto discard_descr;
14827 descr_tail = &(*descr_tail)->dw_loc_next;
14828 padsize = 0;
14830 *descr_tail = cur_descr;
14831 descr_tail = tail;
14832 if (bitsize > decl_size)
14833 goto discard_descr;
14834 decl_size -= bitsize;
14835 if (last == NULL)
14837 HOST_WIDE_INT offset = 0;
14838 if (GET_CODE (varloc) == VAR_LOCATION
14839 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14841 varloc = PAT_VAR_LOCATION_LOC (varloc);
14842 if (GET_CODE (varloc) == EXPR_LIST)
14843 varloc = XEXP (varloc, 0);
14847 if (GET_CODE (varloc) == CONST
14848 || GET_CODE (varloc) == SIGN_EXTEND
14849 || GET_CODE (varloc) == ZERO_EXTEND)
14850 varloc = XEXP (varloc, 0);
14851 else if (GET_CODE (varloc) == SUBREG)
14852 varloc = SUBREG_REG (varloc);
14853 else
14854 break;
14856 while (1);
14857 /* DW_OP_bit_size offset should be zero for register
14858 or implicit location descriptions and empty location
14859 descriptions, but for memory addresses needs big endian
14860 adjustment. */
14861 if (MEM_P (varloc))
14863 unsigned HOST_WIDE_INT memsize
14864 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14865 if (memsize != bitsize)
14867 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14868 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14869 goto discard_descr;
14870 if (memsize < bitsize)
14871 goto discard_descr;
14872 if (BITS_BIG_ENDIAN)
14873 offset = memsize - bitsize;
14877 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14878 if (*descr_tail == NULL)
14879 goto discard_descr;
14880 descr_tail = &(*descr_tail)->dw_loc_next;
14884 /* If there were any non-empty expressions, add padding till the end of
14885 the decl. */
14886 if (descr != NULL && decl_size != 0)
14888 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14889 if (*descr_tail == NULL)
14890 goto discard_descr;
14892 return descr;
14894 discard_descr:
14895 /* Discard the descriptor and release any addr_table entries it uses. */
14896 remove_loc_list_addr_table_entries (descr);
14897 return NULL;
14900 /* Return the dwarf representation of the location list LOC_LIST of
14901 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14902 function. */
14904 static dw_loc_list_ref
14905 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14907 const char *endname, *secname;
14908 rtx varloc;
14909 enum var_init_status initialized;
14910 struct var_loc_node *node;
14911 dw_loc_descr_ref descr;
14912 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14913 dw_loc_list_ref list = NULL;
14914 dw_loc_list_ref *listp = &list;
14916 /* Now that we know what section we are using for a base,
14917 actually construct the list of locations.
14918 The first location information is what is passed to the
14919 function that creates the location list, and the remaining
14920 locations just get added on to that list.
14921 Note that we only know the start address for a location
14922 (IE location changes), so to build the range, we use
14923 the range [current location start, next location start].
14924 This means we have to special case the last node, and generate
14925 a range of [last location start, end of function label]. */
14927 secname = secname_for_decl (decl);
14929 for (node = loc_list->first; node; node = node->next)
14930 if (GET_CODE (node->loc) == EXPR_LIST
14931 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14933 if (GET_CODE (node->loc) == EXPR_LIST)
14935 /* This requires DW_OP_{,bit_}piece, which is not usable
14936 inside DWARF expressions. */
14937 if (want_address != 2)
14938 continue;
14939 descr = dw_sra_loc_expr (decl, node->loc);
14940 if (descr == NULL)
14941 continue;
14943 else
14945 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14946 varloc = NOTE_VAR_LOCATION (node->loc);
14947 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14949 if (descr)
14951 bool range_across_switch = false;
14952 /* If section switch happens in between node->label
14953 and node->next->label (or end of function) and
14954 we can't emit it as a single entry list,
14955 emit two ranges, first one ending at the end
14956 of first partition and second one starting at the
14957 beginning of second partition. */
14958 if (node == loc_list->last_before_switch
14959 && (node != loc_list->first || loc_list->first->next)
14960 && current_function_decl)
14962 endname = cfun->fde->dw_fde_end;
14963 range_across_switch = true;
14965 /* The variable has a location between NODE->LABEL and
14966 NODE->NEXT->LABEL. */
14967 else if (node->next)
14968 endname = node->next->label;
14969 /* If the variable has a location at the last label
14970 it keeps its location until the end of function. */
14971 else if (!current_function_decl)
14972 endname = text_end_label;
14973 else
14975 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14976 current_function_funcdef_no);
14977 endname = ggc_strdup (label_id);
14980 *listp = new_loc_list (descr, node->label, endname, secname);
14981 if (TREE_CODE (decl) == PARM_DECL
14982 && node == loc_list->first
14983 && NOTE_P (node->loc)
14984 && strcmp (node->label, endname) == 0)
14985 (*listp)->force = true;
14986 listp = &(*listp)->dw_loc_next;
14988 if (range_across_switch)
14990 if (GET_CODE (node->loc) == EXPR_LIST)
14991 descr = dw_sra_loc_expr (decl, node->loc);
14992 else
14994 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14995 varloc = NOTE_VAR_LOCATION (node->loc);
14996 descr = dw_loc_list_1 (decl, varloc, want_address,
14997 initialized);
14999 gcc_assert (descr);
15000 /* The variable has a location between NODE->LABEL and
15001 NODE->NEXT->LABEL. */
15002 if (node->next)
15003 endname = node->next->label;
15004 else
15005 endname = cfun->fde->dw_fde_second_end;
15006 *listp = new_loc_list (descr,
15007 cfun->fde->dw_fde_second_begin,
15008 endname, secname);
15009 listp = &(*listp)->dw_loc_next;
15014 /* Try to avoid the overhead of a location list emitting a location
15015 expression instead, but only if we didn't have more than one
15016 location entry in the first place. If some entries were not
15017 representable, we don't want to pretend a single entry that was
15018 applies to the entire scope in which the variable is
15019 available. */
15020 if (list && loc_list->first->next)
15021 gen_llsym (list);
15023 return list;
15026 /* Return if the loc_list has only single element and thus can be represented
15027 as location description. */
15029 static bool
15030 single_element_loc_list_p (dw_loc_list_ref list)
15032 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15033 return !list->ll_symbol;
15036 /* To each location in list LIST add loc descr REF. */
15038 static void
15039 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15041 dw_loc_descr_ref copy;
15042 add_loc_descr (&list->expr, ref);
15043 list = list->dw_loc_next;
15044 while (list)
15046 copy = ggc_alloc<dw_loc_descr_node> ();
15047 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15048 add_loc_descr (&list->expr, copy);
15049 while (copy->dw_loc_next)
15051 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15052 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15053 copy->dw_loc_next = new_copy;
15054 copy = new_copy;
15056 list = list->dw_loc_next;
15060 /* Given two lists RET and LIST
15061 produce location list that is result of adding expression in LIST
15062 to expression in RET on each position in program.
15063 Might be destructive on both RET and LIST.
15065 TODO: We handle only simple cases of RET or LIST having at most one
15066 element. General case would inolve sorting the lists in program order
15067 and merging them that will need some additional work.
15068 Adding that will improve quality of debug info especially for SRA-ed
15069 structures. */
15071 static void
15072 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15074 if (!list)
15075 return;
15076 if (!*ret)
15078 *ret = list;
15079 return;
15081 if (!list->dw_loc_next)
15083 add_loc_descr_to_each (*ret, list->expr);
15084 return;
15086 if (!(*ret)->dw_loc_next)
15088 add_loc_descr_to_each (list, (*ret)->expr);
15089 *ret = list;
15090 return;
15092 expansion_failed (NULL_TREE, NULL_RTX,
15093 "Don't know how to merge two non-trivial"
15094 " location lists.\n");
15095 *ret = NULL;
15096 return;
15099 /* LOC is constant expression. Try a luck, look it up in constant
15100 pool and return its loc_descr of its address. */
15102 static dw_loc_descr_ref
15103 cst_pool_loc_descr (tree loc)
15105 /* Get an RTL for this, if something has been emitted. */
15106 rtx rtl = lookup_constant_def (loc);
15108 if (!rtl || !MEM_P (rtl))
15110 gcc_assert (!rtl);
15111 return 0;
15113 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15115 /* TODO: We might get more coverage if we was actually delaying expansion
15116 of all expressions till end of compilation when constant pools are fully
15117 populated. */
15118 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15120 expansion_failed (loc, NULL_RTX,
15121 "CST value in contant pool but not marked.");
15122 return 0;
15124 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15125 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15128 /* Return dw_loc_list representing address of addr_expr LOC
15129 by looking for inner INDIRECT_REF expression and turning
15130 it into simple arithmetics.
15132 See loc_list_from_tree for the meaning of CONTEXT. */
15134 static dw_loc_list_ref
15135 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15136 const loc_descr_context *context)
15138 tree obj, offset;
15139 HOST_WIDE_INT bitsize, bitpos, bytepos;
15140 machine_mode mode;
15141 int unsignedp, reversep, volatilep = 0;
15142 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15144 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15145 &bitsize, &bitpos, &offset, &mode,
15146 &unsignedp, &reversep, &volatilep, false);
15147 STRIP_NOPS (obj);
15148 if (bitpos % BITS_PER_UNIT)
15150 expansion_failed (loc, NULL_RTX, "bitfield access");
15151 return 0;
15153 if (!INDIRECT_REF_P (obj))
15155 expansion_failed (obj,
15156 NULL_RTX, "no indirect ref in inner refrence");
15157 return 0;
15159 if (!offset && !bitpos)
15160 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15161 context);
15162 else if (toplev
15163 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15164 && (dwarf_version >= 4 || !dwarf_strict))
15166 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15167 if (!list_ret)
15168 return 0;
15169 if (offset)
15171 /* Variable offset. */
15172 list_ret1 = loc_list_from_tree (offset, 0, context);
15173 if (list_ret1 == 0)
15174 return 0;
15175 add_loc_list (&list_ret, list_ret1);
15176 if (!list_ret)
15177 return 0;
15178 add_loc_descr_to_each (list_ret,
15179 new_loc_descr (DW_OP_plus, 0, 0));
15181 bytepos = bitpos / BITS_PER_UNIT;
15182 if (bytepos > 0)
15183 add_loc_descr_to_each (list_ret,
15184 new_loc_descr (DW_OP_plus_uconst,
15185 bytepos, 0));
15186 else if (bytepos < 0)
15187 loc_list_plus_const (list_ret, bytepos);
15188 add_loc_descr_to_each (list_ret,
15189 new_loc_descr (DW_OP_stack_value, 0, 0));
15191 return list_ret;
15194 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15195 all operations from LOC are nops, move to the last one. Insert in NOPS all
15196 operations that are skipped. */
15198 static void
15199 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15200 hash_set<dw_loc_descr_ref> &nops)
15202 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15204 nops.add (loc);
15205 loc = loc->dw_loc_next;
15209 /* Helper for loc_descr_without_nops: free the location description operation
15210 P. */
15212 bool
15213 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15215 ggc_free (loc);
15216 return true;
15219 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15220 finishes LOC. */
15222 static void
15223 loc_descr_without_nops (dw_loc_descr_ref &loc)
15225 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15226 return;
15228 /* Set of all DW_OP_nop operations we remove. */
15229 hash_set<dw_loc_descr_ref> nops;
15231 /* First, strip all prefix NOP operations in order to keep the head of the
15232 operations list. */
15233 loc_descr_to_next_no_nop (loc, nops);
15235 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15237 /* For control flow operations: strip "prefix" nops in destination
15238 labels. */
15239 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15240 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15241 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15242 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15244 /* Do the same for the operations that follow, then move to the next
15245 iteration. */
15246 if (cur->dw_loc_next != NULL)
15247 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15248 cur = cur->dw_loc_next;
15251 nops.traverse<void *, free_loc_descr> (NULL);
15255 struct dwarf_procedure_info;
15257 /* Helper structure for location descriptions generation. */
15258 struct loc_descr_context
15260 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15261 NULL_TREE if DW_OP_push_object_address in invalid for this location
15262 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15263 tree context_type;
15264 /* The ..._DECL node that should be translated as a
15265 DW_OP_push_object_address operation. */
15266 tree base_decl;
15267 /* Information about the DWARF procedure we are currently generating. NULL if
15268 we are not generating a DWARF procedure. */
15269 struct dwarf_procedure_info *dpi;
15272 /* DWARF procedures generation
15274 DWARF expressions (aka. location descriptions) are used to encode variable
15275 things such as sizes or offsets. Such computations can have redundant parts
15276 that can be factorized in order to reduce the size of the output debug
15277 information. This is the whole point of DWARF procedures.
15279 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15280 already factorized into functions ("size functions") in order to handle very
15281 big and complex types. Such functions are quite simple: they have integral
15282 arguments, they return an integral result and their body contains only a
15283 return statement with arithmetic expressions. This is the only kind of
15284 function we are interested in translating into DWARF procedures, here.
15286 DWARF expressions and DWARF procedure are executed using a stack, so we have
15287 to define some calling convention for them to interact. Let's say that:
15289 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15290 all arguments in reverse order (right-to-left) so that when the DWARF
15291 procedure execution starts, the first argument is the top of the stack.
15293 - Then, when returning, the DWARF procedure must have consumed all arguments
15294 on the stack, must have pushed the result and touched nothing else.
15296 - Each integral argument and the result are integral types can be hold in a
15297 single stack slot.
15299 - We call "frame offset" the number of stack slots that are "under DWARF
15300 procedure control": it includes the arguments slots, the temporaries and
15301 the result slot. Thus, it is equal to the number of arguments when the
15302 procedure execution starts and must be equal to one (the result) when it
15303 returns. */
15305 /* Helper structure used when generating operations for a DWARF procedure. */
15306 struct dwarf_procedure_info
15308 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15309 currently translated. */
15310 tree fndecl;
15311 /* The number of arguments FNDECL takes. */
15312 unsigned args_count;
15315 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15316 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15317 equate it to this DIE. */
15319 static dw_die_ref
15320 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15321 dw_die_ref parent_die)
15323 const bool dwarf_proc_supported = dwarf_version >= 4;
15324 dw_die_ref dwarf_proc_die;
15326 if ((dwarf_version < 3 && dwarf_strict)
15327 || location == NULL)
15328 return NULL;
15330 dwarf_proc_die = new_die (dwarf_proc_supported
15331 ? DW_TAG_dwarf_procedure
15332 : DW_TAG_variable,
15333 parent_die,
15334 fndecl);
15335 if (fndecl)
15336 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15337 if (!dwarf_proc_supported)
15338 add_AT_flag (dwarf_proc_die, DW_AT_artificial, 1);
15339 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15340 return dwarf_proc_die;
15343 /* Return whether TYPE is a supported type as a DWARF procedure argument
15344 type or return type (we handle only scalar types and pointer types that
15345 aren't wider than the DWARF expression evaluation stack. */
15347 static bool
15348 is_handled_procedure_type (tree type)
15350 return ((INTEGRAL_TYPE_P (type)
15351 || TREE_CODE (type) == OFFSET_TYPE
15352 || TREE_CODE (type) == POINTER_TYPE)
15353 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15356 /* Helper for resolve_args_picking. Stop when coming across VISITED nodes. */
15358 static bool
15359 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15360 struct dwarf_procedure_info *dpi,
15361 hash_set<dw_loc_descr_ref> &visited)
15363 /* The "frame_offset" identifier is already used to name a macro... */
15364 unsigned frame_offset_ = initial_frame_offset;
15365 dw_loc_descr_ref l;
15367 for (l = loc; l != NULL;)
15369 /* If we already met this node, there is nothing to compute anymore. */
15370 if (visited.add (l))
15372 #if CHECKING_P
15373 /* Make sure that the stack size is consistent wherever the execution
15374 flow comes from. */
15375 gcc_assert ((unsigned) l->dw_loc_frame_offset == frame_offset_);
15376 #endif
15377 break;
15379 #if CHECKING_P
15380 l->dw_loc_frame_offset = frame_offset_;
15381 #endif
15383 /* If needed, relocate the picking offset with respect to the frame
15384 offset. */
15385 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15387 /* frame_offset_ is the size of the current stack frame, including
15388 incoming arguments. Besides, the arguments are pushed
15389 right-to-left. Thus, in order to access the Nth argument from
15390 this operation node, the picking has to skip temporaries *plus*
15391 one stack slot per argument (0 for the first one, 1 for the second
15392 one, etc.).
15394 The targetted argument number (N) is already set as the operand,
15395 and the number of temporaries can be computed with:
15396 frame_offsets_ - dpi->args_count */
15397 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15399 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15400 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15401 return false;
15404 /* Update frame_offset according to the effect the current operation has
15405 on the stack. */
15406 switch (l->dw_loc_opc)
15408 case DW_OP_deref:
15409 case DW_OP_swap:
15410 case DW_OP_rot:
15411 case DW_OP_abs:
15412 case DW_OP_not:
15413 case DW_OP_plus_uconst:
15414 case DW_OP_skip:
15415 case DW_OP_reg0:
15416 case DW_OP_reg1:
15417 case DW_OP_reg2:
15418 case DW_OP_reg3:
15419 case DW_OP_reg4:
15420 case DW_OP_reg5:
15421 case DW_OP_reg6:
15422 case DW_OP_reg7:
15423 case DW_OP_reg8:
15424 case DW_OP_reg9:
15425 case DW_OP_reg10:
15426 case DW_OP_reg11:
15427 case DW_OP_reg12:
15428 case DW_OP_reg13:
15429 case DW_OP_reg14:
15430 case DW_OP_reg15:
15431 case DW_OP_reg16:
15432 case DW_OP_reg17:
15433 case DW_OP_reg18:
15434 case DW_OP_reg19:
15435 case DW_OP_reg20:
15436 case DW_OP_reg21:
15437 case DW_OP_reg22:
15438 case DW_OP_reg23:
15439 case DW_OP_reg24:
15440 case DW_OP_reg25:
15441 case DW_OP_reg26:
15442 case DW_OP_reg27:
15443 case DW_OP_reg28:
15444 case DW_OP_reg29:
15445 case DW_OP_reg30:
15446 case DW_OP_reg31:
15447 case DW_OP_bregx:
15448 case DW_OP_piece:
15449 case DW_OP_deref_size:
15450 case DW_OP_nop:
15451 case DW_OP_form_tls_address:
15452 case DW_OP_bit_piece:
15453 case DW_OP_implicit_value:
15454 case DW_OP_stack_value:
15455 break;
15457 case DW_OP_addr:
15458 case DW_OP_const1u:
15459 case DW_OP_const1s:
15460 case DW_OP_const2u:
15461 case DW_OP_const2s:
15462 case DW_OP_const4u:
15463 case DW_OP_const4s:
15464 case DW_OP_const8u:
15465 case DW_OP_const8s:
15466 case DW_OP_constu:
15467 case DW_OP_consts:
15468 case DW_OP_dup:
15469 case DW_OP_over:
15470 case DW_OP_pick:
15471 case DW_OP_lit0:
15472 case DW_OP_lit1:
15473 case DW_OP_lit2:
15474 case DW_OP_lit3:
15475 case DW_OP_lit4:
15476 case DW_OP_lit5:
15477 case DW_OP_lit6:
15478 case DW_OP_lit7:
15479 case DW_OP_lit8:
15480 case DW_OP_lit9:
15481 case DW_OP_lit10:
15482 case DW_OP_lit11:
15483 case DW_OP_lit12:
15484 case DW_OP_lit13:
15485 case DW_OP_lit14:
15486 case DW_OP_lit15:
15487 case DW_OP_lit16:
15488 case DW_OP_lit17:
15489 case DW_OP_lit18:
15490 case DW_OP_lit19:
15491 case DW_OP_lit20:
15492 case DW_OP_lit21:
15493 case DW_OP_lit22:
15494 case DW_OP_lit23:
15495 case DW_OP_lit24:
15496 case DW_OP_lit25:
15497 case DW_OP_lit26:
15498 case DW_OP_lit27:
15499 case DW_OP_lit28:
15500 case DW_OP_lit29:
15501 case DW_OP_lit30:
15502 case DW_OP_lit31:
15503 case DW_OP_breg0:
15504 case DW_OP_breg1:
15505 case DW_OP_breg2:
15506 case DW_OP_breg3:
15507 case DW_OP_breg4:
15508 case DW_OP_breg5:
15509 case DW_OP_breg6:
15510 case DW_OP_breg7:
15511 case DW_OP_breg8:
15512 case DW_OP_breg9:
15513 case DW_OP_breg10:
15514 case DW_OP_breg11:
15515 case DW_OP_breg12:
15516 case DW_OP_breg13:
15517 case DW_OP_breg14:
15518 case DW_OP_breg15:
15519 case DW_OP_breg16:
15520 case DW_OP_breg17:
15521 case DW_OP_breg18:
15522 case DW_OP_breg19:
15523 case DW_OP_breg20:
15524 case DW_OP_breg21:
15525 case DW_OP_breg22:
15526 case DW_OP_breg23:
15527 case DW_OP_breg24:
15528 case DW_OP_breg25:
15529 case DW_OP_breg26:
15530 case DW_OP_breg27:
15531 case DW_OP_breg28:
15532 case DW_OP_breg29:
15533 case DW_OP_breg30:
15534 case DW_OP_breg31:
15535 case DW_OP_fbreg:
15536 case DW_OP_push_object_address:
15537 case DW_OP_call_frame_cfa:
15538 ++frame_offset_;
15539 break;
15541 case DW_OP_drop:
15542 case DW_OP_xderef:
15543 case DW_OP_and:
15544 case DW_OP_div:
15545 case DW_OP_minus:
15546 case DW_OP_mod:
15547 case DW_OP_mul:
15548 case DW_OP_neg:
15549 case DW_OP_or:
15550 case DW_OP_plus:
15551 case DW_OP_shl:
15552 case DW_OP_shr:
15553 case DW_OP_shra:
15554 case DW_OP_xor:
15555 case DW_OP_bra:
15556 case DW_OP_eq:
15557 case DW_OP_ge:
15558 case DW_OP_gt:
15559 case DW_OP_le:
15560 case DW_OP_lt:
15561 case DW_OP_ne:
15562 case DW_OP_regx:
15563 case DW_OP_xderef_size:
15564 --frame_offset_;
15565 break;
15567 case DW_OP_call2:
15568 case DW_OP_call4:
15569 case DW_OP_call_ref:
15571 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15572 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15574 if (stack_usage == NULL)
15575 return false;
15576 frame_offset += *stack_usage;
15577 break;
15580 case DW_OP_GNU_push_tls_address:
15581 case DW_OP_GNU_uninit:
15582 case DW_OP_GNU_encoded_addr:
15583 case DW_OP_GNU_implicit_pointer:
15584 case DW_OP_GNU_entry_value:
15585 case DW_OP_GNU_const_type:
15586 case DW_OP_GNU_regval_type:
15587 case DW_OP_GNU_deref_type:
15588 case DW_OP_GNU_convert:
15589 case DW_OP_GNU_reinterpret:
15590 case DW_OP_GNU_parameter_ref:
15591 /* loc_list_from_tree will probably not output these operations for
15592 size functions, so assume they will not appear here. */
15593 /* Fall through... */
15595 default:
15596 gcc_unreachable ();
15599 /* Now, follow the control flow (except subroutine calls). */
15600 switch (l->dw_loc_opc)
15602 case DW_OP_bra:
15603 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15604 visited))
15605 return false;
15606 /* Fall through... */
15608 case DW_OP_skip:
15609 l = l->dw_loc_oprnd1.v.val_loc;
15610 break;
15612 case DW_OP_stack_value:
15613 return true;
15615 default:
15616 l = l->dw_loc_next;
15617 break;
15621 return true;
15624 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15625 operations) in order to resolve the operand of DW_OP_pick operations that
15626 target DWARF procedure arguments (DPI). Stop at already visited nodes.
15627 INITIAL_FRAME_OFFSET is the frame offset *before* LOC is executed. Return
15628 if all relocations were successful. */
15630 static bool
15631 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15632 struct dwarf_procedure_info *dpi)
15634 hash_set<dw_loc_descr_ref> visited;
15636 return resolve_args_picking_1 (loc, initial_frame_offset, dpi, visited);
15639 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15640 Return NULL if it is not possible. */
15642 static dw_die_ref
15643 function_to_dwarf_procedure (tree fndecl)
15645 struct loc_descr_context ctx;
15646 struct dwarf_procedure_info dpi;
15647 dw_die_ref dwarf_proc_die;
15648 tree tree_body = DECL_SAVED_TREE (fndecl);
15649 dw_loc_descr_ref loc_body, epilogue;
15651 tree cursor;
15652 unsigned i;
15654 /* Do not generate multiple DWARF procedures for the same function
15655 declaration. */
15656 dwarf_proc_die = lookup_decl_die (fndecl);
15657 if (dwarf_proc_die != NULL)
15658 return dwarf_proc_die;
15660 /* DWARF procedures are available starting with the DWARFv3 standard, but
15661 it's the DWARFv4 standard that introduces the DW_TAG_dwarf_procedure
15662 DIE. */
15663 if (dwarf_version < 3 && dwarf_strict)
15664 return NULL;
15666 /* We handle only functions for which we still have a body, that return a
15667 supported type and that takes arguments with supported types. Note that
15668 there is no point translating functions that return nothing. */
15669 if (tree_body == NULL_TREE
15670 || DECL_RESULT (fndecl) == NULL_TREE
15671 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15672 return NULL;
15674 for (cursor = DECL_ARGUMENTS (fndecl);
15675 cursor != NULL_TREE;
15676 cursor = TREE_CHAIN (cursor))
15677 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15678 return NULL;
15680 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15681 if (TREE_CODE (tree_body) != RETURN_EXPR)
15682 return NULL;
15683 tree_body = TREE_OPERAND (tree_body, 0);
15684 if (TREE_CODE (tree_body) != MODIFY_EXPR
15685 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15686 return NULL;
15687 tree_body = TREE_OPERAND (tree_body, 1);
15689 /* Try to translate the body expression itself. Note that this will probably
15690 cause an infinite recursion if its call graph has a cycle. This is very
15691 unlikely for size functions, however, so don't bother with such things at
15692 the moment. */
15693 ctx.context_type = NULL_TREE;
15694 ctx.base_decl = NULL_TREE;
15695 ctx.dpi = &dpi;
15696 dpi.fndecl = fndecl;
15697 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15698 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15699 if (!loc_body)
15700 return NULL;
15702 /* After evaluating all operands in "loc_body", we should still have on the
15703 stack all arguments plus the desired function result (top of the stack).
15704 Generate code in order to keep only the result in our stack frame. */
15705 epilogue = NULL;
15706 for (i = 0; i < dpi.args_count; ++i)
15708 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15709 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15710 op_couple->dw_loc_next->dw_loc_next = epilogue;
15711 epilogue = op_couple;
15713 add_loc_descr (&loc_body, epilogue);
15714 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15715 return NULL;
15717 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15718 because they are considered useful. Now there is an epilogue, they are
15719 not anymore, so give it another try. */
15720 loc_descr_without_nops (loc_body);
15722 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15723 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15724 though, given that size functions do not come from source, so they should
15725 not have a dedicated DW_TAG_subprogram DIE. */
15726 dwarf_proc_die
15727 = new_dwarf_proc_die (loc_body, fndecl,
15728 get_context_die (DECL_CONTEXT (fndecl)));
15730 /* The called DWARF procedure consumes one stack slot per argument and
15731 returns one stack slot. */
15732 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15734 return dwarf_proc_die;
15738 /* Generate Dwarf location list representing LOC.
15739 If WANT_ADDRESS is false, expression computing LOC will be computed
15740 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15741 if WANT_ADDRESS is 2, expression computing address useable in location
15742 will be returned (i.e. DW_OP_reg can be used
15743 to refer to register values).
15745 CONTEXT provides information to customize the location descriptions
15746 generation. Its context_type field specifies what type is implicitly
15747 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15748 will not be generated.
15750 Its DPI field determines whether we are generating a DWARF expression for a
15751 DWARF procedure, so PARM_DECL references are processed specifically.
15753 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15754 and dpi fields were null. */
15756 static dw_loc_list_ref
15757 loc_list_from_tree_1 (tree loc, int want_address,
15758 const struct loc_descr_context *context)
15760 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15761 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15762 int have_address = 0;
15763 enum dwarf_location_atom op;
15765 /* ??? Most of the time we do not take proper care for sign/zero
15766 extending the values properly. Hopefully this won't be a real
15767 problem... */
15769 if (context != NULL
15770 && context->base_decl == loc
15771 && want_address == 0)
15773 if (dwarf_version >= 3 || !dwarf_strict)
15774 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15775 NULL, NULL, NULL);
15776 else
15777 return NULL;
15780 switch (TREE_CODE (loc))
15782 case ERROR_MARK:
15783 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15784 return 0;
15786 case PLACEHOLDER_EXPR:
15787 /* This case involves extracting fields from an object to determine the
15788 position of other fields. It is supposed to appear only as the first
15789 operand of COMPONENT_REF nodes and to reference precisely the type
15790 that the context allows. */
15791 if (context != NULL
15792 && TREE_TYPE (loc) == context->context_type
15793 && want_address >= 1)
15795 if (dwarf_version >= 3 || !dwarf_strict)
15797 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15798 have_address = 1;
15799 break;
15801 else
15802 return NULL;
15804 else
15805 expansion_failed (loc, NULL_RTX,
15806 "PLACEHOLDER_EXPR for an unexpected type");
15807 break;
15809 case CALL_EXPR:
15811 const int nargs = call_expr_nargs (loc);
15812 tree callee = get_callee_fndecl (loc);
15813 int i;
15814 dw_die_ref dwarf_proc;
15816 if (callee == NULL_TREE)
15817 goto call_expansion_failed;
15819 /* We handle only functions that return an integer. */
15820 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15821 goto call_expansion_failed;
15823 dwarf_proc = function_to_dwarf_procedure (callee);
15824 if (dwarf_proc == NULL)
15825 goto call_expansion_failed;
15827 /* Evaluate arguments right-to-left so that the first argument will
15828 be the top-most one on the stack. */
15829 for (i = nargs - 1; i >= 0; --i)
15831 dw_loc_descr_ref loc_descr
15832 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15833 context);
15835 if (loc_descr == NULL)
15836 goto call_expansion_failed;
15838 add_loc_descr (&ret, loc_descr);
15841 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15842 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15843 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15844 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15845 add_loc_descr (&ret, ret1);
15846 break;
15848 call_expansion_failed:
15849 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15850 /* There are no opcodes for these operations. */
15851 return 0;
15854 case PREINCREMENT_EXPR:
15855 case PREDECREMENT_EXPR:
15856 case POSTINCREMENT_EXPR:
15857 case POSTDECREMENT_EXPR:
15858 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15859 /* There are no opcodes for these operations. */
15860 return 0;
15862 case ADDR_EXPR:
15863 /* If we already want an address, see if there is INDIRECT_REF inside
15864 e.g. for &this->field. */
15865 if (want_address)
15867 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15868 (loc, want_address == 2, context);
15869 if (list_ret)
15870 have_address = 1;
15871 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15872 && (ret = cst_pool_loc_descr (loc)))
15873 have_address = 1;
15875 /* Otherwise, process the argument and look for the address. */
15876 if (!list_ret && !ret)
15877 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15878 else
15880 if (want_address)
15881 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15882 return NULL;
15884 break;
15886 case VAR_DECL:
15887 if (DECL_THREAD_LOCAL_P (loc))
15889 rtx rtl;
15890 enum dwarf_location_atom tls_op;
15891 enum dtprel_bool dtprel = dtprel_false;
15893 if (targetm.have_tls)
15895 /* If this is not defined, we have no way to emit the
15896 data. */
15897 if (!targetm.asm_out.output_dwarf_dtprel)
15898 return 0;
15900 /* The way DW_OP_GNU_push_tls_address is specified, we
15901 can only look up addresses of objects in the current
15902 module. We used DW_OP_addr as first op, but that's
15903 wrong, because DW_OP_addr is relocated by the debug
15904 info consumer, while DW_OP_GNU_push_tls_address
15905 operand shouldn't be. */
15906 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15907 return 0;
15908 dtprel = dtprel_true;
15909 tls_op = DW_OP_GNU_push_tls_address;
15911 else
15913 if (!targetm.emutls.debug_form_tls_address
15914 || !(dwarf_version >= 3 || !dwarf_strict))
15915 return 0;
15916 /* We stuffed the control variable into the DECL_VALUE_EXPR
15917 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15918 no longer appear in gimple code. We used the control
15919 variable in specific so that we could pick it up here. */
15920 loc = DECL_VALUE_EXPR (loc);
15921 tls_op = DW_OP_form_tls_address;
15924 rtl = rtl_for_decl_location (loc);
15925 if (rtl == NULL_RTX)
15926 return 0;
15928 if (!MEM_P (rtl))
15929 return 0;
15930 rtl = XEXP (rtl, 0);
15931 if (! CONSTANT_P (rtl))
15932 return 0;
15934 ret = new_addr_loc_descr (rtl, dtprel);
15935 ret1 = new_loc_descr (tls_op, 0, 0);
15936 add_loc_descr (&ret, ret1);
15938 have_address = 1;
15939 break;
15941 /* FALLTHRU */
15943 case PARM_DECL:
15944 if (context != NULL && context->dpi != NULL
15945 && DECL_CONTEXT (loc) == context->dpi->fndecl)
15947 /* We are generating code for a DWARF procedure and we want to access
15948 one of its arguments: find the appropriate argument offset and let
15949 the resolve_args_picking pass compute the offset that complies
15950 with the stack frame size. */
15951 unsigned i = 0;
15952 tree cursor;
15954 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
15955 cursor != NULL_TREE && cursor != loc;
15956 cursor = TREE_CHAIN (cursor), ++i)
15958 /* If we are translating a DWARF procedure, all referenced parameters
15959 must belong to the current function. */
15960 gcc_assert (cursor != NULL_TREE);
15962 ret = new_loc_descr (DW_OP_pick, i, 0);
15963 ret->frame_offset_rel = 1;
15964 break;
15966 /* FALLTHRU */
15968 case RESULT_DECL:
15969 if (DECL_HAS_VALUE_EXPR_P (loc))
15970 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
15971 want_address, context);
15972 /* FALLTHRU */
15974 case FUNCTION_DECL:
15976 rtx rtl;
15977 var_loc_list *loc_list = lookup_decl_loc (loc);
15979 if (loc_list && loc_list->first)
15981 list_ret = dw_loc_list (loc_list, loc, want_address);
15982 have_address = want_address != 0;
15983 break;
15985 rtl = rtl_for_decl_location (loc);
15986 if (rtl == NULL_RTX)
15988 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15989 return 0;
15991 else if (CONST_INT_P (rtl))
15993 HOST_WIDE_INT val = INTVAL (rtl);
15994 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15995 val &= GET_MODE_MASK (DECL_MODE (loc));
15996 ret = int_loc_descriptor (val);
15998 else if (GET_CODE (rtl) == CONST_STRING)
16000 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16001 return 0;
16003 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16004 ret = new_addr_loc_descr (rtl, dtprel_false);
16005 else
16007 machine_mode mode, mem_mode;
16009 /* Certain constructs can only be represented at top-level. */
16010 if (want_address == 2)
16012 ret = loc_descriptor (rtl, VOIDmode,
16013 VAR_INIT_STATUS_INITIALIZED);
16014 have_address = 1;
16016 else
16018 mode = GET_MODE (rtl);
16019 mem_mode = VOIDmode;
16020 if (MEM_P (rtl))
16022 mem_mode = mode;
16023 mode = get_address_mode (rtl);
16024 rtl = XEXP (rtl, 0);
16025 have_address = 1;
16027 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16028 VAR_INIT_STATUS_INITIALIZED);
16030 if (!ret)
16031 expansion_failed (loc, rtl,
16032 "failed to produce loc descriptor for rtl");
16035 break;
16037 case MEM_REF:
16038 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16040 have_address = 1;
16041 goto do_plus;
16043 /* Fallthru. */
16044 case INDIRECT_REF:
16045 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16046 have_address = 1;
16047 break;
16049 case TARGET_MEM_REF:
16050 case SSA_NAME:
16051 case DEBUG_EXPR_DECL:
16052 return NULL;
16054 case COMPOUND_EXPR:
16055 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16056 context);
16058 CASE_CONVERT:
16059 case VIEW_CONVERT_EXPR:
16060 case SAVE_EXPR:
16061 case MODIFY_EXPR:
16062 case NON_LVALUE_EXPR:
16063 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16064 context);
16066 case COMPONENT_REF:
16067 case BIT_FIELD_REF:
16068 case ARRAY_REF:
16069 case ARRAY_RANGE_REF:
16070 case REALPART_EXPR:
16071 case IMAGPART_EXPR:
16073 tree obj, offset;
16074 HOST_WIDE_INT bitsize, bitpos, bytepos;
16075 machine_mode mode;
16076 int unsignedp, reversep, volatilep = 0;
16078 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16079 &unsignedp, &reversep, &volatilep, false);
16081 gcc_assert (obj != loc);
16083 list_ret = loc_list_from_tree_1 (obj,
16084 want_address == 2
16085 && !bitpos && !offset ? 2 : 1,
16086 context);
16087 /* TODO: We can extract value of the small expression via shifting even
16088 for nonzero bitpos. */
16089 if (list_ret == 0)
16090 return 0;
16091 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16093 expansion_failed (loc, NULL_RTX,
16094 "bitfield access");
16095 return 0;
16098 if (offset != NULL_TREE)
16100 /* Variable offset. */
16101 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16102 if (list_ret1 == 0)
16103 return 0;
16104 add_loc_list (&list_ret, list_ret1);
16105 if (!list_ret)
16106 return 0;
16107 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16110 bytepos = bitpos / BITS_PER_UNIT;
16111 if (bytepos > 0)
16112 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16113 else if (bytepos < 0)
16114 loc_list_plus_const (list_ret, bytepos);
16116 have_address = 1;
16117 break;
16120 case INTEGER_CST:
16121 if ((want_address || !tree_fits_shwi_p (loc))
16122 && (ret = cst_pool_loc_descr (loc)))
16123 have_address = 1;
16124 else if (want_address == 2
16125 && tree_fits_shwi_p (loc)
16126 && (ret = address_of_int_loc_descriptor
16127 (int_size_in_bytes (TREE_TYPE (loc)),
16128 tree_to_shwi (loc))))
16129 have_address = 1;
16130 else if (tree_fits_shwi_p (loc))
16131 ret = int_loc_descriptor (tree_to_shwi (loc));
16132 else if (tree_fits_uhwi_p (loc))
16133 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16134 else
16136 expansion_failed (loc, NULL_RTX,
16137 "Integer operand is not host integer");
16138 return 0;
16140 break;
16142 case CONSTRUCTOR:
16143 case REAL_CST:
16144 case STRING_CST:
16145 case COMPLEX_CST:
16146 if ((ret = cst_pool_loc_descr (loc)))
16147 have_address = 1;
16148 else
16149 /* We can construct small constants here using int_loc_descriptor. */
16150 expansion_failed (loc, NULL_RTX,
16151 "constructor or constant not in constant pool");
16152 break;
16154 case TRUTH_AND_EXPR:
16155 case TRUTH_ANDIF_EXPR:
16156 case BIT_AND_EXPR:
16157 op = DW_OP_and;
16158 goto do_binop;
16160 case TRUTH_XOR_EXPR:
16161 case BIT_XOR_EXPR:
16162 op = DW_OP_xor;
16163 goto do_binop;
16165 case TRUTH_OR_EXPR:
16166 case TRUTH_ORIF_EXPR:
16167 case BIT_IOR_EXPR:
16168 op = DW_OP_or;
16169 goto do_binop;
16171 case FLOOR_DIV_EXPR:
16172 case CEIL_DIV_EXPR:
16173 case ROUND_DIV_EXPR:
16174 case TRUNC_DIV_EXPR:
16175 case EXACT_DIV_EXPR:
16176 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16177 return 0;
16178 op = DW_OP_div;
16179 goto do_binop;
16181 case MINUS_EXPR:
16182 op = DW_OP_minus;
16183 goto do_binop;
16185 case FLOOR_MOD_EXPR:
16186 case CEIL_MOD_EXPR:
16187 case ROUND_MOD_EXPR:
16188 case TRUNC_MOD_EXPR:
16189 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16191 op = DW_OP_mod;
16192 goto do_binop;
16194 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16195 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16196 if (list_ret == 0 || list_ret1 == 0)
16197 return 0;
16199 add_loc_list (&list_ret, list_ret1);
16200 if (list_ret == 0)
16201 return 0;
16202 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16203 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16204 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16205 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16206 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16207 break;
16209 case MULT_EXPR:
16210 op = DW_OP_mul;
16211 goto do_binop;
16213 case LSHIFT_EXPR:
16214 op = DW_OP_shl;
16215 goto do_binop;
16217 case RSHIFT_EXPR:
16218 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16219 goto do_binop;
16221 case POINTER_PLUS_EXPR:
16222 case PLUS_EXPR:
16223 do_plus:
16224 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16226 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16227 smarter to encode their opposite. The DW_OP_plus_uconst operation
16228 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16229 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16230 bytes, Y being the size of the operation that pushes the opposite
16231 of the addend. So let's choose the smallest representation. */
16232 const tree tree_addend = TREE_OPERAND (loc, 1);
16233 offset_int wi_addend;
16234 HOST_WIDE_INT shwi_addend;
16235 dw_loc_descr_ref loc_naddend;
16237 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16238 if (list_ret == 0)
16239 return 0;
16241 /* Try to get the literal to push. It is the opposite of the addend,
16242 so as we rely on wrapping during DWARF evaluation, first decode
16243 the literal as a "DWARF-sized" signed number. */
16244 wi_addend = wi::to_offset (tree_addend);
16245 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16246 shwi_addend = wi_addend.to_shwi ();
16247 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16248 ? int_loc_descriptor (-shwi_addend)
16249 : NULL;
16251 if (loc_naddend != NULL
16252 && ((unsigned) size_of_uleb128 (shwi_addend)
16253 > size_of_loc_descr (loc_naddend)))
16255 add_loc_descr_to_each (list_ret, loc_naddend);
16256 add_loc_descr_to_each (list_ret,
16257 new_loc_descr (DW_OP_minus, 0, 0));
16259 else
16261 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16263 loc_naddend = loc_cur;
16264 loc_cur = loc_cur->dw_loc_next;
16265 ggc_free (loc_naddend);
16267 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16269 break;
16272 op = DW_OP_plus;
16273 goto do_binop;
16275 case LE_EXPR:
16276 op = DW_OP_le;
16277 goto do_comp_binop;
16279 case GE_EXPR:
16280 op = DW_OP_ge;
16281 goto do_comp_binop;
16283 case LT_EXPR:
16284 op = DW_OP_lt;
16285 goto do_comp_binop;
16287 case GT_EXPR:
16288 op = DW_OP_gt;
16289 goto do_comp_binop;
16291 do_comp_binop:
16292 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16294 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16295 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16296 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16297 TREE_CODE (loc));
16298 break;
16300 else
16301 goto do_binop;
16303 case EQ_EXPR:
16304 op = DW_OP_eq;
16305 goto do_binop;
16307 case NE_EXPR:
16308 op = DW_OP_ne;
16309 goto do_binop;
16311 do_binop:
16312 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16313 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16314 if (list_ret == 0 || list_ret1 == 0)
16315 return 0;
16317 add_loc_list (&list_ret, list_ret1);
16318 if (list_ret == 0)
16319 return 0;
16320 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16321 break;
16323 case TRUTH_NOT_EXPR:
16324 case BIT_NOT_EXPR:
16325 op = DW_OP_not;
16326 goto do_unop;
16328 case ABS_EXPR:
16329 op = DW_OP_abs;
16330 goto do_unop;
16332 case NEGATE_EXPR:
16333 op = DW_OP_neg;
16334 goto do_unop;
16336 do_unop:
16337 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16338 if (list_ret == 0)
16339 return 0;
16341 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16342 break;
16344 case MIN_EXPR:
16345 case MAX_EXPR:
16347 const enum tree_code code =
16348 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16350 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16351 build2 (code, integer_type_node,
16352 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16353 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16356 /* ... fall through ... */
16358 case COND_EXPR:
16360 dw_loc_descr_ref lhs
16361 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16362 dw_loc_list_ref rhs
16363 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16364 dw_loc_descr_ref bra_node, jump_node, tmp;
16366 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16367 if (list_ret == 0 || lhs == 0 || rhs == 0)
16368 return 0;
16370 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16371 add_loc_descr_to_each (list_ret, bra_node);
16373 add_loc_list (&list_ret, rhs);
16374 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16375 add_loc_descr_to_each (list_ret, jump_node);
16377 add_loc_descr_to_each (list_ret, lhs);
16378 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16379 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16381 /* ??? Need a node to point the skip at. Use a nop. */
16382 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16383 add_loc_descr_to_each (list_ret, tmp);
16384 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16385 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16387 break;
16389 case FIX_TRUNC_EXPR:
16390 return 0;
16392 default:
16393 /* Leave front-end specific codes as simply unknown. This comes
16394 up, for instance, with the C STMT_EXPR. */
16395 if ((unsigned int) TREE_CODE (loc)
16396 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16398 expansion_failed (loc, NULL_RTX,
16399 "language specific tree node");
16400 return 0;
16403 /* Otherwise this is a generic code; we should just lists all of
16404 these explicitly. We forgot one. */
16405 if (flag_checking)
16406 gcc_unreachable ();
16408 /* In a release build, we want to degrade gracefully: better to
16409 generate incomplete debugging information than to crash. */
16410 return NULL;
16413 if (!ret && !list_ret)
16414 return 0;
16416 if (want_address == 2 && !have_address
16417 && (dwarf_version >= 4 || !dwarf_strict))
16419 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16421 expansion_failed (loc, NULL_RTX,
16422 "DWARF address size mismatch");
16423 return 0;
16425 if (ret)
16426 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16427 else
16428 add_loc_descr_to_each (list_ret,
16429 new_loc_descr (DW_OP_stack_value, 0, 0));
16430 have_address = 1;
16432 /* Show if we can't fill the request for an address. */
16433 if (want_address && !have_address)
16435 expansion_failed (loc, NULL_RTX,
16436 "Want address and only have value");
16437 return 0;
16440 gcc_assert (!ret || !list_ret);
16442 /* If we've got an address and don't want one, dereference. */
16443 if (!want_address && have_address)
16445 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16447 if (size > DWARF2_ADDR_SIZE || size == -1)
16449 expansion_failed (loc, NULL_RTX,
16450 "DWARF address size mismatch");
16451 return 0;
16453 else if (size == DWARF2_ADDR_SIZE)
16454 op = DW_OP_deref;
16455 else
16456 op = DW_OP_deref_size;
16458 if (ret)
16459 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16460 else
16461 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16463 if (ret)
16464 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16466 return list_ret;
16469 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16470 expressions. */
16472 static dw_loc_list_ref
16473 loc_list_from_tree (tree loc, int want_address,
16474 const struct loc_descr_context *context)
16476 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16478 for (dw_loc_list_ref loc_cur = result;
16479 loc_cur != NULL; loc_cur =
16480 loc_cur->dw_loc_next)
16481 loc_descr_without_nops (loc_cur->expr);
16482 return result;
16485 /* Same as above but return only single location expression. */
16486 static dw_loc_descr_ref
16487 loc_descriptor_from_tree (tree loc, int want_address,
16488 const struct loc_descr_context *context)
16490 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16491 if (!ret)
16492 return NULL;
16493 if (ret->dw_loc_next)
16495 expansion_failed (loc, NULL_RTX,
16496 "Location list where only loc descriptor needed");
16497 return NULL;
16499 return ret->expr;
16502 /* Given a value, round it up to the lowest multiple of `boundary'
16503 which is not less than the value itself. */
16505 static inline HOST_WIDE_INT
16506 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16508 return (((value + boundary - 1) / boundary) * boundary);
16511 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16512 pointer to the declared type for the relevant field variable, or return
16513 `integer_type_node' if the given node turns out to be an
16514 ERROR_MARK node. */
16516 static inline tree
16517 field_type (const_tree decl)
16519 tree type;
16521 if (TREE_CODE (decl) == ERROR_MARK)
16522 return integer_type_node;
16524 type = DECL_BIT_FIELD_TYPE (decl);
16525 if (type == NULL_TREE)
16526 type = TREE_TYPE (decl);
16528 return type;
16531 /* Given a pointer to a tree node, return the alignment in bits for
16532 it, or else return BITS_PER_WORD if the node actually turns out to
16533 be an ERROR_MARK node. */
16535 static inline unsigned
16536 simple_type_align_in_bits (const_tree type)
16538 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16541 static inline unsigned
16542 simple_decl_align_in_bits (const_tree decl)
16544 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16547 /* Return the result of rounding T up to ALIGN. */
16549 static inline offset_int
16550 round_up_to_align (const offset_int &t, unsigned int align)
16552 return wi::udiv_trunc (t + align - 1, align) * align;
16555 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16556 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16557 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16558 if we fail to return the size in one of these two forms. */
16560 static dw_loc_descr_ref
16561 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16563 tree tree_size;
16564 struct loc_descr_context ctx;
16566 /* Return a constant integer in priority, if possible. */
16567 *cst_size = int_size_in_bytes (type);
16568 if (*cst_size != -1)
16569 return NULL;
16571 ctx.context_type = const_cast<tree> (type);
16572 ctx.base_decl = NULL_TREE;
16573 ctx.dpi = NULL;
16575 type = TYPE_MAIN_VARIANT (type);
16576 tree_size = TYPE_SIZE_UNIT (type);
16577 return ((tree_size != NULL_TREE)
16578 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16579 : NULL);
16582 /* Helper structure for RECORD_TYPE processing. */
16583 struct vlr_context
16585 /* Root RECORD_TYPE. It is needed to generate data member location
16586 descriptions in variable-length records (VLR), but also to cope with
16587 variants, which are composed of nested structures multiplexed with
16588 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16589 function processing a FIELD_DECL, it is required to be non null. */
16590 tree struct_type;
16591 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16592 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16593 this variant part as part of the root record (in storage units). For
16594 regular records, it must be NULL_TREE. */
16595 tree variant_part_offset;
16598 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16599 addressed byte of the "containing object" for the given FIELD_DECL. If
16600 possible, return a native constant through CST_OFFSET (in which case NULL is
16601 returned); otherwise return a DWARF expression that computes the offset.
16603 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16604 that offset is, either because the argument turns out to be a pointer to an
16605 ERROR_MARK node, or because the offset expression is too complex for us.
16607 CTX is required: see the comment for VLR_CONTEXT. */
16609 static dw_loc_descr_ref
16610 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16611 HOST_WIDE_INT *cst_offset)
16613 offset_int object_offset_in_bits;
16614 offset_int object_offset_in_bytes;
16615 offset_int bitpos_int;
16616 bool is_byte_offset_cst, is_bit_offset_cst;
16617 tree tree_result;
16618 dw_loc_list_ref loc_result;
16620 *cst_offset = 0;
16622 if (TREE_CODE (decl) == ERROR_MARK)
16623 return NULL;
16624 else
16625 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16627 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16628 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16630 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16631 case. */
16632 if (is_bit_offset_cst)
16633 return NULL;
16635 #ifdef PCC_BITFIELD_TYPE_MATTERS
16636 /* We used to handle only constant offsets in all cases. Now, we handle
16637 properly dynamic byte offsets only when PCC bitfield type doesn't
16638 matter. */
16639 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16641 tree type;
16642 tree field_size_tree;
16643 offset_int deepest_bitpos;
16644 offset_int field_size_in_bits;
16645 unsigned int type_align_in_bits;
16646 unsigned int decl_align_in_bits;
16647 offset_int type_size_in_bits;
16649 bitpos_int = wi::to_offset (bit_position (decl));
16650 type = field_type (decl);
16651 type_size_in_bits = offset_int_type_size_in_bits (type);
16652 type_align_in_bits = simple_type_align_in_bits (type);
16654 field_size_tree = DECL_SIZE (decl);
16656 /* The size could be unspecified if there was an error, or for
16657 a flexible array member. */
16658 if (!field_size_tree)
16659 field_size_tree = bitsize_zero_node;
16661 /* If the size of the field is not constant, use the type size. */
16662 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16663 field_size_in_bits = wi::to_offset (field_size_tree);
16664 else
16665 field_size_in_bits = type_size_in_bits;
16667 decl_align_in_bits = simple_decl_align_in_bits (decl);
16669 /* The GCC front-end doesn't make any attempt to keep track of the
16670 starting bit offset (relative to the start of the containing
16671 structure type) of the hypothetical "containing object" for a
16672 bit-field. Thus, when computing the byte offset value for the
16673 start of the "containing object" of a bit-field, we must deduce
16674 this information on our own. This can be rather tricky to do in
16675 some cases. For example, handling the following structure type
16676 definition when compiling for an i386/i486 target (which only
16677 aligns long long's to 32-bit boundaries) can be very tricky:
16679 struct S { int field1; long long field2:31; };
16681 Fortunately, there is a simple rule-of-thumb which can be used
16682 in such cases. When compiling for an i386/i486, GCC will
16683 allocate 8 bytes for the structure shown above. It decides to
16684 do this based upon one simple rule for bit-field allocation.
16685 GCC allocates each "containing object" for each bit-field at
16686 the first (i.e. lowest addressed) legitimate alignment boundary
16687 (based upon the required minimum alignment for the declared
16688 type of the field) which it can possibly use, subject to the
16689 condition that there is still enough available space remaining
16690 in the containing object (when allocated at the selected point)
16691 to fully accommodate all of the bits of the bit-field itself.
16693 This simple rule makes it obvious why GCC allocates 8 bytes for
16694 each object of the structure type shown above. When looking
16695 for a place to allocate the "containing object" for `field2',
16696 the compiler simply tries to allocate a 64-bit "containing
16697 object" at each successive 32-bit boundary (starting at zero)
16698 until it finds a place to allocate that 64- bit field such that
16699 at least 31 contiguous (and previously unallocated) bits remain
16700 within that selected 64 bit field. (As it turns out, for the
16701 example above, the compiler finds it is OK to allocate the
16702 "containing object" 64-bit field at bit-offset zero within the
16703 structure type.)
16705 Here we attempt to work backwards from the limited set of facts
16706 we're given, and we try to deduce from those facts, where GCC
16707 must have believed that the containing object started (within
16708 the structure type). The value we deduce is then used (by the
16709 callers of this routine) to generate DW_AT_location and
16710 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16711 the case of DW_AT_location, regular fields as well). */
16713 /* Figure out the bit-distance from the start of the structure to
16714 the "deepest" bit of the bit-field. */
16715 deepest_bitpos = bitpos_int + field_size_in_bits;
16717 /* This is the tricky part. Use some fancy footwork to deduce
16718 where the lowest addressed bit of the containing object must
16719 be. */
16720 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16722 /* Round up to type_align by default. This works best for
16723 bitfields. */
16724 object_offset_in_bits
16725 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16727 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16729 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16731 /* Round up to decl_align instead. */
16732 object_offset_in_bits
16733 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16736 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16738 tree_result = byte_position (decl);
16739 if (ctx->variant_part_offset != NULL_TREE)
16740 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16741 ctx->variant_part_offset, tree_result));
16743 /* If the byte offset is a constant, it's simplier to handle a native
16744 constant rather than a DWARF expression. */
16745 if (TREE_CODE (tree_result) == INTEGER_CST)
16747 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16748 return NULL;
16750 struct loc_descr_context loc_ctx = {
16751 ctx->struct_type, /* context_type */
16752 NULL_TREE, /* base_decl */
16753 NULL /* dpi */
16755 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16757 /* We want a DWARF expression: abort if we only have a location list with
16758 multiple elements. */
16759 if (!loc_result || !single_element_loc_list_p (loc_result))
16760 return NULL;
16761 else
16762 return loc_result->expr;
16765 /* The following routines define various Dwarf attributes and any data
16766 associated with them. */
16768 /* Add a location description attribute value to a DIE.
16770 This emits location attributes suitable for whole variables and
16771 whole parameters. Note that the location attributes for struct fields are
16772 generated by the routine `data_member_location_attribute' below. */
16774 static inline void
16775 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16776 dw_loc_list_ref descr)
16778 if (descr == 0)
16779 return;
16780 if (single_element_loc_list_p (descr))
16781 add_AT_loc (die, attr_kind, descr->expr);
16782 else
16783 add_AT_loc_list (die, attr_kind, descr);
16786 /* Add DW_AT_accessibility attribute to DIE if needed. */
16788 static void
16789 add_accessibility_attribute (dw_die_ref die, tree decl)
16791 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16792 children, otherwise the default is DW_ACCESS_public. In DWARF2
16793 the default has always been DW_ACCESS_public. */
16794 if (TREE_PROTECTED (decl))
16795 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16796 else if (TREE_PRIVATE (decl))
16798 if (dwarf_version == 2
16799 || die->die_parent == NULL
16800 || die->die_parent->die_tag != DW_TAG_class_type)
16801 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16803 else if (dwarf_version > 2
16804 && die->die_parent
16805 && die->die_parent->die_tag == DW_TAG_class_type)
16806 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16809 /* Attach the specialized form of location attribute used for data members of
16810 struct and union types. In the special case of a FIELD_DECL node which
16811 represents a bit-field, the "offset" part of this special location
16812 descriptor must indicate the distance in bytes from the lowest-addressed
16813 byte of the containing struct or union type to the lowest-addressed byte of
16814 the "containing object" for the bit-field. (See the `field_byte_offset'
16815 function above).
16817 For any given bit-field, the "containing object" is a hypothetical object
16818 (of some integral or enum type) within which the given bit-field lives. The
16819 type of this hypothetical "containing object" is always the same as the
16820 declared type of the individual bit-field itself (for GCC anyway... the
16821 DWARF spec doesn't actually mandate this). Note that it is the size (in
16822 bytes) of the hypothetical "containing object" which will be given in the
16823 DW_AT_byte_size attribute for this bit-field. (See the
16824 `byte_size_attribute' function below.) It is also used when calculating the
16825 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16826 function below.)
16828 CTX is required: see the comment for VLR_CONTEXT. */
16830 static void
16831 add_data_member_location_attribute (dw_die_ref die,
16832 tree decl,
16833 struct vlr_context *ctx)
16835 HOST_WIDE_INT offset;
16836 dw_loc_descr_ref loc_descr = 0;
16838 if (TREE_CODE (decl) == TREE_BINFO)
16840 /* We're working on the TAG_inheritance for a base class. */
16841 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16843 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16844 aren't at a fixed offset from all (sub)objects of the same
16845 type. We need to extract the appropriate offset from our
16846 vtable. The following dwarf expression means
16848 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16850 This is specific to the V3 ABI, of course. */
16852 dw_loc_descr_ref tmp;
16854 /* Make a copy of the object address. */
16855 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16856 add_loc_descr (&loc_descr, tmp);
16858 /* Extract the vtable address. */
16859 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16860 add_loc_descr (&loc_descr, tmp);
16862 /* Calculate the address of the offset. */
16863 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
16864 gcc_assert (offset < 0);
16866 tmp = int_loc_descriptor (-offset);
16867 add_loc_descr (&loc_descr, tmp);
16868 tmp = new_loc_descr (DW_OP_minus, 0, 0);
16869 add_loc_descr (&loc_descr, tmp);
16871 /* Extract the offset. */
16872 tmp = new_loc_descr (DW_OP_deref, 0, 0);
16873 add_loc_descr (&loc_descr, tmp);
16875 /* Add it to the object address. */
16876 tmp = new_loc_descr (DW_OP_plus, 0, 0);
16877 add_loc_descr (&loc_descr, tmp);
16879 else
16880 offset = tree_to_shwi (BINFO_OFFSET (decl));
16882 else
16884 loc_descr = field_byte_offset (decl, ctx, &offset);
16886 /* If loc_descr is available then we know the field offset is dynamic.
16887 However, GDB does not handle dynamic field offsets very well at the
16888 moment. */
16889 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
16891 loc_descr = NULL;
16892 offset = 0;
16895 /* Data member location evalutation starts with the base address on the
16896 stack. Compute the field offset and add it to this base address. */
16897 else if (loc_descr != NULL)
16898 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
16901 if (! loc_descr)
16903 if (dwarf_version > 2)
16905 /* Don't need to output a location expression, just the constant. */
16906 if (offset < 0)
16907 add_AT_int (die, DW_AT_data_member_location, offset);
16908 else
16909 add_AT_unsigned (die, DW_AT_data_member_location, offset);
16910 return;
16912 else
16914 enum dwarf_location_atom op;
16916 /* The DWARF2 standard says that we should assume that the structure
16917 address is already on the stack, so we can specify a structure
16918 field address by using DW_OP_plus_uconst. */
16919 op = DW_OP_plus_uconst;
16920 loc_descr = new_loc_descr (op, offset, 0);
16924 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16927 /* Writes integer values to dw_vec_const array. */
16929 static void
16930 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16932 while (size != 0)
16934 *dest++ = val & 0xff;
16935 val >>= 8;
16936 --size;
16940 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
16942 static HOST_WIDE_INT
16943 extract_int (const unsigned char *src, unsigned int size)
16945 HOST_WIDE_INT val = 0;
16947 src += size;
16948 while (size != 0)
16950 val <<= 8;
16951 val |= *--src & 0xff;
16952 --size;
16954 return val;
16957 /* Writes wide_int values to dw_vec_const array. */
16959 static void
16960 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
16962 int i;
16964 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
16966 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
16967 return;
16970 /* We'd have to extend this code to support odd sizes. */
16971 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
16973 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
16975 if (WORDS_BIG_ENDIAN)
16976 for (i = n - 1; i >= 0; i--)
16978 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16979 dest += sizeof (HOST_WIDE_INT);
16981 else
16982 for (i = 0; i < n; i++)
16984 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
16985 dest += sizeof (HOST_WIDE_INT);
16989 /* Writes floating point values to dw_vec_const array. */
16991 static void
16992 insert_float (const_rtx rtl, unsigned char *array)
16994 long val[4];
16995 int i;
16997 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
16999 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17000 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17002 insert_int (val[i], 4, array);
17003 array += 4;
17007 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17008 does not have a "location" either in memory or in a register. These
17009 things can arise in GNU C when a constant is passed as an actual parameter
17010 to an inlined function. They can also arise in C++ where declared
17011 constants do not necessarily get memory "homes". */
17013 static bool
17014 add_const_value_attribute (dw_die_ref die, rtx rtl)
17016 switch (GET_CODE (rtl))
17018 case CONST_INT:
17020 HOST_WIDE_INT val = INTVAL (rtl);
17022 if (val < 0)
17023 add_AT_int (die, DW_AT_const_value, val);
17024 else
17025 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17027 return true;
17029 case CONST_WIDE_INT:
17031 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17032 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17033 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17034 wide_int w = wi::zext (w1, prec);
17035 add_AT_wide (die, DW_AT_const_value, w);
17037 return true;
17039 case CONST_DOUBLE:
17040 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17041 floating-point constant. A CONST_DOUBLE is used whenever the
17042 constant requires more than one word in order to be adequately
17043 represented. */
17045 machine_mode mode = GET_MODE (rtl);
17047 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17048 add_AT_double (die, DW_AT_const_value,
17049 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17050 else
17052 unsigned int length = GET_MODE_SIZE (mode);
17053 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17055 insert_float (rtl, array);
17056 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17059 return true;
17061 case CONST_VECTOR:
17063 machine_mode mode = GET_MODE (rtl);
17064 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17065 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17066 unsigned char *array
17067 = ggc_vec_alloc<unsigned char> (length * elt_size);
17068 unsigned int i;
17069 unsigned char *p;
17070 machine_mode imode = GET_MODE_INNER (mode);
17072 switch (GET_MODE_CLASS (mode))
17074 case MODE_VECTOR_INT:
17075 for (i = 0, p = array; i < length; i++, p += elt_size)
17077 rtx elt = CONST_VECTOR_ELT (rtl, i);
17078 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17080 break;
17082 case MODE_VECTOR_FLOAT:
17083 for (i = 0, p = array; i < length; i++, p += elt_size)
17085 rtx elt = CONST_VECTOR_ELT (rtl, i);
17086 insert_float (elt, p);
17088 break;
17090 default:
17091 gcc_unreachable ();
17094 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17096 return true;
17098 case CONST_STRING:
17099 if (dwarf_version >= 4 || !dwarf_strict)
17101 dw_loc_descr_ref loc_result;
17102 resolve_one_addr (&rtl);
17103 rtl_addr:
17104 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17105 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17106 add_AT_loc (die, DW_AT_location, loc_result);
17107 vec_safe_push (used_rtx_array, rtl);
17108 return true;
17110 return false;
17112 case CONST:
17113 if (CONSTANT_P (XEXP (rtl, 0)))
17114 return add_const_value_attribute (die, XEXP (rtl, 0));
17115 /* FALLTHROUGH */
17116 case SYMBOL_REF:
17117 if (!const_ok_for_output (rtl))
17118 return false;
17119 case LABEL_REF:
17120 if (dwarf_version >= 4 || !dwarf_strict)
17121 goto rtl_addr;
17122 return false;
17124 case PLUS:
17125 /* In cases where an inlined instance of an inline function is passed
17126 the address of an `auto' variable (which is local to the caller) we
17127 can get a situation where the DECL_RTL of the artificial local
17128 variable (for the inlining) which acts as a stand-in for the
17129 corresponding formal parameter (of the inline function) will look
17130 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17131 exactly a compile-time constant expression, but it isn't the address
17132 of the (artificial) local variable either. Rather, it represents the
17133 *value* which the artificial local variable always has during its
17134 lifetime. We currently have no way to represent such quasi-constant
17135 values in Dwarf, so for now we just punt and generate nothing. */
17136 return false;
17138 case HIGH:
17139 case CONST_FIXED:
17140 return false;
17142 case MEM:
17143 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17144 && MEM_READONLY_P (rtl)
17145 && GET_MODE (rtl) == BLKmode)
17147 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17148 return true;
17150 return false;
17152 default:
17153 /* No other kinds of rtx should be possible here. */
17154 gcc_unreachable ();
17156 return false;
17159 /* Determine whether the evaluation of EXPR references any variables
17160 or functions which aren't otherwise used (and therefore may not be
17161 output). */
17162 static tree
17163 reference_to_unused (tree * tp, int * walk_subtrees,
17164 void * data ATTRIBUTE_UNUSED)
17166 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17167 *walk_subtrees = 0;
17169 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17170 && ! TREE_ASM_WRITTEN (*tp))
17171 return *tp;
17172 /* ??? The C++ FE emits debug information for using decls, so
17173 putting gcc_unreachable here falls over. See PR31899. For now
17174 be conservative. */
17175 else if (!symtab->global_info_ready
17176 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17177 return *tp;
17178 else if (TREE_CODE (*tp) == VAR_DECL)
17180 varpool_node *node = varpool_node::get (*tp);
17181 if (!node || !node->definition)
17182 return *tp;
17184 else if (TREE_CODE (*tp) == FUNCTION_DECL
17185 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17187 /* The call graph machinery must have finished analyzing,
17188 optimizing and gimplifying the CU by now.
17189 So if *TP has no call graph node associated
17190 to it, it means *TP will not be emitted. */
17191 if (!cgraph_node::get (*tp))
17192 return *tp;
17194 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17195 return *tp;
17197 return NULL_TREE;
17200 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17201 for use in a later add_const_value_attribute call. */
17203 static rtx
17204 rtl_for_decl_init (tree init, tree type)
17206 rtx rtl = NULL_RTX;
17208 STRIP_NOPS (init);
17210 /* If a variable is initialized with a string constant without embedded
17211 zeros, build CONST_STRING. */
17212 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17214 tree enttype = TREE_TYPE (type);
17215 tree domain = TYPE_DOMAIN (type);
17216 machine_mode mode = TYPE_MODE (enttype);
17218 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17219 && domain
17220 && integer_zerop (TYPE_MIN_VALUE (domain))
17221 && compare_tree_int (TYPE_MAX_VALUE (domain),
17222 TREE_STRING_LENGTH (init) - 1) == 0
17223 && ((size_t) TREE_STRING_LENGTH (init)
17224 == strlen (TREE_STRING_POINTER (init)) + 1))
17226 rtl = gen_rtx_CONST_STRING (VOIDmode,
17227 ggc_strdup (TREE_STRING_POINTER (init)));
17228 rtl = gen_rtx_MEM (BLKmode, rtl);
17229 MEM_READONLY_P (rtl) = 1;
17232 /* Other aggregates, and complex values, could be represented using
17233 CONCAT: FIXME! */
17234 else if (AGGREGATE_TYPE_P (type)
17235 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17236 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17237 || TREE_CODE (type) == COMPLEX_TYPE)
17239 /* Vectors only work if their mode is supported by the target.
17240 FIXME: generic vectors ought to work too. */
17241 else if (TREE_CODE (type) == VECTOR_TYPE
17242 && !VECTOR_MODE_P (TYPE_MODE (type)))
17244 /* If the initializer is something that we know will expand into an
17245 immediate RTL constant, expand it now. We must be careful not to
17246 reference variables which won't be output. */
17247 else if (initializer_constant_valid_p (init, type)
17248 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17250 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17251 possible. */
17252 if (TREE_CODE (type) == VECTOR_TYPE)
17253 switch (TREE_CODE (init))
17255 case VECTOR_CST:
17256 break;
17257 case CONSTRUCTOR:
17258 if (TREE_CONSTANT (init))
17260 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17261 bool constant_p = true;
17262 tree value;
17263 unsigned HOST_WIDE_INT ix;
17265 /* Even when ctor is constant, it might contain non-*_CST
17266 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17267 belong into VECTOR_CST nodes. */
17268 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17269 if (!CONSTANT_CLASS_P (value))
17271 constant_p = false;
17272 break;
17275 if (constant_p)
17277 init = build_vector_from_ctor (type, elts);
17278 break;
17281 /* FALLTHRU */
17283 default:
17284 return NULL;
17287 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17289 /* If expand_expr returns a MEM, it wasn't immediate. */
17290 gcc_assert (!rtl || !MEM_P (rtl));
17293 return rtl;
17296 /* Generate RTL for the variable DECL to represent its location. */
17298 static rtx
17299 rtl_for_decl_location (tree decl)
17301 rtx rtl;
17303 /* Here we have to decide where we are going to say the parameter "lives"
17304 (as far as the debugger is concerned). We only have a couple of
17305 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17307 DECL_RTL normally indicates where the parameter lives during most of the
17308 activation of the function. If optimization is enabled however, this
17309 could be either NULL or else a pseudo-reg. Both of those cases indicate
17310 that the parameter doesn't really live anywhere (as far as the code
17311 generation parts of GCC are concerned) during most of the function's
17312 activation. That will happen (for example) if the parameter is never
17313 referenced within the function.
17315 We could just generate a location descriptor here for all non-NULL
17316 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17317 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17318 where DECL_RTL is NULL or is a pseudo-reg.
17320 Note however that we can only get away with using DECL_INCOMING_RTL as
17321 a backup substitute for DECL_RTL in certain limited cases. In cases
17322 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17323 we can be sure that the parameter was passed using the same type as it is
17324 declared to have within the function, and that its DECL_INCOMING_RTL
17325 points us to a place where a value of that type is passed.
17327 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17328 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17329 because in these cases DECL_INCOMING_RTL points us to a value of some
17330 type which is *different* from the type of the parameter itself. Thus,
17331 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17332 such cases, the debugger would end up (for example) trying to fetch a
17333 `float' from a place which actually contains the first part of a
17334 `double'. That would lead to really incorrect and confusing
17335 output at debug-time.
17337 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17338 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17339 are a couple of exceptions however. On little-endian machines we can
17340 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17341 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17342 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17343 when (on a little-endian machine) a non-prototyped function has a
17344 parameter declared to be of type `short' or `char'. In such cases,
17345 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17346 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17347 passed `int' value. If the debugger then uses that address to fetch
17348 a `short' or a `char' (on a little-endian machine) the result will be
17349 the correct data, so we allow for such exceptional cases below.
17351 Note that our goal here is to describe the place where the given formal
17352 parameter lives during most of the function's activation (i.e. between the
17353 end of the prologue and the start of the epilogue). We'll do that as best
17354 as we can. Note however that if the given formal parameter is modified
17355 sometime during the execution of the function, then a stack backtrace (at
17356 debug-time) will show the function as having been called with the *new*
17357 value rather than the value which was originally passed in. This happens
17358 rarely enough that it is not a major problem, but it *is* a problem, and
17359 I'd like to fix it.
17361 A future version of dwarf2out.c may generate two additional attributes for
17362 any given DW_TAG_formal_parameter DIE which will describe the "passed
17363 type" and the "passed location" for the given formal parameter in addition
17364 to the attributes we now generate to indicate the "declared type" and the
17365 "active location" for each parameter. This additional set of attributes
17366 could be used by debuggers for stack backtraces. Separately, note that
17367 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17368 This happens (for example) for inlined-instances of inline function formal
17369 parameters which are never referenced. This really shouldn't be
17370 happening. All PARM_DECL nodes should get valid non-NULL
17371 DECL_INCOMING_RTL values. FIXME. */
17373 /* Use DECL_RTL as the "location" unless we find something better. */
17374 rtl = DECL_RTL_IF_SET (decl);
17376 /* When generating abstract instances, ignore everything except
17377 constants, symbols living in memory, and symbols living in
17378 fixed registers. */
17379 if (! reload_completed)
17381 if (rtl
17382 && (CONSTANT_P (rtl)
17383 || (MEM_P (rtl)
17384 && CONSTANT_P (XEXP (rtl, 0)))
17385 || (REG_P (rtl)
17386 && TREE_CODE (decl) == VAR_DECL
17387 && TREE_STATIC (decl))))
17389 rtl = targetm.delegitimize_address (rtl);
17390 return rtl;
17392 rtl = NULL_RTX;
17394 else if (TREE_CODE (decl) == PARM_DECL)
17396 if (rtl == NULL_RTX
17397 || is_pseudo_reg (rtl)
17398 || (MEM_P (rtl)
17399 && is_pseudo_reg (XEXP (rtl, 0))
17400 && DECL_INCOMING_RTL (decl)
17401 && MEM_P (DECL_INCOMING_RTL (decl))
17402 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17404 tree declared_type = TREE_TYPE (decl);
17405 tree passed_type = DECL_ARG_TYPE (decl);
17406 machine_mode dmode = TYPE_MODE (declared_type);
17407 machine_mode pmode = TYPE_MODE (passed_type);
17409 /* This decl represents a formal parameter which was optimized out.
17410 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17411 all cases where (rtl == NULL_RTX) just below. */
17412 if (dmode == pmode)
17413 rtl = DECL_INCOMING_RTL (decl);
17414 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17415 && SCALAR_INT_MODE_P (dmode)
17416 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17417 && DECL_INCOMING_RTL (decl))
17419 rtx inc = DECL_INCOMING_RTL (decl);
17420 if (REG_P (inc))
17421 rtl = inc;
17422 else if (MEM_P (inc))
17424 if (BYTES_BIG_ENDIAN)
17425 rtl = adjust_address_nv (inc, dmode,
17426 GET_MODE_SIZE (pmode)
17427 - GET_MODE_SIZE (dmode));
17428 else
17429 rtl = inc;
17434 /* If the parm was passed in registers, but lives on the stack, then
17435 make a big endian correction if the mode of the type of the
17436 parameter is not the same as the mode of the rtl. */
17437 /* ??? This is the same series of checks that are made in dbxout.c before
17438 we reach the big endian correction code there. It isn't clear if all
17439 of these checks are necessary here, but keeping them all is the safe
17440 thing to do. */
17441 else if (MEM_P (rtl)
17442 && XEXP (rtl, 0) != const0_rtx
17443 && ! CONSTANT_P (XEXP (rtl, 0))
17444 /* Not passed in memory. */
17445 && !MEM_P (DECL_INCOMING_RTL (decl))
17446 /* Not passed by invisible reference. */
17447 && (!REG_P (XEXP (rtl, 0))
17448 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17449 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17450 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17451 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17452 #endif
17454 /* Big endian correction check. */
17455 && BYTES_BIG_ENDIAN
17456 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17457 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17458 < UNITS_PER_WORD))
17460 machine_mode addr_mode = get_address_mode (rtl);
17461 int offset = (UNITS_PER_WORD
17462 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17464 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17465 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17468 else if (TREE_CODE (decl) == VAR_DECL
17469 && rtl
17470 && MEM_P (rtl)
17471 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17472 && BYTES_BIG_ENDIAN)
17474 machine_mode addr_mode = get_address_mode (rtl);
17475 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17476 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17478 /* If a variable is declared "register" yet is smaller than
17479 a register, then if we store the variable to memory, it
17480 looks like we're storing a register-sized value, when in
17481 fact we are not. We need to adjust the offset of the
17482 storage location to reflect the actual value's bytes,
17483 else gdb will not be able to display it. */
17484 if (rsize > dsize)
17485 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17486 plus_constant (addr_mode, XEXP (rtl, 0),
17487 rsize - dsize));
17490 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17491 and will have been substituted directly into all expressions that use it.
17492 C does not have such a concept, but C++ and other languages do. */
17493 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17494 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17496 if (rtl)
17497 rtl = targetm.delegitimize_address (rtl);
17499 /* If we don't look past the constant pool, we risk emitting a
17500 reference to a constant pool entry that isn't referenced from
17501 code, and thus is not emitted. */
17502 if (rtl)
17503 rtl = avoid_constant_pool_reference (rtl);
17505 /* Try harder to get a rtl. If this symbol ends up not being emitted
17506 in the current CU, resolve_addr will remove the expression referencing
17507 it. */
17508 if (rtl == NULL_RTX
17509 && TREE_CODE (decl) == VAR_DECL
17510 && !DECL_EXTERNAL (decl)
17511 && TREE_STATIC (decl)
17512 && DECL_NAME (decl)
17513 && !DECL_HARD_REGISTER (decl)
17514 && DECL_MODE (decl) != VOIDmode)
17516 rtl = make_decl_rtl_for_debug (decl);
17517 if (!MEM_P (rtl)
17518 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17519 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17520 rtl = NULL_RTX;
17523 return rtl;
17526 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17527 returned. If so, the decl for the COMMON block is returned, and the
17528 value is the offset into the common block for the symbol. */
17530 static tree
17531 fortran_common (tree decl, HOST_WIDE_INT *value)
17533 tree val_expr, cvar;
17534 machine_mode mode;
17535 HOST_WIDE_INT bitsize, bitpos;
17536 tree offset;
17537 int unsignedp, reversep, volatilep = 0;
17539 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17540 it does not have a value (the offset into the common area), or if it
17541 is thread local (as opposed to global) then it isn't common, and shouldn't
17542 be handled as such. */
17543 if (TREE_CODE (decl) != VAR_DECL
17544 || !TREE_STATIC (decl)
17545 || !DECL_HAS_VALUE_EXPR_P (decl)
17546 || !is_fortran ())
17547 return NULL_TREE;
17549 val_expr = DECL_VALUE_EXPR (decl);
17550 if (TREE_CODE (val_expr) != COMPONENT_REF)
17551 return NULL_TREE;
17553 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17554 &unsignedp, &reversep, &volatilep, true);
17556 if (cvar == NULL_TREE
17557 || TREE_CODE (cvar) != VAR_DECL
17558 || DECL_ARTIFICIAL (cvar)
17559 || !TREE_PUBLIC (cvar))
17560 return NULL_TREE;
17562 *value = 0;
17563 if (offset != NULL)
17565 if (!tree_fits_shwi_p (offset))
17566 return NULL_TREE;
17567 *value = tree_to_shwi (offset);
17569 if (bitpos != 0)
17570 *value += bitpos / BITS_PER_UNIT;
17572 return cvar;
17575 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17576 data attribute for a variable or a parameter. We generate the
17577 DW_AT_const_value attribute only in those cases where the given variable
17578 or parameter does not have a true "location" either in memory or in a
17579 register. This can happen (for example) when a constant is passed as an
17580 actual argument in a call to an inline function. (It's possible that
17581 these things can crop up in other ways also.) Note that one type of
17582 constant value which can be passed into an inlined function is a constant
17583 pointer. This can happen for example if an actual argument in an inlined
17584 function call evaluates to a compile-time constant address.
17586 CACHE_P is true if it is worth caching the location list for DECL,
17587 so that future calls can reuse it rather than regenerate it from scratch.
17588 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17589 since we will need to refer to them each time the function is inlined. */
17591 static bool
17592 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17594 rtx rtl;
17595 dw_loc_list_ref list;
17596 var_loc_list *loc_list;
17597 cached_dw_loc_list *cache;
17599 if (early_dwarf)
17600 return false;
17602 if (TREE_CODE (decl) == ERROR_MARK)
17603 return false;
17605 if (get_AT (die, DW_AT_location)
17606 || get_AT (die, DW_AT_const_value))
17607 return true;
17609 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17610 || TREE_CODE (decl) == RESULT_DECL);
17612 /* Try to get some constant RTL for this decl, and use that as the value of
17613 the location. */
17615 rtl = rtl_for_decl_location (decl);
17616 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17617 && add_const_value_attribute (die, rtl))
17618 return true;
17620 /* See if we have single element location list that is equivalent to
17621 a constant value. That way we are better to use add_const_value_attribute
17622 rather than expanding constant value equivalent. */
17623 loc_list = lookup_decl_loc (decl);
17624 if (loc_list
17625 && loc_list->first
17626 && loc_list->first->next == NULL
17627 && NOTE_P (loc_list->first->loc)
17628 && NOTE_VAR_LOCATION (loc_list->first->loc)
17629 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17631 struct var_loc_node *node;
17633 node = loc_list->first;
17634 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17635 if (GET_CODE (rtl) == EXPR_LIST)
17636 rtl = XEXP (rtl, 0);
17637 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17638 && add_const_value_attribute (die, rtl))
17639 return true;
17641 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17642 list several times. See if we've already cached the contents. */
17643 list = NULL;
17644 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17645 cache_p = false;
17646 if (cache_p)
17648 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17649 if (cache)
17650 list = cache->loc_list;
17652 if (list == NULL)
17654 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17655 NULL);
17656 /* It is usually worth caching this result if the decl is from
17657 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17658 if (cache_p && list && list->dw_loc_next)
17660 cached_dw_loc_list **slot
17661 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17662 DECL_UID (decl),
17663 INSERT);
17664 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17665 cache->decl_id = DECL_UID (decl);
17666 cache->loc_list = list;
17667 *slot = cache;
17670 if (list)
17672 add_AT_location_description (die, DW_AT_location, list);
17673 return true;
17675 /* None of that worked, so it must not really have a location;
17676 try adding a constant value attribute from the DECL_INITIAL. */
17677 return tree_add_const_value_attribute_for_decl (die, decl);
17680 /* Helper function for tree_add_const_value_attribute. Natively encode
17681 initializer INIT into an array. Return true if successful. */
17683 static bool
17684 native_encode_initializer (tree init, unsigned char *array, int size)
17686 tree type;
17688 if (init == NULL_TREE)
17689 return false;
17691 STRIP_NOPS (init);
17692 switch (TREE_CODE (init))
17694 case STRING_CST:
17695 type = TREE_TYPE (init);
17696 if (TREE_CODE (type) == ARRAY_TYPE)
17698 tree enttype = TREE_TYPE (type);
17699 machine_mode mode = TYPE_MODE (enttype);
17701 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17702 return false;
17703 if (int_size_in_bytes (type) != size)
17704 return false;
17705 if (size > TREE_STRING_LENGTH (init))
17707 memcpy (array, TREE_STRING_POINTER (init),
17708 TREE_STRING_LENGTH (init));
17709 memset (array + TREE_STRING_LENGTH (init),
17710 '\0', size - TREE_STRING_LENGTH (init));
17712 else
17713 memcpy (array, TREE_STRING_POINTER (init), size);
17714 return true;
17716 return false;
17717 case CONSTRUCTOR:
17718 type = TREE_TYPE (init);
17719 if (int_size_in_bytes (type) != size)
17720 return false;
17721 if (TREE_CODE (type) == ARRAY_TYPE)
17723 HOST_WIDE_INT min_index;
17724 unsigned HOST_WIDE_INT cnt;
17725 int curpos = 0, fieldsize;
17726 constructor_elt *ce;
17728 if (TYPE_DOMAIN (type) == NULL_TREE
17729 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17730 return false;
17732 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17733 if (fieldsize <= 0)
17734 return false;
17736 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17737 memset (array, '\0', size);
17738 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17740 tree val = ce->value;
17741 tree index = ce->index;
17742 int pos = curpos;
17743 if (index && TREE_CODE (index) == RANGE_EXPR)
17744 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17745 * fieldsize;
17746 else if (index)
17747 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17749 if (val)
17751 STRIP_NOPS (val);
17752 if (!native_encode_initializer (val, array + pos, fieldsize))
17753 return false;
17755 curpos = pos + fieldsize;
17756 if (index && TREE_CODE (index) == RANGE_EXPR)
17758 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17759 - tree_to_shwi (TREE_OPERAND (index, 0));
17760 while (count-- > 0)
17762 if (val)
17763 memcpy (array + curpos, array + pos, fieldsize);
17764 curpos += fieldsize;
17767 gcc_assert (curpos <= size);
17769 return true;
17771 else if (TREE_CODE (type) == RECORD_TYPE
17772 || TREE_CODE (type) == UNION_TYPE)
17774 tree field = NULL_TREE;
17775 unsigned HOST_WIDE_INT cnt;
17776 constructor_elt *ce;
17778 if (int_size_in_bytes (type) != size)
17779 return false;
17781 if (TREE_CODE (type) == RECORD_TYPE)
17782 field = TYPE_FIELDS (type);
17784 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17786 tree val = ce->value;
17787 int pos, fieldsize;
17789 if (ce->index != 0)
17790 field = ce->index;
17792 if (val)
17793 STRIP_NOPS (val);
17795 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17796 return false;
17798 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17799 && TYPE_DOMAIN (TREE_TYPE (field))
17800 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17801 return false;
17802 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17803 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17804 return false;
17805 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17806 pos = int_byte_position (field);
17807 gcc_assert (pos + fieldsize <= size);
17808 if (val
17809 && !native_encode_initializer (val, array + pos, fieldsize))
17810 return false;
17812 return true;
17814 return false;
17815 case VIEW_CONVERT_EXPR:
17816 case NON_LVALUE_EXPR:
17817 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17818 default:
17819 return native_encode_expr (init, array, size) == size;
17823 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17824 attribute is the const value T. */
17826 static bool
17827 tree_add_const_value_attribute (dw_die_ref die, tree t)
17829 tree init;
17830 tree type = TREE_TYPE (t);
17831 rtx rtl;
17833 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17834 return false;
17836 init = t;
17837 gcc_assert (!DECL_P (init));
17839 rtl = rtl_for_decl_init (init, type);
17840 if (rtl)
17841 return add_const_value_attribute (die, rtl);
17842 /* If the host and target are sane, try harder. */
17843 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17844 && initializer_constant_valid_p (init, type))
17846 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17847 if (size > 0 && (int) size == size)
17849 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17851 if (native_encode_initializer (init, array, size))
17853 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17854 return true;
17856 ggc_free (array);
17859 return false;
17862 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17863 attribute is the const value of T, where T is an integral constant
17864 variable with static storage duration
17865 (so it can't be a PARM_DECL or a RESULT_DECL). */
17867 static bool
17868 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17871 if (!decl
17872 || (TREE_CODE (decl) != VAR_DECL
17873 && TREE_CODE (decl) != CONST_DECL)
17874 || (TREE_CODE (decl) == VAR_DECL
17875 && !TREE_STATIC (decl)))
17876 return false;
17878 if (TREE_READONLY (decl)
17879 && ! TREE_THIS_VOLATILE (decl)
17880 && DECL_INITIAL (decl))
17881 /* OK */;
17882 else
17883 return false;
17885 /* Don't add DW_AT_const_value if abstract origin already has one. */
17886 if (get_AT (var_die, DW_AT_const_value))
17887 return false;
17889 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17892 /* Convert the CFI instructions for the current function into a
17893 location list. This is used for DW_AT_frame_base when we targeting
17894 a dwarf2 consumer that does not support the dwarf3
17895 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
17896 expressions. */
17898 static dw_loc_list_ref
17899 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17901 int ix;
17902 dw_fde_ref fde;
17903 dw_loc_list_ref list, *list_tail;
17904 dw_cfi_ref cfi;
17905 dw_cfa_location last_cfa, next_cfa;
17906 const char *start_label, *last_label, *section;
17907 dw_cfa_location remember;
17909 fde = cfun->fde;
17910 gcc_assert (fde != NULL);
17912 section = secname_for_decl (current_function_decl);
17913 list_tail = &list;
17914 list = NULL;
17916 memset (&next_cfa, 0, sizeof (next_cfa));
17917 next_cfa.reg = INVALID_REGNUM;
17918 remember = next_cfa;
17920 start_label = fde->dw_fde_begin;
17922 /* ??? Bald assumption that the CIE opcode list does not contain
17923 advance opcodes. */
17924 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
17925 lookup_cfa_1 (cfi, &next_cfa, &remember);
17927 last_cfa = next_cfa;
17928 last_label = start_label;
17930 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17932 /* If the first partition contained no CFI adjustments, the
17933 CIE opcodes apply to the whole first partition. */
17934 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17935 fde->dw_fde_begin, fde->dw_fde_end, section);
17936 list_tail =&(*list_tail)->dw_loc_next;
17937 start_label = last_label = fde->dw_fde_second_begin;
17940 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
17942 switch (cfi->dw_cfi_opc)
17944 case DW_CFA_set_loc:
17945 case DW_CFA_advance_loc1:
17946 case DW_CFA_advance_loc2:
17947 case DW_CFA_advance_loc4:
17948 if (!cfa_equal_p (&last_cfa, &next_cfa))
17950 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17951 start_label, last_label, section);
17953 list_tail = &(*list_tail)->dw_loc_next;
17954 last_cfa = next_cfa;
17955 start_label = last_label;
17957 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17958 break;
17960 case DW_CFA_advance_loc:
17961 /* The encoding is complex enough that we should never emit this. */
17962 gcc_unreachable ();
17964 default:
17965 lookup_cfa_1 (cfi, &next_cfa, &remember);
17966 break;
17968 if (ix + 1 == fde->dw_fde_switch_cfi_index)
17970 if (!cfa_equal_p (&last_cfa, &next_cfa))
17972 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17973 start_label, last_label, section);
17975 list_tail = &(*list_tail)->dw_loc_next;
17976 last_cfa = next_cfa;
17977 start_label = last_label;
17979 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17980 start_label, fde->dw_fde_end, section);
17981 list_tail = &(*list_tail)->dw_loc_next;
17982 start_label = last_label = fde->dw_fde_second_begin;
17986 if (!cfa_equal_p (&last_cfa, &next_cfa))
17988 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17989 start_label, last_label, section);
17990 list_tail = &(*list_tail)->dw_loc_next;
17991 start_label = last_label;
17994 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17995 start_label,
17996 fde->dw_fde_second_begin
17997 ? fde->dw_fde_second_end : fde->dw_fde_end,
17998 section);
18000 if (list && list->dw_loc_next)
18001 gen_llsym (list);
18003 return list;
18006 /* Compute a displacement from the "steady-state frame pointer" to the
18007 frame base (often the same as the CFA), and store it in
18008 frame_pointer_fb_offset. OFFSET is added to the displacement
18009 before the latter is negated. */
18011 static void
18012 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18014 rtx reg, elim;
18016 #ifdef FRAME_POINTER_CFA_OFFSET
18017 reg = frame_pointer_rtx;
18018 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18019 #else
18020 reg = arg_pointer_rtx;
18021 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18022 #endif
18024 elim = (ira_use_lra_p
18025 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18026 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18027 if (GET_CODE (elim) == PLUS)
18029 offset += INTVAL (XEXP (elim, 1));
18030 elim = XEXP (elim, 0);
18033 frame_pointer_fb_offset = -offset;
18035 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18036 in which to eliminate. This is because it's stack pointer isn't
18037 directly accessible as a register within the ISA. To work around
18038 this, assume that while we cannot provide a proper value for
18039 frame_pointer_fb_offset, we won't need one either. */
18040 frame_pointer_fb_offset_valid
18041 = ((SUPPORTS_STACK_ALIGNMENT
18042 && (elim == hard_frame_pointer_rtx
18043 || elim == stack_pointer_rtx))
18044 || elim == (frame_pointer_needed
18045 ? hard_frame_pointer_rtx
18046 : stack_pointer_rtx));
18049 /* Generate a DW_AT_name attribute given some string value to be included as
18050 the value of the attribute. */
18052 static void
18053 add_name_attribute (dw_die_ref die, const char *name_string)
18055 if (name_string != NULL && *name_string != 0)
18057 if (demangle_name_func)
18058 name_string = (*demangle_name_func) (name_string);
18060 add_AT_string (die, DW_AT_name, name_string);
18064 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18065 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18066 of TYPE accordingly.
18068 ??? This is a temporary measure until after we're able to generate
18069 regular DWARF for the complex Ada type system. */
18071 static void
18072 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18073 dw_die_ref context_die)
18075 tree dtype;
18076 dw_die_ref dtype_die;
18078 if (!lang_hooks.types.descriptive_type)
18079 return;
18081 dtype = lang_hooks.types.descriptive_type (type);
18082 if (!dtype)
18083 return;
18085 dtype_die = lookup_type_die (dtype);
18086 if (!dtype_die)
18088 gen_type_die (dtype, context_die);
18089 dtype_die = lookup_type_die (dtype);
18090 gcc_assert (dtype_die);
18093 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18096 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18098 static const char *
18099 comp_dir_string (void)
18101 const char *wd;
18102 char *wd1;
18103 static const char *cached_wd = NULL;
18105 if (cached_wd != NULL)
18106 return cached_wd;
18108 wd = get_src_pwd ();
18109 if (wd == NULL)
18110 return NULL;
18112 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18114 int wdlen;
18116 wdlen = strlen (wd);
18117 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18118 strcpy (wd1, wd);
18119 wd1 [wdlen] = DIR_SEPARATOR;
18120 wd1 [wdlen + 1] = 0;
18121 wd = wd1;
18124 cached_wd = remap_debug_filename (wd);
18125 return cached_wd;
18128 /* Generate a DW_AT_comp_dir attribute for DIE. */
18130 static void
18131 add_comp_dir_attribute (dw_die_ref die)
18133 const char * wd = comp_dir_string ();
18134 if (wd != NULL)
18135 add_AT_string (die, DW_AT_comp_dir, wd);
18138 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18139 pointer computation, ...), output a representation for that bound according
18140 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18141 loc_list_from_tree for the meaning of CONTEXT. */
18143 static void
18144 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18145 int forms, const struct loc_descr_context *context)
18147 dw_die_ref context_die, decl_die;
18148 dw_loc_list_ref list;
18150 bool strip_conversions = true;
18152 while (strip_conversions)
18153 switch (TREE_CODE (value))
18155 case ERROR_MARK:
18156 case SAVE_EXPR:
18157 return;
18159 CASE_CONVERT:
18160 case VIEW_CONVERT_EXPR:
18161 value = TREE_OPERAND (value, 0);
18162 break;
18164 default:
18165 strip_conversions = false;
18166 break;
18169 /* If possible and permitted, output the attribute as a constant. */
18170 if ((forms & dw_scalar_form_constant) != 0
18171 && TREE_CODE (value) == INTEGER_CST)
18173 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18175 /* If HOST_WIDE_INT is big enough then represent the bound as
18176 a constant value. We need to choose a form based on
18177 whether the type is signed or unsigned. We cannot just
18178 call add_AT_unsigned if the value itself is positive
18179 (add_AT_unsigned might add the unsigned value encoded as
18180 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18181 bounds type and then sign extend any unsigned values found
18182 for signed types. This is needed only for
18183 DW_AT_{lower,upper}_bound, since for most other attributes,
18184 consumers will treat DW_FORM_data[1248] as unsigned values,
18185 regardless of the underlying type. */
18186 if (prec <= HOST_BITS_PER_WIDE_INT
18187 || tree_fits_uhwi_p (value))
18189 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18190 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18191 else
18192 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18194 else
18195 /* Otherwise represent the bound as an unsigned value with
18196 the precision of its type. The precision and signedness
18197 of the type will be necessary to re-interpret it
18198 unambiguously. */
18199 add_AT_wide (die, attr, value);
18200 return;
18203 /* Otherwise, if it's possible and permitted too, output a reference to
18204 another DIE. */
18205 if ((forms & dw_scalar_form_reference) != 0)
18207 tree decl = NULL_TREE;
18209 /* Some type attributes reference an outer type. For instance, the upper
18210 bound of an array may reference an embedding record (this happens in
18211 Ada). */
18212 if (TREE_CODE (value) == COMPONENT_REF
18213 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18214 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18215 decl = TREE_OPERAND (value, 1);
18217 else if (TREE_CODE (value) == VAR_DECL
18218 || TREE_CODE (value) == PARM_DECL
18219 || TREE_CODE (value) == RESULT_DECL)
18220 decl = value;
18222 if (decl != NULL_TREE)
18224 dw_die_ref decl_die = lookup_decl_die (decl);
18226 /* ??? Can this happen, or should the variable have been bound
18227 first? Probably it can, since I imagine that we try to create
18228 the types of parameters in the order in which they exist in
18229 the list, and won't have created a forward reference to a
18230 later parameter. */
18231 if (decl_die != NULL)
18233 add_AT_die_ref (die, attr, decl_die);
18234 return;
18239 /* Last chance: try to create a stack operation procedure to evaluate the
18240 value. Do nothing if even that is not possible or permitted. */
18241 if ((forms & dw_scalar_form_exprloc) == 0)
18242 return;
18244 list = loc_list_from_tree (value, 2, context);
18245 if (list == NULL || single_element_loc_list_p (list))
18247 /* If this attribute is not a reference nor constant, it is
18248 a DWARF expression rather than location description. For that
18249 loc_list_from_tree (value, 0, &context) is needed. */
18250 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18251 if (list2 && single_element_loc_list_p (list2))
18253 add_AT_loc (die, attr, list2->expr);
18254 return;
18258 /* If that failed to give a single element location list, fall back to
18259 outputting this as a reference... still if permitted. */
18260 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18261 return;
18263 if (current_function_decl == 0)
18264 context_die = comp_unit_die ();
18265 else
18266 context_die = lookup_decl_die (current_function_decl);
18268 decl_die = new_die (DW_TAG_variable, context_die, value);
18269 add_AT_flag (decl_die, DW_AT_artificial, 1);
18270 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18271 context_die);
18272 add_AT_location_description (decl_die, DW_AT_location, list);
18273 add_AT_die_ref (die, attr, decl_die);
18276 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18277 default. */
18279 static int
18280 lower_bound_default (void)
18282 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18284 case DW_LANG_C:
18285 case DW_LANG_C89:
18286 case DW_LANG_C99:
18287 case DW_LANG_C11:
18288 case DW_LANG_C_plus_plus:
18289 case DW_LANG_C_plus_plus_11:
18290 case DW_LANG_C_plus_plus_14:
18291 case DW_LANG_ObjC:
18292 case DW_LANG_ObjC_plus_plus:
18293 case DW_LANG_Java:
18294 return 0;
18295 case DW_LANG_Fortran77:
18296 case DW_LANG_Fortran90:
18297 case DW_LANG_Fortran95:
18298 case DW_LANG_Fortran03:
18299 case DW_LANG_Fortran08:
18300 return 1;
18301 case DW_LANG_UPC:
18302 case DW_LANG_D:
18303 case DW_LANG_Python:
18304 return dwarf_version >= 4 ? 0 : -1;
18305 case DW_LANG_Ada95:
18306 case DW_LANG_Ada83:
18307 case DW_LANG_Cobol74:
18308 case DW_LANG_Cobol85:
18309 case DW_LANG_Pascal83:
18310 case DW_LANG_Modula2:
18311 case DW_LANG_PLI:
18312 return dwarf_version >= 4 ? 1 : -1;
18313 default:
18314 return -1;
18318 /* Given a tree node describing an array bound (either lower or upper) output
18319 a representation for that bound. */
18321 static void
18322 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18323 tree bound, const struct loc_descr_context *context)
18325 int dflt;
18327 while (1)
18328 switch (TREE_CODE (bound))
18330 /* Strip all conversions. */
18331 CASE_CONVERT:
18332 case VIEW_CONVERT_EXPR:
18333 bound = TREE_OPERAND (bound, 0);
18334 break;
18336 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18337 are even omitted when they are the default. */
18338 case INTEGER_CST:
18339 /* If the value for this bound is the default one, we can even omit the
18340 attribute. */
18341 if (bound_attr == DW_AT_lower_bound
18342 && tree_fits_shwi_p (bound)
18343 && (dflt = lower_bound_default ()) != -1
18344 && tree_to_shwi (bound) == dflt)
18345 return;
18347 /* FALLTHRU */
18349 default:
18350 /* Because of the complex interaction there can be with other GNAT
18351 encodings, GDB isn't ready yet to handle proper DWARF description
18352 for self-referencial subrange bounds: let GNAT encodings do the
18353 magic in such a case. */
18354 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18355 && contains_placeholder_p (bound))
18356 return;
18358 add_scalar_info (subrange_die, bound_attr, bound,
18359 dw_scalar_form_constant
18360 | dw_scalar_form_exprloc
18361 | dw_scalar_form_reference,
18362 context);
18363 return;
18367 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18368 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18369 Note that the block of subscript information for an array type also
18370 includes information about the element type of the given array type.
18372 This function reuses previously set type and bound information if
18373 available. */
18375 static void
18376 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18378 unsigned dimension_number;
18379 tree lower, upper;
18380 dw_die_ref child = type_die->die_child;
18382 for (dimension_number = 0;
18383 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18384 type = TREE_TYPE (type), dimension_number++)
18386 tree domain = TYPE_DOMAIN (type);
18388 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18389 break;
18391 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18392 and (in GNU C only) variable bounds. Handle all three forms
18393 here. */
18395 /* Find and reuse a previously generated DW_TAG_subrange_type if
18396 available.
18398 For multi-dimensional arrays, as we iterate through the
18399 various dimensions in the enclosing for loop above, we also
18400 iterate through the DIE children and pick at each
18401 DW_TAG_subrange_type previously generated (if available).
18402 Each child DW_TAG_subrange_type DIE describes the range of
18403 the current dimension. At this point we should have as many
18404 DW_TAG_subrange_type's as we have dimensions in the
18405 array. */
18406 dw_die_ref subrange_die = NULL;
18407 if (child)
18408 while (1)
18410 child = child->die_sib;
18411 if (child->die_tag == DW_TAG_subrange_type)
18412 subrange_die = child;
18413 if (child == type_die->die_child)
18415 /* If we wrapped around, stop looking next time. */
18416 child = NULL;
18417 break;
18419 if (child->die_tag == DW_TAG_subrange_type)
18420 break;
18422 if (!subrange_die)
18423 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18425 if (domain)
18427 /* We have an array type with specified bounds. */
18428 lower = TYPE_MIN_VALUE (domain);
18429 upper = TYPE_MAX_VALUE (domain);
18431 /* Define the index type. */
18432 if (TREE_TYPE (domain)
18433 && !get_AT (subrange_die, DW_AT_type))
18435 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18436 TREE_TYPE field. We can't emit debug info for this
18437 because it is an unnamed integral type. */
18438 if (TREE_CODE (domain) == INTEGER_TYPE
18439 && TYPE_NAME (domain) == NULL_TREE
18440 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18441 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18443 else
18444 add_type_attribute (subrange_die, TREE_TYPE (domain),
18445 TYPE_UNQUALIFIED, false, type_die);
18448 /* ??? If upper is NULL, the array has unspecified length,
18449 but it does have a lower bound. This happens with Fortran
18450 dimension arr(N:*)
18451 Since the debugger is definitely going to need to know N
18452 to produce useful results, go ahead and output the lower
18453 bound solo, and hope the debugger can cope. */
18455 if (!get_AT (subrange_die, DW_AT_lower_bound))
18456 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18457 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18458 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18461 /* Otherwise we have an array type with an unspecified length. The
18462 DWARF-2 spec does not say how to handle this; let's just leave out the
18463 bounds. */
18467 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18469 static void
18470 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18472 dw_die_ref decl_die;
18473 HOST_WIDE_INT size;
18474 dw_loc_descr_ref size_expr = NULL;
18476 switch (TREE_CODE (tree_node))
18478 case ERROR_MARK:
18479 size = 0;
18480 break;
18481 case ENUMERAL_TYPE:
18482 case RECORD_TYPE:
18483 case UNION_TYPE:
18484 case QUAL_UNION_TYPE:
18485 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18486 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18488 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18489 return;
18491 size_expr = type_byte_size (tree_node, &size);
18492 break;
18493 case FIELD_DECL:
18494 /* For a data member of a struct or union, the DW_AT_byte_size is
18495 generally given as the number of bytes normally allocated for an
18496 object of the *declared* type of the member itself. This is true
18497 even for bit-fields. */
18498 size = int_size_in_bytes (field_type (tree_node));
18499 break;
18500 default:
18501 gcc_unreachable ();
18504 /* Support for dynamically-sized objects was introduced by DWARFv3.
18505 At the moment, GDB does not handle variable byte sizes very well,
18506 though. */
18507 if ((dwarf_version >= 3 || !dwarf_strict)
18508 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18509 && size_expr != NULL)
18510 add_AT_loc (die, DW_AT_byte_size, size_expr);
18512 /* Note that `size' might be -1 when we get to this point. If it is, that
18513 indicates that the byte size of the entity in question is variable and
18514 that we could not generate a DWARF expression that computes it. */
18515 if (size >= 0)
18516 add_AT_unsigned (die, DW_AT_byte_size, size);
18519 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18520 which specifies the distance in bits from the highest order bit of the
18521 "containing object" for the bit-field to the highest order bit of the
18522 bit-field itself.
18524 For any given bit-field, the "containing object" is a hypothetical object
18525 (of some integral or enum type) within which the given bit-field lives. The
18526 type of this hypothetical "containing object" is always the same as the
18527 declared type of the individual bit-field itself. The determination of the
18528 exact location of the "containing object" for a bit-field is rather
18529 complicated. It's handled by the `field_byte_offset' function (above).
18531 CTX is required: see the comment for VLR_CONTEXT.
18533 Note that it is the size (in bytes) of the hypothetical "containing object"
18534 which will be given in the DW_AT_byte_size attribute for this bit-field.
18535 (See `byte_size_attribute' above). */
18537 static inline void
18538 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18540 HOST_WIDE_INT object_offset_in_bytes;
18541 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18542 HOST_WIDE_INT bitpos_int;
18543 HOST_WIDE_INT highest_order_object_bit_offset;
18544 HOST_WIDE_INT highest_order_field_bit_offset;
18545 HOST_WIDE_INT bit_offset;
18547 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18549 /* Must be a field and a bit field. */
18550 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18552 /* We can't yet handle bit-fields whose offsets are variable, so if we
18553 encounter such things, just return without generating any attribute
18554 whatsoever. Likewise for variable or too large size. */
18555 if (! tree_fits_shwi_p (bit_position (decl))
18556 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18557 return;
18559 bitpos_int = int_bit_position (decl);
18561 /* Note that the bit offset is always the distance (in bits) from the
18562 highest-order bit of the "containing object" to the highest-order bit of
18563 the bit-field itself. Since the "high-order end" of any object or field
18564 is different on big-endian and little-endian machines, the computation
18565 below must take account of these differences. */
18566 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18567 highest_order_field_bit_offset = bitpos_int;
18569 if (! BYTES_BIG_ENDIAN)
18571 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18572 highest_order_object_bit_offset +=
18573 simple_type_size_in_bits (original_type);
18576 bit_offset
18577 = (! BYTES_BIG_ENDIAN
18578 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18579 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18581 if (bit_offset < 0)
18582 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18583 else
18584 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18587 /* For a FIELD_DECL node which represents a bit field, output an attribute
18588 which specifies the length in bits of the given field. */
18590 static inline void
18591 add_bit_size_attribute (dw_die_ref die, tree decl)
18593 /* Must be a field and a bit field. */
18594 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18595 && DECL_BIT_FIELD_TYPE (decl));
18597 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18598 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18601 /* If the compiled language is ANSI C, then add a 'prototyped'
18602 attribute, if arg types are given for the parameters of a function. */
18604 static inline void
18605 add_prototyped_attribute (dw_die_ref die, tree func_type)
18607 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18609 case DW_LANG_C:
18610 case DW_LANG_C89:
18611 case DW_LANG_C99:
18612 case DW_LANG_C11:
18613 case DW_LANG_ObjC:
18614 if (prototype_p (func_type))
18615 add_AT_flag (die, DW_AT_prototyped, 1);
18616 break;
18617 default:
18618 break;
18622 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18623 by looking in either the type declaration or object declaration
18624 equate table. */
18626 static inline dw_die_ref
18627 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18629 dw_die_ref origin_die = NULL;
18631 if (TREE_CODE (origin) != FUNCTION_DECL)
18633 /* We may have gotten separated from the block for the inlined
18634 function, if we're in an exception handler or some such; make
18635 sure that the abstract function has been written out.
18637 Doing this for nested functions is wrong, however; functions are
18638 distinct units, and our context might not even be inline. */
18639 tree fn = origin;
18641 if (TYPE_P (fn))
18642 fn = TYPE_STUB_DECL (fn);
18644 fn = decl_function_context (fn);
18645 if (fn)
18646 dwarf2out_abstract_function (fn);
18649 if (DECL_P (origin))
18650 origin_die = lookup_decl_die (origin);
18651 else if (TYPE_P (origin))
18652 origin_die = lookup_type_die (origin);
18654 /* XXX: Functions that are never lowered don't always have correct block
18655 trees (in the case of java, they simply have no block tree, in some other
18656 languages). For these functions, there is nothing we can really do to
18657 output correct debug info for inlined functions in all cases. Rather
18658 than die, we'll just produce deficient debug info now, in that we will
18659 have variables without a proper abstract origin. In the future, when all
18660 functions are lowered, we should re-add a gcc_assert (origin_die)
18661 here. */
18663 if (origin_die)
18664 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18665 return origin_die;
18668 /* We do not currently support the pure_virtual attribute. */
18670 static inline void
18671 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18673 if (DECL_VINDEX (func_decl))
18675 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18677 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18678 add_AT_loc (die, DW_AT_vtable_elem_location,
18679 new_loc_descr (DW_OP_constu,
18680 tree_to_shwi (DECL_VINDEX (func_decl)),
18681 0));
18683 /* GNU extension: Record what type this method came from originally. */
18684 if (debug_info_level > DINFO_LEVEL_TERSE
18685 && DECL_CONTEXT (func_decl))
18686 add_AT_die_ref (die, DW_AT_containing_type,
18687 lookup_type_die (DECL_CONTEXT (func_decl)));
18691 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18692 given decl. This used to be a vendor extension until after DWARF 4
18693 standardized it. */
18695 static void
18696 add_linkage_attr (dw_die_ref die, tree decl)
18698 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18700 /* Mimic what assemble_name_raw does with a leading '*'. */
18701 if (name[0] == '*')
18702 name = &name[1];
18704 if (dwarf_version >= 4)
18705 add_AT_string (die, DW_AT_linkage_name, name);
18706 else
18707 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18710 /* Add source coordinate attributes for the given decl. */
18712 static void
18713 add_src_coords_attributes (dw_die_ref die, tree decl)
18715 expanded_location s;
18717 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18718 return;
18719 s = expand_location (DECL_SOURCE_LOCATION (decl));
18720 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18721 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18724 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18726 static void
18727 add_linkage_name_raw (dw_die_ref die, tree decl)
18729 /* Defer until we have an assembler name set. */
18730 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18732 limbo_die_node *asm_name;
18734 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18735 asm_name->die = die;
18736 asm_name->created_for = decl;
18737 asm_name->next = deferred_asm_name;
18738 deferred_asm_name = asm_name;
18740 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18741 add_linkage_attr (die, decl);
18744 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18746 static void
18747 add_linkage_name (dw_die_ref die, tree decl)
18749 if (debug_info_level > DINFO_LEVEL_NONE
18750 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18751 && TREE_PUBLIC (decl)
18752 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18753 && die->die_tag != DW_TAG_member)
18754 add_linkage_name_raw (die, decl);
18757 /* Add a DW_AT_name attribute and source coordinate attribute for the
18758 given decl, but only if it actually has a name. */
18760 static void
18761 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18763 tree decl_name;
18765 decl_name = DECL_NAME (decl);
18766 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18768 const char *name = dwarf2_name (decl, 0);
18769 if (name)
18770 add_name_attribute (die, name);
18771 if (! DECL_ARTIFICIAL (decl))
18772 add_src_coords_attributes (die, decl);
18774 add_linkage_name (die, decl);
18777 #ifdef VMS_DEBUGGING_INFO
18778 /* Get the function's name, as described by its RTL. This may be different
18779 from the DECL_NAME name used in the source file. */
18780 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18782 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18783 XEXP (DECL_RTL (decl), 0), false);
18784 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18786 #endif /* VMS_DEBUGGING_INFO */
18789 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18791 static void
18792 add_discr_value (dw_die_ref die, dw_discr_value *value)
18794 dw_attr_node attr;
18796 attr.dw_attr = DW_AT_discr_value;
18797 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18798 attr.dw_attr_val.val_entry = NULL;
18799 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18800 if (value->pos)
18801 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18802 else
18803 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18804 add_dwarf_attr (die, &attr);
18807 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18809 static void
18810 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18812 dw_attr_node attr;
18814 attr.dw_attr = DW_AT_discr_list;
18815 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18816 attr.dw_attr_val.val_entry = NULL;
18817 attr.dw_attr_val.v.val_discr_list = discr_list;
18818 add_dwarf_attr (die, &attr);
18821 static inline dw_discr_list_ref
18822 AT_discr_list (dw_attr_node *attr)
18824 return attr->dw_attr_val.v.val_discr_list;
18827 #ifdef VMS_DEBUGGING_INFO
18828 /* Output the debug main pointer die for VMS */
18830 void
18831 dwarf2out_vms_debug_main_pointer (void)
18833 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18834 dw_die_ref die;
18836 /* Allocate the VMS debug main subprogram die. */
18837 die = ggc_cleared_alloc<die_node> ();
18838 die->die_tag = DW_TAG_subprogram;
18839 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18840 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18841 current_function_funcdef_no);
18842 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18844 /* Make it the first child of comp_unit_die (). */
18845 die->die_parent = comp_unit_die ();
18846 if (comp_unit_die ()->die_child)
18848 die->die_sib = comp_unit_die ()->die_child->die_sib;
18849 comp_unit_die ()->die_child->die_sib = die;
18851 else
18853 die->die_sib = die;
18854 comp_unit_die ()->die_child = die;
18857 #endif /* VMS_DEBUGGING_INFO */
18859 /* Push a new declaration scope. */
18861 static void
18862 push_decl_scope (tree scope)
18864 vec_safe_push (decl_scope_table, scope);
18867 /* Pop a declaration scope. */
18869 static inline void
18870 pop_decl_scope (void)
18872 decl_scope_table->pop ();
18875 /* walk_tree helper function for uses_local_type, below. */
18877 static tree
18878 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
18880 if (!TYPE_P (*tp))
18881 *walk_subtrees = 0;
18882 else
18884 tree name = TYPE_NAME (*tp);
18885 if (name && DECL_P (name) && decl_function_context (name))
18886 return *tp;
18888 return NULL_TREE;
18891 /* If TYPE involves a function-local type (including a local typedef to a
18892 non-local type), returns that type; otherwise returns NULL_TREE. */
18894 static tree
18895 uses_local_type (tree type)
18897 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
18898 return used;
18901 /* Return the DIE for the scope that immediately contains this type.
18902 Non-named types that do not involve a function-local type get global
18903 scope. Named types nested in namespaces or other types get their
18904 containing scope. All other types (i.e. function-local named types) get
18905 the current active scope. */
18907 static dw_die_ref
18908 scope_die_for (tree t, dw_die_ref context_die)
18910 dw_die_ref scope_die = NULL;
18911 tree containing_scope;
18913 /* Non-types always go in the current scope. */
18914 gcc_assert (TYPE_P (t));
18916 /* Use the scope of the typedef, rather than the scope of the type
18917 it refers to. */
18918 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
18919 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
18920 else
18921 containing_scope = TYPE_CONTEXT (t);
18923 /* Use the containing namespace if there is one. */
18924 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18926 if (context_die == lookup_decl_die (containing_scope))
18927 /* OK */;
18928 else if (debug_info_level > DINFO_LEVEL_TERSE)
18929 context_die = get_context_die (containing_scope);
18930 else
18931 containing_scope = NULL_TREE;
18934 /* Ignore function type "scopes" from the C frontend. They mean that
18935 a tagged type is local to a parmlist of a function declarator, but
18936 that isn't useful to DWARF. */
18937 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18938 containing_scope = NULL_TREE;
18940 if (SCOPE_FILE_SCOPE_P (containing_scope))
18942 /* If T uses a local type keep it local as well, to avoid references
18943 to function-local DIEs from outside the function. */
18944 if (current_function_decl && uses_local_type (t))
18945 scope_die = context_die;
18946 else
18947 scope_die = comp_unit_die ();
18949 else if (TYPE_P (containing_scope))
18951 /* For types, we can just look up the appropriate DIE. */
18952 if (debug_info_level > DINFO_LEVEL_TERSE)
18953 scope_die = get_context_die (containing_scope);
18954 else
18956 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18957 if (scope_die == NULL)
18958 scope_die = comp_unit_die ();
18961 else
18962 scope_die = context_die;
18964 return scope_die;
18967 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
18969 static inline int
18970 local_scope_p (dw_die_ref context_die)
18972 for (; context_die; context_die = context_die->die_parent)
18973 if (context_die->die_tag == DW_TAG_inlined_subroutine
18974 || context_die->die_tag == DW_TAG_subprogram)
18975 return 1;
18977 return 0;
18980 /* Returns nonzero if CONTEXT_DIE is a class. */
18982 static inline int
18983 class_scope_p (dw_die_ref context_die)
18985 return (context_die
18986 && (context_die->die_tag == DW_TAG_structure_type
18987 || context_die->die_tag == DW_TAG_class_type
18988 || context_die->die_tag == DW_TAG_interface_type
18989 || context_die->die_tag == DW_TAG_union_type));
18992 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18993 whether or not to treat a DIE in this context as a declaration. */
18995 static inline int
18996 class_or_namespace_scope_p (dw_die_ref context_die)
18998 return (class_scope_p (context_die)
18999 || (context_die && context_die->die_tag == DW_TAG_namespace));
19002 /* Many forms of DIEs require a "type description" attribute. This
19003 routine locates the proper "type descriptor" die for the type given
19004 by 'type' plus any additional qualifiers given by 'cv_quals', and
19005 adds a DW_AT_type attribute below the given die. */
19007 static void
19008 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19009 bool reverse, dw_die_ref context_die)
19011 enum tree_code code = TREE_CODE (type);
19012 dw_die_ref type_die = NULL;
19014 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19015 or fixed-point type, use the inner type. This is because we have no
19016 support for unnamed types in base_type_die. This can happen if this is
19017 an Ada subrange type. Correct solution is emit a subrange type die. */
19018 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19019 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19020 type = TREE_TYPE (type), code = TREE_CODE (type);
19022 if (code == ERROR_MARK
19023 /* Handle a special case. For functions whose return type is void, we
19024 generate *no* type attribute. (Note that no object may have type
19025 `void', so this only applies to function return types). */
19026 || code == VOID_TYPE)
19027 return;
19029 type_die = modified_type_die (type,
19030 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19031 reverse,
19032 context_die);
19034 if (type_die != NULL)
19035 add_AT_die_ref (object_die, DW_AT_type, type_die);
19038 /* Given an object die, add the calling convention attribute for the
19039 function call type. */
19040 static void
19041 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19043 enum dwarf_calling_convention value = DW_CC_normal;
19045 value = ((enum dwarf_calling_convention)
19046 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19048 if (is_fortran ()
19049 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19051 /* DWARF 2 doesn't provide a way to identify a program's source-level
19052 entry point. DW_AT_calling_convention attributes are only meant
19053 to describe functions' calling conventions. However, lacking a
19054 better way to signal the Fortran main program, we used this for
19055 a long time, following existing custom. Now, DWARF 4 has
19056 DW_AT_main_subprogram, which we add below, but some tools still
19057 rely on the old way, which we thus keep. */
19058 value = DW_CC_program;
19060 if (dwarf_version >= 4 || !dwarf_strict)
19061 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19064 /* Only add the attribute if the backend requests it, and
19065 is not DW_CC_normal. */
19066 if (value && (value != DW_CC_normal))
19067 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19070 /* Given a tree pointer to a struct, class, union, or enum type node, return
19071 a pointer to the (string) tag name for the given type, or zero if the type
19072 was declared without a tag. */
19074 static const char *
19075 type_tag (const_tree type)
19077 const char *name = 0;
19079 if (TYPE_NAME (type) != 0)
19081 tree t = 0;
19083 /* Find the IDENTIFIER_NODE for the type name. */
19084 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19085 && !TYPE_NAMELESS (type))
19086 t = TYPE_NAME (type);
19088 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19089 a TYPE_DECL node, regardless of whether or not a `typedef' was
19090 involved. */
19091 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19092 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19094 /* We want to be extra verbose. Don't call dwarf_name if
19095 DECL_NAME isn't set. The default hook for decl_printable_name
19096 doesn't like that, and in this context it's correct to return
19097 0, instead of "<anonymous>" or the like. */
19098 if (DECL_NAME (TYPE_NAME (type))
19099 && !DECL_NAMELESS (TYPE_NAME (type)))
19100 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19103 /* Now get the name as a string, or invent one. */
19104 if (!name && t != 0)
19105 name = IDENTIFIER_POINTER (t);
19108 return (name == 0 || *name == '\0') ? 0 : name;
19111 /* Return the type associated with a data member, make a special check
19112 for bit field types. */
19114 static inline tree
19115 member_declared_type (const_tree member)
19117 return (DECL_BIT_FIELD_TYPE (member)
19118 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19121 /* Get the decl's label, as described by its RTL. This may be different
19122 from the DECL_NAME name used in the source file. */
19124 #if 0
19125 static const char *
19126 decl_start_label (tree decl)
19128 rtx x;
19129 const char *fnname;
19131 x = DECL_RTL (decl);
19132 gcc_assert (MEM_P (x));
19134 x = XEXP (x, 0);
19135 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19137 fnname = XSTR (x, 0);
19138 return fnname;
19140 #endif
19142 /* For variable-length arrays that have been previously generated, but
19143 may be incomplete due to missing subscript info, fill the subscript
19144 info. Return TRUE if this is one of those cases. */
19145 static bool
19146 fill_variable_array_bounds (tree type)
19148 if (TREE_ASM_WRITTEN (type)
19149 && TREE_CODE (type) == ARRAY_TYPE
19150 && variably_modified_type_p (type, NULL))
19152 dw_die_ref array_die = lookup_type_die (type);
19153 if (!array_die)
19154 return false;
19155 add_subscript_info (array_die, type, !is_ada ());
19156 return true;
19158 return false;
19161 /* These routines generate the internal representation of the DIE's for
19162 the compilation unit. Debugging information is collected by walking
19163 the declaration trees passed in from dwarf2out_decl(). */
19165 static void
19166 gen_array_type_die (tree type, dw_die_ref context_die)
19168 dw_die_ref array_die;
19170 /* GNU compilers represent multidimensional array types as sequences of one
19171 dimensional array types whose element types are themselves array types.
19172 We sometimes squish that down to a single array_type DIE with multiple
19173 subscripts in the Dwarf debugging info. The draft Dwarf specification
19174 say that we are allowed to do this kind of compression in C, because
19175 there is no difference between an array of arrays and a multidimensional
19176 array. We don't do this for Ada to remain as close as possible to the
19177 actual representation, which is especially important against the language
19178 flexibilty wrt arrays of variable size. */
19180 bool collapse_nested_arrays = !is_ada ();
19182 if (fill_variable_array_bounds (type))
19183 return;
19185 dw_die_ref scope_die = scope_die_for (type, context_die);
19186 tree element_type;
19188 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19189 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19190 if (TYPE_STRING_FLAG (type)
19191 && TREE_CODE (type) == ARRAY_TYPE
19192 && is_fortran ()
19193 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19195 HOST_WIDE_INT size;
19197 array_die = new_die (DW_TAG_string_type, scope_die, type);
19198 add_name_attribute (array_die, type_tag (type));
19199 equate_type_number_to_die (type, array_die);
19200 size = int_size_in_bytes (type);
19201 if (size >= 0)
19202 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19203 else if (TYPE_DOMAIN (type) != NULL_TREE
19204 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19205 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19207 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19208 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19210 size = int_size_in_bytes (TREE_TYPE (szdecl));
19211 if (loc && size > 0)
19213 add_AT_location_description (array_die, DW_AT_string_length, loc);
19214 if (size != DWARF2_ADDR_SIZE)
19215 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19218 return;
19221 array_die = new_die (DW_TAG_array_type, scope_die, type);
19222 add_name_attribute (array_die, type_tag (type));
19223 equate_type_number_to_die (type, array_die);
19225 if (TREE_CODE (type) == VECTOR_TYPE)
19226 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19228 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19229 if (is_fortran ()
19230 && TREE_CODE (type) == ARRAY_TYPE
19231 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19232 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19233 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19235 #if 0
19236 /* We default the array ordering. SDB will probably do
19237 the right things even if DW_AT_ordering is not present. It's not even
19238 an issue until we start to get into multidimensional arrays anyway. If
19239 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19240 then we'll have to put the DW_AT_ordering attribute back in. (But if
19241 and when we find out that we need to put these in, we will only do so
19242 for multidimensional arrays. */
19243 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19244 #endif
19246 if (TREE_CODE (type) == VECTOR_TYPE)
19248 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19249 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19250 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19251 add_bound_info (subrange_die, DW_AT_upper_bound,
19252 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19254 else
19255 add_subscript_info (array_die, type, collapse_nested_arrays);
19257 /* Add representation of the type of the elements of this array type and
19258 emit the corresponding DIE if we haven't done it already. */
19259 element_type = TREE_TYPE (type);
19260 if (collapse_nested_arrays)
19261 while (TREE_CODE (element_type) == ARRAY_TYPE)
19263 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19264 break;
19265 element_type = TREE_TYPE (element_type);
19268 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19269 TREE_CODE (type) == ARRAY_TYPE
19270 && TYPE_REVERSE_STORAGE_ORDER (type),
19271 context_die);
19273 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19274 if (TYPE_ARTIFICIAL (type))
19275 add_AT_flag (array_die, DW_AT_artificial, 1);
19277 if (get_AT (array_die, DW_AT_name))
19278 add_pubtype (type, array_die);
19281 /* This routine generates DIE for array with hidden descriptor, details
19282 are filled into *info by a langhook. */
19284 static void
19285 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19286 dw_die_ref context_die)
19288 const dw_die_ref scope_die = scope_die_for (type, context_die);
19289 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19290 const struct loc_descr_context context = { type, info->base_decl, NULL };
19291 int dim;
19293 add_name_attribute (array_die, type_tag (type));
19294 equate_type_number_to_die (type, array_die);
19296 if (info->ndimensions > 1)
19297 switch (info->ordering)
19299 case array_descr_ordering_row_major:
19300 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19301 break;
19302 case array_descr_ordering_column_major:
19303 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19304 break;
19305 default:
19306 break;
19309 if (dwarf_version >= 3 || !dwarf_strict)
19311 if (info->data_location)
19312 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19313 dw_scalar_form_exprloc, &context);
19314 if (info->associated)
19315 add_scalar_info (array_die, DW_AT_associated, info->associated,
19316 dw_scalar_form_constant
19317 | dw_scalar_form_exprloc
19318 | dw_scalar_form_reference, &context);
19319 if (info->allocated)
19320 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19321 dw_scalar_form_constant
19322 | dw_scalar_form_exprloc
19323 | dw_scalar_form_reference, &context);
19324 if (info->stride)
19326 const enum dwarf_attribute attr
19327 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19328 const int forms
19329 = (info->stride_in_bits)
19330 ? dw_scalar_form_constant
19331 : (dw_scalar_form_constant
19332 | dw_scalar_form_exprloc
19333 | dw_scalar_form_reference);
19335 add_scalar_info (array_die, attr, info->stride, forms, &context);
19339 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19341 for (dim = 0; dim < info->ndimensions; dim++)
19343 dw_die_ref subrange_die
19344 = new_die (DW_TAG_subrange_type, array_die, NULL);
19346 if (info->dimen[dim].bounds_type)
19347 add_type_attribute (subrange_die,
19348 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19349 false, context_die);
19350 if (info->dimen[dim].lower_bound)
19351 add_bound_info (subrange_die, DW_AT_lower_bound,
19352 info->dimen[dim].lower_bound, &context);
19353 if (info->dimen[dim].upper_bound)
19354 add_bound_info (subrange_die, DW_AT_upper_bound,
19355 info->dimen[dim].upper_bound, &context);
19356 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19357 add_scalar_info (subrange_die, DW_AT_byte_stride,
19358 info->dimen[dim].stride,
19359 dw_scalar_form_constant
19360 | dw_scalar_form_exprloc
19361 | dw_scalar_form_reference,
19362 &context);
19365 gen_type_die (info->element_type, context_die);
19366 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19367 TREE_CODE (type) == ARRAY_TYPE
19368 && TYPE_REVERSE_STORAGE_ORDER (type),
19369 context_die);
19371 if (get_AT (array_die, DW_AT_name))
19372 add_pubtype (type, array_die);
19375 #if 0
19376 static void
19377 gen_entry_point_die (tree decl, dw_die_ref context_die)
19379 tree origin = decl_ultimate_origin (decl);
19380 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19382 if (origin != NULL)
19383 add_abstract_origin_attribute (decl_die, origin);
19384 else
19386 add_name_and_src_coords_attributes (decl_die, decl);
19387 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19388 TYPE_UNQUALIFIED, false, context_die);
19391 if (DECL_ABSTRACT_P (decl))
19392 equate_decl_number_to_die (decl, decl_die);
19393 else
19394 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19396 #endif
19398 /* Walk through the list of incomplete types again, trying once more to
19399 emit full debugging info for them. */
19401 static void
19402 retry_incomplete_types (void)
19404 int i;
19406 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19407 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19408 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19411 /* Determine what tag to use for a record type. */
19413 static enum dwarf_tag
19414 record_type_tag (tree type)
19416 if (! lang_hooks.types.classify_record)
19417 return DW_TAG_structure_type;
19419 switch (lang_hooks.types.classify_record (type))
19421 case RECORD_IS_STRUCT:
19422 return DW_TAG_structure_type;
19424 case RECORD_IS_CLASS:
19425 return DW_TAG_class_type;
19427 case RECORD_IS_INTERFACE:
19428 if (dwarf_version >= 3 || !dwarf_strict)
19429 return DW_TAG_interface_type;
19430 return DW_TAG_structure_type;
19432 default:
19433 gcc_unreachable ();
19437 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19438 include all of the information about the enumeration values also. Each
19439 enumerated type name/value is listed as a child of the enumerated type
19440 DIE. */
19442 static dw_die_ref
19443 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19445 dw_die_ref type_die = lookup_type_die (type);
19447 if (type_die == NULL)
19449 type_die = new_die (DW_TAG_enumeration_type,
19450 scope_die_for (type, context_die), type);
19451 equate_type_number_to_die (type, type_die);
19452 add_name_attribute (type_die, type_tag (type));
19453 if (dwarf_version >= 4 || !dwarf_strict)
19455 if (ENUM_IS_SCOPED (type))
19456 add_AT_flag (type_die, DW_AT_enum_class, 1);
19457 if (ENUM_IS_OPAQUE (type))
19458 add_AT_flag (type_die, DW_AT_declaration, 1);
19461 else if (! TYPE_SIZE (type))
19462 return type_die;
19463 else
19464 remove_AT (type_die, DW_AT_declaration);
19466 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19467 given enum type is incomplete, do not generate the DW_AT_byte_size
19468 attribute or the DW_AT_element_list attribute. */
19469 if (TYPE_SIZE (type))
19471 tree link;
19473 TREE_ASM_WRITTEN (type) = 1;
19474 add_byte_size_attribute (type_die, type);
19475 if (dwarf_version >= 3 || !dwarf_strict)
19477 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19478 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19479 context_die);
19481 if (TYPE_STUB_DECL (type) != NULL_TREE)
19483 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19484 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19487 /* If the first reference to this type was as the return type of an
19488 inline function, then it may not have a parent. Fix this now. */
19489 if (type_die->die_parent == NULL)
19490 add_child_die (scope_die_for (type, context_die), type_die);
19492 for (link = TYPE_VALUES (type);
19493 link != NULL; link = TREE_CHAIN (link))
19495 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19496 tree value = TREE_VALUE (link);
19498 add_name_attribute (enum_die,
19499 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19501 if (TREE_CODE (value) == CONST_DECL)
19502 value = DECL_INITIAL (value);
19504 if (simple_type_size_in_bits (TREE_TYPE (value))
19505 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19507 /* For constant forms created by add_AT_unsigned DWARF
19508 consumers (GDB, elfutils, etc.) always zero extend
19509 the value. Only when the actual value is negative
19510 do we need to use add_AT_int to generate a constant
19511 form that can represent negative values. */
19512 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19513 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19514 add_AT_unsigned (enum_die, DW_AT_const_value,
19515 (unsigned HOST_WIDE_INT) val);
19516 else
19517 add_AT_int (enum_die, DW_AT_const_value, val);
19519 else
19520 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19521 that here. TODO: This should be re-worked to use correct
19522 signed/unsigned double tags for all cases. */
19523 add_AT_wide (enum_die, DW_AT_const_value, value);
19526 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19527 if (TYPE_ARTIFICIAL (type))
19528 add_AT_flag (type_die, DW_AT_artificial, 1);
19530 else
19531 add_AT_flag (type_die, DW_AT_declaration, 1);
19533 add_pubtype (type, type_die);
19535 return type_die;
19538 /* Generate a DIE to represent either a real live formal parameter decl or to
19539 represent just the type of some formal parameter position in some function
19540 type.
19542 Note that this routine is a bit unusual because its argument may be a
19543 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19544 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19545 node. If it's the former then this function is being called to output a
19546 DIE to represent a formal parameter object (or some inlining thereof). If
19547 it's the latter, then this function is only being called to output a
19548 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19549 argument type of some subprogram type.
19550 If EMIT_NAME_P is true, name and source coordinate attributes
19551 are emitted. */
19553 static dw_die_ref
19554 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19555 dw_die_ref context_die)
19557 tree node_or_origin = node ? node : origin;
19558 tree ultimate_origin;
19559 dw_die_ref parm_die = NULL;
19561 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19563 parm_die = lookup_decl_die (node);
19565 /* If the contexts differ, we may not be talking about the same
19566 thing. */
19567 if (parm_die && parm_die->die_parent != context_die)
19569 if (!DECL_ABSTRACT_P (node))
19571 /* This can happen when creating an inlined instance, in
19572 which case we need to create a new DIE that will get
19573 annotated with DW_AT_abstract_origin. */
19574 parm_die = NULL;
19576 else
19578 /* FIXME: Reuse DIE even with a differing context.
19580 This can happen when calling
19581 dwarf2out_abstract_function to build debug info for
19582 the abstract instance of a function for which we have
19583 already generated a DIE in
19584 dwarf2out_early_global_decl.
19586 Once we remove dwarf2out_abstract_function, we should
19587 have a call to gcc_unreachable here. */
19591 if (parm_die && parm_die->die_parent == NULL)
19593 /* Check that parm_die already has the right attributes that
19594 we would have added below. If any attributes are
19595 missing, fall through to add them. */
19596 if (! DECL_ABSTRACT_P (node_or_origin)
19597 && !get_AT (parm_die, DW_AT_location)
19598 && !get_AT (parm_die, DW_AT_const_value))
19599 /* We are missing location info, and are about to add it. */
19601 else
19603 add_child_die (context_die, parm_die);
19604 return parm_die;
19609 /* If we have a previously generated DIE, use it, unless this is an
19610 concrete instance (origin != NULL), in which case we need a new
19611 DIE with a corresponding DW_AT_abstract_origin. */
19612 bool reusing_die;
19613 if (parm_die && origin == NULL)
19614 reusing_die = true;
19615 else
19617 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19618 reusing_die = false;
19621 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19623 case tcc_declaration:
19624 ultimate_origin = decl_ultimate_origin (node_or_origin);
19625 if (node || ultimate_origin)
19626 origin = ultimate_origin;
19628 if (reusing_die)
19629 goto add_location;
19631 if (origin != NULL)
19632 add_abstract_origin_attribute (parm_die, origin);
19633 else if (emit_name_p)
19634 add_name_and_src_coords_attributes (parm_die, node);
19635 if (origin == NULL
19636 || (! DECL_ABSTRACT_P (node_or_origin)
19637 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19638 decl_function_context
19639 (node_or_origin))))
19641 tree type = TREE_TYPE (node_or_origin);
19642 if (decl_by_reference_p (node_or_origin))
19643 add_type_attribute (parm_die, TREE_TYPE (type),
19644 TYPE_UNQUALIFIED,
19645 false, context_die);
19646 else
19647 add_type_attribute (parm_die, type,
19648 decl_quals (node_or_origin),
19649 false, context_die);
19651 if (origin == NULL && DECL_ARTIFICIAL (node))
19652 add_AT_flag (parm_die, DW_AT_artificial, 1);
19653 add_location:
19654 if (node && node != origin)
19655 equate_decl_number_to_die (node, parm_die);
19656 if (! DECL_ABSTRACT_P (node_or_origin))
19657 add_location_or_const_value_attribute (parm_die, node_or_origin,
19658 node == NULL);
19660 break;
19662 case tcc_type:
19663 /* We were called with some kind of a ..._TYPE node. */
19664 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19665 context_die);
19666 break;
19668 default:
19669 gcc_unreachable ();
19672 return parm_die;
19675 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19676 children DW_TAG_formal_parameter DIEs representing the arguments of the
19677 parameter pack.
19679 PARM_PACK must be a function parameter pack.
19680 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19681 must point to the subsequent arguments of the function PACK_ARG belongs to.
19682 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19683 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19684 following the last one for which a DIE was generated. */
19686 static dw_die_ref
19687 gen_formal_parameter_pack_die (tree parm_pack,
19688 tree pack_arg,
19689 dw_die_ref subr_die,
19690 tree *next_arg)
19692 tree arg;
19693 dw_die_ref parm_pack_die;
19695 gcc_assert (parm_pack
19696 && lang_hooks.function_parameter_pack_p (parm_pack)
19697 && subr_die);
19699 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19700 add_src_coords_attributes (parm_pack_die, parm_pack);
19702 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19704 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19705 parm_pack))
19706 break;
19707 gen_formal_parameter_die (arg, NULL,
19708 false /* Don't emit name attribute. */,
19709 parm_pack_die);
19711 if (next_arg)
19712 *next_arg = arg;
19713 return parm_pack_die;
19716 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19717 at the end of an (ANSI prototyped) formal parameters list. */
19719 static void
19720 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19722 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19725 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19726 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19727 parameters as specified in some function type specification (except for
19728 those which appear as part of a function *definition*). */
19730 static void
19731 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19733 tree link;
19734 tree formal_type = NULL;
19735 tree first_parm_type;
19736 tree arg;
19738 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19740 arg = DECL_ARGUMENTS (function_or_method_type);
19741 function_or_method_type = TREE_TYPE (function_or_method_type);
19743 else
19744 arg = NULL_TREE;
19746 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19748 /* Make our first pass over the list of formal parameter types and output a
19749 DW_TAG_formal_parameter DIE for each one. */
19750 for (link = first_parm_type; link; )
19752 dw_die_ref parm_die;
19754 formal_type = TREE_VALUE (link);
19755 if (formal_type == void_type_node)
19756 break;
19758 /* Output a (nameless) DIE to represent the formal parameter itself. */
19759 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19761 parm_die = gen_formal_parameter_die (formal_type, NULL,
19762 true /* Emit name attribute. */,
19763 context_die);
19764 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19765 && link == first_parm_type)
19767 add_AT_flag (parm_die, DW_AT_artificial, 1);
19768 if (dwarf_version >= 3 || !dwarf_strict)
19769 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19771 else if (arg && DECL_ARTIFICIAL (arg))
19772 add_AT_flag (parm_die, DW_AT_artificial, 1);
19775 link = TREE_CHAIN (link);
19776 if (arg)
19777 arg = DECL_CHAIN (arg);
19780 /* If this function type has an ellipsis, add a
19781 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
19782 if (formal_type != void_type_node)
19783 gen_unspecified_parameters_die (function_or_method_type, context_die);
19785 /* Make our second (and final) pass over the list of formal parameter types
19786 and output DIEs to represent those types (as necessary). */
19787 for (link = TYPE_ARG_TYPES (function_or_method_type);
19788 link && TREE_VALUE (link);
19789 link = TREE_CHAIN (link))
19790 gen_type_die (TREE_VALUE (link), context_die);
19793 /* We want to generate the DIE for TYPE so that we can generate the
19794 die for MEMBER, which has been defined; we will need to refer back
19795 to the member declaration nested within TYPE. If we're trying to
19796 generate minimal debug info for TYPE, processing TYPE won't do the
19797 trick; we need to attach the member declaration by hand. */
19799 static void
19800 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19802 gen_type_die (type, context_die);
19804 /* If we're trying to avoid duplicate debug info, we may not have
19805 emitted the member decl for this function. Emit it now. */
19806 if (TYPE_STUB_DECL (type)
19807 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19808 && ! lookup_decl_die (member))
19810 dw_die_ref type_die;
19811 gcc_assert (!decl_ultimate_origin (member));
19813 push_decl_scope (type);
19814 type_die = lookup_type_die_strip_naming_typedef (type);
19815 if (TREE_CODE (member) == FUNCTION_DECL)
19816 gen_subprogram_die (member, type_die);
19817 else if (TREE_CODE (member) == FIELD_DECL)
19819 /* Ignore the nameless fields that are used to skip bits but handle
19820 C++ anonymous unions and structs. */
19821 if (DECL_NAME (member) != NULL_TREE
19822 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19823 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19825 struct vlr_context vlr_ctx = {
19826 DECL_CONTEXT (member), /* struct_type */
19827 NULL_TREE /* variant_part_offset */
19829 gen_type_die (member_declared_type (member), type_die);
19830 gen_field_die (member, &vlr_ctx, type_die);
19833 else
19834 gen_variable_die (member, NULL_TREE, type_die);
19836 pop_decl_scope ();
19840 /* Forward declare these functions, because they are mutually recursive
19841 with their set_block_* pairing functions. */
19842 static void set_decl_origin_self (tree);
19843 static void set_decl_abstract_flags (tree, vec<tree> &);
19845 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
19846 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
19847 that it points to the node itself, thus indicating that the node is its
19848 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
19849 the given node is NULL, recursively descend the decl/block tree which
19850 it is the root of, and for each other ..._DECL or BLOCK node contained
19851 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
19852 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
19853 values to point to themselves. */
19855 static void
19856 set_block_origin_self (tree stmt)
19858 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
19860 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
19863 tree local_decl;
19865 for (local_decl = BLOCK_VARS (stmt);
19866 local_decl != NULL_TREE;
19867 local_decl = DECL_CHAIN (local_decl))
19868 /* Do not recurse on nested functions since the inlining status
19869 of parent and child can be different as per the DWARF spec. */
19870 if (TREE_CODE (local_decl) != FUNCTION_DECL
19871 && !DECL_EXTERNAL (local_decl))
19872 set_decl_origin_self (local_decl);
19876 tree subblock;
19878 for (subblock = BLOCK_SUBBLOCKS (stmt);
19879 subblock != NULL_TREE;
19880 subblock = BLOCK_CHAIN (subblock))
19881 set_block_origin_self (subblock); /* Recurse. */
19886 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
19887 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
19888 node to so that it points to the node itself, thus indicating that the
19889 node represents its own (abstract) origin. Additionally, if the
19890 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
19891 the decl/block tree of which the given node is the root of, and for
19892 each other ..._DECL or BLOCK node contained therein whose
19893 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
19894 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
19895 point to themselves. */
19897 static void
19898 set_decl_origin_self (tree decl)
19900 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
19902 DECL_ABSTRACT_ORIGIN (decl) = decl;
19903 if (TREE_CODE (decl) == FUNCTION_DECL)
19905 tree arg;
19907 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19908 DECL_ABSTRACT_ORIGIN (arg) = arg;
19909 if (DECL_INITIAL (decl) != NULL_TREE
19910 && DECL_INITIAL (decl) != error_mark_node)
19911 set_block_origin_self (DECL_INITIAL (decl));
19916 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
19917 and if it wasn't 1 before, push it to abstract_vec vector.
19918 For all local decls and all local sub-blocks (recursively) do it
19919 too. */
19921 static void
19922 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
19924 tree local_decl;
19925 tree subblock;
19926 unsigned int i;
19928 if (!BLOCK_ABSTRACT (stmt))
19930 abstract_vec.safe_push (stmt);
19931 BLOCK_ABSTRACT (stmt) = 1;
19934 for (local_decl = BLOCK_VARS (stmt);
19935 local_decl != NULL_TREE;
19936 local_decl = DECL_CHAIN (local_decl))
19937 if (! DECL_EXTERNAL (local_decl))
19938 set_decl_abstract_flags (local_decl, abstract_vec);
19940 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19942 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
19943 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
19944 || TREE_CODE (local_decl) == PARM_DECL)
19945 set_decl_abstract_flags (local_decl, abstract_vec);
19948 for (subblock = BLOCK_SUBBLOCKS (stmt);
19949 subblock != NULL_TREE;
19950 subblock = BLOCK_CHAIN (subblock))
19951 set_block_abstract_flags (subblock, abstract_vec);
19954 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
19955 to 1 and if it wasn't 1 before, push to abstract_vec vector.
19956 In the case where the decl is a FUNCTION_DECL also set the abstract
19957 flags for all of the parameters, local vars, local
19958 blocks and sub-blocks (recursively). */
19960 static void
19961 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
19963 if (!DECL_ABSTRACT_P (decl))
19965 abstract_vec.safe_push (decl);
19966 DECL_ABSTRACT_P (decl) = 1;
19969 if (TREE_CODE (decl) == FUNCTION_DECL)
19971 tree arg;
19973 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
19974 if (!DECL_ABSTRACT_P (arg))
19976 abstract_vec.safe_push (arg);
19977 DECL_ABSTRACT_P (arg) = 1;
19979 if (DECL_INITIAL (decl) != NULL_TREE
19980 && DECL_INITIAL (decl) != error_mark_node)
19981 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
19985 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19986 may later generate inlined and/or out-of-line instances of.
19988 FIXME: In the early-dwarf world, this function, and most of the
19989 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
19990 the abstract instance. All we would need to do is annotate
19991 the early DIE with the appropriate DW_AT_inline in late
19992 dwarf (perhaps in gen_inlined_subroutine_die).
19994 However, we can't do this yet, because LTO streaming of DIEs
19995 has not been implemented yet. */
19997 static void
19998 dwarf2out_abstract_function (tree decl)
20000 dw_die_ref old_die;
20001 tree save_fn;
20002 tree context;
20003 hash_table<decl_loc_hasher> *old_decl_loc_table;
20004 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20005 int old_call_site_count, old_tail_call_site_count;
20006 struct call_arg_loc_node *old_call_arg_locations;
20008 /* Make sure we have the actual abstract inline, not a clone. */
20009 decl = DECL_ORIGIN (decl);
20011 old_die = lookup_decl_die (decl);
20012 if (old_die && get_AT (old_die, DW_AT_inline))
20013 /* We've already generated the abstract instance. */
20014 return;
20016 /* We can be called while recursively when seeing block defining inlined subroutine
20017 DIE. Be sure to not clobber the outer location table nor use it or we would
20018 get locations in abstract instantces. */
20019 old_decl_loc_table = decl_loc_table;
20020 decl_loc_table = NULL;
20021 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20022 cached_dw_loc_list_table = NULL;
20023 old_call_arg_locations = call_arg_locations;
20024 call_arg_locations = NULL;
20025 old_call_site_count = call_site_count;
20026 call_site_count = -1;
20027 old_tail_call_site_count = tail_call_site_count;
20028 tail_call_site_count = -1;
20030 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20031 we don't get confused by DECL_ABSTRACT_P. */
20032 if (debug_info_level > DINFO_LEVEL_TERSE)
20034 context = decl_class_context (decl);
20035 if (context)
20036 gen_type_die_for_member
20037 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20040 /* Pretend we've just finished compiling this function. */
20041 save_fn = current_function_decl;
20042 current_function_decl = decl;
20044 auto_vec<tree, 64> abstract_vec;
20045 set_decl_abstract_flags (decl, abstract_vec);
20046 dwarf2out_decl (decl);
20047 unsigned int i;
20048 tree t;
20049 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20050 if (TREE_CODE (t) == BLOCK)
20051 BLOCK_ABSTRACT (t) = 0;
20052 else
20053 DECL_ABSTRACT_P (t) = 0;
20055 current_function_decl = save_fn;
20056 decl_loc_table = old_decl_loc_table;
20057 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20058 call_arg_locations = old_call_arg_locations;
20059 call_site_count = old_call_site_count;
20060 tail_call_site_count = old_tail_call_site_count;
20063 /* Helper function of premark_used_types() which gets called through
20064 htab_traverse.
20066 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20067 marked as unused by prune_unused_types. */
20069 bool
20070 premark_used_types_helper (tree const &type, void *)
20072 dw_die_ref die;
20074 die = lookup_type_die (type);
20075 if (die != NULL)
20076 die->die_perennial_p = 1;
20077 return true;
20080 /* Helper function of premark_types_used_by_global_vars which gets called
20081 through htab_traverse.
20083 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20084 marked as unused by prune_unused_types. The DIE of the type is marked
20085 only if the global variable using the type will actually be emitted. */
20088 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20089 void *)
20091 struct types_used_by_vars_entry *entry;
20092 dw_die_ref die;
20094 entry = (struct types_used_by_vars_entry *) *slot;
20095 gcc_assert (entry->type != NULL
20096 && entry->var_decl != NULL);
20097 die = lookup_type_die (entry->type);
20098 if (die)
20100 /* Ask cgraph if the global variable really is to be emitted.
20101 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20102 varpool_node *node = varpool_node::get (entry->var_decl);
20103 if (node && node->definition)
20105 die->die_perennial_p = 1;
20106 /* Keep the parent DIEs as well. */
20107 while ((die = die->die_parent) && die->die_perennial_p == 0)
20108 die->die_perennial_p = 1;
20111 return 1;
20114 /* Mark all members of used_types_hash as perennial. */
20116 static void
20117 premark_used_types (struct function *fun)
20119 if (fun && fun->used_types_hash)
20120 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20123 /* Mark all members of types_used_by_vars_entry as perennial. */
20125 static void
20126 premark_types_used_by_global_vars (void)
20128 if (types_used_by_vars_hash)
20129 types_used_by_vars_hash
20130 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20133 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20134 for CA_LOC call arg loc node. */
20136 static dw_die_ref
20137 gen_call_site_die (tree decl, dw_die_ref subr_die,
20138 struct call_arg_loc_node *ca_loc)
20140 dw_die_ref stmt_die = NULL, die;
20141 tree block = ca_loc->block;
20143 while (block
20144 && block != DECL_INITIAL (decl)
20145 && TREE_CODE (block) == BLOCK)
20147 stmt_die = BLOCK_DIE (block);
20148 if (stmt_die)
20149 break;
20150 block = BLOCK_SUPERCONTEXT (block);
20152 if (stmt_die == NULL)
20153 stmt_die = subr_die;
20154 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20155 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20156 if (ca_loc->tail_call_p)
20157 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20158 if (ca_loc->symbol_ref)
20160 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20161 if (tdie)
20162 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20163 else
20164 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20166 return die;
20169 /* Generate a DIE to represent a declared function (either file-scope or
20170 block-local). */
20172 static void
20173 gen_subprogram_die (tree decl, dw_die_ref context_die)
20175 tree origin = decl_ultimate_origin (decl);
20176 dw_die_ref subr_die;
20177 dw_die_ref old_die = lookup_decl_die (decl);
20179 /* This function gets called multiple times for different stages of
20180 the debug process. For example, for func() in this code:
20182 namespace S
20184 void func() { ... }
20187 ...we get called 4 times. Twice in early debug and twice in
20188 late debug:
20190 Early debug
20191 -----------
20193 1. Once while generating func() within the namespace. This is
20194 the declaration. The declaration bit below is set, as the
20195 context is the namespace.
20197 A new DIE will be generated with DW_AT_declaration set.
20199 2. Once for func() itself. This is the specification. The
20200 declaration bit below is clear as the context is the CU.
20202 We will use the cached DIE from (1) to create a new DIE with
20203 DW_AT_specification pointing to the declaration in (1).
20205 Late debug via rest_of_handle_final()
20206 -------------------------------------
20208 3. Once generating func() within the namespace. This is also the
20209 declaration, as in (1), but this time we will early exit below
20210 as we have a cached DIE and a declaration needs no additional
20211 annotations (no locations), as the source declaration line
20212 info is enough.
20214 4. Once for func() itself. As in (2), this is the specification,
20215 but this time we will re-use the cached DIE, and just annotate
20216 it with the location information that should now be available.
20218 For something without namespaces, but with abstract instances, we
20219 are also called a multiple times:
20221 class Base
20223 public:
20224 Base (); // constructor declaration (1)
20227 Base::Base () { } // constructor specification (2)
20229 Early debug
20230 -----------
20232 1. Once for the Base() constructor by virtue of it being a
20233 member of the Base class. This is done via
20234 rest_of_type_compilation.
20236 This is a declaration, so a new DIE will be created with
20237 DW_AT_declaration.
20239 2. Once for the Base() constructor definition, but this time
20240 while generating the abstract instance of the base
20241 constructor (__base_ctor) which is being generated via early
20242 debug of reachable functions.
20244 Even though we have a cached version of the declaration (1),
20245 we will create a DW_AT_specification of the declaration DIE
20246 in (1).
20248 3. Once for the __base_ctor itself, but this time, we generate
20249 an DW_AT_abstract_origin version of the DW_AT_specification in
20250 (2).
20252 Late debug via rest_of_handle_final
20253 -----------------------------------
20255 4. One final time for the __base_ctor (which will have a cached
20256 DIE with DW_AT_abstract_origin created in (3). This time,
20257 we will just annotate the location information now
20258 available.
20260 int declaration = (current_function_decl != decl
20261 || class_or_namespace_scope_p (context_die));
20263 premark_used_types (DECL_STRUCT_FUNCTION (decl));
20265 /* Now that the C++ front end lazily declares artificial member fns, we
20266 might need to retrofit the declaration into its class. */
20267 if (!declaration && !origin && !old_die
20268 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20269 && !class_or_namespace_scope_p (context_die)
20270 && debug_info_level > DINFO_LEVEL_TERSE)
20271 old_die = force_decl_die (decl);
20273 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20274 if (origin != NULL)
20276 gcc_assert (!declaration || local_scope_p (context_die));
20278 /* Fixup die_parent for the abstract instance of a nested
20279 inline function. */
20280 if (old_die && old_die->die_parent == NULL)
20281 add_child_die (context_die, old_die);
20283 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20285 /* If we have a DW_AT_abstract_origin we have a working
20286 cached version. */
20287 subr_die = old_die;
20289 else
20291 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20292 add_abstract_origin_attribute (subr_die, origin);
20293 /* This is where the actual code for a cloned function is.
20294 Let's emit linkage name attribute for it. This helps
20295 debuggers to e.g, set breakpoints into
20296 constructors/destructors when the user asks "break
20297 K::K". */
20298 add_linkage_name (subr_die, decl);
20301 /* A cached copy, possibly from early dwarf generation. Reuse as
20302 much as possible. */
20303 else if (old_die)
20305 /* A declaration that has been previously dumped needs no
20306 additional information. */
20307 if (declaration)
20308 return;
20310 if (!get_AT_flag (old_die, DW_AT_declaration)
20311 /* We can have a normal definition following an inline one in the
20312 case of redefinition of GNU C extern inlines.
20313 It seems reasonable to use AT_specification in this case. */
20314 && !get_AT (old_die, DW_AT_inline))
20316 /* Detect and ignore this case, where we are trying to output
20317 something we have already output. */
20318 if (get_AT (old_die, DW_AT_low_pc)
20319 || get_AT (old_die, DW_AT_ranges))
20320 return;
20322 /* If we have no location information, this must be a
20323 partially generated DIE from early dwarf generation.
20324 Fall through and generate it. */
20327 /* If the definition comes from the same place as the declaration,
20328 maybe use the old DIE. We always want the DIE for this function
20329 that has the *_pc attributes to be under comp_unit_die so the
20330 debugger can find it. We also need to do this for abstract
20331 instances of inlines, since the spec requires the out-of-line copy
20332 to have the same parent. For local class methods, this doesn't
20333 apply; we just use the old DIE. */
20334 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20335 struct dwarf_file_data * file_index = lookup_filename (s.file);
20336 if ((is_cu_die (old_die->die_parent)
20337 /* This condition fixes the inconsistency/ICE with the
20338 following Fortran test (or some derivative thereof) while
20339 building libgfortran:
20341 module some_m
20342 contains
20343 logical function funky (FLAG)
20344 funky = .true.
20345 end function
20346 end module
20348 || (old_die->die_parent
20349 && old_die->die_parent->die_tag == DW_TAG_module)
20350 || context_die == NULL)
20351 && (DECL_ARTIFICIAL (decl)
20352 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20353 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20354 == (unsigned) s.line))))
20356 subr_die = old_die;
20358 /* Clear out the declaration attribute, but leave the
20359 parameters so they can be augmented with location
20360 information later. Unless this was a declaration, in
20361 which case, wipe out the nameless parameters and recreate
20362 them further down. */
20363 if (remove_AT (subr_die, DW_AT_declaration))
20366 remove_AT (subr_die, DW_AT_object_pointer);
20367 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20370 /* Make a specification pointing to the previously built
20371 declaration. */
20372 else
20374 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20375 add_AT_specification (subr_die, old_die);
20376 add_pubname (decl, subr_die);
20377 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20378 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20379 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20380 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20382 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20383 emit the real type on the definition die. */
20384 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20386 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20387 if (die == auto_die || die == decltype_auto_die)
20388 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20389 TYPE_UNQUALIFIED, false, context_die);
20393 /* Create a fresh DIE for anything else. */
20394 else
20396 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20398 if (TREE_PUBLIC (decl))
20399 add_AT_flag (subr_die, DW_AT_external, 1);
20401 add_name_and_src_coords_attributes (subr_die, decl);
20402 add_pubname (decl, subr_die);
20403 if (debug_info_level > DINFO_LEVEL_TERSE)
20405 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20406 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20407 TYPE_UNQUALIFIED, false, context_die);
20410 add_pure_or_virtual_attribute (subr_die, decl);
20411 if (DECL_ARTIFICIAL (decl))
20412 add_AT_flag (subr_die, DW_AT_artificial, 1);
20414 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20415 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20417 add_accessibility_attribute (subr_die, decl);
20420 /* Unless we have an existing non-declaration DIE, equate the new
20421 DIE. */
20422 if (!old_die || is_declaration_die (old_die))
20423 equate_decl_number_to_die (decl, subr_die);
20425 if (declaration)
20427 if (!old_die || !get_AT (old_die, DW_AT_inline))
20429 add_AT_flag (subr_die, DW_AT_declaration, 1);
20431 /* If this is an explicit function declaration then generate
20432 a DW_AT_explicit attribute. */
20433 if (lang_hooks.decls.function_decl_explicit_p (decl)
20434 && (dwarf_version >= 3 || !dwarf_strict))
20435 add_AT_flag (subr_die, DW_AT_explicit, 1);
20437 /* If this is a C++11 deleted special function member then generate
20438 a DW_AT_GNU_deleted attribute. */
20439 if (lang_hooks.decls.function_decl_deleted_p (decl)
20440 && (! dwarf_strict))
20441 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
20444 /* Tag abstract instances with DW_AT_inline. */
20445 else if (DECL_ABSTRACT_P (decl))
20447 if (DECL_DECLARED_INLINE_P (decl))
20449 if (cgraph_function_possibly_inlined_p (decl))
20450 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20451 else
20452 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20454 else
20456 if (cgraph_function_possibly_inlined_p (decl))
20457 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20458 else
20459 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20462 if (DECL_DECLARED_INLINE_P (decl)
20463 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20464 add_AT_flag (subr_die, DW_AT_artificial, 1);
20466 /* For non DECL_EXTERNALs, if range information is available, fill
20467 the DIE with it. */
20468 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20470 HOST_WIDE_INT cfa_fb_offset;
20472 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20474 if (!flag_reorder_blocks_and_partition)
20476 dw_fde_ref fde = fun->fde;
20477 if (fde->dw_fde_begin)
20479 /* We have already generated the labels. */
20480 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20481 fde->dw_fde_end, false);
20483 else
20485 /* Create start/end labels and add the range. */
20486 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20487 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20488 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20489 current_function_funcdef_no);
20490 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20491 current_function_funcdef_no);
20492 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20493 false);
20496 #if VMS_DEBUGGING_INFO
20497 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20498 Section 2.3 Prologue and Epilogue Attributes:
20499 When a breakpoint is set on entry to a function, it is generally
20500 desirable for execution to be suspended, not on the very first
20501 instruction of the function, but rather at a point after the
20502 function's frame has been set up, after any language defined local
20503 declaration processing has been completed, and before execution of
20504 the first statement of the function begins. Debuggers generally
20505 cannot properly determine where this point is. Similarly for a
20506 breakpoint set on exit from a function. The prologue and epilogue
20507 attributes allow a compiler to communicate the location(s) to use. */
20510 if (fde->dw_fde_vms_end_prologue)
20511 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20512 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20514 if (fde->dw_fde_vms_begin_epilogue)
20515 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20516 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20518 #endif
20521 else
20523 /* Generate pubnames entries for the split function code ranges. */
20524 dw_fde_ref fde = fun->fde;
20526 if (fde->dw_fde_second_begin)
20528 if (dwarf_version >= 3 || !dwarf_strict)
20530 /* We should use ranges for non-contiguous code section
20531 addresses. Use the actual code range for the initial
20532 section, since the HOT/COLD labels might precede an
20533 alignment offset. */
20534 bool range_list_added = false;
20535 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20536 fde->dw_fde_end, &range_list_added,
20537 false);
20538 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20539 fde->dw_fde_second_end,
20540 &range_list_added, false);
20541 if (range_list_added)
20542 add_ranges (NULL);
20544 else
20546 /* There is no real support in DW2 for this .. so we make
20547 a work-around. First, emit the pub name for the segment
20548 containing the function label. Then make and emit a
20549 simplified subprogram DIE for the second segment with the
20550 name pre-fixed by __hot/cold_sect_of_. We use the same
20551 linkage name for the second die so that gdb will find both
20552 sections when given "b foo". */
20553 const char *name = NULL;
20554 tree decl_name = DECL_NAME (decl);
20555 dw_die_ref seg_die;
20557 /* Do the 'primary' section. */
20558 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20559 fde->dw_fde_end, false);
20561 /* Build a minimal DIE for the secondary section. */
20562 seg_die = new_die (DW_TAG_subprogram,
20563 subr_die->die_parent, decl);
20565 if (TREE_PUBLIC (decl))
20566 add_AT_flag (seg_die, DW_AT_external, 1);
20568 if (decl_name != NULL
20569 && IDENTIFIER_POINTER (decl_name) != NULL)
20571 name = dwarf2_name (decl, 1);
20572 if (! DECL_ARTIFICIAL (decl))
20573 add_src_coords_attributes (seg_die, decl);
20575 add_linkage_name (seg_die, decl);
20577 gcc_assert (name != NULL);
20578 add_pure_or_virtual_attribute (seg_die, decl);
20579 if (DECL_ARTIFICIAL (decl))
20580 add_AT_flag (seg_die, DW_AT_artificial, 1);
20582 name = concat ("__second_sect_of_", name, NULL);
20583 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20584 fde->dw_fde_second_end, false);
20585 add_name_attribute (seg_die, name);
20586 if (want_pubnames ())
20587 add_pubname_string (name, seg_die);
20590 else
20591 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20592 false);
20595 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20597 /* We define the "frame base" as the function's CFA. This is more
20598 convenient for several reasons: (1) It's stable across the prologue
20599 and epilogue, which makes it better than just a frame pointer,
20600 (2) With dwarf3, there exists a one-byte encoding that allows us
20601 to reference the .debug_frame data by proxy, but failing that,
20602 (3) We can at least reuse the code inspection and interpretation
20603 code that determines the CFA position at various points in the
20604 function. */
20605 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20607 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20608 add_AT_loc (subr_die, DW_AT_frame_base, op);
20610 else
20612 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20613 if (list->dw_loc_next)
20614 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20615 else
20616 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20619 /* Compute a displacement from the "steady-state frame pointer" to
20620 the CFA. The former is what all stack slots and argument slots
20621 will reference in the rtl; the latter is what we've told the
20622 debugger about. We'll need to adjust all frame_base references
20623 by this displacement. */
20624 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20626 if (fun->static_chain_decl)
20628 /* DWARF requires here a location expression that computes the
20629 address of the enclosing subprogram's frame base. The machinery
20630 in tree-nested.c is supposed to store this specific address in the
20631 last field of the FRAME record. */
20632 const tree frame_type
20633 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20634 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20636 tree fb_expr
20637 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20638 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20639 fb_expr, fb_decl, NULL_TREE);
20641 add_AT_location_description (subr_die, DW_AT_static_link,
20642 loc_list_from_tree (fb_expr, 0, NULL));
20646 /* Generate child dies for template paramaters. */
20647 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20648 gen_generic_params_dies (decl);
20650 /* Now output descriptions of the arguments for this function. This gets
20651 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20652 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20653 `...' at the end of the formal parameter list. In order to find out if
20654 there was a trailing ellipsis or not, we must instead look at the type
20655 associated with the FUNCTION_DECL. This will be a node of type
20656 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20657 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20658 an ellipsis at the end. */
20660 /* In the case where we are describing a mere function declaration, all we
20661 need to do here (and all we *can* do here) is to describe the *types* of
20662 its formal parameters. */
20663 if (debug_info_level <= DINFO_LEVEL_TERSE)
20665 else if (declaration)
20666 gen_formal_types_die (decl, subr_die);
20667 else
20669 /* Generate DIEs to represent all known formal parameters. */
20670 tree parm = DECL_ARGUMENTS (decl);
20671 tree generic_decl = early_dwarf
20672 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20673 tree generic_decl_parm = generic_decl
20674 ? DECL_ARGUMENTS (generic_decl)
20675 : NULL;
20677 /* Now we want to walk the list of parameters of the function and
20678 emit their relevant DIEs.
20680 We consider the case of DECL being an instance of a generic function
20681 as well as it being a normal function.
20683 If DECL is an instance of a generic function we walk the
20684 parameters of the generic function declaration _and_ the parameters of
20685 DECL itself. This is useful because we want to emit specific DIEs for
20686 function parameter packs and those are declared as part of the
20687 generic function declaration. In that particular case,
20688 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20689 That DIE has children DIEs representing the set of arguments
20690 of the pack. Note that the set of pack arguments can be empty.
20691 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20692 children DIE.
20694 Otherwise, we just consider the parameters of DECL. */
20695 while (generic_decl_parm || parm)
20697 if (generic_decl_parm
20698 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20699 gen_formal_parameter_pack_die (generic_decl_parm,
20700 parm, subr_die,
20701 &parm);
20702 else if (parm && !POINTER_BOUNDS_P (parm))
20704 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20706 if (parm == DECL_ARGUMENTS (decl)
20707 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20708 && parm_die
20709 && (dwarf_version >= 3 || !dwarf_strict))
20710 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20712 parm = DECL_CHAIN (parm);
20714 else if (parm)
20715 parm = DECL_CHAIN (parm);
20717 if (generic_decl_parm)
20718 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20721 /* Decide whether we need an unspecified_parameters DIE at the end.
20722 There are 2 more cases to do this for: 1) the ansi ... declaration -
20723 this is detectable when the end of the arg list is not a
20724 void_type_node 2) an unprototyped function declaration (not a
20725 definition). This just means that we have no info about the
20726 parameters at all. */
20727 if (prototype_p (TREE_TYPE (decl)))
20729 /* This is the prototyped case, check for.... */
20730 if (stdarg_p (TREE_TYPE (decl)))
20731 gen_unspecified_parameters_die (decl, subr_die);
20733 else if (DECL_INITIAL (decl) == NULL_TREE)
20734 gen_unspecified_parameters_die (decl, subr_die);
20737 if (subr_die != old_die)
20738 /* Add the calling convention attribute if requested. */
20739 add_calling_convention_attribute (subr_die, decl);
20741 /* Output Dwarf info for all of the stuff within the body of the function
20742 (if it has one - it may be just a declaration).
20744 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20745 a function. This BLOCK actually represents the outermost binding contour
20746 for the function, i.e. the contour in which the function's formal
20747 parameters and labels get declared. Curiously, it appears that the front
20748 end doesn't actually put the PARM_DECL nodes for the current function onto
20749 the BLOCK_VARS list for this outer scope, but are strung off of the
20750 DECL_ARGUMENTS list for the function instead.
20752 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20753 the LABEL_DECL nodes for the function however, and we output DWARF info
20754 for those in decls_for_scope. Just within the `outer_scope' there will be
20755 a BLOCK node representing the function's outermost pair of curly braces,
20756 and any blocks used for the base and member initializers of a C++
20757 constructor function. */
20758 tree outer_scope = DECL_INITIAL (decl);
20759 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
20761 int call_site_note_count = 0;
20762 int tail_call_site_note_count = 0;
20764 /* Emit a DW_TAG_variable DIE for a named return value. */
20765 if (DECL_NAME (DECL_RESULT (decl)))
20766 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
20768 /* The first time through decls_for_scope we will generate the
20769 DIEs for the locals. The second time, we fill in the
20770 location info. */
20771 decls_for_scope (outer_scope, subr_die);
20773 if (call_arg_locations && !dwarf_strict)
20775 struct call_arg_loc_node *ca_loc;
20776 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20778 dw_die_ref die = NULL;
20779 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20780 rtx arg, next_arg;
20782 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
20783 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
20784 : NULL_RTX);
20785 arg; arg = next_arg)
20787 dw_loc_descr_ref reg, val;
20788 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20789 dw_die_ref cdie, tdie = NULL;
20791 next_arg = XEXP (arg, 1);
20792 if (REG_P (XEXP (XEXP (arg, 0), 0))
20793 && next_arg
20794 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20795 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20796 && REGNO (XEXP (XEXP (arg, 0), 0))
20797 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20798 next_arg = XEXP (next_arg, 1);
20799 if (mode == VOIDmode)
20801 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20802 if (mode == VOIDmode)
20803 mode = GET_MODE (XEXP (arg, 0));
20805 if (mode == VOIDmode || mode == BLKmode)
20806 continue;
20807 /* Get dynamic information about call target only if we
20808 have no static information: we cannot generate both
20809 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
20810 attributes. */
20811 if (ca_loc->symbol_ref == NULL_RTX)
20813 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20815 tloc = XEXP (XEXP (arg, 0), 1);
20816 continue;
20818 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20819 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20821 tlocc = XEXP (XEXP (arg, 0), 1);
20822 continue;
20825 reg = NULL;
20826 if (REG_P (XEXP (XEXP (arg, 0), 0)))
20827 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20828 VAR_INIT_STATUS_INITIALIZED);
20829 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20831 rtx mem = XEXP (XEXP (arg, 0), 0);
20832 reg = mem_loc_descriptor (XEXP (mem, 0),
20833 get_address_mode (mem),
20834 GET_MODE (mem),
20835 VAR_INIT_STATUS_INITIALIZED);
20837 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20838 == DEBUG_PARAMETER_REF)
20840 tree tdecl
20841 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20842 tdie = lookup_decl_die (tdecl);
20843 if (tdie == NULL)
20844 continue;
20846 else
20847 continue;
20848 if (reg == NULL
20849 && GET_CODE (XEXP (XEXP (arg, 0), 0))
20850 != DEBUG_PARAMETER_REF)
20851 continue;
20852 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20853 VOIDmode,
20854 VAR_INIT_STATUS_INITIALIZED);
20855 if (val == NULL)
20856 continue;
20857 if (die == NULL)
20858 die = gen_call_site_die (decl, subr_die, ca_loc);
20859 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20860 NULL_TREE);
20861 if (reg != NULL)
20862 add_AT_loc (cdie, DW_AT_location, reg);
20863 else if (tdie != NULL)
20864 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20865 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20866 if (next_arg != XEXP (arg, 1))
20868 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20869 if (mode == VOIDmode)
20870 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20871 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20872 0), 1),
20873 mode, VOIDmode,
20874 VAR_INIT_STATUS_INITIALIZED);
20875 if (val != NULL)
20876 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20879 if (die == NULL
20880 && (ca_loc->symbol_ref || tloc))
20881 die = gen_call_site_die (decl, subr_die, ca_loc);
20882 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20884 dw_loc_descr_ref tval = NULL;
20886 if (tloc != NULL_RTX)
20887 tval = mem_loc_descriptor (tloc,
20888 GET_MODE (tloc) == VOIDmode
20889 ? Pmode : GET_MODE (tloc),
20890 VOIDmode,
20891 VAR_INIT_STATUS_INITIALIZED);
20892 if (tval)
20893 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20894 else if (tlocc != NULL_RTX)
20896 tval = mem_loc_descriptor (tlocc,
20897 GET_MODE (tlocc) == VOIDmode
20898 ? Pmode : GET_MODE (tlocc),
20899 VOIDmode,
20900 VAR_INIT_STATUS_INITIALIZED);
20901 if (tval)
20902 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20903 tval);
20906 if (die != NULL)
20908 call_site_note_count++;
20909 if (ca_loc->tail_call_p)
20910 tail_call_site_note_count++;
20914 call_arg_locations = NULL;
20915 call_arg_loc_last = NULL;
20916 if (tail_call_site_count >= 0
20917 && tail_call_site_count == tail_call_site_note_count
20918 && !dwarf_strict)
20920 if (call_site_count >= 0
20921 && call_site_count == call_site_note_count)
20922 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20923 else
20924 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20926 call_site_count = -1;
20927 tail_call_site_count = -1;
20931 /* Returns a hash value for X (which really is a die_struct). */
20933 hashval_t
20934 block_die_hasher::hash (die_struct *d)
20936 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20939 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20940 as decl_id and die_parent of die_struct Y. */
20942 bool
20943 block_die_hasher::equal (die_struct *x, die_struct *y)
20945 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
20948 /* Return TRUE if DECL, which may have been previously generated as
20949 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
20950 true if decl (or its origin) is either an extern declaration or a
20951 class/namespace scoped declaration.
20953 The declare_in_namespace support causes us to get two DIEs for one
20954 variable, both of which are declarations. We want to avoid
20955 considering one to be a specification, so we must test for
20956 DECLARATION and DW_AT_declaration. */
20957 static inline bool
20958 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
20960 return (old_die && TREE_STATIC (decl) && !declaration
20961 && get_AT_flag (old_die, DW_AT_declaration) == 1);
20964 /* Return true if DECL is a local static. */
20966 static inline bool
20967 local_function_static (tree decl)
20969 gcc_assert (TREE_CODE (decl) == VAR_DECL);
20970 return TREE_STATIC (decl)
20971 && DECL_CONTEXT (decl)
20972 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
20975 /* Generate a DIE to represent a declared data object.
20976 Either DECL or ORIGIN must be non-null. */
20978 static void
20979 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20981 HOST_WIDE_INT off = 0;
20982 tree com_decl;
20983 tree decl_or_origin = decl ? decl : origin;
20984 tree ultimate_origin;
20985 dw_die_ref var_die;
20986 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20987 dw_die_ref origin_die = NULL;
20988 bool declaration = (DECL_EXTERNAL (decl_or_origin)
20989 || class_or_namespace_scope_p (context_die));
20990 bool specialization_p = false;
20992 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20993 if (decl || ultimate_origin)
20994 origin = ultimate_origin;
20995 com_decl = fortran_common (decl_or_origin, &off);
20997 /* Symbol in common gets emitted as a child of the common block, in the form
20998 of a data member. */
20999 if (com_decl)
21001 dw_die_ref com_die;
21002 dw_loc_list_ref loc;
21003 die_node com_die_arg;
21005 var_die = lookup_decl_die (decl_or_origin);
21006 if (var_die)
21008 if (get_AT (var_die, DW_AT_location) == NULL)
21010 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21011 if (loc)
21013 if (off)
21015 /* Optimize the common case. */
21016 if (single_element_loc_list_p (loc)
21017 && loc->expr->dw_loc_opc == DW_OP_addr
21018 && loc->expr->dw_loc_next == NULL
21019 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21020 == SYMBOL_REF)
21022 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21023 loc->expr->dw_loc_oprnd1.v.val_addr
21024 = plus_constant (GET_MODE (x), x , off);
21026 else
21027 loc_list_plus_const (loc, off);
21029 add_AT_location_description (var_die, DW_AT_location, loc);
21030 remove_AT (var_die, DW_AT_declaration);
21033 return;
21036 if (common_block_die_table == NULL)
21037 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21039 com_die_arg.decl_id = DECL_UID (com_decl);
21040 com_die_arg.die_parent = context_die;
21041 com_die = common_block_die_table->find (&com_die_arg);
21042 loc = loc_list_from_tree (com_decl, 2, NULL);
21043 if (com_die == NULL)
21045 const char *cnam
21046 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21047 die_node **slot;
21049 com_die = new_die (DW_TAG_common_block, context_die, decl);
21050 add_name_and_src_coords_attributes (com_die, com_decl);
21051 if (loc)
21053 add_AT_location_description (com_die, DW_AT_location, loc);
21054 /* Avoid sharing the same loc descriptor between
21055 DW_TAG_common_block and DW_TAG_variable. */
21056 loc = loc_list_from_tree (com_decl, 2, NULL);
21058 else if (DECL_EXTERNAL (decl_or_origin))
21059 add_AT_flag (com_die, DW_AT_declaration, 1);
21060 if (want_pubnames ())
21061 add_pubname_string (cnam, com_die); /* ??? needed? */
21062 com_die->decl_id = DECL_UID (com_decl);
21063 slot = common_block_die_table->find_slot (com_die, INSERT);
21064 *slot = com_die;
21066 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21068 add_AT_location_description (com_die, DW_AT_location, loc);
21069 loc = loc_list_from_tree (com_decl, 2, NULL);
21070 remove_AT (com_die, DW_AT_declaration);
21072 var_die = new_die (DW_TAG_variable, com_die, decl);
21073 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21074 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21075 decl_quals (decl_or_origin), false,
21076 context_die);
21077 add_AT_flag (var_die, DW_AT_external, 1);
21078 if (loc)
21080 if (off)
21082 /* Optimize the common case. */
21083 if (single_element_loc_list_p (loc)
21084 && loc->expr->dw_loc_opc == DW_OP_addr
21085 && loc->expr->dw_loc_next == NULL
21086 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21088 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21089 loc->expr->dw_loc_oprnd1.v.val_addr
21090 = plus_constant (GET_MODE (x), x, off);
21092 else
21093 loc_list_plus_const (loc, off);
21095 add_AT_location_description (var_die, DW_AT_location, loc);
21097 else if (DECL_EXTERNAL (decl_or_origin))
21098 add_AT_flag (var_die, DW_AT_declaration, 1);
21099 if (decl)
21100 equate_decl_number_to_die (decl, var_die);
21101 return;
21104 if (old_die)
21106 if (declaration)
21108 /* A declaration that has been previously dumped, needs no
21109 further annotations, since it doesn't need location on
21110 the second pass. */
21111 return;
21113 else if (decl_will_get_specification_p (old_die, decl, declaration)
21114 && !get_AT (old_die, DW_AT_specification))
21116 /* Fall-thru so we can make a new variable die along with a
21117 DW_AT_specification. */
21119 else if (origin && old_die->die_parent != context_die)
21121 /* If we will be creating an inlined instance, we need a
21122 new DIE that will get annotated with
21123 DW_AT_abstract_origin. Clear things so we can get a
21124 new DIE. */
21125 gcc_assert (!DECL_ABSTRACT_P (decl));
21126 old_die = NULL;
21128 else
21130 /* If a DIE was dumped early, it still needs location info.
21131 Skip to where we fill the location bits. */
21132 var_die = old_die;
21133 goto gen_variable_die_location;
21137 /* For static data members, the declaration in the class is supposed
21138 to have DW_TAG_member tag; the specification should still be
21139 DW_TAG_variable referencing the DW_TAG_member DIE. */
21140 if (declaration && class_scope_p (context_die))
21141 var_die = new_die (DW_TAG_member, context_die, decl);
21142 else
21143 var_die = new_die (DW_TAG_variable, context_die, decl);
21145 if (origin != NULL)
21146 origin_die = add_abstract_origin_attribute (var_die, origin);
21148 /* Loop unrolling can create multiple blocks that refer to the same
21149 static variable, so we must test for the DW_AT_declaration flag.
21151 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21152 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21153 sharing them.
21155 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21156 else if (decl_will_get_specification_p (old_die, decl, declaration))
21158 /* This is a definition of a C++ class level static. */
21159 add_AT_specification (var_die, old_die);
21160 specialization_p = true;
21161 if (DECL_NAME (decl))
21163 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21164 struct dwarf_file_data * file_index = lookup_filename (s.file);
21166 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21167 add_AT_file (var_die, DW_AT_decl_file, file_index);
21169 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21170 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21172 if (old_die->die_tag == DW_TAG_member)
21173 add_linkage_name (var_die, decl);
21176 else
21177 add_name_and_src_coords_attributes (var_die, decl);
21179 if ((origin == NULL && !specialization_p)
21180 || (origin != NULL
21181 && !DECL_ABSTRACT_P (decl_or_origin)
21182 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21183 decl_function_context
21184 (decl_or_origin))))
21186 tree type = TREE_TYPE (decl_or_origin);
21188 if (decl_by_reference_p (decl_or_origin))
21189 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21190 context_die);
21191 else
21192 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21193 context_die);
21196 if (origin == NULL && !specialization_p)
21198 if (TREE_PUBLIC (decl))
21199 add_AT_flag (var_die, DW_AT_external, 1);
21201 if (DECL_ARTIFICIAL (decl))
21202 add_AT_flag (var_die, DW_AT_artificial, 1);
21204 add_accessibility_attribute (var_die, decl);
21207 if (declaration)
21208 add_AT_flag (var_die, DW_AT_declaration, 1);
21210 if (decl && (DECL_ABSTRACT_P (decl)
21211 || !old_die || is_declaration_die (old_die)))
21212 equate_decl_number_to_die (decl, var_die);
21214 gen_variable_die_location:
21215 if (! declaration
21216 && (! DECL_ABSTRACT_P (decl_or_origin)
21217 /* Local static vars are shared between all clones/inlines,
21218 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21219 already set. */
21220 || (TREE_CODE (decl_or_origin) == VAR_DECL
21221 && TREE_STATIC (decl_or_origin)
21222 && DECL_RTL_SET_P (decl_or_origin)))
21223 /* When abstract origin already has DW_AT_location attribute, no need
21224 to add it again. */
21225 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21227 if (early_dwarf)
21228 add_pubname (decl_or_origin, var_die);
21229 else
21230 add_location_or_const_value_attribute (var_die, decl_or_origin,
21231 decl == NULL);
21233 else
21234 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21237 /* Generate a DIE to represent a named constant. */
21239 static void
21240 gen_const_die (tree decl, dw_die_ref context_die)
21242 dw_die_ref const_die;
21243 tree type = TREE_TYPE (decl);
21245 const_die = lookup_decl_die (decl);
21246 if (const_die)
21247 return;
21249 const_die = new_die (DW_TAG_constant, context_die, decl);
21250 equate_decl_number_to_die (decl, const_die);
21251 add_name_and_src_coords_attributes (const_die, decl);
21252 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21253 if (TREE_PUBLIC (decl))
21254 add_AT_flag (const_die, DW_AT_external, 1);
21255 if (DECL_ARTIFICIAL (decl))
21256 add_AT_flag (const_die, DW_AT_artificial, 1);
21257 tree_add_const_value_attribute_for_decl (const_die, decl);
21260 /* Generate a DIE to represent a label identifier. */
21262 static void
21263 gen_label_die (tree decl, dw_die_ref context_die)
21265 tree origin = decl_ultimate_origin (decl);
21266 dw_die_ref lbl_die = lookup_decl_die (decl);
21267 rtx insn;
21268 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21270 if (!lbl_die)
21272 lbl_die = new_die (DW_TAG_label, context_die, decl);
21273 equate_decl_number_to_die (decl, lbl_die);
21275 if (origin != NULL)
21276 add_abstract_origin_attribute (lbl_die, origin);
21277 else
21278 add_name_and_src_coords_attributes (lbl_die, decl);
21281 if (DECL_ABSTRACT_P (decl))
21282 equate_decl_number_to_die (decl, lbl_die);
21283 else
21285 insn = DECL_RTL_IF_SET (decl);
21287 /* Deleted labels are programmer specified labels which have been
21288 eliminated because of various optimizations. We still emit them
21289 here so that it is possible to put breakpoints on them. */
21290 if (insn
21291 && (LABEL_P (insn)
21292 || ((NOTE_P (insn)
21293 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21295 /* When optimization is enabled (via -O) some parts of the compiler
21296 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21297 represent source-level labels which were explicitly declared by
21298 the user. This really shouldn't be happening though, so catch
21299 it if it ever does happen. */
21300 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21302 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21303 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21305 else if (insn
21306 && NOTE_P (insn)
21307 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21308 && CODE_LABEL_NUMBER (insn) != -1)
21310 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21311 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21316 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21317 attributes to the DIE for a block STMT, to describe where the inlined
21318 function was called from. This is similar to add_src_coords_attributes. */
21320 static inline void
21321 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21323 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21325 if (dwarf_version >= 3 || !dwarf_strict)
21327 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21328 add_AT_unsigned (die, DW_AT_call_line, s.line);
21333 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21334 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21336 static inline void
21337 add_high_low_attributes (tree stmt, dw_die_ref die)
21339 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21341 if (BLOCK_FRAGMENT_CHAIN (stmt)
21342 && (dwarf_version >= 3 || !dwarf_strict))
21344 tree chain, superblock = NULL_TREE;
21345 dw_die_ref pdie;
21346 dw_attr_node *attr = NULL;
21348 if (inlined_function_outer_scope_p (stmt))
21350 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21351 BLOCK_NUMBER (stmt));
21352 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21355 /* Optimize duplicate .debug_ranges lists or even tails of
21356 lists. If this BLOCK has same ranges as its supercontext,
21357 lookup DW_AT_ranges attribute in the supercontext (and
21358 recursively so), verify that the ranges_table contains the
21359 right values and use it instead of adding a new .debug_range. */
21360 for (chain = stmt, pdie = die;
21361 BLOCK_SAME_RANGE (chain);
21362 chain = BLOCK_SUPERCONTEXT (chain))
21364 dw_attr_node *new_attr;
21366 pdie = pdie->die_parent;
21367 if (pdie == NULL)
21368 break;
21369 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21370 break;
21371 new_attr = get_AT (pdie, DW_AT_ranges);
21372 if (new_attr == NULL
21373 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21374 break;
21375 attr = new_attr;
21376 superblock = BLOCK_SUPERCONTEXT (chain);
21378 if (attr != NULL
21379 && (ranges_table[attr->dw_attr_val.v.val_offset
21380 / 2 / DWARF2_ADDR_SIZE].num
21381 == BLOCK_NUMBER (superblock))
21382 && BLOCK_FRAGMENT_CHAIN (superblock))
21384 unsigned long off = attr->dw_attr_val.v.val_offset
21385 / 2 / DWARF2_ADDR_SIZE;
21386 unsigned long supercnt = 0, thiscnt = 0;
21387 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21388 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21390 ++supercnt;
21391 gcc_checking_assert (ranges_table[off + supercnt].num
21392 == BLOCK_NUMBER (chain));
21394 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21395 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21396 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21397 ++thiscnt;
21398 gcc_assert (supercnt >= thiscnt);
21399 add_AT_range_list (die, DW_AT_ranges,
21400 ((off + supercnt - thiscnt)
21401 * 2 * DWARF2_ADDR_SIZE),
21402 false);
21403 return;
21406 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21408 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21411 add_ranges (chain);
21412 chain = BLOCK_FRAGMENT_CHAIN (chain);
21414 while (chain);
21415 add_ranges (NULL);
21417 else
21419 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21420 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21421 BLOCK_NUMBER (stmt));
21422 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21423 BLOCK_NUMBER (stmt));
21424 add_AT_low_high_pc (die, label, label_high, false);
21428 /* Generate a DIE for a lexical block. */
21430 static void
21431 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21433 dw_die_ref old_die = BLOCK_DIE (stmt);
21434 dw_die_ref stmt_die = NULL;
21435 if (!old_die)
21437 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21438 BLOCK_DIE (stmt) = stmt_die;
21441 if (BLOCK_ABSTRACT (stmt))
21443 if (old_die)
21445 /* This must have been generated early and it won't even
21446 need location information since it's a DW_AT_inline
21447 function. */
21448 if (flag_checking)
21449 for (dw_die_ref c = context_die; c; c = c->die_parent)
21450 if (c->die_tag == DW_TAG_inlined_subroutine
21451 || c->die_tag == DW_TAG_subprogram)
21453 gcc_assert (get_AT (c, DW_AT_inline));
21454 break;
21456 return;
21459 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21461 /* If this is an inlined instance, create a new lexical die for
21462 anything below to attach DW_AT_abstract_origin to. */
21463 if (old_die)
21465 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21466 BLOCK_DIE (stmt) = stmt_die;
21467 old_die = NULL;
21471 if (old_die)
21472 stmt_die = old_die;
21474 /* A non abstract block whose blocks have already been reordered
21475 should have the instruction range for this block. If so, set the
21476 high/low attributes. */
21477 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21479 gcc_assert (stmt_die);
21480 add_high_low_attributes (stmt, stmt_die);
21483 decls_for_scope (stmt, stmt_die);
21486 /* Generate a DIE for an inlined subprogram. */
21488 static void
21489 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21491 tree decl;
21493 /* The instance of function that is effectively being inlined shall not
21494 be abstract. */
21495 gcc_assert (! BLOCK_ABSTRACT (stmt));
21497 decl = block_ultimate_origin (stmt);
21499 /* Make sure any inlined functions are known to be inlineable. */
21500 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21501 || cgraph_function_possibly_inlined_p (decl));
21503 /* Emit info for the abstract instance first, if we haven't yet. We
21504 must emit this even if the block is abstract, otherwise when we
21505 emit the block below (or elsewhere), we may end up trying to emit
21506 a die whose origin die hasn't been emitted, and crashing. */
21507 dwarf2out_abstract_function (decl);
21509 if (! BLOCK_ABSTRACT (stmt))
21511 dw_die_ref subr_die
21512 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21514 if (call_arg_locations)
21515 BLOCK_DIE (stmt) = subr_die;
21516 add_abstract_origin_attribute (subr_die, decl);
21517 if (TREE_ASM_WRITTEN (stmt))
21518 add_high_low_attributes (stmt, subr_die);
21519 add_call_src_coords_attributes (stmt, subr_die);
21521 decls_for_scope (stmt, subr_die);
21525 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21526 the comment for VLR_CONTEXT. */
21528 static void
21529 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21531 dw_die_ref decl_die;
21533 if (TREE_TYPE (decl) == error_mark_node)
21534 return;
21536 decl_die = new_die (DW_TAG_member, context_die, decl);
21537 add_name_and_src_coords_attributes (decl_die, decl);
21538 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21539 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21540 context_die);
21542 if (DECL_BIT_FIELD_TYPE (decl))
21544 add_byte_size_attribute (decl_die, decl);
21545 add_bit_size_attribute (decl_die, decl);
21546 add_bit_offset_attribute (decl_die, decl, ctx);
21549 /* If we have a variant part offset, then we are supposed to process a member
21550 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21551 trees. */
21552 gcc_assert (ctx->variant_part_offset == NULL_TREE
21553 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21554 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21555 add_data_member_location_attribute (decl_die, decl, ctx);
21557 if (DECL_ARTIFICIAL (decl))
21558 add_AT_flag (decl_die, DW_AT_artificial, 1);
21560 add_accessibility_attribute (decl_die, decl);
21562 /* Equate decl number to die, so that we can look up this decl later on. */
21563 equate_decl_number_to_die (decl, decl_die);
21566 #if 0
21567 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21568 Use modified_type_die instead.
21569 We keep this code here just in case these types of DIEs may be needed to
21570 represent certain things in other languages (e.g. Pascal) someday. */
21572 static void
21573 gen_pointer_type_die (tree type, dw_die_ref context_die)
21575 dw_die_ref ptr_die
21576 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21578 equate_type_number_to_die (type, ptr_die);
21579 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21580 context_die);
21581 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21584 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21585 Use modified_type_die instead.
21586 We keep this code here just in case these types of DIEs may be needed to
21587 represent certain things in other languages (e.g. Pascal) someday. */
21589 static void
21590 gen_reference_type_die (tree type, dw_die_ref context_die)
21592 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21594 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21595 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21596 else
21597 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21599 equate_type_number_to_die (type, ref_die);
21600 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21601 context_die);
21602 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21604 #endif
21606 /* Generate a DIE for a pointer to a member type. */
21608 static void
21609 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21611 dw_die_ref ptr_die
21612 = new_die (DW_TAG_ptr_to_member_type,
21613 scope_die_for (type, context_die), type);
21615 equate_type_number_to_die (type, ptr_die);
21616 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21617 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21618 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21619 context_die);
21622 static char *producer_string;
21624 /* Return a heap allocated producer string including command line options
21625 if -grecord-gcc-switches. */
21627 static char *
21628 gen_producer_string (void)
21630 size_t j;
21631 auto_vec<const char *> switches;
21632 const char *language_string = lang_hooks.name;
21633 char *producer, *tail;
21634 const char *p;
21635 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21636 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21638 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21639 switch (save_decoded_options[j].opt_index)
21641 case OPT_o:
21642 case OPT_d:
21643 case OPT_dumpbase:
21644 case OPT_dumpdir:
21645 case OPT_auxbase:
21646 case OPT_auxbase_strip:
21647 case OPT_quiet:
21648 case OPT_version:
21649 case OPT_v:
21650 case OPT_w:
21651 case OPT_L:
21652 case OPT_D:
21653 case OPT_I:
21654 case OPT_U:
21655 case OPT_SPECIAL_unknown:
21656 case OPT_SPECIAL_ignore:
21657 case OPT_SPECIAL_program_name:
21658 case OPT_SPECIAL_input_file:
21659 case OPT_grecord_gcc_switches:
21660 case OPT_gno_record_gcc_switches:
21661 case OPT__output_pch_:
21662 case OPT_fdiagnostics_show_location_:
21663 case OPT_fdiagnostics_show_option:
21664 case OPT_fdiagnostics_show_caret:
21665 case OPT_fdiagnostics_color_:
21666 case OPT_fverbose_asm:
21667 case OPT____:
21668 case OPT__sysroot_:
21669 case OPT_nostdinc:
21670 case OPT_nostdinc__:
21671 case OPT_fpreprocessed:
21672 case OPT_fltrans_output_list_:
21673 case OPT_fresolution_:
21674 case OPT_fdebug_prefix_map_:
21675 /* Ignore these. */
21676 continue;
21677 default:
21678 if (cl_options[save_decoded_options[j].opt_index].flags
21679 & CL_NO_DWARF_RECORD)
21680 continue;
21681 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21682 == '-');
21683 switch (save_decoded_options[j].canonical_option[0][1])
21685 case 'M':
21686 case 'i':
21687 case 'W':
21688 continue;
21689 case 'f':
21690 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21691 "dump", 4) == 0)
21692 continue;
21693 break;
21694 default:
21695 break;
21697 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21698 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21699 break;
21702 producer = XNEWVEC (char, plen + 1 + len + 1);
21703 tail = producer;
21704 sprintf (tail, "%s %s", language_string, version_string);
21705 tail += plen;
21707 FOR_EACH_VEC_ELT (switches, j, p)
21709 len = strlen (p);
21710 *tail = ' ';
21711 memcpy (tail + 1, p, len);
21712 tail += len + 1;
21715 *tail = '\0';
21716 return producer;
21719 /* Given a C and/or C++ language/version string return the "highest".
21720 C++ is assumed to be "higher" than C in this case. Used for merging
21721 LTO translation unit languages. */
21722 static const char *
21723 highest_c_language (const char *lang1, const char *lang2)
21725 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
21726 return "GNU C++14";
21727 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
21728 return "GNU C++11";
21729 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
21730 return "GNU C++98";
21732 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
21733 return "GNU C11";
21734 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
21735 return "GNU C99";
21736 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
21737 return "GNU C89";
21739 gcc_unreachable ();
21743 /* Generate the DIE for the compilation unit. */
21745 static dw_die_ref
21746 gen_compile_unit_die (const char *filename)
21748 dw_die_ref die;
21749 const char *language_string = lang_hooks.name;
21750 int language;
21752 die = new_die (DW_TAG_compile_unit, NULL, NULL);
21754 if (filename)
21756 add_name_attribute (die, filename);
21757 /* Don't add cwd for <built-in>. */
21758 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21759 add_comp_dir_attribute (die);
21762 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
21764 /* If our producer is LTO try to figure out a common language to use
21765 from the global list of translation units. */
21766 if (strcmp (language_string, "GNU GIMPLE") == 0)
21768 unsigned i;
21769 tree t;
21770 const char *common_lang = NULL;
21772 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
21774 if (!TRANSLATION_UNIT_LANGUAGE (t))
21775 continue;
21776 if (!common_lang)
21777 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21778 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21780 else if (strncmp (common_lang, "GNU C", 5) == 0
21781 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21782 /* Mixing C and C++ is ok, use C++ in that case. */
21783 common_lang = highest_c_language (common_lang,
21784 TRANSLATION_UNIT_LANGUAGE (t));
21785 else
21787 /* Fall back to C. */
21788 common_lang = NULL;
21789 break;
21793 if (common_lang)
21794 language_string = common_lang;
21797 language = DW_LANG_C;
21798 if (strncmp (language_string, "GNU C", 5) == 0
21799 && ISDIGIT (language_string[5]))
21801 language = DW_LANG_C89;
21802 if (dwarf_version >= 3 || !dwarf_strict)
21804 if (strcmp (language_string, "GNU C89") != 0)
21805 language = DW_LANG_C99;
21807 if (dwarf_version >= 5 /* || !dwarf_strict */)
21808 if (strcmp (language_string, "GNU C11") == 0)
21809 language = DW_LANG_C11;
21812 else if (strncmp (language_string, "GNU C++", 7) == 0)
21814 language = DW_LANG_C_plus_plus;
21815 if (dwarf_version >= 5 /* || !dwarf_strict */)
21817 if (strcmp (language_string, "GNU C++11") == 0)
21818 language = DW_LANG_C_plus_plus_11;
21819 else if (strcmp (language_string, "GNU C++14") == 0)
21820 language = DW_LANG_C_plus_plus_14;
21823 else if (strcmp (language_string, "GNU F77") == 0)
21824 language = DW_LANG_Fortran77;
21825 else if (strcmp (language_string, "GNU Pascal") == 0)
21826 language = DW_LANG_Pascal83;
21827 else if (dwarf_version >= 3 || !dwarf_strict)
21829 if (strcmp (language_string, "GNU Ada") == 0)
21830 language = DW_LANG_Ada95;
21831 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21833 language = DW_LANG_Fortran95;
21834 if (dwarf_version >= 5 /* || !dwarf_strict */)
21836 if (strcmp (language_string, "GNU Fortran2003") == 0)
21837 language = DW_LANG_Fortran03;
21838 else if (strcmp (language_string, "GNU Fortran2008") == 0)
21839 language = DW_LANG_Fortran08;
21842 else if (strcmp (language_string, "GNU Java") == 0)
21843 language = DW_LANG_Java;
21844 else if (strcmp (language_string, "GNU Objective-C") == 0)
21845 language = DW_LANG_ObjC;
21846 else if (strcmp (language_string, "GNU Objective-C++") == 0)
21847 language = DW_LANG_ObjC_plus_plus;
21848 else if (dwarf_version >= 5 || !dwarf_strict)
21850 if (strcmp (language_string, "GNU Go") == 0)
21851 language = DW_LANG_Go;
21854 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
21855 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
21856 language = DW_LANG_Fortran90;
21858 add_AT_unsigned (die, DW_AT_language, language);
21860 switch (language)
21862 case DW_LANG_Fortran77:
21863 case DW_LANG_Fortran90:
21864 case DW_LANG_Fortran95:
21865 case DW_LANG_Fortran03:
21866 case DW_LANG_Fortran08:
21867 /* Fortran has case insensitive identifiers and the front-end
21868 lowercases everything. */
21869 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21870 break;
21871 default:
21872 /* The default DW_ID_case_sensitive doesn't need to be specified. */
21873 break;
21875 return die;
21878 /* Generate the DIE for a base class. */
21880 static void
21881 gen_inheritance_die (tree binfo, tree access, tree type,
21882 dw_die_ref context_die)
21884 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21885 struct vlr_context ctx = { type, NULL };
21887 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
21888 context_die);
21889 add_data_member_location_attribute (die, binfo, &ctx);
21891 if (BINFO_VIRTUAL_P (binfo))
21892 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21894 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21895 children, otherwise the default is DW_ACCESS_public. In DWARF2
21896 the default has always been DW_ACCESS_private. */
21897 if (access == access_public_node)
21899 if (dwarf_version == 2
21900 || context_die->die_tag == DW_TAG_class_type)
21901 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21903 else if (access == access_protected_node)
21904 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21905 else if (dwarf_version > 2
21906 && context_die->die_tag != DW_TAG_class_type)
21907 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21910 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
21911 structure. */
21912 static bool
21913 is_variant_part (tree decl)
21915 return (TREE_CODE (decl) == FIELD_DECL
21916 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
21919 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
21920 return the FIELD_DECL. Return NULL_TREE otherwise. */
21922 static tree
21923 analyze_discr_in_predicate (tree operand, tree struct_type)
21925 bool continue_stripping = true;
21926 while (continue_stripping)
21927 switch (TREE_CODE (operand))
21929 CASE_CONVERT:
21930 operand = TREE_OPERAND (operand, 0);
21931 break;
21932 default:
21933 continue_stripping = false;
21934 break;
21937 /* Match field access to members of struct_type only. */
21938 if (TREE_CODE (operand) == COMPONENT_REF
21939 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
21940 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
21941 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
21942 return TREE_OPERAND (operand, 1);
21943 else
21944 return NULL_TREE;
21947 /* Check that SRC is a constant integer that can be represented as a native
21948 integer constant (either signed or unsigned). If so, store it into DEST and
21949 return true. Return false otherwise. */
21951 static bool
21952 get_discr_value (tree src, dw_discr_value *dest)
21954 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
21956 if (TREE_CODE (src) != INTEGER_CST
21957 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
21958 return false;
21960 dest->pos = is_unsigned;
21961 if (is_unsigned)
21962 dest->v.uval = tree_to_uhwi (src);
21963 else
21964 dest->v.sval = tree_to_shwi (src);
21966 return true;
21969 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
21970 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
21971 store NULL_TREE in DISCR_DECL. Otherwise:
21973 - store the discriminant field in STRUCT_TYPE that controls the variant
21974 part to *DISCR_DECL
21976 - put in *DISCR_LISTS_P an array where for each variant, the item
21977 represents the corresponding matching list of discriminant values.
21979 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
21980 the above array.
21982 Note that when the array is allocated (i.e. when the analysis is
21983 successful), it is up to the caller to free the array. */
21985 static void
21986 analyze_variants_discr (tree variant_part_decl,
21987 tree struct_type,
21988 tree *discr_decl,
21989 dw_discr_list_ref **discr_lists_p,
21990 unsigned *discr_lists_length)
21992 tree variant_part_type = TREE_TYPE (variant_part_decl);
21993 tree variant;
21994 dw_discr_list_ref *discr_lists;
21995 unsigned i;
21997 /* Compute how many variants there are in this variant part. */
21998 *discr_lists_length = 0;
21999 for (variant = TYPE_FIELDS (variant_part_type);
22000 variant != NULL_TREE;
22001 variant = DECL_CHAIN (variant))
22002 ++*discr_lists_length;
22004 *discr_decl = NULL_TREE;
22005 *discr_lists_p
22006 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22007 sizeof (**discr_lists_p));
22008 discr_lists = *discr_lists_p;
22010 /* And then analyze all variants to extract discriminant information for all
22011 of them. This analysis is conservative: as soon as we detect something we
22012 do not support, abort everything and pretend we found nothing. */
22013 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22014 variant != NULL_TREE;
22015 variant = DECL_CHAIN (variant), ++i)
22017 tree match_expr = DECL_QUALIFIER (variant);
22019 /* Now, try to analyze the predicate and deduce a discriminant for
22020 it. */
22021 if (match_expr == boolean_true_node)
22022 /* Typically happens for the default variant: it matches all cases that
22023 previous variants rejected. Don't output any matching value for
22024 this one. */
22025 continue;
22027 /* The following loop tries to iterate over each discriminant
22028 possibility: single values or ranges. */
22029 while (match_expr != NULL_TREE)
22031 tree next_round_match_expr;
22032 tree candidate_discr = NULL_TREE;
22033 dw_discr_list_ref new_node = NULL;
22035 /* Possibilities are matched one after the other by nested
22036 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22037 continue with the rest at next iteration. */
22038 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22040 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22041 match_expr = TREE_OPERAND (match_expr, 1);
22043 else
22044 next_round_match_expr = NULL_TREE;
22046 if (match_expr == boolean_false_node)
22047 /* This sub-expression matches nothing: just wait for the next
22048 one. */
22051 else if (TREE_CODE (match_expr) == EQ_EXPR)
22053 /* We are matching: <discr_field> == <integer_cst>
22054 This sub-expression matches a single value. */
22055 tree integer_cst = TREE_OPERAND (match_expr, 1);
22057 candidate_discr
22058 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22059 struct_type);
22061 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22062 if (!get_discr_value (integer_cst,
22063 &new_node->dw_discr_lower_bound))
22064 goto abort;
22065 new_node->dw_discr_range = false;
22068 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22070 /* We are matching:
22071 <discr_field> > <integer_cst>
22072 && <discr_field> < <integer_cst>.
22073 This sub-expression matches the range of values between the
22074 two matched integer constants. Note that comparisons can be
22075 inclusive or exclusive. */
22076 tree candidate_discr_1, candidate_discr_2;
22077 tree lower_cst, upper_cst;
22078 bool lower_cst_included, upper_cst_included;
22079 tree lower_op = TREE_OPERAND (match_expr, 0);
22080 tree upper_op = TREE_OPERAND (match_expr, 1);
22082 /* When the comparison is exclusive, the integer constant is not
22083 the discriminant range bound we are looking for: we will have
22084 to increment or decrement it. */
22085 if (TREE_CODE (lower_op) == GE_EXPR)
22086 lower_cst_included = true;
22087 else if (TREE_CODE (lower_op) == GT_EXPR)
22088 lower_cst_included = false;
22089 else
22090 goto abort;
22092 if (TREE_CODE (upper_op) == LE_EXPR)
22093 upper_cst_included = true;
22094 else if (TREE_CODE (upper_op) == LT_EXPR)
22095 upper_cst_included = false;
22096 else
22097 goto abort;
22099 /* Extract the discriminant from the first operand and check it
22100 is consistant with the same analysis in the second
22101 operand. */
22102 candidate_discr_1
22103 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22104 struct_type);
22105 candidate_discr_2
22106 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22107 struct_type);
22108 if (candidate_discr_1 == candidate_discr_2)
22109 candidate_discr = candidate_discr_1;
22110 else
22111 goto abort;
22113 /* Extract bounds from both. */
22114 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22115 lower_cst = TREE_OPERAND (lower_op, 1);
22116 upper_cst = TREE_OPERAND (upper_op, 1);
22118 if (!lower_cst_included)
22119 lower_cst
22120 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22121 lower_cst,
22122 build_int_cst (TREE_TYPE (lower_cst), 1)));
22123 if (!upper_cst_included)
22124 upper_cst
22125 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22126 upper_cst,
22127 build_int_cst (TREE_TYPE (upper_cst), 1)));
22129 if (!get_discr_value (lower_cst,
22130 &new_node->dw_discr_lower_bound)
22131 || !get_discr_value (upper_cst,
22132 &new_node->dw_discr_upper_bound))
22133 goto abort;
22135 new_node->dw_discr_range = true;
22138 else
22139 /* Unsupported sub-expression: we cannot determine the set of
22140 matching discriminant values. Abort everything. */
22141 goto abort;
22143 /* If the discriminant info is not consistant with what we saw so
22144 far, consider the analysis failed and abort everything. */
22145 if (candidate_discr == NULL_TREE
22146 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22147 goto abort;
22148 else
22149 *discr_decl = candidate_discr;
22151 if (new_node != NULL)
22153 new_node->dw_discr_next = discr_lists[i];
22154 discr_lists[i] = new_node;
22156 match_expr = next_round_match_expr;
22160 /* If we reach this point, we could match everything we were interested
22161 in. */
22162 return;
22164 abort:
22165 /* Clean all data structure and return no result. */
22166 free (*discr_lists_p);
22167 *discr_lists_p = NULL;
22168 *discr_decl = NULL_TREE;
22171 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22172 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22173 under CONTEXT_DIE.
22175 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22176 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22177 this type, which are record types, represent the available variants and each
22178 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22179 values are inferred from these attributes.
22181 In trees, the offsets for the fields inside these sub-records are relative
22182 to the variant part itself, whereas the corresponding DIEs should have
22183 offset attributes that are relative to the embedding record base address.
22184 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22185 must be an expression that computes the offset of the variant part to
22186 describe in DWARF. */
22188 static void
22189 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22190 dw_die_ref context_die)
22192 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22193 tree variant_part_offset = vlr_ctx->variant_part_offset;
22194 struct loc_descr_context ctx = {
22195 vlr_ctx->struct_type, /* context_type */
22196 NULL_TREE, /* base_decl */
22197 NULL /* dpi */
22200 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22201 NULL_TREE if there is no such field. */
22202 tree discr_decl = NULL_TREE;
22203 dw_discr_list_ref *discr_lists;
22204 unsigned discr_lists_length = 0;
22205 unsigned i;
22207 dw_die_ref dwarf_proc_die = NULL;
22208 dw_die_ref variant_part_die
22209 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22211 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22213 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22214 &discr_decl, &discr_lists, &discr_lists_length);
22216 if (discr_decl != NULL_TREE)
22218 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22220 if (discr_die)
22221 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22222 else
22223 /* We have no DIE for the discriminant, so just discard all
22224 discrimimant information in the output. */
22225 discr_decl = NULL_TREE;
22228 /* If the offset for this variant part is more complex than a constant,
22229 create a DWARF procedure for it so that we will not have to generate DWARF
22230 expressions for it for each member. */
22231 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22232 && (dwarf_version >= 3 || !dwarf_strict))
22234 const tree dwarf_proc_fndecl
22235 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22236 build_function_type (TREE_TYPE (variant_part_offset),
22237 NULL_TREE));
22238 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22239 const dw_loc_descr_ref dwarf_proc_body
22240 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22242 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22243 dwarf_proc_fndecl, context_die);
22244 if (dwarf_proc_die != NULL)
22245 variant_part_offset = dwarf_proc_call;
22248 /* Output DIEs for all variants. */
22249 i = 0;
22250 for (tree variant = TYPE_FIELDS (variant_part_type);
22251 variant != NULL_TREE;
22252 variant = DECL_CHAIN (variant), ++i)
22254 tree variant_type = TREE_TYPE (variant);
22255 dw_die_ref variant_die;
22257 /* All variants (i.e. members of a variant part) are supposed to be
22258 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22259 under these records. */
22260 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22262 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22263 equate_decl_number_to_die (variant, variant_die);
22265 /* Output discriminant values this variant matches, if any. */
22266 if (discr_decl == NULL || discr_lists[i] == NULL)
22267 /* In the case we have discriminant information at all, this is
22268 probably the default variant: as the standard says, don't
22269 output any discriminant value/list attribute. */
22271 else if (discr_lists[i]->dw_discr_next == NULL
22272 && !discr_lists[i]->dw_discr_range)
22273 /* If there is only one accepted value, don't bother outputting a
22274 list. */
22275 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22276 else
22277 add_discr_list (variant_die, discr_lists[i]);
22279 for (tree member = TYPE_FIELDS (variant_type);
22280 member != NULL_TREE;
22281 member = DECL_CHAIN (member))
22283 struct vlr_context vlr_sub_ctx = {
22284 vlr_ctx->struct_type, /* struct_type */
22285 NULL /* variant_part_offset */
22287 if (is_variant_part (member))
22289 /* All offsets for fields inside variant parts are relative to
22290 the top-level embedding RECORD_TYPE's base address. On the
22291 other hand, offsets in GCC's types are relative to the
22292 nested-most variant part. So we have to sum offsets each time
22293 we recurse. */
22295 vlr_sub_ctx.variant_part_offset
22296 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22297 variant_part_offset, byte_position (member)));
22298 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22300 else
22302 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22303 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22308 free (discr_lists);
22311 /* Generate a DIE for a class member. */
22313 static void
22314 gen_member_die (tree type, dw_die_ref context_die)
22316 tree member;
22317 tree binfo = TYPE_BINFO (type);
22318 dw_die_ref child;
22320 /* If this is not an incomplete type, output descriptions of each of its
22321 members. Note that as we output the DIEs necessary to represent the
22322 members of this record or union type, we will also be trying to output
22323 DIEs to represent the *types* of those members. However the `type'
22324 function (above) will specifically avoid generating type DIEs for member
22325 types *within* the list of member DIEs for this (containing) type except
22326 for those types (of members) which are explicitly marked as also being
22327 members of this (containing) type themselves. The g++ front- end can
22328 force any given type to be treated as a member of some other (containing)
22329 type by setting the TYPE_CONTEXT of the given (member) type to point to
22330 the TREE node representing the appropriate (containing) type. */
22332 /* First output info about the base classes. */
22333 if (binfo)
22335 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22336 int i;
22337 tree base;
22339 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22340 gen_inheritance_die (base,
22341 (accesses ? (*accesses)[i] : access_public_node),
22342 type,
22343 context_die);
22346 /* Now output info about the data members and type members. */
22347 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22349 struct vlr_context vlr_ctx = { type, NULL_TREE };
22351 /* If we thought we were generating minimal debug info for TYPE
22352 and then changed our minds, some of the member declarations
22353 may have already been defined. Don't define them again, but
22354 do put them in the right order. */
22356 child = lookup_decl_die (member);
22357 if (child)
22358 splice_child_die (context_die, child);
22360 /* Do not generate standard DWARF for variant parts if we are generating
22361 the corresponding GNAT encodings: DIEs generated for both would
22362 conflict in our mappings. */
22363 else if (is_variant_part (member)
22364 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22366 vlr_ctx.variant_part_offset = byte_position (member);
22367 gen_variant_part (member, &vlr_ctx, context_die);
22369 else
22371 vlr_ctx.variant_part_offset = NULL_TREE;
22372 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22376 /* We do not keep type methods in type variants. */
22377 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22378 /* Now output info about the function members (if any). */
22379 if (TYPE_METHODS (type) != error_mark_node)
22380 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22382 /* Don't include clones in the member list. */
22383 if (DECL_ABSTRACT_ORIGIN (member))
22384 continue;
22385 /* Nor constructors for anonymous classes. */
22386 if (DECL_ARTIFICIAL (member)
22387 && dwarf2_name (member, 0) == NULL)
22388 continue;
22390 child = lookup_decl_die (member);
22391 if (child)
22392 splice_child_die (context_die, child);
22393 else
22394 gen_decl_die (member, NULL, NULL, context_die);
22398 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22399 is set, we pretend that the type was never defined, so we only get the
22400 member DIEs needed by later specification DIEs. */
22402 static void
22403 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22404 enum debug_info_usage usage)
22406 if (TREE_ASM_WRITTEN (type))
22408 /* Fill in the bound of variable-length fields in late dwarf if
22409 still incomplete. */
22410 if (!early_dwarf && variably_modified_type_p (type, NULL))
22411 for (tree member = TYPE_FIELDS (type);
22412 member;
22413 member = DECL_CHAIN (member))
22414 fill_variable_array_bounds (TREE_TYPE (member));
22415 return;
22418 dw_die_ref type_die = lookup_type_die (type);
22419 dw_die_ref scope_die = 0;
22420 int nested = 0;
22421 int complete = (TYPE_SIZE (type)
22422 && (! TYPE_STUB_DECL (type)
22423 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22424 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22425 complete = complete && should_emit_struct_debug (type, usage);
22427 if (type_die && ! complete)
22428 return;
22430 if (TYPE_CONTEXT (type) != NULL_TREE
22431 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22432 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22433 nested = 1;
22435 scope_die = scope_die_for (type, context_die);
22437 /* Generate child dies for template paramaters. */
22438 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22439 schedule_generic_params_dies_gen (type);
22441 if (! type_die || (nested && is_cu_die (scope_die)))
22442 /* First occurrence of type or toplevel definition of nested class. */
22444 dw_die_ref old_die = type_die;
22446 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22447 ? record_type_tag (type) : DW_TAG_union_type,
22448 scope_die, type);
22449 equate_type_number_to_die (type, type_die);
22450 if (old_die)
22451 add_AT_specification (type_die, old_die);
22452 else
22453 add_name_attribute (type_die, type_tag (type));
22455 else
22456 remove_AT (type_die, DW_AT_declaration);
22458 /* If this type has been completed, then give it a byte_size attribute and
22459 then give a list of members. */
22460 if (complete && !ns_decl)
22462 /* Prevent infinite recursion in cases where the type of some member of
22463 this type is expressed in terms of this type itself. */
22464 TREE_ASM_WRITTEN (type) = 1;
22465 add_byte_size_attribute (type_die, type);
22466 if (TYPE_STUB_DECL (type) != NULL_TREE)
22468 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22469 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22472 /* If the first reference to this type was as the return type of an
22473 inline function, then it may not have a parent. Fix this now. */
22474 if (type_die->die_parent == NULL)
22475 add_child_die (scope_die, type_die);
22477 push_decl_scope (type);
22478 gen_member_die (type, type_die);
22479 pop_decl_scope ();
22481 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22482 if (TYPE_ARTIFICIAL (type))
22483 add_AT_flag (type_die, DW_AT_artificial, 1);
22485 /* GNU extension: Record what type our vtable lives in. */
22486 if (TYPE_VFIELD (type))
22488 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22490 gen_type_die (vtype, context_die);
22491 add_AT_die_ref (type_die, DW_AT_containing_type,
22492 lookup_type_die (vtype));
22495 else
22497 add_AT_flag (type_die, DW_AT_declaration, 1);
22499 /* We don't need to do this for function-local types. */
22500 if (TYPE_STUB_DECL (type)
22501 && ! decl_function_context (TYPE_STUB_DECL (type)))
22502 vec_safe_push (incomplete_types, type);
22505 if (get_AT (type_die, DW_AT_name))
22506 add_pubtype (type, type_die);
22509 /* Generate a DIE for a subroutine _type_. */
22511 static void
22512 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22514 tree return_type = TREE_TYPE (type);
22515 dw_die_ref subr_die
22516 = new_die (DW_TAG_subroutine_type,
22517 scope_die_for (type, context_die), type);
22519 equate_type_number_to_die (type, subr_die);
22520 add_prototyped_attribute (subr_die, type);
22521 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22522 context_die);
22523 gen_formal_types_die (type, subr_die);
22525 if (get_AT (subr_die, DW_AT_name))
22526 add_pubtype (type, subr_die);
22529 /* Generate a DIE for a type definition. */
22531 static void
22532 gen_typedef_die (tree decl, dw_die_ref context_die)
22534 dw_die_ref type_die;
22535 tree origin;
22537 if (TREE_ASM_WRITTEN (decl))
22539 if (DECL_ORIGINAL_TYPE (decl))
22540 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22541 return;
22544 TREE_ASM_WRITTEN (decl) = 1;
22545 type_die = new_die (DW_TAG_typedef, context_die, decl);
22546 origin = decl_ultimate_origin (decl);
22547 if (origin != NULL)
22548 add_abstract_origin_attribute (type_die, origin);
22549 else
22551 tree type;
22553 add_name_and_src_coords_attributes (type_die, decl);
22554 if (DECL_ORIGINAL_TYPE (decl))
22556 type = DECL_ORIGINAL_TYPE (decl);
22558 if (type == error_mark_node)
22559 return;
22561 gcc_assert (type != TREE_TYPE (decl));
22562 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22564 else
22566 type = TREE_TYPE (decl);
22568 if (type == error_mark_node)
22569 return;
22571 if (is_naming_typedef_decl (TYPE_NAME (type)))
22573 /* Here, we are in the case of decl being a typedef naming
22574 an anonymous type, e.g:
22575 typedef struct {...} foo;
22576 In that case TREE_TYPE (decl) is not a typedef variant
22577 type and TYPE_NAME of the anonymous type is set to the
22578 TYPE_DECL of the typedef. This construct is emitted by
22579 the C++ FE.
22581 TYPE is the anonymous struct named by the typedef
22582 DECL. As we need the DW_AT_type attribute of the
22583 DW_TAG_typedef to point to the DIE of TYPE, let's
22584 generate that DIE right away. add_type_attribute
22585 called below will then pick (via lookup_type_die) that
22586 anonymous struct DIE. */
22587 if (!TREE_ASM_WRITTEN (type))
22588 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22590 /* This is a GNU Extension. We are adding a
22591 DW_AT_linkage_name attribute to the DIE of the
22592 anonymous struct TYPE. The value of that attribute
22593 is the name of the typedef decl naming the anonymous
22594 struct. This greatly eases the work of consumers of
22595 this debug info. */
22596 add_linkage_name_raw (lookup_type_die (type), decl);
22600 add_type_attribute (type_die, type, decl_quals (decl), false,
22601 context_die);
22603 if (is_naming_typedef_decl (decl))
22604 /* We want that all subsequent calls to lookup_type_die with
22605 TYPE in argument yield the DW_TAG_typedef we have just
22606 created. */
22607 equate_type_number_to_die (type, type_die);
22609 add_accessibility_attribute (type_die, decl);
22612 if (DECL_ABSTRACT_P (decl))
22613 equate_decl_number_to_die (decl, type_die);
22615 if (get_AT (type_die, DW_AT_name))
22616 add_pubtype (decl, type_die);
22619 /* Generate a DIE for a struct, class, enum or union type. */
22621 static void
22622 gen_tagged_type_die (tree type,
22623 dw_die_ref context_die,
22624 enum debug_info_usage usage)
22626 int need_pop;
22628 if (type == NULL_TREE
22629 || !is_tagged_type (type))
22630 return;
22632 if (TREE_ASM_WRITTEN (type))
22633 need_pop = 0;
22634 /* If this is a nested type whose containing class hasn't been written
22635 out yet, writing it out will cover this one, too. This does not apply
22636 to instantiations of member class templates; they need to be added to
22637 the containing class as they are generated. FIXME: This hurts the
22638 idea of combining type decls from multiple TUs, since we can't predict
22639 what set of template instantiations we'll get. */
22640 else if (TYPE_CONTEXT (type)
22641 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22642 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22644 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22646 if (TREE_ASM_WRITTEN (type))
22647 return;
22649 /* If that failed, attach ourselves to the stub. */
22650 push_decl_scope (TYPE_CONTEXT (type));
22651 context_die = lookup_type_die (TYPE_CONTEXT (type));
22652 need_pop = 1;
22654 else if (TYPE_CONTEXT (type) != NULL_TREE
22655 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22657 /* If this type is local to a function that hasn't been written
22658 out yet, use a NULL context for now; it will be fixed up in
22659 decls_for_scope. */
22660 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22661 /* A declaration DIE doesn't count; nested types need to go in the
22662 specification. */
22663 if (context_die && is_declaration_die (context_die))
22664 context_die = NULL;
22665 need_pop = 0;
22667 else
22669 context_die = declare_in_namespace (type, context_die);
22670 need_pop = 0;
22673 if (TREE_CODE (type) == ENUMERAL_TYPE)
22675 /* This might have been written out by the call to
22676 declare_in_namespace. */
22677 if (!TREE_ASM_WRITTEN (type))
22678 gen_enumeration_type_die (type, context_die);
22680 else
22681 gen_struct_or_union_type_die (type, context_die, usage);
22683 if (need_pop)
22684 pop_decl_scope ();
22686 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22687 it up if it is ever completed. gen_*_type_die will set it for us
22688 when appropriate. */
22691 /* Generate a type description DIE. */
22693 static void
22694 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22695 enum debug_info_usage usage)
22697 struct array_descr_info info;
22699 if (type == NULL_TREE || type == error_mark_node)
22700 return;
22702 if (flag_checking && type)
22703 verify_type (type);
22705 if (TYPE_NAME (type) != NULL_TREE
22706 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
22707 && is_redundant_typedef (TYPE_NAME (type))
22708 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
22709 /* The DECL of this type is a typedef we don't want to emit debug
22710 info for but we want debug info for its underlying typedef.
22711 This can happen for e.g, the injected-class-name of a C++
22712 type. */
22713 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
22715 /* If TYPE is a typedef type variant, let's generate debug info
22716 for the parent typedef which TYPE is a type of. */
22717 if (typedef_variant_p (type))
22719 if (TREE_ASM_WRITTEN (type))
22720 return;
22722 /* Prevent broken recursion; we can't hand off to the same type. */
22723 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
22725 /* Give typedefs the right scope. */
22726 context_die = scope_die_for (type, context_die);
22728 TREE_ASM_WRITTEN (type) = 1;
22730 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22731 return;
22734 /* If type is an anonymous tagged type named by a typedef, let's
22735 generate debug info for the typedef. */
22736 if (is_naming_typedef_decl (TYPE_NAME (type)))
22738 /* Use the DIE of the containing namespace as the parent DIE of
22739 the type description DIE we want to generate. */
22740 if (DECL_CONTEXT (TYPE_NAME (type))
22741 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
22742 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
22744 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
22745 return;
22748 /* We are going to output a DIE to represent the unqualified version
22749 of this type (i.e. without any const or volatile qualifiers) so
22750 get the main variant (i.e. the unqualified version) of this type
22751 now. (Vectors and arrays are special because the debugging info is in the
22752 cloned type itself). */
22753 if (TREE_CODE (type) != VECTOR_TYPE
22754 && TREE_CODE (type) != ARRAY_TYPE)
22755 type = type_main_variant (type);
22757 /* If this is an array type with hidden descriptor, handle it first. */
22758 if (!TREE_ASM_WRITTEN (type)
22759 && lang_hooks.types.get_array_descr_info)
22761 memset (&info, 0, sizeof (info));
22762 if (lang_hooks.types.get_array_descr_info (type, &info))
22764 /* Fortran sometimes emits array types with no dimension. */
22765 gcc_assert (info.ndimensions >= 0
22766 && (info.ndimensions
22767 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
22768 gen_descr_array_type_die (type, &info, context_die);
22769 TREE_ASM_WRITTEN (type) = 1;
22770 return;
22774 if (TREE_ASM_WRITTEN (type))
22776 /* Variable-length types may be incomplete even if
22777 TREE_ASM_WRITTEN. For such types, fall through to
22778 gen_array_type_die() and possibly fill in
22779 DW_AT_{upper,lower}_bound attributes. */
22780 if ((TREE_CODE (type) != ARRAY_TYPE
22781 && TREE_CODE (type) != RECORD_TYPE
22782 && TREE_CODE (type) != UNION_TYPE
22783 && TREE_CODE (type) != QUAL_UNION_TYPE)
22784 || !variably_modified_type_p (type, NULL))
22785 return;
22788 switch (TREE_CODE (type))
22790 case ERROR_MARK:
22791 break;
22793 case POINTER_TYPE:
22794 case REFERENCE_TYPE:
22795 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
22796 ensures that the gen_type_die recursion will terminate even if the
22797 type is recursive. Recursive types are possible in Ada. */
22798 /* ??? We could perhaps do this for all types before the switch
22799 statement. */
22800 TREE_ASM_WRITTEN (type) = 1;
22802 /* For these types, all that is required is that we output a DIE (or a
22803 set of DIEs) to represent the "basis" type. */
22804 gen_type_die_with_usage (TREE_TYPE (type), context_die,
22805 DINFO_USAGE_IND_USE);
22806 break;
22808 case OFFSET_TYPE:
22809 /* This code is used for C++ pointer-to-data-member types.
22810 Output a description of the relevant class type. */
22811 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
22812 DINFO_USAGE_IND_USE);
22814 /* Output a description of the type of the object pointed to. */
22815 gen_type_die_with_usage (TREE_TYPE (type), context_die,
22816 DINFO_USAGE_IND_USE);
22818 /* Now output a DIE to represent this pointer-to-data-member type
22819 itself. */
22820 gen_ptr_to_mbr_type_die (type, context_die);
22821 break;
22823 case FUNCTION_TYPE:
22824 /* Force out return type (in case it wasn't forced out already). */
22825 gen_type_die_with_usage (TREE_TYPE (type), context_die,
22826 DINFO_USAGE_DIR_USE);
22827 gen_subroutine_type_die (type, context_die);
22828 break;
22830 case METHOD_TYPE:
22831 /* Force out return type (in case it wasn't forced out already). */
22832 gen_type_die_with_usage (TREE_TYPE (type), context_die,
22833 DINFO_USAGE_DIR_USE);
22834 gen_subroutine_type_die (type, context_die);
22835 break;
22837 case ARRAY_TYPE:
22838 case VECTOR_TYPE:
22839 gen_array_type_die (type, context_die);
22840 break;
22842 case ENUMERAL_TYPE:
22843 case RECORD_TYPE:
22844 case UNION_TYPE:
22845 case QUAL_UNION_TYPE:
22846 gen_tagged_type_die (type, context_die, usage);
22847 return;
22849 case VOID_TYPE:
22850 case INTEGER_TYPE:
22851 case REAL_TYPE:
22852 case FIXED_POINT_TYPE:
22853 case COMPLEX_TYPE:
22854 case BOOLEAN_TYPE:
22855 case POINTER_BOUNDS_TYPE:
22856 /* No DIEs needed for fundamental types. */
22857 break;
22859 case NULLPTR_TYPE:
22860 case LANG_TYPE:
22861 /* Just use DW_TAG_unspecified_type. */
22863 dw_die_ref type_die = lookup_type_die (type);
22864 if (type_die == NULL)
22866 tree name = TYPE_IDENTIFIER (type);
22867 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
22868 type);
22869 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
22870 equate_type_number_to_die (type, type_die);
22873 break;
22875 default:
22876 if (is_cxx_auto (type))
22878 tree name = TYPE_IDENTIFIER (type);
22879 dw_die_ref *die = (name == get_identifier ("auto")
22880 ? &auto_die : &decltype_auto_die);
22881 if (!*die)
22883 *die = new_die (DW_TAG_unspecified_type,
22884 comp_unit_die (), NULL_TREE);
22885 add_name_attribute (*die, IDENTIFIER_POINTER (name));
22887 equate_type_number_to_die (type, *die);
22888 break;
22890 gcc_unreachable ();
22893 TREE_ASM_WRITTEN (type) = 1;
22896 static void
22897 gen_type_die (tree type, dw_die_ref context_die)
22899 if (type != error_mark_node)
22901 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
22902 if (flag_checking)
22904 dw_die_ref die = lookup_type_die (type);
22905 if (die)
22906 check_die (die);
22911 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
22912 things which are local to the given block. */
22914 static void
22915 gen_block_die (tree stmt, dw_die_ref context_die)
22917 int must_output_die = 0;
22918 bool inlined_func;
22920 /* Ignore blocks that are NULL. */
22921 if (stmt == NULL_TREE)
22922 return;
22924 inlined_func = inlined_function_outer_scope_p (stmt);
22926 /* If the block is one fragment of a non-contiguous block, do not
22927 process the variables, since they will have been done by the
22928 origin block. Do process subblocks. */
22929 if (BLOCK_FRAGMENT_ORIGIN (stmt))
22931 tree sub;
22933 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
22934 gen_block_die (sub, context_die);
22936 return;
22939 /* Determine if we need to output any Dwarf DIEs at all to represent this
22940 block. */
22941 if (inlined_func)
22942 /* The outer scopes for inlinings *must* always be represented. We
22943 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
22944 must_output_die = 1;
22945 else
22947 /* Determine if this block directly contains any "significant"
22948 local declarations which we will need to output DIEs for. */
22949 if (debug_info_level > DINFO_LEVEL_TERSE)
22950 /* We are not in terse mode so *any* local declaration counts
22951 as being a "significant" one. */
22952 must_output_die = ((BLOCK_VARS (stmt) != NULL
22953 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
22954 && (TREE_USED (stmt)
22955 || TREE_ASM_WRITTEN (stmt)
22956 || BLOCK_ABSTRACT (stmt)));
22957 else if ((TREE_USED (stmt)
22958 || TREE_ASM_WRITTEN (stmt)
22959 || BLOCK_ABSTRACT (stmt))
22960 && !dwarf2out_ignore_block (stmt))
22961 must_output_die = 1;
22964 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
22965 DIE for any block which contains no significant local declarations at
22966 all. Rather, in such cases we just call `decls_for_scope' so that any
22967 needed Dwarf info for any sub-blocks will get properly generated. Note
22968 that in terse mode, our definition of what constitutes a "significant"
22969 local declaration gets restricted to include only inlined function
22970 instances and local (nested) function definitions. */
22971 if (must_output_die)
22973 if (inlined_func)
22975 /* If STMT block is abstract, that means we have been called
22976 indirectly from dwarf2out_abstract_function.
22977 That function rightfully marks the descendent blocks (of
22978 the abstract function it is dealing with) as being abstract,
22979 precisely to prevent us from emitting any
22980 DW_TAG_inlined_subroutine DIE as a descendent
22981 of an abstract function instance. So in that case, we should
22982 not call gen_inlined_subroutine_die.
22984 Later though, when cgraph asks dwarf2out to emit info
22985 for the concrete instance of the function decl into which
22986 the concrete instance of STMT got inlined, the later will lead
22987 to the generation of a DW_TAG_inlined_subroutine DIE. */
22988 if (! BLOCK_ABSTRACT (stmt))
22989 gen_inlined_subroutine_die (stmt, context_die);
22991 else
22992 gen_lexical_block_die (stmt, context_die);
22994 else
22995 decls_for_scope (stmt, context_die);
22998 /* Process variable DECL (or variable with origin ORIGIN) within
22999 block STMT and add it to CONTEXT_DIE. */
23000 static void
23001 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23003 dw_die_ref die;
23004 tree decl_or_origin = decl ? decl : origin;
23006 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23007 die = lookup_decl_die (decl_or_origin);
23008 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
23009 && TYPE_DECL_IS_STUB (decl_or_origin))
23010 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23011 else
23012 die = NULL;
23014 if (die != NULL && die->die_parent == NULL)
23015 add_child_die (context_die, die);
23016 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23018 if (early_dwarf)
23019 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23020 stmt, context_die);
23022 else
23023 gen_decl_die (decl, origin, NULL, context_die);
23026 /* Generate all of the decls declared within a given scope and (recursively)
23027 all of its sub-blocks. */
23029 static void
23030 decls_for_scope (tree stmt, dw_die_ref context_die)
23032 tree decl;
23033 unsigned int i;
23034 tree subblocks;
23036 /* Ignore NULL blocks. */
23037 if (stmt == NULL_TREE)
23038 return;
23040 /* Output the DIEs to represent all of the data objects and typedefs
23041 declared directly within this block but not within any nested
23042 sub-blocks. Also, nested function and tag DIEs have been
23043 generated with a parent of NULL; fix that up now. We don't
23044 have to do this if we're at -g1. */
23045 if (debug_info_level > DINFO_LEVEL_TERSE)
23047 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23048 process_scope_var (stmt, decl, NULL_TREE, context_die);
23049 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23050 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23051 context_die);
23054 /* Even if we're at -g1, we need to process the subblocks in order to get
23055 inlined call information. */
23057 /* Output the DIEs to represent all sub-blocks (and the items declared
23058 therein) of this block. */
23059 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23060 subblocks != NULL;
23061 subblocks = BLOCK_CHAIN (subblocks))
23062 gen_block_die (subblocks, context_die);
23065 /* Is this a typedef we can avoid emitting? */
23067 bool
23068 is_redundant_typedef (const_tree decl)
23070 if (TYPE_DECL_IS_STUB (decl))
23071 return true;
23073 if (DECL_ARTIFICIAL (decl)
23074 && DECL_CONTEXT (decl)
23075 && is_tagged_type (DECL_CONTEXT (decl))
23076 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23077 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23078 /* Also ignore the artificial member typedef for the class name. */
23079 return true;
23081 return false;
23084 /* Return TRUE if TYPE is a typedef that names a type for linkage
23085 purposes. This kind of typedefs is produced by the C++ FE for
23086 constructs like:
23088 typedef struct {...} foo;
23090 In that case, there is no typedef variant type produced for foo.
23091 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23092 struct type. */
23094 static bool
23095 is_naming_typedef_decl (const_tree decl)
23097 if (decl == NULL_TREE
23098 || TREE_CODE (decl) != TYPE_DECL
23099 || DECL_NAMELESS (decl)
23100 || !is_tagged_type (TREE_TYPE (decl))
23101 || DECL_IS_BUILTIN (decl)
23102 || is_redundant_typedef (decl)
23103 /* It looks like Ada produces TYPE_DECLs that are very similar
23104 to C++ naming typedefs but that have different
23105 semantics. Let's be specific to c++ for now. */
23106 || !is_cxx ())
23107 return FALSE;
23109 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23110 && TYPE_NAME (TREE_TYPE (decl)) == decl
23111 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23112 != TYPE_NAME (TREE_TYPE (decl))));
23115 /* Looks up the DIE for a context. */
23117 static inline dw_die_ref
23118 lookup_context_die (tree context)
23120 if (context)
23122 /* Find die that represents this context. */
23123 if (TYPE_P (context))
23125 context = TYPE_MAIN_VARIANT (context);
23126 dw_die_ref ctx = lookup_type_die (context);
23127 if (!ctx)
23128 return NULL;
23129 return strip_naming_typedef (context, ctx);
23131 else
23132 return lookup_decl_die (context);
23134 return comp_unit_die ();
23137 /* Returns the DIE for a context. */
23139 static inline dw_die_ref
23140 get_context_die (tree context)
23142 if (context)
23144 /* Find die that represents this context. */
23145 if (TYPE_P (context))
23147 context = TYPE_MAIN_VARIANT (context);
23148 return strip_naming_typedef (context, force_type_die (context));
23150 else
23151 return force_decl_die (context);
23153 return comp_unit_die ();
23156 /* Returns the DIE for decl. A DIE will always be returned. */
23158 static dw_die_ref
23159 force_decl_die (tree decl)
23161 dw_die_ref decl_die;
23162 unsigned saved_external_flag;
23163 tree save_fn = NULL_TREE;
23164 decl_die = lookup_decl_die (decl);
23165 if (!decl_die)
23167 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23169 decl_die = lookup_decl_die (decl);
23170 if (decl_die)
23171 return decl_die;
23173 switch (TREE_CODE (decl))
23175 case FUNCTION_DECL:
23176 /* Clear current_function_decl, so that gen_subprogram_die thinks
23177 that this is a declaration. At this point, we just want to force
23178 declaration die. */
23179 save_fn = current_function_decl;
23180 current_function_decl = NULL_TREE;
23181 gen_subprogram_die (decl, context_die);
23182 current_function_decl = save_fn;
23183 break;
23185 case VAR_DECL:
23186 /* Set external flag to force declaration die. Restore it after
23187 gen_decl_die() call. */
23188 saved_external_flag = DECL_EXTERNAL (decl);
23189 DECL_EXTERNAL (decl) = 1;
23190 gen_decl_die (decl, NULL, NULL, context_die);
23191 DECL_EXTERNAL (decl) = saved_external_flag;
23192 break;
23194 case NAMESPACE_DECL:
23195 if (dwarf_version >= 3 || !dwarf_strict)
23196 dwarf2out_decl (decl);
23197 else
23198 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23199 decl_die = comp_unit_die ();
23200 break;
23202 case TRANSLATION_UNIT_DECL:
23203 decl_die = comp_unit_die ();
23204 break;
23206 default:
23207 gcc_unreachable ();
23210 /* We should be able to find the DIE now. */
23211 if (!decl_die)
23212 decl_die = lookup_decl_die (decl);
23213 gcc_assert (decl_die);
23216 return decl_die;
23219 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23220 always returned. */
23222 static dw_die_ref
23223 force_type_die (tree type)
23225 dw_die_ref type_die;
23227 type_die = lookup_type_die (type);
23228 if (!type_die)
23230 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23232 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23233 false, context_die);
23234 gcc_assert (type_die);
23236 return type_die;
23239 /* Force out any required namespaces to be able to output DECL,
23240 and return the new context_die for it, if it's changed. */
23242 static dw_die_ref
23243 setup_namespace_context (tree thing, dw_die_ref context_die)
23245 tree context = (DECL_P (thing)
23246 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23247 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23248 /* Force out the namespace. */
23249 context_die = force_decl_die (context);
23251 return context_die;
23254 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23255 type) within its namespace, if appropriate.
23257 For compatibility with older debuggers, namespace DIEs only contain
23258 declarations; all definitions are emitted at CU scope, with
23259 DW_AT_specification pointing to the declaration (like with class
23260 members). */
23262 static dw_die_ref
23263 declare_in_namespace (tree thing, dw_die_ref context_die)
23265 dw_die_ref ns_context;
23267 if (debug_info_level <= DINFO_LEVEL_TERSE)
23268 return context_die;
23270 /* External declarations in the local scope only need to be emitted
23271 once, not once in the namespace and once in the scope.
23273 This avoids declaring the `extern' below in the
23274 namespace DIE as well as in the innermost scope:
23276 namespace S
23278 int i=5;
23279 int foo()
23281 int i=8;
23282 extern int i;
23283 return i;
23287 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23288 return context_die;
23290 /* If this decl is from an inlined function, then don't try to emit it in its
23291 namespace, as we will get confused. It would have already been emitted
23292 when the abstract instance of the inline function was emitted anyways. */
23293 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23294 return context_die;
23296 ns_context = setup_namespace_context (thing, context_die);
23298 if (ns_context != context_die)
23300 if (is_fortran ())
23301 return ns_context;
23302 if (DECL_P (thing))
23303 gen_decl_die (thing, NULL, NULL, ns_context);
23304 else
23305 gen_type_die (thing, ns_context);
23307 return context_die;
23310 /* Generate a DIE for a namespace or namespace alias. */
23312 static void
23313 gen_namespace_die (tree decl, dw_die_ref context_die)
23315 dw_die_ref namespace_die;
23317 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23318 they are an alias of. */
23319 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23321 /* Output a real namespace or module. */
23322 context_die = setup_namespace_context (decl, comp_unit_die ());
23323 namespace_die = new_die (is_fortran ()
23324 ? DW_TAG_module : DW_TAG_namespace,
23325 context_die, decl);
23326 /* For Fortran modules defined in different CU don't add src coords. */
23327 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23329 const char *name = dwarf2_name (decl, 0);
23330 if (name)
23331 add_name_attribute (namespace_die, name);
23333 else
23334 add_name_and_src_coords_attributes (namespace_die, decl);
23335 if (DECL_EXTERNAL (decl))
23336 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23337 equate_decl_number_to_die (decl, namespace_die);
23339 else
23341 /* Output a namespace alias. */
23343 /* Force out the namespace we are an alias of, if necessary. */
23344 dw_die_ref origin_die
23345 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23347 if (DECL_FILE_SCOPE_P (decl)
23348 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23349 context_die = setup_namespace_context (decl, comp_unit_die ());
23350 /* Now create the namespace alias DIE. */
23351 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23352 add_name_and_src_coords_attributes (namespace_die, decl);
23353 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23354 equate_decl_number_to_die (decl, namespace_die);
23356 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23357 if (want_pubnames ())
23358 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23361 /* Generate Dwarf debug information for a decl described by DECL.
23362 The return value is currently only meaningful for PARM_DECLs,
23363 for all other decls it returns NULL.
23365 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23366 It can be NULL otherwise. */
23368 static dw_die_ref
23369 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23370 dw_die_ref context_die)
23372 tree decl_or_origin = decl ? decl : origin;
23373 tree class_origin = NULL, ultimate_origin;
23375 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23376 return NULL;
23378 /* Ignore pointer bounds decls. */
23379 if (DECL_P (decl_or_origin)
23380 && TREE_TYPE (decl_or_origin)
23381 && POINTER_BOUNDS_P (decl_or_origin))
23382 return NULL;
23384 switch (TREE_CODE (decl_or_origin))
23386 case ERROR_MARK:
23387 break;
23389 case CONST_DECL:
23390 if (!is_fortran () && !is_ada ())
23392 /* The individual enumerators of an enum type get output when we output
23393 the Dwarf representation of the relevant enum type itself. */
23394 break;
23397 /* Emit its type. */
23398 gen_type_die (TREE_TYPE (decl), context_die);
23400 /* And its containing namespace. */
23401 context_die = declare_in_namespace (decl, context_die);
23403 gen_const_die (decl, context_die);
23404 break;
23406 case FUNCTION_DECL:
23407 /* Don't output any DIEs to represent mere function declarations,
23408 unless they are class members or explicit block externs. */
23409 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23410 && DECL_FILE_SCOPE_P (decl_or_origin)
23411 && (current_function_decl == NULL_TREE
23412 || DECL_ARTIFICIAL (decl_or_origin)))
23413 break;
23415 #if 0
23416 /* FIXME */
23417 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23418 on local redeclarations of global functions. That seems broken. */
23419 if (current_function_decl != decl)
23420 /* This is only a declaration. */;
23421 #endif
23423 /* If we're emitting a clone, emit info for the abstract instance. */
23424 if (origin || DECL_ORIGIN (decl) != decl)
23425 dwarf2out_abstract_function (origin
23426 ? DECL_ORIGIN (origin)
23427 : DECL_ABSTRACT_ORIGIN (decl));
23429 /* If we're emitting an out-of-line copy of an inline function,
23430 emit info for the abstract instance and set up to refer to it. */
23431 else if (cgraph_function_possibly_inlined_p (decl)
23432 && ! DECL_ABSTRACT_P (decl)
23433 && ! class_or_namespace_scope_p (context_die)
23434 /* dwarf2out_abstract_function won't emit a die if this is just
23435 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23436 that case, because that works only if we have a die. */
23437 && DECL_INITIAL (decl) != NULL_TREE)
23439 dwarf2out_abstract_function (decl);
23440 set_decl_origin_self (decl);
23443 /* Otherwise we're emitting the primary DIE for this decl. */
23444 else if (debug_info_level > DINFO_LEVEL_TERSE)
23446 /* Before we describe the FUNCTION_DECL itself, make sure that we
23447 have its containing type. */
23448 if (!origin)
23449 origin = decl_class_context (decl);
23450 if (origin != NULL_TREE)
23451 gen_type_die (origin, context_die);
23453 /* And its return type. */
23454 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23456 /* And its virtual context. */
23457 if (DECL_VINDEX (decl) != NULL_TREE)
23458 gen_type_die (DECL_CONTEXT (decl), context_die);
23460 /* Make sure we have a member DIE for decl. */
23461 if (origin != NULL_TREE)
23462 gen_type_die_for_member (origin, decl, context_die);
23464 /* And its containing namespace. */
23465 context_die = declare_in_namespace (decl, context_die);
23468 /* Now output a DIE to represent the function itself. */
23469 if (decl)
23470 gen_subprogram_die (decl, context_die);
23471 break;
23473 case TYPE_DECL:
23474 /* If we are in terse mode, don't generate any DIEs to represent any
23475 actual typedefs. */
23476 if (debug_info_level <= DINFO_LEVEL_TERSE)
23477 break;
23479 /* In the special case of a TYPE_DECL node representing the declaration
23480 of some type tag, if the given TYPE_DECL is marked as having been
23481 instantiated from some other (original) TYPE_DECL node (e.g. one which
23482 was generated within the original definition of an inline function) we
23483 used to generate a special (abbreviated) DW_TAG_structure_type,
23484 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23485 should be actually referencing those DIEs, as variable DIEs with that
23486 type would be emitted already in the abstract origin, so it was always
23487 removed during unused type prunning. Don't add anything in this
23488 case. */
23489 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23490 break;
23492 if (is_redundant_typedef (decl))
23493 gen_type_die (TREE_TYPE (decl), context_die);
23494 else
23495 /* Output a DIE to represent the typedef itself. */
23496 gen_typedef_die (decl, context_die);
23497 break;
23499 case LABEL_DECL:
23500 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23501 gen_label_die (decl, context_die);
23502 break;
23504 case VAR_DECL:
23505 case RESULT_DECL:
23506 /* If we are in terse mode, don't generate any DIEs to represent any
23507 variable declarations or definitions. */
23508 if (debug_info_level <= DINFO_LEVEL_TERSE)
23509 break;
23511 /* Output any DIEs that are needed to specify the type of this data
23512 object. */
23513 if (decl_by_reference_p (decl_or_origin))
23514 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23515 else
23516 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23518 /* And its containing type. */
23519 class_origin = decl_class_context (decl_or_origin);
23520 if (class_origin != NULL_TREE)
23521 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23523 /* And its containing namespace. */
23524 context_die = declare_in_namespace (decl_or_origin, context_die);
23526 /* Now output the DIE to represent the data object itself. This gets
23527 complicated because of the possibility that the VAR_DECL really
23528 represents an inlined instance of a formal parameter for an inline
23529 function. */
23530 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23531 if (ultimate_origin != NULL_TREE
23532 && TREE_CODE (ultimate_origin) == PARM_DECL)
23533 gen_formal_parameter_die (decl, origin,
23534 true /* Emit name attribute. */,
23535 context_die);
23536 else
23537 gen_variable_die (decl, origin, context_die);
23538 break;
23540 case FIELD_DECL:
23541 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23542 /* Ignore the nameless fields that are used to skip bits but handle C++
23543 anonymous unions and structs. */
23544 if (DECL_NAME (decl) != NULL_TREE
23545 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23546 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23548 gen_type_die (member_declared_type (decl), context_die);
23549 gen_field_die (decl, ctx, context_die);
23551 break;
23553 case PARM_DECL:
23554 if (DECL_BY_REFERENCE (decl_or_origin))
23555 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23556 else
23557 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23558 return gen_formal_parameter_die (decl, origin,
23559 true /* Emit name attribute. */,
23560 context_die);
23562 case NAMESPACE_DECL:
23563 if (dwarf_version >= 3 || !dwarf_strict)
23564 gen_namespace_die (decl, context_die);
23565 break;
23567 case IMPORTED_DECL:
23568 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23569 DECL_CONTEXT (decl), context_die);
23570 break;
23572 case NAMELIST_DECL:
23573 gen_namelist_decl (DECL_NAME (decl), context_die,
23574 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23575 break;
23577 default:
23578 /* Probably some frontend-internal decl. Assume we don't care. */
23579 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23580 break;
23583 return NULL;
23586 /* Output initial debug information for global DECL. Called at the
23587 end of the parsing process.
23589 This is the initial debug generation process. As such, the DIEs
23590 generated may be incomplete. A later debug generation pass
23591 (dwarf2out_late_global_decl) will augment the information generated
23592 in this pass (e.g., with complete location info). */
23594 static void
23595 dwarf2out_early_global_decl (tree decl)
23597 set_early_dwarf s;
23599 /* gen_decl_die() will set DECL_ABSTRACT because
23600 cgraph_function_possibly_inlined_p() returns true. This is in
23601 turn will cause DW_AT_inline attributes to be set.
23603 This happens because at early dwarf generation, there is no
23604 cgraph information, causing cgraph_function_possibly_inlined_p()
23605 to return true. Trick cgraph_function_possibly_inlined_p()
23606 while we generate dwarf early. */
23607 bool save = symtab->global_info_ready;
23608 symtab->global_info_ready = true;
23610 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23611 other DECLs and they can point to template types or other things
23612 that dwarf2out can't handle when done via dwarf2out_decl. */
23613 if (TREE_CODE (decl) != TYPE_DECL
23614 && TREE_CODE (decl) != PARM_DECL)
23616 tree save_fndecl = current_function_decl;
23617 if (TREE_CODE (decl) == FUNCTION_DECL)
23619 /* No cfun means the symbol has no body, so there's nothing
23620 to emit. */
23621 if (!DECL_STRUCT_FUNCTION (decl))
23622 goto early_decl_exit;
23624 current_function_decl = decl;
23626 dwarf2out_decl (decl);
23627 if (TREE_CODE (decl) == FUNCTION_DECL)
23628 current_function_decl = save_fndecl;
23630 early_decl_exit:
23631 symtab->global_info_ready = save;
23634 /* Output debug information for global decl DECL. Called from
23635 toplev.c after compilation proper has finished. */
23637 static void
23638 dwarf2out_late_global_decl (tree decl)
23640 /* We have to generate early debug late for LTO. */
23641 if (in_lto_p)
23642 dwarf2out_early_global_decl (decl);
23644 /* Fill-in any location information we were unable to determine
23645 on the first pass. */
23646 if (TREE_CODE (decl) == VAR_DECL
23647 && !POINTER_BOUNDS_P (decl))
23649 dw_die_ref die = lookup_decl_die (decl);
23650 if (die)
23651 add_location_or_const_value_attribute (die, decl, false);
23655 /* Output debug information for type decl DECL. Called from toplev.c
23656 and from language front ends (to record built-in types). */
23657 static void
23658 dwarf2out_type_decl (tree decl, int local)
23660 if (!local)
23662 set_early_dwarf s;
23663 dwarf2out_decl (decl);
23667 /* Output debug information for imported module or decl DECL.
23668 NAME is non-NULL name in the lexical block if the decl has been renamed.
23669 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
23670 that DECL belongs to.
23671 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
23672 static void
23673 dwarf2out_imported_module_or_decl_1 (tree decl,
23674 tree name,
23675 tree lexical_block,
23676 dw_die_ref lexical_block_die)
23678 expanded_location xloc;
23679 dw_die_ref imported_die = NULL;
23680 dw_die_ref at_import_die;
23682 if (TREE_CODE (decl) == IMPORTED_DECL)
23684 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
23685 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
23686 gcc_assert (decl);
23688 else
23689 xloc = expand_location (input_location);
23691 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
23693 at_import_die = force_type_die (TREE_TYPE (decl));
23694 /* For namespace N { typedef void T; } using N::T; base_type_die
23695 returns NULL, but DW_TAG_imported_declaration requires
23696 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
23697 if (!at_import_die)
23699 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
23700 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
23701 at_import_die = lookup_type_die (TREE_TYPE (decl));
23702 gcc_assert (at_import_die);
23705 else
23707 at_import_die = lookup_decl_die (decl);
23708 if (!at_import_die)
23710 /* If we're trying to avoid duplicate debug info, we may not have
23711 emitted the member decl for this field. Emit it now. */
23712 if (TREE_CODE (decl) == FIELD_DECL)
23714 tree type = DECL_CONTEXT (decl);
23716 if (TYPE_CONTEXT (type)
23717 && TYPE_P (TYPE_CONTEXT (type))
23718 && !should_emit_struct_debug (TYPE_CONTEXT (type),
23719 DINFO_USAGE_DIR_USE))
23720 return;
23721 gen_type_die_for_member (type, decl,
23722 get_context_die (TYPE_CONTEXT (type)));
23724 if (TREE_CODE (decl) == NAMELIST_DECL)
23725 at_import_die = gen_namelist_decl (DECL_NAME (decl),
23726 get_context_die (DECL_CONTEXT (decl)),
23727 NULL_TREE);
23728 else
23729 at_import_die = force_decl_die (decl);
23733 if (TREE_CODE (decl) == NAMESPACE_DECL)
23735 if (dwarf_version >= 3 || !dwarf_strict)
23736 imported_die = new_die (DW_TAG_imported_module,
23737 lexical_block_die,
23738 lexical_block);
23739 else
23740 return;
23742 else
23743 imported_die = new_die (DW_TAG_imported_declaration,
23744 lexical_block_die,
23745 lexical_block);
23747 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
23748 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
23749 if (name)
23750 add_AT_string (imported_die, DW_AT_name,
23751 IDENTIFIER_POINTER (name));
23752 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
23755 /* Output debug information for imported module or decl DECL.
23756 NAME is non-NULL name in context if the decl has been renamed.
23757 CHILD is true if decl is one of the renamed decls as part of
23758 importing whole module. */
23760 static void
23761 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
23762 bool child)
23764 /* dw_die_ref at_import_die; */
23765 dw_die_ref scope_die;
23767 if (debug_info_level <= DINFO_LEVEL_TERSE)
23768 return;
23770 gcc_assert (decl);
23772 set_early_dwarf s;
23774 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
23775 We need decl DIE for reference and scope die. First, get DIE for the decl
23776 itself. */
23778 /* Get the scope die for decl context. Use comp_unit_die for global module
23779 or decl. If die is not found for non globals, force new die. */
23780 if (context
23781 && TYPE_P (context)
23782 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
23783 return;
23785 if (!(dwarf_version >= 3 || !dwarf_strict))
23786 return;
23788 scope_die = get_context_die (context);
23790 if (child)
23792 gcc_assert (scope_die->die_child);
23793 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
23794 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
23795 scope_die = scope_die->die_child;
23798 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
23799 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
23802 /* Output debug information for namelists. */
23804 static dw_die_ref
23805 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
23807 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
23808 tree value;
23809 unsigned i;
23811 if (debug_info_level <= DINFO_LEVEL_TERSE)
23812 return NULL;
23814 gcc_assert (scope_die != NULL);
23815 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
23816 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
23818 /* If there are no item_decls, we have a nondefining namelist, e.g.
23819 with USE association; hence, set DW_AT_declaration. */
23820 if (item_decls == NULL_TREE)
23822 add_AT_flag (nml_die, DW_AT_declaration, 1);
23823 return nml_die;
23826 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
23828 nml_item_ref_die = lookup_decl_die (value);
23829 if (!nml_item_ref_die)
23830 nml_item_ref_die = force_decl_die (value);
23832 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
23833 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
23835 return nml_die;
23839 /* Write the debugging output for DECL and return the DIE. */
23841 static void
23842 dwarf2out_decl (tree decl)
23844 dw_die_ref context_die = comp_unit_die ();
23846 switch (TREE_CODE (decl))
23848 case ERROR_MARK:
23849 return;
23851 case FUNCTION_DECL:
23852 /* What we would really like to do here is to filter out all mere
23853 file-scope declarations of file-scope functions which are never
23854 referenced later within this translation unit (and keep all of ones
23855 that *are* referenced later on) but we aren't clairvoyant, so we have
23856 no idea which functions will be referenced in the future (i.e. later
23857 on within the current translation unit). So here we just ignore all
23858 file-scope function declarations which are not also definitions. If
23859 and when the debugger needs to know something about these functions,
23860 it will have to hunt around and find the DWARF information associated
23861 with the definition of the function.
23863 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
23864 nodes represent definitions and which ones represent mere
23865 declarations. We have to check DECL_INITIAL instead. That's because
23866 the C front-end supports some weird semantics for "extern inline"
23867 function definitions. These can get inlined within the current
23868 translation unit (and thus, we need to generate Dwarf info for their
23869 abstract instances so that the Dwarf info for the concrete inlined
23870 instances can have something to refer to) but the compiler never
23871 generates any out-of-lines instances of such things (despite the fact
23872 that they *are* definitions).
23874 The important point is that the C front-end marks these "extern
23875 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
23876 them anyway. Note that the C++ front-end also plays some similar games
23877 for inline function definitions appearing within include files which
23878 also contain `#pragma interface' pragmas.
23880 If we are called from dwarf2out_abstract_function output a DIE
23881 anyway. We can end up here this way with early inlining and LTO
23882 where the inlined function is output in a different LTRANS unit
23883 or not at all. */
23884 if (DECL_INITIAL (decl) == NULL_TREE
23885 && ! DECL_ABSTRACT_P (decl))
23886 return;
23888 /* If we're a nested function, initially use a parent of NULL; if we're
23889 a plain function, this will be fixed up in decls_for_scope. If
23890 we're a method, it will be ignored, since we already have a DIE. */
23891 if (decl_function_context (decl)
23892 /* But if we're in terse mode, we don't care about scope. */
23893 && debug_info_level > DINFO_LEVEL_TERSE)
23894 context_die = NULL;
23895 break;
23897 case VAR_DECL:
23898 /* For local statics lookup proper context die. */
23899 if (local_function_static (decl))
23900 context_die = lookup_decl_die (DECL_CONTEXT (decl));
23902 /* If we are in terse mode, don't generate any DIEs to represent any
23903 variable declarations or definitions. */
23904 if (debug_info_level <= DINFO_LEVEL_TERSE)
23905 return;
23906 break;
23908 case CONST_DECL:
23909 if (debug_info_level <= DINFO_LEVEL_TERSE)
23910 return;
23911 if (!is_fortran () && !is_ada ())
23912 return;
23913 if (TREE_STATIC (decl) && decl_function_context (decl))
23914 context_die = lookup_decl_die (DECL_CONTEXT (decl));
23915 break;
23917 case NAMESPACE_DECL:
23918 case IMPORTED_DECL:
23919 if (debug_info_level <= DINFO_LEVEL_TERSE)
23920 return;
23921 if (lookup_decl_die (decl) != NULL)
23922 return;
23923 break;
23925 case TYPE_DECL:
23926 /* Don't emit stubs for types unless they are needed by other DIEs. */
23927 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
23928 return;
23930 /* Don't bother trying to generate any DIEs to represent any of the
23931 normal built-in types for the language we are compiling. */
23932 if (DECL_IS_BUILTIN (decl))
23933 return;
23935 /* If we are in terse mode, don't generate any DIEs for types. */
23936 if (debug_info_level <= DINFO_LEVEL_TERSE)
23937 return;
23939 /* If we're a function-scope tag, initially use a parent of NULL;
23940 this will be fixed up in decls_for_scope. */
23941 if (decl_function_context (decl))
23942 context_die = NULL;
23944 break;
23946 case NAMELIST_DECL:
23947 break;
23949 default:
23950 return;
23953 gen_decl_die (decl, NULL, NULL, context_die);
23955 if (flag_checking)
23957 dw_die_ref die = lookup_decl_die (decl);
23958 if (die)
23959 check_die (die);
23963 /* Write the debugging output for DECL. */
23965 static void
23966 dwarf2out_function_decl (tree decl)
23968 dwarf2out_decl (decl);
23969 call_arg_locations = NULL;
23970 call_arg_loc_last = NULL;
23971 call_site_count = -1;
23972 tail_call_site_count = -1;
23973 decl_loc_table->empty ();
23974 cached_dw_loc_list_table->empty ();
23977 /* Output a marker (i.e. a label) for the beginning of the generated code for
23978 a lexical block. */
23980 static void
23981 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
23982 unsigned int blocknum)
23984 switch_to_section (current_function_section ());
23985 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
23988 /* Output a marker (i.e. a label) for the end of the generated code for a
23989 lexical block. */
23991 static void
23992 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
23994 switch_to_section (current_function_section ());
23995 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
23998 /* Returns nonzero if it is appropriate not to emit any debugging
23999 information for BLOCK, because it doesn't contain any instructions.
24001 Don't allow this for blocks with nested functions or local classes
24002 as we would end up with orphans, and in the presence of scheduling
24003 we may end up calling them anyway. */
24005 static bool
24006 dwarf2out_ignore_block (const_tree block)
24008 tree decl;
24009 unsigned int i;
24011 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24012 if (TREE_CODE (decl) == FUNCTION_DECL
24013 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24014 return 0;
24015 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24017 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24018 if (TREE_CODE (decl) == FUNCTION_DECL
24019 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24020 return 0;
24023 return 1;
24026 /* Hash table routines for file_hash. */
24028 bool
24029 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24031 return filename_cmp (p1->filename, p2) == 0;
24034 hashval_t
24035 dwarf_file_hasher::hash (dwarf_file_data *p)
24037 return htab_hash_string (p->filename);
24040 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24041 dwarf2out.c) and return its "index". The index of each (known) filename is
24042 just a unique number which is associated with only that one filename. We
24043 need such numbers for the sake of generating labels (in the .debug_sfnames
24044 section) and references to those files numbers (in the .debug_srcinfo
24045 and .debug_macinfo sections). If the filename given as an argument is not
24046 found in our current list, add it to the list and assign it the next
24047 available unique index number. */
24049 static struct dwarf_file_data *
24050 lookup_filename (const char *file_name)
24052 struct dwarf_file_data * created;
24054 if (!file_name)
24055 return NULL;
24057 dwarf_file_data **slot
24058 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24059 INSERT);
24060 if (*slot)
24061 return *slot;
24063 created = ggc_alloc<dwarf_file_data> ();
24064 created->filename = file_name;
24065 created->emitted_number = 0;
24066 *slot = created;
24067 return created;
24070 /* If the assembler will construct the file table, then translate the compiler
24071 internal file table number into the assembler file table number, and emit
24072 a .file directive if we haven't already emitted one yet. The file table
24073 numbers are different because we prune debug info for unused variables and
24074 types, which may include filenames. */
24076 static int
24077 maybe_emit_file (struct dwarf_file_data * fd)
24079 if (! fd->emitted_number)
24081 if (last_emitted_file)
24082 fd->emitted_number = last_emitted_file->emitted_number + 1;
24083 else
24084 fd->emitted_number = 1;
24085 last_emitted_file = fd;
24087 if (DWARF2_ASM_LINE_DEBUG_INFO)
24089 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24090 output_quoted_string (asm_out_file,
24091 remap_debug_filename (fd->filename));
24092 fputc ('\n', asm_out_file);
24096 return fd->emitted_number;
24099 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24100 That generation should happen after function debug info has been
24101 generated. The value of the attribute is the constant value of ARG. */
24103 static void
24104 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24106 die_arg_entry entry;
24108 if (!die || !arg)
24109 return;
24111 gcc_assert (early_dwarf);
24113 if (!tmpl_value_parm_die_table)
24114 vec_alloc (tmpl_value_parm_die_table, 32);
24116 entry.die = die;
24117 entry.arg = arg;
24118 vec_safe_push (tmpl_value_parm_die_table, entry);
24121 /* Return TRUE if T is an instance of generic type, FALSE
24122 otherwise. */
24124 static bool
24125 generic_type_p (tree t)
24127 if (t == NULL_TREE || !TYPE_P (t))
24128 return false;
24129 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24132 /* Schedule the generation of the generic parameter dies for the
24133 instance of generic type T. The proper generation itself is later
24134 done by gen_scheduled_generic_parms_dies. */
24136 static void
24137 schedule_generic_params_dies_gen (tree t)
24139 if (!generic_type_p (t))
24140 return;
24142 gcc_assert (early_dwarf);
24144 if (!generic_type_instances)
24145 vec_alloc (generic_type_instances, 256);
24147 vec_safe_push (generic_type_instances, t);
24150 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24151 by append_entry_to_tmpl_value_parm_die_table. This function must
24152 be called after function DIEs have been generated. */
24154 static void
24155 gen_remaining_tmpl_value_param_die_attribute (void)
24157 if (tmpl_value_parm_die_table)
24159 unsigned i, j;
24160 die_arg_entry *e;
24162 /* We do this in two phases - first get the cases we can
24163 handle during early-finish, preserving those we cannot
24164 (containing symbolic constants where we don't yet know
24165 whether we are going to output the referenced symbols).
24166 For those we try again at late-finish. */
24167 j = 0;
24168 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24170 if (!tree_add_const_value_attribute (e->die, e->arg))
24171 (*tmpl_value_parm_die_table)[j++] = *e;
24173 tmpl_value_parm_die_table->truncate (j);
24177 /* Generate generic parameters DIEs for instances of generic types
24178 that have been previously scheduled by
24179 schedule_generic_params_dies_gen. This function must be called
24180 after all the types of the CU have been laid out. */
24182 static void
24183 gen_scheduled_generic_parms_dies (void)
24185 unsigned i;
24186 tree t;
24188 if (!generic_type_instances)
24189 return;
24191 /* We end up "recursing" into schedule_generic_params_dies_gen, so
24192 pretend this generation is part of "early dwarf" as well. */
24193 set_early_dwarf s;
24195 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24196 if (COMPLETE_TYPE_P (t))
24197 gen_generic_params_dies (t);
24199 generic_type_instances = NULL;
24203 /* Replace DW_AT_name for the decl with name. */
24205 static void
24206 dwarf2out_set_name (tree decl, tree name)
24208 dw_die_ref die;
24209 dw_attr_node *attr;
24210 const char *dname;
24212 die = TYPE_SYMTAB_DIE (decl);
24213 if (!die)
24214 return;
24216 dname = dwarf2_name (name, 0);
24217 if (!dname)
24218 return;
24220 attr = get_AT (die, DW_AT_name);
24221 if (attr)
24223 struct indirect_string_node *node;
24225 node = find_AT_string (dname);
24226 /* replace the string. */
24227 attr->dw_attr_val.v.val_str = node;
24230 else
24231 add_name_attribute (die, dname);
24234 /* True if before or during processing of the first function being emitted. */
24235 static bool in_first_function_p = true;
24236 /* True if loc_note during dwarf2out_var_location call might still be
24237 before first real instruction at address equal to .Ltext0. */
24238 static bool maybe_at_text_label_p = true;
24239 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24240 static unsigned int first_loclabel_num_not_at_text_label;
24242 /* Called by the final INSN scan whenever we see a var location. We
24243 use it to drop labels in the right places, and throw the location in
24244 our lookup table. */
24246 static void
24247 dwarf2out_var_location (rtx_insn *loc_note)
24249 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24250 struct var_loc_node *newloc;
24251 rtx_insn *next_real, *next_note;
24252 rtx_insn *call_insn = NULL;
24253 static const char *last_label;
24254 static const char *last_postcall_label;
24255 static bool last_in_cold_section_p;
24256 static rtx_insn *expected_next_loc_note;
24257 tree decl;
24258 bool var_loc_p;
24260 if (!NOTE_P (loc_note))
24262 if (CALL_P (loc_note))
24264 call_site_count++;
24265 if (SIBLING_CALL_P (loc_note))
24266 tail_call_site_count++;
24267 if (optimize == 0 && !flag_var_tracking)
24269 /* When the var-tracking pass is not running, there is no note
24270 for indirect calls whose target is compile-time known. In this
24271 case, process such calls specifically so that we generate call
24272 sites for them anyway. */
24273 rtx x = PATTERN (loc_note);
24274 if (GET_CODE (x) == PARALLEL)
24275 x = XVECEXP (x, 0, 0);
24276 if (GET_CODE (x) == SET)
24277 x = SET_SRC (x);
24278 if (GET_CODE (x) == CALL)
24279 x = XEXP (x, 0);
24280 if (!MEM_P (x)
24281 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24282 || !SYMBOL_REF_DECL (XEXP (x, 0))
24283 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24284 != FUNCTION_DECL))
24286 call_insn = loc_note;
24287 loc_note = NULL;
24288 var_loc_p = false;
24290 next_real = next_real_insn (call_insn);
24291 next_note = NULL;
24292 cached_next_real_insn = NULL;
24293 goto create_label;
24297 return;
24300 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24301 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24302 return;
24304 /* Optimize processing a large consecutive sequence of location
24305 notes so we don't spend too much time in next_real_insn. If the
24306 next insn is another location note, remember the next_real_insn
24307 calculation for next time. */
24308 next_real = cached_next_real_insn;
24309 if (next_real)
24311 if (expected_next_loc_note != loc_note)
24312 next_real = NULL;
24315 next_note = NEXT_INSN (loc_note);
24316 if (! next_note
24317 || next_note->deleted ()
24318 || ! NOTE_P (next_note)
24319 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24320 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24321 next_note = NULL;
24323 if (! next_real)
24324 next_real = next_real_insn (loc_note);
24326 if (next_note)
24328 expected_next_loc_note = next_note;
24329 cached_next_real_insn = next_real;
24331 else
24332 cached_next_real_insn = NULL;
24334 /* If there are no instructions which would be affected by this note,
24335 don't do anything. */
24336 if (var_loc_p
24337 && next_real == NULL_RTX
24338 && !NOTE_DURING_CALL_P (loc_note))
24339 return;
24341 create_label:
24343 if (next_real == NULL_RTX)
24344 next_real = get_last_insn ();
24346 /* If there were any real insns between note we processed last time
24347 and this note (or if it is the first note), clear
24348 last_{,postcall_}label so that they are not reused this time. */
24349 if (last_var_location_insn == NULL_RTX
24350 || last_var_location_insn != next_real
24351 || last_in_cold_section_p != in_cold_section_p)
24353 last_label = NULL;
24354 last_postcall_label = NULL;
24357 if (var_loc_p)
24359 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24360 newloc = add_var_loc_to_decl (decl, loc_note,
24361 NOTE_DURING_CALL_P (loc_note)
24362 ? last_postcall_label : last_label);
24363 if (newloc == NULL)
24364 return;
24366 else
24368 decl = NULL_TREE;
24369 newloc = NULL;
24372 /* If there were no real insns between note we processed last time
24373 and this note, use the label we emitted last time. Otherwise
24374 create a new label and emit it. */
24375 if (last_label == NULL)
24377 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24378 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24379 loclabel_num++;
24380 last_label = ggc_strdup (loclabel);
24381 /* See if loclabel might be equal to .Ltext0. If yes,
24382 bump first_loclabel_num_not_at_text_label. */
24383 if (!have_multiple_function_sections
24384 && in_first_function_p
24385 && maybe_at_text_label_p)
24387 static rtx_insn *last_start;
24388 rtx_insn *insn;
24389 for (insn = loc_note; insn; insn = previous_insn (insn))
24390 if (insn == last_start)
24391 break;
24392 else if (!NONDEBUG_INSN_P (insn))
24393 continue;
24394 else
24396 rtx body = PATTERN (insn);
24397 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24398 continue;
24399 /* Inline asm could occupy zero bytes. */
24400 else if (GET_CODE (body) == ASM_INPUT
24401 || asm_noperands (body) >= 0)
24402 continue;
24403 #ifdef HAVE_attr_length
24404 else if (get_attr_min_length (insn) == 0)
24405 continue;
24406 #endif
24407 else
24409 /* Assume insn has non-zero length. */
24410 maybe_at_text_label_p = false;
24411 break;
24414 if (maybe_at_text_label_p)
24416 last_start = loc_note;
24417 first_loclabel_num_not_at_text_label = loclabel_num;
24422 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24423 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24425 if (!var_loc_p)
24427 struct call_arg_loc_node *ca_loc
24428 = ggc_cleared_alloc<call_arg_loc_node> ();
24429 rtx_insn *prev
24430 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24432 ca_loc->call_arg_loc_note = loc_note;
24433 ca_loc->next = NULL;
24434 ca_loc->label = last_label;
24435 gcc_assert (prev
24436 && (CALL_P (prev)
24437 || (NONJUMP_INSN_P (prev)
24438 && GET_CODE (PATTERN (prev)) == SEQUENCE
24439 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24440 if (!CALL_P (prev))
24441 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24442 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24444 /* Look for a SYMBOL_REF in the "prev" instruction. */
24445 rtx x = get_call_rtx_from (PATTERN (prev));
24446 if (x)
24448 /* Try to get the call symbol, if any. */
24449 if (MEM_P (XEXP (x, 0)))
24450 x = XEXP (x, 0);
24451 /* First, look for a memory access to a symbol_ref. */
24452 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24453 && SYMBOL_REF_DECL (XEXP (x, 0))
24454 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24455 ca_loc->symbol_ref = XEXP (x, 0);
24456 /* Otherwise, look at a compile-time known user-level function
24457 declaration. */
24458 else if (MEM_P (x)
24459 && MEM_EXPR (x)
24460 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24461 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24464 ca_loc->block = insn_scope (prev);
24465 if (call_arg_locations)
24466 call_arg_loc_last->next = ca_loc;
24467 else
24468 call_arg_locations = ca_loc;
24469 call_arg_loc_last = ca_loc;
24471 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24472 newloc->label = last_label;
24473 else
24475 if (!last_postcall_label)
24477 sprintf (loclabel, "%s-1", last_label);
24478 last_postcall_label = ggc_strdup (loclabel);
24480 newloc->label = last_postcall_label;
24483 last_var_location_insn = next_real;
24484 last_in_cold_section_p = in_cold_section_p;
24487 /* Called from finalize_size_functions for size functions so that their body
24488 can be encoded in the debug info to describe the layout of variable-length
24489 structures. */
24491 static void
24492 dwarf2out_size_function (tree decl)
24494 function_to_dwarf_procedure (decl);
24497 /* Note in one location list that text section has changed. */
24500 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24502 var_loc_list *list = *slot;
24503 if (list->first)
24504 list->last_before_switch
24505 = list->last->next ? list->last->next : list->last;
24506 return 1;
24509 /* Note in all location lists that text section has changed. */
24511 static void
24512 var_location_switch_text_section (void)
24514 if (decl_loc_table == NULL)
24515 return;
24517 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24520 /* Create a new line number table. */
24522 static dw_line_info_table *
24523 new_line_info_table (void)
24525 dw_line_info_table *table;
24527 table = ggc_cleared_alloc<dw_line_info_table> ();
24528 table->file_num = 1;
24529 table->line_num = 1;
24530 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24532 return table;
24535 /* Lookup the "current" table into which we emit line info, so
24536 that we don't have to do it for every source line. */
24538 static void
24539 set_cur_line_info_table (section *sec)
24541 dw_line_info_table *table;
24543 if (sec == text_section)
24544 table = text_section_line_info;
24545 else if (sec == cold_text_section)
24547 table = cold_text_section_line_info;
24548 if (!table)
24550 cold_text_section_line_info = table = new_line_info_table ();
24551 table->end_label = cold_end_label;
24554 else
24556 const char *end_label;
24558 if (flag_reorder_blocks_and_partition)
24560 if (in_cold_section_p)
24561 end_label = crtl->subsections.cold_section_end_label;
24562 else
24563 end_label = crtl->subsections.hot_section_end_label;
24565 else
24567 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24568 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24569 current_function_funcdef_no);
24570 end_label = ggc_strdup (label);
24573 table = new_line_info_table ();
24574 table->end_label = end_label;
24576 vec_safe_push (separate_line_info, table);
24579 if (DWARF2_ASM_LINE_DEBUG_INFO)
24580 table->is_stmt = (cur_line_info_table
24581 ? cur_line_info_table->is_stmt
24582 : DWARF_LINE_DEFAULT_IS_STMT_START);
24583 cur_line_info_table = table;
24587 /* We need to reset the locations at the beginning of each
24588 function. We can't do this in the end_function hook, because the
24589 declarations that use the locations won't have been output when
24590 that hook is called. Also compute have_multiple_function_sections here. */
24592 static void
24593 dwarf2out_begin_function (tree fun)
24595 section *sec = function_section (fun);
24597 if (sec != text_section)
24598 have_multiple_function_sections = true;
24600 if (flag_reorder_blocks_and_partition && !cold_text_section)
24602 gcc_assert (current_function_decl == fun);
24603 cold_text_section = unlikely_text_section ();
24604 switch_to_section (cold_text_section);
24605 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24606 switch_to_section (sec);
24609 dwarf2out_note_section_used ();
24610 call_site_count = 0;
24611 tail_call_site_count = 0;
24613 set_cur_line_info_table (sec);
24616 /* Helper function of dwarf2out_end_function, called only after emitting
24617 the very first function into assembly. Check if some .debug_loc range
24618 might end with a .LVL* label that could be equal to .Ltext0.
24619 In that case we must force using absolute addresses in .debug_loc ranges,
24620 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24621 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24622 list terminator.
24623 Set have_multiple_function_sections to true in that case and
24624 terminate htab traversal. */
24627 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24629 var_loc_list *entry = *slot;
24630 struct var_loc_node *node;
24632 node = entry->first;
24633 if (node && node->next && node->next->label)
24635 unsigned int i;
24636 const char *label = node->next->label;
24637 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24639 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24641 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24642 if (strcmp (label, loclabel) == 0)
24644 have_multiple_function_sections = true;
24645 return 0;
24649 return 1;
24652 /* Hook called after emitting a function into assembly.
24653 This does something only for the very first function emitted. */
24655 static void
24656 dwarf2out_end_function (unsigned int)
24658 if (in_first_function_p
24659 && !have_multiple_function_sections
24660 && first_loclabel_num_not_at_text_label
24661 && decl_loc_table)
24662 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
24663 in_first_function_p = false;
24664 maybe_at_text_label_p = false;
24667 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
24668 front-ends register a translation unit even before dwarf2out_init is
24669 called. */
24670 static tree main_translation_unit = NULL_TREE;
24672 /* Hook called by front-ends after they built their main translation unit.
24673 Associate comp_unit_die to UNIT. */
24675 static void
24676 dwarf2out_register_main_translation_unit (tree unit)
24678 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
24679 && main_translation_unit == NULL_TREE);
24680 main_translation_unit = unit;
24681 /* If dwarf2out_init has not been called yet, it will perform the association
24682 itself looking at main_translation_unit. */
24683 if (decl_die_table != NULL)
24684 equate_decl_number_to_die (unit, comp_unit_die ());
24687 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
24689 static void
24690 push_dw_line_info_entry (dw_line_info_table *table,
24691 enum dw_line_info_opcode opcode, unsigned int val)
24693 dw_line_info_entry e;
24694 e.opcode = opcode;
24695 e.val = val;
24696 vec_safe_push (table->entries, e);
24699 /* Output a label to mark the beginning of a source code line entry
24700 and record information relating to this source line, in
24701 'line_info_table' for later output of the .debug_line section. */
24702 /* ??? The discriminator parameter ought to be unsigned. */
24704 static void
24705 dwarf2out_source_line (unsigned int line, const char *filename,
24706 int discriminator, bool is_stmt)
24708 unsigned int file_num;
24709 dw_line_info_table *table;
24711 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
24712 return;
24714 /* The discriminator column was added in dwarf4. Simplify the below
24715 by simply removing it if we're not supposed to output it. */
24716 if (dwarf_version < 4 && dwarf_strict)
24717 discriminator = 0;
24719 table = cur_line_info_table;
24720 file_num = maybe_emit_file (lookup_filename (filename));
24722 /* ??? TODO: Elide duplicate line number entries. Traditionally,
24723 the debugger has used the second (possibly duplicate) line number
24724 at the beginning of the function to mark the end of the prologue.
24725 We could eliminate any other duplicates within the function. For
24726 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
24727 that second line number entry. */
24728 /* Recall that this end-of-prologue indication is *not* the same thing
24729 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
24730 to which the hook corresponds, follows the last insn that was
24731 emitted by gen_prologue. What we need is to precede the first insn
24732 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
24733 insn that corresponds to something the user wrote. These may be
24734 very different locations once scheduling is enabled. */
24736 if (0 && file_num == table->file_num
24737 && line == table->line_num
24738 && discriminator == table->discrim_num
24739 && is_stmt == table->is_stmt)
24740 return;
24742 switch_to_section (current_function_section ());
24744 /* If requested, emit something human-readable. */
24745 if (flag_debug_asm)
24746 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
24748 if (DWARF2_ASM_LINE_DEBUG_INFO)
24750 /* Emit the .loc directive understood by GNU as. */
24751 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
24752 file_num, line, is_stmt, discriminator */
24753 fputs ("\t.loc ", asm_out_file);
24754 fprint_ul (asm_out_file, file_num);
24755 putc (' ', asm_out_file);
24756 fprint_ul (asm_out_file, line);
24757 putc (' ', asm_out_file);
24758 putc ('0', asm_out_file);
24760 if (is_stmt != table->is_stmt)
24762 fputs (" is_stmt ", asm_out_file);
24763 putc (is_stmt ? '1' : '0', asm_out_file);
24765 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
24767 gcc_assert (discriminator > 0);
24768 fputs (" discriminator ", asm_out_file);
24769 fprint_ul (asm_out_file, (unsigned long) discriminator);
24771 putc ('\n', asm_out_file);
24773 else
24775 unsigned int label_num = ++line_info_label_num;
24777 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
24779 push_dw_line_info_entry (table, LI_set_address, label_num);
24780 if (file_num != table->file_num)
24781 push_dw_line_info_entry (table, LI_set_file, file_num);
24782 if (discriminator != table->discrim_num)
24783 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
24784 if (is_stmt != table->is_stmt)
24785 push_dw_line_info_entry (table, LI_negate_stmt, 0);
24786 push_dw_line_info_entry (table, LI_set_line, line);
24789 table->file_num = file_num;
24790 table->line_num = line;
24791 table->discrim_num = discriminator;
24792 table->is_stmt = is_stmt;
24793 table->in_use = true;
24796 /* Record the beginning of a new source file. */
24798 static void
24799 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
24801 if (flag_eliminate_dwarf2_dups)
24803 /* Record the beginning of the file for break_out_includes. */
24804 dw_die_ref bincl_die;
24806 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
24807 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
24810 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24812 macinfo_entry e;
24813 e.code = DW_MACINFO_start_file;
24814 e.lineno = lineno;
24815 e.info = ggc_strdup (filename);
24816 vec_safe_push (macinfo_table, e);
24820 /* Record the end of a source file. */
24822 static void
24823 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
24825 if (flag_eliminate_dwarf2_dups)
24826 /* Record the end of the file for break_out_includes. */
24827 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
24829 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24831 macinfo_entry e;
24832 e.code = DW_MACINFO_end_file;
24833 e.lineno = lineno;
24834 e.info = NULL;
24835 vec_safe_push (macinfo_table, e);
24839 /* Called from debug_define in toplev.c. The `buffer' parameter contains
24840 the tail part of the directive line, i.e. the part which is past the
24841 initial whitespace, #, whitespace, directive-name, whitespace part. */
24843 static void
24844 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
24845 const char *buffer ATTRIBUTE_UNUSED)
24847 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24849 macinfo_entry e;
24850 /* Insert a dummy first entry to be able to optimize the whole
24851 predefined macro block using DW_MACRO_GNU_transparent_include. */
24852 if (macinfo_table->is_empty () && lineno <= 1)
24854 e.code = 0;
24855 e.lineno = 0;
24856 e.info = NULL;
24857 vec_safe_push (macinfo_table, e);
24859 e.code = DW_MACINFO_define;
24860 e.lineno = lineno;
24861 e.info = ggc_strdup (buffer);
24862 vec_safe_push (macinfo_table, e);
24866 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
24867 the tail part of the directive line, i.e. the part which is past the
24868 initial whitespace, #, whitespace, directive-name, whitespace part. */
24870 static void
24871 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
24872 const char *buffer ATTRIBUTE_UNUSED)
24874 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24876 macinfo_entry e;
24877 /* Insert a dummy first entry to be able to optimize the whole
24878 predefined macro block using DW_MACRO_GNU_transparent_include. */
24879 if (macinfo_table->is_empty () && lineno <= 1)
24881 e.code = 0;
24882 e.lineno = 0;
24883 e.info = NULL;
24884 vec_safe_push (macinfo_table, e);
24886 e.code = DW_MACINFO_undef;
24887 e.lineno = lineno;
24888 e.info = ggc_strdup (buffer);
24889 vec_safe_push (macinfo_table, e);
24893 /* Helpers to manipulate hash table of CUs. */
24895 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
24897 static inline hashval_t hash (const macinfo_entry *);
24898 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
24901 inline hashval_t
24902 macinfo_entry_hasher::hash (const macinfo_entry *entry)
24904 return htab_hash_string (entry->info);
24907 inline bool
24908 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
24909 const macinfo_entry *entry2)
24911 return !strcmp (entry1->info, entry2->info);
24914 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
24916 /* Output a single .debug_macinfo entry. */
24918 static void
24919 output_macinfo_op (macinfo_entry *ref)
24921 int file_num;
24922 size_t len;
24923 struct indirect_string_node *node;
24924 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24925 struct dwarf_file_data *fd;
24927 switch (ref->code)
24929 case DW_MACINFO_start_file:
24930 fd = lookup_filename (ref->info);
24931 file_num = maybe_emit_file (fd);
24932 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
24933 dw2_asm_output_data_uleb128 (ref->lineno,
24934 "Included from line number %lu",
24935 (unsigned long) ref->lineno);
24936 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
24937 break;
24938 case DW_MACINFO_end_file:
24939 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
24940 break;
24941 case DW_MACINFO_define:
24942 case DW_MACINFO_undef:
24943 len = strlen (ref->info) + 1;
24944 if (!dwarf_strict
24945 && len > DWARF_OFFSET_SIZE
24946 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
24947 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
24949 ref->code = ref->code == DW_MACINFO_define
24950 ? DW_MACRO_GNU_define_indirect
24951 : DW_MACRO_GNU_undef_indirect;
24952 output_macinfo_op (ref);
24953 return;
24955 dw2_asm_output_data (1, ref->code,
24956 ref->code == DW_MACINFO_define
24957 ? "Define macro" : "Undefine macro");
24958 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
24959 (unsigned long) ref->lineno);
24960 dw2_asm_output_nstring (ref->info, -1, "The macro");
24961 break;
24962 case DW_MACRO_GNU_define_indirect:
24963 case DW_MACRO_GNU_undef_indirect:
24964 node = find_AT_string (ref->info);
24965 gcc_assert (node
24966 && ((node->form == DW_FORM_strp)
24967 || (node->form == DW_FORM_GNU_str_index)));
24968 dw2_asm_output_data (1, ref->code,
24969 ref->code == DW_MACRO_GNU_define_indirect
24970 ? "Define macro indirect"
24971 : "Undefine macro indirect");
24972 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
24973 (unsigned long) ref->lineno);
24974 if (node->form == DW_FORM_strp)
24975 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
24976 debug_str_section, "The macro: \"%s\"",
24977 ref->info);
24978 else
24979 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
24980 ref->info);
24981 break;
24982 case DW_MACRO_GNU_transparent_include:
24983 dw2_asm_output_data (1, ref->code, "Transparent include");
24984 ASM_GENERATE_INTERNAL_LABEL (label,
24985 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
24986 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
24987 break;
24988 default:
24989 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
24990 ASM_COMMENT_START, (unsigned long) ref->code);
24991 break;
24995 /* Attempt to make a sequence of define/undef macinfo ops shareable with
24996 other compilation unit .debug_macinfo sections. IDX is the first
24997 index of a define/undef, return the number of ops that should be
24998 emitted in a comdat .debug_macinfo section and emit
24999 a DW_MACRO_GNU_transparent_include entry referencing it.
25000 If the define/undef entry should be emitted normally, return 0. */
25002 static unsigned
25003 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25004 macinfo_hash_type **macinfo_htab)
25006 macinfo_entry *first, *second, *cur, *inc;
25007 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25008 unsigned char checksum[16];
25009 struct md5_ctx ctx;
25010 char *grp_name, *tail;
25011 const char *base;
25012 unsigned int i, count, encoded_filename_len, linebuf_len;
25013 macinfo_entry **slot;
25015 first = &(*macinfo_table)[idx];
25016 second = &(*macinfo_table)[idx + 1];
25018 /* Optimize only if there are at least two consecutive define/undef ops,
25019 and either all of them are before first DW_MACINFO_start_file
25020 with lineno {0,1} (i.e. predefined macro block), or all of them are
25021 in some included header file. */
25022 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25023 return 0;
25024 if (vec_safe_is_empty (files))
25026 if (first->lineno > 1 || second->lineno > 1)
25027 return 0;
25029 else if (first->lineno == 0)
25030 return 0;
25032 /* Find the last define/undef entry that can be grouped together
25033 with first and at the same time compute md5 checksum of their
25034 codes, linenumbers and strings. */
25035 md5_init_ctx (&ctx);
25036 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25037 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25038 break;
25039 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25040 break;
25041 else
25043 unsigned char code = cur->code;
25044 md5_process_bytes (&code, 1, &ctx);
25045 checksum_uleb128 (cur->lineno, &ctx);
25046 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25048 md5_finish_ctx (&ctx, checksum);
25049 count = i - idx;
25051 /* From the containing include filename (if any) pick up just
25052 usable characters from its basename. */
25053 if (vec_safe_is_empty (files))
25054 base = "";
25055 else
25056 base = lbasename (files->last ().info);
25057 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25058 if (ISIDNUM (base[i]) || base[i] == '.')
25059 encoded_filename_len++;
25060 /* Count . at the end. */
25061 if (encoded_filename_len)
25062 encoded_filename_len++;
25064 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25065 linebuf_len = strlen (linebuf);
25067 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25068 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25069 + 16 * 2 + 1);
25070 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25071 tail = grp_name + 4;
25072 if (encoded_filename_len)
25074 for (i = 0; base[i]; i++)
25075 if (ISIDNUM (base[i]) || base[i] == '.')
25076 *tail++ = base[i];
25077 *tail++ = '.';
25079 memcpy (tail, linebuf, linebuf_len);
25080 tail += linebuf_len;
25081 *tail++ = '.';
25082 for (i = 0; i < 16; i++)
25083 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25085 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25086 in the empty vector entry before the first define/undef. */
25087 inc = &(*macinfo_table)[idx - 1];
25088 inc->code = DW_MACRO_GNU_transparent_include;
25089 inc->lineno = 0;
25090 inc->info = ggc_strdup (grp_name);
25091 if (!*macinfo_htab)
25092 *macinfo_htab = new macinfo_hash_type (10);
25093 /* Avoid emitting duplicates. */
25094 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25095 if (*slot != NULL)
25097 inc->code = 0;
25098 inc->info = NULL;
25099 /* If such an entry has been used before, just emit
25100 a DW_MACRO_GNU_transparent_include op. */
25101 inc = *slot;
25102 output_macinfo_op (inc);
25103 /* And clear all macinfo_entry in the range to avoid emitting them
25104 in the second pass. */
25105 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25107 cur->code = 0;
25108 cur->info = NULL;
25111 else
25113 *slot = inc;
25114 inc->lineno = (*macinfo_htab)->elements ();
25115 output_macinfo_op (inc);
25117 return count;
25120 /* Save any strings needed by the macinfo table in the debug str
25121 table. All strings must be collected into the table by the time
25122 index_string is called. */
25124 static void
25125 save_macinfo_strings (void)
25127 unsigned len;
25128 unsigned i;
25129 macinfo_entry *ref;
25131 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25133 switch (ref->code)
25135 /* Match the logic in output_macinfo_op to decide on
25136 indirect strings. */
25137 case DW_MACINFO_define:
25138 case DW_MACINFO_undef:
25139 len = strlen (ref->info) + 1;
25140 if (!dwarf_strict
25141 && len > DWARF_OFFSET_SIZE
25142 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25143 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25144 set_indirect_string (find_AT_string (ref->info));
25145 break;
25146 case DW_MACRO_GNU_define_indirect:
25147 case DW_MACRO_GNU_undef_indirect:
25148 set_indirect_string (find_AT_string (ref->info));
25149 break;
25150 default:
25151 break;
25156 /* Output macinfo section(s). */
25158 static void
25159 output_macinfo (void)
25161 unsigned i;
25162 unsigned long length = vec_safe_length (macinfo_table);
25163 macinfo_entry *ref;
25164 vec<macinfo_entry, va_gc> *files = NULL;
25165 macinfo_hash_type *macinfo_htab = NULL;
25167 if (! length)
25168 return;
25170 /* output_macinfo* uses these interchangeably. */
25171 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25172 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25173 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25174 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25176 /* For .debug_macro emit the section header. */
25177 if (!dwarf_strict)
25179 dw2_asm_output_data (2, 4, "DWARF macro version number");
25180 if (DWARF_OFFSET_SIZE == 8)
25181 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25182 else
25183 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25184 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25185 (!dwarf_split_debug_info ? debug_line_section_label
25186 : debug_skeleton_line_section_label),
25187 debug_line_section, NULL);
25190 /* In the first loop, it emits the primary .debug_macinfo section
25191 and after each emitted op the macinfo_entry is cleared.
25192 If a longer range of define/undef ops can be optimized using
25193 DW_MACRO_GNU_transparent_include, the
25194 DW_MACRO_GNU_transparent_include op is emitted and kept in
25195 the vector before the first define/undef in the range and the
25196 whole range of define/undef ops is not emitted and kept. */
25197 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25199 switch (ref->code)
25201 case DW_MACINFO_start_file:
25202 vec_safe_push (files, *ref);
25203 break;
25204 case DW_MACINFO_end_file:
25205 if (!vec_safe_is_empty (files))
25206 files->pop ();
25207 break;
25208 case DW_MACINFO_define:
25209 case DW_MACINFO_undef:
25210 if (!dwarf_strict
25211 && HAVE_COMDAT_GROUP
25212 && vec_safe_length (files) != 1
25213 && i > 0
25214 && i + 1 < length
25215 && (*macinfo_table)[i - 1].code == 0)
25217 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25218 if (count)
25220 i += count - 1;
25221 continue;
25224 break;
25225 case 0:
25226 /* A dummy entry may be inserted at the beginning to be able
25227 to optimize the whole block of predefined macros. */
25228 if (i == 0)
25229 continue;
25230 default:
25231 break;
25233 output_macinfo_op (ref);
25234 ref->info = NULL;
25235 ref->code = 0;
25238 if (!macinfo_htab)
25239 return;
25241 delete macinfo_htab;
25242 macinfo_htab = NULL;
25244 /* If any DW_MACRO_GNU_transparent_include were used, on those
25245 DW_MACRO_GNU_transparent_include entries terminate the
25246 current chain and switch to a new comdat .debug_macinfo
25247 section and emit the define/undef entries within it. */
25248 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25249 switch (ref->code)
25251 case 0:
25252 continue;
25253 case DW_MACRO_GNU_transparent_include:
25255 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25256 tree comdat_key = get_identifier (ref->info);
25257 /* Terminate the previous .debug_macinfo section. */
25258 dw2_asm_output_data (1, 0, "End compilation unit");
25259 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
25260 SECTION_DEBUG
25261 | SECTION_LINKONCE,
25262 comdat_key);
25263 ASM_GENERATE_INTERNAL_LABEL (label,
25264 DEBUG_MACRO_SECTION_LABEL,
25265 ref->lineno);
25266 ASM_OUTPUT_LABEL (asm_out_file, label);
25267 ref->code = 0;
25268 ref->info = NULL;
25269 dw2_asm_output_data (2, 4, "DWARF macro version number");
25270 if (DWARF_OFFSET_SIZE == 8)
25271 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25272 else
25273 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25275 break;
25276 case DW_MACINFO_define:
25277 case DW_MACINFO_undef:
25278 output_macinfo_op (ref);
25279 ref->code = 0;
25280 ref->info = NULL;
25281 break;
25282 default:
25283 gcc_unreachable ();
25287 /* Set up for Dwarf output at the start of compilation. */
25289 static void
25290 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25292 /* This option is currently broken, see (PR53118 and PR46102). */
25293 if (flag_eliminate_dwarf2_dups
25294 && strstr (lang_hooks.name, "C++"))
25296 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25297 flag_eliminate_dwarf2_dups = 0;
25300 /* Allocate the file_table. */
25301 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25303 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25304 /* Allocate the decl_die_table. */
25305 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25307 /* Allocate the decl_loc_table. */
25308 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25310 /* Allocate the cached_dw_loc_list_table. */
25311 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25313 /* Allocate the initial hunk of the decl_scope_table. */
25314 vec_alloc (decl_scope_table, 256);
25316 /* Allocate the initial hunk of the abbrev_die_table. */
25317 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25318 (ABBREV_DIE_TABLE_INCREMENT);
25319 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25320 /* Zero-th entry is allocated, but unused. */
25321 abbrev_die_table_in_use = 1;
25323 /* Allocate the dwarf_proc_stack_usage_map. */
25324 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25326 /* Allocate the pubtypes and pubnames vectors. */
25327 vec_alloc (pubname_table, 32);
25328 vec_alloc (pubtype_table, 32);
25330 vec_alloc (incomplete_types, 64);
25332 vec_alloc (used_rtx_array, 32);
25334 if (!dwarf_split_debug_info)
25336 debug_info_section = get_section (DEBUG_INFO_SECTION,
25337 SECTION_DEBUG, NULL);
25338 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25339 SECTION_DEBUG, NULL);
25340 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25341 SECTION_DEBUG, NULL);
25343 else
25345 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25346 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25347 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25348 SECTION_DEBUG | SECTION_EXCLUDE,
25349 NULL);
25350 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25351 SECTION_DEBUG, NULL);
25352 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25353 SECTION_DEBUG, NULL);
25354 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25355 SECTION_DEBUG, NULL);
25356 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25357 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25359 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25360 the main .o, but the skeleton_line goes into the split off dwo. */
25361 debug_skeleton_line_section
25362 = get_section (DEBUG_DWO_LINE_SECTION,
25363 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25364 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25365 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25366 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
25367 SECTION_DEBUG | SECTION_EXCLUDE,
25368 NULL);
25369 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25370 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25371 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25372 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25373 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25374 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25376 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25377 SECTION_DEBUG, NULL);
25378 debug_macinfo_section = get_section (dwarf_strict
25379 ? DEBUG_MACINFO_SECTION
25380 : DEBUG_MACRO_SECTION,
25381 DEBUG_MACRO_SECTION_FLAGS, NULL);
25382 debug_line_section = get_section (DEBUG_LINE_SECTION,
25383 SECTION_DEBUG, NULL);
25384 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25385 SECTION_DEBUG, NULL);
25386 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25387 SECTION_DEBUG, NULL);
25388 debug_str_section = get_section (DEBUG_STR_SECTION,
25389 DEBUG_STR_SECTION_FLAGS, NULL);
25390 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25391 SECTION_DEBUG, NULL);
25392 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25393 SECTION_DEBUG, NULL);
25395 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25396 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25397 DEBUG_ABBREV_SECTION_LABEL, 0);
25398 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25399 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25400 COLD_TEXT_SECTION_LABEL, 0);
25401 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25403 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25404 DEBUG_INFO_SECTION_LABEL, 0);
25405 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25406 DEBUG_LINE_SECTION_LABEL, 0);
25407 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25408 DEBUG_RANGES_SECTION_LABEL, 0);
25409 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25410 DEBUG_ADDR_SECTION_LABEL, 0);
25411 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25412 dwarf_strict
25413 ? DEBUG_MACINFO_SECTION_LABEL
25414 : DEBUG_MACRO_SECTION_LABEL, 0);
25415 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25417 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25418 vec_alloc (macinfo_table, 64);
25420 switch_to_section (text_section);
25421 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25422 #endif
25424 /* Make sure the line number table for .text always exists. */
25425 text_section_line_info = new_line_info_table ();
25426 text_section_line_info->end_label = text_end_label;
25428 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25429 cur_line_info_table = text_section_line_info;
25430 #endif
25432 /* If front-ends already registered a main translation unit but we were not
25433 ready to perform the association, do this now. */
25434 if (main_translation_unit != NULL_TREE)
25435 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25438 /* Called before compile () starts outputtting functions, variables
25439 and toplevel asms into assembly. */
25441 static void
25442 dwarf2out_assembly_start (void)
25444 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25445 && dwarf2out_do_cfi_asm ()
25446 && (!(flag_unwind_tables || flag_exceptions)
25447 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25448 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25451 /* A helper function for dwarf2out_finish called through
25452 htab_traverse. Assign a string its index. All strings must be
25453 collected into the table by the time index_string is called,
25454 because the indexing code relies on htab_traverse to traverse nodes
25455 in the same order for each run. */
25458 index_string (indirect_string_node **h, unsigned int *index)
25460 indirect_string_node *node = *h;
25462 find_string_form (node);
25463 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25465 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25466 node->index = *index;
25467 *index += 1;
25469 return 1;
25472 /* A helper function for output_indirect_strings called through
25473 htab_traverse. Output the offset to a string and update the
25474 current offset. */
25477 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25479 indirect_string_node *node = *h;
25481 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25483 /* Assert that this node has been assigned an index. */
25484 gcc_assert (node->index != NO_INDEX_ASSIGNED
25485 && node->index != NOT_INDEXED);
25486 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25487 "indexed string 0x%x: %s", node->index, node->str);
25488 *offset += strlen (node->str) + 1;
25490 return 1;
25493 /* A helper function for dwarf2out_finish called through
25494 htab_traverse. Output the indexed string. */
25497 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25499 struct indirect_string_node *node = *h;
25501 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25503 /* Assert that the strings are output in the same order as their
25504 indexes were assigned. */
25505 gcc_assert (*cur_idx == node->index);
25506 assemble_string (node->str, strlen (node->str) + 1);
25507 *cur_idx += 1;
25509 return 1;
25512 /* A helper function for dwarf2out_finish called through
25513 htab_traverse. Emit one queued .debug_str string. */
25516 output_indirect_string (indirect_string_node **h, void *)
25518 struct indirect_string_node *node = *h;
25520 node->form = find_string_form (node);
25521 if (node->form == DW_FORM_strp && node->refcount > 0)
25523 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25524 assemble_string (node->str, strlen (node->str) + 1);
25527 return 1;
25530 /* Output the indexed string table. */
25532 static void
25533 output_indirect_strings (void)
25535 switch_to_section (debug_str_section);
25536 if (!dwarf_split_debug_info)
25537 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25538 else
25540 unsigned int offset = 0;
25541 unsigned int cur_idx = 0;
25543 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25545 switch_to_section (debug_str_offsets_section);
25546 debug_str_hash->traverse_noresize
25547 <unsigned int *, output_index_string_offset> (&offset);
25548 switch_to_section (debug_str_dwo_section);
25549 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25550 (&cur_idx);
25554 /* Callback for htab_traverse to assign an index to an entry in the
25555 table, and to write that entry to the .debug_addr section. */
25558 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25560 addr_table_entry *entry = *slot;
25562 if (entry->refcount == 0)
25564 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25565 || entry->index == NOT_INDEXED);
25566 return 1;
25569 gcc_assert (entry->index == *cur_index);
25570 (*cur_index)++;
25572 switch (entry->kind)
25574 case ate_kind_rtx:
25575 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25576 "0x%x", entry->index);
25577 break;
25578 case ate_kind_rtx_dtprel:
25579 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25580 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25581 DWARF2_ADDR_SIZE,
25582 entry->addr.rtl);
25583 fputc ('\n', asm_out_file);
25584 break;
25585 case ate_kind_label:
25586 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25587 "0x%x", entry->index);
25588 break;
25589 default:
25590 gcc_unreachable ();
25592 return 1;
25595 /* Produce the .debug_addr section. */
25597 static void
25598 output_addr_table (void)
25600 unsigned int index = 0;
25601 if (addr_index_table == NULL || addr_index_table->size () == 0)
25602 return;
25604 switch_to_section (debug_addr_section);
25605 addr_index_table
25606 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25609 #if ENABLE_ASSERT_CHECKING
25610 /* Verify that all marks are clear. */
25612 static void
25613 verify_marks_clear (dw_die_ref die)
25615 dw_die_ref c;
25617 gcc_assert (! die->die_mark);
25618 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25620 #endif /* ENABLE_ASSERT_CHECKING */
25622 /* Clear the marks for a die and its children.
25623 Be cool if the mark isn't set. */
25625 static void
25626 prune_unmark_dies (dw_die_ref die)
25628 dw_die_ref c;
25630 if (die->die_mark)
25631 die->die_mark = 0;
25632 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25635 /* Given LOC that is referenced by a DIE we're marking as used, find all
25636 referenced DWARF procedures it references and mark them as used. */
25638 static void
25639 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25641 for (; loc != NULL; loc = loc->dw_loc_next)
25642 switch (loc->dw_loc_opc)
25644 case DW_OP_GNU_implicit_pointer:
25645 case DW_OP_GNU_convert:
25646 case DW_OP_GNU_reinterpret:
25647 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25648 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25649 break;
25650 case DW_OP_call2:
25651 case DW_OP_call4:
25652 case DW_OP_call_ref:
25653 case DW_OP_GNU_const_type:
25654 case DW_OP_GNU_parameter_ref:
25655 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
25656 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25657 break;
25658 case DW_OP_GNU_regval_type:
25659 case DW_OP_GNU_deref_type:
25660 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
25661 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
25662 break;
25663 case DW_OP_GNU_entry_value:
25664 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
25665 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
25666 break;
25667 default:
25668 break;
25672 /* Given DIE that we're marking as used, find any other dies
25673 it references as attributes and mark them as used. */
25675 static void
25676 prune_unused_types_walk_attribs (dw_die_ref die)
25678 dw_attr_node *a;
25679 unsigned ix;
25681 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25683 switch (AT_class (a))
25685 /* Make sure DWARF procedures referenced by location descriptions will
25686 get emitted. */
25687 case dw_val_class_loc:
25688 prune_unused_types_walk_loc_descr (AT_loc (a));
25689 break;
25690 case dw_val_class_loc_list:
25691 for (dw_loc_list_ref list = AT_loc_list (a);
25692 list != NULL;
25693 list = list->dw_loc_next)
25694 prune_unused_types_walk_loc_descr (list->expr);
25695 break;
25697 case dw_val_class_die_ref:
25698 /* A reference to another DIE.
25699 Make sure that it will get emitted.
25700 If it was broken out into a comdat group, don't follow it. */
25701 if (! AT_ref (a)->comdat_type_p
25702 || a->dw_attr == DW_AT_specification)
25703 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
25704 break;
25706 case dw_val_class_str:
25707 /* Set the string's refcount to 0 so that prune_unused_types_mark
25708 accounts properly for it. */
25709 a->dw_attr_val.v.val_str->refcount = 0;
25710 break;
25712 default:
25713 break;
25718 /* Mark the generic parameters and arguments children DIEs of DIE. */
25720 static void
25721 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
25723 dw_die_ref c;
25725 if (die == NULL || die->die_child == NULL)
25726 return;
25727 c = die->die_child;
25730 if (is_template_parameter (c))
25731 prune_unused_types_mark (c, 1);
25732 c = c->die_sib;
25733 } while (c && c != die->die_child);
25736 /* Mark DIE as being used. If DOKIDS is true, then walk down
25737 to DIE's children. */
25739 static void
25740 prune_unused_types_mark (dw_die_ref die, int dokids)
25742 dw_die_ref c;
25744 if (die->die_mark == 0)
25746 /* We haven't done this node yet. Mark it as used. */
25747 die->die_mark = 1;
25748 /* If this is the DIE of a generic type instantiation,
25749 mark the children DIEs that describe its generic parms and
25750 args. */
25751 prune_unused_types_mark_generic_parms_dies (die);
25753 /* We also have to mark its parents as used.
25754 (But we don't want to mark our parent's kids due to this,
25755 unless it is a class.) */
25756 if (die->die_parent)
25757 prune_unused_types_mark (die->die_parent,
25758 class_scope_p (die->die_parent));
25760 /* Mark any referenced nodes. */
25761 prune_unused_types_walk_attribs (die);
25763 /* If this node is a specification,
25764 also mark the definition, if it exists. */
25765 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
25766 prune_unused_types_mark (die->die_definition, 1);
25769 if (dokids && die->die_mark != 2)
25771 /* We need to walk the children, but haven't done so yet.
25772 Remember that we've walked the kids. */
25773 die->die_mark = 2;
25775 /* If this is an array type, we need to make sure our
25776 kids get marked, even if they're types. If we're
25777 breaking out types into comdat sections, do this
25778 for all type definitions. */
25779 if (die->die_tag == DW_TAG_array_type
25780 || (use_debug_types
25781 && is_type_die (die) && ! is_declaration_die (die)))
25782 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
25783 else
25784 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25788 /* For local classes, look if any static member functions were emitted
25789 and if so, mark them. */
25791 static void
25792 prune_unused_types_walk_local_classes (dw_die_ref die)
25794 dw_die_ref c;
25796 if (die->die_mark == 2)
25797 return;
25799 switch (die->die_tag)
25801 case DW_TAG_structure_type:
25802 case DW_TAG_union_type:
25803 case DW_TAG_class_type:
25804 break;
25806 case DW_TAG_subprogram:
25807 if (!get_AT_flag (die, DW_AT_declaration)
25808 || die->die_definition != NULL)
25809 prune_unused_types_mark (die, 1);
25810 return;
25812 default:
25813 return;
25816 /* Mark children. */
25817 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
25820 /* Walk the tree DIE and mark types that we actually use. */
25822 static void
25823 prune_unused_types_walk (dw_die_ref die)
25825 dw_die_ref c;
25827 /* Don't do anything if this node is already marked and
25828 children have been marked as well. */
25829 if (die->die_mark == 2)
25830 return;
25832 switch (die->die_tag)
25834 case DW_TAG_structure_type:
25835 case DW_TAG_union_type:
25836 case DW_TAG_class_type:
25837 if (die->die_perennial_p)
25838 break;
25840 for (c = die->die_parent; c; c = c->die_parent)
25841 if (c->die_tag == DW_TAG_subprogram)
25842 break;
25844 /* Finding used static member functions inside of classes
25845 is needed just for local classes, because for other classes
25846 static member function DIEs with DW_AT_specification
25847 are emitted outside of the DW_TAG_*_type. If we ever change
25848 it, we'd need to call this even for non-local classes. */
25849 if (c)
25850 prune_unused_types_walk_local_classes (die);
25852 /* It's a type node --- don't mark it. */
25853 return;
25855 case DW_TAG_const_type:
25856 case DW_TAG_packed_type:
25857 case DW_TAG_pointer_type:
25858 case DW_TAG_reference_type:
25859 case DW_TAG_rvalue_reference_type:
25860 case DW_TAG_volatile_type:
25861 case DW_TAG_typedef:
25862 case DW_TAG_array_type:
25863 case DW_TAG_interface_type:
25864 case DW_TAG_friend:
25865 case DW_TAG_enumeration_type:
25866 case DW_TAG_subroutine_type:
25867 case DW_TAG_string_type:
25868 case DW_TAG_set_type:
25869 case DW_TAG_subrange_type:
25870 case DW_TAG_ptr_to_member_type:
25871 case DW_TAG_file_type:
25872 /* Type nodes are useful only when other DIEs reference them --- don't
25873 mark them. */
25874 /* FALLTHROUGH */
25876 case DW_TAG_dwarf_procedure:
25877 /* Likewise for DWARF procedures. */
25879 if (die->die_perennial_p)
25880 break;
25882 return;
25884 default:
25885 /* Mark everything else. */
25886 break;
25889 if (die->die_mark == 0)
25891 die->die_mark = 1;
25893 /* Now, mark any dies referenced from here. */
25894 prune_unused_types_walk_attribs (die);
25897 die->die_mark = 2;
25899 /* Mark children. */
25900 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
25903 /* Increment the string counts on strings referred to from DIE's
25904 attributes. */
25906 static void
25907 prune_unused_types_update_strings (dw_die_ref die)
25909 dw_attr_node *a;
25910 unsigned ix;
25912 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25913 if (AT_class (a) == dw_val_class_str)
25915 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
25916 s->refcount++;
25917 /* Avoid unnecessarily putting strings that are used less than
25918 twice in the hash table. */
25919 if (s->refcount
25920 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
25922 indirect_string_node **slot
25923 = debug_str_hash->find_slot_with_hash (s->str,
25924 htab_hash_string (s->str),
25925 INSERT);
25926 gcc_assert (*slot == NULL);
25927 *slot = s;
25932 /* Remove from the tree DIE any dies that aren't marked. */
25934 static void
25935 prune_unused_types_prune (dw_die_ref die)
25937 dw_die_ref c;
25939 gcc_assert (die->die_mark);
25940 prune_unused_types_update_strings (die);
25942 if (! die->die_child)
25943 return;
25945 c = die->die_child;
25946 do {
25947 dw_die_ref prev = c;
25948 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
25949 if (c == die->die_child)
25951 /* No marked children between 'prev' and the end of the list. */
25952 if (prev == c)
25953 /* No marked children at all. */
25954 die->die_child = NULL;
25955 else
25957 prev->die_sib = c->die_sib;
25958 die->die_child = prev;
25960 return;
25963 if (c != prev->die_sib)
25964 prev->die_sib = c;
25965 prune_unused_types_prune (c);
25966 } while (c != die->die_child);
25969 /* Remove dies representing declarations that we never use. */
25971 static void
25972 prune_unused_types (void)
25974 unsigned int i;
25975 limbo_die_node *node;
25976 comdat_type_node *ctnode;
25977 pubname_entry *pub;
25978 dw_die_ref base_type;
25980 #if ENABLE_ASSERT_CHECKING
25981 /* All the marks should already be clear. */
25982 verify_marks_clear (comp_unit_die ());
25983 for (node = limbo_die_list; node; node = node->next)
25984 verify_marks_clear (node->die);
25985 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25986 verify_marks_clear (ctnode->root_die);
25987 #endif /* ENABLE_ASSERT_CHECKING */
25989 /* Mark types that are used in global variables. */
25990 premark_types_used_by_global_vars ();
25992 /* Set the mark on nodes that are actually used. */
25993 prune_unused_types_walk (comp_unit_die ());
25994 for (node = limbo_die_list; node; node = node->next)
25995 prune_unused_types_walk (node->die);
25996 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
25998 prune_unused_types_walk (ctnode->root_die);
25999 prune_unused_types_mark (ctnode->type_die, 1);
26002 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26003 are unusual in that they are pubnames that are the children of pubtypes.
26004 They should only be marked via their parent DW_TAG_enumeration_type die,
26005 not as roots in themselves. */
26006 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26007 if (pub->die->die_tag != DW_TAG_enumerator)
26008 prune_unused_types_mark (pub->die, 1);
26009 for (i = 0; base_types.iterate (i, &base_type); i++)
26010 prune_unused_types_mark (base_type, 1);
26012 if (debug_str_hash)
26013 debug_str_hash->empty ();
26014 if (skeleton_debug_str_hash)
26015 skeleton_debug_str_hash->empty ();
26016 prune_unused_types_prune (comp_unit_die ());
26017 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26019 node = *pnode;
26020 if (!node->die->die_mark)
26021 *pnode = node->next;
26022 else
26024 prune_unused_types_prune (node->die);
26025 pnode = &node->next;
26028 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26029 prune_unused_types_prune (ctnode->root_die);
26031 /* Leave the marks clear. */
26032 prune_unmark_dies (comp_unit_die ());
26033 for (node = limbo_die_list; node; node = node->next)
26034 prune_unmark_dies (node->die);
26035 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26036 prune_unmark_dies (ctnode->root_die);
26039 /* Set the parameter to true if there are any relative pathnames in
26040 the file table. */
26042 file_table_relative_p (dwarf_file_data **slot, bool *p)
26044 struct dwarf_file_data *d = *slot;
26045 if (!IS_ABSOLUTE_PATH (d->filename))
26047 *p = true;
26048 return 0;
26050 return 1;
26053 /* Helpers to manipulate hash table of comdat type units. */
26055 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26057 static inline hashval_t hash (const comdat_type_node *);
26058 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26061 inline hashval_t
26062 comdat_type_hasher::hash (const comdat_type_node *type_node)
26064 hashval_t h;
26065 memcpy (&h, type_node->signature, sizeof (h));
26066 return h;
26069 inline bool
26070 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26071 const comdat_type_node *type_node_2)
26073 return (! memcmp (type_node_1->signature, type_node_2->signature,
26074 DWARF_TYPE_SIGNATURE_SIZE));
26077 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26078 to the location it would have been added, should we know its
26079 DECL_ASSEMBLER_NAME when we added other attributes. This will
26080 probably improve compactness of debug info, removing equivalent
26081 abbrevs, and hide any differences caused by deferring the
26082 computation of the assembler name, triggered by e.g. PCH. */
26084 static inline void
26085 move_linkage_attr (dw_die_ref die)
26087 unsigned ix = vec_safe_length (die->die_attr);
26088 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26090 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26091 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26093 while (--ix > 0)
26095 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26097 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26098 break;
26101 if (ix != vec_safe_length (die->die_attr) - 1)
26103 die->die_attr->pop ();
26104 die->die_attr->quick_insert (ix, linkage);
26108 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26109 referenced from typed stack ops and count how often they are used. */
26111 static void
26112 mark_base_types (dw_loc_descr_ref loc)
26114 dw_die_ref base_type = NULL;
26116 for (; loc; loc = loc->dw_loc_next)
26118 switch (loc->dw_loc_opc)
26120 case DW_OP_GNU_regval_type:
26121 case DW_OP_GNU_deref_type:
26122 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26123 break;
26124 case DW_OP_GNU_convert:
26125 case DW_OP_GNU_reinterpret:
26126 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26127 continue;
26128 /* FALLTHRU */
26129 case DW_OP_GNU_const_type:
26130 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26131 break;
26132 case DW_OP_GNU_entry_value:
26133 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26134 continue;
26135 default:
26136 continue;
26138 gcc_assert (base_type->die_parent == comp_unit_die ());
26139 if (base_type->die_mark)
26140 base_type->die_mark++;
26141 else
26143 base_types.safe_push (base_type);
26144 base_type->die_mark = 1;
26149 /* Comparison function for sorting marked base types. */
26151 static int
26152 base_type_cmp (const void *x, const void *y)
26154 dw_die_ref dx = *(const dw_die_ref *) x;
26155 dw_die_ref dy = *(const dw_die_ref *) y;
26156 unsigned int byte_size1, byte_size2;
26157 unsigned int encoding1, encoding2;
26158 if (dx->die_mark > dy->die_mark)
26159 return -1;
26160 if (dx->die_mark < dy->die_mark)
26161 return 1;
26162 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26163 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26164 if (byte_size1 < byte_size2)
26165 return 1;
26166 if (byte_size1 > byte_size2)
26167 return -1;
26168 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26169 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26170 if (encoding1 < encoding2)
26171 return 1;
26172 if (encoding1 > encoding2)
26173 return -1;
26174 return 0;
26177 /* Move base types marked by mark_base_types as early as possible
26178 in the CU, sorted by decreasing usage count both to make the
26179 uleb128 references as small as possible and to make sure they
26180 will have die_offset already computed by calc_die_sizes when
26181 sizes of typed stack loc ops is computed. */
26183 static void
26184 move_marked_base_types (void)
26186 unsigned int i;
26187 dw_die_ref base_type, die, c;
26189 if (base_types.is_empty ())
26190 return;
26192 /* Sort by decreasing usage count, they will be added again in that
26193 order later on. */
26194 base_types.qsort (base_type_cmp);
26195 die = comp_unit_die ();
26196 c = die->die_child;
26199 dw_die_ref prev = c;
26200 c = c->die_sib;
26201 while (c->die_mark)
26203 remove_child_with_prev (c, prev);
26204 /* As base types got marked, there must be at least
26205 one node other than DW_TAG_base_type. */
26206 gcc_assert (c != c->die_sib);
26207 c = c->die_sib;
26210 while (c != die->die_child);
26211 gcc_assert (die->die_child);
26212 c = die->die_child;
26213 for (i = 0; base_types.iterate (i, &base_type); i++)
26215 base_type->die_mark = 0;
26216 base_type->die_sib = c->die_sib;
26217 c->die_sib = base_type;
26218 c = base_type;
26222 /* Helper function for resolve_addr, attempt to resolve
26223 one CONST_STRING, return true if successful. Similarly verify that
26224 SYMBOL_REFs refer to variables emitted in the current CU. */
26226 static bool
26227 resolve_one_addr (rtx *addr)
26229 rtx rtl = *addr;
26231 if (GET_CODE (rtl) == CONST_STRING)
26233 size_t len = strlen (XSTR (rtl, 0)) + 1;
26234 tree t = build_string (len, XSTR (rtl, 0));
26235 tree tlen = size_int (len - 1);
26236 TREE_TYPE (t)
26237 = build_array_type (char_type_node, build_index_type (tlen));
26238 rtl = lookup_constant_def (t);
26239 if (!rtl || !MEM_P (rtl))
26240 return false;
26241 rtl = XEXP (rtl, 0);
26242 if (GET_CODE (rtl) == SYMBOL_REF
26243 && SYMBOL_REF_DECL (rtl)
26244 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26245 return false;
26246 vec_safe_push (used_rtx_array, rtl);
26247 *addr = rtl;
26248 return true;
26251 if (GET_CODE (rtl) == SYMBOL_REF
26252 && SYMBOL_REF_DECL (rtl))
26254 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26256 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26257 return false;
26259 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26260 return false;
26263 if (GET_CODE (rtl) == CONST)
26265 subrtx_ptr_iterator::array_type array;
26266 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26267 if (!resolve_one_addr (*iter))
26268 return false;
26271 return true;
26274 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26275 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26276 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26278 static rtx
26279 string_cst_pool_decl (tree t)
26281 rtx rtl = output_constant_def (t, 1);
26282 unsigned char *array;
26283 dw_loc_descr_ref l;
26284 tree decl;
26285 size_t len;
26286 dw_die_ref ref;
26288 if (!rtl || !MEM_P (rtl))
26289 return NULL_RTX;
26290 rtl = XEXP (rtl, 0);
26291 if (GET_CODE (rtl) != SYMBOL_REF
26292 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26293 return NULL_RTX;
26295 decl = SYMBOL_REF_DECL (rtl);
26296 if (!lookup_decl_die (decl))
26298 len = TREE_STRING_LENGTH (t);
26299 vec_safe_push (used_rtx_array, rtl);
26300 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26301 array = ggc_vec_alloc<unsigned char> (len);
26302 memcpy (array, TREE_STRING_POINTER (t), len);
26303 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26304 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26305 l->dw_loc_oprnd2.v.val_vec.length = len;
26306 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26307 l->dw_loc_oprnd2.v.val_vec.array = array;
26308 add_AT_loc (ref, DW_AT_location, l);
26309 equate_decl_number_to_die (decl, ref);
26311 return rtl;
26314 /* Helper function of resolve_addr_in_expr. LOC is
26315 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26316 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26317 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26318 with DW_OP_GNU_implicit_pointer if possible
26319 and return true, if unsuccessful, return false. */
26321 static bool
26322 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26324 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26325 HOST_WIDE_INT offset = 0;
26326 dw_die_ref ref = NULL;
26327 tree decl;
26329 if (GET_CODE (rtl) == CONST
26330 && GET_CODE (XEXP (rtl, 0)) == PLUS
26331 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26333 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26334 rtl = XEXP (XEXP (rtl, 0), 0);
26336 if (GET_CODE (rtl) == CONST_STRING)
26338 size_t len = strlen (XSTR (rtl, 0)) + 1;
26339 tree t = build_string (len, XSTR (rtl, 0));
26340 tree tlen = size_int (len - 1);
26342 TREE_TYPE (t)
26343 = build_array_type (char_type_node, build_index_type (tlen));
26344 rtl = string_cst_pool_decl (t);
26345 if (!rtl)
26346 return false;
26348 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26350 decl = SYMBOL_REF_DECL (rtl);
26351 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
26353 ref = lookup_decl_die (decl);
26354 if (ref && (get_AT (ref, DW_AT_location)
26355 || get_AT (ref, DW_AT_const_value)))
26357 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26358 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26359 loc->dw_loc_oprnd1.val_entry = NULL;
26360 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26361 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26362 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26363 loc->dw_loc_oprnd2.v.val_int = offset;
26364 return true;
26368 return false;
26371 /* Helper function for resolve_addr, handle one location
26372 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26373 the location list couldn't be resolved. */
26375 static bool
26376 resolve_addr_in_expr (dw_loc_descr_ref loc)
26378 dw_loc_descr_ref keep = NULL;
26379 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26380 switch (loc->dw_loc_opc)
26382 case DW_OP_addr:
26383 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26385 if ((prev == NULL
26386 || prev->dw_loc_opc == DW_OP_piece
26387 || prev->dw_loc_opc == DW_OP_bit_piece)
26388 && loc->dw_loc_next
26389 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26390 && !dwarf_strict
26391 && optimize_one_addr_into_implicit_ptr (loc))
26392 break;
26393 return false;
26395 break;
26396 case DW_OP_GNU_addr_index:
26397 case DW_OP_GNU_const_index:
26398 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26399 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26401 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26402 if (!resolve_one_addr (&rtl))
26403 return false;
26404 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26405 loc->dw_loc_oprnd1.val_entry =
26406 add_addr_table_entry (rtl, ate_kind_rtx);
26408 break;
26409 case DW_OP_const4u:
26410 case DW_OP_const8u:
26411 if (loc->dtprel
26412 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26413 return false;
26414 break;
26415 case DW_OP_plus_uconst:
26416 if (size_of_loc_descr (loc)
26417 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26419 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26421 dw_loc_descr_ref repl
26422 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26423 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26424 add_loc_descr (&repl, loc->dw_loc_next);
26425 *loc = *repl;
26427 break;
26428 case DW_OP_implicit_value:
26429 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26430 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26431 return false;
26432 break;
26433 case DW_OP_GNU_implicit_pointer:
26434 case DW_OP_GNU_parameter_ref:
26435 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26437 dw_die_ref ref
26438 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26439 if (ref == NULL)
26440 return false;
26441 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26442 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26443 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26445 break;
26446 case DW_OP_GNU_const_type:
26447 case DW_OP_GNU_regval_type:
26448 case DW_OP_GNU_deref_type:
26449 case DW_OP_GNU_convert:
26450 case DW_OP_GNU_reinterpret:
26451 while (loc->dw_loc_next
26452 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26454 dw_die_ref base1, base2;
26455 unsigned enc1, enc2, size1, size2;
26456 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26457 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26458 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26459 else if (loc->dw_loc_oprnd1.val_class
26460 == dw_val_class_unsigned_const)
26461 break;
26462 else
26463 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26464 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26465 == dw_val_class_unsigned_const)
26466 break;
26467 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26468 gcc_assert (base1->die_tag == DW_TAG_base_type
26469 && base2->die_tag == DW_TAG_base_type);
26470 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26471 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26472 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26473 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26474 if (size1 == size2
26475 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26476 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26477 && loc != keep)
26478 || enc1 == enc2))
26480 /* Optimize away next DW_OP_GNU_convert after
26481 adjusting LOC's base type die reference. */
26482 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26483 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26484 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26485 else
26486 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26487 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26488 continue;
26490 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26491 point typed stack entry. */
26492 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26493 keep = loc->dw_loc_next;
26494 break;
26496 break;
26497 default:
26498 break;
26500 return true;
26503 /* Helper function of resolve_addr. DIE had DW_AT_location of
26504 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26505 and DW_OP_addr couldn't be resolved. resolve_addr has already
26506 removed the DW_AT_location attribute. This function attempts to
26507 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26508 to it or DW_AT_const_value attribute, if possible. */
26510 static void
26511 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26513 if (TREE_CODE (decl) != VAR_DECL
26514 || lookup_decl_die (decl) != die
26515 || DECL_EXTERNAL (decl)
26516 || !TREE_STATIC (decl)
26517 || DECL_INITIAL (decl) == NULL_TREE
26518 || DECL_P (DECL_INITIAL (decl))
26519 || get_AT (die, DW_AT_const_value))
26520 return;
26522 tree init = DECL_INITIAL (decl);
26523 HOST_WIDE_INT offset = 0;
26524 /* For variables that have been optimized away and thus
26525 don't have a memory location, see if we can emit
26526 DW_AT_const_value instead. */
26527 if (tree_add_const_value_attribute (die, init))
26528 return;
26529 if (dwarf_strict)
26530 return;
26531 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26532 and ADDR_EXPR refers to a decl that has DW_AT_location or
26533 DW_AT_const_value (but isn't addressable, otherwise
26534 resolving the original DW_OP_addr wouldn't fail), see if
26535 we can add DW_OP_GNU_implicit_pointer. */
26536 STRIP_NOPS (init);
26537 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26538 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26540 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26541 init = TREE_OPERAND (init, 0);
26542 STRIP_NOPS (init);
26544 if (TREE_CODE (init) != ADDR_EXPR)
26545 return;
26546 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26547 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26548 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26549 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26550 && TREE_OPERAND (init, 0) != decl))
26552 dw_die_ref ref;
26553 dw_loc_descr_ref l;
26555 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26557 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26558 if (!rtl)
26559 return;
26560 decl = SYMBOL_REF_DECL (rtl);
26562 else
26563 decl = TREE_OPERAND (init, 0);
26564 ref = lookup_decl_die (decl);
26565 if (ref == NULL
26566 || (!get_AT (ref, DW_AT_location)
26567 && !get_AT (ref, DW_AT_const_value)))
26568 return;
26569 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26570 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26571 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26572 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26573 add_AT_loc (die, DW_AT_location, l);
26577 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
26578 an address in .rodata section if the string literal is emitted there,
26579 or remove the containing location list or replace DW_AT_const_value
26580 with DW_AT_location and empty location expression, if it isn't found
26581 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
26582 to something that has been emitted in the current CU. */
26584 static void
26585 resolve_addr (dw_die_ref die)
26587 dw_die_ref c;
26588 dw_attr_node *a;
26589 dw_loc_list_ref *curr, *start, loc;
26590 unsigned ix;
26592 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26593 switch (AT_class (a))
26595 case dw_val_class_loc_list:
26596 start = curr = AT_loc_list_ptr (a);
26597 loc = *curr;
26598 gcc_assert (loc);
26599 /* The same list can be referenced more than once. See if we have
26600 already recorded the result from a previous pass. */
26601 if (loc->replaced)
26602 *curr = loc->dw_loc_next;
26603 else if (!loc->resolved_addr)
26605 /* As things stand, we do not expect or allow one die to
26606 reference a suffix of another die's location list chain.
26607 References must be identical or completely separate.
26608 There is therefore no need to cache the result of this
26609 pass on any list other than the first; doing so
26610 would lead to unnecessary writes. */
26611 while (*curr)
26613 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
26614 if (!resolve_addr_in_expr ((*curr)->expr))
26616 dw_loc_list_ref next = (*curr)->dw_loc_next;
26617 dw_loc_descr_ref l = (*curr)->expr;
26619 if (next && (*curr)->ll_symbol)
26621 gcc_assert (!next->ll_symbol);
26622 next->ll_symbol = (*curr)->ll_symbol;
26624 if (dwarf_split_debug_info)
26625 remove_loc_list_addr_table_entries (l);
26626 *curr = next;
26628 else
26630 mark_base_types ((*curr)->expr);
26631 curr = &(*curr)->dw_loc_next;
26634 if (loc == *start)
26635 loc->resolved_addr = 1;
26636 else
26638 loc->replaced = 1;
26639 loc->dw_loc_next = *start;
26642 if (!*start)
26644 remove_AT (die, a->dw_attr);
26645 ix--;
26647 break;
26648 case dw_val_class_loc:
26650 dw_loc_descr_ref l = AT_loc (a);
26651 /* For -gdwarf-2 don't attempt to optimize
26652 DW_AT_data_member_location containing
26653 DW_OP_plus_uconst - older consumers might
26654 rely on it being that op instead of a more complex,
26655 but shorter, location description. */
26656 if ((dwarf_version > 2
26657 || a->dw_attr != DW_AT_data_member_location
26658 || l == NULL
26659 || l->dw_loc_opc != DW_OP_plus_uconst
26660 || l->dw_loc_next != NULL)
26661 && !resolve_addr_in_expr (l))
26663 if (dwarf_split_debug_info)
26664 remove_loc_list_addr_table_entries (l);
26665 if (l != NULL
26666 && l->dw_loc_next == NULL
26667 && l->dw_loc_opc == DW_OP_addr
26668 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
26669 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
26670 && a->dw_attr == DW_AT_location)
26672 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
26673 remove_AT (die, a->dw_attr);
26674 ix--;
26675 optimize_location_into_implicit_ptr (die, decl);
26676 break;
26678 remove_AT (die, a->dw_attr);
26679 ix--;
26681 else
26682 mark_base_types (l);
26684 break;
26685 case dw_val_class_addr:
26686 if (a->dw_attr == DW_AT_const_value
26687 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
26689 if (AT_index (a) != NOT_INDEXED)
26690 remove_addr_table_entry (a->dw_attr_val.val_entry);
26691 remove_AT (die, a->dw_attr);
26692 ix--;
26694 if (die->die_tag == DW_TAG_GNU_call_site
26695 && a->dw_attr == DW_AT_abstract_origin)
26697 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
26698 dw_die_ref tdie = lookup_decl_die (tdecl);
26699 dw_die_ref cdie;
26700 if (tdie == NULL
26701 && DECL_EXTERNAL (tdecl)
26702 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
26703 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
26705 /* Creating a full DIE for tdecl is overly expensive and
26706 at this point even wrong when in the LTO phase
26707 as it can end up generating new type DIEs we didn't
26708 output and thus optimize_external_refs will crash. */
26709 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
26710 add_AT_flag (tdie, DW_AT_external, 1);
26711 add_AT_flag (tdie, DW_AT_declaration, 1);
26712 add_linkage_attr (tdie, tdecl);
26713 add_name_and_src_coords_attributes (tdie, tdecl);
26714 equate_decl_number_to_die (tdecl, tdie);
26716 if (tdie)
26718 a->dw_attr_val.val_class = dw_val_class_die_ref;
26719 a->dw_attr_val.v.val_die_ref.die = tdie;
26720 a->dw_attr_val.v.val_die_ref.external = 0;
26722 else
26724 if (AT_index (a) != NOT_INDEXED)
26725 remove_addr_table_entry (a->dw_attr_val.val_entry);
26726 remove_AT (die, a->dw_attr);
26727 ix--;
26730 break;
26731 default:
26732 break;
26735 FOR_EACH_CHILD (die, c, resolve_addr (c));
26738 /* Helper routines for optimize_location_lists.
26739 This pass tries to share identical local lists in .debug_loc
26740 section. */
26742 /* Iteratively hash operands of LOC opcode into HSTATE. */
26744 static void
26745 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
26747 dw_val_ref val1 = &loc->dw_loc_oprnd1;
26748 dw_val_ref val2 = &loc->dw_loc_oprnd2;
26750 switch (loc->dw_loc_opc)
26752 case DW_OP_const4u:
26753 case DW_OP_const8u:
26754 if (loc->dtprel)
26755 goto hash_addr;
26756 /* FALLTHRU */
26757 case DW_OP_const1u:
26758 case DW_OP_const1s:
26759 case DW_OP_const2u:
26760 case DW_OP_const2s:
26761 case DW_OP_const4s:
26762 case DW_OP_const8s:
26763 case DW_OP_constu:
26764 case DW_OP_consts:
26765 case DW_OP_pick:
26766 case DW_OP_plus_uconst:
26767 case DW_OP_breg0:
26768 case DW_OP_breg1:
26769 case DW_OP_breg2:
26770 case DW_OP_breg3:
26771 case DW_OP_breg4:
26772 case DW_OP_breg5:
26773 case DW_OP_breg6:
26774 case DW_OP_breg7:
26775 case DW_OP_breg8:
26776 case DW_OP_breg9:
26777 case DW_OP_breg10:
26778 case DW_OP_breg11:
26779 case DW_OP_breg12:
26780 case DW_OP_breg13:
26781 case DW_OP_breg14:
26782 case DW_OP_breg15:
26783 case DW_OP_breg16:
26784 case DW_OP_breg17:
26785 case DW_OP_breg18:
26786 case DW_OP_breg19:
26787 case DW_OP_breg20:
26788 case DW_OP_breg21:
26789 case DW_OP_breg22:
26790 case DW_OP_breg23:
26791 case DW_OP_breg24:
26792 case DW_OP_breg25:
26793 case DW_OP_breg26:
26794 case DW_OP_breg27:
26795 case DW_OP_breg28:
26796 case DW_OP_breg29:
26797 case DW_OP_breg30:
26798 case DW_OP_breg31:
26799 case DW_OP_regx:
26800 case DW_OP_fbreg:
26801 case DW_OP_piece:
26802 case DW_OP_deref_size:
26803 case DW_OP_xderef_size:
26804 hstate.add_object (val1->v.val_int);
26805 break;
26806 case DW_OP_skip:
26807 case DW_OP_bra:
26809 int offset;
26811 gcc_assert (val1->val_class == dw_val_class_loc);
26812 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
26813 hstate.add_object (offset);
26815 break;
26816 case DW_OP_implicit_value:
26817 hstate.add_object (val1->v.val_unsigned);
26818 switch (val2->val_class)
26820 case dw_val_class_const:
26821 hstate.add_object (val2->v.val_int);
26822 break;
26823 case dw_val_class_vec:
26825 unsigned int elt_size = val2->v.val_vec.elt_size;
26826 unsigned int len = val2->v.val_vec.length;
26828 hstate.add_int (elt_size);
26829 hstate.add_int (len);
26830 hstate.add (val2->v.val_vec.array, len * elt_size);
26832 break;
26833 case dw_val_class_const_double:
26834 hstate.add_object (val2->v.val_double.low);
26835 hstate.add_object (val2->v.val_double.high);
26836 break;
26837 case dw_val_class_wide_int:
26838 hstate.add (val2->v.val_wide->get_val (),
26839 get_full_len (*val2->v.val_wide)
26840 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26841 break;
26842 case dw_val_class_addr:
26843 inchash::add_rtx (val2->v.val_addr, hstate);
26844 break;
26845 default:
26846 gcc_unreachable ();
26848 break;
26849 case DW_OP_bregx:
26850 case DW_OP_bit_piece:
26851 hstate.add_object (val1->v.val_int);
26852 hstate.add_object (val2->v.val_int);
26853 break;
26854 case DW_OP_addr:
26855 hash_addr:
26856 if (loc->dtprel)
26858 unsigned char dtprel = 0xd1;
26859 hstate.add_object (dtprel);
26861 inchash::add_rtx (val1->v.val_addr, hstate);
26862 break;
26863 case DW_OP_GNU_addr_index:
26864 case DW_OP_GNU_const_index:
26866 if (loc->dtprel)
26868 unsigned char dtprel = 0xd1;
26869 hstate.add_object (dtprel);
26871 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
26873 break;
26874 case DW_OP_GNU_implicit_pointer:
26875 hstate.add_int (val2->v.val_int);
26876 break;
26877 case DW_OP_GNU_entry_value:
26878 hstate.add_object (val1->v.val_loc);
26879 break;
26880 case DW_OP_GNU_regval_type:
26881 case DW_OP_GNU_deref_type:
26883 unsigned int byte_size
26884 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
26885 unsigned int encoding
26886 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
26887 hstate.add_object (val1->v.val_int);
26888 hstate.add_object (byte_size);
26889 hstate.add_object (encoding);
26891 break;
26892 case DW_OP_GNU_convert:
26893 case DW_OP_GNU_reinterpret:
26894 if (val1->val_class == dw_val_class_unsigned_const)
26896 hstate.add_object (val1->v.val_unsigned);
26897 break;
26899 /* FALLTHRU */
26900 case DW_OP_GNU_const_type:
26902 unsigned int byte_size
26903 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
26904 unsigned int encoding
26905 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
26906 hstate.add_object (byte_size);
26907 hstate.add_object (encoding);
26908 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
26909 break;
26910 hstate.add_object (val2->val_class);
26911 switch (val2->val_class)
26913 case dw_val_class_const:
26914 hstate.add_object (val2->v.val_int);
26915 break;
26916 case dw_val_class_vec:
26918 unsigned int elt_size = val2->v.val_vec.elt_size;
26919 unsigned int len = val2->v.val_vec.length;
26921 hstate.add_object (elt_size);
26922 hstate.add_object (len);
26923 hstate.add (val2->v.val_vec.array, len * elt_size);
26925 break;
26926 case dw_val_class_const_double:
26927 hstate.add_object (val2->v.val_double.low);
26928 hstate.add_object (val2->v.val_double.high);
26929 break;
26930 case dw_val_class_wide_int:
26931 hstate.add (val2->v.val_wide->get_val (),
26932 get_full_len (*val2->v.val_wide)
26933 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
26934 break;
26935 default:
26936 gcc_unreachable ();
26939 break;
26941 default:
26942 /* Other codes have no operands. */
26943 break;
26947 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
26949 static inline void
26950 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
26952 dw_loc_descr_ref l;
26953 bool sizes_computed = false;
26954 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
26955 size_of_locs (loc);
26957 for (l = loc; l != NULL; l = l->dw_loc_next)
26959 enum dwarf_location_atom opc = l->dw_loc_opc;
26960 hstate.add_object (opc);
26961 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
26963 size_of_locs (loc);
26964 sizes_computed = true;
26966 hash_loc_operands (l, hstate);
26970 /* Compute hash of the whole location list LIST_HEAD. */
26972 static inline void
26973 hash_loc_list (dw_loc_list_ref list_head)
26975 dw_loc_list_ref curr = list_head;
26976 inchash::hash hstate;
26978 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
26980 hstate.add (curr->begin, strlen (curr->begin) + 1);
26981 hstate.add (curr->end, strlen (curr->end) + 1);
26982 if (curr->section)
26983 hstate.add (curr->section, strlen (curr->section) + 1);
26984 hash_locs (curr->expr, hstate);
26986 list_head->hash = hstate.end ();
26989 /* Return true if X and Y opcodes have the same operands. */
26991 static inline bool
26992 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
26994 dw_val_ref valx1 = &x->dw_loc_oprnd1;
26995 dw_val_ref valx2 = &x->dw_loc_oprnd2;
26996 dw_val_ref valy1 = &y->dw_loc_oprnd1;
26997 dw_val_ref valy2 = &y->dw_loc_oprnd2;
26999 switch (x->dw_loc_opc)
27001 case DW_OP_const4u:
27002 case DW_OP_const8u:
27003 if (x->dtprel)
27004 goto hash_addr;
27005 /* FALLTHRU */
27006 case DW_OP_const1u:
27007 case DW_OP_const1s:
27008 case DW_OP_const2u:
27009 case DW_OP_const2s:
27010 case DW_OP_const4s:
27011 case DW_OP_const8s:
27012 case DW_OP_constu:
27013 case DW_OP_consts:
27014 case DW_OP_pick:
27015 case DW_OP_plus_uconst:
27016 case DW_OP_breg0:
27017 case DW_OP_breg1:
27018 case DW_OP_breg2:
27019 case DW_OP_breg3:
27020 case DW_OP_breg4:
27021 case DW_OP_breg5:
27022 case DW_OP_breg6:
27023 case DW_OP_breg7:
27024 case DW_OP_breg8:
27025 case DW_OP_breg9:
27026 case DW_OP_breg10:
27027 case DW_OP_breg11:
27028 case DW_OP_breg12:
27029 case DW_OP_breg13:
27030 case DW_OP_breg14:
27031 case DW_OP_breg15:
27032 case DW_OP_breg16:
27033 case DW_OP_breg17:
27034 case DW_OP_breg18:
27035 case DW_OP_breg19:
27036 case DW_OP_breg20:
27037 case DW_OP_breg21:
27038 case DW_OP_breg22:
27039 case DW_OP_breg23:
27040 case DW_OP_breg24:
27041 case DW_OP_breg25:
27042 case DW_OP_breg26:
27043 case DW_OP_breg27:
27044 case DW_OP_breg28:
27045 case DW_OP_breg29:
27046 case DW_OP_breg30:
27047 case DW_OP_breg31:
27048 case DW_OP_regx:
27049 case DW_OP_fbreg:
27050 case DW_OP_piece:
27051 case DW_OP_deref_size:
27052 case DW_OP_xderef_size:
27053 return valx1->v.val_int == valy1->v.val_int;
27054 case DW_OP_skip:
27055 case DW_OP_bra:
27056 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27057 can cause irrelevant differences in dw_loc_addr. */
27058 gcc_assert (valx1->val_class == dw_val_class_loc
27059 && valy1->val_class == dw_val_class_loc
27060 && (dwarf_split_debug_info
27061 || x->dw_loc_addr == y->dw_loc_addr));
27062 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27063 case DW_OP_implicit_value:
27064 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27065 || valx2->val_class != valy2->val_class)
27066 return false;
27067 switch (valx2->val_class)
27069 case dw_val_class_const:
27070 return valx2->v.val_int == valy2->v.val_int;
27071 case dw_val_class_vec:
27072 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27073 && valx2->v.val_vec.length == valy2->v.val_vec.length
27074 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27075 valx2->v.val_vec.elt_size
27076 * valx2->v.val_vec.length) == 0;
27077 case dw_val_class_const_double:
27078 return valx2->v.val_double.low == valy2->v.val_double.low
27079 && valx2->v.val_double.high == valy2->v.val_double.high;
27080 case dw_val_class_wide_int:
27081 return *valx2->v.val_wide == *valy2->v.val_wide;
27082 case dw_val_class_addr:
27083 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27084 default:
27085 gcc_unreachable ();
27087 case DW_OP_bregx:
27088 case DW_OP_bit_piece:
27089 return valx1->v.val_int == valy1->v.val_int
27090 && valx2->v.val_int == valy2->v.val_int;
27091 case DW_OP_addr:
27092 hash_addr:
27093 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27094 case DW_OP_GNU_addr_index:
27095 case DW_OP_GNU_const_index:
27097 rtx ax1 = valx1->val_entry->addr.rtl;
27098 rtx ay1 = valy1->val_entry->addr.rtl;
27099 return rtx_equal_p (ax1, ay1);
27101 case DW_OP_GNU_implicit_pointer:
27102 return valx1->val_class == dw_val_class_die_ref
27103 && valx1->val_class == valy1->val_class
27104 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27105 && valx2->v.val_int == valy2->v.val_int;
27106 case DW_OP_GNU_entry_value:
27107 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27108 case DW_OP_GNU_const_type:
27109 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27110 || valx2->val_class != valy2->val_class)
27111 return false;
27112 switch (valx2->val_class)
27114 case dw_val_class_const:
27115 return valx2->v.val_int == valy2->v.val_int;
27116 case dw_val_class_vec:
27117 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27118 && valx2->v.val_vec.length == valy2->v.val_vec.length
27119 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27120 valx2->v.val_vec.elt_size
27121 * valx2->v.val_vec.length) == 0;
27122 case dw_val_class_const_double:
27123 return valx2->v.val_double.low == valy2->v.val_double.low
27124 && valx2->v.val_double.high == valy2->v.val_double.high;
27125 case dw_val_class_wide_int:
27126 return *valx2->v.val_wide == *valy2->v.val_wide;
27127 default:
27128 gcc_unreachable ();
27130 case DW_OP_GNU_regval_type:
27131 case DW_OP_GNU_deref_type:
27132 return valx1->v.val_int == valy1->v.val_int
27133 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27134 case DW_OP_GNU_convert:
27135 case DW_OP_GNU_reinterpret:
27136 if (valx1->val_class != valy1->val_class)
27137 return false;
27138 if (valx1->val_class == dw_val_class_unsigned_const)
27139 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27140 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27141 case DW_OP_GNU_parameter_ref:
27142 return valx1->val_class == dw_val_class_die_ref
27143 && valx1->val_class == valy1->val_class
27144 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27145 default:
27146 /* Other codes have no operands. */
27147 return true;
27151 /* Return true if DWARF location expressions X and Y are the same. */
27153 static inline bool
27154 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27156 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27157 if (x->dw_loc_opc != y->dw_loc_opc
27158 || x->dtprel != y->dtprel
27159 || !compare_loc_operands (x, y))
27160 break;
27161 return x == NULL && y == NULL;
27164 /* Hashtable helpers. */
27166 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27168 static inline hashval_t hash (const dw_loc_list_struct *);
27169 static inline bool equal (const dw_loc_list_struct *,
27170 const dw_loc_list_struct *);
27173 /* Return precomputed hash of location list X. */
27175 inline hashval_t
27176 loc_list_hasher::hash (const dw_loc_list_struct *x)
27178 return x->hash;
27181 /* Return true if location lists A and B are the same. */
27183 inline bool
27184 loc_list_hasher::equal (const dw_loc_list_struct *a,
27185 const dw_loc_list_struct *b)
27187 if (a == b)
27188 return 1;
27189 if (a->hash != b->hash)
27190 return 0;
27191 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27192 if (strcmp (a->begin, b->begin) != 0
27193 || strcmp (a->end, b->end) != 0
27194 || (a->section == NULL) != (b->section == NULL)
27195 || (a->section && strcmp (a->section, b->section) != 0)
27196 || !compare_locs (a->expr, b->expr))
27197 break;
27198 return a == NULL && b == NULL;
27201 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27204 /* Recursively optimize location lists referenced from DIE
27205 children and share them whenever possible. */
27207 static void
27208 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27210 dw_die_ref c;
27211 dw_attr_node *a;
27212 unsigned ix;
27213 dw_loc_list_struct **slot;
27215 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27216 if (AT_class (a) == dw_val_class_loc_list)
27218 dw_loc_list_ref list = AT_loc_list (a);
27219 /* TODO: perform some optimizations here, before hashing
27220 it and storing into the hash table. */
27221 hash_loc_list (list);
27222 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27223 if (*slot == NULL)
27224 *slot = list;
27225 else
27226 a->dw_attr_val.v.val_loc_list = *slot;
27229 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27233 /* Recursively assign each location list a unique index into the debug_addr
27234 section. */
27236 static void
27237 index_location_lists (dw_die_ref die)
27239 dw_die_ref c;
27240 dw_attr_node *a;
27241 unsigned ix;
27243 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27244 if (AT_class (a) == dw_val_class_loc_list)
27246 dw_loc_list_ref list = AT_loc_list (a);
27247 dw_loc_list_ref curr;
27248 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27250 /* Don't index an entry that has already been indexed
27251 or won't be output. */
27252 if (curr->begin_entry != NULL
27253 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27254 continue;
27256 curr->begin_entry
27257 = add_addr_table_entry (xstrdup (curr->begin),
27258 ate_kind_label);
27262 FOR_EACH_CHILD (die, c, index_location_lists (c));
27265 /* Optimize location lists referenced from DIE
27266 children and share them whenever possible. */
27268 static void
27269 optimize_location_lists (dw_die_ref die)
27271 loc_list_hash_type htab (500);
27272 optimize_location_lists_1 (die, &htab);
27275 /* Traverse the limbo die list, and add parent/child links. The only
27276 dies without parents that should be here are concrete instances of
27277 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27278 For concrete instances, we can get the parent die from the abstract
27279 instance. */
27281 static void
27282 flush_limbo_die_list (void)
27284 limbo_die_node *node;
27286 /* get_context_die calls force_decl_die, which can put new DIEs on the
27287 limbo list in LTO mode when nested functions are put in a different
27288 partition than that of their parent function. */
27289 while ((node = limbo_die_list))
27291 dw_die_ref die = node->die;
27292 limbo_die_list = node->next;
27294 if (die->die_parent == NULL)
27296 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27298 if (origin && origin->die_parent)
27299 add_child_die (origin->die_parent, die);
27300 else if (is_cu_die (die))
27302 else if (seen_error ())
27303 /* It's OK to be confused by errors in the input. */
27304 add_child_die (comp_unit_die (), die);
27305 else
27307 /* In certain situations, the lexical block containing a
27308 nested function can be optimized away, which results
27309 in the nested function die being orphaned. Likewise
27310 with the return type of that nested function. Force
27311 this to be a child of the containing function.
27313 It may happen that even the containing function got fully
27314 inlined and optimized out. In that case we are lost and
27315 assign the empty child. This should not be big issue as
27316 the function is likely unreachable too. */
27317 gcc_assert (node->created_for);
27319 if (DECL_P (node->created_for))
27320 origin = get_context_die (DECL_CONTEXT (node->created_for));
27321 else if (TYPE_P (node->created_for))
27322 origin = scope_die_for (node->created_for, comp_unit_die ());
27323 else
27324 origin = comp_unit_die ();
27326 add_child_die (origin, die);
27332 /* Output stuff that dwarf requires at the end of every file,
27333 and generate the DWARF-2 debugging info. */
27335 static void
27336 dwarf2out_finish (const char *filename)
27338 comdat_type_node *ctnode;
27339 dw_die_ref main_comp_unit_die;
27341 /* Flush out any latecomers to the limbo party. */
27342 flush_limbo_die_list ();
27344 /* We shouldn't have any symbols with delayed asm names for
27345 DIEs generated after early finish. */
27346 gcc_assert (deferred_asm_name == NULL);
27348 /* PCH might result in DW_AT_producer string being restored from the
27349 header compilation, so always fill it with empty string initially
27350 and overwrite only here. */
27351 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
27352 producer_string = gen_producer_string ();
27353 producer->dw_attr_val.v.val_str->refcount--;
27354 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
27356 gen_remaining_tmpl_value_param_die_attribute ();
27358 /* Add the name for the main input file now. We delayed this from
27359 dwarf2out_init to avoid complications with PCH.
27360 For LTO produced units use a fixed artificial name to avoid
27361 leaking tempfile names into the dwarf. */
27362 if (!in_lto_p)
27363 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
27364 else
27365 add_name_attribute (comp_unit_die (), "<artificial>");
27366 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
27367 add_comp_dir_attribute (comp_unit_die ());
27368 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
27370 bool p = false;
27371 file_table->traverse<bool *, file_table_relative_p> (&p);
27372 if (p)
27373 add_comp_dir_attribute (comp_unit_die ());
27376 #if ENABLE_ASSERT_CHECKING
27378 dw_die_ref die = comp_unit_die (), c;
27379 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27381 #endif
27382 resolve_addr (comp_unit_die ());
27383 move_marked_base_types ();
27385 /* Walk through the list of incomplete types again, trying once more to
27386 emit full debugging info for them. */
27387 retry_incomplete_types ();
27389 if (flag_eliminate_unused_debug_types)
27390 prune_unused_types ();
27392 /* Generate separate COMDAT sections for type DIEs. */
27393 if (use_debug_types)
27395 break_out_comdat_types (comp_unit_die ());
27397 /* Each new type_unit DIE was added to the limbo die list when created.
27398 Since these have all been added to comdat_type_list, clear the
27399 limbo die list. */
27400 limbo_die_list = NULL;
27402 /* For each new comdat type unit, copy declarations for incomplete
27403 types to make the new unit self-contained (i.e., no direct
27404 references to the main compile unit). */
27405 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27406 copy_decls_for_unworthy_types (ctnode->root_die);
27407 copy_decls_for_unworthy_types (comp_unit_die ());
27409 /* In the process of copying declarations from one unit to another,
27410 we may have left some declarations behind that are no longer
27411 referenced. Prune them. */
27412 prune_unused_types ();
27415 /* Generate separate CUs for each of the include files we've seen.
27416 They will go into limbo_die_list. */
27417 if (flag_eliminate_dwarf2_dups)
27418 break_out_includes (comp_unit_die ());
27420 /* Traverse the DIE's and add sibling attributes to those DIE's that
27421 have children. */
27422 add_sibling_attributes (comp_unit_die ());
27423 limbo_die_node *node;
27424 for (node = limbo_die_list; node; node = node->next)
27425 add_sibling_attributes (node->die);
27426 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27427 add_sibling_attributes (ctnode->root_die);
27429 /* When splitting DWARF info, we put some attributes in the
27430 skeleton compile_unit DIE that remains in the .o, while
27431 most attributes go in the DWO compile_unit_die. */
27432 if (dwarf_split_debug_info)
27433 main_comp_unit_die = gen_compile_unit_die (NULL);
27434 else
27435 main_comp_unit_die = comp_unit_die ();
27437 /* Output a terminator label for the .text section. */
27438 switch_to_section (text_section);
27439 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27440 if (cold_text_section)
27442 switch_to_section (cold_text_section);
27443 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27446 /* We can only use the low/high_pc attributes if all of the code was
27447 in .text. */
27448 if (!have_multiple_function_sections
27449 || (dwarf_version < 3 && dwarf_strict))
27451 /* Don't add if the CU has no associated code. */
27452 if (text_section_used)
27453 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27454 text_end_label, true);
27456 else
27458 unsigned fde_idx;
27459 dw_fde_ref fde;
27460 bool range_list_added = false;
27462 if (text_section_used)
27463 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27464 text_end_label, &range_list_added, true);
27465 if (cold_text_section_used)
27466 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27467 cold_end_label, &range_list_added, true);
27469 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
27471 if (DECL_IGNORED_P (fde->decl))
27472 continue;
27473 if (!fde->in_std_section)
27474 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
27475 fde->dw_fde_end, &range_list_added,
27476 true);
27477 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
27478 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
27479 fde->dw_fde_second_end, &range_list_added,
27480 true);
27483 if (range_list_added)
27485 /* We need to give .debug_loc and .debug_ranges an appropriate
27486 "base address". Use zero so that these addresses become
27487 absolute. Historically, we've emitted the unexpected
27488 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
27489 Emit both to give time for other tools to adapt. */
27490 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
27491 if (! dwarf_strict && dwarf_version < 4)
27492 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
27494 add_ranges (NULL);
27498 if (debug_info_level >= DINFO_LEVEL_TERSE)
27499 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
27500 debug_line_section_label);
27502 if (have_macinfo)
27503 add_AT_macptr (comp_unit_die (),
27504 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
27505 macinfo_section_label);
27507 if (dwarf_split_debug_info)
27509 /* optimize_location_lists calculates the size of the lists,
27510 so index them first, and assign indices to the entries.
27511 Although optimize_location_lists will remove entries from
27512 the table, it only does so for duplicates, and therefore
27513 only reduces ref_counts to 1. */
27514 index_location_lists (comp_unit_die ());
27516 if (addr_index_table != NULL)
27518 unsigned int index = 0;
27519 addr_index_table
27520 ->traverse_noresize<unsigned int *, index_addr_table_entry>
27521 (&index);
27525 if (have_location_lists)
27526 optimize_location_lists (comp_unit_die ());
27528 save_macinfo_strings ();
27530 if (dwarf_split_debug_info)
27532 unsigned int index = 0;
27534 /* Add attributes common to skeleton compile_units and
27535 type_units. Because these attributes include strings, it
27536 must be done before freezing the string table. Top-level
27537 skeleton die attrs are added when the skeleton type unit is
27538 created, so ensure it is created by this point. */
27539 add_top_level_skeleton_die_attrs (main_comp_unit_die);
27540 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
27543 /* Output all of the compilation units. We put the main one last so that
27544 the offsets are available to output_pubnames. */
27545 for (node = limbo_die_list; node; node = node->next)
27546 output_comp_unit (node->die, 0);
27548 hash_table<comdat_type_hasher> comdat_type_table (100);
27549 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27551 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
27553 /* Don't output duplicate types. */
27554 if (*slot != HTAB_EMPTY_ENTRY)
27555 continue;
27557 /* Add a pointer to the line table for the main compilation unit
27558 so that the debugger can make sense of DW_AT_decl_file
27559 attributes. */
27560 if (debug_info_level >= DINFO_LEVEL_TERSE)
27561 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
27562 (!dwarf_split_debug_info
27563 ? debug_line_section_label
27564 : debug_skeleton_line_section_label));
27566 output_comdat_type_unit (ctnode);
27567 *slot = ctnode;
27570 /* The AT_pubnames attribute needs to go in all skeleton dies, including
27571 both the main_cu and all skeleton TUs. Making this call unconditional
27572 would end up either adding a second copy of the AT_pubnames attribute, or
27573 requiring a special case in add_top_level_skeleton_die_attrs. */
27574 if (!dwarf_split_debug_info)
27575 add_AT_pubnames (comp_unit_die ());
27577 if (dwarf_split_debug_info)
27579 int mark;
27580 unsigned char checksum[16];
27581 struct md5_ctx ctx;
27583 /* Compute a checksum of the comp_unit to use as the dwo_id. */
27584 md5_init_ctx (&ctx);
27585 mark = 0;
27586 die_checksum (comp_unit_die (), &ctx, &mark);
27587 unmark_all_dies (comp_unit_die ());
27588 md5_finish_ctx (&ctx, checksum);
27590 /* Use the first 8 bytes of the checksum as the dwo_id,
27591 and add it to both comp-unit DIEs. */
27592 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
27593 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
27595 /* Add the base offset of the ranges table to the skeleton
27596 comp-unit DIE. */
27597 if (ranges_table_in_use)
27598 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
27599 ranges_section_label);
27601 switch_to_section (debug_addr_section);
27602 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
27603 output_addr_table ();
27606 /* Output the main compilation unit if non-empty or if .debug_macinfo
27607 or .debug_macro will be emitted. */
27608 output_comp_unit (comp_unit_die (), have_macinfo);
27610 if (dwarf_split_debug_info && info_section_emitted)
27611 output_skeleton_debug_sections (main_comp_unit_die);
27613 /* Output the abbreviation table. */
27614 if (abbrev_die_table_in_use != 1)
27616 switch_to_section (debug_abbrev_section);
27617 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
27618 output_abbrev_section ();
27621 /* Output location list section if necessary. */
27622 if (have_location_lists)
27624 /* Output the location lists info. */
27625 switch_to_section (debug_loc_section);
27626 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
27627 output_location_lists (comp_unit_die ());
27630 output_pubtables ();
27632 /* Output the address range information if a CU (.debug_info section)
27633 was emitted. We output an empty table even if we had no functions
27634 to put in it. This because the consumer has no way to tell the
27635 difference between an empty table that we omitted and failure to
27636 generate a table that would have contained data. */
27637 if (info_section_emitted)
27639 switch_to_section (debug_aranges_section);
27640 output_aranges ();
27643 /* Output ranges section if necessary. */
27644 if (ranges_table_in_use)
27646 switch_to_section (debug_ranges_section);
27647 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
27648 output_ranges ();
27651 /* Have to end the macro section. */
27652 if (have_macinfo)
27654 switch_to_section (debug_macinfo_section);
27655 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
27656 output_macinfo ();
27657 dw2_asm_output_data (1, 0, "End compilation unit");
27660 /* Output the source line correspondence table. We must do this
27661 even if there is no line information. Otherwise, on an empty
27662 translation unit, we will generate a present, but empty,
27663 .debug_info section. IRIX 6.5 `nm' will then complain when
27664 examining the file. This is done late so that any filenames
27665 used by the debug_info section are marked as 'used'. */
27666 switch_to_section (debug_line_section);
27667 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
27668 if (! DWARF2_ASM_LINE_DEBUG_INFO)
27669 output_line_info (false);
27671 if (dwarf_split_debug_info && info_section_emitted)
27673 switch_to_section (debug_skeleton_line_section);
27674 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
27675 output_line_info (true);
27678 /* If we emitted any indirect strings, output the string table too. */
27679 if (debug_str_hash || skeleton_debug_str_hash)
27680 output_indirect_strings ();
27683 /* Perform any cleanups needed after the early debug generation pass
27684 has run. */
27686 static void
27687 dwarf2out_early_finish (void)
27689 /* The point here is to flush out the limbo list so that it is empty
27690 and we don't need to stream it for LTO. */
27691 flush_limbo_die_list ();
27693 gen_scheduled_generic_parms_dies ();
27694 gen_remaining_tmpl_value_param_die_attribute ();
27696 /* Add DW_AT_linkage_name for all deferred DIEs. */
27697 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
27699 tree decl = node->created_for;
27700 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
27701 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
27702 ended up in deferred_asm_name before we knew it was
27703 constant and never written to disk. */
27704 && DECL_ASSEMBLER_NAME (decl))
27706 add_linkage_attr (node->die, decl);
27707 move_linkage_attr (node->die);
27710 deferred_asm_name = NULL;
27713 /* Reset all state within dwarf2out.c so that we can rerun the compiler
27714 within the same process. For use by toplev::finalize. */
27716 void
27717 dwarf2out_c_finalize (void)
27719 last_var_location_insn = NULL;
27720 cached_next_real_insn = NULL;
27721 used_rtx_array = NULL;
27722 incomplete_types = NULL;
27723 decl_scope_table = NULL;
27724 debug_info_section = NULL;
27725 debug_skeleton_info_section = NULL;
27726 debug_abbrev_section = NULL;
27727 debug_skeleton_abbrev_section = NULL;
27728 debug_aranges_section = NULL;
27729 debug_addr_section = NULL;
27730 debug_macinfo_section = NULL;
27731 debug_line_section = NULL;
27732 debug_skeleton_line_section = NULL;
27733 debug_loc_section = NULL;
27734 debug_pubnames_section = NULL;
27735 debug_pubtypes_section = NULL;
27736 debug_str_section = NULL;
27737 debug_str_dwo_section = NULL;
27738 debug_str_offsets_section = NULL;
27739 debug_ranges_section = NULL;
27740 debug_frame_section = NULL;
27741 fde_vec = NULL;
27742 debug_str_hash = NULL;
27743 skeleton_debug_str_hash = NULL;
27744 dw2_string_counter = 0;
27745 have_multiple_function_sections = false;
27746 text_section_used = false;
27747 cold_text_section_used = false;
27748 cold_text_section = NULL;
27749 current_unit_personality = NULL;
27751 next_die_offset = 0;
27752 single_comp_unit_die = NULL;
27753 comdat_type_list = NULL;
27754 limbo_die_list = NULL;
27755 file_table = NULL;
27756 decl_die_table = NULL;
27757 common_block_die_table = NULL;
27758 decl_loc_table = NULL;
27759 call_arg_locations = NULL;
27760 call_arg_loc_last = NULL;
27761 call_site_count = -1;
27762 tail_call_site_count = -1;
27763 cached_dw_loc_list_table = NULL;
27764 abbrev_die_table = NULL;
27765 abbrev_die_table_allocated = 0;
27766 abbrev_die_table_in_use = 0;
27767 delete dwarf_proc_stack_usage_map;
27768 dwarf_proc_stack_usage_map = NULL;
27769 line_info_label_num = 0;
27770 cur_line_info_table = NULL;
27771 text_section_line_info = NULL;
27772 cold_text_section_line_info = NULL;
27773 separate_line_info = NULL;
27774 info_section_emitted = false;
27775 pubname_table = NULL;
27776 pubtype_table = NULL;
27777 macinfo_table = NULL;
27778 ranges_table = NULL;
27779 ranges_table_allocated = 0;
27780 ranges_table_in_use = 0;
27781 ranges_by_label = 0;
27782 ranges_by_label_allocated = 0;
27783 ranges_by_label_in_use = 0;
27784 have_location_lists = false;
27785 loclabel_num = 0;
27786 poc_label_num = 0;
27787 last_emitted_file = NULL;
27788 label_num = 0;
27789 tmpl_value_parm_die_table = NULL;
27790 generic_type_instances = NULL;
27791 frame_pointer_fb_offset = 0;
27792 frame_pointer_fb_offset_valid = false;
27793 base_types.release ();
27794 XDELETEVEC (producer_string);
27795 producer_string = NULL;
27798 #include "gt-dwarf2out.h"