2016-10-13 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / dwarf2out.c
bloba84deb70f986024bfc5f4b87d70c2f348ca119f2
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
3 Contributed by Gary Funck (gary@intrepid.com).
4 Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5 Extensively modified by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* TODO: Emit .debug_line header even when there are no functions, since
24 the file numbers are used by .debug_info. Alternately, leave
25 out locations for types and decls.
26 Avoid talking about ctors and op= for PODs.
27 Factor out common prologue sequences into multiple CIEs. */
29 /* The first part of this file deals with the DWARF 2 frame unwind
30 information, which is also used by the GCC efficient exception handling
31 mechanism. The second part, controlled only by an #ifdef
32 DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33 information. */
35 /* DWARF2 Abbreviation Glossary:
37 CFA = Canonical Frame Address
38 a fixed address on the stack which identifies a call frame.
39 We define it to be the value of SP just before the call insn.
40 The CFA register and offset, which may change during the course
41 of the function, are used to calculate its value at runtime.
43 CFI = Call Frame Instruction
44 an instruction for the DWARF2 abstract machine
46 CIE = Common Information Entry
47 information describing information common to one or more FDEs
49 DIE = Debugging Information Entry
51 FDE = Frame Description Entry
52 information describing the stack call frame, in particular,
53 how to restore registers
55 DW_CFA_... = DWARF2 CFA call frame instruction
56 DW_TAG_... = DWARF2 DIE tag */
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "target.h"
62 #include "function.h"
63 #include "rtl.h"
64 #include "tree.h"
65 #include "tm_p.h"
66 #include "stringpool.h"
67 #include "insn-config.h"
68 #include "ira.h"
69 #include "cgraph.h"
70 #include "diagnostic.h"
71 #include "fold-const.h"
72 #include "stor-layout.h"
73 #include "varasm.h"
74 #include "version.h"
75 #include "flags.h"
76 #include "rtlhash.h"
77 #include "reload.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "dwarf2out.h"
81 #include "dwarf2asm.h"
82 #include "toplev.h"
83 #include "md5.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "common/common-target.h"
87 #include "langhooks.h"
88 #include "lra.h"
89 #include "dumpfile.h"
90 #include "opts.h"
91 #include "tree-dfa.h"
92 #include "gdb/gdb-index.h"
93 #include "rtl-iter.h"
95 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
96 static rtx_insn *last_var_location_insn;
97 static rtx_insn *cached_next_real_insn;
98 static void dwarf2out_decl (tree);
100 #ifndef XCOFF_DEBUGGING_INFO
101 #define XCOFF_DEBUGGING_INFO 0
102 #endif
104 #ifndef HAVE_XCOFF_DWARF_EXTRAS
105 #define HAVE_XCOFF_DWARF_EXTRAS 0
106 #endif
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static const char *debug_macinfo_section_name;
163 static GTY(()) section *debug_line_section;
164 static GTY(()) section *debug_skeleton_line_section;
165 static GTY(()) section *debug_loc_section;
166 static GTY(()) section *debug_pubnames_section;
167 static GTY(()) section *debug_pubtypes_section;
168 static GTY(()) section *debug_str_section;
169 static GTY(()) section *debug_str_dwo_section;
170 static GTY(()) section *debug_str_offsets_section;
171 static GTY(()) section *debug_ranges_section;
172 static GTY(()) section *debug_frame_section;
174 /* Maximum size (in bytes) of an artificially generated label. */
175 #define MAX_ARTIFICIAL_LABEL_BYTES 30
177 /* According to the (draft) DWARF 3 specification, the initial length
178 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
179 bytes are 0xffffffff, followed by the length stored in the next 8
180 bytes.
182 However, the SGI/MIPS ABI uses an initial length which is equal to
183 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
185 #ifndef DWARF_INITIAL_LENGTH_SIZE
186 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
187 #endif
189 /* Round SIZE up to the nearest BOUNDARY. */
190 #define DWARF_ROUND(SIZE,BOUNDARY) \
191 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
193 /* CIE identifier. */
194 #if HOST_BITS_PER_WIDE_INT >= 64
195 #define DWARF_CIE_ID \
196 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
197 #else
198 #define DWARF_CIE_ID DW_CIE_ID
199 #endif
202 /* A vector for a table that contains frame description
203 information for each routine. */
204 #define NOT_INDEXED (-1U)
205 #define NO_INDEX_ASSIGNED (-2U)
207 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
209 struct GTY((for_user)) indirect_string_node {
210 const char *str;
211 unsigned int refcount;
212 enum dwarf_form form;
213 char *label;
214 unsigned int index;
217 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
219 typedef const char *compare_type;
221 static hashval_t hash (indirect_string_node *);
222 static bool equal (indirect_string_node *, const char *);
225 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
227 /* With split_debug_info, both the comp_dir and dwo_name go in the
228 main object file, rather than the dwo, similar to the force_direct
229 parameter elsewhere but with additional complications:
231 1) The string is needed in both the main object file and the dwo.
232 That is, the comp_dir and dwo_name will appear in both places.
234 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
235 DW_FORM_GNU_str_index.
237 3) GCC chooses the form to use late, depending on the size and
238 reference count.
240 Rather than forcing the all debug string handling functions and
241 callers to deal with these complications, simply use a separate,
242 special-cased string table for any attribute that should go in the
243 main object file. This limits the complexity to just the places
244 that need it. */
246 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
248 static GTY(()) int dw2_string_counter;
250 /* True if the compilation unit places functions in more than one section. */
251 static GTY(()) bool have_multiple_function_sections = false;
253 /* Whether the default text and cold text sections have been used at all. */
255 static GTY(()) bool text_section_used = false;
256 static GTY(()) bool cold_text_section_used = false;
258 /* The default cold text section. */
259 static GTY(()) section *cold_text_section;
261 /* The DIE for C++14 'auto' in a function return type. */
262 static GTY(()) dw_die_ref auto_die;
264 /* The DIE for C++14 'decltype(auto)' in a function return type. */
265 static GTY(()) dw_die_ref decltype_auto_die;
267 /* Forward declarations for functions defined in this file. */
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. Some constants have a large uniform precision, so
372 we get the precision needed for the actual value of the number. */
374 static unsigned int
375 get_full_len (const wide_int &op)
377 int prec = wi::min_precision (op, UNSIGNED);
378 return ((prec + HOST_BITS_PER_WIDE_INT - 1)
379 / HOST_BITS_PER_WIDE_INT);
382 static bool
383 should_emit_struct_debug (tree type, enum debug_info_usage usage)
385 enum debug_struct_file criterion;
386 tree type_decl;
387 bool generic = lang_hooks.types.generic_p (type);
389 if (generic)
390 criterion = debug_struct_generic[usage];
391 else
392 criterion = debug_struct_ordinary[usage];
394 if (criterion == DINFO_STRUCT_FILE_NONE)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
396 if (criterion == DINFO_STRUCT_FILE_ANY)
397 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
399 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
401 if (type_decl != NULL)
403 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
404 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
406 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
407 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
410 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
413 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
414 switch to the data section instead, and write out a synthetic start label
415 for collect2 the first time around. */
417 static void
418 switch_to_eh_frame_section (bool back ATTRIBUTE_UNUSED)
420 if (eh_frame_section == 0)
422 int flags;
424 if (EH_TABLES_CAN_BE_READ_ONLY)
426 int fde_encoding;
427 int per_encoding;
428 int lsda_encoding;
430 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
431 /*global=*/0);
432 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
433 /*global=*/1);
434 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
435 /*global=*/0);
436 flags = ((! flag_pic
437 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
438 && (fde_encoding & 0x70) != DW_EH_PE_aligned
439 && (per_encoding & 0x70) != DW_EH_PE_absptr
440 && (per_encoding & 0x70) != DW_EH_PE_aligned
441 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
442 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
443 ? 0 : SECTION_WRITE);
445 else
446 flags = SECTION_WRITE;
448 #ifdef EH_FRAME_SECTION_NAME
449 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
450 #else
451 eh_frame_section = ((flags == SECTION_WRITE)
452 ? data_section : readonly_data_section);
453 #endif /* EH_FRAME_SECTION_NAME */
456 switch_to_section (eh_frame_section);
458 #ifdef EH_FRAME_THROUGH_COLLECT2
459 /* We have no special eh_frame section. Emit special labels to guide
460 collect2. */
461 if (!back)
463 tree label = get_file_function_name ("F");
464 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
465 targetm.asm_out.globalize_label (asm_out_file,
466 IDENTIFIER_POINTER (label));
467 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
469 #endif
472 /* Switch [BACK] to the eh or debug frame table section, depending on
473 FOR_EH. */
475 static void
476 switch_to_frame_table_section (int for_eh, bool back)
478 if (for_eh)
479 switch_to_eh_frame_section (back);
480 else
482 if (!debug_frame_section)
483 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
484 SECTION_DEBUG, NULL);
485 switch_to_section (debug_frame_section);
489 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
491 enum dw_cfi_oprnd_type
492 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
494 switch (cfi)
496 case DW_CFA_nop:
497 case DW_CFA_GNU_window_save:
498 case DW_CFA_remember_state:
499 case DW_CFA_restore_state:
500 return dw_cfi_oprnd_unused;
502 case DW_CFA_set_loc:
503 case DW_CFA_advance_loc1:
504 case DW_CFA_advance_loc2:
505 case DW_CFA_advance_loc4:
506 case DW_CFA_MIPS_advance_loc8:
507 return dw_cfi_oprnd_addr;
509 case DW_CFA_offset:
510 case DW_CFA_offset_extended:
511 case DW_CFA_def_cfa:
512 case DW_CFA_offset_extended_sf:
513 case DW_CFA_def_cfa_sf:
514 case DW_CFA_restore:
515 case DW_CFA_restore_extended:
516 case DW_CFA_undefined:
517 case DW_CFA_same_value:
518 case DW_CFA_def_cfa_register:
519 case DW_CFA_register:
520 case DW_CFA_expression:
521 return dw_cfi_oprnd_reg_num;
523 case DW_CFA_def_cfa_offset:
524 case DW_CFA_GNU_args_size:
525 case DW_CFA_def_cfa_offset_sf:
526 return dw_cfi_oprnd_offset;
528 case DW_CFA_def_cfa_expression:
529 return dw_cfi_oprnd_loc;
531 default:
532 gcc_unreachable ();
536 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
538 enum dw_cfi_oprnd_type
539 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
541 switch (cfi)
543 case DW_CFA_def_cfa:
544 case DW_CFA_def_cfa_sf:
545 case DW_CFA_offset:
546 case DW_CFA_offset_extended_sf:
547 case DW_CFA_offset_extended:
548 return dw_cfi_oprnd_offset;
550 case DW_CFA_register:
551 return dw_cfi_oprnd_reg_num;
553 case DW_CFA_expression:
554 return dw_cfi_oprnd_loc;
556 default:
557 return dw_cfi_oprnd_unused;
561 /* Output one FDE. */
563 static void
564 output_fde (dw_fde_ref fde, bool for_eh, bool second,
565 char *section_start_label, int fde_encoding, char *augmentation,
566 bool any_lsda_needed, int lsda_encoding)
568 const char *begin, *end;
569 static unsigned int j;
570 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
572 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
573 /* empty */ 0);
574 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
575 for_eh + j);
576 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
577 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
578 if (!XCOFF_DEBUGGING_INFO || for_eh)
580 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
581 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
582 " indicating 64-bit DWARF extension");
583 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
584 "FDE Length");
586 ASM_OUTPUT_LABEL (asm_out_file, l1);
588 if (for_eh)
589 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
590 else
591 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
592 debug_frame_section, "FDE CIE offset");
594 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
595 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
597 if (for_eh)
599 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
600 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
601 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
602 "FDE initial location");
603 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
604 end, begin, "FDE address range");
606 else
608 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
609 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
612 if (augmentation[0])
614 if (any_lsda_needed)
616 int size = size_of_encoded_value (lsda_encoding);
618 if (lsda_encoding == DW_EH_PE_aligned)
620 int offset = ( 4 /* Length */
621 + 4 /* CIE offset */
622 + 2 * size_of_encoded_value (fde_encoding)
623 + 1 /* Augmentation size */ );
624 int pad = -offset & (PTR_SIZE - 1);
626 size += pad;
627 gcc_assert (size_of_uleb128 (size) == 1);
630 dw2_asm_output_data_uleb128 (size, "Augmentation size");
632 if (fde->uses_eh_lsda)
634 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
635 fde->funcdef_number);
636 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
637 gen_rtx_SYMBOL_REF (Pmode, l1),
638 false,
639 "Language Specific Data Area");
641 else
643 if (lsda_encoding == DW_EH_PE_aligned)
644 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
645 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
646 "Language Specific Data Area (none)");
649 else
650 dw2_asm_output_data_uleb128 (0, "Augmentation size");
653 /* Loop through the Call Frame Instructions associated with this FDE. */
654 fde->dw_fde_current_label = begin;
656 size_t from, until, i;
658 from = 0;
659 until = vec_safe_length (fde->dw_fde_cfi);
661 if (fde->dw_fde_second_begin == NULL)
663 else if (!second)
664 until = fde->dw_fde_switch_cfi_index;
665 else
666 from = fde->dw_fde_switch_cfi_index;
668 for (i = from; i < until; i++)
669 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
672 /* If we are to emit a ref/link from function bodies to their frame tables,
673 do it now. This is typically performed to make sure that tables
674 associated with functions are dragged with them and not discarded in
675 garbage collecting links. We need to do this on a per function basis to
676 cope with -ffunction-sections. */
678 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
679 /* Switch to the function section, emit the ref to the tables, and
680 switch *back* into the table section. */
681 switch_to_section (function_section (fde->decl));
682 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
683 switch_to_frame_table_section (for_eh, true);
684 #endif
686 /* Pad the FDE out to an address sized boundary. */
687 ASM_OUTPUT_ALIGN (asm_out_file,
688 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
689 ASM_OUTPUT_LABEL (asm_out_file, l2);
691 j += 2;
694 /* Return true if frame description entry FDE is needed for EH. */
696 static bool
697 fde_needed_for_eh_p (dw_fde_ref fde)
699 if (flag_asynchronous_unwind_tables)
700 return true;
702 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
703 return true;
705 if (fde->uses_eh_lsda)
706 return true;
708 /* If exceptions are enabled, we have collected nothrow info. */
709 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
710 return false;
712 return true;
715 /* Output the call frame information used to record information
716 that relates to calculating the frame pointer, and records the
717 location of saved registers. */
719 static void
720 output_call_frame_info (int for_eh)
722 unsigned int i;
723 dw_fde_ref fde;
724 dw_cfi_ref cfi;
725 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
726 char section_start_label[MAX_ARTIFICIAL_LABEL_BYTES];
727 bool any_lsda_needed = false;
728 char augmentation[6];
729 int augmentation_size;
730 int fde_encoding = DW_EH_PE_absptr;
731 int per_encoding = DW_EH_PE_absptr;
732 int lsda_encoding = DW_EH_PE_absptr;
733 int return_reg;
734 rtx personality = NULL;
735 int dw_cie_version;
737 /* Don't emit a CIE if there won't be any FDEs. */
738 if (!fde_vec)
739 return;
741 /* Nothing to do if the assembler's doing it all. */
742 if (dwarf2out_do_cfi_asm ())
743 return;
745 /* If we don't have any functions we'll want to unwind out of, don't emit
746 any EH unwind information. If we make FDEs linkonce, we may have to
747 emit an empty label for an FDE that wouldn't otherwise be emitted. We
748 want to avoid having an FDE kept around when the function it refers to
749 is discarded. Example where this matters: a primary function template
750 in C++ requires EH information, an explicit specialization doesn't. */
751 if (for_eh)
753 bool any_eh_needed = false;
755 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
757 if (fde->uses_eh_lsda)
758 any_eh_needed = any_lsda_needed = true;
759 else if (fde_needed_for_eh_p (fde))
760 any_eh_needed = true;
761 else if (TARGET_USES_WEAK_UNWIND_INFO)
762 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
765 if (!any_eh_needed)
766 return;
769 /* We're going to be generating comments, so turn on app. */
770 if (flag_debug_asm)
771 app_enable ();
773 /* Switch to the proper frame section, first time. */
774 switch_to_frame_table_section (for_eh, false);
776 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
777 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
779 /* Output the CIE. */
780 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
781 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
782 if (!XCOFF_DEBUGGING_INFO || for_eh)
784 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
785 dw2_asm_output_data (4, 0xffffffff,
786 "Initial length escape value indicating 64-bit DWARF extension");
787 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
788 "Length of Common Information Entry");
790 ASM_OUTPUT_LABEL (asm_out_file, l1);
792 /* Now that the CIE pointer is PC-relative for EH,
793 use 0 to identify the CIE. */
794 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
795 (for_eh ? 0 : DWARF_CIE_ID),
796 "CIE Identifier Tag");
798 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
799 use CIE version 1, unless that would produce incorrect results
800 due to overflowing the return register column. */
801 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
802 dw_cie_version = 1;
803 if (return_reg >= 256 || dwarf_version > 2)
804 dw_cie_version = 3;
805 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
807 augmentation[0] = 0;
808 augmentation_size = 0;
810 personality = current_unit_personality;
811 if (for_eh)
813 char *p;
815 /* Augmentation:
816 z Indicates that a uleb128 is present to size the
817 augmentation section.
818 L Indicates the encoding (and thus presence) of
819 an LSDA pointer in the FDE augmentation.
820 R Indicates a non-default pointer encoding for
821 FDE code pointers.
822 P Indicates the presence of an encoding + language
823 personality routine in the CIE augmentation. */
825 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
826 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
827 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
829 p = augmentation + 1;
830 if (personality)
832 *p++ = 'P';
833 augmentation_size += 1 + size_of_encoded_value (per_encoding);
834 assemble_external_libcall (personality);
836 if (any_lsda_needed)
838 *p++ = 'L';
839 augmentation_size += 1;
841 if (fde_encoding != DW_EH_PE_absptr)
843 *p++ = 'R';
844 augmentation_size += 1;
846 if (p > augmentation + 1)
848 augmentation[0] = 'z';
849 *p = '\0';
852 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
853 if (personality && per_encoding == DW_EH_PE_aligned)
855 int offset = ( 4 /* Length */
856 + 4 /* CIE Id */
857 + 1 /* CIE version */
858 + strlen (augmentation) + 1 /* Augmentation */
859 + size_of_uleb128 (1) /* Code alignment */
860 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
861 + 1 /* RA column */
862 + 1 /* Augmentation size */
863 + 1 /* Personality encoding */ );
864 int pad = -offset & (PTR_SIZE - 1);
866 augmentation_size += pad;
868 /* Augmentations should be small, so there's scarce need to
869 iterate for a solution. Die if we exceed one uleb128 byte. */
870 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
874 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
875 if (dw_cie_version >= 4)
877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
878 dw2_asm_output_data (1, 0, "CIE Segment Size");
880 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
881 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
882 "CIE Data Alignment Factor");
884 if (dw_cie_version == 1)
885 dw2_asm_output_data (1, return_reg, "CIE RA Column");
886 else
887 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
889 if (augmentation[0])
891 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
892 if (personality)
894 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
895 eh_data_format_name (per_encoding));
896 dw2_asm_output_encoded_addr_rtx (per_encoding,
897 personality,
898 true, NULL);
901 if (any_lsda_needed)
902 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
903 eh_data_format_name (lsda_encoding));
905 if (fde_encoding != DW_EH_PE_absptr)
906 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
907 eh_data_format_name (fde_encoding));
910 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
911 output_cfi (cfi, NULL, for_eh);
913 /* Pad the CIE out to an address sized boundary. */
914 ASM_OUTPUT_ALIGN (asm_out_file,
915 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
916 ASM_OUTPUT_LABEL (asm_out_file, l2);
918 /* Loop through all of the FDE's. */
919 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
921 unsigned int k;
923 /* Don't emit EH unwind info for leaf functions that don't need it. */
924 if (for_eh && !fde_needed_for_eh_p (fde))
925 continue;
927 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
928 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
929 augmentation, any_lsda_needed, lsda_encoding);
932 if (for_eh && targetm.terminate_dw2_eh_frame_info)
933 dw2_asm_output_data (4, 0, "End of Table");
935 /* Turn off app to make assembly quicker. */
936 if (flag_debug_asm)
937 app_disable ();
940 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
942 static void
943 dwarf2out_do_cfi_startproc (bool second)
945 int enc;
946 rtx ref;
947 rtx personality = get_personality_function (current_function_decl);
949 fprintf (asm_out_file, "\t.cfi_startproc\n");
951 if (personality)
953 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
954 ref = personality;
956 /* ??? The GAS support isn't entirely consistent. We have to
957 handle indirect support ourselves, but PC-relative is done
958 in the assembler. Further, the assembler can't handle any
959 of the weirder relocation types. */
960 if (enc & DW_EH_PE_indirect)
961 ref = dw2_force_const_mem (ref, true);
963 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
964 output_addr_const (asm_out_file, ref);
965 fputc ('\n', asm_out_file);
968 if (crtl->uses_eh_lsda)
970 char lab[MAX_ARTIFICIAL_LABEL_BYTES];
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
973 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
974 current_function_funcdef_no);
975 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
976 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
981 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
987 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
988 this allocation may be done before pass_final. */
990 dw_fde_ref
991 dwarf2out_alloc_current_fde (void)
993 dw_fde_ref fde;
995 fde = ggc_cleared_alloc<dw_fde_node> ();
996 fde->decl = current_function_decl;
997 fde->funcdef_number = current_function_funcdef_no;
998 fde->fde_index = vec_safe_length (fde_vec);
999 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1000 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1001 fde->nothrow = crtl->nothrow;
1002 fde->drap_reg = INVALID_REGNUM;
1003 fde->vdrap_reg = INVALID_REGNUM;
1005 /* Record the FDE associated with this function. */
1006 cfun->fde = fde;
1007 vec_safe_push (fde_vec, fde);
1009 return fde;
1012 /* Output a marker (i.e. a label) for the beginning of a function, before
1013 the prologue. */
1015 void
1016 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1017 const char *file ATTRIBUTE_UNUSED)
1019 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1020 char * dup_label;
1021 dw_fde_ref fde;
1022 section *fnsec;
1023 bool do_frame;
1025 current_function_func_begin_label = NULL;
1027 do_frame = dwarf2out_do_frame ();
1029 /* ??? current_function_func_begin_label is also used by except.c for
1030 call-site information. We must emit this label if it might be used. */
1031 if (!do_frame
1032 && (!flag_exceptions
1033 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1034 return;
1036 fnsec = function_section (current_function_decl);
1037 switch_to_section (fnsec);
1038 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1039 current_function_funcdef_no);
1040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 dup_label = xstrdup (label);
1043 current_function_func_begin_label = dup_label;
1045 /* We can elide the fde allocation if we're not emitting debug info. */
1046 if (!do_frame)
1047 return;
1049 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1050 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1051 would include pass_dwarf2_frame. If we've not created the FDE yet,
1052 do so now. */
1053 fde = cfun->fde;
1054 if (fde == NULL)
1055 fde = dwarf2out_alloc_current_fde ();
1057 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1058 fde->dw_fde_begin = dup_label;
1059 fde->dw_fde_current_label = dup_label;
1060 fde->in_std_section = (fnsec == text_section
1061 || (cold_text_section && fnsec == cold_text_section));
1063 /* We only want to output line number information for the genuine dwarf2
1064 prologue case, not the eh frame case. */
1065 #ifdef DWARF2_DEBUGGING_INFO
1066 if (file)
1067 dwarf2out_source_line (line, file, 0, true);
1068 #endif
1070 if (dwarf2out_do_cfi_asm ())
1071 dwarf2out_do_cfi_startproc (false);
1072 else
1074 rtx personality = get_personality_function (current_function_decl);
1075 if (!current_unit_personality)
1076 current_unit_personality = personality;
1078 /* We cannot keep a current personality per function as without CFI
1079 asm, at the point where we emit the CFI data, there is no current
1080 function anymore. */
1081 if (personality && current_unit_personality != personality)
1082 sorry ("multiple EH personalities are supported only with assemblers "
1083 "supporting .cfi_personality directive");
1087 /* Output a marker (i.e. a label) for the end of the generated code
1088 for a function prologue. This gets called *after* the prologue code has
1089 been generated. */
1091 void
1092 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1093 const char *file ATTRIBUTE_UNUSED)
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1106 /* Output a marker (i.e. a label) for the beginning of the generated code
1107 for a function epilogue. This gets called *before* the prologue code has
1108 been generated. */
1110 void
1111 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1114 dw_fde_ref fde = cfun->fde;
1115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1117 if (fde->dw_fde_vms_begin_epilogue)
1118 return;
1120 /* Output a label to mark the endpoint of the code generated for this
1121 function. */
1122 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1123 current_function_funcdef_no);
1124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1129 /* Output a marker (i.e. a label) for the absolute end of the generated code
1130 for a function definition. This gets called *after* the epilogue code has
1131 been generated. */
1133 void
1134 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1135 const char *file ATTRIBUTE_UNUSED)
1137 dw_fde_ref fde;
1138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1140 last_var_location_insn = NULL;
1141 cached_next_real_insn = NULL;
1143 if (dwarf2out_do_cfi_asm ())
1144 fprintf (asm_out_file, "\t.cfi_endproc\n");
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_LABEL (asm_out_file, label);
1151 fde = cfun->fde;
1152 gcc_assert (fde != NULL);
1153 if (fde->dw_fde_second_begin == NULL)
1154 fde->dw_fde_end = xstrdup (label);
1157 void
1158 dwarf2out_frame_finish (void)
1160 /* Output call frame information. */
1161 if (targetm.debug_unwind_info () == UI_DWARF2)
1162 output_call_frame_info (0);
1164 /* Output another copy for the unwinder. */
1165 if ((flag_unwind_tables || flag_exceptions)
1166 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1167 output_call_frame_info (1);
1170 /* Note that the current function section is being used for code. */
1172 static void
1173 dwarf2out_note_section_used (void)
1175 section *sec = current_function_section ();
1176 if (sec == text_section)
1177 text_section_used = true;
1178 else if (sec == cold_text_section)
1179 cold_text_section_used = true;
1182 static void var_location_switch_text_section (void);
1183 static void set_cur_line_info_table (section *);
1185 void
1186 dwarf2out_switch_text_section (void)
1188 section *sect;
1189 dw_fde_ref fde = cfun->fde;
1191 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1193 if (!in_cold_section_p)
1195 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1196 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1197 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1199 else
1201 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1202 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1203 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1205 have_multiple_function_sections = true;
1207 /* There is no need to mark used sections when not debugging. */
1208 if (cold_text_section != NULL)
1209 dwarf2out_note_section_used ();
1211 if (dwarf2out_do_cfi_asm ())
1212 fprintf (asm_out_file, "\t.cfi_endproc\n");
1214 /* Now do the real section switch. */
1215 sect = current_function_section ();
1216 switch_to_section (sect);
1218 fde->second_in_std_section
1219 = (sect == text_section
1220 || (cold_text_section && sect == cold_text_section));
1222 if (dwarf2out_do_cfi_asm ())
1223 dwarf2out_do_cfi_startproc (true);
1225 var_location_switch_text_section ();
1227 if (cold_text_section != NULL)
1228 set_cur_line_info_table (sect);
1231 /* And now, the subset of the debugging information support code necessary
1232 for emitting location expressions. */
1234 /* Data about a single source file. */
1235 struct GTY((for_user)) dwarf_file_data {
1236 const char * filename;
1237 int emitted_number;
1240 /* Describe an entry into the .debug_addr section. */
1242 enum ate_kind {
1243 ate_kind_rtx,
1244 ate_kind_rtx_dtprel,
1245 ate_kind_label
1248 struct GTY((for_user)) addr_table_entry {
1249 enum ate_kind kind;
1250 unsigned int refcount;
1251 unsigned int index;
1252 union addr_table_entry_struct_union
1254 rtx GTY ((tag ("0"))) rtl;
1255 char * GTY ((tag ("1"))) label;
1257 GTY ((desc ("%1.kind"))) addr;
1260 /* Location lists are ranges + location descriptions for that range,
1261 so you can track variables that are in different places over
1262 their entire life. */
1263 typedef struct GTY(()) dw_loc_list_struct {
1264 dw_loc_list_ref dw_loc_next;
1265 const char *begin; /* Label and addr_entry for start of range */
1266 addr_table_entry *begin_entry;
1267 const char *end; /* Label for end of range */
1268 char *ll_symbol; /* Label for beginning of location list.
1269 Only on head of list */
1270 const char *section; /* Section this loclist is relative to */
1271 dw_loc_descr_ref expr;
1272 hashval_t hash;
1273 /* True if all addresses in this and subsequent lists are known to be
1274 resolved. */
1275 bool resolved_addr;
1276 /* True if this list has been replaced by dw_loc_next. */
1277 bool replaced;
1278 bool emitted;
1279 /* True if the range should be emitted even if begin and end
1280 are the same. */
1281 bool force;
1282 } dw_loc_list_node;
1284 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1285 static dw_loc_descr_ref uint_loc_descriptor (unsigned HOST_WIDE_INT);
1287 /* Convert a DWARF stack opcode into its string name. */
1289 static const char *
1290 dwarf_stack_op_name (unsigned int op)
1292 const char *name = get_DW_OP_name (op);
1294 if (name != NULL)
1295 return name;
1297 return "OP_<unknown>";
1300 /* Return a pointer to a newly allocated location description. Location
1301 descriptions are simple expression terms that can be strung
1302 together to form more complicated location (address) descriptions. */
1304 static inline dw_loc_descr_ref
1305 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1306 unsigned HOST_WIDE_INT oprnd2)
1308 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1310 descr->dw_loc_opc = op;
1311 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1312 descr->dw_loc_oprnd1.val_entry = NULL;
1313 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1314 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1315 descr->dw_loc_oprnd2.val_entry = NULL;
1316 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1318 return descr;
1321 /* Return a pointer to a newly allocated location description for
1322 REG and OFFSET. */
1324 static inline dw_loc_descr_ref
1325 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1327 if (reg <= 31)
1328 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1329 offset, 0);
1330 else
1331 return new_loc_descr (DW_OP_bregx, reg, offset);
1334 /* Add a location description term to a location description expression. */
1336 static inline void
1337 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1339 dw_loc_descr_ref *d;
1341 /* Find the end of the chain. */
1342 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1345 *d = descr;
1348 /* Compare two location operands for exact equality. */
1350 static bool
1351 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1353 if (a->val_class != b->val_class)
1354 return false;
1355 switch (a->val_class)
1357 case dw_val_class_none:
1358 return true;
1359 case dw_val_class_addr:
1360 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1362 case dw_val_class_offset:
1363 case dw_val_class_unsigned_const:
1364 case dw_val_class_const:
1365 case dw_val_class_range_list:
1366 case dw_val_class_lineptr:
1367 case dw_val_class_macptr:
1368 /* These are all HOST_WIDE_INT, signed or unsigned. */
1369 return a->v.val_unsigned == b->v.val_unsigned;
1371 case dw_val_class_loc:
1372 return a->v.val_loc == b->v.val_loc;
1373 case dw_val_class_loc_list:
1374 return a->v.val_loc_list == b->v.val_loc_list;
1375 case dw_val_class_die_ref:
1376 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1377 case dw_val_class_fde_ref:
1378 return a->v.val_fde_index == b->v.val_fde_index;
1379 case dw_val_class_lbl_id:
1380 case dw_val_class_high_pc:
1381 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1382 case dw_val_class_str:
1383 return a->v.val_str == b->v.val_str;
1384 case dw_val_class_flag:
1385 return a->v.val_flag == b->v.val_flag;
1386 case dw_val_class_file:
1387 return a->v.val_file == b->v.val_file;
1388 case dw_val_class_decl_ref:
1389 return a->v.val_decl_ref == b->v.val_decl_ref;
1391 case dw_val_class_const_double:
1392 return (a->v.val_double.high == b->v.val_double.high
1393 && a->v.val_double.low == b->v.val_double.low);
1395 case dw_val_class_wide_int:
1396 return *a->v.val_wide == *b->v.val_wide;
1398 case dw_val_class_vec:
1400 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1401 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1403 return (a_len == b_len
1404 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1407 case dw_val_class_data8:
1408 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1410 case dw_val_class_vms_delta:
1411 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1412 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1414 case dw_val_class_discr_value:
1415 return (a->v.val_discr_value.pos == b->v.val_discr_value.pos
1416 && a->v.val_discr_value.v.uval == b->v.val_discr_value.v.uval);
1417 case dw_val_class_discr_list:
1418 /* It makes no sense comparing two discriminant value lists. */
1419 return false;
1421 gcc_unreachable ();
1424 /* Compare two location atoms for exact equality. */
1426 static bool
1427 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1429 if (a->dw_loc_opc != b->dw_loc_opc)
1430 return false;
1432 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1433 address size, but since we always allocate cleared storage it
1434 should be zero for other types of locations. */
1435 if (a->dtprel != b->dtprel)
1436 return false;
1438 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1439 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1442 /* Compare two complete location expressions for exact equality. */
1444 bool
1445 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1447 while (1)
1449 if (a == b)
1450 return true;
1451 if (a == NULL || b == NULL)
1452 return false;
1453 if (!loc_descr_equal_p_1 (a, b))
1454 return false;
1456 a = a->dw_loc_next;
1457 b = b->dw_loc_next;
1462 /* Add a constant OFFSET to a location expression. */
1464 static void
1465 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1467 dw_loc_descr_ref loc;
1468 HOST_WIDE_INT *p;
1470 gcc_assert (*list_head != NULL);
1472 if (!offset)
1473 return;
1475 /* Find the end of the chain. */
1476 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1479 p = NULL;
1480 if (loc->dw_loc_opc == DW_OP_fbreg
1481 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1482 p = &loc->dw_loc_oprnd1.v.val_int;
1483 else if (loc->dw_loc_opc == DW_OP_bregx)
1484 p = &loc->dw_loc_oprnd2.v.val_int;
1486 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1487 offset. Don't optimize if an signed integer overflow would happen. */
1488 if (p != NULL
1489 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1490 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1491 *p += offset;
1493 else if (offset > 0)
1494 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1496 else
1498 loc->dw_loc_next = int_loc_descriptor (-offset);
1499 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1503 /* Add a constant OFFSET to a location list. */
1505 static void
1506 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1508 dw_loc_list_ref d;
1509 for (d = list_head; d != NULL; d = d->dw_loc_next)
1510 loc_descr_plus_const (&d->expr, offset);
1513 #define DWARF_REF_SIZE \
1514 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1516 static unsigned long int get_base_type_offset (dw_die_ref);
1518 /* Return the size of a location descriptor. */
1520 static unsigned long
1521 size_of_loc_descr (dw_loc_descr_ref loc)
1523 unsigned long size = 1;
1525 switch (loc->dw_loc_opc)
1527 case DW_OP_addr:
1528 size += DWARF2_ADDR_SIZE;
1529 break;
1530 case DW_OP_GNU_addr_index:
1531 case DW_OP_GNU_const_index:
1532 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1533 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1534 break;
1535 case DW_OP_const1u:
1536 case DW_OP_const1s:
1537 size += 1;
1538 break;
1539 case DW_OP_const2u:
1540 case DW_OP_const2s:
1541 size += 2;
1542 break;
1543 case DW_OP_const4u:
1544 case DW_OP_const4s:
1545 size += 4;
1546 break;
1547 case DW_OP_const8u:
1548 case DW_OP_const8s:
1549 size += 8;
1550 break;
1551 case DW_OP_constu:
1552 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1553 break;
1554 case DW_OP_consts:
1555 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1556 break;
1557 case DW_OP_pick:
1558 size += 1;
1559 break;
1560 case DW_OP_plus_uconst:
1561 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1562 break;
1563 case DW_OP_skip:
1564 case DW_OP_bra:
1565 size += 2;
1566 break;
1567 case DW_OP_breg0:
1568 case DW_OP_breg1:
1569 case DW_OP_breg2:
1570 case DW_OP_breg3:
1571 case DW_OP_breg4:
1572 case DW_OP_breg5:
1573 case DW_OP_breg6:
1574 case DW_OP_breg7:
1575 case DW_OP_breg8:
1576 case DW_OP_breg9:
1577 case DW_OP_breg10:
1578 case DW_OP_breg11:
1579 case DW_OP_breg12:
1580 case DW_OP_breg13:
1581 case DW_OP_breg14:
1582 case DW_OP_breg15:
1583 case DW_OP_breg16:
1584 case DW_OP_breg17:
1585 case DW_OP_breg18:
1586 case DW_OP_breg19:
1587 case DW_OP_breg20:
1588 case DW_OP_breg21:
1589 case DW_OP_breg22:
1590 case DW_OP_breg23:
1591 case DW_OP_breg24:
1592 case DW_OP_breg25:
1593 case DW_OP_breg26:
1594 case DW_OP_breg27:
1595 case DW_OP_breg28:
1596 case DW_OP_breg29:
1597 case DW_OP_breg30:
1598 case DW_OP_breg31:
1599 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1600 break;
1601 case DW_OP_regx:
1602 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1603 break;
1604 case DW_OP_fbreg:
1605 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1606 break;
1607 case DW_OP_bregx:
1608 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1609 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1610 break;
1611 case DW_OP_piece:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_bit_piece:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1617 break;
1618 case DW_OP_deref_size:
1619 case DW_OP_xderef_size:
1620 size += 1;
1621 break;
1622 case DW_OP_call2:
1623 size += 2;
1624 break;
1625 case DW_OP_call4:
1626 size += 4;
1627 break;
1628 case DW_OP_call_ref:
1629 size += DWARF_REF_SIZE;
1630 break;
1631 case DW_OP_implicit_value:
1632 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1633 + loc->dw_loc_oprnd1.v.val_unsigned;
1634 break;
1635 case DW_OP_GNU_implicit_pointer:
1636 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1637 break;
1638 case DW_OP_GNU_entry_value:
1640 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1641 size += size_of_uleb128 (op_size) + op_size;
1642 break;
1644 case DW_OP_GNU_const_type:
1646 unsigned long o
1647 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1648 size += size_of_uleb128 (o) + 1;
1649 switch (loc->dw_loc_oprnd2.val_class)
1651 case dw_val_class_vec:
1652 size += loc->dw_loc_oprnd2.v.val_vec.length
1653 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1654 break;
1655 case dw_val_class_const:
1656 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1657 break;
1658 case dw_val_class_const_double:
1659 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1660 break;
1661 case dw_val_class_wide_int:
1662 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1663 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1664 break;
1665 default:
1666 gcc_unreachable ();
1668 break;
1670 case DW_OP_GNU_regval_type:
1672 unsigned long o
1673 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1674 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1675 + size_of_uleb128 (o);
1677 break;
1678 case DW_OP_GNU_deref_type:
1680 unsigned long o
1681 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1682 size += 1 + size_of_uleb128 (o);
1684 break;
1685 case DW_OP_GNU_convert:
1686 case DW_OP_GNU_reinterpret:
1687 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1688 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1689 else
1691 unsigned long o
1692 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1693 size += size_of_uleb128 (o);
1695 break;
1696 case DW_OP_GNU_parameter_ref:
1697 size += 4;
1698 break;
1699 default:
1700 break;
1703 return size;
1706 /* Return the size of a series of location descriptors. */
1708 unsigned long
1709 size_of_locs (dw_loc_descr_ref loc)
1711 dw_loc_descr_ref l;
1712 unsigned long size;
1714 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1715 field, to avoid writing to a PCH file. */
1716 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1718 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1719 break;
1720 size += size_of_loc_descr (l);
1722 if (! l)
1723 return size;
1725 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1727 l->dw_loc_addr = size;
1728 size += size_of_loc_descr (l);
1731 return size;
1734 /* Return the size of the value in a DW_AT_discr_value attribute. */
1736 static int
1737 size_of_discr_value (dw_discr_value *discr_value)
1739 if (discr_value->pos)
1740 return size_of_uleb128 (discr_value->v.uval);
1741 else
1742 return size_of_sleb128 (discr_value->v.sval);
1745 /* Return the size of the value in a DW_discr_list attribute. */
1747 static int
1748 size_of_discr_list (dw_discr_list_ref discr_list)
1750 int size = 0;
1752 for (dw_discr_list_ref list = discr_list;
1753 list != NULL;
1754 list = list->dw_discr_next)
1756 /* One byte for the discriminant value descriptor, and then one or two
1757 LEB128 numbers, depending on whether it's a single case label or a
1758 range label. */
1759 size += 1;
1760 size += size_of_discr_value (&list->dw_discr_lower_bound);
1761 if (list->dw_discr_range != 0)
1762 size += size_of_discr_value (&list->dw_discr_upper_bound);
1764 return size;
1767 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1768 static void get_ref_die_offset_label (char *, dw_die_ref);
1769 static unsigned long int get_ref_die_offset (dw_die_ref);
1771 /* Output location description stack opcode's operands (if any).
1772 The for_eh_or_skip parameter controls whether register numbers are
1773 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1774 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1775 info). This should be suppressed for the cases that have not been converted
1776 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1778 static void
1779 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1781 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1782 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1784 switch (loc->dw_loc_opc)
1786 #ifdef DWARF2_DEBUGGING_INFO
1787 case DW_OP_const2u:
1788 case DW_OP_const2s:
1789 dw2_asm_output_data (2, val1->v.val_int, NULL);
1790 break;
1791 case DW_OP_const4u:
1792 if (loc->dtprel)
1794 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1795 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1796 val1->v.val_addr);
1797 fputc ('\n', asm_out_file);
1798 break;
1800 /* FALLTHRU */
1801 case DW_OP_const4s:
1802 dw2_asm_output_data (4, val1->v.val_int, NULL);
1803 break;
1804 case DW_OP_const8u:
1805 if (loc->dtprel)
1807 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1808 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1809 val1->v.val_addr);
1810 fputc ('\n', asm_out_file);
1811 break;
1813 /* FALLTHRU */
1814 case DW_OP_const8s:
1815 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1816 dw2_asm_output_data (8, val1->v.val_int, NULL);
1817 break;
1818 case DW_OP_skip:
1819 case DW_OP_bra:
1821 int offset;
1823 gcc_assert (val1->val_class == dw_val_class_loc);
1824 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1826 dw2_asm_output_data (2, offset, NULL);
1828 break;
1829 case DW_OP_implicit_value:
1830 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1831 switch (val2->val_class)
1833 case dw_val_class_const:
1834 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1835 break;
1836 case dw_val_class_vec:
1838 unsigned int elt_size = val2->v.val_vec.elt_size;
1839 unsigned int len = val2->v.val_vec.length;
1840 unsigned int i;
1841 unsigned char *p;
1843 if (elt_size > sizeof (HOST_WIDE_INT))
1845 elt_size /= 2;
1846 len *= 2;
1848 for (i = 0, p = val2->v.val_vec.array;
1849 i < len;
1850 i++, p += elt_size)
1851 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1852 "fp or vector constant word %u", i);
1854 break;
1855 case dw_val_class_const_double:
1857 unsigned HOST_WIDE_INT first, second;
1859 if (WORDS_BIG_ENDIAN)
1861 first = val2->v.val_double.high;
1862 second = val2->v.val_double.low;
1864 else
1866 first = val2->v.val_double.low;
1867 second = val2->v.val_double.high;
1869 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1870 first, NULL);
1871 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1872 second, NULL);
1874 break;
1875 case dw_val_class_wide_int:
1877 int i;
1878 int len = get_full_len (*val2->v.val_wide);
1879 if (WORDS_BIG_ENDIAN)
1880 for (i = len - 1; i >= 0; --i)
1881 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1882 val2->v.val_wide->elt (i), NULL);
1883 else
1884 for (i = 0; i < len; ++i)
1885 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1886 val2->v.val_wide->elt (i), NULL);
1888 break;
1889 case dw_val_class_addr:
1890 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1891 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1892 break;
1893 default:
1894 gcc_unreachable ();
1896 break;
1897 #else
1898 case DW_OP_const2u:
1899 case DW_OP_const2s:
1900 case DW_OP_const4u:
1901 case DW_OP_const4s:
1902 case DW_OP_const8u:
1903 case DW_OP_const8s:
1904 case DW_OP_skip:
1905 case DW_OP_bra:
1906 case DW_OP_implicit_value:
1907 /* We currently don't make any attempt to make sure these are
1908 aligned properly like we do for the main unwind info, so
1909 don't support emitting things larger than a byte if we're
1910 only doing unwinding. */
1911 gcc_unreachable ();
1912 #endif
1913 case DW_OP_const1u:
1914 case DW_OP_const1s:
1915 dw2_asm_output_data (1, val1->v.val_int, NULL);
1916 break;
1917 case DW_OP_constu:
1918 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1919 break;
1920 case DW_OP_consts:
1921 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1922 break;
1923 case DW_OP_pick:
1924 dw2_asm_output_data (1, val1->v.val_int, NULL);
1925 break;
1926 case DW_OP_plus_uconst:
1927 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1928 break;
1929 case DW_OP_breg0:
1930 case DW_OP_breg1:
1931 case DW_OP_breg2:
1932 case DW_OP_breg3:
1933 case DW_OP_breg4:
1934 case DW_OP_breg5:
1935 case DW_OP_breg6:
1936 case DW_OP_breg7:
1937 case DW_OP_breg8:
1938 case DW_OP_breg9:
1939 case DW_OP_breg10:
1940 case DW_OP_breg11:
1941 case DW_OP_breg12:
1942 case DW_OP_breg13:
1943 case DW_OP_breg14:
1944 case DW_OP_breg15:
1945 case DW_OP_breg16:
1946 case DW_OP_breg17:
1947 case DW_OP_breg18:
1948 case DW_OP_breg19:
1949 case DW_OP_breg20:
1950 case DW_OP_breg21:
1951 case DW_OP_breg22:
1952 case DW_OP_breg23:
1953 case DW_OP_breg24:
1954 case DW_OP_breg25:
1955 case DW_OP_breg26:
1956 case DW_OP_breg27:
1957 case DW_OP_breg28:
1958 case DW_OP_breg29:
1959 case DW_OP_breg30:
1960 case DW_OP_breg31:
1961 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1962 break;
1963 case DW_OP_regx:
1965 unsigned r = val1->v.val_unsigned;
1966 if (for_eh_or_skip >= 0)
1967 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1968 gcc_assert (size_of_uleb128 (r)
1969 == size_of_uleb128 (val1->v.val_unsigned));
1970 dw2_asm_output_data_uleb128 (r, NULL);
1972 break;
1973 case DW_OP_fbreg:
1974 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1975 break;
1976 case DW_OP_bregx:
1978 unsigned r = val1->v.val_unsigned;
1979 if (for_eh_or_skip >= 0)
1980 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1981 gcc_assert (size_of_uleb128 (r)
1982 == size_of_uleb128 (val1->v.val_unsigned));
1983 dw2_asm_output_data_uleb128 (r, NULL);
1984 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1986 break;
1987 case DW_OP_piece:
1988 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1989 break;
1990 case DW_OP_bit_piece:
1991 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1992 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1993 break;
1994 case DW_OP_deref_size:
1995 case DW_OP_xderef_size:
1996 dw2_asm_output_data (1, val1->v.val_int, NULL);
1997 break;
1999 case DW_OP_addr:
2000 if (loc->dtprel)
2002 if (targetm.asm_out.output_dwarf_dtprel)
2004 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2005 DWARF2_ADDR_SIZE,
2006 val1->v.val_addr);
2007 fputc ('\n', asm_out_file);
2009 else
2010 gcc_unreachable ();
2012 else
2014 #ifdef DWARF2_DEBUGGING_INFO
2015 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2016 #else
2017 gcc_unreachable ();
2018 #endif
2020 break;
2022 case DW_OP_GNU_addr_index:
2023 case DW_OP_GNU_const_index:
2024 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2025 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2026 "(index into .debug_addr)");
2027 break;
2029 case DW_OP_call2:
2030 case DW_OP_call4:
2032 unsigned long die_offset
2033 = get_ref_die_offset (val1->v.val_die_ref.die);
2034 /* Make sure the offset has been computed and that we can encode it as
2035 an operand. */
2036 gcc_assert (die_offset > 0
2037 && die_offset <= (loc->dw_loc_opc == DW_OP_call2
2038 ? 0xffff
2039 : 0xffffffff));
2040 dw2_asm_output_data ((loc->dw_loc_opc == DW_OP_call2) ? 2 : 4,
2041 die_offset, NULL);
2043 break;
2045 case DW_OP_GNU_implicit_pointer:
2047 char label[MAX_ARTIFICIAL_LABEL_BYTES
2048 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2049 gcc_assert (val1->val_class == dw_val_class_die_ref);
2050 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2051 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2052 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2054 break;
2056 case DW_OP_GNU_entry_value:
2057 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2058 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2059 break;
2061 case DW_OP_GNU_const_type:
2063 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2064 gcc_assert (o);
2065 dw2_asm_output_data_uleb128 (o, NULL);
2066 switch (val2->val_class)
2068 case dw_val_class_const:
2069 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2070 dw2_asm_output_data (1, l, NULL);
2071 dw2_asm_output_data (l, val2->v.val_int, NULL);
2072 break;
2073 case dw_val_class_vec:
2075 unsigned int elt_size = val2->v.val_vec.elt_size;
2076 unsigned int len = val2->v.val_vec.length;
2077 unsigned int i;
2078 unsigned char *p;
2080 l = len * elt_size;
2081 dw2_asm_output_data (1, l, NULL);
2082 if (elt_size > sizeof (HOST_WIDE_INT))
2084 elt_size /= 2;
2085 len *= 2;
2087 for (i = 0, p = val2->v.val_vec.array;
2088 i < len;
2089 i++, p += elt_size)
2090 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2091 "fp or vector constant word %u", i);
2093 break;
2094 case dw_val_class_const_double:
2096 unsigned HOST_WIDE_INT first, second;
2097 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2099 dw2_asm_output_data (1, 2 * l, NULL);
2100 if (WORDS_BIG_ENDIAN)
2102 first = val2->v.val_double.high;
2103 second = val2->v.val_double.low;
2105 else
2107 first = val2->v.val_double.low;
2108 second = val2->v.val_double.high;
2110 dw2_asm_output_data (l, first, NULL);
2111 dw2_asm_output_data (l, second, NULL);
2113 break;
2114 case dw_val_class_wide_int:
2116 int i;
2117 int len = get_full_len (*val2->v.val_wide);
2118 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2120 dw2_asm_output_data (1, len * l, NULL);
2121 if (WORDS_BIG_ENDIAN)
2122 for (i = len - 1; i >= 0; --i)
2123 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2124 else
2125 for (i = 0; i < len; ++i)
2126 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2128 break;
2129 default:
2130 gcc_unreachable ();
2133 break;
2134 case DW_OP_GNU_regval_type:
2136 unsigned r = val1->v.val_unsigned;
2137 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2138 gcc_assert (o);
2139 if (for_eh_or_skip >= 0)
2141 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2142 gcc_assert (size_of_uleb128 (r)
2143 == size_of_uleb128 (val1->v.val_unsigned));
2145 dw2_asm_output_data_uleb128 (r, NULL);
2146 dw2_asm_output_data_uleb128 (o, NULL);
2148 break;
2149 case DW_OP_GNU_deref_type:
2151 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2152 gcc_assert (o);
2153 dw2_asm_output_data (1, val1->v.val_int, NULL);
2154 dw2_asm_output_data_uleb128 (o, NULL);
2156 break;
2157 case DW_OP_GNU_convert:
2158 case DW_OP_GNU_reinterpret:
2159 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2160 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2161 else
2163 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2164 gcc_assert (o);
2165 dw2_asm_output_data_uleb128 (o, NULL);
2167 break;
2169 case DW_OP_GNU_parameter_ref:
2171 unsigned long o;
2172 gcc_assert (val1->val_class == dw_val_class_die_ref);
2173 o = get_ref_die_offset (val1->v.val_die_ref.die);
2174 dw2_asm_output_data (4, o, NULL);
2176 break;
2178 default:
2179 /* Other codes have no operands. */
2180 break;
2184 /* Output a sequence of location operations.
2185 The for_eh_or_skip parameter controls whether register numbers are
2186 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2187 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2188 info). This should be suppressed for the cases that have not been converted
2189 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2191 void
2192 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2194 for (; loc != NULL; loc = loc->dw_loc_next)
2196 enum dwarf_location_atom opc = loc->dw_loc_opc;
2197 /* Output the opcode. */
2198 if (for_eh_or_skip >= 0
2199 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2201 unsigned r = (opc - DW_OP_breg0);
2202 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2203 gcc_assert (r <= 31);
2204 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2206 else if (for_eh_or_skip >= 0
2207 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2209 unsigned r = (opc - DW_OP_reg0);
2210 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2211 gcc_assert (r <= 31);
2212 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2215 dw2_asm_output_data (1, opc,
2216 "%s", dwarf_stack_op_name (opc));
2218 /* Output the operand(s) (if any). */
2219 output_loc_operands (loc, for_eh_or_skip);
2223 /* Output location description stack opcode's operands (if any).
2224 The output is single bytes on a line, suitable for .cfi_escape. */
2226 static void
2227 output_loc_operands_raw (dw_loc_descr_ref loc)
2229 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2230 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2232 switch (loc->dw_loc_opc)
2234 case DW_OP_addr:
2235 case DW_OP_GNU_addr_index:
2236 case DW_OP_GNU_const_index:
2237 case DW_OP_implicit_value:
2238 /* We cannot output addresses in .cfi_escape, only bytes. */
2239 gcc_unreachable ();
2241 case DW_OP_const1u:
2242 case DW_OP_const1s:
2243 case DW_OP_pick:
2244 case DW_OP_deref_size:
2245 case DW_OP_xderef_size:
2246 fputc (',', asm_out_file);
2247 dw2_asm_output_data_raw (1, val1->v.val_int);
2248 break;
2250 case DW_OP_const2u:
2251 case DW_OP_const2s:
2252 fputc (',', asm_out_file);
2253 dw2_asm_output_data_raw (2, val1->v.val_int);
2254 break;
2256 case DW_OP_const4u:
2257 case DW_OP_const4s:
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_raw (4, val1->v.val_int);
2260 break;
2262 case DW_OP_const8u:
2263 case DW_OP_const8s:
2264 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2265 fputc (',', asm_out_file);
2266 dw2_asm_output_data_raw (8, val1->v.val_int);
2267 break;
2269 case DW_OP_skip:
2270 case DW_OP_bra:
2272 int offset;
2274 gcc_assert (val1->val_class == dw_val_class_loc);
2275 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2277 fputc (',', asm_out_file);
2278 dw2_asm_output_data_raw (2, offset);
2280 break;
2282 case DW_OP_regx:
2284 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2285 gcc_assert (size_of_uleb128 (r)
2286 == size_of_uleb128 (val1->v.val_unsigned));
2287 fputc (',', asm_out_file);
2288 dw2_asm_output_data_uleb128_raw (r);
2290 break;
2292 case DW_OP_constu:
2293 case DW_OP_plus_uconst:
2294 case DW_OP_piece:
2295 fputc (',', asm_out_file);
2296 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2297 break;
2299 case DW_OP_bit_piece:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2302 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2303 break;
2305 case DW_OP_consts:
2306 case DW_OP_breg0:
2307 case DW_OP_breg1:
2308 case DW_OP_breg2:
2309 case DW_OP_breg3:
2310 case DW_OP_breg4:
2311 case DW_OP_breg5:
2312 case DW_OP_breg6:
2313 case DW_OP_breg7:
2314 case DW_OP_breg8:
2315 case DW_OP_breg9:
2316 case DW_OP_breg10:
2317 case DW_OP_breg11:
2318 case DW_OP_breg12:
2319 case DW_OP_breg13:
2320 case DW_OP_breg14:
2321 case DW_OP_breg15:
2322 case DW_OP_breg16:
2323 case DW_OP_breg17:
2324 case DW_OP_breg18:
2325 case DW_OP_breg19:
2326 case DW_OP_breg20:
2327 case DW_OP_breg21:
2328 case DW_OP_breg22:
2329 case DW_OP_breg23:
2330 case DW_OP_breg24:
2331 case DW_OP_breg25:
2332 case DW_OP_breg26:
2333 case DW_OP_breg27:
2334 case DW_OP_breg28:
2335 case DW_OP_breg29:
2336 case DW_OP_breg30:
2337 case DW_OP_breg31:
2338 case DW_OP_fbreg:
2339 fputc (',', asm_out_file);
2340 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2341 break;
2343 case DW_OP_bregx:
2345 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2346 gcc_assert (size_of_uleb128 (r)
2347 == size_of_uleb128 (val1->v.val_unsigned));
2348 fputc (',', asm_out_file);
2349 dw2_asm_output_data_uleb128_raw (r);
2350 fputc (',', asm_out_file);
2351 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2353 break;
2355 case DW_OP_GNU_implicit_pointer:
2356 case DW_OP_GNU_entry_value:
2357 case DW_OP_GNU_const_type:
2358 case DW_OP_GNU_regval_type:
2359 case DW_OP_GNU_deref_type:
2360 case DW_OP_GNU_convert:
2361 case DW_OP_GNU_reinterpret:
2362 case DW_OP_GNU_parameter_ref:
2363 gcc_unreachable ();
2364 break;
2366 default:
2367 /* Other codes have no operands. */
2368 break;
2372 void
2373 output_loc_sequence_raw (dw_loc_descr_ref loc)
2375 while (1)
2377 enum dwarf_location_atom opc = loc->dw_loc_opc;
2378 /* Output the opcode. */
2379 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2381 unsigned r = (opc - DW_OP_breg0);
2382 r = DWARF2_FRAME_REG_OUT (r, 1);
2383 gcc_assert (r <= 31);
2384 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2386 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2388 unsigned r = (opc - DW_OP_reg0);
2389 r = DWARF2_FRAME_REG_OUT (r, 1);
2390 gcc_assert (r <= 31);
2391 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2393 /* Output the opcode. */
2394 fprintf (asm_out_file, "%#x", opc);
2395 output_loc_operands_raw (loc);
2397 if (!loc->dw_loc_next)
2398 break;
2399 loc = loc->dw_loc_next;
2401 fputc (',', asm_out_file);
2405 /* This function builds a dwarf location descriptor sequence from a
2406 dw_cfa_location, adding the given OFFSET to the result of the
2407 expression. */
2409 struct dw_loc_descr_node *
2410 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2412 struct dw_loc_descr_node *head, *tmp;
2414 offset += cfa->offset;
2416 if (cfa->indirect)
2418 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2419 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2420 head->dw_loc_oprnd1.val_entry = NULL;
2421 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2422 add_loc_descr (&head, tmp);
2423 if (offset != 0)
2425 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2426 add_loc_descr (&head, tmp);
2429 else
2430 head = new_reg_loc_descr (cfa->reg, offset);
2432 return head;
2435 /* This function builds a dwarf location descriptor sequence for
2436 the address at OFFSET from the CFA when stack is aligned to
2437 ALIGNMENT byte. */
2439 struct dw_loc_descr_node *
2440 build_cfa_aligned_loc (dw_cfa_location *cfa,
2441 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2443 struct dw_loc_descr_node *head;
2444 unsigned int dwarf_fp
2445 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2447 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2448 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2450 head = new_reg_loc_descr (dwarf_fp, 0);
2451 add_loc_descr (&head, int_loc_descriptor (alignment));
2452 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2453 loc_descr_plus_const (&head, offset);
2455 else
2456 head = new_reg_loc_descr (dwarf_fp, offset);
2457 return head;
2460 /* And now, the support for symbolic debugging information. */
2462 /* .debug_str support. */
2464 static void dwarf2out_init (const char *);
2465 static void dwarf2out_finish (const char *);
2466 static void dwarf2out_early_finish (const char *);
2467 static void dwarf2out_assembly_start (void);
2468 static void dwarf2out_define (unsigned int, const char *);
2469 static void dwarf2out_undef (unsigned int, const char *);
2470 static void dwarf2out_start_source_file (unsigned, const char *);
2471 static void dwarf2out_end_source_file (unsigned);
2472 static void dwarf2out_function_decl (tree);
2473 static void dwarf2out_begin_block (unsigned, unsigned);
2474 static void dwarf2out_end_block (unsigned, unsigned);
2475 static bool dwarf2out_ignore_block (const_tree);
2476 static void dwarf2out_early_global_decl (tree);
2477 static void dwarf2out_late_global_decl (tree);
2478 static void dwarf2out_type_decl (tree, int);
2479 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2480 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2481 dw_die_ref);
2482 static void dwarf2out_abstract_function (tree);
2483 static void dwarf2out_var_location (rtx_insn *);
2484 static void dwarf2out_size_function (tree);
2485 static void dwarf2out_begin_function (tree);
2486 static void dwarf2out_end_function (unsigned int);
2487 static void dwarf2out_register_main_translation_unit (tree unit);
2488 static void dwarf2out_set_name (tree, tree);
2490 /* The debug hooks structure. */
2492 const struct gcc_debug_hooks dwarf2_debug_hooks =
2494 dwarf2out_init,
2495 dwarf2out_finish,
2496 dwarf2out_early_finish,
2497 dwarf2out_assembly_start,
2498 dwarf2out_define,
2499 dwarf2out_undef,
2500 dwarf2out_start_source_file,
2501 dwarf2out_end_source_file,
2502 dwarf2out_begin_block,
2503 dwarf2out_end_block,
2504 dwarf2out_ignore_block,
2505 dwarf2out_source_line,
2506 dwarf2out_begin_prologue,
2507 #if VMS_DEBUGGING_INFO
2508 dwarf2out_vms_end_prologue,
2509 dwarf2out_vms_begin_epilogue,
2510 #else
2511 debug_nothing_int_charstar,
2512 debug_nothing_int_charstar,
2513 #endif
2514 dwarf2out_end_epilogue,
2515 dwarf2out_begin_function,
2516 dwarf2out_end_function, /* end_function */
2517 dwarf2out_register_main_translation_unit,
2518 dwarf2out_function_decl, /* function_decl */
2519 dwarf2out_early_global_decl,
2520 dwarf2out_late_global_decl,
2521 dwarf2out_type_decl, /* type_decl */
2522 dwarf2out_imported_module_or_decl,
2523 debug_nothing_tree, /* deferred_inline_function */
2524 /* The DWARF 2 backend tries to reduce debugging bloat by not
2525 emitting the abstract description of inline functions until
2526 something tries to reference them. */
2527 dwarf2out_abstract_function, /* outlining_inline_function */
2528 debug_nothing_rtx_code_label, /* label */
2529 debug_nothing_int, /* handle_pch */
2530 dwarf2out_var_location,
2531 dwarf2out_size_function, /* size_function */
2532 dwarf2out_switch_text_section,
2533 dwarf2out_set_name,
2534 1, /* start_end_main_source_file */
2535 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2538 const struct gcc_debug_hooks dwarf2_lineno_debug_hooks =
2540 dwarf2out_init,
2541 debug_nothing_charstar,
2542 debug_nothing_charstar,
2543 dwarf2out_assembly_start,
2544 debug_nothing_int_charstar,
2545 debug_nothing_int_charstar,
2546 debug_nothing_int_charstar,
2547 debug_nothing_int,
2548 debug_nothing_int_int, /* begin_block */
2549 debug_nothing_int_int, /* end_block */
2550 debug_true_const_tree, /* ignore_block */
2551 dwarf2out_source_line, /* source_line */
2552 debug_nothing_int_charstar, /* begin_prologue */
2553 debug_nothing_int_charstar, /* end_prologue */
2554 debug_nothing_int_charstar, /* begin_epilogue */
2555 debug_nothing_int_charstar, /* end_epilogue */
2556 debug_nothing_tree, /* begin_function */
2557 debug_nothing_int, /* end_function */
2558 debug_nothing_tree, /* register_main_translation_unit */
2559 debug_nothing_tree, /* function_decl */
2560 debug_nothing_tree, /* early_global_decl */
2561 debug_nothing_tree, /* late_global_decl */
2562 debug_nothing_tree_int, /* type_decl */
2563 debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
2564 debug_nothing_tree, /* deferred_inline_function */
2565 debug_nothing_tree, /* outlining_inline_function */
2566 debug_nothing_rtx_code_label, /* label */
2567 debug_nothing_int, /* handle_pch */
2568 debug_nothing_rtx_insn, /* var_location */
2569 debug_nothing_tree, /* size_function */
2570 debug_nothing_void, /* switch_text_section */
2571 debug_nothing_tree_tree, /* set_name */
2572 0, /* start_end_main_source_file */
2573 TYPE_SYMTAB_IS_ADDRESS /* tree_type_symtab_field */
2576 /* NOTE: In the comments in this file, many references are made to
2577 "Debugging Information Entries". This term is abbreviated as `DIE'
2578 throughout the remainder of this file. */
2580 /* An internal representation of the DWARF output is built, and then
2581 walked to generate the DWARF debugging info. The walk of the internal
2582 representation is done after the entire program has been compiled.
2583 The types below are used to describe the internal representation. */
2585 /* Whether to put type DIEs into their own section .debug_types instead
2586 of making them part of the .debug_info section. Only supported for
2587 Dwarf V4 or higher and the user didn't disable them through
2588 -fno-debug-types-section. It is more efficient to put them in a
2589 separate comdat sections since the linker will then be able to
2590 remove duplicates. But not all tools support .debug_types sections
2591 yet. */
2593 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2595 /* Various DIE's use offsets relative to the beginning of the
2596 .debug_info section to refer to each other. */
2598 typedef long int dw_offset;
2600 struct comdat_type_node;
2602 /* The entries in the line_info table more-or-less mirror the opcodes
2603 that are used in the real dwarf line table. Arrays of these entries
2604 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2605 supported. */
2607 enum dw_line_info_opcode {
2608 /* Emit DW_LNE_set_address; the operand is the label index. */
2609 LI_set_address,
2611 /* Emit a row to the matrix with the given line. This may be done
2612 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2613 special opcodes. */
2614 LI_set_line,
2616 /* Emit a DW_LNS_set_file. */
2617 LI_set_file,
2619 /* Emit a DW_LNS_set_column. */
2620 LI_set_column,
2622 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2623 LI_negate_stmt,
2625 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2626 LI_set_prologue_end,
2627 LI_set_epilogue_begin,
2629 /* Emit a DW_LNE_set_discriminator. */
2630 LI_set_discriminator
2633 typedef struct GTY(()) dw_line_info_struct {
2634 enum dw_line_info_opcode opcode;
2635 unsigned int val;
2636 } dw_line_info_entry;
2639 struct GTY(()) dw_line_info_table {
2640 /* The label that marks the end of this section. */
2641 const char *end_label;
2643 /* The values for the last row of the matrix, as collected in the table.
2644 These are used to minimize the changes to the next row. */
2645 unsigned int file_num;
2646 unsigned int line_num;
2647 unsigned int column_num;
2648 int discrim_num;
2649 bool is_stmt;
2650 bool in_use;
2652 vec<dw_line_info_entry, va_gc> *entries;
2656 /* Each DIE attribute has a field specifying the attribute kind,
2657 a link to the next attribute in the chain, and an attribute value.
2658 Attributes are typically linked below the DIE they modify. */
2660 typedef struct GTY(()) dw_attr_struct {
2661 enum dwarf_attribute dw_attr;
2662 dw_val_node dw_attr_val;
2664 dw_attr_node;
2667 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2668 The children of each node form a circular list linked by
2669 die_sib. die_child points to the node *before* the "first" child node. */
2671 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2672 union die_symbol_or_type_node
2674 const char * GTY ((tag ("0"))) die_symbol;
2675 comdat_type_node *GTY ((tag ("1"))) die_type_node;
2677 GTY ((desc ("%0.comdat_type_p"))) die_id;
2678 vec<dw_attr_node, va_gc> *die_attr;
2679 dw_die_ref die_parent;
2680 dw_die_ref die_child;
2681 dw_die_ref die_sib;
2682 dw_die_ref die_definition; /* ref from a specification to its definition */
2683 dw_offset die_offset;
2684 unsigned long die_abbrev;
2685 int die_mark;
2686 unsigned int decl_id;
2687 enum dwarf_tag die_tag;
2688 /* Die is used and must not be pruned as unused. */
2689 BOOL_BITFIELD die_perennial_p : 1;
2690 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2691 /* Whether this DIE was removed from the DIE tree, for example via
2692 prune_unused_types. We don't consider those present from the
2693 DIE lookup routines. */
2694 BOOL_BITFIELD removed : 1;
2695 /* Lots of spare bits. */
2697 die_node;
2699 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2700 static bool early_dwarf;
2701 static bool early_dwarf_finished;
2702 struct set_early_dwarf {
2703 bool saved;
2704 set_early_dwarf () : saved(early_dwarf)
2706 gcc_assert (! early_dwarf_finished);
2707 early_dwarf = true;
2709 ~set_early_dwarf () { early_dwarf = saved; }
2712 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2713 #define FOR_EACH_CHILD(die, c, expr) do { \
2714 c = die->die_child; \
2715 if (c) do { \
2716 c = c->die_sib; \
2717 expr; \
2718 } while (c != die->die_child); \
2719 } while (0)
2721 /* The pubname structure */
2723 typedef struct GTY(()) pubname_struct {
2724 dw_die_ref die;
2725 const char *name;
2727 pubname_entry;
2730 struct GTY(()) dw_ranges {
2731 /* If this is positive, it's a block number, otherwise it's a
2732 bitwise-negated index into dw_ranges_by_label. */
2733 int num;
2736 /* A structure to hold a macinfo entry. */
2738 typedef struct GTY(()) macinfo_struct {
2739 unsigned char code;
2740 unsigned HOST_WIDE_INT lineno;
2741 const char *info;
2743 macinfo_entry;
2746 struct GTY(()) dw_ranges_by_label {
2747 const char *begin;
2748 const char *end;
2751 /* The comdat type node structure. */
2752 struct GTY(()) comdat_type_node
2754 dw_die_ref root_die;
2755 dw_die_ref type_die;
2756 dw_die_ref skeleton_die;
2757 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2758 comdat_type_node *next;
2761 /* A list of DIEs for which we can't determine ancestry (parent_die
2762 field) just yet. Later in dwarf2out_finish we will fill in the
2763 missing bits. */
2764 typedef struct GTY(()) limbo_die_struct {
2765 dw_die_ref die;
2766 /* The tree for which this DIE was created. We use this to
2767 determine ancestry later. */
2768 tree created_for;
2769 struct limbo_die_struct *next;
2771 limbo_die_node;
2773 typedef struct skeleton_chain_struct
2775 dw_die_ref old_die;
2776 dw_die_ref new_die;
2777 struct skeleton_chain_struct *parent;
2779 skeleton_chain_node;
2781 /* Define a macro which returns nonzero for a TYPE_DECL which was
2782 implicitly generated for a type.
2784 Note that, unlike the C front-end (which generates a NULL named
2785 TYPE_DECL node for each complete tagged type, each array type,
2786 and each function type node created) the C++ front-end generates
2787 a _named_ TYPE_DECL node for each tagged type node created.
2788 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2789 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2790 front-end, but for each type, tagged or not. */
2792 #define TYPE_DECL_IS_STUB(decl) \
2793 (DECL_NAME (decl) == NULL_TREE \
2794 || (DECL_ARTIFICIAL (decl) \
2795 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2796 /* This is necessary for stub decls that \
2797 appear in nested inline functions. */ \
2798 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2799 && (decl_ultimate_origin (decl) \
2800 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2802 /* Information concerning the compilation unit's programming
2803 language, and compiler version. */
2805 /* Fixed size portion of the DWARF compilation unit header. */
2806 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2807 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2809 /* Fixed size portion of the DWARF comdat type unit header. */
2810 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2811 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2812 + DWARF_OFFSET_SIZE)
2814 /* Fixed size portion of public names info. */
2815 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2817 /* Fixed size portion of the address range info. */
2818 #define DWARF_ARANGES_HEADER_SIZE \
2819 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2820 DWARF2_ADDR_SIZE * 2) \
2821 - DWARF_INITIAL_LENGTH_SIZE)
2823 /* Size of padding portion in the address range info. It must be
2824 aligned to twice the pointer size. */
2825 #define DWARF_ARANGES_PAD_SIZE \
2826 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2827 DWARF2_ADDR_SIZE * 2) \
2828 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2830 /* Use assembler line directives if available. */
2831 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2832 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2833 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2834 #else
2835 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2836 #endif
2837 #endif
2839 /* Minimum line offset in a special line info. opcode.
2840 This value was chosen to give a reasonable range of values. */
2841 #define DWARF_LINE_BASE -10
2843 /* First special line opcode - leave room for the standard opcodes. */
2844 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2846 /* Range of line offsets in a special line info. opcode. */
2847 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2849 /* Flag that indicates the initial value of the is_stmt_start flag.
2850 In the present implementation, we do not mark any lines as
2851 the beginning of a source statement, because that information
2852 is not made available by the GCC front-end. */
2853 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2855 /* Maximum number of operations per instruction bundle. */
2856 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2857 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2858 #endif
2860 /* This location is used by calc_die_sizes() to keep track
2861 the offset of each DIE within the .debug_info section. */
2862 static unsigned long next_die_offset;
2864 /* Record the root of the DIE's built for the current compilation unit. */
2865 static GTY(()) dw_die_ref single_comp_unit_die;
2867 /* A list of type DIEs that have been separated into comdat sections. */
2868 static GTY(()) comdat_type_node *comdat_type_list;
2870 /* A list of CU DIEs that have been separated. */
2871 static GTY(()) limbo_die_node *cu_die_list;
2873 /* A list of DIEs with a NULL parent waiting to be relocated. */
2874 static GTY(()) limbo_die_node *limbo_die_list;
2876 /* A list of DIEs for which we may have to generate
2877 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2878 static GTY(()) limbo_die_node *deferred_asm_name;
2880 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2882 typedef const char *compare_type;
2884 static hashval_t hash (dwarf_file_data *);
2885 static bool equal (dwarf_file_data *, const char *);
2888 /* Filenames referenced by this compilation unit. */
2889 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2891 struct decl_die_hasher : ggc_ptr_hash<die_node>
2893 typedef tree compare_type;
2895 static hashval_t hash (die_node *);
2896 static bool equal (die_node *, tree);
2898 /* A hash table of references to DIE's that describe declarations.
2899 The key is a DECL_UID() which is a unique number identifying each decl. */
2900 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2902 struct block_die_hasher : ggc_ptr_hash<die_struct>
2904 static hashval_t hash (die_struct *);
2905 static bool equal (die_struct *, die_struct *);
2908 /* A hash table of references to DIE's that describe COMMON blocks.
2909 The key is DECL_UID() ^ die_parent. */
2910 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2912 typedef struct GTY(()) die_arg_entry_struct {
2913 dw_die_ref die;
2914 tree arg;
2915 } die_arg_entry;
2918 /* Node of the variable location list. */
2919 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2920 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2921 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2922 in mode of the EXPR_LIST node and first EXPR_LIST operand
2923 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2924 location or NULL for padding. For larger bitsizes,
2925 mode is 0 and first operand is a CONCAT with bitsize
2926 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2927 NULL as second operand. */
2928 rtx GTY (()) loc;
2929 const char * GTY (()) label;
2930 struct var_loc_node * GTY (()) next;
2933 /* Variable location list. */
2934 struct GTY ((for_user)) var_loc_list_def {
2935 struct var_loc_node * GTY (()) first;
2937 /* Pointer to the last but one or last element of the
2938 chained list. If the list is empty, both first and
2939 last are NULL, if the list contains just one node
2940 or the last node certainly is not redundant, it points
2941 to the last node, otherwise points to the last but one.
2942 Do not mark it for GC because it is marked through the chain. */
2943 struct var_loc_node * GTY ((skip ("%h"))) last;
2945 /* Pointer to the last element before section switch,
2946 if NULL, either sections weren't switched or first
2947 is after section switch. */
2948 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2950 /* DECL_UID of the variable decl. */
2951 unsigned int decl_id;
2953 typedef struct var_loc_list_def var_loc_list;
2955 /* Call argument location list. */
2956 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2957 rtx GTY (()) call_arg_loc_note;
2958 const char * GTY (()) label;
2959 tree GTY (()) block;
2960 bool tail_call_p;
2961 rtx GTY (()) symbol_ref;
2962 struct call_arg_loc_node * GTY (()) next;
2966 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2968 typedef const_tree compare_type;
2970 static hashval_t hash (var_loc_list *);
2971 static bool equal (var_loc_list *, const_tree);
2974 /* Table of decl location linked lists. */
2975 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2977 /* Head and tail of call_arg_loc chain. */
2978 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2979 static struct call_arg_loc_node *call_arg_loc_last;
2981 /* Number of call sites in the current function. */
2982 static int call_site_count = -1;
2983 /* Number of tail call sites in the current function. */
2984 static int tail_call_site_count = -1;
2986 /* A cached location list. */
2987 struct GTY ((for_user)) cached_dw_loc_list_def {
2988 /* The DECL_UID of the decl that this entry describes. */
2989 unsigned int decl_id;
2991 /* The cached location list. */
2992 dw_loc_list_ref loc_list;
2994 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2996 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2999 typedef const_tree compare_type;
3001 static hashval_t hash (cached_dw_loc_list *);
3002 static bool equal (cached_dw_loc_list *, const_tree);
3005 /* Table of cached location lists. */
3006 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
3008 /* A pointer to the base of a list of references to DIE's that
3009 are uniquely identified by their tag, presence/absence of
3010 children DIE's, and list of attribute/value pairs. */
3011 static GTY((length ("abbrev_die_table_allocated")))
3012 dw_die_ref *abbrev_die_table;
3014 /* Number of elements currently allocated for abbrev_die_table. */
3015 static GTY(()) unsigned abbrev_die_table_allocated;
3017 /* Number of elements in abbrev_die_table currently in use. */
3018 static GTY(()) unsigned abbrev_die_table_in_use;
3020 /* A hash map to remember the stack usage for DWARF procedures. The value
3021 stored is the stack size difference between before the DWARF procedure
3022 invokation and after it returned. In other words, for a DWARF procedure
3023 that consumes N stack slots and that pushes M ones, this stores M - N. */
3024 static hash_map<dw_die_ref, int> *dwarf_proc_stack_usage_map;
3026 /* Size (in elements) of increments by which we may expand the
3027 abbrev_die_table. */
3028 #define ABBREV_DIE_TABLE_INCREMENT 256
3030 /* A global counter for generating labels for line number data. */
3031 static unsigned int line_info_label_num;
3033 /* The current table to which we should emit line number information
3034 for the current function. This will be set up at the beginning of
3035 assembly for the function. */
3036 static GTY(()) dw_line_info_table *cur_line_info_table;
3038 /* The two default tables of line number info. */
3039 static GTY(()) dw_line_info_table *text_section_line_info;
3040 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3042 /* The set of all non-default tables of line number info. */
3043 static GTY(()) vec<dw_line_info_table *, va_gc> *separate_line_info;
3045 /* A flag to tell pubnames/types export if there is an info section to
3046 refer to. */
3047 static bool info_section_emitted;
3049 /* A pointer to the base of a table that contains a list of publicly
3050 accessible names. */
3051 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
3053 /* A pointer to the base of a table that contains a list of publicly
3054 accessible types. */
3055 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
3057 /* A pointer to the base of a table that contains a list of macro
3058 defines/undefines (and file start/end markers). */
3059 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
3061 /* True if .debug_macinfo or .debug_macros section is going to be
3062 emitted. */
3063 #define have_macinfo \
3064 ((!XCOFF_DEBUGGING_INFO || HAVE_XCOFF_DWARF_EXTRAS) \
3065 && debug_info_level >= DINFO_LEVEL_VERBOSE \
3066 && !macinfo_table->is_empty ())
3068 /* Array of dies for which we should generate .debug_ranges info. */
3069 static GTY ((length ("ranges_table_allocated"))) dw_ranges *ranges_table;
3071 /* Number of elements currently allocated for ranges_table. */
3072 static GTY(()) unsigned ranges_table_allocated;
3074 /* Number of elements in ranges_table currently in use. */
3075 static GTY(()) unsigned ranges_table_in_use;
3077 /* Array of pairs of labels referenced in ranges_table. */
3078 static GTY ((length ("ranges_by_label_allocated")))
3079 dw_ranges_by_label *ranges_by_label;
3081 /* Number of elements currently allocated for ranges_by_label. */
3082 static GTY(()) unsigned ranges_by_label_allocated;
3084 /* Number of elements in ranges_by_label currently in use. */
3085 static GTY(()) unsigned ranges_by_label_in_use;
3087 /* Size (in elements) of increments by which we may expand the
3088 ranges_table. */
3089 #define RANGES_TABLE_INCREMENT 64
3091 /* Whether we have location lists that need outputting */
3092 static GTY(()) bool have_location_lists;
3094 /* Unique label counter. */
3095 static GTY(()) unsigned int loclabel_num;
3097 /* Unique label counter for point-of-call tables. */
3098 static GTY(()) unsigned int poc_label_num;
3100 /* The last file entry emitted by maybe_emit_file(). */
3101 static GTY(()) struct dwarf_file_data * last_emitted_file;
3103 /* Number of internal labels generated by gen_internal_sym(). */
3104 static GTY(()) int label_num;
3106 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3108 /* Instances of generic types for which we need to generate debug
3109 info that describe their generic parameters and arguments. That
3110 generation needs to happen once all types are properly laid out so
3111 we do it at the end of compilation. */
3112 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3114 /* Offset from the "steady-state frame pointer" to the frame base,
3115 within the current function. */
3116 static HOST_WIDE_INT frame_pointer_fb_offset;
3117 static bool frame_pointer_fb_offset_valid;
3119 static vec<dw_die_ref> base_types;
3121 /* Pointer to vector of DW_TAG_string_type DIEs that need finalization
3122 once all arguments are parsed. */
3123 static vec<dw_die_ref> *string_types;
3125 /* Flags to represent a set of attribute classes for attributes that represent
3126 a scalar value (bounds, pointers, ...). */
3127 enum dw_scalar_form
3129 dw_scalar_form_constant = 0x01,
3130 dw_scalar_form_exprloc = 0x02,
3131 dw_scalar_form_reference = 0x04
3134 /* Forward declarations for functions defined in this file. */
3136 static int is_pseudo_reg (const_rtx);
3137 static tree type_main_variant (tree);
3138 static int is_tagged_type (const_tree);
3139 static const char *dwarf_tag_name (unsigned);
3140 static const char *dwarf_attr_name (unsigned);
3141 static const char *dwarf_form_name (unsigned);
3142 static tree decl_ultimate_origin (const_tree);
3143 static tree decl_class_context (tree);
3144 static void add_dwarf_attr (dw_die_ref, dw_attr_node *);
3145 static inline enum dw_val_class AT_class (dw_attr_node *);
3146 static inline unsigned int AT_index (dw_attr_node *);
3147 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3148 static inline unsigned AT_flag (dw_attr_node *);
3149 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3150 static inline HOST_WIDE_INT AT_int (dw_attr_node *);
3151 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3152 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_node *);
3153 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3154 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3155 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3156 unsigned int, unsigned char *);
3157 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3158 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3159 static inline const char *AT_string (dw_attr_node *);
3160 static enum dwarf_form AT_string_form (dw_attr_node *);
3161 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3162 static void add_AT_specification (dw_die_ref, dw_die_ref);
3163 static inline dw_die_ref AT_ref (dw_attr_node *);
3164 static inline int AT_ref_external (dw_attr_node *);
3165 static inline void set_AT_ref_external (dw_attr_node *, int);
3166 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3167 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3168 static inline dw_loc_descr_ref AT_loc (dw_attr_node *);
3169 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3170 dw_loc_list_ref);
3171 static inline dw_loc_list_ref AT_loc_list (dw_attr_node *);
3172 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3173 static void remove_addr_table_entry (addr_table_entry *);
3174 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3175 static inline rtx AT_addr (dw_attr_node *);
3176 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3177 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3178 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3179 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3180 unsigned HOST_WIDE_INT);
3181 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3182 unsigned long, bool);
3183 static inline const char *AT_lbl (dw_attr_node *);
3184 static dw_attr_node *get_AT (dw_die_ref, enum dwarf_attribute);
3185 static const char *get_AT_low_pc (dw_die_ref);
3186 static const char *get_AT_hi_pc (dw_die_ref);
3187 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3188 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3189 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3190 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3191 static bool is_cxx (void);
3192 static bool is_fortran (void);
3193 static bool is_ada (void);
3194 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3195 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3196 static void add_child_die (dw_die_ref, dw_die_ref);
3197 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3198 static dw_die_ref lookup_type_die (tree);
3199 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3200 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3201 static void equate_type_number_to_die (tree, dw_die_ref);
3202 static dw_die_ref lookup_decl_die (tree);
3203 static var_loc_list *lookup_decl_loc (const_tree);
3204 static void equate_decl_number_to_die (tree, dw_die_ref);
3205 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3206 static void print_spaces (FILE *);
3207 static void print_die (dw_die_ref, FILE *);
3208 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3209 static dw_die_ref pop_compile_unit (dw_die_ref);
3210 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3211 static void attr_checksum (dw_attr_node *, struct md5_ctx *, int *);
3212 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3213 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3214 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3215 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3216 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_node *,
3217 struct md5_ctx *, int *);
3218 struct checksum_attributes;
3219 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3220 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3221 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3222 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3223 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3224 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3225 static int same_attr_p (dw_attr_node *, dw_attr_node *, int *);
3226 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3227 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3228 static void compute_section_prefix (dw_die_ref);
3229 static int is_type_die (dw_die_ref);
3230 static int is_comdat_die (dw_die_ref);
3231 static int is_symbol_die (dw_die_ref);
3232 static inline bool is_template_instantiation (dw_die_ref);
3233 static void assign_symbol_names (dw_die_ref);
3234 static void break_out_includes (dw_die_ref);
3235 static int is_declaration_die (dw_die_ref);
3236 static int should_move_die_to_comdat (dw_die_ref);
3237 static dw_die_ref clone_as_declaration (dw_die_ref);
3238 static dw_die_ref clone_die (dw_die_ref);
3239 static dw_die_ref clone_tree (dw_die_ref);
3240 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3241 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3242 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3243 static dw_die_ref generate_skeleton (dw_die_ref);
3244 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3245 dw_die_ref,
3246 dw_die_ref);
3247 static void break_out_comdat_types (dw_die_ref);
3248 static void copy_decls_for_unworthy_types (dw_die_ref);
3250 static void add_sibling_attributes (dw_die_ref);
3251 static void output_location_lists (dw_die_ref);
3252 static int constant_size (unsigned HOST_WIDE_INT);
3253 static unsigned long size_of_die (dw_die_ref);
3254 static void calc_die_sizes (dw_die_ref);
3255 static void calc_base_type_die_sizes (void);
3256 static void mark_dies (dw_die_ref);
3257 static void unmark_dies (dw_die_ref);
3258 static void unmark_all_dies (dw_die_ref);
3259 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3260 static unsigned long size_of_aranges (void);
3261 static enum dwarf_form value_format (dw_attr_node *);
3262 static void output_value_format (dw_attr_node *);
3263 static void output_abbrev_section (void);
3264 static void output_die_abbrevs (unsigned long, dw_die_ref);
3265 static void output_die_symbol (dw_die_ref);
3266 static void output_die (dw_die_ref);
3267 static void output_compilation_unit_header (void);
3268 static void output_comp_unit (dw_die_ref, int);
3269 static void output_comdat_type_unit (comdat_type_node *);
3270 static const char *dwarf2_name (tree, int);
3271 static void add_pubname (tree, dw_die_ref);
3272 static void add_enumerator_pubname (const char *, dw_die_ref);
3273 static void add_pubname_string (const char *, dw_die_ref);
3274 static void add_pubtype (tree, dw_die_ref);
3275 static void output_pubnames (vec<pubname_entry, va_gc> *);
3276 static void output_aranges (void);
3277 static unsigned int add_ranges_num (int);
3278 static unsigned int add_ranges (const_tree);
3279 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3280 bool *, bool);
3281 static void output_ranges (void);
3282 static dw_line_info_table *new_line_info_table (void);
3283 static void output_line_info (bool);
3284 static void output_file_names (void);
3285 static dw_die_ref base_type_die (tree, bool);
3286 static int is_base_type (tree);
3287 static dw_die_ref subrange_type_die (tree, tree, tree, tree, dw_die_ref);
3288 static int decl_quals (const_tree);
3289 static dw_die_ref modified_type_die (tree, int, bool, dw_die_ref);
3290 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3291 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3292 static int type_is_enum (const_tree);
3293 static unsigned int dbx_reg_number (const_rtx);
3294 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3295 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3296 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3297 enum var_init_status);
3298 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3299 enum var_init_status);
3300 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3301 enum var_init_status);
3302 static int is_based_loc (const_rtx);
3303 static bool resolve_one_addr (rtx *);
3304 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3305 enum var_init_status);
3306 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3307 enum var_init_status);
3308 struct loc_descr_context;
3309 static void add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref);
3310 static void add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list);
3311 static dw_loc_list_ref loc_list_from_tree (tree, int,
3312 const struct loc_descr_context *);
3313 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3314 const struct loc_descr_context *);
3315 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3316 static tree field_type (const_tree);
3317 static unsigned int simple_type_align_in_bits (const_tree);
3318 static unsigned int simple_decl_align_in_bits (const_tree);
3319 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3320 struct vlr_context;
3321 static dw_loc_descr_ref field_byte_offset (const_tree, struct vlr_context *,
3322 HOST_WIDE_INT *);
3323 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3324 dw_loc_list_ref);
3325 static void add_data_member_location_attribute (dw_die_ref, tree,
3326 struct vlr_context *);
3327 static bool add_const_value_attribute (dw_die_ref, rtx);
3328 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3329 static void insert_wide_int (const wide_int &, unsigned char *, int);
3330 static void insert_float (const_rtx, unsigned char *);
3331 static rtx rtl_for_decl_location (tree);
3332 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool);
3333 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3334 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3335 static void add_name_attribute (dw_die_ref, const char *);
3336 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3337 static void add_comp_dir_attribute (dw_die_ref);
3338 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3339 const struct loc_descr_context *);
3340 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3341 const struct loc_descr_context *);
3342 static void add_subscript_info (dw_die_ref, tree, bool);
3343 static void add_byte_size_attribute (dw_die_ref, tree);
3344 static inline void add_bit_offset_attribute (dw_die_ref, tree,
3345 struct vlr_context *);
3346 static void add_bit_size_attribute (dw_die_ref, tree);
3347 static void add_prototyped_attribute (dw_die_ref, tree);
3348 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3349 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3350 static void add_src_coords_attributes (dw_die_ref, tree);
3351 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3352 static void add_discr_value (dw_die_ref, dw_discr_value *);
3353 static void add_discr_list (dw_die_ref, dw_discr_list_ref);
3354 static inline dw_discr_list_ref AT_discr_list (dw_attr_node *);
3355 static void push_decl_scope (tree);
3356 static void pop_decl_scope (void);
3357 static dw_die_ref scope_die_for (tree, dw_die_ref);
3358 static inline int local_scope_p (dw_die_ref);
3359 static inline int class_scope_p (dw_die_ref);
3360 static inline int class_or_namespace_scope_p (dw_die_ref);
3361 static void add_type_attribute (dw_die_ref, tree, int, bool, dw_die_ref);
3362 static void add_calling_convention_attribute (dw_die_ref, tree);
3363 static const char *type_tag (const_tree);
3364 static tree member_declared_type (const_tree);
3365 #if 0
3366 static const char *decl_start_label (tree);
3367 #endif
3368 static void gen_array_type_die (tree, dw_die_ref);
3369 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3370 #if 0
3371 static void gen_entry_point_die (tree, dw_die_ref);
3372 #endif
3373 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3374 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3375 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3376 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3377 static void gen_formal_types_die (tree, dw_die_ref);
3378 static void gen_subprogram_die (tree, dw_die_ref);
3379 static void gen_variable_die (tree, tree, dw_die_ref);
3380 static void gen_const_die (tree, dw_die_ref);
3381 static void gen_label_die (tree, dw_die_ref);
3382 static void gen_lexical_block_die (tree, dw_die_ref);
3383 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3384 static void gen_field_die (tree, struct vlr_context *, dw_die_ref);
3385 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3386 static dw_die_ref gen_compile_unit_die (const char *);
3387 static void gen_inheritance_die (tree, tree, tree, dw_die_ref);
3388 static void gen_member_die (tree, dw_die_ref);
3389 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3390 enum debug_info_usage);
3391 static void gen_subroutine_type_die (tree, dw_die_ref);
3392 static void gen_typedef_die (tree, dw_die_ref);
3393 static void gen_type_die (tree, dw_die_ref);
3394 static void gen_block_die (tree, dw_die_ref);
3395 static void decls_for_scope (tree, dw_die_ref);
3396 static bool is_naming_typedef_decl (const_tree);
3397 static inline dw_die_ref get_context_die (tree);
3398 static void gen_namespace_die (tree, dw_die_ref);
3399 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3400 static dw_die_ref gen_decl_die (tree, tree, struct vlr_context *, dw_die_ref);
3401 static dw_die_ref force_decl_die (tree);
3402 static dw_die_ref force_type_die (tree);
3403 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3404 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3405 static struct dwarf_file_data * lookup_filename (const char *);
3406 static void retry_incomplete_types (void);
3407 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3408 static void gen_generic_params_dies (tree);
3409 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3410 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3411 static void splice_child_die (dw_die_ref, dw_die_ref);
3412 static int file_info_cmp (const void *, const void *);
3413 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3414 const char *, const char *);
3415 static void output_loc_list (dw_loc_list_ref);
3416 static char *gen_internal_sym (const char *);
3417 static bool want_pubnames (void);
3419 static void prune_unmark_dies (dw_die_ref);
3420 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3421 static void prune_unused_types_mark (dw_die_ref, int);
3422 static void prune_unused_types_walk (dw_die_ref);
3423 static void prune_unused_types_walk_attribs (dw_die_ref);
3424 static void prune_unused_types_prune (dw_die_ref);
3425 static void prune_unused_types (void);
3426 static int maybe_emit_file (struct dwarf_file_data *fd);
3427 static inline const char *AT_vms_delta1 (dw_attr_node *);
3428 static inline const char *AT_vms_delta2 (dw_attr_node *);
3429 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3430 const char *, const char *);
3431 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3432 static void gen_remaining_tmpl_value_param_die_attribute (void);
3433 static bool generic_type_p (tree);
3434 static void schedule_generic_params_dies_gen (tree t);
3435 static void gen_scheduled_generic_parms_dies (void);
3437 static const char *comp_dir_string (void);
3439 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3441 /* enum for tracking thread-local variables whose address is really an offset
3442 relative to the TLS pointer, which will need link-time relocation, but will
3443 not need relocation by the DWARF consumer. */
3445 enum dtprel_bool
3447 dtprel_false = 0,
3448 dtprel_true = 1
3451 /* Return the operator to use for an address of a variable. For dtprel_true, we
3452 use DW_OP_const*. For regular variables, which need both link-time
3453 relocation and consumer-level relocation (e.g., to account for shared objects
3454 loaded at a random address), we use DW_OP_addr*. */
3456 static inline enum dwarf_location_atom
3457 dw_addr_op (enum dtprel_bool dtprel)
3459 if (dtprel == dtprel_true)
3460 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3461 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3462 else
3463 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3466 /* Return a pointer to a newly allocated address location description. If
3467 dwarf_split_debug_info is true, then record the address with the appropriate
3468 relocation. */
3469 static inline dw_loc_descr_ref
3470 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3472 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3474 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3475 ref->dw_loc_oprnd1.v.val_addr = addr;
3476 ref->dtprel = dtprel;
3477 if (dwarf_split_debug_info)
3478 ref->dw_loc_oprnd1.val_entry
3479 = add_addr_table_entry (addr,
3480 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3481 else
3482 ref->dw_loc_oprnd1.val_entry = NULL;
3484 return ref;
3487 /* Section names used to hold DWARF debugging information. */
3489 #ifndef DEBUG_INFO_SECTION
3490 #define DEBUG_INFO_SECTION ".debug_info"
3491 #endif
3492 #ifndef DEBUG_DWO_INFO_SECTION
3493 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3494 #endif
3495 #ifndef DEBUG_ABBREV_SECTION
3496 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3497 #endif
3498 #ifndef DEBUG_DWO_ABBREV_SECTION
3499 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3500 #endif
3501 #ifndef DEBUG_ARANGES_SECTION
3502 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3503 #endif
3504 #ifndef DEBUG_ADDR_SECTION
3505 #define DEBUG_ADDR_SECTION ".debug_addr"
3506 #endif
3507 #ifndef DEBUG_MACINFO_SECTION
3508 #define DEBUG_MACINFO_SECTION ".debug_macinfo"
3509 #endif
3510 #ifndef DEBUG_DWO_MACINFO_SECTION
3511 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3512 #endif
3513 #ifndef DEBUG_DWO_MACRO_SECTION
3514 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3515 #endif
3516 #ifndef DEBUG_MACRO_SECTION
3517 #define DEBUG_MACRO_SECTION ".debug_macro"
3518 #endif
3519 #ifndef DEBUG_LINE_SECTION
3520 #define DEBUG_LINE_SECTION ".debug_line"
3521 #endif
3522 #ifndef DEBUG_DWO_LINE_SECTION
3523 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3524 #endif
3525 #ifndef DEBUG_LOC_SECTION
3526 #define DEBUG_LOC_SECTION ".debug_loc"
3527 #endif
3528 #ifndef DEBUG_DWO_LOC_SECTION
3529 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3530 #endif
3531 #ifndef DEBUG_PUBNAMES_SECTION
3532 #define DEBUG_PUBNAMES_SECTION \
3533 ((debug_generate_pub_sections == 2) \
3534 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3535 #endif
3536 #ifndef DEBUG_PUBTYPES_SECTION
3537 #define DEBUG_PUBTYPES_SECTION \
3538 ((debug_generate_pub_sections == 2) \
3539 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3540 #endif
3541 #ifndef DEBUG_STR_OFFSETS_SECTION
3542 #define DEBUG_STR_OFFSETS_SECTION ".debug_str_offsets"
3543 #endif
3544 #ifndef DEBUG_DWO_STR_OFFSETS_SECTION
3545 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3546 #endif
3547 #ifndef DEBUG_STR_DWO_SECTION
3548 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3549 #endif
3550 #ifndef DEBUG_STR_SECTION
3551 #define DEBUG_STR_SECTION ".debug_str"
3552 #endif
3553 #ifndef DEBUG_RANGES_SECTION
3554 #define DEBUG_RANGES_SECTION ".debug_ranges"
3555 #endif
3557 /* Standard ELF section names for compiled code and data. */
3558 #ifndef TEXT_SECTION_NAME
3559 #define TEXT_SECTION_NAME ".text"
3560 #endif
3562 /* Section flags for .debug_str section. */
3563 #define DEBUG_STR_SECTION_FLAGS \
3564 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3565 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3566 : SECTION_DEBUG)
3568 /* Section flags for .debug_str.dwo section. */
3569 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3571 /* Labels we insert at beginning sections we can reference instead of
3572 the section names themselves. */
3574 #ifndef TEXT_SECTION_LABEL
3575 #define TEXT_SECTION_LABEL "Ltext"
3576 #endif
3577 #ifndef COLD_TEXT_SECTION_LABEL
3578 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3579 #endif
3580 #ifndef DEBUG_LINE_SECTION_LABEL
3581 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3582 #endif
3583 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3584 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3585 #endif
3586 #ifndef DEBUG_INFO_SECTION_LABEL
3587 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3588 #endif
3589 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3590 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3591 #endif
3592 #ifndef DEBUG_ABBREV_SECTION_LABEL
3593 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3594 #endif
3595 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3596 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3597 #endif
3598 #ifndef DEBUG_ADDR_SECTION_LABEL
3599 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3600 #endif
3601 #ifndef DEBUG_LOC_SECTION_LABEL
3602 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3603 #endif
3604 #ifndef DEBUG_RANGES_SECTION_LABEL
3605 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3606 #endif
3607 #ifndef DEBUG_MACINFO_SECTION_LABEL
3608 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3609 #endif
3610 #ifndef DEBUG_MACRO_SECTION_LABEL
3611 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3612 #endif
3613 #define SKELETON_COMP_DIE_ABBREV 1
3614 #define SKELETON_TYPE_DIE_ABBREV 2
3616 /* Definitions of defaults for formats and names of various special
3617 (artificial) labels which may be generated within this file (when the -g
3618 options is used and DWARF2_DEBUGGING_INFO is in effect.
3619 If necessary, these may be overridden from within the tm.h file, but
3620 typically, overriding these defaults is unnecessary. */
3622 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3623 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3624 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3625 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3626 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3627 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3628 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3629 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3630 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3631 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3632 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3633 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3634 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3635 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3637 #ifndef TEXT_END_LABEL
3638 #define TEXT_END_LABEL "Letext"
3639 #endif
3640 #ifndef COLD_END_LABEL
3641 #define COLD_END_LABEL "Letext_cold"
3642 #endif
3643 #ifndef BLOCK_BEGIN_LABEL
3644 #define BLOCK_BEGIN_LABEL "LBB"
3645 #endif
3646 #ifndef BLOCK_END_LABEL
3647 #define BLOCK_END_LABEL "LBE"
3648 #endif
3649 #ifndef LINE_CODE_LABEL
3650 #define LINE_CODE_LABEL "LM"
3651 #endif
3654 /* Return the root of the DIE's built for the current compilation unit. */
3655 static dw_die_ref
3656 comp_unit_die (void)
3658 if (!single_comp_unit_die)
3659 single_comp_unit_die = gen_compile_unit_die (NULL);
3660 return single_comp_unit_die;
3663 /* We allow a language front-end to designate a function that is to be
3664 called to "demangle" any name before it is put into a DIE. */
3666 static const char *(*demangle_name_func) (const char *);
3668 void
3669 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3671 demangle_name_func = func;
3674 /* Test if rtl node points to a pseudo register. */
3676 static inline int
3677 is_pseudo_reg (const_rtx rtl)
3679 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3680 || (GET_CODE (rtl) == SUBREG
3681 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3684 /* Return a reference to a type, with its const and volatile qualifiers
3685 removed. */
3687 static inline tree
3688 type_main_variant (tree type)
3690 type = TYPE_MAIN_VARIANT (type);
3692 /* ??? There really should be only one main variant among any group of
3693 variants of a given type (and all of the MAIN_VARIANT values for all
3694 members of the group should point to that one type) but sometimes the C
3695 front-end messes this up for array types, so we work around that bug
3696 here. */
3697 if (TREE_CODE (type) == ARRAY_TYPE)
3698 while (type != TYPE_MAIN_VARIANT (type))
3699 type = TYPE_MAIN_VARIANT (type);
3701 return type;
3704 /* Return nonzero if the given type node represents a tagged type. */
3706 static inline int
3707 is_tagged_type (const_tree type)
3709 enum tree_code code = TREE_CODE (type);
3711 return (code == RECORD_TYPE || code == UNION_TYPE
3712 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3715 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3717 static void
3718 get_ref_die_offset_label (char *label, dw_die_ref ref)
3720 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3723 /* Return die_offset of a DIE reference to a base type. */
3725 static unsigned long int
3726 get_base_type_offset (dw_die_ref ref)
3728 if (ref->die_offset)
3729 return ref->die_offset;
3730 if (comp_unit_die ()->die_abbrev)
3732 calc_base_type_die_sizes ();
3733 gcc_assert (ref->die_offset);
3735 return ref->die_offset;
3738 /* Return die_offset of a DIE reference other than base type. */
3740 static unsigned long int
3741 get_ref_die_offset (dw_die_ref ref)
3743 gcc_assert (ref->die_offset);
3744 return ref->die_offset;
3747 /* Convert a DIE tag into its string name. */
3749 static const char *
3750 dwarf_tag_name (unsigned int tag)
3752 const char *name = get_DW_TAG_name (tag);
3754 if (name != NULL)
3755 return name;
3757 return "DW_TAG_<unknown>";
3760 /* Convert a DWARF attribute code into its string name. */
3762 static const char *
3763 dwarf_attr_name (unsigned int attr)
3765 const char *name;
3767 switch (attr)
3769 #if VMS_DEBUGGING_INFO
3770 case DW_AT_HP_prologue:
3771 return "DW_AT_HP_prologue";
3772 #else
3773 case DW_AT_MIPS_loop_unroll_factor:
3774 return "DW_AT_MIPS_loop_unroll_factor";
3775 #endif
3777 #if VMS_DEBUGGING_INFO
3778 case DW_AT_HP_epilogue:
3779 return "DW_AT_HP_epilogue";
3780 #else
3781 case DW_AT_MIPS_stride:
3782 return "DW_AT_MIPS_stride";
3783 #endif
3786 name = get_DW_AT_name (attr);
3788 if (name != NULL)
3789 return name;
3791 return "DW_AT_<unknown>";
3794 /* Convert a DWARF value form code into its string name. */
3796 static const char *
3797 dwarf_form_name (unsigned int form)
3799 const char *name = get_DW_FORM_name (form);
3801 if (name != NULL)
3802 return name;
3804 return "DW_FORM_<unknown>";
3807 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3808 instance of an inlined instance of a decl which is local to an inline
3809 function, so we have to trace all of the way back through the origin chain
3810 to find out what sort of node actually served as the original seed for the
3811 given block. */
3813 static tree
3814 decl_ultimate_origin (const_tree decl)
3816 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3817 return NULL_TREE;
3819 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3820 we're trying to output the abstract instance of this function. */
3821 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3822 return NULL_TREE;
3824 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3825 most distant ancestor, this should never happen. */
3826 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3828 return DECL_ABSTRACT_ORIGIN (decl);
3831 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3832 of a virtual function may refer to a base class, so we check the 'this'
3833 parameter. */
3835 static tree
3836 decl_class_context (tree decl)
3838 tree context = NULL_TREE;
3840 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3841 context = DECL_CONTEXT (decl);
3842 else
3843 context = TYPE_MAIN_VARIANT
3844 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3846 if (context && !TYPE_P (context))
3847 context = NULL_TREE;
3849 return context;
3852 /* Add an attribute/value pair to a DIE. */
3854 static inline void
3855 add_dwarf_attr (dw_die_ref die, dw_attr_node *attr)
3857 /* Maybe this should be an assert? */
3858 if (die == NULL)
3859 return;
3861 vec_safe_reserve (die->die_attr, 1);
3862 vec_safe_push (die->die_attr, *attr);
3865 static inline enum dw_val_class
3866 AT_class (dw_attr_node *a)
3868 return a->dw_attr_val.val_class;
3871 /* Return the index for any attribute that will be referenced with a
3872 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3873 are stored in dw_attr_val.v.val_str for reference counting
3874 pruning. */
3876 static inline unsigned int
3877 AT_index (dw_attr_node *a)
3879 if (AT_class (a) == dw_val_class_str)
3880 return a->dw_attr_val.v.val_str->index;
3881 else if (a->dw_attr_val.val_entry != NULL)
3882 return a->dw_attr_val.val_entry->index;
3883 return NOT_INDEXED;
3886 /* Add a flag value attribute to a DIE. */
3888 static inline void
3889 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3891 dw_attr_node attr;
3893 attr.dw_attr = attr_kind;
3894 attr.dw_attr_val.val_class = dw_val_class_flag;
3895 attr.dw_attr_val.val_entry = NULL;
3896 attr.dw_attr_val.v.val_flag = flag;
3897 add_dwarf_attr (die, &attr);
3900 static inline unsigned
3901 AT_flag (dw_attr_node *a)
3903 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3904 return a->dw_attr_val.v.val_flag;
3907 /* Add a signed integer attribute value to a DIE. */
3909 static inline void
3910 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3912 dw_attr_node attr;
3914 attr.dw_attr = attr_kind;
3915 attr.dw_attr_val.val_class = dw_val_class_const;
3916 attr.dw_attr_val.val_entry = NULL;
3917 attr.dw_attr_val.v.val_int = int_val;
3918 add_dwarf_attr (die, &attr);
3921 static inline HOST_WIDE_INT
3922 AT_int (dw_attr_node *a)
3924 gcc_assert (a && AT_class (a) == dw_val_class_const);
3925 return a->dw_attr_val.v.val_int;
3928 /* Add an unsigned integer attribute value to a DIE. */
3930 static inline void
3931 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3932 unsigned HOST_WIDE_INT unsigned_val)
3934 dw_attr_node attr;
3936 attr.dw_attr = attr_kind;
3937 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3938 attr.dw_attr_val.val_entry = NULL;
3939 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3940 add_dwarf_attr (die, &attr);
3943 static inline unsigned HOST_WIDE_INT
3944 AT_unsigned (dw_attr_node *a)
3946 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3947 return a->dw_attr_val.v.val_unsigned;
3950 /* Add an unsigned wide integer attribute value to a DIE. */
3952 static inline void
3953 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3954 const wide_int& w)
3956 dw_attr_node attr;
3958 attr.dw_attr = attr_kind;
3959 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3960 attr.dw_attr_val.val_entry = NULL;
3961 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3962 *attr.dw_attr_val.v.val_wide = w;
3963 add_dwarf_attr (die, &attr);
3966 /* Add an unsigned double integer attribute value to a DIE. */
3968 static inline void
3969 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3970 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3972 dw_attr_node attr;
3974 attr.dw_attr = attr_kind;
3975 attr.dw_attr_val.val_class = dw_val_class_const_double;
3976 attr.dw_attr_val.val_entry = NULL;
3977 attr.dw_attr_val.v.val_double.high = high;
3978 attr.dw_attr_val.v.val_double.low = low;
3979 add_dwarf_attr (die, &attr);
3982 /* Add a floating point attribute value to a DIE and return it. */
3984 static inline void
3985 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3986 unsigned int length, unsigned int elt_size, unsigned char *array)
3988 dw_attr_node attr;
3990 attr.dw_attr = attr_kind;
3991 attr.dw_attr_val.val_class = dw_val_class_vec;
3992 attr.dw_attr_val.val_entry = NULL;
3993 attr.dw_attr_val.v.val_vec.length = length;
3994 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3995 attr.dw_attr_val.v.val_vec.array = array;
3996 add_dwarf_attr (die, &attr);
3999 /* Add an 8-byte data attribute value to a DIE. */
4001 static inline void
4002 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4003 unsigned char data8[8])
4005 dw_attr_node attr;
4007 attr.dw_attr = attr_kind;
4008 attr.dw_attr_val.val_class = dw_val_class_data8;
4009 attr.dw_attr_val.val_entry = NULL;
4010 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4011 add_dwarf_attr (die, &attr);
4014 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
4015 dwarf_split_debug_info, address attributes in dies destined for the
4016 final executable have force_direct set to avoid using indexed
4017 references. */
4019 static inline void
4020 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
4021 bool force_direct)
4023 dw_attr_node attr;
4024 char * lbl_id;
4026 lbl_id = xstrdup (lbl_low);
4027 attr.dw_attr = DW_AT_low_pc;
4028 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4029 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4030 if (dwarf_split_debug_info && !force_direct)
4031 attr.dw_attr_val.val_entry
4032 = add_addr_table_entry (lbl_id, ate_kind_label);
4033 else
4034 attr.dw_attr_val.val_entry = NULL;
4035 add_dwarf_attr (die, &attr);
4037 attr.dw_attr = DW_AT_high_pc;
4038 if (dwarf_version < 4)
4039 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4040 else
4041 attr.dw_attr_val.val_class = dw_val_class_high_pc;
4042 lbl_id = xstrdup (lbl_high);
4043 attr.dw_attr_val.v.val_lbl_id = lbl_id;
4044 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
4045 && dwarf_split_debug_info && !force_direct)
4046 attr.dw_attr_val.val_entry
4047 = add_addr_table_entry (lbl_id, ate_kind_label);
4048 else
4049 attr.dw_attr_val.val_entry = NULL;
4050 add_dwarf_attr (die, &attr);
4053 /* Hash and equality functions for debug_str_hash. */
4055 hashval_t
4056 indirect_string_hasher::hash (indirect_string_node *x)
4058 return htab_hash_string (x->str);
4061 bool
4062 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
4064 return strcmp (x1->str, x2) == 0;
4067 /* Add STR to the given string hash table. */
4069 static struct indirect_string_node *
4070 find_AT_string_in_table (const char *str,
4071 hash_table<indirect_string_hasher> *table)
4073 struct indirect_string_node *node;
4075 indirect_string_node **slot
4076 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
4077 if (*slot == NULL)
4079 node = ggc_cleared_alloc<indirect_string_node> ();
4080 node->str = ggc_strdup (str);
4081 *slot = node;
4083 else
4084 node = *slot;
4086 node->refcount++;
4087 return node;
4090 /* Add STR to the indirect string hash table. */
4092 static struct indirect_string_node *
4093 find_AT_string (const char *str)
4095 if (! debug_str_hash)
4096 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4098 return find_AT_string_in_table (str, debug_str_hash);
4101 /* Add a string attribute value to a DIE. */
4103 static inline void
4104 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4106 dw_attr_node attr;
4107 struct indirect_string_node *node;
4109 node = find_AT_string (str);
4111 attr.dw_attr = attr_kind;
4112 attr.dw_attr_val.val_class = dw_val_class_str;
4113 attr.dw_attr_val.val_entry = NULL;
4114 attr.dw_attr_val.v.val_str = node;
4115 add_dwarf_attr (die, &attr);
4118 static inline const char *
4119 AT_string (dw_attr_node *a)
4121 gcc_assert (a && AT_class (a) == dw_val_class_str);
4122 return a->dw_attr_val.v.val_str->str;
4125 /* Call this function directly to bypass AT_string_form's logic to put
4126 the string inline in the die. */
4128 static void
4129 set_indirect_string (struct indirect_string_node *node)
4131 char label[MAX_ARTIFICIAL_LABEL_BYTES];
4132 /* Already indirect is a no op. */
4133 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4135 gcc_assert (node->label);
4136 return;
4138 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4139 ++dw2_string_counter;
4140 node->label = xstrdup (label);
4142 if (!dwarf_split_debug_info)
4144 node->form = DW_FORM_strp;
4145 node->index = NOT_INDEXED;
4147 else
4149 node->form = DW_FORM_GNU_str_index;
4150 node->index = NO_INDEX_ASSIGNED;
4154 /* Find out whether a string should be output inline in DIE
4155 or out-of-line in .debug_str section. */
4157 static enum dwarf_form
4158 find_string_form (struct indirect_string_node *node)
4160 unsigned int len;
4162 if (node->form)
4163 return node->form;
4165 len = strlen (node->str) + 1;
4167 /* If the string is shorter or equal to the size of the reference, it is
4168 always better to put it inline. */
4169 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4170 return node->form = DW_FORM_string;
4172 /* If we cannot expect the linker to merge strings in .debug_str
4173 section, only put it into .debug_str if it is worth even in this
4174 single module. */
4175 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4176 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4177 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4178 return node->form = DW_FORM_string;
4180 set_indirect_string (node);
4182 return node->form;
4185 /* Find out whether the string referenced from the attribute should be
4186 output inline in DIE or out-of-line in .debug_str section. */
4188 static enum dwarf_form
4189 AT_string_form (dw_attr_node *a)
4191 gcc_assert (a && AT_class (a) == dw_val_class_str);
4192 return find_string_form (a->dw_attr_val.v.val_str);
4195 /* Add a DIE reference attribute value to a DIE. */
4197 static inline void
4198 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4200 dw_attr_node attr;
4201 gcc_checking_assert (targ_die != NULL);
4203 /* With LTO we can end up trying to reference something we didn't create
4204 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4205 if (targ_die == NULL)
4206 return;
4208 attr.dw_attr = attr_kind;
4209 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4210 attr.dw_attr_val.val_entry = NULL;
4211 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4212 attr.dw_attr_val.v.val_die_ref.external = 0;
4213 add_dwarf_attr (die, &attr);
4216 /* Change DIE reference REF to point to NEW_DIE instead. */
4218 static inline void
4219 change_AT_die_ref (dw_attr_node *ref, dw_die_ref new_die)
4221 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4222 ref->dw_attr_val.v.val_die_ref.die = new_die;
4223 ref->dw_attr_val.v.val_die_ref.external = 0;
4226 /* Add an AT_specification attribute to a DIE, and also make the back
4227 pointer from the specification to the definition. */
4229 static inline void
4230 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4232 add_AT_die_ref (die, DW_AT_specification, targ_die);
4233 gcc_assert (!targ_die->die_definition);
4234 targ_die->die_definition = die;
4237 static inline dw_die_ref
4238 AT_ref (dw_attr_node *a)
4240 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4241 return a->dw_attr_val.v.val_die_ref.die;
4244 static inline int
4245 AT_ref_external (dw_attr_node *a)
4247 if (a && AT_class (a) == dw_val_class_die_ref)
4248 return a->dw_attr_val.v.val_die_ref.external;
4250 return 0;
4253 static inline void
4254 set_AT_ref_external (dw_attr_node *a, int i)
4256 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4257 a->dw_attr_val.v.val_die_ref.external = i;
4260 /* Add an FDE reference attribute value to a DIE. */
4262 static inline void
4263 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4265 dw_attr_node attr;
4267 attr.dw_attr = attr_kind;
4268 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4269 attr.dw_attr_val.val_entry = NULL;
4270 attr.dw_attr_val.v.val_fde_index = targ_fde;
4271 add_dwarf_attr (die, &attr);
4274 /* Add a location description attribute value to a DIE. */
4276 static inline void
4277 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4279 dw_attr_node attr;
4281 attr.dw_attr = attr_kind;
4282 attr.dw_attr_val.val_class = dw_val_class_loc;
4283 attr.dw_attr_val.val_entry = NULL;
4284 attr.dw_attr_val.v.val_loc = loc;
4285 add_dwarf_attr (die, &attr);
4288 static inline dw_loc_descr_ref
4289 AT_loc (dw_attr_node *a)
4291 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4292 return a->dw_attr_val.v.val_loc;
4295 static inline void
4296 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4298 dw_attr_node attr;
4300 if (XCOFF_DEBUGGING_INFO && !HAVE_XCOFF_DWARF_EXTRAS)
4301 return;
4303 attr.dw_attr = attr_kind;
4304 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4305 attr.dw_attr_val.val_entry = NULL;
4306 attr.dw_attr_val.v.val_loc_list = loc_list;
4307 add_dwarf_attr (die, &attr);
4308 have_location_lists = true;
4311 static inline dw_loc_list_ref
4312 AT_loc_list (dw_attr_node *a)
4314 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4315 return a->dw_attr_val.v.val_loc_list;
4318 static inline dw_loc_list_ref *
4319 AT_loc_list_ptr (dw_attr_node *a)
4321 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4322 return &a->dw_attr_val.v.val_loc_list;
4325 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4327 static hashval_t hash (addr_table_entry *);
4328 static bool equal (addr_table_entry *, addr_table_entry *);
4331 /* Table of entries into the .debug_addr section. */
4333 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4335 /* Hash an address_table_entry. */
4337 hashval_t
4338 addr_hasher::hash (addr_table_entry *a)
4340 inchash::hash hstate;
4341 switch (a->kind)
4343 case ate_kind_rtx:
4344 hstate.add_int (0);
4345 break;
4346 case ate_kind_rtx_dtprel:
4347 hstate.add_int (1);
4348 break;
4349 case ate_kind_label:
4350 return htab_hash_string (a->addr.label);
4351 default:
4352 gcc_unreachable ();
4354 inchash::add_rtx (a->addr.rtl, hstate);
4355 return hstate.end ();
4358 /* Determine equality for two address_table_entries. */
4360 bool
4361 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4363 if (a1->kind != a2->kind)
4364 return 0;
4365 switch (a1->kind)
4367 case ate_kind_rtx:
4368 case ate_kind_rtx_dtprel:
4369 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4370 case ate_kind_label:
4371 return strcmp (a1->addr.label, a2->addr.label) == 0;
4372 default:
4373 gcc_unreachable ();
4377 /* Initialize an addr_table_entry. */
4379 void
4380 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4382 e->kind = kind;
4383 switch (kind)
4385 case ate_kind_rtx:
4386 case ate_kind_rtx_dtprel:
4387 e->addr.rtl = (rtx) addr;
4388 break;
4389 case ate_kind_label:
4390 e->addr.label = (char *) addr;
4391 break;
4393 e->refcount = 0;
4394 e->index = NO_INDEX_ASSIGNED;
4397 /* Add attr to the address table entry to the table. Defer setting an
4398 index until output time. */
4400 static addr_table_entry *
4401 add_addr_table_entry (void *addr, enum ate_kind kind)
4403 addr_table_entry *node;
4404 addr_table_entry finder;
4406 gcc_assert (dwarf_split_debug_info);
4407 if (! addr_index_table)
4408 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4409 init_addr_table_entry (&finder, kind, addr);
4410 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4412 if (*slot == HTAB_EMPTY_ENTRY)
4414 node = ggc_cleared_alloc<addr_table_entry> ();
4415 init_addr_table_entry (node, kind, addr);
4416 *slot = node;
4418 else
4419 node = *slot;
4421 node->refcount++;
4422 return node;
4425 /* Remove an entry from the addr table by decrementing its refcount.
4426 Strictly, decrementing the refcount would be enough, but the
4427 assertion that the entry is actually in the table has found
4428 bugs. */
4430 static void
4431 remove_addr_table_entry (addr_table_entry *entry)
4433 gcc_assert (dwarf_split_debug_info && addr_index_table);
4434 /* After an index is assigned, the table is frozen. */
4435 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4436 entry->refcount--;
4439 /* Given a location list, remove all addresses it refers to from the
4440 address_table. */
4442 static void
4443 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4445 for (; descr; descr = descr->dw_loc_next)
4446 if (descr->dw_loc_oprnd1.val_entry != NULL)
4448 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4449 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4453 /* A helper function for dwarf2out_finish called through
4454 htab_traverse. Assign an addr_table_entry its index. All entries
4455 must be collected into the table when this function is called,
4456 because the indexing code relies on htab_traverse to traverse nodes
4457 in the same order for each run. */
4460 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4462 addr_table_entry *node = *h;
4464 /* Don't index unreferenced nodes. */
4465 if (node->refcount == 0)
4466 return 1;
4468 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4469 node->index = *index;
4470 *index += 1;
4472 return 1;
4475 /* Add an address constant attribute value to a DIE. When using
4476 dwarf_split_debug_info, address attributes in dies destined for the
4477 final executable should be direct references--setting the parameter
4478 force_direct ensures this behavior. */
4480 static inline void
4481 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4482 bool force_direct)
4484 dw_attr_node attr;
4486 attr.dw_attr = attr_kind;
4487 attr.dw_attr_val.val_class = dw_val_class_addr;
4488 attr.dw_attr_val.v.val_addr = addr;
4489 if (dwarf_split_debug_info && !force_direct)
4490 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4491 else
4492 attr.dw_attr_val.val_entry = NULL;
4493 add_dwarf_attr (die, &attr);
4496 /* Get the RTX from to an address DIE attribute. */
4498 static inline rtx
4499 AT_addr (dw_attr_node *a)
4501 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4502 return a->dw_attr_val.v.val_addr;
4505 /* Add a file attribute value to a DIE. */
4507 static inline void
4508 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4509 struct dwarf_file_data *fd)
4511 dw_attr_node attr;
4513 attr.dw_attr = attr_kind;
4514 attr.dw_attr_val.val_class = dw_val_class_file;
4515 attr.dw_attr_val.val_entry = NULL;
4516 attr.dw_attr_val.v.val_file = fd;
4517 add_dwarf_attr (die, &attr);
4520 /* Get the dwarf_file_data from a file DIE attribute. */
4522 static inline struct dwarf_file_data *
4523 AT_file (dw_attr_node *a)
4525 gcc_assert (a && AT_class (a) == dw_val_class_file);
4526 return a->dw_attr_val.v.val_file;
4529 /* Add a vms delta attribute value to a DIE. */
4531 static inline void
4532 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4533 const char *lbl1, const char *lbl2)
4535 dw_attr_node attr;
4537 attr.dw_attr = attr_kind;
4538 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4539 attr.dw_attr_val.val_entry = NULL;
4540 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4541 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4542 add_dwarf_attr (die, &attr);
4545 /* Add a label identifier attribute value to a DIE. */
4547 static inline void
4548 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4549 const char *lbl_id)
4551 dw_attr_node attr;
4553 attr.dw_attr = attr_kind;
4554 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4555 attr.dw_attr_val.val_entry = NULL;
4556 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4557 if (dwarf_split_debug_info)
4558 attr.dw_attr_val.val_entry
4559 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4560 ate_kind_label);
4561 add_dwarf_attr (die, &attr);
4564 /* Add a section offset attribute value to a DIE, an offset into the
4565 debug_line section. */
4567 static inline void
4568 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4569 const char *label)
4571 dw_attr_node attr;
4573 attr.dw_attr = attr_kind;
4574 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4575 attr.dw_attr_val.val_entry = NULL;
4576 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4577 add_dwarf_attr (die, &attr);
4580 /* Add a section offset attribute value to a DIE, an offset into the
4581 debug_macinfo section. */
4583 static inline void
4584 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4585 const char *label)
4587 dw_attr_node attr;
4589 attr.dw_attr = attr_kind;
4590 attr.dw_attr_val.val_class = dw_val_class_macptr;
4591 attr.dw_attr_val.val_entry = NULL;
4592 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4593 add_dwarf_attr (die, &attr);
4596 /* Add an offset attribute value to a DIE. */
4598 static inline void
4599 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4600 unsigned HOST_WIDE_INT offset)
4602 dw_attr_node attr;
4604 attr.dw_attr = attr_kind;
4605 attr.dw_attr_val.val_class = dw_val_class_offset;
4606 attr.dw_attr_val.val_entry = NULL;
4607 attr.dw_attr_val.v.val_offset = offset;
4608 add_dwarf_attr (die, &attr);
4611 /* Add a range_list attribute value to a DIE. When using
4612 dwarf_split_debug_info, address attributes in dies destined for the
4613 final executable should be direct references--setting the parameter
4614 force_direct ensures this behavior. */
4616 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4617 #define RELOCATED_OFFSET (NULL)
4619 static void
4620 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4621 long unsigned int offset, bool force_direct)
4623 dw_attr_node attr;
4625 attr.dw_attr = attr_kind;
4626 attr.dw_attr_val.val_class = dw_val_class_range_list;
4627 /* For the range_list attribute, use val_entry to store whether the
4628 offset should follow split-debug-info or normal semantics. This
4629 value is read in output_range_list_offset. */
4630 if (dwarf_split_debug_info && !force_direct)
4631 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4632 else
4633 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4634 attr.dw_attr_val.v.val_offset = offset;
4635 add_dwarf_attr (die, &attr);
4638 /* Return the start label of a delta attribute. */
4640 static inline const char *
4641 AT_vms_delta1 (dw_attr_node *a)
4643 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4644 return a->dw_attr_val.v.val_vms_delta.lbl1;
4647 /* Return the end label of a delta attribute. */
4649 static inline const char *
4650 AT_vms_delta2 (dw_attr_node *a)
4652 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4653 return a->dw_attr_val.v.val_vms_delta.lbl2;
4656 static inline const char *
4657 AT_lbl (dw_attr_node *a)
4659 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4660 || AT_class (a) == dw_val_class_lineptr
4661 || AT_class (a) == dw_val_class_macptr
4662 || AT_class (a) == dw_val_class_high_pc));
4663 return a->dw_attr_val.v.val_lbl_id;
4666 /* Get the attribute of type attr_kind. */
4668 static dw_attr_node *
4669 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4671 dw_attr_node *a;
4672 unsigned ix;
4673 dw_die_ref spec = NULL;
4675 if (! die)
4676 return NULL;
4678 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4679 if (a->dw_attr == attr_kind)
4680 return a;
4681 else if (a->dw_attr == DW_AT_specification
4682 || a->dw_attr == DW_AT_abstract_origin)
4683 spec = AT_ref (a);
4685 if (spec)
4686 return get_AT (spec, attr_kind);
4688 return NULL;
4691 /* Returns the parent of the declaration of DIE. */
4693 static dw_die_ref
4694 get_die_parent (dw_die_ref die)
4696 dw_die_ref t;
4698 if (!die)
4699 return NULL;
4701 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4702 || (t = get_AT_ref (die, DW_AT_specification)))
4703 die = t;
4705 return die->die_parent;
4708 /* Return the "low pc" attribute value, typically associated with a subprogram
4709 DIE. Return null if the "low pc" attribute is either not present, or if it
4710 cannot be represented as an assembler label identifier. */
4712 static inline const char *
4713 get_AT_low_pc (dw_die_ref die)
4715 dw_attr_node *a = get_AT (die, DW_AT_low_pc);
4717 return a ? AT_lbl (a) : NULL;
4720 /* Return the "high pc" attribute value, typically associated with a subprogram
4721 DIE. Return null if the "high pc" attribute is either not present, or if it
4722 cannot be represented as an assembler label identifier. */
4724 static inline const char *
4725 get_AT_hi_pc (dw_die_ref die)
4727 dw_attr_node *a = get_AT (die, DW_AT_high_pc);
4729 return a ? AT_lbl (a) : NULL;
4732 /* Return the value of the string attribute designated by ATTR_KIND, or
4733 NULL if it is not present. */
4735 static inline const char *
4736 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4738 dw_attr_node *a = get_AT (die, attr_kind);
4740 return a ? AT_string (a) : NULL;
4743 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4744 if it is not present. */
4746 static inline int
4747 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4749 dw_attr_node *a = get_AT (die, attr_kind);
4751 return a ? AT_flag (a) : 0;
4754 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4755 if it is not present. */
4757 static inline unsigned
4758 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4760 dw_attr_node *a = get_AT (die, attr_kind);
4762 return a ? AT_unsigned (a) : 0;
4765 static inline dw_die_ref
4766 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4768 dw_attr_node *a = get_AT (die, attr_kind);
4770 return a ? AT_ref (a) : NULL;
4773 static inline struct dwarf_file_data *
4774 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4776 dw_attr_node *a = get_AT (die, attr_kind);
4778 return a ? AT_file (a) : NULL;
4781 /* Return TRUE if the language is C++. */
4783 static inline bool
4784 is_cxx (void)
4786 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4788 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4789 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4792 /* Return TRUE if the language is Java. */
4794 static inline bool
4795 is_java (void)
4797 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4799 return lang == DW_LANG_Java;
4802 /* Return TRUE if the language is Fortran. */
4804 static inline bool
4805 is_fortran (void)
4807 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4809 return (lang == DW_LANG_Fortran77
4810 || lang == DW_LANG_Fortran90
4811 || lang == DW_LANG_Fortran95
4812 || lang == DW_LANG_Fortran03
4813 || lang == DW_LANG_Fortran08);
4816 /* Return TRUE if the language is Ada. */
4818 static inline bool
4819 is_ada (void)
4821 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4823 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4826 /* Remove the specified attribute if present. Return TRUE if removal
4827 was successful. */
4829 static bool
4830 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4832 dw_attr_node *a;
4833 unsigned ix;
4835 if (! die)
4836 return false;
4838 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4839 if (a->dw_attr == attr_kind)
4841 if (AT_class (a) == dw_val_class_str)
4842 if (a->dw_attr_val.v.val_str->refcount)
4843 a->dw_attr_val.v.val_str->refcount--;
4845 /* vec::ordered_remove should help reduce the number of abbrevs
4846 that are needed. */
4847 die->die_attr->ordered_remove (ix);
4848 return true;
4850 return false;
4853 /* Remove CHILD from its parent. PREV must have the property that
4854 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4856 static void
4857 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4859 gcc_assert (child->die_parent == prev->die_parent);
4860 gcc_assert (prev->die_sib == child);
4861 if (prev == child)
4863 gcc_assert (child->die_parent->die_child == child);
4864 prev = NULL;
4866 else
4867 prev->die_sib = child->die_sib;
4868 if (child->die_parent->die_child == child)
4869 child->die_parent->die_child = prev;
4870 child->die_sib = NULL;
4873 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4874 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4876 static void
4877 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4879 dw_die_ref parent = old_child->die_parent;
4881 gcc_assert (parent == prev->die_parent);
4882 gcc_assert (prev->die_sib == old_child);
4884 new_child->die_parent = parent;
4885 if (prev == old_child)
4887 gcc_assert (parent->die_child == old_child);
4888 new_child->die_sib = new_child;
4890 else
4892 prev->die_sib = new_child;
4893 new_child->die_sib = old_child->die_sib;
4895 if (old_child->die_parent->die_child == old_child)
4896 old_child->die_parent->die_child = new_child;
4897 old_child->die_sib = NULL;
4900 /* Move all children from OLD_PARENT to NEW_PARENT. */
4902 static void
4903 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4905 dw_die_ref c;
4906 new_parent->die_child = old_parent->die_child;
4907 old_parent->die_child = NULL;
4908 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4911 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4912 matches TAG. */
4914 static void
4915 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4917 dw_die_ref c;
4919 c = die->die_child;
4920 if (c) do {
4921 dw_die_ref prev = c;
4922 c = c->die_sib;
4923 while (c->die_tag == tag)
4925 remove_child_with_prev (c, prev);
4926 c->die_parent = NULL;
4927 /* Might have removed every child. */
4928 if (die->die_child == NULL)
4929 return;
4930 c = prev->die_sib;
4932 } while (c != die->die_child);
4935 /* Add a CHILD_DIE as the last child of DIE. */
4937 static void
4938 add_child_die (dw_die_ref die, dw_die_ref child_die)
4940 /* FIXME this should probably be an assert. */
4941 if (! die || ! child_die)
4942 return;
4943 gcc_assert (die != child_die);
4945 child_die->die_parent = die;
4946 if (die->die_child)
4948 child_die->die_sib = die->die_child->die_sib;
4949 die->die_child->die_sib = child_die;
4951 else
4952 child_die->die_sib = child_die;
4953 die->die_child = child_die;
4956 /* Like add_child_die, but put CHILD_DIE after AFTER_DIE. */
4958 static void
4959 add_child_die_after (dw_die_ref die, dw_die_ref child_die,
4960 dw_die_ref after_die)
4962 gcc_assert (die
4963 && child_die
4964 && after_die
4965 && die->die_child
4966 && die != child_die);
4968 child_die->die_parent = die;
4969 child_die->die_sib = after_die->die_sib;
4970 after_die->die_sib = child_die;
4971 if (die->die_child == after_die)
4972 die->die_child = child_die;
4975 /* Unassociate CHILD from its parent, and make its parent be
4976 NEW_PARENT. */
4978 static void
4979 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4981 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4982 if (p->die_sib == child)
4984 remove_child_with_prev (child, p);
4985 break;
4987 add_child_die (new_parent, child);
4990 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4991 is the specification, to the end of PARENT's list of children.
4992 This is done by removing and re-adding it. */
4994 static void
4995 splice_child_die (dw_die_ref parent, dw_die_ref child)
4997 /* We want the declaration DIE from inside the class, not the
4998 specification DIE at toplevel. */
4999 if (child->die_parent != parent)
5001 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5003 if (tmp)
5004 child = tmp;
5007 gcc_assert (child->die_parent == parent
5008 || (child->die_parent
5009 == get_AT_ref (parent, DW_AT_specification)));
5011 reparent_child (child, parent);
5014 /* Create and return a new die with a parent of PARENT_DIE. If
5015 PARENT_DIE is NULL, the new DIE is placed in limbo and an
5016 associated tree T must be supplied to determine parenthood
5017 later. */
5019 static inline dw_die_ref
5020 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5022 dw_die_ref die = ggc_cleared_alloc<die_node> ();
5024 die->die_tag = tag_value;
5026 if (parent_die != NULL)
5027 add_child_die (parent_die, die);
5028 else
5030 limbo_die_node *limbo_node;
5032 /* No DIEs created after early dwarf should end up in limbo,
5033 because the limbo list should not persist past LTO
5034 streaming. */
5035 if (tag_value != DW_TAG_compile_unit
5036 /* These are allowed because they're generated while
5037 breaking out COMDAT units late. */
5038 && tag_value != DW_TAG_type_unit
5039 && !early_dwarf
5040 /* Allow nested functions to live in limbo because they will
5041 only temporarily live there, as decls_for_scope will fix
5042 them up. */
5043 && (TREE_CODE (t) != FUNCTION_DECL
5044 || !decl_function_context (t))
5045 /* Same as nested functions above but for types. Types that
5046 are local to a function will be fixed in
5047 decls_for_scope. */
5048 && (!RECORD_OR_UNION_TYPE_P (t)
5049 || !TYPE_CONTEXT (t)
5050 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
5051 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
5052 especially in the ltrans stage, but once we implement LTO
5053 dwarf streaming, we should remove this exception. */
5054 && !in_lto_p)
5056 fprintf (stderr, "symbol ended up in limbo too late:");
5057 debug_generic_stmt (t);
5058 gcc_unreachable ();
5061 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
5062 limbo_node->die = die;
5063 limbo_node->created_for = t;
5064 limbo_node->next = limbo_die_list;
5065 limbo_die_list = limbo_node;
5068 return die;
5071 /* Return the DIE associated with the given type specifier. */
5073 static inline dw_die_ref
5074 lookup_type_die (tree type)
5076 dw_die_ref die = TYPE_SYMTAB_DIE (type);
5077 if (die && die->removed)
5079 TYPE_SYMTAB_DIE (type) = NULL;
5080 return NULL;
5082 return die;
5085 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5086 anonymous type named by the typedef TYPE_DIE, return the DIE of the
5087 anonymous type instead the one of the naming typedef. */
5089 static inline dw_die_ref
5090 strip_naming_typedef (tree type, dw_die_ref type_die)
5092 if (type
5093 && TREE_CODE (type) == RECORD_TYPE
5094 && type_die
5095 && type_die->die_tag == DW_TAG_typedef
5096 && is_naming_typedef_decl (TYPE_NAME (type)))
5097 type_die = get_AT_ref (type_die, DW_AT_type);
5098 return type_die;
5101 /* Like lookup_type_die, but if type is an anonymous type named by a
5102 typedef[1], return the DIE of the anonymous type instead the one of
5103 the naming typedef. This is because in gen_typedef_die, we did
5104 equate the anonymous struct named by the typedef with the DIE of
5105 the naming typedef. So by default, lookup_type_die on an anonymous
5106 struct yields the DIE of the naming typedef.
5108 [1]: Read the comment of is_naming_typedef_decl to learn about what
5109 a naming typedef is. */
5111 static inline dw_die_ref
5112 lookup_type_die_strip_naming_typedef (tree type)
5114 dw_die_ref die = lookup_type_die (type);
5115 return strip_naming_typedef (type, die);
5118 /* Equate a DIE to a given type specifier. */
5120 static inline void
5121 equate_type_number_to_die (tree type, dw_die_ref type_die)
5123 TYPE_SYMTAB_DIE (type) = type_die;
5126 /* Returns a hash value for X (which really is a die_struct). */
5128 inline hashval_t
5129 decl_die_hasher::hash (die_node *x)
5131 return (hashval_t) x->decl_id;
5134 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5136 inline bool
5137 decl_die_hasher::equal (die_node *x, tree y)
5139 return (x->decl_id == DECL_UID (y));
5142 /* Return the DIE associated with a given declaration. */
5144 static inline dw_die_ref
5145 lookup_decl_die (tree decl)
5147 dw_die_ref *die = decl_die_table->find_slot_with_hash (decl, DECL_UID (decl),
5148 NO_INSERT);
5149 if (!die)
5150 return NULL;
5151 if ((*die)->removed)
5153 decl_die_table->clear_slot (die);
5154 return NULL;
5156 return *die;
5159 /* Returns a hash value for X (which really is a var_loc_list). */
5161 inline hashval_t
5162 decl_loc_hasher::hash (var_loc_list *x)
5164 return (hashval_t) x->decl_id;
5167 /* Return nonzero if decl_id of var_loc_list X is the same as
5168 UID of decl *Y. */
5170 inline bool
5171 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5173 return (x->decl_id == DECL_UID (y));
5176 /* Return the var_loc list associated with a given declaration. */
5178 static inline var_loc_list *
5179 lookup_decl_loc (const_tree decl)
5181 if (!decl_loc_table)
5182 return NULL;
5183 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5186 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5188 inline hashval_t
5189 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5191 return (hashval_t) x->decl_id;
5194 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5195 UID of decl *Y. */
5197 inline bool
5198 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5200 return (x->decl_id == DECL_UID (y));
5203 /* Equate a DIE to a particular declaration. */
5205 static void
5206 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5208 unsigned int decl_id = DECL_UID (decl);
5210 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5211 decl_die->decl_id = decl_id;
5214 /* Return how many bits covers PIECE EXPR_LIST. */
5216 static HOST_WIDE_INT
5217 decl_piece_bitsize (rtx piece)
5219 int ret = (int) GET_MODE (piece);
5220 if (ret)
5221 return ret;
5222 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5223 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5224 return INTVAL (XEXP (XEXP (piece, 0), 0));
5227 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5229 static rtx *
5230 decl_piece_varloc_ptr (rtx piece)
5232 if ((int) GET_MODE (piece))
5233 return &XEXP (piece, 0);
5234 else
5235 return &XEXP (XEXP (piece, 0), 1);
5238 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5239 Next is the chain of following piece nodes. */
5241 static rtx_expr_list *
5242 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5244 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5245 return alloc_EXPR_LIST (bitsize, loc_note, next);
5246 else
5247 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5248 GEN_INT (bitsize),
5249 loc_note), next);
5252 /* Return rtx that should be stored into loc field for
5253 LOC_NOTE and BITPOS/BITSIZE. */
5255 static rtx
5256 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5257 HOST_WIDE_INT bitsize)
5259 if (bitsize != -1)
5261 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5262 if (bitpos != 0)
5263 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5265 return loc_note;
5268 /* This function either modifies location piece list *DEST in
5269 place (if SRC and INNER is NULL), or copies location piece list
5270 *SRC to *DEST while modifying it. Location BITPOS is modified
5271 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5272 not copied and if needed some padding around it is added.
5273 When modifying in place, DEST should point to EXPR_LIST where
5274 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5275 to the start of the whole list and INNER points to the EXPR_LIST
5276 where earlier pieces cover PIECE_BITPOS bits. */
5278 static void
5279 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5280 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5281 HOST_WIDE_INT bitsize, rtx loc_note)
5283 HOST_WIDE_INT diff;
5284 bool copy = inner != NULL;
5286 if (copy)
5288 /* First copy all nodes preceding the current bitpos. */
5289 while (src != inner)
5291 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5292 decl_piece_bitsize (*src), NULL_RTX);
5293 dest = &XEXP (*dest, 1);
5294 src = &XEXP (*src, 1);
5297 /* Add padding if needed. */
5298 if (bitpos != piece_bitpos)
5300 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5301 copy ? NULL_RTX : *dest);
5302 dest = &XEXP (*dest, 1);
5304 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5306 gcc_assert (!copy);
5307 /* A piece with correct bitpos and bitsize already exist,
5308 just update the location for it and return. */
5309 *decl_piece_varloc_ptr (*dest) = loc_note;
5310 return;
5312 /* Add the piece that changed. */
5313 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5314 dest = &XEXP (*dest, 1);
5315 /* Skip over pieces that overlap it. */
5316 diff = bitpos - piece_bitpos + bitsize;
5317 if (!copy)
5318 src = dest;
5319 while (diff > 0 && *src)
5321 rtx piece = *src;
5322 diff -= decl_piece_bitsize (piece);
5323 if (copy)
5324 src = &XEXP (piece, 1);
5325 else
5327 *src = XEXP (piece, 1);
5328 free_EXPR_LIST_node (piece);
5331 /* Add padding if needed. */
5332 if (diff < 0 && *src)
5334 if (!copy)
5335 dest = src;
5336 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5337 dest = &XEXP (*dest, 1);
5339 if (!copy)
5340 return;
5341 /* Finally copy all nodes following it. */
5342 while (*src)
5344 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5345 decl_piece_bitsize (*src), NULL_RTX);
5346 dest = &XEXP (*dest, 1);
5347 src = &XEXP (*src, 1);
5351 /* Add a variable location node to the linked list for DECL. */
5353 static struct var_loc_node *
5354 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5356 unsigned int decl_id;
5357 var_loc_list *temp;
5358 struct var_loc_node *loc = NULL;
5359 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5361 if (VAR_P (decl) && DECL_HAS_DEBUG_EXPR_P (decl))
5363 tree realdecl = DECL_DEBUG_EXPR (decl);
5364 if (handled_component_p (realdecl)
5365 || (TREE_CODE (realdecl) == MEM_REF
5366 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5368 HOST_WIDE_INT maxsize;
5369 bool reverse;
5370 tree innerdecl
5371 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize,
5372 &reverse);
5373 if (!DECL_P (innerdecl)
5374 || DECL_IGNORED_P (innerdecl)
5375 || TREE_STATIC (innerdecl)
5376 || bitsize <= 0
5377 || bitpos + bitsize > 256
5378 || bitsize != maxsize)
5379 return NULL;
5380 decl = innerdecl;
5384 decl_id = DECL_UID (decl);
5385 var_loc_list **slot
5386 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5387 if (*slot == NULL)
5389 temp = ggc_cleared_alloc<var_loc_list> ();
5390 temp->decl_id = decl_id;
5391 *slot = temp;
5393 else
5394 temp = *slot;
5396 /* For PARM_DECLs try to keep around the original incoming value,
5397 even if that means we'll emit a zero-range .debug_loc entry. */
5398 if (temp->last
5399 && temp->first == temp->last
5400 && TREE_CODE (decl) == PARM_DECL
5401 && NOTE_P (temp->first->loc)
5402 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5403 && DECL_INCOMING_RTL (decl)
5404 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5405 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5406 == GET_CODE (DECL_INCOMING_RTL (decl))
5407 && prev_real_insn (as_a<rtx_insn *> (temp->first->loc)) == NULL_RTX
5408 && (bitsize != -1
5409 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5410 NOTE_VAR_LOCATION_LOC (loc_note))
5411 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5412 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5414 loc = ggc_cleared_alloc<var_loc_node> ();
5415 temp->first->next = loc;
5416 temp->last = loc;
5417 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5419 else if (temp->last)
5421 struct var_loc_node *last = temp->last, *unused = NULL;
5422 rtx *piece_loc = NULL, last_loc_note;
5423 HOST_WIDE_INT piece_bitpos = 0;
5424 if (last->next)
5426 last = last->next;
5427 gcc_assert (last->next == NULL);
5429 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5431 piece_loc = &last->loc;
5434 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5435 if (piece_bitpos + cur_bitsize > bitpos)
5436 break;
5437 piece_bitpos += cur_bitsize;
5438 piece_loc = &XEXP (*piece_loc, 1);
5440 while (*piece_loc);
5442 /* TEMP->LAST here is either pointer to the last but one or
5443 last element in the chained list, LAST is pointer to the
5444 last element. */
5445 if (label && strcmp (last->label, label) == 0)
5447 /* For SRA optimized variables if there weren't any real
5448 insns since last note, just modify the last node. */
5449 if (piece_loc != NULL)
5451 adjust_piece_list (piece_loc, NULL, NULL,
5452 bitpos, piece_bitpos, bitsize, loc_note);
5453 return NULL;
5455 /* If the last note doesn't cover any instructions, remove it. */
5456 if (temp->last != last)
5458 temp->last->next = NULL;
5459 unused = last;
5460 last = temp->last;
5461 gcc_assert (strcmp (last->label, label) != 0);
5463 else
5465 gcc_assert (temp->first == temp->last
5466 || (temp->first->next == temp->last
5467 && TREE_CODE (decl) == PARM_DECL));
5468 memset (temp->last, '\0', sizeof (*temp->last));
5469 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5470 return temp->last;
5473 if (bitsize == -1 && NOTE_P (last->loc))
5474 last_loc_note = last->loc;
5475 else if (piece_loc != NULL
5476 && *piece_loc != NULL_RTX
5477 && piece_bitpos == bitpos
5478 && decl_piece_bitsize (*piece_loc) == bitsize)
5479 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5480 else
5481 last_loc_note = NULL_RTX;
5482 /* If the current location is the same as the end of the list,
5483 and either both or neither of the locations is uninitialized,
5484 we have nothing to do. */
5485 if (last_loc_note == NULL_RTX
5486 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5487 NOTE_VAR_LOCATION_LOC (loc_note)))
5488 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5489 != NOTE_VAR_LOCATION_STATUS (loc_note))
5490 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5491 == VAR_INIT_STATUS_UNINITIALIZED)
5492 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5493 == VAR_INIT_STATUS_UNINITIALIZED))))
5495 /* Add LOC to the end of list and update LAST. If the last
5496 element of the list has been removed above, reuse its
5497 memory for the new node, otherwise allocate a new one. */
5498 if (unused)
5500 loc = unused;
5501 memset (loc, '\0', sizeof (*loc));
5503 else
5504 loc = ggc_cleared_alloc<var_loc_node> ();
5505 if (bitsize == -1 || piece_loc == NULL)
5506 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5507 else
5508 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5509 bitpos, piece_bitpos, bitsize, loc_note);
5510 last->next = loc;
5511 /* Ensure TEMP->LAST will point either to the new last but one
5512 element of the chain, or to the last element in it. */
5513 if (last != temp->last)
5514 temp->last = last;
5516 else if (unused)
5517 ggc_free (unused);
5519 else
5521 loc = ggc_cleared_alloc<var_loc_node> ();
5522 temp->first = loc;
5523 temp->last = loc;
5524 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5526 return loc;
5529 /* Keep track of the number of spaces used to indent the
5530 output of the debugging routines that print the structure of
5531 the DIE internal representation. */
5532 static int print_indent;
5534 /* Indent the line the number of spaces given by print_indent. */
5536 static inline void
5537 print_spaces (FILE *outfile)
5539 fprintf (outfile, "%*s", print_indent, "");
5542 /* Print a type signature in hex. */
5544 static inline void
5545 print_signature (FILE *outfile, char *sig)
5547 int i;
5549 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5550 fprintf (outfile, "%02x", sig[i] & 0xff);
5553 static inline void
5554 print_discr_value (FILE *outfile, dw_discr_value *discr_value)
5556 if (discr_value->pos)
5557 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, discr_value->v.sval);
5558 else
5559 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, discr_value->v.uval);
5562 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5564 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5565 RECURSE, output location descriptor operations. */
5567 static void
5568 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5570 switch (val->val_class)
5572 case dw_val_class_addr:
5573 fprintf (outfile, "address");
5574 break;
5575 case dw_val_class_offset:
5576 fprintf (outfile, "offset");
5577 break;
5578 case dw_val_class_loc:
5579 fprintf (outfile, "location descriptor");
5580 if (val->v.val_loc == NULL)
5581 fprintf (outfile, " -> <null>\n");
5582 else if (recurse)
5584 fprintf (outfile, ":\n");
5585 print_indent += 4;
5586 print_loc_descr (val->v.val_loc, outfile);
5587 print_indent -= 4;
5589 else
5590 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5591 break;
5592 case dw_val_class_loc_list:
5593 fprintf (outfile, "location list -> label:%s",
5594 val->v.val_loc_list->ll_symbol);
5595 break;
5596 case dw_val_class_range_list:
5597 fprintf (outfile, "range list");
5598 break;
5599 case dw_val_class_const:
5600 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5601 break;
5602 case dw_val_class_unsigned_const:
5603 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5604 break;
5605 case dw_val_class_const_double:
5606 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5607 HOST_WIDE_INT_PRINT_UNSIGNED")",
5608 val->v.val_double.high,
5609 val->v.val_double.low);
5610 break;
5611 case dw_val_class_wide_int:
5613 int i = val->v.val_wide->get_len ();
5614 fprintf (outfile, "constant (");
5615 gcc_assert (i > 0);
5616 if (val->v.val_wide->elt (i - 1) == 0)
5617 fprintf (outfile, "0x");
5618 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5619 val->v.val_wide->elt (--i));
5620 while (--i >= 0)
5621 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5622 val->v.val_wide->elt (i));
5623 fprintf (outfile, ")");
5624 break;
5626 case dw_val_class_vec:
5627 fprintf (outfile, "floating-point or vector constant");
5628 break;
5629 case dw_val_class_flag:
5630 fprintf (outfile, "%u", val->v.val_flag);
5631 break;
5632 case dw_val_class_die_ref:
5633 if (val->v.val_die_ref.die != NULL)
5635 dw_die_ref die = val->v.val_die_ref.die;
5637 if (die->comdat_type_p)
5639 fprintf (outfile, "die -> signature: ");
5640 print_signature (outfile,
5641 die->die_id.die_type_node->signature);
5643 else if (die->die_id.die_symbol)
5644 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5645 else
5646 fprintf (outfile, "die -> %ld", die->die_offset);
5647 fprintf (outfile, " (%p)", (void *) die);
5649 else
5650 fprintf (outfile, "die -> <null>");
5651 break;
5652 case dw_val_class_vms_delta:
5653 fprintf (outfile, "delta: @slotcount(%s-%s)",
5654 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5655 break;
5656 case dw_val_class_lbl_id:
5657 case dw_val_class_lineptr:
5658 case dw_val_class_macptr:
5659 case dw_val_class_high_pc:
5660 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5661 break;
5662 case dw_val_class_str:
5663 if (val->v.val_str->str != NULL)
5664 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5665 else
5666 fprintf (outfile, "<null>");
5667 break;
5668 case dw_val_class_file:
5669 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5670 val->v.val_file->emitted_number);
5671 break;
5672 case dw_val_class_data8:
5674 int i;
5676 for (i = 0; i < 8; i++)
5677 fprintf (outfile, "%02x", val->v.val_data8[i]);
5678 break;
5680 case dw_val_class_discr_value:
5681 print_discr_value (outfile, &val->v.val_discr_value);
5682 break;
5683 case dw_val_class_discr_list:
5684 for (dw_discr_list_ref node = val->v.val_discr_list;
5685 node != NULL;
5686 node = node->dw_discr_next)
5688 if (node->dw_discr_range)
5690 fprintf (outfile, " .. ");
5691 print_discr_value (outfile, &node->dw_discr_lower_bound);
5692 print_discr_value (outfile, &node->dw_discr_upper_bound);
5694 else
5695 print_discr_value (outfile, &node->dw_discr_lower_bound);
5697 if (node->dw_discr_next != NULL)
5698 fprintf (outfile, " | ");
5700 default:
5701 break;
5705 /* Likewise, for a DIE attribute. */
5707 static void
5708 print_attribute (dw_attr_node *a, bool recurse, FILE *outfile)
5710 print_dw_val (&a->dw_attr_val, recurse, outfile);
5714 /* Print the list of operands in the LOC location description to OUTFILE. This
5715 routine is a debugging aid only. */
5717 static void
5718 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5720 dw_loc_descr_ref l = loc;
5722 if (loc == NULL)
5724 print_spaces (outfile);
5725 fprintf (outfile, "<null>\n");
5726 return;
5729 for (l = loc; l != NULL; l = l->dw_loc_next)
5731 print_spaces (outfile);
5732 fprintf (outfile, "(%p) %s",
5733 (void *) l,
5734 dwarf_stack_op_name (l->dw_loc_opc));
5735 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5737 fprintf (outfile, " ");
5738 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5740 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5742 fprintf (outfile, ", ");
5743 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5745 fprintf (outfile, "\n");
5749 /* Print the information associated with a given DIE, and its children.
5750 This routine is a debugging aid only. */
5752 static void
5753 print_die (dw_die_ref die, FILE *outfile)
5755 dw_attr_node *a;
5756 dw_die_ref c;
5757 unsigned ix;
5759 print_spaces (outfile);
5760 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5761 die->die_offset, dwarf_tag_name (die->die_tag),
5762 (void*) die);
5763 print_spaces (outfile);
5764 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5765 fprintf (outfile, " offset: %ld", die->die_offset);
5766 fprintf (outfile, " mark: %d\n", die->die_mark);
5768 if (die->comdat_type_p)
5770 print_spaces (outfile);
5771 fprintf (outfile, " signature: ");
5772 print_signature (outfile, die->die_id.die_type_node->signature);
5773 fprintf (outfile, "\n");
5776 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5778 print_spaces (outfile);
5779 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5781 print_attribute (a, true, outfile);
5782 fprintf (outfile, "\n");
5785 if (die->die_child != NULL)
5787 print_indent += 4;
5788 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5789 print_indent -= 4;
5791 if (print_indent == 0)
5792 fprintf (outfile, "\n");
5795 /* Print the list of operations in the LOC location description. */
5797 DEBUG_FUNCTION void
5798 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5800 print_loc_descr (loc, stderr);
5803 /* Print the information collected for a given DIE. */
5805 DEBUG_FUNCTION void
5806 debug_dwarf_die (dw_die_ref die)
5808 print_die (die, stderr);
5811 DEBUG_FUNCTION void
5812 debug (die_struct &ref)
5814 print_die (&ref, stderr);
5817 DEBUG_FUNCTION void
5818 debug (die_struct *ptr)
5820 if (ptr)
5821 debug (*ptr);
5822 else
5823 fprintf (stderr, "<nil>\n");
5827 /* Print all DWARF information collected for the compilation unit.
5828 This routine is a debugging aid only. */
5830 DEBUG_FUNCTION void
5831 debug_dwarf (void)
5833 print_indent = 0;
5834 print_die (comp_unit_die (), stderr);
5837 /* Verify the DIE tree structure. */
5839 DEBUG_FUNCTION void
5840 verify_die (dw_die_ref die)
5842 gcc_assert (!die->die_mark);
5843 if (die->die_parent == NULL
5844 && die->die_sib == NULL)
5845 return;
5846 /* Verify the die_sib list is cyclic. */
5847 dw_die_ref x = die;
5850 x->die_mark = 1;
5851 x = x->die_sib;
5853 while (x && !x->die_mark);
5854 gcc_assert (x == die);
5855 x = die;
5858 /* Verify all dies have the same parent. */
5859 gcc_assert (x->die_parent == die->die_parent);
5860 if (x->die_child)
5862 /* Verify the child has the proper parent and recurse. */
5863 gcc_assert (x->die_child->die_parent == x);
5864 verify_die (x->die_child);
5866 x->die_mark = 0;
5867 x = x->die_sib;
5869 while (x && x->die_mark);
5872 /* Sanity checks on DIEs. */
5874 static void
5875 check_die (dw_die_ref die)
5877 unsigned ix;
5878 dw_attr_node *a;
5879 bool inline_found = false;
5880 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5881 int n_decl_line = 0, n_decl_file = 0;
5882 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5884 switch (a->dw_attr)
5886 case DW_AT_inline:
5887 if (a->dw_attr_val.v.val_unsigned)
5888 inline_found = true;
5889 break;
5890 case DW_AT_location:
5891 ++n_location;
5892 break;
5893 case DW_AT_low_pc:
5894 ++n_low_pc;
5895 break;
5896 case DW_AT_high_pc:
5897 ++n_high_pc;
5898 break;
5899 case DW_AT_artificial:
5900 ++n_artificial;
5901 break;
5902 case DW_AT_decl_line:
5903 ++n_decl_line;
5904 break;
5905 case DW_AT_decl_file:
5906 ++n_decl_file;
5907 break;
5908 default:
5909 break;
5912 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5913 || n_decl_line > 1 || n_decl_file > 1)
5915 fprintf (stderr, "Duplicate attributes in DIE:\n");
5916 debug_dwarf_die (die);
5917 gcc_unreachable ();
5919 if (inline_found)
5921 /* A debugging information entry that is a member of an abstract
5922 instance tree [that has DW_AT_inline] should not contain any
5923 attributes which describe aspects of the subroutine which vary
5924 between distinct inlined expansions or distinct out-of-line
5925 expansions. */
5926 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5927 gcc_assert (a->dw_attr != DW_AT_low_pc
5928 && a->dw_attr != DW_AT_high_pc
5929 && a->dw_attr != DW_AT_location
5930 && a->dw_attr != DW_AT_frame_base
5931 && a->dw_attr != DW_AT_GNU_all_call_sites);
5935 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5936 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5937 DIE that marks the start of the DIEs for this include file. */
5939 static dw_die_ref
5940 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5942 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5943 dw_die_ref new_unit = gen_compile_unit_die (filename);
5945 new_unit->die_sib = old_unit;
5946 return new_unit;
5949 /* Close an include-file CU and reopen the enclosing one. */
5951 static dw_die_ref
5952 pop_compile_unit (dw_die_ref old_unit)
5954 dw_die_ref new_unit = old_unit->die_sib;
5956 old_unit->die_sib = NULL;
5957 return new_unit;
5960 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5961 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5962 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5964 /* Calculate the checksum of a location expression. */
5966 static inline void
5967 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5969 int tem;
5970 inchash::hash hstate;
5971 hashval_t hash;
5973 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5974 CHECKSUM (tem);
5975 hash_loc_operands (loc, hstate);
5976 hash = hstate.end();
5977 CHECKSUM (hash);
5980 /* Calculate the checksum of an attribute. */
5982 static void
5983 attr_checksum (dw_attr_node *at, struct md5_ctx *ctx, int *mark)
5985 dw_loc_descr_ref loc;
5986 rtx r;
5988 CHECKSUM (at->dw_attr);
5990 /* We don't care that this was compiled with a different compiler
5991 snapshot; if the output is the same, that's what matters. */
5992 if (at->dw_attr == DW_AT_producer)
5993 return;
5995 switch (AT_class (at))
5997 case dw_val_class_const:
5998 CHECKSUM (at->dw_attr_val.v.val_int);
5999 break;
6000 case dw_val_class_unsigned_const:
6001 CHECKSUM (at->dw_attr_val.v.val_unsigned);
6002 break;
6003 case dw_val_class_const_double:
6004 CHECKSUM (at->dw_attr_val.v.val_double);
6005 break;
6006 case dw_val_class_wide_int:
6007 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6008 get_full_len (*at->dw_attr_val.v.val_wide)
6009 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6010 break;
6011 case dw_val_class_vec:
6012 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6013 (at->dw_attr_val.v.val_vec.length
6014 * at->dw_attr_val.v.val_vec.elt_size));
6015 break;
6016 case dw_val_class_flag:
6017 CHECKSUM (at->dw_attr_val.v.val_flag);
6018 break;
6019 case dw_val_class_str:
6020 CHECKSUM_STRING (AT_string (at));
6021 break;
6023 case dw_val_class_addr:
6024 r = AT_addr (at);
6025 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6026 CHECKSUM_STRING (XSTR (r, 0));
6027 break;
6029 case dw_val_class_offset:
6030 CHECKSUM (at->dw_attr_val.v.val_offset);
6031 break;
6033 case dw_val_class_loc:
6034 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6035 loc_checksum (loc, ctx);
6036 break;
6038 case dw_val_class_die_ref:
6039 die_checksum (AT_ref (at), ctx, mark);
6040 break;
6042 case dw_val_class_fde_ref:
6043 case dw_val_class_vms_delta:
6044 case dw_val_class_lbl_id:
6045 case dw_val_class_lineptr:
6046 case dw_val_class_macptr:
6047 case dw_val_class_high_pc:
6048 break;
6050 case dw_val_class_file:
6051 CHECKSUM_STRING (AT_file (at)->filename);
6052 break;
6054 case dw_val_class_data8:
6055 CHECKSUM (at->dw_attr_val.v.val_data8);
6056 break;
6058 default:
6059 break;
6063 /* Calculate the checksum of a DIE. */
6065 static void
6066 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6068 dw_die_ref c;
6069 dw_attr_node *a;
6070 unsigned ix;
6072 /* To avoid infinite recursion. */
6073 if (die->die_mark)
6075 CHECKSUM (die->die_mark);
6076 return;
6078 die->die_mark = ++(*mark);
6080 CHECKSUM (die->die_tag);
6082 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6083 attr_checksum (a, ctx, mark);
6085 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6088 #undef CHECKSUM
6089 #undef CHECKSUM_BLOCK
6090 #undef CHECKSUM_STRING
6092 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
6093 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6094 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
6095 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6096 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6097 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6098 #define CHECKSUM_ATTR(FOO) \
6099 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6101 /* Calculate the checksum of a number in signed LEB128 format. */
6103 static void
6104 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6106 unsigned char byte;
6107 bool more;
6109 while (1)
6111 byte = (value & 0x7f);
6112 value >>= 7;
6113 more = !((value == 0 && (byte & 0x40) == 0)
6114 || (value == -1 && (byte & 0x40) != 0));
6115 if (more)
6116 byte |= 0x80;
6117 CHECKSUM (byte);
6118 if (!more)
6119 break;
6123 /* Calculate the checksum of a number in unsigned LEB128 format. */
6125 static void
6126 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6128 while (1)
6130 unsigned char byte = (value & 0x7f);
6131 value >>= 7;
6132 if (value != 0)
6133 /* More bytes to follow. */
6134 byte |= 0x80;
6135 CHECKSUM (byte);
6136 if (value == 0)
6137 break;
6141 /* Checksum the context of the DIE. This adds the names of any
6142 surrounding namespaces or structures to the checksum. */
6144 static void
6145 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6147 const char *name;
6148 dw_die_ref spec;
6149 int tag = die->die_tag;
6151 if (tag != DW_TAG_namespace
6152 && tag != DW_TAG_structure_type
6153 && tag != DW_TAG_class_type)
6154 return;
6156 name = get_AT_string (die, DW_AT_name);
6158 spec = get_AT_ref (die, DW_AT_specification);
6159 if (spec != NULL)
6160 die = spec;
6162 if (die->die_parent != NULL)
6163 checksum_die_context (die->die_parent, ctx);
6165 CHECKSUM_ULEB128 ('C');
6166 CHECKSUM_ULEB128 (tag);
6167 if (name != NULL)
6168 CHECKSUM_STRING (name);
6171 /* Calculate the checksum of a location expression. */
6173 static inline void
6174 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6176 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6177 were emitted as a DW_FORM_sdata instead of a location expression. */
6178 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6180 CHECKSUM_ULEB128 (DW_FORM_sdata);
6181 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6182 return;
6185 /* Otherwise, just checksum the raw location expression. */
6186 while (loc != NULL)
6188 inchash::hash hstate;
6189 hashval_t hash;
6191 CHECKSUM_ULEB128 (loc->dtprel);
6192 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6193 hash_loc_operands (loc, hstate);
6194 hash = hstate.end ();
6195 CHECKSUM (hash);
6196 loc = loc->dw_loc_next;
6200 /* Calculate the checksum of an attribute. */
6202 static void
6203 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_node *at,
6204 struct md5_ctx *ctx, int *mark)
6206 dw_loc_descr_ref loc;
6207 rtx r;
6209 if (AT_class (at) == dw_val_class_die_ref)
6211 dw_die_ref target_die = AT_ref (at);
6213 /* For pointer and reference types, we checksum only the (qualified)
6214 name of the target type (if there is a name). For friend entries,
6215 we checksum only the (qualified) name of the target type or function.
6216 This allows the checksum to remain the same whether the target type
6217 is complete or not. */
6218 if ((at->dw_attr == DW_AT_type
6219 && (tag == DW_TAG_pointer_type
6220 || tag == DW_TAG_reference_type
6221 || tag == DW_TAG_rvalue_reference_type
6222 || tag == DW_TAG_ptr_to_member_type))
6223 || (at->dw_attr == DW_AT_friend
6224 && tag == DW_TAG_friend))
6226 dw_attr_node *name_attr = get_AT (target_die, DW_AT_name);
6228 if (name_attr != NULL)
6230 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6232 if (decl == NULL)
6233 decl = target_die;
6234 CHECKSUM_ULEB128 ('N');
6235 CHECKSUM_ULEB128 (at->dw_attr);
6236 if (decl->die_parent != NULL)
6237 checksum_die_context (decl->die_parent, ctx);
6238 CHECKSUM_ULEB128 ('E');
6239 CHECKSUM_STRING (AT_string (name_attr));
6240 return;
6244 /* For all other references to another DIE, we check to see if the
6245 target DIE has already been visited. If it has, we emit a
6246 backward reference; if not, we descend recursively. */
6247 if (target_die->die_mark > 0)
6249 CHECKSUM_ULEB128 ('R');
6250 CHECKSUM_ULEB128 (at->dw_attr);
6251 CHECKSUM_ULEB128 (target_die->die_mark);
6253 else
6255 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6257 if (decl == NULL)
6258 decl = target_die;
6259 target_die->die_mark = ++(*mark);
6260 CHECKSUM_ULEB128 ('T');
6261 CHECKSUM_ULEB128 (at->dw_attr);
6262 if (decl->die_parent != NULL)
6263 checksum_die_context (decl->die_parent, ctx);
6264 die_checksum_ordered (target_die, ctx, mark);
6266 return;
6269 CHECKSUM_ULEB128 ('A');
6270 CHECKSUM_ULEB128 (at->dw_attr);
6272 switch (AT_class (at))
6274 case dw_val_class_const:
6275 CHECKSUM_ULEB128 (DW_FORM_sdata);
6276 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6277 break;
6279 case dw_val_class_unsigned_const:
6280 CHECKSUM_ULEB128 (DW_FORM_sdata);
6281 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6282 break;
6284 case dw_val_class_const_double:
6285 CHECKSUM_ULEB128 (DW_FORM_block);
6286 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6287 CHECKSUM (at->dw_attr_val.v.val_double);
6288 break;
6290 case dw_val_class_wide_int:
6291 CHECKSUM_ULEB128 (DW_FORM_block);
6292 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6293 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6294 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6295 get_full_len (*at->dw_attr_val.v.val_wide)
6296 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6297 break;
6299 case dw_val_class_vec:
6300 CHECKSUM_ULEB128 (DW_FORM_block);
6301 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6302 * at->dw_attr_val.v.val_vec.elt_size);
6303 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6304 (at->dw_attr_val.v.val_vec.length
6305 * at->dw_attr_val.v.val_vec.elt_size));
6306 break;
6308 case dw_val_class_flag:
6309 CHECKSUM_ULEB128 (DW_FORM_flag);
6310 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6311 break;
6313 case dw_val_class_str:
6314 CHECKSUM_ULEB128 (DW_FORM_string);
6315 CHECKSUM_STRING (AT_string (at));
6316 break;
6318 case dw_val_class_addr:
6319 r = AT_addr (at);
6320 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6321 CHECKSUM_ULEB128 (DW_FORM_string);
6322 CHECKSUM_STRING (XSTR (r, 0));
6323 break;
6325 case dw_val_class_offset:
6326 CHECKSUM_ULEB128 (DW_FORM_sdata);
6327 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6328 break;
6330 case dw_val_class_loc:
6331 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6332 loc_checksum_ordered (loc, ctx);
6333 break;
6335 case dw_val_class_fde_ref:
6336 case dw_val_class_lbl_id:
6337 case dw_val_class_lineptr:
6338 case dw_val_class_macptr:
6339 case dw_val_class_high_pc:
6340 break;
6342 case dw_val_class_file:
6343 CHECKSUM_ULEB128 (DW_FORM_string);
6344 CHECKSUM_STRING (AT_file (at)->filename);
6345 break;
6347 case dw_val_class_data8:
6348 CHECKSUM (at->dw_attr_val.v.val_data8);
6349 break;
6351 default:
6352 break;
6356 struct checksum_attributes
6358 dw_attr_node *at_name;
6359 dw_attr_node *at_type;
6360 dw_attr_node *at_friend;
6361 dw_attr_node *at_accessibility;
6362 dw_attr_node *at_address_class;
6363 dw_attr_node *at_allocated;
6364 dw_attr_node *at_artificial;
6365 dw_attr_node *at_associated;
6366 dw_attr_node *at_binary_scale;
6367 dw_attr_node *at_bit_offset;
6368 dw_attr_node *at_bit_size;
6369 dw_attr_node *at_bit_stride;
6370 dw_attr_node *at_byte_size;
6371 dw_attr_node *at_byte_stride;
6372 dw_attr_node *at_const_value;
6373 dw_attr_node *at_containing_type;
6374 dw_attr_node *at_count;
6375 dw_attr_node *at_data_location;
6376 dw_attr_node *at_data_member_location;
6377 dw_attr_node *at_decimal_scale;
6378 dw_attr_node *at_decimal_sign;
6379 dw_attr_node *at_default_value;
6380 dw_attr_node *at_digit_count;
6381 dw_attr_node *at_discr;
6382 dw_attr_node *at_discr_list;
6383 dw_attr_node *at_discr_value;
6384 dw_attr_node *at_encoding;
6385 dw_attr_node *at_endianity;
6386 dw_attr_node *at_explicit;
6387 dw_attr_node *at_is_optional;
6388 dw_attr_node *at_location;
6389 dw_attr_node *at_lower_bound;
6390 dw_attr_node *at_mutable;
6391 dw_attr_node *at_ordering;
6392 dw_attr_node *at_picture_string;
6393 dw_attr_node *at_prototyped;
6394 dw_attr_node *at_small;
6395 dw_attr_node *at_segment;
6396 dw_attr_node *at_string_length;
6397 dw_attr_node *at_string_length_bit_size;
6398 dw_attr_node *at_string_length_byte_size;
6399 dw_attr_node *at_threads_scaled;
6400 dw_attr_node *at_upper_bound;
6401 dw_attr_node *at_use_location;
6402 dw_attr_node *at_use_UTF8;
6403 dw_attr_node *at_variable_parameter;
6404 dw_attr_node *at_virtuality;
6405 dw_attr_node *at_visibility;
6406 dw_attr_node *at_vtable_elem_location;
6409 /* Collect the attributes that we will want to use for the checksum. */
6411 static void
6412 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6414 dw_attr_node *a;
6415 unsigned ix;
6417 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6419 switch (a->dw_attr)
6421 case DW_AT_name:
6422 attrs->at_name = a;
6423 break;
6424 case DW_AT_type:
6425 attrs->at_type = a;
6426 break;
6427 case DW_AT_friend:
6428 attrs->at_friend = a;
6429 break;
6430 case DW_AT_accessibility:
6431 attrs->at_accessibility = a;
6432 break;
6433 case DW_AT_address_class:
6434 attrs->at_address_class = a;
6435 break;
6436 case DW_AT_allocated:
6437 attrs->at_allocated = a;
6438 break;
6439 case DW_AT_artificial:
6440 attrs->at_artificial = a;
6441 break;
6442 case DW_AT_associated:
6443 attrs->at_associated = a;
6444 break;
6445 case DW_AT_binary_scale:
6446 attrs->at_binary_scale = a;
6447 break;
6448 case DW_AT_bit_offset:
6449 attrs->at_bit_offset = a;
6450 break;
6451 case DW_AT_bit_size:
6452 attrs->at_bit_size = a;
6453 break;
6454 case DW_AT_bit_stride:
6455 attrs->at_bit_stride = a;
6456 break;
6457 case DW_AT_byte_size:
6458 attrs->at_byte_size = a;
6459 break;
6460 case DW_AT_byte_stride:
6461 attrs->at_byte_stride = a;
6462 break;
6463 case DW_AT_const_value:
6464 attrs->at_const_value = a;
6465 break;
6466 case DW_AT_containing_type:
6467 attrs->at_containing_type = a;
6468 break;
6469 case DW_AT_count:
6470 attrs->at_count = a;
6471 break;
6472 case DW_AT_data_location:
6473 attrs->at_data_location = a;
6474 break;
6475 case DW_AT_data_member_location:
6476 attrs->at_data_member_location = a;
6477 break;
6478 case DW_AT_decimal_scale:
6479 attrs->at_decimal_scale = a;
6480 break;
6481 case DW_AT_decimal_sign:
6482 attrs->at_decimal_sign = a;
6483 break;
6484 case DW_AT_default_value:
6485 attrs->at_default_value = a;
6486 break;
6487 case DW_AT_digit_count:
6488 attrs->at_digit_count = a;
6489 break;
6490 case DW_AT_discr:
6491 attrs->at_discr = a;
6492 break;
6493 case DW_AT_discr_list:
6494 attrs->at_discr_list = a;
6495 break;
6496 case DW_AT_discr_value:
6497 attrs->at_discr_value = a;
6498 break;
6499 case DW_AT_encoding:
6500 attrs->at_encoding = a;
6501 break;
6502 case DW_AT_endianity:
6503 attrs->at_endianity = a;
6504 break;
6505 case DW_AT_explicit:
6506 attrs->at_explicit = a;
6507 break;
6508 case DW_AT_is_optional:
6509 attrs->at_is_optional = a;
6510 break;
6511 case DW_AT_location:
6512 attrs->at_location = a;
6513 break;
6514 case DW_AT_lower_bound:
6515 attrs->at_lower_bound = a;
6516 break;
6517 case DW_AT_mutable:
6518 attrs->at_mutable = a;
6519 break;
6520 case DW_AT_ordering:
6521 attrs->at_ordering = a;
6522 break;
6523 case DW_AT_picture_string:
6524 attrs->at_picture_string = a;
6525 break;
6526 case DW_AT_prototyped:
6527 attrs->at_prototyped = a;
6528 break;
6529 case DW_AT_small:
6530 attrs->at_small = a;
6531 break;
6532 case DW_AT_segment:
6533 attrs->at_segment = a;
6534 break;
6535 case DW_AT_string_length:
6536 attrs->at_string_length = a;
6537 break;
6538 case DW_AT_string_length_bit_size:
6539 attrs->at_string_length_bit_size = a;
6540 break;
6541 case DW_AT_string_length_byte_size:
6542 attrs->at_string_length_byte_size = a;
6543 break;
6544 case DW_AT_threads_scaled:
6545 attrs->at_threads_scaled = a;
6546 break;
6547 case DW_AT_upper_bound:
6548 attrs->at_upper_bound = a;
6549 break;
6550 case DW_AT_use_location:
6551 attrs->at_use_location = a;
6552 break;
6553 case DW_AT_use_UTF8:
6554 attrs->at_use_UTF8 = a;
6555 break;
6556 case DW_AT_variable_parameter:
6557 attrs->at_variable_parameter = a;
6558 break;
6559 case DW_AT_virtuality:
6560 attrs->at_virtuality = a;
6561 break;
6562 case DW_AT_visibility:
6563 attrs->at_visibility = a;
6564 break;
6565 case DW_AT_vtable_elem_location:
6566 attrs->at_vtable_elem_location = a;
6567 break;
6568 default:
6569 break;
6574 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6576 static void
6577 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6579 dw_die_ref c;
6580 dw_die_ref decl;
6581 struct checksum_attributes attrs;
6583 CHECKSUM_ULEB128 ('D');
6584 CHECKSUM_ULEB128 (die->die_tag);
6586 memset (&attrs, 0, sizeof (attrs));
6588 decl = get_AT_ref (die, DW_AT_specification);
6589 if (decl != NULL)
6590 collect_checksum_attributes (&attrs, decl);
6591 collect_checksum_attributes (&attrs, die);
6593 CHECKSUM_ATTR (attrs.at_name);
6594 CHECKSUM_ATTR (attrs.at_accessibility);
6595 CHECKSUM_ATTR (attrs.at_address_class);
6596 CHECKSUM_ATTR (attrs.at_allocated);
6597 CHECKSUM_ATTR (attrs.at_artificial);
6598 CHECKSUM_ATTR (attrs.at_associated);
6599 CHECKSUM_ATTR (attrs.at_binary_scale);
6600 CHECKSUM_ATTR (attrs.at_bit_offset);
6601 CHECKSUM_ATTR (attrs.at_bit_size);
6602 CHECKSUM_ATTR (attrs.at_bit_stride);
6603 CHECKSUM_ATTR (attrs.at_byte_size);
6604 CHECKSUM_ATTR (attrs.at_byte_stride);
6605 CHECKSUM_ATTR (attrs.at_const_value);
6606 CHECKSUM_ATTR (attrs.at_containing_type);
6607 CHECKSUM_ATTR (attrs.at_count);
6608 CHECKSUM_ATTR (attrs.at_data_location);
6609 CHECKSUM_ATTR (attrs.at_data_member_location);
6610 CHECKSUM_ATTR (attrs.at_decimal_scale);
6611 CHECKSUM_ATTR (attrs.at_decimal_sign);
6612 CHECKSUM_ATTR (attrs.at_default_value);
6613 CHECKSUM_ATTR (attrs.at_digit_count);
6614 CHECKSUM_ATTR (attrs.at_discr);
6615 CHECKSUM_ATTR (attrs.at_discr_list);
6616 CHECKSUM_ATTR (attrs.at_discr_value);
6617 CHECKSUM_ATTR (attrs.at_encoding);
6618 CHECKSUM_ATTR (attrs.at_endianity);
6619 CHECKSUM_ATTR (attrs.at_explicit);
6620 CHECKSUM_ATTR (attrs.at_is_optional);
6621 CHECKSUM_ATTR (attrs.at_location);
6622 CHECKSUM_ATTR (attrs.at_lower_bound);
6623 CHECKSUM_ATTR (attrs.at_mutable);
6624 CHECKSUM_ATTR (attrs.at_ordering);
6625 CHECKSUM_ATTR (attrs.at_picture_string);
6626 CHECKSUM_ATTR (attrs.at_prototyped);
6627 CHECKSUM_ATTR (attrs.at_small);
6628 CHECKSUM_ATTR (attrs.at_segment);
6629 CHECKSUM_ATTR (attrs.at_string_length);
6630 CHECKSUM_ATTR (attrs.at_string_length_bit_size);
6631 CHECKSUM_ATTR (attrs.at_string_length_byte_size);
6632 CHECKSUM_ATTR (attrs.at_threads_scaled);
6633 CHECKSUM_ATTR (attrs.at_upper_bound);
6634 CHECKSUM_ATTR (attrs.at_use_location);
6635 CHECKSUM_ATTR (attrs.at_use_UTF8);
6636 CHECKSUM_ATTR (attrs.at_variable_parameter);
6637 CHECKSUM_ATTR (attrs.at_virtuality);
6638 CHECKSUM_ATTR (attrs.at_visibility);
6639 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6640 CHECKSUM_ATTR (attrs.at_type);
6641 CHECKSUM_ATTR (attrs.at_friend);
6643 /* Checksum the child DIEs. */
6644 c = die->die_child;
6645 if (c) do {
6646 dw_attr_node *name_attr;
6648 c = c->die_sib;
6649 name_attr = get_AT (c, DW_AT_name);
6650 if (is_template_instantiation (c))
6652 /* Ignore instantiations of member type and function templates. */
6654 else if (name_attr != NULL
6655 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6657 /* Use a shallow checksum for named nested types and member
6658 functions. */
6659 CHECKSUM_ULEB128 ('S');
6660 CHECKSUM_ULEB128 (c->die_tag);
6661 CHECKSUM_STRING (AT_string (name_attr));
6663 else
6665 /* Use a deep checksum for other children. */
6666 /* Mark this DIE so it gets processed when unmarking. */
6667 if (c->die_mark == 0)
6668 c->die_mark = -1;
6669 die_checksum_ordered (c, ctx, mark);
6671 } while (c != die->die_child);
6673 CHECKSUM_ULEB128 (0);
6676 /* Add a type name and tag to a hash. */
6677 static void
6678 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6680 CHECKSUM_ULEB128 (tag);
6681 CHECKSUM_STRING (name);
6684 #undef CHECKSUM
6685 #undef CHECKSUM_STRING
6686 #undef CHECKSUM_ATTR
6687 #undef CHECKSUM_LEB128
6688 #undef CHECKSUM_ULEB128
6690 /* Generate the type signature for DIE. This is computed by generating an
6691 MD5 checksum over the DIE's tag, its relevant attributes, and its
6692 children. Attributes that are references to other DIEs are processed
6693 by recursion, using the MARK field to prevent infinite recursion.
6694 If the DIE is nested inside a namespace or another type, we also
6695 need to include that context in the signature. The lower 64 bits
6696 of the resulting MD5 checksum comprise the signature. */
6698 static void
6699 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6701 int mark;
6702 const char *name;
6703 unsigned char checksum[16];
6704 struct md5_ctx ctx;
6705 dw_die_ref decl;
6706 dw_die_ref parent;
6708 name = get_AT_string (die, DW_AT_name);
6709 decl = get_AT_ref (die, DW_AT_specification);
6710 parent = get_die_parent (die);
6712 /* First, compute a signature for just the type name (and its surrounding
6713 context, if any. This is stored in the type unit DIE for link-time
6714 ODR (one-definition rule) checking. */
6716 if (is_cxx () && name != NULL)
6718 md5_init_ctx (&ctx);
6720 /* Checksum the names of surrounding namespaces and structures. */
6721 if (parent != NULL)
6722 checksum_die_context (parent, &ctx);
6724 /* Checksum the current DIE. */
6725 die_odr_checksum (die->die_tag, name, &ctx);
6726 md5_finish_ctx (&ctx, checksum);
6728 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6731 /* Next, compute the complete type signature. */
6733 md5_init_ctx (&ctx);
6734 mark = 1;
6735 die->die_mark = mark;
6737 /* Checksum the names of surrounding namespaces and structures. */
6738 if (parent != NULL)
6739 checksum_die_context (parent, &ctx);
6741 /* Checksum the DIE and its children. */
6742 die_checksum_ordered (die, &ctx, &mark);
6743 unmark_all_dies (die);
6744 md5_finish_ctx (&ctx, checksum);
6746 /* Store the signature in the type node and link the type DIE and the
6747 type node together. */
6748 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6749 DWARF_TYPE_SIGNATURE_SIZE);
6750 die->comdat_type_p = true;
6751 die->die_id.die_type_node = type_node;
6752 type_node->type_die = die;
6754 /* If the DIE is a specification, link its declaration to the type node
6755 as well. */
6756 if (decl != NULL)
6758 decl->comdat_type_p = true;
6759 decl->die_id.die_type_node = type_node;
6763 /* Do the location expressions look same? */
6764 static inline int
6765 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6767 return loc1->dw_loc_opc == loc2->dw_loc_opc
6768 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6769 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6772 /* Do the values look the same? */
6773 static int
6774 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6776 dw_loc_descr_ref loc1, loc2;
6777 rtx r1, r2;
6779 if (v1->val_class != v2->val_class)
6780 return 0;
6782 switch (v1->val_class)
6784 case dw_val_class_const:
6785 return v1->v.val_int == v2->v.val_int;
6786 case dw_val_class_unsigned_const:
6787 return v1->v.val_unsigned == v2->v.val_unsigned;
6788 case dw_val_class_const_double:
6789 return v1->v.val_double.high == v2->v.val_double.high
6790 && v1->v.val_double.low == v2->v.val_double.low;
6791 case dw_val_class_wide_int:
6792 return *v1->v.val_wide == *v2->v.val_wide;
6793 case dw_val_class_vec:
6794 if (v1->v.val_vec.length != v2->v.val_vec.length
6795 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6796 return 0;
6797 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6798 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6799 return 0;
6800 return 1;
6801 case dw_val_class_flag:
6802 return v1->v.val_flag == v2->v.val_flag;
6803 case dw_val_class_str:
6804 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6806 case dw_val_class_addr:
6807 r1 = v1->v.val_addr;
6808 r2 = v2->v.val_addr;
6809 if (GET_CODE (r1) != GET_CODE (r2))
6810 return 0;
6811 return !rtx_equal_p (r1, r2);
6813 case dw_val_class_offset:
6814 return v1->v.val_offset == v2->v.val_offset;
6816 case dw_val_class_loc:
6817 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6818 loc1 && loc2;
6819 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6820 if (!same_loc_p (loc1, loc2, mark))
6821 return 0;
6822 return !loc1 && !loc2;
6824 case dw_val_class_die_ref:
6825 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6827 case dw_val_class_fde_ref:
6828 case dw_val_class_vms_delta:
6829 case dw_val_class_lbl_id:
6830 case dw_val_class_lineptr:
6831 case dw_val_class_macptr:
6832 case dw_val_class_high_pc:
6833 return 1;
6835 case dw_val_class_file:
6836 return v1->v.val_file == v2->v.val_file;
6838 case dw_val_class_data8:
6839 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6841 default:
6842 return 1;
6846 /* Do the attributes look the same? */
6848 static int
6849 same_attr_p (dw_attr_node *at1, dw_attr_node *at2, int *mark)
6851 if (at1->dw_attr != at2->dw_attr)
6852 return 0;
6854 /* We don't care that this was compiled with a different compiler
6855 snapshot; if the output is the same, that's what matters. */
6856 if (at1->dw_attr == DW_AT_producer)
6857 return 1;
6859 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6862 /* Do the dies look the same? */
6864 static int
6865 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6867 dw_die_ref c1, c2;
6868 dw_attr_node *a1;
6869 unsigned ix;
6871 /* To avoid infinite recursion. */
6872 if (die1->die_mark)
6873 return die1->die_mark == die2->die_mark;
6874 die1->die_mark = die2->die_mark = ++(*mark);
6876 if (die1->die_tag != die2->die_tag)
6877 return 0;
6879 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6880 return 0;
6882 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6883 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6884 return 0;
6886 c1 = die1->die_child;
6887 c2 = die2->die_child;
6888 if (! c1)
6890 if (c2)
6891 return 0;
6893 else
6894 for (;;)
6896 if (!same_die_p (c1, c2, mark))
6897 return 0;
6898 c1 = c1->die_sib;
6899 c2 = c2->die_sib;
6900 if (c1 == die1->die_child)
6902 if (c2 == die2->die_child)
6903 break;
6904 else
6905 return 0;
6909 return 1;
6912 /* Do the dies look the same? Wrapper around same_die_p. */
6914 static int
6915 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6917 int mark = 0;
6918 int ret = same_die_p (die1, die2, &mark);
6920 unmark_all_dies (die1);
6921 unmark_all_dies (die2);
6923 return ret;
6926 /* The prefix to attach to symbols on DIEs in the current comdat debug
6927 info section. */
6928 static const char *comdat_symbol_id;
6930 /* The index of the current symbol within the current comdat CU. */
6931 static unsigned int comdat_symbol_number;
6933 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6934 children, and set comdat_symbol_id accordingly. */
6936 static void
6937 compute_section_prefix (dw_die_ref unit_die)
6939 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6940 const char *base = die_name ? lbasename (die_name) : "anonymous";
6941 char *name = XALLOCAVEC (char, strlen (base) + 64);
6942 char *p;
6943 int i, mark;
6944 unsigned char checksum[16];
6945 struct md5_ctx ctx;
6947 /* Compute the checksum of the DIE, then append part of it as hex digits to
6948 the name filename of the unit. */
6950 md5_init_ctx (&ctx);
6951 mark = 0;
6952 die_checksum (unit_die, &ctx, &mark);
6953 unmark_all_dies (unit_die);
6954 md5_finish_ctx (&ctx, checksum);
6956 sprintf (name, "%s.", base);
6957 clean_symbol_name (name);
6959 p = name + strlen (name);
6960 for (i = 0; i < 4; i++)
6962 sprintf (p, "%.2x", checksum[i]);
6963 p += 2;
6966 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6967 comdat_symbol_number = 0;
6970 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6972 static int
6973 is_type_die (dw_die_ref die)
6975 switch (die->die_tag)
6977 case DW_TAG_array_type:
6978 case DW_TAG_class_type:
6979 case DW_TAG_interface_type:
6980 case DW_TAG_enumeration_type:
6981 case DW_TAG_pointer_type:
6982 case DW_TAG_reference_type:
6983 case DW_TAG_rvalue_reference_type:
6984 case DW_TAG_string_type:
6985 case DW_TAG_structure_type:
6986 case DW_TAG_subroutine_type:
6987 case DW_TAG_union_type:
6988 case DW_TAG_ptr_to_member_type:
6989 case DW_TAG_set_type:
6990 case DW_TAG_subrange_type:
6991 case DW_TAG_base_type:
6992 case DW_TAG_const_type:
6993 case DW_TAG_file_type:
6994 case DW_TAG_packed_type:
6995 case DW_TAG_volatile_type:
6996 case DW_TAG_typedef:
6997 return 1;
6998 default:
6999 return 0;
7003 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7004 Basically, we want to choose the bits that are likely to be shared between
7005 compilations (types) and leave out the bits that are specific to individual
7006 compilations (functions). */
7008 static int
7009 is_comdat_die (dw_die_ref c)
7011 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7012 we do for stabs. The advantage is a greater likelihood of sharing between
7013 objects that don't include headers in the same order (and therefore would
7014 put the base types in a different comdat). jason 8/28/00 */
7016 if (c->die_tag == DW_TAG_base_type)
7017 return 0;
7019 if (c->die_tag == DW_TAG_pointer_type
7020 || c->die_tag == DW_TAG_reference_type
7021 || c->die_tag == DW_TAG_rvalue_reference_type
7022 || c->die_tag == DW_TAG_const_type
7023 || c->die_tag == DW_TAG_volatile_type)
7025 dw_die_ref t = get_AT_ref (c, DW_AT_type);
7027 return t ? is_comdat_die (t) : 0;
7030 return is_type_die (c);
7033 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7034 compilation unit. */
7036 static int
7037 is_symbol_die (dw_die_ref c)
7039 return (is_type_die (c)
7040 || is_declaration_die (c)
7041 || c->die_tag == DW_TAG_namespace
7042 || c->die_tag == DW_TAG_module);
7045 /* Returns true iff C is a compile-unit DIE. */
7047 static inline bool
7048 is_cu_die (dw_die_ref c)
7050 return c && c->die_tag == DW_TAG_compile_unit;
7053 /* Returns true iff C is a unit DIE of some sort. */
7055 static inline bool
7056 is_unit_die (dw_die_ref c)
7058 return c && (c->die_tag == DW_TAG_compile_unit
7059 || c->die_tag == DW_TAG_partial_unit
7060 || c->die_tag == DW_TAG_type_unit);
7063 /* Returns true iff C is a namespace DIE. */
7065 static inline bool
7066 is_namespace_die (dw_die_ref c)
7068 return c && c->die_tag == DW_TAG_namespace;
7071 /* Returns true iff C is a class or structure DIE. */
7073 static inline bool
7074 is_class_die (dw_die_ref c)
7076 return c && (c->die_tag == DW_TAG_class_type
7077 || c->die_tag == DW_TAG_structure_type);
7080 /* Return non-zero if this DIE is a template parameter. */
7082 static inline bool
7083 is_template_parameter (dw_die_ref die)
7085 switch (die->die_tag)
7087 case DW_TAG_template_type_param:
7088 case DW_TAG_template_value_param:
7089 case DW_TAG_GNU_template_template_param:
7090 case DW_TAG_GNU_template_parameter_pack:
7091 return true;
7092 default:
7093 return false;
7097 /* Return non-zero if this DIE represents a template instantiation. */
7099 static inline bool
7100 is_template_instantiation (dw_die_ref die)
7102 dw_die_ref c;
7104 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
7105 return false;
7106 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
7107 return false;
7110 static char *
7111 gen_internal_sym (const char *prefix)
7113 char buf[MAX_ARTIFICIAL_LABEL_BYTES];
7115 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7116 return xstrdup (buf);
7119 /* Assign symbols to all worthy DIEs under DIE. */
7121 static void
7122 assign_symbol_names (dw_die_ref die)
7124 dw_die_ref c;
7126 if (is_symbol_die (die) && !die->comdat_type_p)
7128 if (comdat_symbol_id)
7130 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7132 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7133 comdat_symbol_id, comdat_symbol_number++);
7134 die->die_id.die_symbol = xstrdup (p);
7136 else
7137 die->die_id.die_symbol = gen_internal_sym ("LDIE");
7140 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7143 struct cu_hash_table_entry
7145 dw_die_ref cu;
7146 unsigned min_comdat_num, max_comdat_num;
7147 struct cu_hash_table_entry *next;
7150 /* Helpers to manipulate hash table of CUs. */
7152 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
7154 typedef die_struct *compare_type;
7155 static inline hashval_t hash (const cu_hash_table_entry *);
7156 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
7157 static inline void remove (cu_hash_table_entry *);
7160 inline hashval_t
7161 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
7163 return htab_hash_string (entry->cu->die_id.die_symbol);
7166 inline bool
7167 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
7168 const die_struct *entry2)
7170 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7173 inline void
7174 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
7176 struct cu_hash_table_entry *next;
7178 while (entry)
7180 next = entry->next;
7181 free (entry);
7182 entry = next;
7186 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
7188 /* Check whether we have already seen this CU and set up SYM_NUM
7189 accordingly. */
7190 static int
7191 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7193 struct cu_hash_table_entry dummy;
7194 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7196 dummy.max_comdat_num = 0;
7198 slot = htable->find_slot_with_hash (cu,
7199 htab_hash_string (cu->die_id.die_symbol),
7200 INSERT);
7201 entry = *slot;
7203 for (; entry; last = entry, entry = entry->next)
7205 if (same_die_p_wrap (cu, entry->cu))
7206 break;
7209 if (entry)
7211 *sym_num = entry->min_comdat_num;
7212 return 1;
7215 entry = XCNEW (struct cu_hash_table_entry);
7216 entry->cu = cu;
7217 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7218 entry->next = *slot;
7219 *slot = entry;
7221 return 0;
7224 /* Record SYM_NUM to record of CU in HTABLE. */
7225 static void
7226 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7227 unsigned int sym_num)
7229 struct cu_hash_table_entry **slot, *entry;
7231 slot = htable->find_slot_with_hash (cu,
7232 htab_hash_string (cu->die_id.die_symbol),
7233 NO_INSERT);
7234 entry = *slot;
7236 entry->max_comdat_num = sym_num;
7239 /* Traverse the DIE (which is always comp_unit_die), and set up
7240 additional compilation units for each of the include files we see
7241 bracketed by BINCL/EINCL. */
7243 static void
7244 break_out_includes (dw_die_ref die)
7246 dw_die_ref c;
7247 dw_die_ref unit = NULL;
7248 limbo_die_node *node, **pnode;
7250 c = die->die_child;
7251 if (c) do {
7252 dw_die_ref prev = c;
7253 c = c->die_sib;
7254 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7255 || (unit && is_comdat_die (c)))
7257 dw_die_ref next = c->die_sib;
7259 /* This DIE is for a secondary CU; remove it from the main one. */
7260 remove_child_with_prev (c, prev);
7262 if (c->die_tag == DW_TAG_GNU_BINCL)
7263 unit = push_new_compile_unit (unit, c);
7264 else if (c->die_tag == DW_TAG_GNU_EINCL)
7265 unit = pop_compile_unit (unit);
7266 else
7267 add_child_die (unit, c);
7268 c = next;
7269 if (c == die->die_child)
7270 break;
7272 } while (c != die->die_child);
7274 #if 0
7275 /* We can only use this in debugging, since the frontend doesn't check
7276 to make sure that we leave every include file we enter. */
7277 gcc_assert (!unit);
7278 #endif
7280 assign_symbol_names (die);
7281 cu_hash_type cu_hash_table (10);
7282 for (node = limbo_die_list, pnode = &limbo_die_list;
7283 node;
7284 node = node->next)
7286 int is_dupl;
7288 compute_section_prefix (node->die);
7289 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7290 &comdat_symbol_number);
7291 assign_symbol_names (node->die);
7292 if (is_dupl)
7293 *pnode = node->next;
7294 else
7296 pnode = &node->next;
7297 record_comdat_symbol_number (node->die, &cu_hash_table,
7298 comdat_symbol_number);
7303 /* Return non-zero if this DIE is a declaration. */
7305 static int
7306 is_declaration_die (dw_die_ref die)
7308 dw_attr_node *a;
7309 unsigned ix;
7311 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7312 if (a->dw_attr == DW_AT_declaration)
7313 return 1;
7315 return 0;
7318 /* Return non-zero if this DIE is nested inside a subprogram. */
7320 static int
7321 is_nested_in_subprogram (dw_die_ref die)
7323 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7325 if (decl == NULL)
7326 decl = die;
7327 return local_scope_p (decl);
7330 /* Return non-zero if this DIE contains a defining declaration of a
7331 subprogram. */
7333 static int
7334 contains_subprogram_definition (dw_die_ref die)
7336 dw_die_ref c;
7338 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7339 return 1;
7340 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7341 return 0;
7344 /* Return non-zero if this is a type DIE that should be moved to a
7345 COMDAT .debug_types section. */
7347 static int
7348 should_move_die_to_comdat (dw_die_ref die)
7350 switch (die->die_tag)
7352 case DW_TAG_class_type:
7353 case DW_TAG_structure_type:
7354 case DW_TAG_enumeration_type:
7355 case DW_TAG_union_type:
7356 /* Don't move declarations, inlined instances, types nested in a
7357 subprogram, or types that contain subprogram definitions. */
7358 if (is_declaration_die (die)
7359 || get_AT (die, DW_AT_abstract_origin)
7360 || is_nested_in_subprogram (die)
7361 || contains_subprogram_definition (die))
7362 return 0;
7363 return 1;
7364 case DW_TAG_array_type:
7365 case DW_TAG_interface_type:
7366 case DW_TAG_pointer_type:
7367 case DW_TAG_reference_type:
7368 case DW_TAG_rvalue_reference_type:
7369 case DW_TAG_string_type:
7370 case DW_TAG_subroutine_type:
7371 case DW_TAG_ptr_to_member_type:
7372 case DW_TAG_set_type:
7373 case DW_TAG_subrange_type:
7374 case DW_TAG_base_type:
7375 case DW_TAG_const_type:
7376 case DW_TAG_file_type:
7377 case DW_TAG_packed_type:
7378 case DW_TAG_volatile_type:
7379 case DW_TAG_typedef:
7380 default:
7381 return 0;
7385 /* Make a clone of DIE. */
7387 static dw_die_ref
7388 clone_die (dw_die_ref die)
7390 dw_die_ref clone;
7391 dw_attr_node *a;
7392 unsigned ix;
7394 clone = ggc_cleared_alloc<die_node> ();
7395 clone->die_tag = die->die_tag;
7397 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7398 add_dwarf_attr (clone, a);
7400 return clone;
7403 /* Make a clone of the tree rooted at DIE. */
7405 static dw_die_ref
7406 clone_tree (dw_die_ref die)
7408 dw_die_ref c;
7409 dw_die_ref clone = clone_die (die);
7411 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7413 return clone;
7416 /* Make a clone of DIE as a declaration. */
7418 static dw_die_ref
7419 clone_as_declaration (dw_die_ref die)
7421 dw_die_ref clone;
7422 dw_die_ref decl;
7423 dw_attr_node *a;
7424 unsigned ix;
7426 /* If the DIE is already a declaration, just clone it. */
7427 if (is_declaration_die (die))
7428 return clone_die (die);
7430 /* If the DIE is a specification, just clone its declaration DIE. */
7431 decl = get_AT_ref (die, DW_AT_specification);
7432 if (decl != NULL)
7434 clone = clone_die (decl);
7435 if (die->comdat_type_p)
7436 add_AT_die_ref (clone, DW_AT_signature, die);
7437 return clone;
7440 clone = ggc_cleared_alloc<die_node> ();
7441 clone->die_tag = die->die_tag;
7443 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7445 /* We don't want to copy over all attributes.
7446 For example we don't want DW_AT_byte_size because otherwise we will no
7447 longer have a declaration and GDB will treat it as a definition. */
7449 switch (a->dw_attr)
7451 case DW_AT_abstract_origin:
7452 case DW_AT_artificial:
7453 case DW_AT_containing_type:
7454 case DW_AT_external:
7455 case DW_AT_name:
7456 case DW_AT_type:
7457 case DW_AT_virtuality:
7458 case DW_AT_linkage_name:
7459 case DW_AT_MIPS_linkage_name:
7460 add_dwarf_attr (clone, a);
7461 break;
7462 case DW_AT_byte_size:
7463 default:
7464 break;
7468 if (die->comdat_type_p)
7469 add_AT_die_ref (clone, DW_AT_signature, die);
7471 add_AT_flag (clone, DW_AT_declaration, 1);
7472 return clone;
7476 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7478 struct decl_table_entry
7480 dw_die_ref orig;
7481 dw_die_ref copy;
7484 /* Helpers to manipulate hash table of copied declarations. */
7486 /* Hashtable helpers. */
7488 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7490 typedef die_struct *compare_type;
7491 static inline hashval_t hash (const decl_table_entry *);
7492 static inline bool equal (const decl_table_entry *, const die_struct *);
7495 inline hashval_t
7496 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7498 return htab_hash_pointer (entry->orig);
7501 inline bool
7502 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7503 const die_struct *entry2)
7505 return entry1->orig == entry2;
7508 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7510 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7511 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7512 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7513 to check if the ancestor has already been copied into UNIT. */
7515 static dw_die_ref
7516 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7517 decl_hash_type *decl_table)
7519 dw_die_ref parent = die->die_parent;
7520 dw_die_ref new_parent = unit;
7521 dw_die_ref copy;
7522 decl_table_entry **slot = NULL;
7523 struct decl_table_entry *entry = NULL;
7525 if (decl_table)
7527 /* Check if the entry has already been copied to UNIT. */
7528 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7529 INSERT);
7530 if (*slot != HTAB_EMPTY_ENTRY)
7532 entry = *slot;
7533 return entry->copy;
7536 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7537 entry = XCNEW (struct decl_table_entry);
7538 entry->orig = die;
7539 entry->copy = NULL;
7540 *slot = entry;
7543 if (parent != NULL)
7545 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7546 if (spec != NULL)
7547 parent = spec;
7548 if (!is_unit_die (parent))
7549 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7552 copy = clone_as_declaration (die);
7553 add_child_die (new_parent, copy);
7555 if (decl_table)
7557 /* Record the pointer to the copy. */
7558 entry->copy = copy;
7561 return copy;
7563 /* Copy the declaration context to the new type unit DIE. This includes
7564 any surrounding namespace or type declarations. If the DIE has an
7565 AT_specification attribute, it also includes attributes and children
7566 attached to the specification, and returns a pointer to the original
7567 parent of the declaration DIE. Returns NULL otherwise. */
7569 static dw_die_ref
7570 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7572 dw_die_ref decl;
7573 dw_die_ref new_decl;
7574 dw_die_ref orig_parent = NULL;
7576 decl = get_AT_ref (die, DW_AT_specification);
7577 if (decl == NULL)
7578 decl = die;
7579 else
7581 unsigned ix;
7582 dw_die_ref c;
7583 dw_attr_node *a;
7585 /* The original DIE will be changed to a declaration, and must
7586 be moved to be a child of the original declaration DIE. */
7587 orig_parent = decl->die_parent;
7589 /* Copy the type node pointer from the new DIE to the original
7590 declaration DIE so we can forward references later. */
7591 decl->comdat_type_p = true;
7592 decl->die_id.die_type_node = die->die_id.die_type_node;
7594 remove_AT (die, DW_AT_specification);
7596 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7598 if (a->dw_attr != DW_AT_name
7599 && a->dw_attr != DW_AT_declaration
7600 && a->dw_attr != DW_AT_external)
7601 add_dwarf_attr (die, a);
7604 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7607 if (decl->die_parent != NULL
7608 && !is_unit_die (decl->die_parent))
7610 new_decl = copy_ancestor_tree (unit, decl, NULL);
7611 if (new_decl != NULL)
7613 remove_AT (new_decl, DW_AT_signature);
7614 add_AT_specification (die, new_decl);
7618 return orig_parent;
7621 /* Generate the skeleton ancestor tree for the given NODE, then clone
7622 the DIE and add the clone into the tree. */
7624 static void
7625 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7627 if (node->new_die != NULL)
7628 return;
7630 node->new_die = clone_as_declaration (node->old_die);
7632 if (node->parent != NULL)
7634 generate_skeleton_ancestor_tree (node->parent);
7635 add_child_die (node->parent->new_die, node->new_die);
7639 /* Generate a skeleton tree of DIEs containing any declarations that are
7640 found in the original tree. We traverse the tree looking for declaration
7641 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7643 static void
7644 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7646 skeleton_chain_node node;
7647 dw_die_ref c;
7648 dw_die_ref first;
7649 dw_die_ref prev = NULL;
7650 dw_die_ref next = NULL;
7652 node.parent = parent;
7654 first = c = parent->old_die->die_child;
7655 if (c)
7656 next = c->die_sib;
7657 if (c) do {
7658 if (prev == NULL || prev->die_sib == c)
7659 prev = c;
7660 c = next;
7661 next = (c == first ? NULL : c->die_sib);
7662 node.old_die = c;
7663 node.new_die = NULL;
7664 if (is_declaration_die (c))
7666 if (is_template_instantiation (c))
7668 /* Instantiated templates do not need to be cloned into the
7669 type unit. Just move the DIE and its children back to
7670 the skeleton tree (in the main CU). */
7671 remove_child_with_prev (c, prev);
7672 add_child_die (parent->new_die, c);
7673 c = prev;
7675 else
7677 /* Clone the existing DIE, move the original to the skeleton
7678 tree (which is in the main CU), and put the clone, with
7679 all the original's children, where the original came from
7680 (which is about to be moved to the type unit). */
7681 dw_die_ref clone = clone_die (c);
7682 move_all_children (c, clone);
7684 /* If the original has a DW_AT_object_pointer attribute,
7685 it would now point to a child DIE just moved to the
7686 cloned tree, so we need to remove that attribute from
7687 the original. */
7688 remove_AT (c, DW_AT_object_pointer);
7690 replace_child (c, clone, prev);
7691 generate_skeleton_ancestor_tree (parent);
7692 add_child_die (parent->new_die, c);
7693 node.new_die = c;
7694 c = clone;
7697 generate_skeleton_bottom_up (&node);
7698 } while (next != NULL);
7701 /* Wrapper function for generate_skeleton_bottom_up. */
7703 static dw_die_ref
7704 generate_skeleton (dw_die_ref die)
7706 skeleton_chain_node node;
7708 node.old_die = die;
7709 node.new_die = NULL;
7710 node.parent = NULL;
7712 /* If this type definition is nested inside another type,
7713 and is not an instantiation of a template, always leave
7714 at least a declaration in its place. */
7715 if (die->die_parent != NULL
7716 && is_type_die (die->die_parent)
7717 && !is_template_instantiation (die))
7718 node.new_die = clone_as_declaration (die);
7720 generate_skeleton_bottom_up (&node);
7721 return node.new_die;
7724 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7725 declaration. The original DIE is moved to a new compile unit so that
7726 existing references to it follow it to the new location. If any of the
7727 original DIE's descendants is a declaration, we need to replace the
7728 original DIE with a skeleton tree and move the declarations back into the
7729 skeleton tree. */
7731 static dw_die_ref
7732 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7733 dw_die_ref prev)
7735 dw_die_ref skeleton, orig_parent;
7737 /* Copy the declaration context to the type unit DIE. If the returned
7738 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7739 that DIE. */
7740 orig_parent = copy_declaration_context (unit, child);
7742 skeleton = generate_skeleton (child);
7743 if (skeleton == NULL)
7744 remove_child_with_prev (child, prev);
7745 else
7747 skeleton->comdat_type_p = true;
7748 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7750 /* If the original DIE was a specification, we need to put
7751 the skeleton under the parent DIE of the declaration.
7752 This leaves the original declaration in the tree, but
7753 it will be pruned later since there are no longer any
7754 references to it. */
7755 if (orig_parent != NULL)
7757 remove_child_with_prev (child, prev);
7758 add_child_die (orig_parent, skeleton);
7760 else
7761 replace_child (child, skeleton, prev);
7764 return skeleton;
7767 static void
7768 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7769 comdat_type_node *type_node,
7770 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs);
7772 /* Helper for copy_dwarf_procs_ref_in_dies. Make a copy of the DIE DWARF
7773 procedure, put it under TYPE_NODE and return the copy. Continue looking for
7774 DWARF procedure references in the DW_AT_location attribute. */
7776 static dw_die_ref
7777 copy_dwarf_procedure (dw_die_ref die,
7778 comdat_type_node *type_node,
7779 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7781 gcc_assert (die->die_tag == DW_TAG_dwarf_procedure);
7783 /* DWARF procedures are not supposed to have children... */
7784 gcc_assert (die->die_child == NULL);
7786 /* ... and they are supposed to have only one attribute: DW_AT_location. */
7787 gcc_assert (vec_safe_length (die->die_attr) == 1
7788 && ((*die->die_attr)[0].dw_attr == DW_AT_location));
7790 /* Do not copy more than once DWARF procedures. */
7791 bool existed;
7792 dw_die_ref &die_copy = copied_dwarf_procs.get_or_insert (die, &existed);
7793 if (existed)
7794 return die_copy;
7796 die_copy = clone_die (die);
7797 add_child_die (type_node->root_die, die_copy);
7798 copy_dwarf_procs_ref_in_attrs (die_copy, type_node, copied_dwarf_procs);
7799 return die_copy;
7802 /* Helper for copy_dwarf_procs_ref_in_dies. Look for references to DWARF
7803 procedures in DIE's attributes. */
7805 static void
7806 copy_dwarf_procs_ref_in_attrs (dw_die_ref die,
7807 comdat_type_node *type_node,
7808 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7810 dw_attr_node *a;
7811 unsigned i;
7813 FOR_EACH_VEC_SAFE_ELT (die->die_attr, i, a)
7815 dw_loc_descr_ref loc;
7817 if (a->dw_attr_val.val_class != dw_val_class_loc)
7818 continue;
7820 for (loc = a->dw_attr_val.v.val_loc; loc != NULL; loc = loc->dw_loc_next)
7822 switch (loc->dw_loc_opc)
7824 case DW_OP_call2:
7825 case DW_OP_call4:
7826 case DW_OP_call_ref:
7827 gcc_assert (loc->dw_loc_oprnd1.val_class
7828 == dw_val_class_die_ref);
7829 loc->dw_loc_oprnd1.v.val_die_ref.die
7830 = copy_dwarf_procedure (loc->dw_loc_oprnd1.v.val_die_ref.die,
7831 type_node,
7832 copied_dwarf_procs);
7834 default:
7835 break;
7841 /* Copy DWARF procedures that are referenced by the DIE tree to TREE_NODE and
7842 rewrite references to point to the copies.
7844 References are looked for in DIE's attributes and recursively in all its
7845 children attributes that are location descriptions. COPIED_DWARF_PROCS is a
7846 mapping from old DWARF procedures to their copy. It is used not to copy
7847 twice the same DWARF procedure under TYPE_NODE. */
7849 static void
7850 copy_dwarf_procs_ref_in_dies (dw_die_ref die,
7851 comdat_type_node *type_node,
7852 hash_map<dw_die_ref, dw_die_ref> &copied_dwarf_procs)
7854 dw_die_ref c;
7856 copy_dwarf_procs_ref_in_attrs (die, type_node, copied_dwarf_procs);
7857 FOR_EACH_CHILD (die, c, copy_dwarf_procs_ref_in_dies (c,
7858 type_node,
7859 copied_dwarf_procs));
7862 /* Traverse the DIE and set up additional .debug_types sections for each
7863 type worthy of being placed in a COMDAT section. */
7865 static void
7866 break_out_comdat_types (dw_die_ref die)
7868 dw_die_ref c;
7869 dw_die_ref first;
7870 dw_die_ref prev = NULL;
7871 dw_die_ref next = NULL;
7872 dw_die_ref unit = NULL;
7874 first = c = die->die_child;
7875 if (c)
7876 next = c->die_sib;
7877 if (c) do {
7878 if (prev == NULL || prev->die_sib == c)
7879 prev = c;
7880 c = next;
7881 next = (c == first ? NULL : c->die_sib);
7882 if (should_move_die_to_comdat (c))
7884 dw_die_ref replacement;
7885 comdat_type_node *type_node;
7887 /* Break out nested types into their own type units. */
7888 break_out_comdat_types (c);
7890 /* Create a new type unit DIE as the root for the new tree, and
7891 add it to the list of comdat types. */
7892 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7893 add_AT_unsigned (unit, DW_AT_language,
7894 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7895 type_node = ggc_cleared_alloc<comdat_type_node> ();
7896 type_node->root_die = unit;
7897 type_node->next = comdat_type_list;
7898 comdat_type_list = type_node;
7900 /* Generate the type signature. */
7901 generate_type_signature (c, type_node);
7903 /* Copy the declaration context, attributes, and children of the
7904 declaration into the new type unit DIE, then remove this DIE
7905 from the main CU (or replace it with a skeleton if necessary). */
7906 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7907 type_node->skeleton_die = replacement;
7909 /* Add the DIE to the new compunit. */
7910 add_child_die (unit, c);
7912 /* Types can reference DWARF procedures for type size or data location
7913 expressions. Calls in DWARF expressions cannot target procedures
7914 that are not in the same section. So we must copy DWARF procedures
7915 along with this type and then rewrite references to them. */
7916 hash_map<dw_die_ref, dw_die_ref> copied_dwarf_procs;
7917 copy_dwarf_procs_ref_in_dies (c, type_node, copied_dwarf_procs);
7919 if (replacement != NULL)
7920 c = replacement;
7922 else if (c->die_tag == DW_TAG_namespace
7923 || c->die_tag == DW_TAG_class_type
7924 || c->die_tag == DW_TAG_structure_type
7925 || c->die_tag == DW_TAG_union_type)
7927 /* Look for nested types that can be broken out. */
7928 break_out_comdat_types (c);
7930 } while (next != NULL);
7933 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7934 Enter all the cloned children into the hash table decl_table. */
7936 static dw_die_ref
7937 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7939 dw_die_ref c;
7940 dw_die_ref clone;
7941 struct decl_table_entry *entry;
7942 decl_table_entry **slot;
7944 if (die->die_tag == DW_TAG_subprogram)
7945 clone = clone_as_declaration (die);
7946 else
7947 clone = clone_die (die);
7949 slot = decl_table->find_slot_with_hash (die,
7950 htab_hash_pointer (die), INSERT);
7952 /* Assert that DIE isn't in the hash table yet. If it would be there
7953 before, the ancestors would be necessarily there as well, therefore
7954 clone_tree_partial wouldn't be called. */
7955 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7957 entry = XCNEW (struct decl_table_entry);
7958 entry->orig = die;
7959 entry->copy = clone;
7960 *slot = entry;
7962 if (die->die_tag != DW_TAG_subprogram)
7963 FOR_EACH_CHILD (die, c,
7964 add_child_die (clone, clone_tree_partial (c, decl_table)));
7966 return clone;
7969 /* Walk the DIE and its children, looking for references to incomplete
7970 or trivial types that are unmarked (i.e., that are not in the current
7971 type_unit). */
7973 static void
7974 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7976 dw_die_ref c;
7977 dw_attr_node *a;
7978 unsigned ix;
7980 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7982 if (AT_class (a) == dw_val_class_die_ref)
7984 dw_die_ref targ = AT_ref (a);
7985 decl_table_entry **slot;
7986 struct decl_table_entry *entry;
7988 if (targ->die_mark != 0 || targ->comdat_type_p)
7989 continue;
7991 slot = decl_table->find_slot_with_hash (targ,
7992 htab_hash_pointer (targ),
7993 INSERT);
7995 if (*slot != HTAB_EMPTY_ENTRY)
7997 /* TARG has already been copied, so we just need to
7998 modify the reference to point to the copy. */
7999 entry = *slot;
8000 a->dw_attr_val.v.val_die_ref.die = entry->copy;
8002 else
8004 dw_die_ref parent = unit;
8005 dw_die_ref copy = clone_die (targ);
8007 /* Record in DECL_TABLE that TARG has been copied.
8008 Need to do this now, before the recursive call,
8009 because DECL_TABLE may be expanded and SLOT
8010 would no longer be a valid pointer. */
8011 entry = XCNEW (struct decl_table_entry);
8012 entry->orig = targ;
8013 entry->copy = copy;
8014 *slot = entry;
8016 /* If TARG is not a declaration DIE, we need to copy its
8017 children. */
8018 if (!is_declaration_die (targ))
8020 FOR_EACH_CHILD (
8021 targ, c,
8022 add_child_die (copy,
8023 clone_tree_partial (c, decl_table)));
8026 /* Make sure the cloned tree is marked as part of the
8027 type unit. */
8028 mark_dies (copy);
8030 /* If TARG has surrounding context, copy its ancestor tree
8031 into the new type unit. */
8032 if (targ->die_parent != NULL
8033 && !is_unit_die (targ->die_parent))
8034 parent = copy_ancestor_tree (unit, targ->die_parent,
8035 decl_table);
8037 add_child_die (parent, copy);
8038 a->dw_attr_val.v.val_die_ref.die = copy;
8040 /* Make sure the newly-copied DIE is walked. If it was
8041 installed in a previously-added context, it won't
8042 get visited otherwise. */
8043 if (parent != unit)
8045 /* Find the highest point of the newly-added tree,
8046 mark each node along the way, and walk from there. */
8047 parent->die_mark = 1;
8048 while (parent->die_parent
8049 && parent->die_parent->die_mark == 0)
8051 parent = parent->die_parent;
8052 parent->die_mark = 1;
8054 copy_decls_walk (unit, parent, decl_table);
8060 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8063 /* Copy declarations for "unworthy" types into the new comdat section.
8064 Incomplete types, modified types, and certain other types aren't broken
8065 out into comdat sections of their own, so they don't have a signature,
8066 and we need to copy the declaration into the same section so that we
8067 don't have an external reference. */
8069 static void
8070 copy_decls_for_unworthy_types (dw_die_ref unit)
8072 mark_dies (unit);
8073 decl_hash_type decl_table (10);
8074 copy_decls_walk (unit, unit, &decl_table);
8075 unmark_dies (unit);
8078 /* Traverse the DIE and add a sibling attribute if it may have the
8079 effect of speeding up access to siblings. To save some space,
8080 avoid generating sibling attributes for DIE's without children. */
8082 static void
8083 add_sibling_attributes (dw_die_ref die)
8085 dw_die_ref c;
8087 if (! die->die_child)
8088 return;
8090 if (die->die_parent && die != die->die_parent->die_child)
8091 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8093 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8096 /* Output all location lists for the DIE and its children. */
8098 static void
8099 output_location_lists (dw_die_ref die)
8101 dw_die_ref c;
8102 dw_attr_node *a;
8103 unsigned ix;
8105 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8106 if (AT_class (a) == dw_val_class_loc_list)
8107 output_loc_list (AT_loc_list (a));
8109 FOR_EACH_CHILD (die, c, output_location_lists (c));
8112 /* We want to limit the number of external references, because they are
8113 larger than local references: a relocation takes multiple words, and
8114 even a sig8 reference is always eight bytes, whereas a local reference
8115 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
8116 So if we encounter multiple external references to the same type DIE, we
8117 make a local typedef stub for it and redirect all references there.
8119 This is the element of the hash table for keeping track of these
8120 references. */
8122 struct external_ref
8124 dw_die_ref type;
8125 dw_die_ref stub;
8126 unsigned n_refs;
8129 /* Hashtable helpers. */
8131 struct external_ref_hasher : free_ptr_hash <external_ref>
8133 static inline hashval_t hash (const external_ref *);
8134 static inline bool equal (const external_ref *, const external_ref *);
8137 inline hashval_t
8138 external_ref_hasher::hash (const external_ref *r)
8140 dw_die_ref die = r->type;
8141 hashval_t h = 0;
8143 /* We can't use the address of the DIE for hashing, because
8144 that will make the order of the stub DIEs non-deterministic. */
8145 if (! die->comdat_type_p)
8146 /* We have a symbol; use it to compute a hash. */
8147 h = htab_hash_string (die->die_id.die_symbol);
8148 else
8150 /* We have a type signature; use a subset of the bits as the hash.
8151 The 8-byte signature is at least as large as hashval_t. */
8152 comdat_type_node *type_node = die->die_id.die_type_node;
8153 memcpy (&h, type_node->signature, sizeof (h));
8155 return h;
8158 inline bool
8159 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
8161 return r1->type == r2->type;
8164 typedef hash_table<external_ref_hasher> external_ref_hash_type;
8166 /* Return a pointer to the external_ref for references to DIE. */
8168 static struct external_ref *
8169 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
8171 struct external_ref ref, *ref_p;
8172 external_ref **slot;
8174 ref.type = die;
8175 slot = map->find_slot (&ref, INSERT);
8176 if (*slot != HTAB_EMPTY_ENTRY)
8177 return *slot;
8179 ref_p = XCNEW (struct external_ref);
8180 ref_p->type = die;
8181 *slot = ref_p;
8182 return ref_p;
8185 /* Subroutine of optimize_external_refs, below.
8187 If we see a type skeleton, record it as our stub. If we see external
8188 references, remember how many we've seen. */
8190 static void
8191 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
8193 dw_die_ref c;
8194 dw_attr_node *a;
8195 unsigned ix;
8196 struct external_ref *ref_p;
8198 if (is_type_die (die)
8199 && (c = get_AT_ref (die, DW_AT_signature)))
8201 /* This is a local skeleton; use it for local references. */
8202 ref_p = lookup_external_ref (map, c);
8203 ref_p->stub = die;
8206 /* Scan the DIE references, and remember any that refer to DIEs from
8207 other CUs (i.e. those which are not marked). */
8208 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8209 if (AT_class (a) == dw_val_class_die_ref
8210 && (c = AT_ref (a))->die_mark == 0
8211 && is_type_die (c))
8213 ref_p = lookup_external_ref (map, c);
8214 ref_p->n_refs++;
8217 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
8220 /* htab_traverse callback function for optimize_external_refs, below. SLOT
8221 points to an external_ref, DATA is the CU we're processing. If we don't
8222 already have a local stub, and we have multiple refs, build a stub. */
8225 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
8227 struct external_ref *ref_p = *slot;
8229 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
8231 /* We have multiple references to this type, so build a small stub.
8232 Both of these forms are a bit dodgy from the perspective of the
8233 DWARF standard, since technically they should have names. */
8234 dw_die_ref cu = data;
8235 dw_die_ref type = ref_p->type;
8236 dw_die_ref stub = NULL;
8238 if (type->comdat_type_p)
8240 /* If we refer to this type via sig8, use AT_signature. */
8241 stub = new_die (type->die_tag, cu, NULL_TREE);
8242 add_AT_die_ref (stub, DW_AT_signature, type);
8244 else
8246 /* Otherwise, use a typedef with no name. */
8247 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
8248 add_AT_die_ref (stub, DW_AT_type, type);
8251 stub->die_mark++;
8252 ref_p->stub = stub;
8254 return 1;
8257 /* DIE is a unit; look through all the DIE references to see if there are
8258 any external references to types, and if so, create local stubs for
8259 them which will be applied in build_abbrev_table. This is useful because
8260 references to local DIEs are smaller. */
8262 static external_ref_hash_type *
8263 optimize_external_refs (dw_die_ref die)
8265 external_ref_hash_type *map = new external_ref_hash_type (10);
8266 optimize_external_refs_1 (die, map);
8267 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
8268 return map;
8271 /* The format of each DIE (and its attribute value pairs) is encoded in an
8272 abbreviation table. This routine builds the abbreviation table and assigns
8273 a unique abbreviation id for each abbreviation entry. The children of each
8274 die are visited recursively. */
8276 static void
8277 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
8279 unsigned long abbrev_id;
8280 unsigned int n_alloc;
8281 dw_die_ref c;
8282 dw_attr_node *a;
8283 unsigned ix;
8285 /* Scan the DIE references, and replace any that refer to
8286 DIEs from other CUs (i.e. those which are not marked) with
8287 the local stubs we built in optimize_external_refs. */
8288 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8289 if (AT_class (a) == dw_val_class_die_ref
8290 && (c = AT_ref (a))->die_mark == 0)
8292 struct external_ref *ref_p;
8293 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8295 ref_p = lookup_external_ref (extern_map, c);
8296 if (ref_p->stub && ref_p->stub != die)
8297 change_AT_die_ref (a, ref_p->stub);
8298 else
8299 /* We aren't changing this reference, so mark it external. */
8300 set_AT_ref_external (a, 1);
8303 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8305 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8306 dw_attr_node *die_a, *abbrev_a;
8307 unsigned ix;
8308 bool ok = true;
8310 if (abbrev->die_tag != die->die_tag)
8311 continue;
8312 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8313 continue;
8315 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8316 continue;
8318 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8320 abbrev_a = &(*abbrev->die_attr)[ix];
8321 if ((abbrev_a->dw_attr != die_a->dw_attr)
8322 || (value_format (abbrev_a) != value_format (die_a)))
8324 ok = false;
8325 break;
8328 if (ok)
8329 break;
8332 if (abbrev_id >= abbrev_die_table_in_use)
8334 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8336 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8337 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8338 n_alloc);
8340 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8341 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8342 abbrev_die_table_allocated = n_alloc;
8345 ++abbrev_die_table_in_use;
8346 abbrev_die_table[abbrev_id] = die;
8349 die->die_abbrev = abbrev_id;
8350 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8353 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8355 static int
8356 constant_size (unsigned HOST_WIDE_INT value)
8358 int log;
8360 if (value == 0)
8361 log = 0;
8362 else
8363 log = floor_log2 (value);
8365 log = log / 8;
8366 log = 1 << (floor_log2 (log) + 1);
8368 return log;
8371 /* Return the size of a DIE as it is represented in the
8372 .debug_info section. */
8374 static unsigned long
8375 size_of_die (dw_die_ref die)
8377 unsigned long size = 0;
8378 dw_attr_node *a;
8379 unsigned ix;
8380 enum dwarf_form form;
8382 size += size_of_uleb128 (die->die_abbrev);
8383 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8385 switch (AT_class (a))
8387 case dw_val_class_addr:
8388 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8390 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8391 size += size_of_uleb128 (AT_index (a));
8393 else
8394 size += DWARF2_ADDR_SIZE;
8395 break;
8396 case dw_val_class_offset:
8397 size += DWARF_OFFSET_SIZE;
8398 break;
8399 case dw_val_class_loc:
8401 unsigned long lsize = size_of_locs (AT_loc (a));
8403 /* Block length. */
8404 if (dwarf_version >= 4)
8405 size += size_of_uleb128 (lsize);
8406 else
8407 size += constant_size (lsize);
8408 size += lsize;
8410 break;
8411 case dw_val_class_loc_list:
8412 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8414 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8415 size += size_of_uleb128 (AT_index (a));
8417 else
8418 size += DWARF_OFFSET_SIZE;
8419 break;
8420 case dw_val_class_range_list:
8421 size += DWARF_OFFSET_SIZE;
8422 break;
8423 case dw_val_class_const:
8424 size += size_of_sleb128 (AT_int (a));
8425 break;
8426 case dw_val_class_unsigned_const:
8428 int csize = constant_size (AT_unsigned (a));
8429 if (dwarf_version == 3
8430 && a->dw_attr == DW_AT_data_member_location
8431 && csize >= 4)
8432 size += size_of_uleb128 (AT_unsigned (a));
8433 else
8434 size += csize;
8436 break;
8437 case dw_val_class_const_double:
8438 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8439 if (HOST_BITS_PER_WIDE_INT >= 64)
8440 size++; /* block */
8441 break;
8442 case dw_val_class_wide_int:
8443 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8444 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8445 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8446 > 64)
8447 size++; /* block */
8448 break;
8449 case dw_val_class_vec:
8450 size += constant_size (a->dw_attr_val.v.val_vec.length
8451 * a->dw_attr_val.v.val_vec.elt_size)
8452 + a->dw_attr_val.v.val_vec.length
8453 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8454 break;
8455 case dw_val_class_flag:
8456 if (dwarf_version >= 4)
8457 /* Currently all add_AT_flag calls pass in 1 as last argument,
8458 so DW_FORM_flag_present can be used. If that ever changes,
8459 we'll need to use DW_FORM_flag and have some optimization
8460 in build_abbrev_table that will change those to
8461 DW_FORM_flag_present if it is set to 1 in all DIEs using
8462 the same abbrev entry. */
8463 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8464 else
8465 size += 1;
8466 break;
8467 case dw_val_class_die_ref:
8468 if (AT_ref_external (a))
8470 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8471 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8472 is sized by target address length, whereas in DWARF3
8473 it's always sized as an offset. */
8474 if (use_debug_types)
8475 size += DWARF_TYPE_SIGNATURE_SIZE;
8476 else if (dwarf_version == 2)
8477 size += DWARF2_ADDR_SIZE;
8478 else
8479 size += DWARF_OFFSET_SIZE;
8481 else
8482 size += DWARF_OFFSET_SIZE;
8483 break;
8484 case dw_val_class_fde_ref:
8485 size += DWARF_OFFSET_SIZE;
8486 break;
8487 case dw_val_class_lbl_id:
8488 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8490 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8491 size += size_of_uleb128 (AT_index (a));
8493 else
8494 size += DWARF2_ADDR_SIZE;
8495 break;
8496 case dw_val_class_lineptr:
8497 case dw_val_class_macptr:
8498 size += DWARF_OFFSET_SIZE;
8499 break;
8500 case dw_val_class_str:
8501 form = AT_string_form (a);
8502 if (form == DW_FORM_strp)
8503 size += DWARF_OFFSET_SIZE;
8504 else if (form == DW_FORM_GNU_str_index)
8505 size += size_of_uleb128 (AT_index (a));
8506 else
8507 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8508 break;
8509 case dw_val_class_file:
8510 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8511 break;
8512 case dw_val_class_data8:
8513 size += 8;
8514 break;
8515 case dw_val_class_vms_delta:
8516 size += DWARF_OFFSET_SIZE;
8517 break;
8518 case dw_val_class_high_pc:
8519 size += DWARF2_ADDR_SIZE;
8520 break;
8521 case dw_val_class_discr_value:
8522 size += size_of_discr_value (&a->dw_attr_val.v.val_discr_value);
8523 break;
8524 case dw_val_class_discr_list:
8526 unsigned block_size = size_of_discr_list (AT_discr_list (a));
8528 /* This is a block, so we have the block length and then its
8529 data. */
8530 size += constant_size (block_size) + block_size;
8532 break;
8533 default:
8534 gcc_unreachable ();
8538 return size;
8541 /* Size the debugging information associated with a given DIE. Visits the
8542 DIE's children recursively. Updates the global variable next_die_offset, on
8543 each time through. Uses the current value of next_die_offset to update the
8544 die_offset field in each DIE. */
8546 static void
8547 calc_die_sizes (dw_die_ref die)
8549 dw_die_ref c;
8551 gcc_assert (die->die_offset == 0
8552 || (unsigned long int) die->die_offset == next_die_offset);
8553 die->die_offset = next_die_offset;
8554 next_die_offset += size_of_die (die);
8556 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8558 if (die->die_child != NULL)
8559 /* Count the null byte used to terminate sibling lists. */
8560 next_die_offset += 1;
8563 /* Size just the base type children at the start of the CU.
8564 This is needed because build_abbrev needs to size locs
8565 and sizing of type based stack ops needs to know die_offset
8566 values for the base types. */
8568 static void
8569 calc_base_type_die_sizes (void)
8571 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8572 unsigned int i;
8573 dw_die_ref base_type;
8574 #if ENABLE_ASSERT_CHECKING
8575 dw_die_ref prev = comp_unit_die ()->die_child;
8576 #endif
8578 die_offset += size_of_die (comp_unit_die ());
8579 for (i = 0; base_types.iterate (i, &base_type); i++)
8581 #if ENABLE_ASSERT_CHECKING
8582 gcc_assert (base_type->die_offset == 0
8583 && prev->die_sib == base_type
8584 && base_type->die_child == NULL
8585 && base_type->die_abbrev);
8586 prev = base_type;
8587 #endif
8588 base_type->die_offset = die_offset;
8589 die_offset += size_of_die (base_type);
8593 /* Set the marks for a die and its children. We do this so
8594 that we know whether or not a reference needs to use FORM_ref_addr; only
8595 DIEs in the same CU will be marked. We used to clear out the offset
8596 and use that as the flag, but ran into ordering problems. */
8598 static void
8599 mark_dies (dw_die_ref die)
8601 dw_die_ref c;
8603 gcc_assert (!die->die_mark);
8605 die->die_mark = 1;
8606 FOR_EACH_CHILD (die, c, mark_dies (c));
8609 /* Clear the marks for a die and its children. */
8611 static void
8612 unmark_dies (dw_die_ref die)
8614 dw_die_ref c;
8616 if (! use_debug_types)
8617 gcc_assert (die->die_mark);
8619 die->die_mark = 0;
8620 FOR_EACH_CHILD (die, c, unmark_dies (c));
8623 /* Clear the marks for a die, its children and referred dies. */
8625 static void
8626 unmark_all_dies (dw_die_ref die)
8628 dw_die_ref c;
8629 dw_attr_node *a;
8630 unsigned ix;
8632 if (!die->die_mark)
8633 return;
8634 die->die_mark = 0;
8636 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8638 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8639 if (AT_class (a) == dw_val_class_die_ref)
8640 unmark_all_dies (AT_ref (a));
8643 /* Calculate if the entry should appear in the final output file. It may be
8644 from a pruned a type. */
8646 static bool
8647 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8649 /* By limiting gnu pubnames to definitions only, gold can generate a
8650 gdb index without entries for declarations, which don't include
8651 enough information to be useful. */
8652 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8653 return false;
8655 if (table == pubname_table)
8657 /* Enumerator names are part of the pubname table, but the
8658 parent DW_TAG_enumeration_type die may have been pruned.
8659 Don't output them if that is the case. */
8660 if (p->die->die_tag == DW_TAG_enumerator &&
8661 (p->die->die_parent == NULL
8662 || !p->die->die_parent->die_perennial_p))
8663 return false;
8665 /* Everything else in the pubname table is included. */
8666 return true;
8669 /* The pubtypes table shouldn't include types that have been
8670 pruned. */
8671 return (p->die->die_offset != 0
8672 || !flag_eliminate_unused_debug_types);
8675 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8676 generated for the compilation unit. */
8678 static unsigned long
8679 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8681 unsigned long size;
8682 unsigned i;
8683 pubname_entry *p;
8684 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8686 size = DWARF_PUBNAMES_HEADER_SIZE;
8687 FOR_EACH_VEC_ELT (*names, i, p)
8688 if (include_pubname_in_output (names, p))
8689 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8691 size += DWARF_OFFSET_SIZE;
8692 return size;
8695 /* Return the size of the information in the .debug_aranges section. */
8697 static unsigned long
8698 size_of_aranges (void)
8700 unsigned long size;
8702 size = DWARF_ARANGES_HEADER_SIZE;
8704 /* Count the address/length pair for this compilation unit. */
8705 if (text_section_used)
8706 size += 2 * DWARF2_ADDR_SIZE;
8707 if (cold_text_section_used)
8708 size += 2 * DWARF2_ADDR_SIZE;
8709 if (have_multiple_function_sections)
8711 unsigned fde_idx;
8712 dw_fde_ref fde;
8714 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8716 if (DECL_IGNORED_P (fde->decl))
8717 continue;
8718 if (!fde->in_std_section)
8719 size += 2 * DWARF2_ADDR_SIZE;
8720 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8721 size += 2 * DWARF2_ADDR_SIZE;
8725 /* Count the two zero words used to terminated the address range table. */
8726 size += 2 * DWARF2_ADDR_SIZE;
8727 return size;
8730 /* Select the encoding of an attribute value. */
8732 static enum dwarf_form
8733 value_format (dw_attr_node *a)
8735 switch (AT_class (a))
8737 case dw_val_class_addr:
8738 /* Only very few attributes allow DW_FORM_addr. */
8739 switch (a->dw_attr)
8741 case DW_AT_low_pc:
8742 case DW_AT_high_pc:
8743 case DW_AT_entry_pc:
8744 case DW_AT_trampoline:
8745 return (AT_index (a) == NOT_INDEXED
8746 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8747 default:
8748 break;
8750 switch (DWARF2_ADDR_SIZE)
8752 case 1:
8753 return DW_FORM_data1;
8754 case 2:
8755 return DW_FORM_data2;
8756 case 4:
8757 return DW_FORM_data4;
8758 case 8:
8759 return DW_FORM_data8;
8760 default:
8761 gcc_unreachable ();
8763 case dw_val_class_range_list:
8764 case dw_val_class_loc_list:
8765 if (dwarf_version >= 4)
8766 return DW_FORM_sec_offset;
8767 /* FALLTHRU */
8768 case dw_val_class_vms_delta:
8769 case dw_val_class_offset:
8770 switch (DWARF_OFFSET_SIZE)
8772 case 4:
8773 return DW_FORM_data4;
8774 case 8:
8775 return DW_FORM_data8;
8776 default:
8777 gcc_unreachable ();
8779 case dw_val_class_loc:
8780 if (dwarf_version >= 4)
8781 return DW_FORM_exprloc;
8782 switch (constant_size (size_of_locs (AT_loc (a))))
8784 case 1:
8785 return DW_FORM_block1;
8786 case 2:
8787 return DW_FORM_block2;
8788 case 4:
8789 return DW_FORM_block4;
8790 default:
8791 gcc_unreachable ();
8793 case dw_val_class_const:
8794 return DW_FORM_sdata;
8795 case dw_val_class_unsigned_const:
8796 switch (constant_size (AT_unsigned (a)))
8798 case 1:
8799 return DW_FORM_data1;
8800 case 2:
8801 return DW_FORM_data2;
8802 case 4:
8803 /* In DWARF3 DW_AT_data_member_location with
8804 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8805 constant, so we need to use DW_FORM_udata if we need
8806 a large constant. */
8807 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8808 return DW_FORM_udata;
8809 return DW_FORM_data4;
8810 case 8:
8811 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8812 return DW_FORM_udata;
8813 return DW_FORM_data8;
8814 default:
8815 gcc_unreachable ();
8817 case dw_val_class_const_double:
8818 switch (HOST_BITS_PER_WIDE_INT)
8820 case 8:
8821 return DW_FORM_data2;
8822 case 16:
8823 return DW_FORM_data4;
8824 case 32:
8825 return DW_FORM_data8;
8826 case 64:
8827 default:
8828 return DW_FORM_block1;
8830 case dw_val_class_wide_int:
8831 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8833 case 8:
8834 return DW_FORM_data1;
8835 case 16:
8836 return DW_FORM_data2;
8837 case 32:
8838 return DW_FORM_data4;
8839 case 64:
8840 return DW_FORM_data8;
8841 default:
8842 return DW_FORM_block1;
8844 case dw_val_class_vec:
8845 switch (constant_size (a->dw_attr_val.v.val_vec.length
8846 * a->dw_attr_val.v.val_vec.elt_size))
8848 case 1:
8849 return DW_FORM_block1;
8850 case 2:
8851 return DW_FORM_block2;
8852 case 4:
8853 return DW_FORM_block4;
8854 default:
8855 gcc_unreachable ();
8857 case dw_val_class_flag:
8858 if (dwarf_version >= 4)
8860 /* Currently all add_AT_flag calls pass in 1 as last argument,
8861 so DW_FORM_flag_present can be used. If that ever changes,
8862 we'll need to use DW_FORM_flag and have some optimization
8863 in build_abbrev_table that will change those to
8864 DW_FORM_flag_present if it is set to 1 in all DIEs using
8865 the same abbrev entry. */
8866 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8867 return DW_FORM_flag_present;
8869 return DW_FORM_flag;
8870 case dw_val_class_die_ref:
8871 if (AT_ref_external (a))
8872 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8873 else
8874 return DW_FORM_ref;
8875 case dw_val_class_fde_ref:
8876 return DW_FORM_data;
8877 case dw_val_class_lbl_id:
8878 return (AT_index (a) == NOT_INDEXED
8879 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8880 case dw_val_class_lineptr:
8881 case dw_val_class_macptr:
8882 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8883 case dw_val_class_str:
8884 return AT_string_form (a);
8885 case dw_val_class_file:
8886 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8888 case 1:
8889 return DW_FORM_data1;
8890 case 2:
8891 return DW_FORM_data2;
8892 case 4:
8893 return DW_FORM_data4;
8894 default:
8895 gcc_unreachable ();
8898 case dw_val_class_data8:
8899 return DW_FORM_data8;
8901 case dw_val_class_high_pc:
8902 switch (DWARF2_ADDR_SIZE)
8904 case 1:
8905 return DW_FORM_data1;
8906 case 2:
8907 return DW_FORM_data2;
8908 case 4:
8909 return DW_FORM_data4;
8910 case 8:
8911 return DW_FORM_data8;
8912 default:
8913 gcc_unreachable ();
8916 case dw_val_class_discr_value:
8917 return (a->dw_attr_val.v.val_discr_value.pos
8918 ? DW_FORM_udata
8919 : DW_FORM_sdata);
8920 case dw_val_class_discr_list:
8921 switch (constant_size (size_of_discr_list (AT_discr_list (a))))
8923 case 1:
8924 return DW_FORM_block1;
8925 case 2:
8926 return DW_FORM_block2;
8927 case 4:
8928 return DW_FORM_block4;
8929 default:
8930 gcc_unreachable ();
8933 default:
8934 gcc_unreachable ();
8938 /* Output the encoding of an attribute value. */
8940 static void
8941 output_value_format (dw_attr_node *a)
8943 enum dwarf_form form = value_format (a);
8945 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8948 /* Given a die and id, produce the appropriate abbreviations. */
8950 static void
8951 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8953 unsigned ix;
8954 dw_attr_node *a_attr;
8956 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8957 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8958 dwarf_tag_name (abbrev->die_tag));
8960 if (abbrev->die_child != NULL)
8961 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8962 else
8963 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8965 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8967 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8968 dwarf_attr_name (a_attr->dw_attr));
8969 output_value_format (a_attr);
8972 dw2_asm_output_data (1, 0, NULL);
8973 dw2_asm_output_data (1, 0, NULL);
8977 /* Output the .debug_abbrev section which defines the DIE abbreviation
8978 table. */
8980 static void
8981 output_abbrev_section (void)
8983 unsigned long abbrev_id;
8985 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8986 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8988 /* Terminate the table. */
8989 dw2_asm_output_data (1, 0, NULL);
8992 /* Output a symbol we can use to refer to this DIE from another CU. */
8994 static inline void
8995 output_die_symbol (dw_die_ref die)
8997 const char *sym = die->die_id.die_symbol;
8999 gcc_assert (!die->comdat_type_p);
9001 if (sym == 0)
9002 return;
9004 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
9005 /* We make these global, not weak; if the target doesn't support
9006 .linkonce, it doesn't support combining the sections, so debugging
9007 will break. */
9008 targetm.asm_out.globalize_label (asm_out_file, sym);
9010 ASM_OUTPUT_LABEL (asm_out_file, sym);
9013 /* Return a new location list, given the begin and end range, and the
9014 expression. */
9016 static inline dw_loc_list_ref
9017 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
9018 const char *section)
9020 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
9022 retlist->begin = begin;
9023 retlist->begin_entry = NULL;
9024 retlist->end = end;
9025 retlist->expr = expr;
9026 retlist->section = section;
9028 return retlist;
9031 /* Generate a new internal symbol for this location list node, if it
9032 hasn't got one yet. */
9034 static inline void
9035 gen_llsym (dw_loc_list_ref list)
9037 gcc_assert (!list->ll_symbol);
9038 list->ll_symbol = gen_internal_sym ("LLST");
9041 /* Output the location list given to us. */
9043 static void
9044 output_loc_list (dw_loc_list_ref list_head)
9046 dw_loc_list_ref curr = list_head;
9048 if (list_head->emitted)
9049 return;
9050 list_head->emitted = true;
9052 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
9054 /* Walk the location list, and output each range + expression. */
9055 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
9057 unsigned long size;
9058 /* Don't output an entry that starts and ends at the same address. */
9059 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
9060 continue;
9061 size = size_of_locs (curr->expr);
9062 /* If the expression is too large, drop it on the floor. We could
9063 perhaps put it into DW_TAG_dwarf_procedure and refer to that
9064 in the expression, but >= 64KB expressions for a single value
9065 in a single range are unlikely very useful. */
9066 if (size > 0xffff)
9067 continue;
9068 if (dwarf_split_debug_info)
9070 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
9071 "Location list start/length entry (%s)",
9072 list_head->ll_symbol);
9073 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
9074 "Location list range start index (%s)",
9075 curr->begin);
9076 /* The length field is 4 bytes. If we ever need to support
9077 an 8-byte length, we can add a new DW_LLE code or fall back
9078 to DW_LLE_GNU_start_end_entry. */
9079 dw2_asm_output_delta (4, curr->end, curr->begin,
9080 "Location list range length (%s)",
9081 list_head->ll_symbol);
9083 else if (!have_multiple_function_sections)
9085 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
9086 "Location list begin address (%s)",
9087 list_head->ll_symbol);
9088 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
9089 "Location list end address (%s)",
9090 list_head->ll_symbol);
9092 else
9094 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
9095 "Location list begin address (%s)",
9096 list_head->ll_symbol);
9097 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
9098 "Location list end address (%s)",
9099 list_head->ll_symbol);
9102 /* Output the block length for this list of location operations. */
9103 gcc_assert (size <= 0xffff);
9104 dw2_asm_output_data (2, size, "%s", "Location expression size");
9106 output_loc_sequence (curr->expr, -1);
9109 if (dwarf_split_debug_info)
9110 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
9111 "Location list terminator (%s)",
9112 list_head->ll_symbol);
9113 else
9115 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9116 "Location list terminator begin (%s)",
9117 list_head->ll_symbol);
9118 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
9119 "Location list terminator end (%s)",
9120 list_head->ll_symbol);
9124 /* Output a range_list offset into the debug_range section. Emit a
9125 relocated reference if val_entry is NULL, otherwise, emit an
9126 indirect reference. */
9128 static void
9129 output_range_list_offset (dw_attr_node *a)
9131 const char *name = dwarf_attr_name (a->dw_attr);
9133 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
9135 char *p = strchr (ranges_section_label, '\0');
9136 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
9137 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
9138 debug_ranges_section, "%s", name);
9139 *p = '\0';
9141 else
9142 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9143 "%s (offset from %s)", name, ranges_section_label);
9146 /* Output the offset into the debug_loc section. */
9148 static void
9149 output_loc_list_offset (dw_attr_node *a)
9151 char *sym = AT_loc_list (a)->ll_symbol;
9153 gcc_assert (sym);
9154 if (dwarf_split_debug_info)
9155 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
9156 "%s", dwarf_attr_name (a->dw_attr));
9157 else
9158 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9159 "%s", dwarf_attr_name (a->dw_attr));
9162 /* Output an attribute's index or value appropriately. */
9164 static void
9165 output_attr_index_or_value (dw_attr_node *a)
9167 const char *name = dwarf_attr_name (a->dw_attr);
9169 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
9171 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
9172 return;
9174 switch (AT_class (a))
9176 case dw_val_class_addr:
9177 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
9178 break;
9179 case dw_val_class_high_pc:
9180 case dw_val_class_lbl_id:
9181 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9182 break;
9183 case dw_val_class_loc_list:
9184 output_loc_list_offset (a);
9185 break;
9186 default:
9187 gcc_unreachable ();
9191 /* Output a type signature. */
9193 static inline void
9194 output_signature (const char *sig, const char *name)
9196 int i;
9198 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9199 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
9202 /* Output a discriminant value. */
9204 static inline void
9205 output_discr_value (dw_discr_value *discr_value, const char *name)
9207 if (discr_value->pos)
9208 dw2_asm_output_data_uleb128 (discr_value->v.uval, "%s", name);
9209 else
9210 dw2_asm_output_data_sleb128 (discr_value->v.sval, "%s", name);
9213 /* Output the DIE and its attributes. Called recursively to generate
9214 the definitions of each child DIE. */
9216 static void
9217 output_die (dw_die_ref die)
9219 dw_attr_node *a;
9220 dw_die_ref c;
9221 unsigned long size;
9222 unsigned ix;
9224 /* If someone in another CU might refer to us, set up a symbol for
9225 them to point to. */
9226 if (! die->comdat_type_p && die->die_id.die_symbol)
9227 output_die_symbol (die);
9229 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
9230 (unsigned long)die->die_offset,
9231 dwarf_tag_name (die->die_tag));
9233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
9235 const char *name = dwarf_attr_name (a->dw_attr);
9237 switch (AT_class (a))
9239 case dw_val_class_addr:
9240 output_attr_index_or_value (a);
9241 break;
9243 case dw_val_class_offset:
9244 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
9245 "%s", name);
9246 break;
9248 case dw_val_class_range_list:
9249 output_range_list_offset (a);
9250 break;
9252 case dw_val_class_loc:
9253 size = size_of_locs (AT_loc (a));
9255 /* Output the block length for this list of location operations. */
9256 if (dwarf_version >= 4)
9257 dw2_asm_output_data_uleb128 (size, "%s", name);
9258 else
9259 dw2_asm_output_data (constant_size (size), size, "%s", name);
9261 output_loc_sequence (AT_loc (a), -1);
9262 break;
9264 case dw_val_class_const:
9265 /* ??? It would be slightly more efficient to use a scheme like is
9266 used for unsigned constants below, but gdb 4.x does not sign
9267 extend. Gdb 5.x does sign extend. */
9268 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9269 break;
9271 case dw_val_class_unsigned_const:
9273 int csize = constant_size (AT_unsigned (a));
9274 if (dwarf_version == 3
9275 && a->dw_attr == DW_AT_data_member_location
9276 && csize >= 4)
9277 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
9278 else
9279 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
9281 break;
9283 case dw_val_class_const_double:
9285 unsigned HOST_WIDE_INT first, second;
9287 if (HOST_BITS_PER_WIDE_INT >= 64)
9288 dw2_asm_output_data (1,
9289 HOST_BITS_PER_DOUBLE_INT
9290 / HOST_BITS_PER_CHAR,
9291 NULL);
9293 if (WORDS_BIG_ENDIAN)
9295 first = a->dw_attr_val.v.val_double.high;
9296 second = a->dw_attr_val.v.val_double.low;
9298 else
9300 first = a->dw_attr_val.v.val_double.low;
9301 second = a->dw_attr_val.v.val_double.high;
9304 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9305 first, "%s", name);
9306 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9307 second, NULL);
9309 break;
9311 case dw_val_class_wide_int:
9313 int i;
9314 int len = get_full_len (*a->dw_attr_val.v.val_wide);
9315 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9316 if (len * HOST_BITS_PER_WIDE_INT > 64)
9317 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
9318 NULL);
9320 if (WORDS_BIG_ENDIAN)
9321 for (i = len - 1; i >= 0; --i)
9323 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9324 "%s", name);
9325 name = "";
9327 else
9328 for (i = 0; i < len; ++i)
9330 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
9331 "%s", name);
9332 name = "";
9335 break;
9337 case dw_val_class_vec:
9339 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9340 unsigned int len = a->dw_attr_val.v.val_vec.length;
9341 unsigned int i;
9342 unsigned char *p;
9344 dw2_asm_output_data (constant_size (len * elt_size),
9345 len * elt_size, "%s", name);
9346 if (elt_size > sizeof (HOST_WIDE_INT))
9348 elt_size /= 2;
9349 len *= 2;
9351 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9352 i < len;
9353 i++, p += elt_size)
9354 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9355 "fp or vector constant word %u", i);
9356 break;
9359 case dw_val_class_flag:
9360 if (dwarf_version >= 4)
9362 /* Currently all add_AT_flag calls pass in 1 as last argument,
9363 so DW_FORM_flag_present can be used. If that ever changes,
9364 we'll need to use DW_FORM_flag and have some optimization
9365 in build_abbrev_table that will change those to
9366 DW_FORM_flag_present if it is set to 1 in all DIEs using
9367 the same abbrev entry. */
9368 gcc_assert (AT_flag (a) == 1);
9369 if (flag_debug_asm)
9370 fprintf (asm_out_file, "\t\t\t%s %s\n",
9371 ASM_COMMENT_START, name);
9372 break;
9374 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9375 break;
9377 case dw_val_class_loc_list:
9378 output_attr_index_or_value (a);
9379 break;
9381 case dw_val_class_die_ref:
9382 if (AT_ref_external (a))
9384 if (AT_ref (a)->comdat_type_p)
9386 comdat_type_node *type_node =
9387 AT_ref (a)->die_id.die_type_node;
9389 gcc_assert (type_node);
9390 output_signature (type_node->signature, name);
9392 else
9394 const char *sym = AT_ref (a)->die_id.die_symbol;
9395 int size;
9397 gcc_assert (sym);
9398 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9399 length, whereas in DWARF3 it's always sized as an
9400 offset. */
9401 if (dwarf_version == 2)
9402 size = DWARF2_ADDR_SIZE;
9403 else
9404 size = DWARF_OFFSET_SIZE;
9405 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9406 name);
9409 else
9411 gcc_assert (AT_ref (a)->die_offset);
9412 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9413 "%s", name);
9415 break;
9417 case dw_val_class_fde_ref:
9419 char l1[MAX_ARTIFICIAL_LABEL_BYTES];
9421 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9422 a->dw_attr_val.v.val_fde_index * 2);
9423 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9424 "%s", name);
9426 break;
9428 case dw_val_class_vms_delta:
9429 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9430 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9431 AT_vms_delta2 (a), AT_vms_delta1 (a),
9432 "%s", name);
9433 #else
9434 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9435 AT_vms_delta2 (a), AT_vms_delta1 (a),
9436 "%s", name);
9437 #endif
9438 break;
9440 case dw_val_class_lbl_id:
9441 output_attr_index_or_value (a);
9442 break;
9444 case dw_val_class_lineptr:
9445 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9446 debug_line_section, "%s", name);
9447 break;
9449 case dw_val_class_macptr:
9450 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9451 debug_macinfo_section, "%s", name);
9452 break;
9454 case dw_val_class_str:
9455 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9456 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9457 a->dw_attr_val.v.val_str->label,
9458 debug_str_section,
9459 "%s: \"%s\"", name, AT_string (a));
9460 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9461 dw2_asm_output_data_uleb128 (AT_index (a),
9462 "%s: \"%s\"", name, AT_string (a));
9463 else
9464 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9465 break;
9467 case dw_val_class_file:
9469 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9471 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9472 a->dw_attr_val.v.val_file->filename);
9473 break;
9476 case dw_val_class_data8:
9478 int i;
9480 for (i = 0; i < 8; i++)
9481 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9482 i == 0 ? "%s" : NULL, name);
9483 break;
9486 case dw_val_class_high_pc:
9487 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9488 get_AT_low_pc (die), "DW_AT_high_pc");
9489 break;
9491 case dw_val_class_discr_value:
9492 output_discr_value (&a->dw_attr_val.v.val_discr_value, name);
9493 break;
9495 case dw_val_class_discr_list:
9497 dw_discr_list_ref list = AT_discr_list (a);
9498 const int size = size_of_discr_list (list);
9500 /* This is a block, so output its length first. */
9501 dw2_asm_output_data (constant_size (size), size,
9502 "%s: block size", name);
9504 for (; list != NULL; list = list->dw_discr_next)
9506 /* One byte for the discriminant value descriptor, and then as
9507 many LEB128 numbers as required. */
9508 if (list->dw_discr_range)
9509 dw2_asm_output_data (1, DW_DSC_range,
9510 "%s: DW_DSC_range", name);
9511 else
9512 dw2_asm_output_data (1, DW_DSC_label,
9513 "%s: DW_DSC_label", name);
9515 output_discr_value (&list->dw_discr_lower_bound, name);
9516 if (list->dw_discr_range)
9517 output_discr_value (&list->dw_discr_upper_bound, name);
9519 break;
9522 default:
9523 gcc_unreachable ();
9527 FOR_EACH_CHILD (die, c, output_die (c));
9529 /* Add null byte to terminate sibling list. */
9530 if (die->die_child != NULL)
9531 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9532 (unsigned long) die->die_offset);
9535 /* Output the compilation unit that appears at the beginning of the
9536 .debug_info section, and precedes the DIE descriptions. */
9538 static void
9539 output_compilation_unit_header (void)
9541 /* We don't support actual DWARFv5 units yet, we just use some
9542 DWARFv5 draft DIE tags in DWARFv4 format. */
9543 int ver = dwarf_version < 5 ? dwarf_version : 4;
9545 if (!XCOFF_DEBUGGING_INFO)
9547 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9548 dw2_asm_output_data (4, 0xffffffff,
9549 "Initial length escape value indicating 64-bit DWARF extension");
9550 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9551 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9552 "Length of Compilation Unit Info");
9555 dw2_asm_output_data (2, ver, "DWARF version number");
9556 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9557 debug_abbrev_section,
9558 "Offset Into Abbrev. Section");
9559 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9562 /* Output the compilation unit DIE and its children. */
9564 static void
9565 output_comp_unit (dw_die_ref die, int output_if_empty)
9567 const char *secname, *oldsym;
9568 char *tmp;
9570 /* Unless we are outputting main CU, we may throw away empty ones. */
9571 if (!output_if_empty && die->die_child == NULL)
9572 return;
9574 /* Even if there are no children of this DIE, we must output the information
9575 about the compilation unit. Otherwise, on an empty translation unit, we
9576 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9577 will then complain when examining the file. First mark all the DIEs in
9578 this CU so we know which get local refs. */
9579 mark_dies (die);
9581 external_ref_hash_type *extern_map = optimize_external_refs (die);
9583 build_abbrev_table (die, extern_map);
9585 delete extern_map;
9587 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9588 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9589 calc_die_sizes (die);
9591 oldsym = die->die_id.die_symbol;
9592 if (oldsym)
9594 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9596 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9597 secname = tmp;
9598 die->die_id.die_symbol = NULL;
9599 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9601 else
9603 switch_to_section (debug_info_section);
9604 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9605 info_section_emitted = true;
9608 /* Output debugging information. */
9609 output_compilation_unit_header ();
9610 output_die (die);
9612 /* Leave the marks on the main CU, so we can check them in
9613 output_pubnames. */
9614 if (oldsym)
9616 unmark_dies (die);
9617 die->die_id.die_symbol = oldsym;
9621 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9622 and .debug_pubtypes. This is configured per-target, but can be
9623 overridden by the -gpubnames or -gno-pubnames options. */
9625 static inline bool
9626 want_pubnames (void)
9628 if (debug_info_level <= DINFO_LEVEL_TERSE)
9629 return false;
9630 if (debug_generate_pub_sections != -1)
9631 return debug_generate_pub_sections;
9632 return targetm.want_debug_pub_sections;
9635 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9637 static void
9638 add_AT_pubnames (dw_die_ref die)
9640 if (want_pubnames ())
9641 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9644 /* Add a string attribute value to a skeleton DIE. */
9646 static inline void
9647 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9648 const char *str)
9650 dw_attr_node attr;
9651 struct indirect_string_node *node;
9653 if (! skeleton_debug_str_hash)
9654 skeleton_debug_str_hash
9655 = hash_table<indirect_string_hasher>::create_ggc (10);
9657 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9658 find_string_form (node);
9659 if (node->form == DW_FORM_GNU_str_index)
9660 node->form = DW_FORM_strp;
9662 attr.dw_attr = attr_kind;
9663 attr.dw_attr_val.val_class = dw_val_class_str;
9664 attr.dw_attr_val.val_entry = NULL;
9665 attr.dw_attr_val.v.val_str = node;
9666 add_dwarf_attr (die, &attr);
9669 /* Helper function to generate top-level dies for skeleton debug_info and
9670 debug_types. */
9672 static void
9673 add_top_level_skeleton_die_attrs (dw_die_ref die)
9675 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9676 const char *comp_dir = comp_dir_string ();
9678 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9679 if (comp_dir != NULL)
9680 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9681 add_AT_pubnames (die);
9682 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9685 /* Output skeleton debug sections that point to the dwo file. */
9687 static void
9688 output_skeleton_debug_sections (dw_die_ref comp_unit)
9690 /* We don't support actual DWARFv5 units yet, we just use some
9691 DWARFv5 draft DIE tags in DWARFv4 format. */
9692 int ver = dwarf_version < 5 ? dwarf_version : 4;
9694 /* These attributes will be found in the full debug_info section. */
9695 remove_AT (comp_unit, DW_AT_producer);
9696 remove_AT (comp_unit, DW_AT_language);
9698 switch_to_section (debug_skeleton_info_section);
9699 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9701 /* Produce the skeleton compilation-unit header. This one differs enough from
9702 a normal CU header that it's better not to call output_compilation_unit
9703 header. */
9704 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9705 dw2_asm_output_data (4, 0xffffffff,
9706 "Initial length escape value indicating 64-bit DWARF extension");
9708 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9709 DWARF_COMPILE_UNIT_HEADER_SIZE
9710 - DWARF_INITIAL_LENGTH_SIZE
9711 + size_of_die (comp_unit),
9712 "Length of Compilation Unit Info");
9713 dw2_asm_output_data (2, ver, "DWARF version number");
9714 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9715 debug_abbrev_section,
9716 "Offset Into Abbrev. Section");
9717 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9719 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9720 output_die (comp_unit);
9722 /* Build the skeleton debug_abbrev section. */
9723 switch_to_section (debug_skeleton_abbrev_section);
9724 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9726 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9728 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9731 /* Output a comdat type unit DIE and its children. */
9733 static void
9734 output_comdat_type_unit (comdat_type_node *node)
9736 const char *secname;
9737 char *tmp;
9738 int i;
9739 #if defined (OBJECT_FORMAT_ELF)
9740 tree comdat_key;
9741 #endif
9743 /* First mark all the DIEs in this CU so we know which get local refs. */
9744 mark_dies (node->root_die);
9746 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9748 build_abbrev_table (node->root_die, extern_map);
9750 delete extern_map;
9751 extern_map = NULL;
9753 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9754 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9755 calc_die_sizes (node->root_die);
9757 #if defined (OBJECT_FORMAT_ELF)
9758 if (!dwarf_split_debug_info)
9759 secname = ".debug_types";
9760 else
9761 secname = ".debug_types.dwo";
9763 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9764 sprintf (tmp, "wt.");
9765 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9766 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9767 comdat_key = get_identifier (tmp);
9768 targetm.asm_out.named_section (secname,
9769 SECTION_DEBUG | SECTION_LINKONCE,
9770 comdat_key);
9771 #else
9772 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9773 sprintf (tmp, ".gnu.linkonce.wt.");
9774 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9775 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9776 secname = tmp;
9777 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9778 #endif
9780 /* Output debugging information. */
9781 output_compilation_unit_header ();
9782 output_signature (node->signature, "Type Signature");
9783 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9784 "Offset to Type DIE");
9785 output_die (node->root_die);
9787 unmark_dies (node->root_die);
9790 /* Return the DWARF2/3 pubname associated with a decl. */
9792 static const char *
9793 dwarf2_name (tree decl, int scope)
9795 if (DECL_NAMELESS (decl))
9796 return NULL;
9797 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9800 /* Add a new entry to .debug_pubnames if appropriate. */
9802 static void
9803 add_pubname_string (const char *str, dw_die_ref die)
9805 pubname_entry e;
9807 e.die = die;
9808 e.name = xstrdup (str);
9809 vec_safe_push (pubname_table, e);
9812 static void
9813 add_pubname (tree decl, dw_die_ref die)
9815 if (!want_pubnames ())
9816 return;
9818 /* Don't add items to the table when we expect that the consumer will have
9819 just read the enclosing die. For example, if the consumer is looking at a
9820 class_member, it will either be inside the class already, or will have just
9821 looked up the class to find the member. Either way, searching the class is
9822 faster than searching the index. */
9823 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9824 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9826 const char *name = dwarf2_name (decl, 1);
9828 if (name)
9829 add_pubname_string (name, die);
9833 /* Add an enumerator to the pubnames section. */
9835 static void
9836 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9838 pubname_entry e;
9840 gcc_assert (scope_name);
9841 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9842 e.die = die;
9843 vec_safe_push (pubname_table, e);
9846 /* Add a new entry to .debug_pubtypes if appropriate. */
9848 static void
9849 add_pubtype (tree decl, dw_die_ref die)
9851 pubname_entry e;
9853 if (!want_pubnames ())
9854 return;
9856 if ((TREE_PUBLIC (decl)
9857 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9858 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9860 tree scope = NULL;
9861 const char *scope_name = "";
9862 const char *sep = is_cxx () ? "::" : ".";
9863 const char *name;
9865 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9866 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9868 scope_name = lang_hooks.dwarf_name (scope, 1);
9869 if (scope_name != NULL && scope_name[0] != '\0')
9870 scope_name = concat (scope_name, sep, NULL);
9871 else
9872 scope_name = "";
9875 if (TYPE_P (decl))
9876 name = type_tag (decl);
9877 else
9878 name = lang_hooks.dwarf_name (decl, 1);
9880 /* If we don't have a name for the type, there's no point in adding
9881 it to the table. */
9882 if (name != NULL && name[0] != '\0')
9884 e.die = die;
9885 e.name = concat (scope_name, name, NULL);
9886 vec_safe_push (pubtype_table, e);
9889 /* Although it might be more consistent to add the pubinfo for the
9890 enumerators as their dies are created, they should only be added if the
9891 enum type meets the criteria above. So rather than re-check the parent
9892 enum type whenever an enumerator die is created, just output them all
9893 here. This isn't protected by the name conditional because anonymous
9894 enums don't have names. */
9895 if (die->die_tag == DW_TAG_enumeration_type)
9897 dw_die_ref c;
9899 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9904 /* Output a single entry in the pubnames table. */
9906 static void
9907 output_pubname (dw_offset die_offset, pubname_entry *entry)
9909 dw_die_ref die = entry->die;
9910 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9912 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9914 if (debug_generate_pub_sections == 2)
9916 /* This logic follows gdb's method for determining the value of the flag
9917 byte. */
9918 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9919 switch (die->die_tag)
9921 case DW_TAG_typedef:
9922 case DW_TAG_base_type:
9923 case DW_TAG_subrange_type:
9924 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9925 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9926 break;
9927 case DW_TAG_enumerator:
9928 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9929 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9930 if (!is_cxx () && !is_java ())
9931 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9932 break;
9933 case DW_TAG_subprogram:
9934 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9935 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9936 if (!is_ada ())
9937 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9938 break;
9939 case DW_TAG_constant:
9940 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9941 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9942 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9943 break;
9944 case DW_TAG_variable:
9945 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9946 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9947 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9948 break;
9949 case DW_TAG_namespace:
9950 case DW_TAG_imported_declaration:
9951 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9952 break;
9953 case DW_TAG_class_type:
9954 case DW_TAG_interface_type:
9955 case DW_TAG_structure_type:
9956 case DW_TAG_union_type:
9957 case DW_TAG_enumeration_type:
9958 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9959 if (!is_cxx () && !is_java ())
9960 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9961 break;
9962 default:
9963 /* An unusual tag. Leave the flag-byte empty. */
9964 break;
9966 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9967 "GDB-index flags");
9970 dw2_asm_output_nstring (entry->name, -1, "external name");
9974 /* Output the public names table used to speed up access to externally
9975 visible names; or the public types table used to find type definitions. */
9977 static void
9978 output_pubnames (vec<pubname_entry, va_gc> *names)
9980 unsigned i;
9981 unsigned long pubnames_length = size_of_pubnames (names);
9982 pubname_entry *pub;
9984 if (!XCOFF_DEBUGGING_INFO)
9986 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9987 dw2_asm_output_data (4, 0xffffffff,
9988 "Initial length escape value indicating 64-bit DWARF extension");
9989 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9990 "Pub Info Length");
9993 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9994 dw2_asm_output_data (2, 2, "DWARF Version");
9996 if (dwarf_split_debug_info)
9997 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9998 debug_skeleton_info_section,
9999 "Offset of Compilation Unit Info");
10000 else
10001 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10002 debug_info_section,
10003 "Offset of Compilation Unit Info");
10004 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10005 "Compilation Unit Length");
10007 FOR_EACH_VEC_ELT (*names, i, pub)
10009 if (include_pubname_in_output (names, pub))
10011 dw_offset die_offset = pub->die->die_offset;
10013 /* We shouldn't see pubnames for DIEs outside of the main CU. */
10014 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
10015 gcc_assert (pub->die->die_mark);
10017 /* If we're putting types in their own .debug_types sections,
10018 the .debug_pubtypes table will still point to the compile
10019 unit (not the type unit), so we want to use the offset of
10020 the skeleton DIE (if there is one). */
10021 if (pub->die->comdat_type_p && names == pubtype_table)
10023 comdat_type_node *type_node = pub->die->die_id.die_type_node;
10025 if (type_node != NULL)
10026 die_offset = (type_node->skeleton_die != NULL
10027 ? type_node->skeleton_die->die_offset
10028 : comp_unit_die ()->die_offset);
10031 output_pubname (die_offset, pub);
10035 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10038 /* Output public names and types tables if necessary. */
10040 static void
10041 output_pubtables (void)
10043 if (!want_pubnames () || !info_section_emitted)
10044 return;
10046 switch_to_section (debug_pubnames_section);
10047 output_pubnames (pubname_table);
10048 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
10049 It shouldn't hurt to emit it always, since pure DWARF2 consumers
10050 simply won't look for the section. */
10051 switch_to_section (debug_pubtypes_section);
10052 output_pubnames (pubtype_table);
10056 /* Output the information that goes into the .debug_aranges table.
10057 Namely, define the beginning and ending address range of the
10058 text section generated for this compilation unit. */
10060 static void
10061 output_aranges (void)
10063 unsigned i;
10064 unsigned long aranges_length = size_of_aranges ();
10066 if (!XCOFF_DEBUGGING_INFO)
10068 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10069 dw2_asm_output_data (4, 0xffffffff,
10070 "Initial length escape value indicating 64-bit DWARF extension");
10071 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10072 "Length of Address Ranges Info");
10075 /* Version number for aranges is still 2, even up to DWARF5. */
10076 dw2_asm_output_data (2, 2, "DWARF Version");
10077 if (dwarf_split_debug_info)
10078 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
10079 debug_skeleton_info_section,
10080 "Offset of Compilation Unit Info");
10081 else
10082 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10083 debug_info_section,
10084 "Offset of Compilation Unit Info");
10085 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10086 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10088 /* We need to align to twice the pointer size here. */
10089 if (DWARF_ARANGES_PAD_SIZE)
10091 /* Pad using a 2 byte words so that padding is correct for any
10092 pointer size. */
10093 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10094 2 * DWARF2_ADDR_SIZE);
10095 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10096 dw2_asm_output_data (2, 0, NULL);
10099 /* It is necessary not to output these entries if the sections were
10100 not used; if the sections were not used, the length will be 0 and
10101 the address may end up as 0 if the section is discarded by ld
10102 --gc-sections, leaving an invalid (0, 0) entry that can be
10103 confused with the terminator. */
10104 if (text_section_used)
10106 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10107 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10108 text_section_label, "Length");
10110 if (cold_text_section_used)
10112 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10113 "Address");
10114 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10115 cold_text_section_label, "Length");
10118 if (have_multiple_function_sections)
10120 unsigned fde_idx;
10121 dw_fde_ref fde;
10123 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
10125 if (DECL_IGNORED_P (fde->decl))
10126 continue;
10127 if (!fde->in_std_section)
10129 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
10130 "Address");
10131 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
10132 fde->dw_fde_begin, "Length");
10134 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10136 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
10137 "Address");
10138 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
10139 fde->dw_fde_second_begin, "Length");
10144 /* Output the terminator words. */
10145 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10146 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10149 /* Add a new entry to .debug_ranges. Return the offset at which it
10150 was placed. */
10152 static unsigned int
10153 add_ranges_num (int num)
10155 unsigned int in_use = ranges_table_in_use;
10157 if (in_use == ranges_table_allocated)
10159 ranges_table_allocated += RANGES_TABLE_INCREMENT;
10160 ranges_table = GGC_RESIZEVEC (dw_ranges, ranges_table,
10161 ranges_table_allocated);
10162 memset (ranges_table + ranges_table_in_use, 0,
10163 RANGES_TABLE_INCREMENT * sizeof (dw_ranges));
10166 ranges_table[in_use].num = num;
10167 ranges_table_in_use = in_use + 1;
10169 return in_use * 2 * DWARF2_ADDR_SIZE;
10172 /* Add a new entry to .debug_ranges corresponding to a block, or a
10173 range terminator if BLOCK is NULL. */
10175 static unsigned int
10176 add_ranges (const_tree block)
10178 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10181 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
10182 When using dwarf_split_debug_info, address attributes in dies destined
10183 for the final executable should be direct references--setting the
10184 parameter force_direct ensures this behavior. */
10186 static void
10187 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
10188 bool *added, bool force_direct)
10190 unsigned int in_use = ranges_by_label_in_use;
10191 unsigned int offset;
10193 if (in_use == ranges_by_label_allocated)
10195 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10196 ranges_by_label = GGC_RESIZEVEC (dw_ranges_by_label, ranges_by_label,
10197 ranges_by_label_allocated);
10198 memset (ranges_by_label + ranges_by_label_in_use, 0,
10199 RANGES_TABLE_INCREMENT * sizeof (dw_ranges_by_label));
10202 ranges_by_label[in_use].begin = begin;
10203 ranges_by_label[in_use].end = end;
10204 ranges_by_label_in_use = in_use + 1;
10206 offset = add_ranges_num (-(int)in_use - 1);
10207 if (!*added)
10209 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
10210 *added = true;
10214 static void
10215 output_ranges (void)
10217 unsigned i;
10218 static const char *const start_fmt = "Offset %#x";
10219 const char *fmt = start_fmt;
10221 for (i = 0; i < ranges_table_in_use; i++)
10223 int block_num = ranges_table[i].num;
10225 if (block_num > 0)
10227 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10228 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10230 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10231 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10233 /* If all code is in the text section, then the compilation
10234 unit base address defaults to DW_AT_low_pc, which is the
10235 base of the text section. */
10236 if (!have_multiple_function_sections)
10238 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10239 text_section_label,
10240 fmt, i * 2 * DWARF2_ADDR_SIZE);
10241 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10242 text_section_label, NULL);
10245 /* Otherwise, the compilation unit base address is zero,
10246 which allows us to use absolute addresses, and not worry
10247 about whether the target supports cross-section
10248 arithmetic. */
10249 else
10251 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10252 fmt, i * 2 * DWARF2_ADDR_SIZE);
10253 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10256 fmt = NULL;
10259 /* Negative block_num stands for an index into ranges_by_label. */
10260 else if (block_num < 0)
10262 int lab_idx = - block_num - 1;
10264 if (!have_multiple_function_sections)
10266 gcc_unreachable ();
10267 #if 0
10268 /* If we ever use add_ranges_by_labels () for a single
10269 function section, all we have to do is to take out
10270 the #if 0 above. */
10271 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10272 ranges_by_label[lab_idx].begin,
10273 text_section_label,
10274 fmt, i * 2 * DWARF2_ADDR_SIZE);
10275 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
10276 ranges_by_label[lab_idx].end,
10277 text_section_label, NULL);
10278 #endif
10280 else
10282 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10283 ranges_by_label[lab_idx].begin,
10284 fmt, i * 2 * DWARF2_ADDR_SIZE);
10285 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
10286 ranges_by_label[lab_idx].end,
10287 NULL);
10290 else
10292 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10293 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10294 fmt = start_fmt;
10299 /* Data structure containing information about input files. */
10300 struct file_info
10302 const char *path; /* Complete file name. */
10303 const char *fname; /* File name part. */
10304 int length; /* Length of entire string. */
10305 struct dwarf_file_data * file_idx; /* Index in input file table. */
10306 int dir_idx; /* Index in directory table. */
10309 /* Data structure containing information about directories with source
10310 files. */
10311 struct dir_info
10313 const char *path; /* Path including directory name. */
10314 int length; /* Path length. */
10315 int prefix; /* Index of directory entry which is a prefix. */
10316 int count; /* Number of files in this directory. */
10317 int dir_idx; /* Index of directory used as base. */
10320 /* Callback function for file_info comparison. We sort by looking at
10321 the directories in the path. */
10323 static int
10324 file_info_cmp (const void *p1, const void *p2)
10326 const struct file_info *const s1 = (const struct file_info *) p1;
10327 const struct file_info *const s2 = (const struct file_info *) p2;
10328 const unsigned char *cp1;
10329 const unsigned char *cp2;
10331 /* Take care of file names without directories. We need to make sure that
10332 we return consistent values to qsort since some will get confused if
10333 we return the same value when identical operands are passed in opposite
10334 orders. So if neither has a directory, return 0 and otherwise return
10335 1 or -1 depending on which one has the directory. */
10336 if ((s1->path == s1->fname || s2->path == s2->fname))
10337 return (s2->path == s2->fname) - (s1->path == s1->fname);
10339 cp1 = (const unsigned char *) s1->path;
10340 cp2 = (const unsigned char *) s2->path;
10342 while (1)
10344 ++cp1;
10345 ++cp2;
10346 /* Reached the end of the first path? If so, handle like above. */
10347 if ((cp1 == (const unsigned char *) s1->fname)
10348 || (cp2 == (const unsigned char *) s2->fname))
10349 return ((cp2 == (const unsigned char *) s2->fname)
10350 - (cp1 == (const unsigned char *) s1->fname));
10352 /* Character of current path component the same? */
10353 else if (*cp1 != *cp2)
10354 return *cp1 - *cp2;
10358 struct file_name_acquire_data
10360 struct file_info *files;
10361 int used_files;
10362 int max_files;
10365 /* Traversal function for the hash table. */
10368 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
10370 struct dwarf_file_data *d = *slot;
10371 struct file_info *fi;
10372 const char *f;
10374 gcc_assert (fnad->max_files >= d->emitted_number);
10376 if (! d->emitted_number)
10377 return 1;
10379 gcc_assert (fnad->max_files != fnad->used_files);
10381 fi = fnad->files + fnad->used_files++;
10383 /* Skip all leading "./". */
10384 f = d->filename;
10385 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10386 f += 2;
10388 /* Create a new array entry. */
10389 fi->path = f;
10390 fi->length = strlen (f);
10391 fi->file_idx = d;
10393 /* Search for the file name part. */
10394 f = strrchr (f, DIR_SEPARATOR);
10395 #if defined (DIR_SEPARATOR_2)
10397 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10399 if (g != NULL)
10401 if (f == NULL || f < g)
10402 f = g;
10405 #endif
10407 fi->fname = f == NULL ? fi->path : f + 1;
10408 return 1;
10411 /* Output the directory table and the file name table. We try to minimize
10412 the total amount of memory needed. A heuristic is used to avoid large
10413 slowdowns with many input files. */
10415 static void
10416 output_file_names (void)
10418 struct file_name_acquire_data fnad;
10419 int numfiles;
10420 struct file_info *files;
10421 struct dir_info *dirs;
10422 int *saved;
10423 int *savehere;
10424 int *backmap;
10425 int ndirs;
10426 int idx_offset;
10427 int i;
10429 if (!last_emitted_file)
10431 dw2_asm_output_data (1, 0, "End directory table");
10432 dw2_asm_output_data (1, 0, "End file name table");
10433 return;
10436 numfiles = last_emitted_file->emitted_number;
10438 /* Allocate the various arrays we need. */
10439 files = XALLOCAVEC (struct file_info, numfiles);
10440 dirs = XALLOCAVEC (struct dir_info, numfiles);
10442 fnad.files = files;
10443 fnad.used_files = 0;
10444 fnad.max_files = numfiles;
10445 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10446 gcc_assert (fnad.used_files == fnad.max_files);
10448 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10450 /* Find all the different directories used. */
10451 dirs[0].path = files[0].path;
10452 dirs[0].length = files[0].fname - files[0].path;
10453 dirs[0].prefix = -1;
10454 dirs[0].count = 1;
10455 dirs[0].dir_idx = 0;
10456 files[0].dir_idx = 0;
10457 ndirs = 1;
10459 for (i = 1; i < numfiles; i++)
10460 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10461 && memcmp (dirs[ndirs - 1].path, files[i].path,
10462 dirs[ndirs - 1].length) == 0)
10464 /* Same directory as last entry. */
10465 files[i].dir_idx = ndirs - 1;
10466 ++dirs[ndirs - 1].count;
10468 else
10470 int j;
10472 /* This is a new directory. */
10473 dirs[ndirs].path = files[i].path;
10474 dirs[ndirs].length = files[i].fname - files[i].path;
10475 dirs[ndirs].count = 1;
10476 dirs[ndirs].dir_idx = ndirs;
10477 files[i].dir_idx = ndirs;
10479 /* Search for a prefix. */
10480 dirs[ndirs].prefix = -1;
10481 for (j = 0; j < ndirs; j++)
10482 if (dirs[j].length < dirs[ndirs].length
10483 && dirs[j].length > 1
10484 && (dirs[ndirs].prefix == -1
10485 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10486 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10487 dirs[ndirs].prefix = j;
10489 ++ndirs;
10492 /* Now to the actual work. We have to find a subset of the directories which
10493 allow expressing the file name using references to the directory table
10494 with the least amount of characters. We do not do an exhaustive search
10495 where we would have to check out every combination of every single
10496 possible prefix. Instead we use a heuristic which provides nearly optimal
10497 results in most cases and never is much off. */
10498 saved = XALLOCAVEC (int, ndirs);
10499 savehere = XALLOCAVEC (int, ndirs);
10501 memset (saved, '\0', ndirs * sizeof (saved[0]));
10502 for (i = 0; i < ndirs; i++)
10504 int j;
10505 int total;
10507 /* We can always save some space for the current directory. But this
10508 does not mean it will be enough to justify adding the directory. */
10509 savehere[i] = dirs[i].length;
10510 total = (savehere[i] - saved[i]) * dirs[i].count;
10512 for (j = i + 1; j < ndirs; j++)
10514 savehere[j] = 0;
10515 if (saved[j] < dirs[i].length)
10517 /* Determine whether the dirs[i] path is a prefix of the
10518 dirs[j] path. */
10519 int k;
10521 k = dirs[j].prefix;
10522 while (k != -1 && k != (int) i)
10523 k = dirs[k].prefix;
10525 if (k == (int) i)
10527 /* Yes it is. We can possibly save some memory by
10528 writing the filenames in dirs[j] relative to
10529 dirs[i]. */
10530 savehere[j] = dirs[i].length;
10531 total += (savehere[j] - saved[j]) * dirs[j].count;
10536 /* Check whether we can save enough to justify adding the dirs[i]
10537 directory. */
10538 if (total > dirs[i].length + 1)
10540 /* It's worthwhile adding. */
10541 for (j = i; j < ndirs; j++)
10542 if (savehere[j] > 0)
10544 /* Remember how much we saved for this directory so far. */
10545 saved[j] = savehere[j];
10547 /* Remember the prefix directory. */
10548 dirs[j].dir_idx = i;
10553 /* Emit the directory name table. */
10554 idx_offset = dirs[0].length > 0 ? 1 : 0;
10555 for (i = 1 - idx_offset; i < ndirs; i++)
10556 dw2_asm_output_nstring (dirs[i].path,
10557 dirs[i].length
10558 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10559 "Directory Entry: %#x", i + idx_offset);
10561 dw2_asm_output_data (1, 0, "End directory table");
10563 /* We have to emit them in the order of emitted_number since that's
10564 used in the debug info generation. To do this efficiently we
10565 generate a back-mapping of the indices first. */
10566 backmap = XALLOCAVEC (int, numfiles);
10567 for (i = 0; i < numfiles; i++)
10568 backmap[files[i].file_idx->emitted_number - 1] = i;
10570 /* Now write all the file names. */
10571 for (i = 0; i < numfiles; i++)
10573 int file_idx = backmap[i];
10574 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10576 #ifdef VMS_DEBUGGING_INFO
10577 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10579 /* Setting these fields can lead to debugger miscomparisons,
10580 but VMS Debug requires them to be set correctly. */
10582 int ver;
10583 long long cdt;
10584 long siz;
10585 int maxfilelen = strlen (files[file_idx].path)
10586 + dirs[dir_idx].length
10587 + MAX_VMS_VERSION_LEN + 1;
10588 char *filebuf = XALLOCAVEC (char, maxfilelen);
10590 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10591 snprintf (filebuf, maxfilelen, "%s;%d",
10592 files[file_idx].path + dirs[dir_idx].length, ver);
10594 dw2_asm_output_nstring
10595 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10597 /* Include directory index. */
10598 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10600 /* Modification time. */
10601 dw2_asm_output_data_uleb128
10602 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10603 ? cdt : 0,
10604 NULL);
10606 /* File length in bytes. */
10607 dw2_asm_output_data_uleb128
10608 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10609 ? siz : 0,
10610 NULL);
10611 #else
10612 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10613 "File Entry: %#x", (unsigned) i + 1);
10615 /* Include directory index. */
10616 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10618 /* Modification time. */
10619 dw2_asm_output_data_uleb128 (0, NULL);
10621 /* File length in bytes. */
10622 dw2_asm_output_data_uleb128 (0, NULL);
10623 #endif /* VMS_DEBUGGING_INFO */
10626 dw2_asm_output_data (1, 0, "End file name table");
10630 /* Output one line number table into the .debug_line section. */
10632 static void
10633 output_one_line_info_table (dw_line_info_table *table)
10635 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10636 unsigned int current_line = 1;
10637 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10638 dw_line_info_entry *ent;
10639 size_t i;
10641 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10643 switch (ent->opcode)
10645 case LI_set_address:
10646 /* ??? Unfortunately, we have little choice here currently, and
10647 must always use the most general form. GCC does not know the
10648 address delta itself, so we can't use DW_LNS_advance_pc. Many
10649 ports do have length attributes which will give an upper bound
10650 on the address range. We could perhaps use length attributes
10651 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10652 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10654 /* This can handle any delta. This takes
10655 4+DWARF2_ADDR_SIZE bytes. */
10656 dw2_asm_output_data (1, 0, "set address %s", line_label);
10657 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10658 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10659 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10660 break;
10662 case LI_set_line:
10663 if (ent->val == current_line)
10665 /* We still need to start a new row, so output a copy insn. */
10666 dw2_asm_output_data (1, DW_LNS_copy,
10667 "copy line %u", current_line);
10669 else
10671 int line_offset = ent->val - current_line;
10672 int line_delta = line_offset - DWARF_LINE_BASE;
10674 current_line = ent->val;
10675 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10677 /* This can handle deltas from -10 to 234, using the current
10678 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10679 This takes 1 byte. */
10680 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10681 "line %u", current_line);
10683 else
10685 /* This can handle any delta. This takes at least 4 bytes,
10686 depending on the value being encoded. */
10687 dw2_asm_output_data (1, DW_LNS_advance_line,
10688 "advance to line %u", current_line);
10689 dw2_asm_output_data_sleb128 (line_offset, NULL);
10690 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10693 break;
10695 case LI_set_file:
10696 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10697 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10698 break;
10700 case LI_set_column:
10701 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10702 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10703 break;
10705 case LI_negate_stmt:
10706 current_is_stmt = !current_is_stmt;
10707 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10708 "is_stmt %d", current_is_stmt);
10709 break;
10711 case LI_set_prologue_end:
10712 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10713 "set prologue end");
10714 break;
10716 case LI_set_epilogue_begin:
10717 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10718 "set epilogue begin");
10719 break;
10721 case LI_set_discriminator:
10722 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10723 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10724 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10725 dw2_asm_output_data_uleb128 (ent->val, NULL);
10726 break;
10730 /* Emit debug info for the address of the end of the table. */
10731 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10732 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10733 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10734 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10736 dw2_asm_output_data (1, 0, "end sequence");
10737 dw2_asm_output_data_uleb128 (1, NULL);
10738 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10741 /* Output the source line number correspondence information. This
10742 information goes into the .debug_line section. */
10744 static void
10745 output_line_info (bool prologue_only)
10747 static unsigned int generation;
10748 char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
10749 char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
10750 /* We don't support DWARFv5 line tables yet. */
10751 int ver = dwarf_version < 5 ? dwarf_version : 4;
10752 bool saw_one = false;
10753 int opc;
10755 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
10756 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
10757 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
10758 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
10760 if (!XCOFF_DEBUGGING_INFO)
10762 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10763 dw2_asm_output_data (4, 0xffffffff,
10764 "Initial length escape value indicating 64-bit DWARF extension");
10765 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10766 "Length of Source Line Info");
10769 ASM_OUTPUT_LABEL (asm_out_file, l1);
10771 dw2_asm_output_data (2, ver, "DWARF Version");
10772 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10773 ASM_OUTPUT_LABEL (asm_out_file, p1);
10775 /* Define the architecture-dependent minimum instruction length (in bytes).
10776 In this implementation of DWARF, this field is used for information
10777 purposes only. Since GCC generates assembly language, we have no
10778 a priori knowledge of how many instruction bytes are generated for each
10779 source line, and therefore can use only the DW_LNE_set_address and
10780 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10781 this as '1', which is "correct enough" for all architectures,
10782 and don't let the target override. */
10783 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10785 if (ver >= 4)
10786 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10787 "Maximum Operations Per Instruction");
10788 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10789 "Default is_stmt_start flag");
10790 dw2_asm_output_data (1, DWARF_LINE_BASE,
10791 "Line Base Value (Special Opcodes)");
10792 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10793 "Line Range Value (Special Opcodes)");
10794 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10795 "Special Opcode Base");
10797 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10799 int n_op_args;
10800 switch (opc)
10802 case DW_LNS_advance_pc:
10803 case DW_LNS_advance_line:
10804 case DW_LNS_set_file:
10805 case DW_LNS_set_column:
10806 case DW_LNS_fixed_advance_pc:
10807 case DW_LNS_set_isa:
10808 n_op_args = 1;
10809 break;
10810 default:
10811 n_op_args = 0;
10812 break;
10815 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10816 opc, n_op_args);
10819 /* Write out the information about the files we use. */
10820 output_file_names ();
10821 ASM_OUTPUT_LABEL (asm_out_file, p2);
10822 if (prologue_only)
10824 /* Output the marker for the end of the line number info. */
10825 ASM_OUTPUT_LABEL (asm_out_file, l2);
10826 return;
10829 if (separate_line_info)
10831 dw_line_info_table *table;
10832 size_t i;
10834 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10835 if (table->in_use)
10837 output_one_line_info_table (table);
10838 saw_one = true;
10841 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10843 output_one_line_info_table (cold_text_section_line_info);
10844 saw_one = true;
10847 /* ??? Some Darwin linkers crash on a .debug_line section with no
10848 sequences. Further, merely a DW_LNE_end_sequence entry is not
10849 sufficient -- the address column must also be initialized.
10850 Make sure to output at least one set_address/end_sequence pair,
10851 choosing .text since that section is always present. */
10852 if (text_section_line_info->in_use || !saw_one)
10853 output_one_line_info_table (text_section_line_info);
10855 /* Output the marker for the end of the line number info. */
10856 ASM_OUTPUT_LABEL (asm_out_file, l2);
10859 /* Return true if DW_AT_endianity should be emitted according to REVERSE. */
10861 static inline bool
10862 need_endianity_attribute_p (bool reverse)
10864 return reverse && (dwarf_version >= 3 || !dwarf_strict);
10867 /* Given a pointer to a tree node for some base type, return a pointer to
10868 a DIE that describes the given type. REVERSE is true if the type is
10869 to be interpreted in the reverse storage order wrt the target order.
10871 This routine must only be called for GCC type nodes that correspond to
10872 Dwarf base (fundamental) types. */
10874 static dw_die_ref
10875 base_type_die (tree type, bool reverse)
10877 dw_die_ref base_type_result;
10878 enum dwarf_type encoding;
10879 bool fpt_used = false;
10880 struct fixed_point_type_info fpt_info;
10881 tree type_bias = NULL_TREE;
10883 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10884 return 0;
10886 /* If this is a subtype that should not be emitted as a subrange type,
10887 use the base type. See subrange_type_for_debug_p. */
10888 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10889 type = TREE_TYPE (type);
10891 switch (TREE_CODE (type))
10893 case INTEGER_TYPE:
10894 if ((dwarf_version >= 4 || !dwarf_strict)
10895 && TYPE_NAME (type)
10896 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10897 && DECL_IS_BUILTIN (TYPE_NAME (type))
10898 && DECL_NAME (TYPE_NAME (type)))
10900 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10901 if (strcmp (name, "char16_t") == 0
10902 || strcmp (name, "char32_t") == 0)
10904 encoding = DW_ATE_UTF;
10905 break;
10908 if ((dwarf_version >= 3 || !dwarf_strict)
10909 && lang_hooks.types.get_fixed_point_type_info)
10911 memset (&fpt_info, 0, sizeof (fpt_info));
10912 if (lang_hooks.types.get_fixed_point_type_info (type, &fpt_info))
10914 fpt_used = true;
10915 encoding = ((TYPE_UNSIGNED (type))
10916 ? DW_ATE_unsigned_fixed
10917 : DW_ATE_signed_fixed);
10918 break;
10921 if (TYPE_STRING_FLAG (type))
10923 if (TYPE_UNSIGNED (type))
10924 encoding = DW_ATE_unsigned_char;
10925 else
10926 encoding = DW_ATE_signed_char;
10928 else if (TYPE_UNSIGNED (type))
10929 encoding = DW_ATE_unsigned;
10930 else
10931 encoding = DW_ATE_signed;
10933 if (!dwarf_strict
10934 && lang_hooks.types.get_type_bias)
10935 type_bias = lang_hooks.types.get_type_bias (type);
10936 break;
10938 case REAL_TYPE:
10939 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10941 if (dwarf_version >= 3 || !dwarf_strict)
10942 encoding = DW_ATE_decimal_float;
10943 else
10944 encoding = DW_ATE_lo_user;
10946 else
10947 encoding = DW_ATE_float;
10948 break;
10950 case FIXED_POINT_TYPE:
10951 if (!(dwarf_version >= 3 || !dwarf_strict))
10952 encoding = DW_ATE_lo_user;
10953 else if (TYPE_UNSIGNED (type))
10954 encoding = DW_ATE_unsigned_fixed;
10955 else
10956 encoding = DW_ATE_signed_fixed;
10957 break;
10959 /* Dwarf2 doesn't know anything about complex ints, so use
10960 a user defined type for it. */
10961 case COMPLEX_TYPE:
10962 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10963 encoding = DW_ATE_complex_float;
10964 else
10965 encoding = DW_ATE_lo_user;
10966 break;
10968 case BOOLEAN_TYPE:
10969 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10970 encoding = DW_ATE_boolean;
10971 break;
10973 default:
10974 /* No other TREE_CODEs are Dwarf fundamental types. */
10975 gcc_unreachable ();
10978 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10980 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10981 int_size_in_bytes (type));
10982 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10984 if (need_endianity_attribute_p (reverse))
10985 add_AT_unsigned (base_type_result, DW_AT_endianity,
10986 BYTES_BIG_ENDIAN ? DW_END_little : DW_END_big);
10988 if (fpt_used)
10990 switch (fpt_info.scale_factor_kind)
10992 case fixed_point_scale_factor_binary:
10993 add_AT_int (base_type_result, DW_AT_binary_scale,
10994 fpt_info.scale_factor.binary);
10995 break;
10997 case fixed_point_scale_factor_decimal:
10998 add_AT_int (base_type_result, DW_AT_decimal_scale,
10999 fpt_info.scale_factor.decimal);
11000 break;
11002 case fixed_point_scale_factor_arbitrary:
11003 /* Arbitrary scale factors cannot be described in standard DWARF,
11004 yet. */
11005 if (!dwarf_strict)
11007 /* Describe the scale factor as a rational constant. */
11008 const dw_die_ref scale_factor
11009 = new_die (DW_TAG_constant, comp_unit_die (), type);
11011 add_AT_unsigned (scale_factor, DW_AT_GNU_numerator,
11012 fpt_info.scale_factor.arbitrary.numerator);
11013 add_AT_int (scale_factor, DW_AT_GNU_denominator,
11014 fpt_info.scale_factor.arbitrary.denominator);
11016 add_AT_die_ref (base_type_result, DW_AT_small, scale_factor);
11018 break;
11020 default:
11021 gcc_unreachable ();
11025 if (type_bias)
11026 add_scalar_info (base_type_result, DW_AT_GNU_bias, type_bias,
11027 dw_scalar_form_constant
11028 | dw_scalar_form_exprloc
11029 | dw_scalar_form_reference,
11030 NULL);
11032 add_pubtype (type, base_type_result);
11034 return base_type_result;
11037 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
11038 named 'auto' in its type: return true for it, false otherwise. */
11040 static inline bool
11041 is_cxx_auto (tree type)
11043 if (is_cxx ())
11045 tree name = TYPE_IDENTIFIER (type);
11046 if (name == get_identifier ("auto")
11047 || name == get_identifier ("decltype(auto)"))
11048 return true;
11050 return false;
11053 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11054 given input type is a Dwarf "fundamental" type. Otherwise return null. */
11056 static inline int
11057 is_base_type (tree type)
11059 switch (TREE_CODE (type))
11061 case ERROR_MARK:
11062 case VOID_TYPE:
11063 case INTEGER_TYPE:
11064 case REAL_TYPE:
11065 case FIXED_POINT_TYPE:
11066 case COMPLEX_TYPE:
11067 case BOOLEAN_TYPE:
11068 case POINTER_BOUNDS_TYPE:
11069 return 1;
11071 case ARRAY_TYPE:
11072 case RECORD_TYPE:
11073 case UNION_TYPE:
11074 case QUAL_UNION_TYPE:
11075 case ENUMERAL_TYPE:
11076 case FUNCTION_TYPE:
11077 case METHOD_TYPE:
11078 case POINTER_TYPE:
11079 case REFERENCE_TYPE:
11080 case NULLPTR_TYPE:
11081 case OFFSET_TYPE:
11082 case LANG_TYPE:
11083 case VECTOR_TYPE:
11084 return 0;
11086 default:
11087 if (is_cxx_auto (type))
11088 return 0;
11089 gcc_unreachable ();
11092 return 0;
11095 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11096 node, return the size in bits for the type if it is a constant, or else
11097 return the alignment for the type if the type's size is not constant, or
11098 else return BITS_PER_WORD if the type actually turns out to be an
11099 ERROR_MARK node. */
11101 static inline unsigned HOST_WIDE_INT
11102 simple_type_size_in_bits (const_tree type)
11104 if (TREE_CODE (type) == ERROR_MARK)
11105 return BITS_PER_WORD;
11106 else if (TYPE_SIZE (type) == NULL_TREE)
11107 return 0;
11108 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
11109 return tree_to_uhwi (TYPE_SIZE (type));
11110 else
11111 return TYPE_ALIGN (type);
11114 /* Similarly, but return an offset_int instead of UHWI. */
11116 static inline offset_int
11117 offset_int_type_size_in_bits (const_tree type)
11119 if (TREE_CODE (type) == ERROR_MARK)
11120 return BITS_PER_WORD;
11121 else if (TYPE_SIZE (type) == NULL_TREE)
11122 return 0;
11123 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
11124 return wi::to_offset (TYPE_SIZE (type));
11125 else
11126 return TYPE_ALIGN (type);
11129 /* Given a pointer to a tree node for a subrange type, return a pointer
11130 to a DIE that describes the given type. */
11132 static dw_die_ref
11133 subrange_type_die (tree type, tree low, tree high, tree bias,
11134 dw_die_ref context_die)
11136 dw_die_ref subrange_die;
11137 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11139 if (context_die == NULL)
11140 context_die = comp_unit_die ();
11142 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11144 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11146 /* The size of the subrange type and its base type do not match,
11147 so we need to generate a size attribute for the subrange type. */
11148 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11151 if (low)
11152 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
11153 if (high)
11154 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
11155 if (bias && !dwarf_strict)
11156 add_scalar_info (subrange_die, DW_AT_GNU_bias, bias,
11157 dw_scalar_form_constant
11158 | dw_scalar_form_exprloc
11159 | dw_scalar_form_reference,
11160 NULL);
11162 return subrange_die;
11165 /* Returns the (const and/or volatile) cv_qualifiers associated with
11166 the decl node. This will normally be augmented with the
11167 cv_qualifiers of the underlying type in add_type_attribute. */
11169 static int
11170 decl_quals (const_tree decl)
11172 return ((TREE_READONLY (decl)
11173 /* The C++ front-end correctly marks reference-typed
11174 variables as readonly, but from a language (and debug
11175 info) standpoint they are not const-qualified. */
11176 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
11177 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
11178 | (TREE_THIS_VOLATILE (decl)
11179 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
11182 /* Determine the TYPE whose qualifiers match the largest strict subset
11183 of the given TYPE_QUALS, and return its qualifiers. Ignore all
11184 qualifiers outside QUAL_MASK. */
11186 static int
11187 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
11189 tree t;
11190 int best_rank = 0, best_qual = 0, max_rank;
11192 type_quals &= qual_mask;
11193 max_rank = popcount_hwi (type_quals) - 1;
11195 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
11196 t = TYPE_NEXT_VARIANT (t))
11198 int q = TYPE_QUALS (t) & qual_mask;
11200 if ((q & type_quals) == q && q != type_quals
11201 && check_base_type (t, type))
11203 int rank = popcount_hwi (q);
11205 if (rank > best_rank)
11207 best_rank = rank;
11208 best_qual = q;
11213 return best_qual;
11216 struct dwarf_qual_info_t { int q; enum dwarf_tag t; };
11217 static const dwarf_qual_info_t dwarf_qual_info[] =
11219 { TYPE_QUAL_CONST, DW_TAG_const_type },
11220 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
11221 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
11222 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type }
11224 static const unsigned int dwarf_qual_info_size
11225 = sizeof (dwarf_qual_info) / sizeof (dwarf_qual_info[0]);
11227 /* If DIE is a qualified DIE of some base DIE with the same parent,
11228 return the base DIE, otherwise return NULL. Set MASK to the
11229 qualifiers added compared to the returned DIE. */
11231 static dw_die_ref
11232 qualified_die_p (dw_die_ref die, int *mask, unsigned int depth)
11234 unsigned int i;
11235 for (i = 0; i < dwarf_qual_info_size; i++)
11236 if (die->die_tag == dwarf_qual_info[i].t)
11237 break;
11238 if (i == dwarf_qual_info_size)
11239 return NULL;
11240 if (vec_safe_length (die->die_attr) != 1)
11241 return NULL;
11242 dw_die_ref type = get_AT_ref (die, DW_AT_type);
11243 if (type == NULL || type->die_parent != die->die_parent)
11244 return NULL;
11245 *mask |= dwarf_qual_info[i].q;
11246 if (depth)
11248 dw_die_ref ret = qualified_die_p (type, mask, depth - 1);
11249 if (ret)
11250 return ret;
11252 return type;
11255 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11256 entry that chains the modifiers specified by CV_QUALS in front of the
11257 given type. REVERSE is true if the type is to be interpreted in the
11258 reverse storage order wrt the target order. */
11260 static dw_die_ref
11261 modified_type_die (tree type, int cv_quals, bool reverse,
11262 dw_die_ref context_die)
11264 enum tree_code code = TREE_CODE (type);
11265 dw_die_ref mod_type_die;
11266 dw_die_ref sub_die = NULL;
11267 tree item_type = NULL;
11268 tree qualified_type;
11269 tree name, low, high;
11270 dw_die_ref mod_scope;
11271 /* Only these cv-qualifiers are currently handled. */
11272 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
11273 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
11275 if (code == ERROR_MARK)
11276 return NULL;
11278 if (lang_hooks.types.get_debug_type)
11280 tree debug_type = lang_hooks.types.get_debug_type (type);
11282 if (debug_type != NULL_TREE && debug_type != type)
11283 return modified_type_die (debug_type, cv_quals, reverse, context_die);
11286 cv_quals &= cv_qual_mask;
11288 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
11289 tag modifier (and not an attribute) old consumers won't be able
11290 to handle it. */
11291 if (dwarf_version < 3)
11292 cv_quals &= ~TYPE_QUAL_RESTRICT;
11294 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
11295 if (dwarf_version < 5)
11296 cv_quals &= ~TYPE_QUAL_ATOMIC;
11298 /* See if we already have the appropriately qualified variant of
11299 this type. */
11300 qualified_type = get_qualified_type (type, cv_quals);
11302 if (qualified_type == sizetype
11303 && TYPE_NAME (qualified_type)
11304 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
11306 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
11308 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
11309 && TYPE_PRECISION (t)
11310 == TYPE_PRECISION (qualified_type)
11311 && TYPE_UNSIGNED (t)
11312 == TYPE_UNSIGNED (qualified_type));
11313 qualified_type = t;
11316 /* If we do, then we can just use its DIE, if it exists. */
11317 if (qualified_type)
11319 mod_type_die = lookup_type_die (qualified_type);
11321 /* DW_AT_endianity doesn't come from a qualifier on the type. */
11322 if (mod_type_die
11323 && (!need_endianity_attribute_p (reverse)
11324 || !is_base_type (type)
11325 || get_AT_unsigned (mod_type_die, DW_AT_endianity)))
11326 return mod_type_die;
11329 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11331 /* Handle C typedef types. */
11332 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
11333 && !DECL_ARTIFICIAL (name))
11335 tree dtype = TREE_TYPE (name);
11337 if (qualified_type == dtype)
11339 /* For a named type, use the typedef. */
11340 gen_type_die (qualified_type, context_die);
11341 return lookup_type_die (qualified_type);
11343 else
11345 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
11346 dquals &= cv_qual_mask;
11347 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
11348 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
11349 /* cv-unqualified version of named type. Just use
11350 the unnamed type to which it refers. */
11351 return modified_type_die (DECL_ORIGINAL_TYPE (name), cv_quals,
11352 reverse, context_die);
11353 /* Else cv-qualified version of named type; fall through. */
11357 mod_scope = scope_die_for (type, context_die);
11359 if (cv_quals)
11361 int sub_quals = 0, first_quals = 0;
11362 unsigned i;
11363 dw_die_ref first = NULL, last = NULL;
11365 /* Determine a lesser qualified type that most closely matches
11366 this one. Then generate DW_TAG_* entries for the remaining
11367 qualifiers. */
11368 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
11369 cv_qual_mask);
11370 if (sub_quals && use_debug_types)
11372 bool needed = false;
11373 /* If emitting type units, make sure the order of qualifiers
11374 is canonical. Thus, start from unqualified type if
11375 an earlier qualifier is missing in sub_quals, but some later
11376 one is present there. */
11377 for (i = 0; i < dwarf_qual_info_size; i++)
11378 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11379 needed = true;
11380 else if (needed && (dwarf_qual_info[i].q & cv_quals))
11382 sub_quals = 0;
11383 break;
11386 mod_type_die = modified_type_die (type, sub_quals, reverse, context_die);
11387 if (mod_scope && mod_type_die && mod_type_die->die_parent == mod_scope)
11389 /* As not all intermediate qualified DIEs have corresponding
11390 tree types, ensure that qualified DIEs in the same scope
11391 as their DW_AT_type are emitted after their DW_AT_type,
11392 only with other qualified DIEs for the same type possibly
11393 in between them. Determine the range of such qualified
11394 DIEs now (first being the base type, last being corresponding
11395 last qualified DIE for it). */
11396 unsigned int count = 0;
11397 first = qualified_die_p (mod_type_die, &first_quals,
11398 dwarf_qual_info_size);
11399 if (first == NULL)
11400 first = mod_type_die;
11401 gcc_assert ((first_quals & ~sub_quals) == 0);
11402 for (count = 0, last = first;
11403 count < (1U << dwarf_qual_info_size);
11404 count++, last = last->die_sib)
11406 int quals = 0;
11407 if (last == mod_scope->die_child)
11408 break;
11409 if (qualified_die_p (last->die_sib, &quals, dwarf_qual_info_size)
11410 != first)
11411 break;
11415 for (i = 0; i < dwarf_qual_info_size; i++)
11416 if (dwarf_qual_info[i].q & cv_quals & ~sub_quals)
11418 dw_die_ref d;
11419 if (first && first != last)
11421 for (d = first->die_sib; ; d = d->die_sib)
11423 int quals = 0;
11424 qualified_die_p (d, &quals, dwarf_qual_info_size);
11425 if (quals == (first_quals | dwarf_qual_info[i].q))
11426 break;
11427 if (d == last)
11429 d = NULL;
11430 break;
11433 if (d)
11435 mod_type_die = d;
11436 continue;
11439 if (first)
11441 d = ggc_cleared_alloc<die_node> ();
11442 d->die_tag = dwarf_qual_info[i].t;
11443 add_child_die_after (mod_scope, d, last);
11444 last = d;
11446 else
11447 d = new_die (dwarf_qual_info[i].t, mod_scope, type);
11448 if (mod_type_die)
11449 add_AT_die_ref (d, DW_AT_type, mod_type_die);
11450 mod_type_die = d;
11451 first_quals |= dwarf_qual_info[i].q;
11454 else if (code == POINTER_TYPE || code == REFERENCE_TYPE)
11456 dwarf_tag tag = DW_TAG_pointer_type;
11457 if (code == REFERENCE_TYPE)
11459 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
11460 tag = DW_TAG_rvalue_reference_type;
11461 else
11462 tag = DW_TAG_reference_type;
11464 mod_type_die = new_die (tag, mod_scope, type);
11466 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11467 simple_type_size_in_bits (type) / BITS_PER_UNIT);
11468 item_type = TREE_TYPE (type);
11470 addr_space_t as = TYPE_ADDR_SPACE (item_type);
11471 if (!ADDR_SPACE_GENERIC_P (as))
11473 int action = targetm.addr_space.debug (as);
11474 if (action >= 0)
11476 /* Positive values indicate an address_class. */
11477 add_AT_unsigned (mod_type_die, DW_AT_address_class, action);
11479 else
11481 /* Negative values indicate an (inverted) segment base reg. */
11482 dw_loc_descr_ref d
11483 = one_reg_loc_descriptor (~action, VAR_INIT_STATUS_INITIALIZED);
11484 add_AT_loc (mod_type_die, DW_AT_segment, d);
11488 else if (code == INTEGER_TYPE
11489 && TREE_TYPE (type) != NULL_TREE
11490 && subrange_type_for_debug_p (type, &low, &high))
11492 tree bias = NULL_TREE;
11493 if (lang_hooks.types.get_type_bias)
11494 bias = lang_hooks.types.get_type_bias (type);
11495 mod_type_die = subrange_type_die (type, low, high, bias, context_die);
11496 item_type = TREE_TYPE (type);
11498 else if (is_base_type (type))
11499 mod_type_die = base_type_die (type, reverse);
11500 else
11502 gen_type_die (type, context_die);
11504 /* We have to get the type_main_variant here (and pass that to the
11505 `lookup_type_die' routine) because the ..._TYPE node we have
11506 might simply be a *copy* of some original type node (where the
11507 copy was created to help us keep track of typedef names) and
11508 that copy might have a different TYPE_UID from the original
11509 ..._TYPE node. */
11510 if (TREE_CODE (type) != VECTOR_TYPE
11511 && TREE_CODE (type) != ARRAY_TYPE)
11512 return lookup_type_die (type_main_variant (type));
11513 else
11514 /* Vectors have the debugging information in the type,
11515 not the main variant. */
11516 return lookup_type_die (type);
11519 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
11520 don't output a DW_TAG_typedef, since there isn't one in the
11521 user's program; just attach a DW_AT_name to the type.
11522 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11523 if the base type already has the same name. */
11524 if (name
11525 && ((TREE_CODE (name) != TYPE_DECL
11526 && (qualified_type == TYPE_MAIN_VARIANT (type)
11527 || (cv_quals == TYPE_UNQUALIFIED)))
11528 || (TREE_CODE (name) == TYPE_DECL
11529 && TREE_TYPE (name) == qualified_type
11530 && DECL_NAME (name))))
11532 if (TREE_CODE (name) == TYPE_DECL)
11533 /* Could just call add_name_and_src_coords_attributes here,
11534 but since this is a builtin type it doesn't have any
11535 useful source coordinates anyway. */
11536 name = DECL_NAME (name);
11537 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11539 /* This probably indicates a bug. */
11540 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
11542 name = TYPE_IDENTIFIER (type);
11543 add_name_attribute (mod_type_die,
11544 name ? IDENTIFIER_POINTER (name) : "__unknown__");
11547 if (qualified_type)
11548 equate_type_number_to_die (qualified_type, mod_type_die);
11550 if (item_type)
11551 /* We must do this after the equate_type_number_to_die call, in case
11552 this is a recursive type. This ensures that the modified_type_die
11553 recursion will terminate even if the type is recursive. Recursive
11554 types are possible in Ada. */
11555 sub_die = modified_type_die (item_type,
11556 TYPE_QUALS_NO_ADDR_SPACE (item_type),
11557 reverse,
11558 context_die);
11560 if (sub_die != NULL)
11561 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
11563 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
11564 if (TYPE_ARTIFICIAL (type))
11565 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
11567 return mod_type_die;
11570 /* Generate DIEs for the generic parameters of T.
11571 T must be either a generic type or a generic function.
11572 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
11574 static void
11575 gen_generic_params_dies (tree t)
11577 tree parms, args;
11578 int parms_num, i;
11579 dw_die_ref die = NULL;
11580 int non_default;
11582 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
11583 return;
11585 if (TYPE_P (t))
11586 die = lookup_type_die (t);
11587 else if (DECL_P (t))
11588 die = lookup_decl_die (t);
11590 gcc_assert (die);
11592 parms = lang_hooks.get_innermost_generic_parms (t);
11593 if (!parms)
11594 /* T has no generic parameter. It means T is neither a generic type
11595 or function. End of story. */
11596 return;
11598 parms_num = TREE_VEC_LENGTH (parms);
11599 args = lang_hooks.get_innermost_generic_args (t);
11600 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
11601 non_default = int_cst_value (TREE_CHAIN (args));
11602 else
11603 non_default = TREE_VEC_LENGTH (args);
11604 for (i = 0; i < parms_num; i++)
11606 tree parm, arg, arg_pack_elems;
11607 dw_die_ref parm_die;
11609 parm = TREE_VEC_ELT (parms, i);
11610 arg = TREE_VEC_ELT (args, i);
11611 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11612 gcc_assert (parm && TREE_VALUE (parm) && arg);
11614 if (parm && TREE_VALUE (parm) && arg)
11616 /* If PARM represents a template parameter pack,
11617 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11618 by DW_TAG_template_*_parameter DIEs for the argument
11619 pack elements of ARG. Note that ARG would then be
11620 an argument pack. */
11621 if (arg_pack_elems)
11622 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11623 arg_pack_elems,
11624 die);
11625 else
11626 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11627 true /* emit name */, die);
11628 if (i >= non_default)
11629 add_AT_flag (parm_die, DW_AT_default_value, 1);
11634 /* Create and return a DIE for PARM which should be
11635 the representation of a generic type parameter.
11636 For instance, in the C++ front end, PARM would be a template parameter.
11637 ARG is the argument to PARM.
11638 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11639 name of the PARM.
11640 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11641 as a child node. */
11643 static dw_die_ref
11644 generic_parameter_die (tree parm, tree arg,
11645 bool emit_name_p,
11646 dw_die_ref parent_die)
11648 dw_die_ref tmpl_die = NULL;
11649 const char *name = NULL;
11651 if (!parm || !DECL_NAME (parm) || !arg)
11652 return NULL;
11654 /* We support non-type generic parameters and arguments,
11655 type generic parameters and arguments, as well as
11656 generic generic parameters (a.k.a. template template parameters in C++)
11657 and arguments. */
11658 if (TREE_CODE (parm) == PARM_DECL)
11659 /* PARM is a nontype generic parameter */
11660 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11661 else if (TREE_CODE (parm) == TYPE_DECL)
11662 /* PARM is a type generic parameter. */
11663 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11664 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11665 /* PARM is a generic generic parameter.
11666 Its DIE is a GNU extension. It shall have a
11667 DW_AT_name attribute to represent the name of the template template
11668 parameter, and a DW_AT_GNU_template_name attribute to represent the
11669 name of the template template argument. */
11670 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11671 parent_die, parm);
11672 else
11673 gcc_unreachable ();
11675 if (tmpl_die)
11677 tree tmpl_type;
11679 /* If PARM is a generic parameter pack, it means we are
11680 emitting debug info for a template argument pack element.
11681 In other terms, ARG is a template argument pack element.
11682 In that case, we don't emit any DW_AT_name attribute for
11683 the die. */
11684 if (emit_name_p)
11686 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11687 gcc_assert (name);
11688 add_AT_string (tmpl_die, DW_AT_name, name);
11691 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11693 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11694 TMPL_DIE should have a child DW_AT_type attribute that is set
11695 to the type of the argument to PARM, which is ARG.
11696 If PARM is a type generic parameter, TMPL_DIE should have a
11697 child DW_AT_type that is set to ARG. */
11698 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11699 add_type_attribute (tmpl_die, tmpl_type,
11700 (TREE_THIS_VOLATILE (tmpl_type)
11701 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11702 false, parent_die);
11704 else
11706 /* So TMPL_DIE is a DIE representing a
11707 a generic generic template parameter, a.k.a template template
11708 parameter in C++ and arg is a template. */
11710 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11711 to the name of the argument. */
11712 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11713 if (name)
11714 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11717 if (TREE_CODE (parm) == PARM_DECL)
11718 /* So PARM is a non-type generic parameter.
11719 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11720 attribute of TMPL_DIE which value represents the value
11721 of ARG.
11722 We must be careful here:
11723 The value of ARG might reference some function decls.
11724 We might currently be emitting debug info for a generic
11725 type and types are emitted before function decls, we don't
11726 know if the function decls referenced by ARG will actually be
11727 emitted after cgraph computations.
11728 So must defer the generation of the DW_AT_const_value to
11729 after cgraph is ready. */
11730 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11733 return tmpl_die;
11736 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11737 PARM_PACK must be a template parameter pack. The returned DIE
11738 will be child DIE of PARENT_DIE. */
11740 static dw_die_ref
11741 template_parameter_pack_die (tree parm_pack,
11742 tree parm_pack_args,
11743 dw_die_ref parent_die)
11745 dw_die_ref die;
11746 int j;
11748 gcc_assert (parent_die && parm_pack);
11750 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11751 add_name_and_src_coords_attributes (die, parm_pack);
11752 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11753 generic_parameter_die (parm_pack,
11754 TREE_VEC_ELT (parm_pack_args, j),
11755 false /* Don't emit DW_AT_name */,
11756 die);
11757 return die;
11760 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11761 an enumerated type. */
11763 static inline int
11764 type_is_enum (const_tree type)
11766 return TREE_CODE (type) == ENUMERAL_TYPE;
11769 /* Return the DBX register number described by a given RTL node. */
11771 static unsigned int
11772 dbx_reg_number (const_rtx rtl)
11774 unsigned regno = REGNO (rtl);
11776 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11778 #ifdef LEAF_REG_REMAP
11779 if (crtl->uses_only_leaf_regs)
11781 int leaf_reg = LEAF_REG_REMAP (regno);
11782 if (leaf_reg != -1)
11783 regno = (unsigned) leaf_reg;
11785 #endif
11787 regno = DBX_REGISTER_NUMBER (regno);
11788 gcc_assert (regno != INVALID_REGNUM);
11789 return regno;
11792 /* Optionally add a DW_OP_piece term to a location description expression.
11793 DW_OP_piece is only added if the location description expression already
11794 doesn't end with DW_OP_piece. */
11796 static void
11797 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11799 dw_loc_descr_ref loc;
11801 if (*list_head != NULL)
11803 /* Find the end of the chain. */
11804 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11807 if (loc->dw_loc_opc != DW_OP_piece)
11808 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11812 /* Return a location descriptor that designates a machine register or
11813 zero if there is none. */
11815 static dw_loc_descr_ref
11816 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11818 rtx regs;
11820 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11821 return 0;
11823 /* We only use "frame base" when we're sure we're talking about the
11824 post-prologue local stack frame. We do this by *not* running
11825 register elimination until this point, and recognizing the special
11826 argument pointer and soft frame pointer rtx's.
11827 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11828 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11829 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11831 dw_loc_descr_ref result = NULL;
11833 if (dwarf_version >= 4 || !dwarf_strict)
11835 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11836 initialized);
11837 if (result)
11838 add_loc_descr (&result,
11839 new_loc_descr (DW_OP_stack_value, 0, 0));
11841 return result;
11844 regs = targetm.dwarf_register_span (rtl);
11846 if (REG_NREGS (rtl) > 1 || regs)
11847 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11848 else
11850 unsigned int dbx_regnum = dbx_reg_number (rtl);
11851 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11852 return 0;
11853 return one_reg_loc_descriptor (dbx_regnum, initialized);
11857 /* Return a location descriptor that designates a machine register for
11858 a given hard register number. */
11860 static dw_loc_descr_ref
11861 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11863 dw_loc_descr_ref reg_loc_descr;
11865 if (regno <= 31)
11866 reg_loc_descr
11867 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11868 else
11869 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11871 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11872 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11874 return reg_loc_descr;
11877 /* Given an RTL of a register, return a location descriptor that
11878 designates a value that spans more than one register. */
11880 static dw_loc_descr_ref
11881 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11882 enum var_init_status initialized)
11884 int size, i;
11885 dw_loc_descr_ref loc_result = NULL;
11887 /* Simple, contiguous registers. */
11888 if (regs == NULL_RTX)
11890 unsigned reg = REGNO (rtl);
11891 int nregs;
11893 #ifdef LEAF_REG_REMAP
11894 if (crtl->uses_only_leaf_regs)
11896 int leaf_reg = LEAF_REG_REMAP (reg);
11897 if (leaf_reg != -1)
11898 reg = (unsigned) leaf_reg;
11900 #endif
11902 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11903 nregs = REG_NREGS (rtl);
11905 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11907 loc_result = NULL;
11908 while (nregs--)
11910 dw_loc_descr_ref t;
11912 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11913 VAR_INIT_STATUS_INITIALIZED);
11914 add_loc_descr (&loc_result, t);
11915 add_loc_descr_op_piece (&loc_result, size);
11916 ++reg;
11918 return loc_result;
11921 /* Now onto stupid register sets in non contiguous locations. */
11923 gcc_assert (GET_CODE (regs) == PARALLEL);
11925 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11926 loc_result = NULL;
11928 for (i = 0; i < XVECLEN (regs, 0); ++i)
11930 dw_loc_descr_ref t;
11932 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11933 VAR_INIT_STATUS_INITIALIZED);
11934 add_loc_descr (&loc_result, t);
11935 add_loc_descr_op_piece (&loc_result, size);
11938 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11939 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11940 return loc_result;
11943 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11945 /* Return a location descriptor that designates a constant i,
11946 as a compound operation from constant (i >> shift), constant shift
11947 and DW_OP_shl. */
11949 static dw_loc_descr_ref
11950 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11952 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11953 add_loc_descr (&ret, int_loc_descriptor (shift));
11954 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11955 return ret;
11958 /* Return a location descriptor that designates a constant. */
11960 static dw_loc_descr_ref
11961 int_loc_descriptor (HOST_WIDE_INT i)
11963 enum dwarf_location_atom op;
11965 /* Pick the smallest representation of a constant, rather than just
11966 defaulting to the LEB encoding. */
11967 if (i >= 0)
11969 int clz = clz_hwi (i);
11970 int ctz = ctz_hwi (i);
11971 if (i <= 31)
11972 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11973 else if (i <= 0xff)
11974 op = DW_OP_const1u;
11975 else if (i <= 0xffff)
11976 op = DW_OP_const2u;
11977 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11978 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11979 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11980 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11981 while DW_OP_const4u is 5 bytes. */
11982 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11983 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11984 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11985 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11986 while DW_OP_const4u is 5 bytes. */
11987 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11989 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
11990 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
11991 <= 4)
11993 /* As i >= 2**31, the double cast above will yield a negative number.
11994 Since wrapping is defined in DWARF expressions we can output big
11995 positive integers as small negative ones, regardless of the size
11996 of host wide ints.
11998 Here, since the evaluator will handle 32-bit values and since i >=
11999 2**31, we know it's going to be interpreted as a negative literal:
12000 store it this way if we can do better than 5 bytes this way. */
12001 return int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
12003 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12004 op = DW_OP_const4u;
12006 /* Past this point, i >= 0x100000000 and thus DW_OP_constu will take at
12007 least 6 bytes: see if we can do better before falling back to it. */
12008 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12009 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12010 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes. */
12011 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
12012 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12013 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
12014 >= HOST_BITS_PER_WIDE_INT)
12015 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
12016 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes. */
12017 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
12018 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12019 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12020 && size_of_uleb128 (i) > 6)
12021 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
12022 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
12023 else
12024 op = DW_OP_constu;
12026 else
12028 if (i >= -0x80)
12029 op = DW_OP_const1s;
12030 else if (i >= -0x8000)
12031 op = DW_OP_const2s;
12032 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12034 if (size_of_int_loc_descriptor (i) < 5)
12036 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12037 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12038 return ret;
12040 op = DW_OP_const4s;
12042 else
12044 if (size_of_int_loc_descriptor (i)
12045 < (unsigned long) 1 + size_of_sleb128 (i))
12047 dw_loc_descr_ref ret = int_loc_descriptor (-i);
12048 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12049 return ret;
12051 op = DW_OP_consts;
12055 return new_loc_descr (op, i, 0);
12058 /* Likewise, for unsigned constants. */
12060 static dw_loc_descr_ref
12061 uint_loc_descriptor (unsigned HOST_WIDE_INT i)
12063 const unsigned HOST_WIDE_INT max_int = INTTYPE_MAXIMUM (HOST_WIDE_INT);
12064 const unsigned HOST_WIDE_INT max_uint
12065 = INTTYPE_MAXIMUM (unsigned HOST_WIDE_INT);
12067 /* If possible, use the clever signed constants handling. */
12068 if (i <= max_int)
12069 return int_loc_descriptor ((HOST_WIDE_INT) i);
12071 /* Here, we are left with positive numbers that cannot be represented as
12072 HOST_WIDE_INT, i.e.:
12073 max (HOST_WIDE_INT) < i <= max (unsigned HOST_WIDE_INT)
12075 Using DW_OP_const4/8/./u operation to encode them consumes a lot of bytes
12076 whereas may be better to output a negative integer: thanks to integer
12077 wrapping, we know that:
12078 x = x - 2 ** DWARF2_ADDR_SIZE
12079 = x - 2 * (max (HOST_WIDE_INT) + 1)
12080 So numbers close to max (unsigned HOST_WIDE_INT) could be represented as
12081 small negative integers. Let's try that in cases it will clearly improve
12082 the encoding: there is no gain turning DW_OP_const4u into
12083 DW_OP_const4s. */
12084 if (DWARF2_ADDR_SIZE * 8 == HOST_BITS_PER_WIDE_INT
12085 && ((DWARF2_ADDR_SIZE == 4 && i > max_uint - 0x8000)
12086 || (DWARF2_ADDR_SIZE == 8 && i > max_uint - 0x80000000)))
12088 const unsigned HOST_WIDE_INT first_shift = i - max_int - 1;
12090 /* Now, -1 < first_shift <= max (HOST_WIDE_INT)
12091 i.e. 0 <= first_shift <= max (HOST_WIDE_INT). */
12092 const HOST_WIDE_INT second_shift
12093 = (HOST_WIDE_INT) first_shift - (HOST_WIDE_INT) max_int - 1;
12095 /* So we finally have:
12096 -max (HOST_WIDE_INT) - 1 <= second_shift <= -1.
12097 i.e. min (HOST_WIDE_INT) <= second_shift < 0. */
12098 return int_loc_descriptor (second_shift);
12101 /* Last chance: fallback to a simple constant operation. */
12102 return new_loc_descr
12103 ((HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12104 ? DW_OP_const4u
12105 : DW_OP_const8u,
12106 i, 0);
12109 /* Generate and return a location description that computes the unsigned
12110 comparison of the two stack top entries (a OP b where b is the top-most
12111 entry and a is the second one). The KIND of comparison can be LT_EXPR,
12112 LE_EXPR, GT_EXPR or GE_EXPR. */
12114 static dw_loc_descr_ref
12115 uint_comparison_loc_list (enum tree_code kind)
12117 enum dwarf_location_atom op, flip_op;
12118 dw_loc_descr_ref ret, bra_node, jmp_node, tmp;
12120 switch (kind)
12122 case LT_EXPR:
12123 op = DW_OP_lt;
12124 break;
12125 case LE_EXPR:
12126 op = DW_OP_le;
12127 break;
12128 case GT_EXPR:
12129 op = DW_OP_gt;
12130 break;
12131 case GE_EXPR:
12132 op = DW_OP_ge;
12133 break;
12134 default:
12135 gcc_unreachable ();
12138 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12139 jmp_node = new_loc_descr (DW_OP_skip, 0, 0);
12141 /* Until DWARFv4, operations all work on signed integers. It is nevertheless
12142 possible to perform unsigned comparisons: we just have to distinguish
12143 three cases:
12145 1. when a and b have the same sign (as signed integers); then we should
12146 return: a OP(signed) b;
12148 2. when a is a negative signed integer while b is a positive one, then a
12149 is a greater unsigned integer than b; likewise when a and b's roles
12150 are flipped.
12152 So first, compare the sign of the two operands. */
12153 ret = new_loc_descr (DW_OP_over, 0, 0);
12154 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12155 add_loc_descr (&ret, new_loc_descr (DW_OP_xor, 0, 0));
12156 /* If they have different signs (i.e. they have different sign bits), then
12157 the stack top value has now the sign bit set and thus it's smaller than
12158 zero. */
12159 add_loc_descr (&ret, new_loc_descr (DW_OP_lit0, 0, 0));
12160 add_loc_descr (&ret, new_loc_descr (DW_OP_lt, 0, 0));
12161 add_loc_descr (&ret, bra_node);
12163 /* We are in case 1. At this point, we know both operands have the same
12164 sign, to it's safe to use the built-in signed comparison. */
12165 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12166 add_loc_descr (&ret, jmp_node);
12168 /* We are in case 2. Here, we know both operands do not have the same sign,
12169 so we have to flip the signed comparison. */
12170 flip_op = (kind == LT_EXPR || kind == LE_EXPR) ? DW_OP_gt : DW_OP_lt;
12171 tmp = new_loc_descr (flip_op, 0, 0);
12172 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12173 bra_node->dw_loc_oprnd1.v.val_loc = tmp;
12174 add_loc_descr (&ret, tmp);
12176 /* This dummy operation is necessary to make the two branches join. */
12177 tmp = new_loc_descr (DW_OP_nop, 0, 0);
12178 jmp_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12179 jmp_node->dw_loc_oprnd1.v.val_loc = tmp;
12180 add_loc_descr (&ret, tmp);
12182 return ret;
12185 /* Likewise, but takes the location description lists (might be destructive on
12186 them). Return NULL if either is NULL or if concatenation fails. */
12188 static dw_loc_list_ref
12189 loc_list_from_uint_comparison (dw_loc_list_ref left, dw_loc_list_ref right,
12190 enum tree_code kind)
12192 if (left == NULL || right == NULL)
12193 return NULL;
12195 add_loc_list (&left, right);
12196 if (left == NULL)
12197 return NULL;
12199 add_loc_descr_to_each (left, uint_comparison_loc_list (kind));
12200 return left;
12203 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
12204 without actually allocating it. */
12206 static unsigned long
12207 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
12209 return size_of_int_loc_descriptor (i >> shift)
12210 + size_of_int_loc_descriptor (shift)
12211 + 1;
12214 /* Return size_of_locs (int_loc_descriptor (i)) without
12215 actually allocating it. */
12217 static unsigned long
12218 size_of_int_loc_descriptor (HOST_WIDE_INT i)
12220 unsigned long s;
12222 if (i >= 0)
12224 int clz, ctz;
12225 if (i <= 31)
12226 return 1;
12227 else if (i <= 0xff)
12228 return 2;
12229 else if (i <= 0xffff)
12230 return 3;
12231 clz = clz_hwi (i);
12232 ctz = ctz_hwi (i);
12233 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
12234 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
12235 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12236 - clz - 5);
12237 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12238 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
12239 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12240 - clz - 8);
12241 else if (DWARF2_ADDR_SIZE == 4 && i > 0x7fffffff
12242 && size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i)
12243 <= 4)
12244 return size_of_int_loc_descriptor ((HOST_WIDE_INT) (int32_t) i);
12245 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
12246 return 5;
12247 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12248 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
12249 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
12250 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12251 - clz - 8);
12252 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
12253 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
12254 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12255 - clz - 16);
12256 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
12257 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
12258 && s > 6)
12259 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
12260 - clz - 32);
12261 else
12262 return 1 + s;
12264 else
12266 if (i >= -0x80)
12267 return 2;
12268 else if (i >= -0x8000)
12269 return 3;
12270 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
12272 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12274 s = size_of_int_loc_descriptor (-i) + 1;
12275 if (s < 5)
12276 return s;
12278 return 5;
12280 else
12282 unsigned long r = 1 + size_of_sleb128 (i);
12283 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
12285 s = size_of_int_loc_descriptor (-i) + 1;
12286 if (s < r)
12287 return s;
12289 return r;
12294 /* Return loc description representing "address" of integer value.
12295 This can appear only as toplevel expression. */
12297 static dw_loc_descr_ref
12298 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12300 int litsize;
12301 dw_loc_descr_ref loc_result = NULL;
12303 if (!(dwarf_version >= 4 || !dwarf_strict))
12304 return NULL;
12306 litsize = size_of_int_loc_descriptor (i);
12307 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12308 is more compact. For DW_OP_stack_value we need:
12309 litsize + 1 (DW_OP_stack_value)
12310 and for DW_OP_implicit_value:
12311 1 (DW_OP_implicit_value) + 1 (length) + size. */
12312 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12314 loc_result = int_loc_descriptor (i);
12315 add_loc_descr (&loc_result,
12316 new_loc_descr (DW_OP_stack_value, 0, 0));
12317 return loc_result;
12320 loc_result = new_loc_descr (DW_OP_implicit_value,
12321 size, 0);
12322 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12323 loc_result->dw_loc_oprnd2.v.val_int = i;
12324 return loc_result;
12327 /* Return a location descriptor that designates a base+offset location. */
12329 static dw_loc_descr_ref
12330 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12331 enum var_init_status initialized)
12333 unsigned int regno;
12334 dw_loc_descr_ref result;
12335 dw_fde_ref fde = cfun->fde;
12337 /* We only use "frame base" when we're sure we're talking about the
12338 post-prologue local stack frame. We do this by *not* running
12339 register elimination until this point, and recognizing the special
12340 argument pointer and soft frame pointer rtx's. */
12341 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12343 rtx elim = (ira_use_lra_p
12344 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
12345 : eliminate_regs (reg, VOIDmode, NULL_RTX));
12347 if (elim != reg)
12349 if (GET_CODE (elim) == PLUS)
12351 offset += INTVAL (XEXP (elim, 1));
12352 elim = XEXP (elim, 0);
12354 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12355 && (elim == hard_frame_pointer_rtx
12356 || elim == stack_pointer_rtx))
12357 || elim == (frame_pointer_needed
12358 ? hard_frame_pointer_rtx
12359 : stack_pointer_rtx));
12361 /* If drap register is used to align stack, use frame
12362 pointer + offset to access stack variables. If stack
12363 is aligned without drap, use stack pointer + offset to
12364 access stack variables. */
12365 if (crtl->stack_realign_tried
12366 && reg == frame_pointer_rtx)
12368 int base_reg
12369 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12370 ? HARD_FRAME_POINTER_REGNUM
12371 : REGNO (elim));
12372 return new_reg_loc_descr (base_reg, offset);
12375 gcc_assert (frame_pointer_fb_offset_valid);
12376 offset += frame_pointer_fb_offset;
12377 return new_loc_descr (DW_OP_fbreg, offset, 0);
12381 regno = REGNO (reg);
12382 #ifdef LEAF_REG_REMAP
12383 if (crtl->uses_only_leaf_regs)
12385 int leaf_reg = LEAF_REG_REMAP (regno);
12386 if (leaf_reg != -1)
12387 regno = (unsigned) leaf_reg;
12389 #endif
12390 regno = DWARF_FRAME_REGNUM (regno);
12392 if (!optimize && fde
12393 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
12395 /* Use cfa+offset to represent the location of arguments passed
12396 on the stack when drap is used to align stack.
12397 Only do this when not optimizing, for optimized code var-tracking
12398 is supposed to track where the arguments live and the register
12399 used as vdrap or drap in some spot might be used for something
12400 else in other part of the routine. */
12401 return new_loc_descr (DW_OP_fbreg, offset, 0);
12404 if (regno <= 31)
12405 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12406 offset, 0);
12407 else
12408 result = new_loc_descr (DW_OP_bregx, regno, offset);
12410 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12411 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12413 return result;
12416 /* Return true if this RTL expression describes a base+offset calculation. */
12418 static inline int
12419 is_based_loc (const_rtx rtl)
12421 return (GET_CODE (rtl) == PLUS
12422 && ((REG_P (XEXP (rtl, 0))
12423 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12424 && CONST_INT_P (XEXP (rtl, 1)))));
12427 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12428 failed. */
12430 static dw_loc_descr_ref
12431 tls_mem_loc_descriptor (rtx mem)
12433 tree base;
12434 dw_loc_descr_ref loc_result;
12436 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
12437 return NULL;
12439 base = get_base_address (MEM_EXPR (mem));
12440 if (base == NULL
12441 || !VAR_P (base)
12442 || !DECL_THREAD_LOCAL_P (base))
12443 return NULL;
12445 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
12446 if (loc_result == NULL)
12447 return NULL;
12449 if (MEM_OFFSET (mem))
12450 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
12452 return loc_result;
12455 /* Output debug info about reason why we failed to expand expression as dwarf
12456 expression. */
12458 static void
12459 expansion_failed (tree expr, rtx rtl, char const *reason)
12461 if (dump_file && (dump_flags & TDF_DETAILS))
12463 fprintf (dump_file, "Failed to expand as dwarf: ");
12464 if (expr)
12465 print_generic_expr (dump_file, expr, dump_flags);
12466 if (rtl)
12468 fprintf (dump_file, "\n");
12469 print_rtl (dump_file, rtl);
12471 fprintf (dump_file, "\nReason: %s\n", reason);
12475 /* Helper function for const_ok_for_output. */
12477 static bool
12478 const_ok_for_output_1 (rtx rtl)
12480 if (GET_CODE (rtl) == UNSPEC)
12482 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
12483 we can't express it in the debug info. */
12484 /* Don't complain about TLS UNSPECs, those are just too hard to
12485 delegitimize. Note this could be a non-decl SYMBOL_REF such as
12486 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
12487 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
12488 if (flag_checking
12489 && (XVECLEN (rtl, 0) == 0
12490 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
12491 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE))
12492 inform (current_function_decl
12493 ? DECL_SOURCE_LOCATION (current_function_decl)
12494 : UNKNOWN_LOCATION,
12495 #if NUM_UNSPEC_VALUES > 0
12496 "non-delegitimized UNSPEC %s (%d) found in variable location",
12497 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
12498 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
12499 XINT (rtl, 1));
12500 #else
12501 "non-delegitimized UNSPEC %d found in variable location",
12502 XINT (rtl, 1));
12503 #endif
12504 expansion_failed (NULL_TREE, rtl,
12505 "UNSPEC hasn't been delegitimized.\n");
12506 return false;
12509 if (targetm.const_not_ok_for_debug_p (rtl))
12511 expansion_failed (NULL_TREE, rtl,
12512 "Expression rejected for debug by the backend.\n");
12513 return false;
12516 /* FIXME: Refer to PR60655. It is possible for simplification
12517 of rtl expressions in var tracking to produce such expressions.
12518 We should really identify / validate expressions
12519 enclosed in CONST that can be handled by assemblers on various
12520 targets and only handle legitimate cases here. */
12521 if (GET_CODE (rtl) != SYMBOL_REF)
12523 if (GET_CODE (rtl) == NOT)
12524 return false;
12525 return true;
12528 if (CONSTANT_POOL_ADDRESS_P (rtl))
12530 bool marked;
12531 get_pool_constant_mark (rtl, &marked);
12532 /* If all references to this pool constant were optimized away,
12533 it was not output and thus we can't represent it. */
12534 if (!marked)
12536 expansion_failed (NULL_TREE, rtl,
12537 "Constant was removed from constant pool.\n");
12538 return false;
12542 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12543 return false;
12545 /* Avoid references to external symbols in debug info, on several targets
12546 the linker might even refuse to link when linking a shared library,
12547 and in many other cases the relocations for .debug_info/.debug_loc are
12548 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
12549 to be defined within the same shared library or executable are fine. */
12550 if (SYMBOL_REF_EXTERNAL_P (rtl))
12552 tree decl = SYMBOL_REF_DECL (rtl);
12554 if (decl == NULL || !targetm.binds_local_p (decl))
12556 expansion_failed (NULL_TREE, rtl,
12557 "Symbol not defined in current TU.\n");
12558 return false;
12562 return true;
12565 /* Return true if constant RTL can be emitted in DW_OP_addr or
12566 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
12567 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
12569 static bool
12570 const_ok_for_output (rtx rtl)
12572 if (GET_CODE (rtl) == SYMBOL_REF)
12573 return const_ok_for_output_1 (rtl);
12575 if (GET_CODE (rtl) == CONST)
12577 subrtx_var_iterator::array_type array;
12578 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
12579 if (!const_ok_for_output_1 (*iter))
12580 return false;
12581 return true;
12584 return true;
12587 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
12588 if possible, NULL otherwise. */
12590 static dw_die_ref
12591 base_type_for_mode (machine_mode mode, bool unsignedp)
12593 dw_die_ref type_die;
12594 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
12596 if (type == NULL)
12597 return NULL;
12598 switch (TREE_CODE (type))
12600 case INTEGER_TYPE:
12601 case REAL_TYPE:
12602 break;
12603 default:
12604 return NULL;
12606 type_die = lookup_type_die (type);
12607 if (!type_die)
12608 type_die = modified_type_die (type, TYPE_UNQUALIFIED, false,
12609 comp_unit_die ());
12610 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
12611 return NULL;
12612 return type_die;
12615 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
12616 type matching MODE, or, if MODE is narrower than or as wide as
12617 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
12618 possible. */
12620 static dw_loc_descr_ref
12621 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
12623 machine_mode outer_mode = mode;
12624 dw_die_ref type_die;
12625 dw_loc_descr_ref cvt;
12627 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12629 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
12630 return op;
12632 type_die = base_type_for_mode (outer_mode, 1);
12633 if (type_die == NULL)
12634 return NULL;
12635 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12636 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12637 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12638 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12639 add_loc_descr (&op, cvt);
12640 return op;
12643 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
12645 static dw_loc_descr_ref
12646 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
12647 dw_loc_descr_ref op1)
12649 dw_loc_descr_ref ret = op0;
12650 add_loc_descr (&ret, op1);
12651 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12652 if (STORE_FLAG_VALUE != 1)
12654 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
12655 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
12657 return ret;
12660 /* Return location descriptor for signed comparison OP RTL. */
12662 static dw_loc_descr_ref
12663 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12664 machine_mode mem_mode)
12666 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12667 dw_loc_descr_ref op0, op1;
12668 int shift;
12670 if (op_mode == VOIDmode)
12671 op_mode = GET_MODE (XEXP (rtl, 1));
12672 if (op_mode == VOIDmode)
12673 return NULL;
12675 if (dwarf_strict
12676 && (!SCALAR_INT_MODE_P (op_mode)
12677 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
12678 return NULL;
12680 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12681 VAR_INIT_STATUS_INITIALIZED);
12682 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12683 VAR_INIT_STATUS_INITIALIZED);
12685 if (op0 == NULL || op1 == NULL)
12686 return NULL;
12688 if (!SCALAR_INT_MODE_P (op_mode)
12689 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12690 return compare_loc_descriptor (op, op0, op1);
12692 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12694 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
12695 dw_loc_descr_ref cvt;
12697 if (type_die == NULL)
12698 return NULL;
12699 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12700 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12701 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12702 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12703 add_loc_descr (&op0, cvt);
12704 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12705 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12706 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12707 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12708 add_loc_descr (&op1, cvt);
12709 return compare_loc_descriptor (op, op0, op1);
12712 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
12713 /* For eq/ne, if the operands are known to be zero-extended,
12714 there is no need to do the fancy shifting up. */
12715 if (op == DW_OP_eq || op == DW_OP_ne)
12717 dw_loc_descr_ref last0, last1;
12718 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12720 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12722 /* deref_size zero extends, and for constants we can check
12723 whether they are zero extended or not. */
12724 if (((last0->dw_loc_opc == DW_OP_deref_size
12725 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12726 || (CONST_INT_P (XEXP (rtl, 0))
12727 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
12728 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
12729 && ((last1->dw_loc_opc == DW_OP_deref_size
12730 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
12731 || (CONST_INT_P (XEXP (rtl, 1))
12732 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
12733 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
12734 return compare_loc_descriptor (op, op0, op1);
12736 /* EQ/NE comparison against constant in narrower type than
12737 DWARF2_ADDR_SIZE can be performed either as
12738 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
12739 DW_OP_{eq,ne}
12741 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
12742 DW_OP_{eq,ne}. Pick whatever is shorter. */
12743 if (CONST_INT_P (XEXP (rtl, 1))
12744 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
12745 && (size_of_int_loc_descriptor (shift) + 1
12746 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
12747 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
12748 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12749 & GET_MODE_MASK (op_mode))))
12751 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
12752 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12753 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
12754 & GET_MODE_MASK (op_mode));
12755 return compare_loc_descriptor (op, op0, op1);
12758 add_loc_descr (&op0, int_loc_descriptor (shift));
12759 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12760 if (CONST_INT_P (XEXP (rtl, 1)))
12761 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
12762 else
12764 add_loc_descr (&op1, int_loc_descriptor (shift));
12765 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12767 return compare_loc_descriptor (op, op0, op1);
12770 /* Return location descriptor for unsigned comparison OP RTL. */
12772 static dw_loc_descr_ref
12773 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12774 machine_mode mem_mode)
12776 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12777 dw_loc_descr_ref op0, op1;
12779 if (op_mode == VOIDmode)
12780 op_mode = GET_MODE (XEXP (rtl, 1));
12781 if (op_mode == VOIDmode)
12782 return NULL;
12783 if (!SCALAR_INT_MODE_P (op_mode))
12784 return NULL;
12786 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12787 return NULL;
12789 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12790 VAR_INIT_STATUS_INITIALIZED);
12791 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12792 VAR_INIT_STATUS_INITIALIZED);
12794 if (op0 == NULL || op1 == NULL)
12795 return NULL;
12797 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12799 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12800 dw_loc_descr_ref last0, last1;
12801 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12803 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12805 if (CONST_INT_P (XEXP (rtl, 0)))
12806 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12807 /* deref_size zero extends, so no need to mask it again. */
12808 else if (last0->dw_loc_opc != DW_OP_deref_size
12809 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12811 add_loc_descr (&op0, int_loc_descriptor (mask));
12812 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12814 if (CONST_INT_P (XEXP (rtl, 1)))
12815 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12816 /* deref_size zero extends, so no need to mask it again. */
12817 else if (last1->dw_loc_opc != DW_OP_deref_size
12818 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12820 add_loc_descr (&op1, int_loc_descriptor (mask));
12821 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12824 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12826 HOST_WIDE_INT bias = 1;
12827 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12828 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12829 if (CONST_INT_P (XEXP (rtl, 1)))
12830 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12831 + INTVAL (XEXP (rtl, 1)));
12832 else
12833 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12834 bias, 0));
12836 return compare_loc_descriptor (op, op0, op1);
12839 /* Return location descriptor for {U,S}{MIN,MAX}. */
12841 static dw_loc_descr_ref
12842 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12843 machine_mode mem_mode)
12845 enum dwarf_location_atom op;
12846 dw_loc_descr_ref op0, op1, ret;
12847 dw_loc_descr_ref bra_node, drop_node;
12849 if (dwarf_strict
12850 && (!SCALAR_INT_MODE_P (mode)
12851 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12852 return NULL;
12854 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12855 VAR_INIT_STATUS_INITIALIZED);
12856 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12857 VAR_INIT_STATUS_INITIALIZED);
12859 if (op0 == NULL || op1 == NULL)
12860 return NULL;
12862 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12863 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12864 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12865 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12867 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12869 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12870 add_loc_descr (&op0, int_loc_descriptor (mask));
12871 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12872 add_loc_descr (&op1, int_loc_descriptor (mask));
12873 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12875 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12877 HOST_WIDE_INT bias = 1;
12878 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12879 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12880 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12883 else if (!SCALAR_INT_MODE_P (mode)
12884 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12886 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12887 add_loc_descr (&op0, int_loc_descriptor (shift));
12888 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12889 add_loc_descr (&op1, int_loc_descriptor (shift));
12890 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12892 else if (SCALAR_INT_MODE_P (mode)
12893 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12895 dw_die_ref type_die = base_type_for_mode (mode, 0);
12896 dw_loc_descr_ref cvt;
12897 if (type_die == NULL)
12898 return NULL;
12899 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12900 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12901 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12902 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12903 add_loc_descr (&op0, cvt);
12904 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12905 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12906 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12907 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12908 add_loc_descr (&op1, cvt);
12911 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12912 op = DW_OP_lt;
12913 else
12914 op = DW_OP_gt;
12915 ret = op0;
12916 add_loc_descr (&ret, op1);
12917 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12918 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12919 add_loc_descr (&ret, bra_node);
12920 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12921 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12922 add_loc_descr (&ret, drop_node);
12923 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12924 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12925 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12926 && SCALAR_INT_MODE_P (mode)
12927 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12928 ret = convert_descriptor_to_mode (mode, ret);
12929 return ret;
12932 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12933 but after converting arguments to type_die, afterwards
12934 convert back to unsigned. */
12936 static dw_loc_descr_ref
12937 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12938 machine_mode mode, machine_mode mem_mode)
12940 dw_loc_descr_ref cvt, op0, op1;
12942 if (type_die == NULL)
12943 return NULL;
12944 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12945 VAR_INIT_STATUS_INITIALIZED);
12946 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12947 VAR_INIT_STATUS_INITIALIZED);
12948 if (op0 == NULL || op1 == NULL)
12949 return NULL;
12950 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12951 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12952 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12953 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12954 add_loc_descr (&op0, cvt);
12955 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12956 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12957 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12958 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12959 add_loc_descr (&op1, cvt);
12960 add_loc_descr (&op0, op1);
12961 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12962 return convert_descriptor_to_mode (mode, op0);
12965 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12966 const0 is DW_OP_lit0 or corresponding typed constant,
12967 const1 is DW_OP_lit1 or corresponding typed constant
12968 and constMSB is constant with just the MSB bit set
12969 for the mode):
12970 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12971 L1: const0 DW_OP_swap
12972 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12973 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12974 L3: DW_OP_drop
12975 L4: DW_OP_nop
12977 CTZ is similar:
12978 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12979 L1: const0 DW_OP_swap
12980 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12981 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12982 L3: DW_OP_drop
12983 L4: DW_OP_nop
12985 FFS is similar:
12986 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12987 L1: const1 DW_OP_swap
12988 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12989 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12990 L3: DW_OP_drop
12991 L4: DW_OP_nop */
12993 static dw_loc_descr_ref
12994 clz_loc_descriptor (rtx rtl, machine_mode mode,
12995 machine_mode mem_mode)
12997 dw_loc_descr_ref op0, ret, tmp;
12998 HOST_WIDE_INT valv;
12999 dw_loc_descr_ref l1jump, l1label;
13000 dw_loc_descr_ref l2jump, l2label;
13001 dw_loc_descr_ref l3jump, l3label;
13002 dw_loc_descr_ref l4jump, l4label;
13003 rtx msb;
13005 if (!SCALAR_INT_MODE_P (mode)
13006 || GET_MODE (XEXP (rtl, 0)) != mode)
13007 return NULL;
13009 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13010 VAR_INIT_STATUS_INITIALIZED);
13011 if (op0 == NULL)
13012 return NULL;
13013 ret = op0;
13014 if (GET_CODE (rtl) == CLZ)
13016 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
13017 valv = GET_MODE_BITSIZE (mode);
13019 else if (GET_CODE (rtl) == FFS)
13020 valv = 0;
13021 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
13022 valv = GET_MODE_BITSIZE (mode);
13023 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13024 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
13025 add_loc_descr (&ret, l1jump);
13026 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13027 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
13028 VAR_INIT_STATUS_INITIALIZED);
13029 if (tmp == NULL)
13030 return NULL;
13031 add_loc_descr (&ret, tmp);
13032 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
13033 add_loc_descr (&ret, l4jump);
13034 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
13035 ? const1_rtx : const0_rtx,
13036 mode, mem_mode,
13037 VAR_INIT_STATUS_INITIALIZED);
13038 if (l1label == NULL)
13039 return NULL;
13040 add_loc_descr (&ret, l1label);
13041 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13042 l2label = new_loc_descr (DW_OP_dup, 0, 0);
13043 add_loc_descr (&ret, l2label);
13044 if (GET_CODE (rtl) != CLZ)
13045 msb = const1_rtx;
13046 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
13047 msb = GEN_INT (HOST_WIDE_INT_1U
13048 << (GET_MODE_BITSIZE (mode) - 1));
13049 else
13050 msb = immed_wide_int_const
13051 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
13052 GET_MODE_PRECISION (mode)), mode);
13053 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
13054 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13055 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
13056 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
13057 else
13058 tmp = mem_loc_descriptor (msb, mode, mem_mode,
13059 VAR_INIT_STATUS_INITIALIZED);
13060 if (tmp == NULL)
13061 return NULL;
13062 add_loc_descr (&ret, tmp);
13063 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13064 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
13065 add_loc_descr (&ret, l3jump);
13066 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13067 VAR_INIT_STATUS_INITIALIZED);
13068 if (tmp == NULL)
13069 return NULL;
13070 add_loc_descr (&ret, tmp);
13071 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
13072 ? DW_OP_shl : DW_OP_shr, 0, 0));
13073 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13074 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
13075 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13076 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
13077 add_loc_descr (&ret, l2jump);
13078 l3label = new_loc_descr (DW_OP_drop, 0, 0);
13079 add_loc_descr (&ret, l3label);
13080 l4label = new_loc_descr (DW_OP_nop, 0, 0);
13081 add_loc_descr (&ret, l4label);
13082 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13083 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13084 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13085 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13086 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13087 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
13088 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13089 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
13090 return ret;
13093 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
13094 const1 is DW_OP_lit1 or corresponding typed constant):
13095 const0 DW_OP_swap
13096 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13097 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13098 L2: DW_OP_drop
13100 PARITY is similar:
13101 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
13102 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
13103 L2: DW_OP_drop */
13105 static dw_loc_descr_ref
13106 popcount_loc_descriptor (rtx rtl, machine_mode mode,
13107 machine_mode mem_mode)
13109 dw_loc_descr_ref op0, ret, tmp;
13110 dw_loc_descr_ref l1jump, l1label;
13111 dw_loc_descr_ref l2jump, l2label;
13113 if (!SCALAR_INT_MODE_P (mode)
13114 || GET_MODE (XEXP (rtl, 0)) != mode)
13115 return NULL;
13117 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13118 VAR_INIT_STATUS_INITIALIZED);
13119 if (op0 == NULL)
13120 return NULL;
13121 ret = op0;
13122 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13123 VAR_INIT_STATUS_INITIALIZED);
13124 if (tmp == NULL)
13125 return NULL;
13126 add_loc_descr (&ret, tmp);
13127 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13128 l1label = new_loc_descr (DW_OP_dup, 0, 0);
13129 add_loc_descr (&ret, l1label);
13130 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13131 add_loc_descr (&ret, l2jump);
13132 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13133 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13134 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13135 VAR_INIT_STATUS_INITIALIZED);
13136 if (tmp == NULL)
13137 return NULL;
13138 add_loc_descr (&ret, tmp);
13139 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13140 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
13141 ? DW_OP_plus : DW_OP_xor, 0, 0));
13142 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13143 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
13144 VAR_INIT_STATUS_INITIALIZED);
13145 add_loc_descr (&ret, tmp);
13146 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13147 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13148 add_loc_descr (&ret, l1jump);
13149 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13150 add_loc_descr (&ret, l2label);
13151 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13152 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13153 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13154 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13155 return ret;
13158 /* BSWAP (constS is initial shift count, either 56 or 24):
13159 constS const0
13160 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
13161 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
13162 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
13163 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
13164 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
13166 static dw_loc_descr_ref
13167 bswap_loc_descriptor (rtx rtl, machine_mode mode,
13168 machine_mode mem_mode)
13170 dw_loc_descr_ref op0, ret, tmp;
13171 dw_loc_descr_ref l1jump, l1label;
13172 dw_loc_descr_ref l2jump, l2label;
13174 if (!SCALAR_INT_MODE_P (mode)
13175 || BITS_PER_UNIT != 8
13176 || (GET_MODE_BITSIZE (mode) != 32
13177 && GET_MODE_BITSIZE (mode) != 64))
13178 return NULL;
13180 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13181 VAR_INIT_STATUS_INITIALIZED);
13182 if (op0 == NULL)
13183 return NULL;
13185 ret = op0;
13186 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13187 mode, mem_mode,
13188 VAR_INIT_STATUS_INITIALIZED);
13189 if (tmp == NULL)
13190 return NULL;
13191 add_loc_descr (&ret, tmp);
13192 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13193 VAR_INIT_STATUS_INITIALIZED);
13194 if (tmp == NULL)
13195 return NULL;
13196 add_loc_descr (&ret, tmp);
13197 l1label = new_loc_descr (DW_OP_pick, 2, 0);
13198 add_loc_descr (&ret, l1label);
13199 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
13200 mode, mem_mode,
13201 VAR_INIT_STATUS_INITIALIZED);
13202 add_loc_descr (&ret, tmp);
13203 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
13204 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13205 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13206 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
13207 VAR_INIT_STATUS_INITIALIZED);
13208 if (tmp == NULL)
13209 return NULL;
13210 add_loc_descr (&ret, tmp);
13211 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
13212 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
13213 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13214 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13215 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13216 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
13217 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
13218 VAR_INIT_STATUS_INITIALIZED);
13219 add_loc_descr (&ret, tmp);
13220 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
13221 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
13222 add_loc_descr (&ret, l2jump);
13223 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
13224 VAR_INIT_STATUS_INITIALIZED);
13225 add_loc_descr (&ret, tmp);
13226 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
13227 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13228 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
13229 add_loc_descr (&ret, l1jump);
13230 l2label = new_loc_descr (DW_OP_drop, 0, 0);
13231 add_loc_descr (&ret, l2label);
13232 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13233 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
13234 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13235 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
13236 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
13237 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
13238 return ret;
13241 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
13242 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13243 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
13244 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
13246 ROTATERT is similar:
13247 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
13248 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
13249 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
13251 static dw_loc_descr_ref
13252 rotate_loc_descriptor (rtx rtl, machine_mode mode,
13253 machine_mode mem_mode)
13255 rtx rtlop1 = XEXP (rtl, 1);
13256 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
13257 int i;
13259 if (!SCALAR_INT_MODE_P (mode))
13260 return NULL;
13262 if (GET_MODE (rtlop1) != VOIDmode
13263 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
13264 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13265 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13266 VAR_INIT_STATUS_INITIALIZED);
13267 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13268 VAR_INIT_STATUS_INITIALIZED);
13269 if (op0 == NULL || op1 == NULL)
13270 return NULL;
13271 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13272 for (i = 0; i < 2; i++)
13274 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
13275 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
13276 mode, mem_mode,
13277 VAR_INIT_STATUS_INITIALIZED);
13278 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13279 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
13280 ? DW_OP_const4u
13281 : HOST_BITS_PER_WIDE_INT == 64
13282 ? DW_OP_const8u : DW_OP_constu,
13283 GET_MODE_MASK (mode), 0);
13284 else
13285 mask[i] = NULL;
13286 if (mask[i] == NULL)
13287 return NULL;
13288 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
13290 ret = op0;
13291 add_loc_descr (&ret, op1);
13292 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13293 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
13294 if (GET_CODE (rtl) == ROTATERT)
13296 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13297 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13298 GET_MODE_BITSIZE (mode), 0));
13300 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
13301 if (mask[0] != NULL)
13302 add_loc_descr (&ret, mask[0]);
13303 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
13304 if (mask[1] != NULL)
13306 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13307 add_loc_descr (&ret, mask[1]);
13308 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
13310 if (GET_CODE (rtl) == ROTATE)
13312 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
13313 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
13314 GET_MODE_BITSIZE (mode), 0));
13316 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
13317 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
13318 return ret;
13321 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
13322 for DEBUG_PARAMETER_REF RTL. */
13324 static dw_loc_descr_ref
13325 parameter_ref_descriptor (rtx rtl)
13327 dw_loc_descr_ref ret;
13328 dw_die_ref ref;
13330 if (dwarf_strict)
13331 return NULL;
13332 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
13333 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
13334 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
13335 if (ref)
13337 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13338 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13339 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13341 else
13343 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13344 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
13346 return ret;
13349 /* The following routine converts the RTL for a variable or parameter
13350 (resident in memory) into an equivalent Dwarf representation of a
13351 mechanism for getting the address of that same variable onto the top of a
13352 hypothetical "address evaluation" stack.
13354 When creating memory location descriptors, we are effectively transforming
13355 the RTL for a memory-resident object into its Dwarf postfix expression
13356 equivalent. This routine recursively descends an RTL tree, turning
13357 it into Dwarf postfix code as it goes.
13359 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13361 MEM_MODE is the mode of the memory reference, needed to handle some
13362 autoincrement addressing modes.
13364 Return 0 if we can't represent the location. */
13366 dw_loc_descr_ref
13367 mem_loc_descriptor (rtx rtl, machine_mode mode,
13368 machine_mode mem_mode,
13369 enum var_init_status initialized)
13371 dw_loc_descr_ref mem_loc_result = NULL;
13372 enum dwarf_location_atom op;
13373 dw_loc_descr_ref op0, op1;
13374 rtx inner = NULL_RTX;
13376 if (mode == VOIDmode)
13377 mode = GET_MODE (rtl);
13379 /* Note that for a dynamically sized array, the location we will generate a
13380 description of here will be the lowest numbered location which is
13381 actually within the array. That's *not* necessarily the same as the
13382 zeroth element of the array. */
13384 rtl = targetm.delegitimize_address (rtl);
13386 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13387 return NULL;
13389 switch (GET_CODE (rtl))
13391 case POST_INC:
13392 case POST_DEC:
13393 case POST_MODIFY:
13394 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13396 case SUBREG:
13397 /* The case of a subreg may arise when we have a local (register)
13398 variable or a formal (register) parameter which doesn't quite fill
13399 up an entire register. For now, just assume that it is
13400 legitimate to make the Dwarf info refer to the whole register which
13401 contains the given subreg. */
13402 if (!subreg_lowpart_p (rtl))
13403 break;
13404 inner = SUBREG_REG (rtl);
13405 /* FALLTHRU */
13406 case TRUNCATE:
13407 if (inner == NULL_RTX)
13408 inner = XEXP (rtl, 0);
13409 if (SCALAR_INT_MODE_P (mode)
13410 && SCALAR_INT_MODE_P (GET_MODE (inner))
13411 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13412 #ifdef POINTERS_EXTEND_UNSIGNED
13413 || (mode == Pmode && mem_mode != VOIDmode)
13414 #endif
13416 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
13418 mem_loc_result = mem_loc_descriptor (inner,
13419 GET_MODE (inner),
13420 mem_mode, initialized);
13421 break;
13423 if (dwarf_strict)
13424 break;
13425 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
13426 break;
13427 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
13428 && (!SCALAR_INT_MODE_P (mode)
13429 || !SCALAR_INT_MODE_P (GET_MODE (inner))))
13430 break;
13431 else
13433 dw_die_ref type_die;
13434 dw_loc_descr_ref cvt;
13436 mem_loc_result = mem_loc_descriptor (inner,
13437 GET_MODE (inner),
13438 mem_mode, initialized);
13439 if (mem_loc_result == NULL)
13440 break;
13441 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13442 if (type_die == NULL)
13444 mem_loc_result = NULL;
13445 break;
13447 if (GET_MODE_SIZE (mode)
13448 != GET_MODE_SIZE (GET_MODE (inner)))
13449 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13450 else
13451 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13452 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13453 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13454 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13455 add_loc_descr (&mem_loc_result, cvt);
13456 if (SCALAR_INT_MODE_P (mode)
13457 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13459 /* Convert it to untyped afterwards. */
13460 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13461 add_loc_descr (&mem_loc_result, cvt);
13464 break;
13466 case REG:
13467 if (! SCALAR_INT_MODE_P (mode)
13468 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13469 && rtl != arg_pointer_rtx
13470 && rtl != frame_pointer_rtx
13471 #ifdef POINTERS_EXTEND_UNSIGNED
13472 && (mode != Pmode || mem_mode == VOIDmode)
13473 #endif
13476 dw_die_ref type_die;
13477 unsigned int dbx_regnum;
13479 if (dwarf_strict)
13480 break;
13481 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13482 break;
13483 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13484 if (type_die == NULL)
13485 break;
13487 dbx_regnum = dbx_reg_number (rtl);
13488 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13489 break;
13490 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13491 dbx_regnum, 0);
13492 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13493 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13494 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13495 break;
13497 /* Whenever a register number forms a part of the description of the
13498 method for calculating the (dynamic) address of a memory resident
13499 object, DWARF rules require the register number be referred to as
13500 a "base register". This distinction is not based in any way upon
13501 what category of register the hardware believes the given register
13502 belongs to. This is strictly DWARF terminology we're dealing with
13503 here. Note that in cases where the location of a memory-resident
13504 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13505 OP_CONST (0)) the actual DWARF location descriptor that we generate
13506 may just be OP_BASEREG (basereg). This may look deceptively like
13507 the object in question was allocated to a register (rather than in
13508 memory) so DWARF consumers need to be aware of the subtle
13509 distinction between OP_REG and OP_BASEREG. */
13510 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13511 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13512 else if (stack_realign_drap
13513 && crtl->drap_reg
13514 && crtl->args.internal_arg_pointer == rtl
13515 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13517 /* If RTL is internal_arg_pointer, which has been optimized
13518 out, use DRAP instead. */
13519 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13520 VAR_INIT_STATUS_INITIALIZED);
13522 break;
13524 case SIGN_EXTEND:
13525 case ZERO_EXTEND:
13526 if (!SCALAR_INT_MODE_P (mode))
13527 break;
13528 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13529 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13530 if (op0 == 0)
13531 break;
13532 else if (GET_CODE (rtl) == ZERO_EXTEND
13533 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13534 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13535 < HOST_BITS_PER_WIDE_INT
13536 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
13537 to expand zero extend as two shifts instead of
13538 masking. */
13539 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
13541 machine_mode imode = GET_MODE (XEXP (rtl, 0));
13542 mem_loc_result = op0;
13543 add_loc_descr (&mem_loc_result,
13544 int_loc_descriptor (GET_MODE_MASK (imode)));
13545 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
13547 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13549 int shift = DWARF2_ADDR_SIZE
13550 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13551 shift *= BITS_PER_UNIT;
13552 if (GET_CODE (rtl) == SIGN_EXTEND)
13553 op = DW_OP_shra;
13554 else
13555 op = DW_OP_shr;
13556 mem_loc_result = op0;
13557 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13558 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13559 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13560 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13562 else if (!dwarf_strict)
13564 dw_die_ref type_die1, type_die2;
13565 dw_loc_descr_ref cvt;
13567 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13568 GET_CODE (rtl) == ZERO_EXTEND);
13569 if (type_die1 == NULL)
13570 break;
13571 type_die2 = base_type_for_mode (mode, 1);
13572 if (type_die2 == NULL)
13573 break;
13574 mem_loc_result = op0;
13575 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13576 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13577 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13578 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13579 add_loc_descr (&mem_loc_result, cvt);
13580 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13581 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13582 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
13583 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13584 add_loc_descr (&mem_loc_result, cvt);
13586 break;
13588 case MEM:
13590 rtx new_rtl = avoid_constant_pool_reference (rtl);
13591 if (new_rtl != rtl)
13593 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
13594 initialized);
13595 if (mem_loc_result != NULL)
13596 return mem_loc_result;
13599 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
13600 get_address_mode (rtl), mode,
13601 VAR_INIT_STATUS_INITIALIZED);
13602 if (mem_loc_result == NULL)
13603 mem_loc_result = tls_mem_loc_descriptor (rtl);
13604 if (mem_loc_result != NULL)
13606 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13607 || !SCALAR_INT_MODE_P(mode))
13609 dw_die_ref type_die;
13610 dw_loc_descr_ref deref;
13612 if (dwarf_strict)
13613 return NULL;
13614 type_die
13615 = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
13616 if (type_die == NULL)
13617 return NULL;
13618 deref = new_loc_descr (DW_OP_GNU_deref_type,
13619 GET_MODE_SIZE (mode), 0);
13620 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13621 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13622 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
13623 add_loc_descr (&mem_loc_result, deref);
13625 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
13626 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13627 else
13628 add_loc_descr (&mem_loc_result,
13629 new_loc_descr (DW_OP_deref_size,
13630 GET_MODE_SIZE (mode), 0));
13632 break;
13634 case LO_SUM:
13635 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
13637 case LABEL_REF:
13638 /* Some ports can transform a symbol ref into a label ref, because
13639 the symbol ref is too far away and has to be dumped into a constant
13640 pool. */
13641 case CONST:
13642 case SYMBOL_REF:
13643 if (!SCALAR_INT_MODE_P (mode)
13644 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13645 #ifdef POINTERS_EXTEND_UNSIGNED
13646 && (mode != Pmode || mem_mode == VOIDmode)
13647 #endif
13649 break;
13650 if (GET_CODE (rtl) == SYMBOL_REF
13651 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13653 dw_loc_descr_ref temp;
13655 /* If this is not defined, we have no way to emit the data. */
13656 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13657 break;
13659 temp = new_addr_loc_descr (rtl, dtprel_true);
13661 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13662 add_loc_descr (&mem_loc_result, temp);
13664 break;
13667 if (!const_ok_for_output (rtl))
13669 if (GET_CODE (rtl) == CONST)
13670 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13671 initialized);
13672 break;
13675 symref:
13676 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
13677 vec_safe_push (used_rtx_array, rtl);
13678 break;
13680 case CONCAT:
13681 case CONCATN:
13682 case VAR_LOCATION:
13683 case DEBUG_IMPLICIT_PTR:
13684 expansion_failed (NULL_TREE, rtl,
13685 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13686 return 0;
13688 case ENTRY_VALUE:
13689 if (dwarf_strict)
13690 return NULL;
13691 if (REG_P (ENTRY_VALUE_EXP (rtl)))
13693 if (!SCALAR_INT_MODE_P (mode)
13694 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13695 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13696 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13697 else
13699 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
13700 if (dbx_regnum == IGNORED_DWARF_REGNUM)
13701 return NULL;
13702 op0 = one_reg_loc_descriptor (dbx_regnum,
13703 VAR_INIT_STATUS_INITIALIZED);
13706 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
13707 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13709 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
13710 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13711 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
13712 return NULL;
13714 else
13715 gcc_unreachable ();
13716 if (op0 == NULL)
13717 return NULL;
13718 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13719 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13720 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
13721 break;
13723 case DEBUG_PARAMETER_REF:
13724 mem_loc_result = parameter_ref_descriptor (rtl);
13725 break;
13727 case PRE_MODIFY:
13728 /* Extract the PLUS expression nested inside and fall into
13729 PLUS code below. */
13730 rtl = XEXP (rtl, 1);
13731 goto plus;
13733 case PRE_INC:
13734 case PRE_DEC:
13735 /* Turn these into a PLUS expression and fall into the PLUS code
13736 below. */
13737 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
13738 gen_int_mode (GET_CODE (rtl) == PRE_INC
13739 ? GET_MODE_UNIT_SIZE (mem_mode)
13740 : -GET_MODE_UNIT_SIZE (mem_mode),
13741 mode));
13743 /* fall through */
13745 case PLUS:
13746 plus:
13747 if (is_based_loc (rtl)
13748 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13749 || XEXP (rtl, 0) == arg_pointer_rtx
13750 || XEXP (rtl, 0) == frame_pointer_rtx)
13751 && SCALAR_INT_MODE_P (mode))
13752 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13753 INTVAL (XEXP (rtl, 1)),
13754 VAR_INIT_STATUS_INITIALIZED);
13755 else
13757 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13758 VAR_INIT_STATUS_INITIALIZED);
13759 if (mem_loc_result == 0)
13760 break;
13762 if (CONST_INT_P (XEXP (rtl, 1))
13763 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13764 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13765 else
13767 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13768 VAR_INIT_STATUS_INITIALIZED);
13769 if (op1 == 0)
13770 return NULL;
13771 add_loc_descr (&mem_loc_result, op1);
13772 add_loc_descr (&mem_loc_result,
13773 new_loc_descr (DW_OP_plus, 0, 0));
13776 break;
13778 /* If a pseudo-reg is optimized away, it is possible for it to
13779 be replaced with a MEM containing a multiply or shift. */
13780 case MINUS:
13781 op = DW_OP_minus;
13782 goto do_binop;
13784 case MULT:
13785 op = DW_OP_mul;
13786 goto do_binop;
13788 case DIV:
13789 if (!dwarf_strict
13790 && SCALAR_INT_MODE_P (mode)
13791 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13793 mem_loc_result = typed_binop (DW_OP_div, rtl,
13794 base_type_for_mode (mode, 0),
13795 mode, mem_mode);
13796 break;
13798 op = DW_OP_div;
13799 goto do_binop;
13801 case UMOD:
13802 op = DW_OP_mod;
13803 goto do_binop;
13805 case ASHIFT:
13806 op = DW_OP_shl;
13807 goto do_shift;
13809 case ASHIFTRT:
13810 op = DW_OP_shra;
13811 goto do_shift;
13813 case LSHIFTRT:
13814 op = DW_OP_shr;
13815 goto do_shift;
13817 do_shift:
13818 if (!SCALAR_INT_MODE_P (mode))
13819 break;
13820 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13821 VAR_INIT_STATUS_INITIALIZED);
13823 rtx rtlop1 = XEXP (rtl, 1);
13824 if (GET_MODE (rtlop1) != VOIDmode
13825 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13826 < GET_MODE_BITSIZE (mode))
13827 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13828 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13829 VAR_INIT_STATUS_INITIALIZED);
13832 if (op0 == 0 || op1 == 0)
13833 break;
13835 mem_loc_result = op0;
13836 add_loc_descr (&mem_loc_result, op1);
13837 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13838 break;
13840 case AND:
13841 op = DW_OP_and;
13842 goto do_binop;
13844 case IOR:
13845 op = DW_OP_or;
13846 goto do_binop;
13848 case XOR:
13849 op = DW_OP_xor;
13850 goto do_binop;
13852 do_binop:
13853 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13854 VAR_INIT_STATUS_INITIALIZED);
13855 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13856 VAR_INIT_STATUS_INITIALIZED);
13858 if (op0 == 0 || op1 == 0)
13859 break;
13861 mem_loc_result = op0;
13862 add_loc_descr (&mem_loc_result, op1);
13863 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13864 break;
13866 case MOD:
13867 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13869 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13870 base_type_for_mode (mode, 0),
13871 mode, mem_mode);
13872 break;
13875 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13876 VAR_INIT_STATUS_INITIALIZED);
13877 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13878 VAR_INIT_STATUS_INITIALIZED);
13880 if (op0 == 0 || op1 == 0)
13881 break;
13883 mem_loc_result = op0;
13884 add_loc_descr (&mem_loc_result, op1);
13885 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13886 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13887 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13888 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13889 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13890 break;
13892 case UDIV:
13893 if (!dwarf_strict && SCALAR_INT_MODE_P (mode))
13895 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13897 op = DW_OP_div;
13898 goto do_binop;
13900 mem_loc_result = typed_binop (DW_OP_div, rtl,
13901 base_type_for_mode (mode, 1),
13902 mode, mem_mode);
13904 break;
13906 case NOT:
13907 op = DW_OP_not;
13908 goto do_unop;
13910 case ABS:
13911 op = DW_OP_abs;
13912 goto do_unop;
13914 case NEG:
13915 op = DW_OP_neg;
13916 goto do_unop;
13918 do_unop:
13919 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13920 VAR_INIT_STATUS_INITIALIZED);
13922 if (op0 == 0)
13923 break;
13925 mem_loc_result = op0;
13926 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13927 break;
13929 case CONST_INT:
13930 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13931 #ifdef POINTERS_EXTEND_UNSIGNED
13932 || (mode == Pmode
13933 && mem_mode != VOIDmode
13934 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13935 #endif
13938 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13939 break;
13941 if (!dwarf_strict
13942 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13943 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13945 dw_die_ref type_die = base_type_for_mode (mode, 1);
13946 machine_mode amode;
13947 if (type_die == NULL)
13948 return NULL;
13949 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13950 MODE_INT, 0);
13951 if (INTVAL (rtl) >= 0
13952 && amode != BLKmode
13953 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13954 /* const DW_OP_GNU_convert <XXX> vs.
13955 DW_OP_GNU_const_type <XXX, 1, const>. */
13956 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13957 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13959 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13960 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13961 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13962 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13963 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13964 add_loc_descr (&mem_loc_result, op0);
13965 return mem_loc_result;
13967 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13968 INTVAL (rtl));
13969 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13970 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13971 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13972 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13973 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13974 else
13976 mem_loc_result->dw_loc_oprnd2.val_class
13977 = dw_val_class_const_double;
13978 mem_loc_result->dw_loc_oprnd2.v.val_double
13979 = double_int::from_shwi (INTVAL (rtl));
13982 break;
13984 case CONST_DOUBLE:
13985 if (!dwarf_strict)
13987 dw_die_ref type_die;
13989 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13990 CONST_DOUBLE rtx could represent either a large integer
13991 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13992 the value is always a floating point constant.
13994 When it is an integer, a CONST_DOUBLE is used whenever
13995 the constant requires 2 HWIs to be adequately represented.
13996 We output CONST_DOUBLEs as blocks. */
13997 if (mode == VOIDmode
13998 || (GET_MODE (rtl) == VOIDmode
13999 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
14000 break;
14001 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14002 if (type_die == NULL)
14003 return NULL;
14004 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14005 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14006 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14007 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14008 #if TARGET_SUPPORTS_WIDE_INT == 0
14009 if (!SCALAR_FLOAT_MODE_P (mode))
14011 mem_loc_result->dw_loc_oprnd2.val_class
14012 = dw_val_class_const_double;
14013 mem_loc_result->dw_loc_oprnd2.v.val_double
14014 = rtx_to_double_int (rtl);
14016 else
14017 #endif
14019 unsigned int length = GET_MODE_SIZE (mode);
14020 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14022 insert_float (rtl, array);
14023 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14024 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14025 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14026 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14029 break;
14031 case CONST_WIDE_INT:
14032 if (!dwarf_strict)
14034 dw_die_ref type_die;
14036 type_die = base_type_for_mode (mode, SCALAR_INT_MODE_P (mode));
14037 if (type_die == NULL)
14038 return NULL;
14039 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14040 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14041 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14042 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14043 mem_loc_result->dw_loc_oprnd2.val_class
14044 = dw_val_class_wide_int;
14045 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14046 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14048 break;
14050 case EQ:
14051 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
14052 break;
14054 case GE:
14055 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14056 break;
14058 case GT:
14059 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14060 break;
14062 case LE:
14063 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14064 break;
14066 case LT:
14067 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14068 break;
14070 case NE:
14071 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
14072 break;
14074 case GEU:
14075 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
14076 break;
14078 case GTU:
14079 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
14080 break;
14082 case LEU:
14083 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
14084 break;
14086 case LTU:
14087 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
14088 break;
14090 case UMIN:
14091 case UMAX:
14092 if (!SCALAR_INT_MODE_P (mode))
14093 break;
14094 /* FALLTHRU */
14095 case SMIN:
14096 case SMAX:
14097 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
14098 break;
14100 case ZERO_EXTRACT:
14101 case SIGN_EXTRACT:
14102 if (CONST_INT_P (XEXP (rtl, 1))
14103 && CONST_INT_P (XEXP (rtl, 2))
14104 && ((unsigned) INTVAL (XEXP (rtl, 1))
14105 + (unsigned) INTVAL (XEXP (rtl, 2))
14106 <= GET_MODE_BITSIZE (mode))
14107 && SCALAR_INT_MODE_P (mode)
14108 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14109 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14111 int shift, size;
14112 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14113 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14114 if (op0 == 0)
14115 break;
14116 if (GET_CODE (rtl) == SIGN_EXTRACT)
14117 op = DW_OP_shra;
14118 else
14119 op = DW_OP_shr;
14120 mem_loc_result = op0;
14121 size = INTVAL (XEXP (rtl, 1));
14122 shift = INTVAL (XEXP (rtl, 2));
14123 if (BITS_BIG_ENDIAN)
14124 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14125 - shift - size;
14126 if (shift + size != (int) DWARF2_ADDR_SIZE)
14128 add_loc_descr (&mem_loc_result,
14129 int_loc_descriptor (DWARF2_ADDR_SIZE
14130 - shift - size));
14131 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14133 if (size != (int) DWARF2_ADDR_SIZE)
14135 add_loc_descr (&mem_loc_result,
14136 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14137 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14140 break;
14142 case IF_THEN_ELSE:
14144 dw_loc_descr_ref op2, bra_node, drop_node;
14145 op0 = mem_loc_descriptor (XEXP (rtl, 0),
14146 GET_MODE (XEXP (rtl, 0)) == VOIDmode
14147 ? word_mode : GET_MODE (XEXP (rtl, 0)),
14148 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14149 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14150 VAR_INIT_STATUS_INITIALIZED);
14151 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14152 VAR_INIT_STATUS_INITIALIZED);
14153 if (op0 == NULL || op1 == NULL || op2 == NULL)
14154 break;
14156 mem_loc_result = op1;
14157 add_loc_descr (&mem_loc_result, op2);
14158 add_loc_descr (&mem_loc_result, op0);
14159 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14160 add_loc_descr (&mem_loc_result, bra_node);
14161 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14162 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14163 add_loc_descr (&mem_loc_result, drop_node);
14164 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14165 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14167 break;
14169 case FLOAT_EXTEND:
14170 case FLOAT_TRUNCATE:
14171 case FLOAT:
14172 case UNSIGNED_FLOAT:
14173 case FIX:
14174 case UNSIGNED_FIX:
14175 if (!dwarf_strict)
14177 dw_die_ref type_die;
14178 dw_loc_descr_ref cvt;
14180 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14181 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14182 if (op0 == NULL)
14183 break;
14184 if (SCALAR_INT_MODE_P (GET_MODE (XEXP (rtl, 0)))
14185 && (GET_CODE (rtl) == FLOAT
14186 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14187 <= DWARF2_ADDR_SIZE))
14189 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14190 GET_CODE (rtl) == UNSIGNED_FLOAT);
14191 if (type_die == NULL)
14192 break;
14193 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14194 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14195 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14196 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14197 add_loc_descr (&op0, cvt);
14199 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14200 if (type_die == NULL)
14201 break;
14202 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14203 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14204 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14205 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14206 add_loc_descr (&op0, cvt);
14207 if (SCALAR_INT_MODE_P (mode)
14208 && (GET_CODE (rtl) == FIX
14209 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14211 op0 = convert_descriptor_to_mode (mode, op0);
14212 if (op0 == NULL)
14213 break;
14215 mem_loc_result = op0;
14217 break;
14219 case CLZ:
14220 case CTZ:
14221 case FFS:
14222 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
14223 break;
14225 case POPCOUNT:
14226 case PARITY:
14227 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
14228 break;
14230 case BSWAP:
14231 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
14232 break;
14234 case ROTATE:
14235 case ROTATERT:
14236 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
14237 break;
14239 case COMPARE:
14240 /* In theory, we could implement the above. */
14241 /* DWARF cannot represent the unsigned compare operations
14242 natively. */
14243 case SS_MULT:
14244 case US_MULT:
14245 case SS_DIV:
14246 case US_DIV:
14247 case SS_PLUS:
14248 case US_PLUS:
14249 case SS_MINUS:
14250 case US_MINUS:
14251 case SS_NEG:
14252 case US_NEG:
14253 case SS_ABS:
14254 case SS_ASHIFT:
14255 case US_ASHIFT:
14256 case SS_TRUNCATE:
14257 case US_TRUNCATE:
14258 case UNORDERED:
14259 case ORDERED:
14260 case UNEQ:
14261 case UNGE:
14262 case UNGT:
14263 case UNLE:
14264 case UNLT:
14265 case LTGT:
14266 case FRACT_CONVERT:
14267 case UNSIGNED_FRACT_CONVERT:
14268 case SAT_FRACT:
14269 case UNSIGNED_SAT_FRACT:
14270 case SQRT:
14271 case ASM_OPERANDS:
14272 case VEC_MERGE:
14273 case VEC_SELECT:
14274 case VEC_CONCAT:
14275 case VEC_DUPLICATE:
14276 case UNSPEC:
14277 case HIGH:
14278 case FMA:
14279 case STRICT_LOW_PART:
14280 case CONST_VECTOR:
14281 case CONST_FIXED:
14282 case CLRSB:
14283 case CLOBBER:
14284 /* If delegitimize_address couldn't do anything with the UNSPEC, we
14285 can't express it in the debug info. This can happen e.g. with some
14286 TLS UNSPECs. */
14287 break;
14289 case CONST_STRING:
14290 resolve_one_addr (&rtl);
14291 goto symref;
14293 default:
14294 if (flag_checking)
14296 print_rtl (stderr, rtl);
14297 gcc_unreachable ();
14299 break;
14302 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14303 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14305 return mem_loc_result;
14308 /* Return a descriptor that describes the concatenation of two locations.
14309 This is typically a complex variable. */
14311 static dw_loc_descr_ref
14312 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14314 dw_loc_descr_ref cc_loc_result = NULL;
14315 dw_loc_descr_ref x0_ref
14316 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14317 dw_loc_descr_ref x1_ref
14318 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14320 if (x0_ref == 0 || x1_ref == 0)
14321 return 0;
14323 cc_loc_result = x0_ref;
14324 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14326 add_loc_descr (&cc_loc_result, x1_ref);
14327 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14329 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14330 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14332 return cc_loc_result;
14335 /* Return a descriptor that describes the concatenation of N
14336 locations. */
14338 static dw_loc_descr_ref
14339 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14341 unsigned int i;
14342 dw_loc_descr_ref cc_loc_result = NULL;
14343 unsigned int n = XVECLEN (concatn, 0);
14345 for (i = 0; i < n; ++i)
14347 dw_loc_descr_ref ref;
14348 rtx x = XVECEXP (concatn, 0, i);
14350 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14351 if (ref == NULL)
14352 return NULL;
14354 add_loc_descr (&cc_loc_result, ref);
14355 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14358 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14359 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14361 return cc_loc_result;
14364 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
14365 for DEBUG_IMPLICIT_PTR RTL. */
14367 static dw_loc_descr_ref
14368 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14370 dw_loc_descr_ref ret;
14371 dw_die_ref ref;
14373 if (dwarf_strict)
14374 return NULL;
14375 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14376 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14377 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14378 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14379 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14380 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14381 if (ref)
14383 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14384 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14385 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14387 else
14389 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14390 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14392 return ret;
14395 /* Output a proper Dwarf location descriptor for a variable or parameter
14396 which is either allocated in a register or in a memory location. For a
14397 register, we just generate an OP_REG and the register number. For a
14398 memory location we provide a Dwarf postfix expression describing how to
14399 generate the (dynamic) address of the object onto the address stack.
14401 MODE is mode of the decl if this loc_descriptor is going to be used in
14402 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14403 allowed, VOIDmode otherwise.
14405 If we don't know how to describe it, return 0. */
14407 static dw_loc_descr_ref
14408 loc_descriptor (rtx rtl, machine_mode mode,
14409 enum var_init_status initialized)
14411 dw_loc_descr_ref loc_result = NULL;
14413 switch (GET_CODE (rtl))
14415 case SUBREG:
14416 /* The case of a subreg may arise when we have a local (register)
14417 variable or a formal (register) parameter which doesn't quite fill
14418 up an entire register. For now, just assume that it is
14419 legitimate to make the Dwarf info refer to the whole register which
14420 contains the given subreg. */
14421 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
14422 loc_result = loc_descriptor (SUBREG_REG (rtl),
14423 GET_MODE (SUBREG_REG (rtl)), initialized);
14424 else
14425 goto do_default;
14426 break;
14428 case REG:
14429 loc_result = reg_loc_descriptor (rtl, initialized);
14430 break;
14432 case MEM:
14433 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14434 GET_MODE (rtl), initialized);
14435 if (loc_result == NULL)
14436 loc_result = tls_mem_loc_descriptor (rtl);
14437 if (loc_result == NULL)
14439 rtx new_rtl = avoid_constant_pool_reference (rtl);
14440 if (new_rtl != rtl)
14441 loc_result = loc_descriptor (new_rtl, mode, initialized);
14443 break;
14445 case CONCAT:
14446 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14447 initialized);
14448 break;
14450 case CONCATN:
14451 loc_result = concatn_loc_descriptor (rtl, initialized);
14452 break;
14454 case VAR_LOCATION:
14455 /* Single part. */
14456 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14458 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14459 if (GET_CODE (loc) == EXPR_LIST)
14460 loc = XEXP (loc, 0);
14461 loc_result = loc_descriptor (loc, mode, initialized);
14462 break;
14465 rtl = XEXP (rtl, 1);
14466 /* FALLTHRU */
14468 case PARALLEL:
14470 rtvec par_elems = XVEC (rtl, 0);
14471 int num_elem = GET_NUM_ELEM (par_elems);
14472 machine_mode mode;
14473 int i;
14475 /* Create the first one, so we have something to add to. */
14476 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14477 VOIDmode, initialized);
14478 if (loc_result == NULL)
14479 return NULL;
14480 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14481 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14482 for (i = 1; i < num_elem; i++)
14484 dw_loc_descr_ref temp;
14486 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14487 VOIDmode, initialized);
14488 if (temp == NULL)
14489 return NULL;
14490 add_loc_descr (&loc_result, temp);
14491 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14492 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14495 break;
14497 case CONST_INT:
14498 if (mode != VOIDmode && mode != BLKmode)
14499 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14500 INTVAL (rtl));
14501 break;
14503 case CONST_DOUBLE:
14504 if (mode == VOIDmode)
14505 mode = GET_MODE (rtl);
14507 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14509 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14511 /* Note that a CONST_DOUBLE rtx could represent either an integer
14512 or a floating-point constant. A CONST_DOUBLE is used whenever
14513 the constant requires more than one word in order to be
14514 adequately represented. We output CONST_DOUBLEs as blocks. */
14515 loc_result = new_loc_descr (DW_OP_implicit_value,
14516 GET_MODE_SIZE (mode), 0);
14517 #if TARGET_SUPPORTS_WIDE_INT == 0
14518 if (!SCALAR_FLOAT_MODE_P (mode))
14520 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14521 loc_result->dw_loc_oprnd2.v.val_double
14522 = rtx_to_double_int (rtl);
14524 else
14525 #endif
14527 unsigned int length = GET_MODE_SIZE (mode);
14528 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
14530 insert_float (rtl, array);
14531 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14532 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14533 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14534 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14537 break;
14539 case CONST_WIDE_INT:
14540 if (mode == VOIDmode)
14541 mode = GET_MODE (rtl);
14543 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14545 loc_result = new_loc_descr (DW_OP_implicit_value,
14546 GET_MODE_SIZE (mode), 0);
14547 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
14548 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
14549 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
14551 break;
14553 case CONST_VECTOR:
14554 if (mode == VOIDmode)
14555 mode = GET_MODE (rtl);
14557 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14559 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14560 unsigned int length = CONST_VECTOR_NUNITS (rtl);
14561 unsigned char *array
14562 = ggc_vec_alloc<unsigned char> (length * elt_size);
14563 unsigned int i;
14564 unsigned char *p;
14565 machine_mode imode = GET_MODE_INNER (mode);
14567 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14568 switch (GET_MODE_CLASS (mode))
14570 case MODE_VECTOR_INT:
14571 for (i = 0, p = array; i < length; i++, p += elt_size)
14573 rtx elt = CONST_VECTOR_ELT (rtl, i);
14574 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
14576 break;
14578 case MODE_VECTOR_FLOAT:
14579 for (i = 0, p = array; i < length; i++, p += elt_size)
14581 rtx elt = CONST_VECTOR_ELT (rtl, i);
14582 insert_float (elt, p);
14584 break;
14586 default:
14587 gcc_unreachable ();
14590 loc_result = new_loc_descr (DW_OP_implicit_value,
14591 length * elt_size, 0);
14592 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14593 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14594 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14595 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14597 break;
14599 case CONST:
14600 if (mode == VOIDmode
14601 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
14602 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
14603 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14605 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14606 break;
14608 /* FALLTHROUGH */
14609 case SYMBOL_REF:
14610 if (!const_ok_for_output (rtl))
14611 break;
14612 /* FALLTHROUGH */
14613 case LABEL_REF:
14614 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14615 && (dwarf_version >= 4 || !dwarf_strict))
14617 loc_result = new_addr_loc_descr (rtl, dtprel_false);
14618 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14619 vec_safe_push (used_rtx_array, rtl);
14621 break;
14623 case DEBUG_IMPLICIT_PTR:
14624 loc_result = implicit_ptr_descriptor (rtl, 0);
14625 break;
14627 case PLUS:
14628 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14629 && CONST_INT_P (XEXP (rtl, 1)))
14631 loc_result
14632 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14633 break;
14635 /* FALLTHRU */
14636 do_default:
14637 default:
14638 if ((SCALAR_INT_MODE_P (mode)
14639 && GET_MODE (rtl) == mode
14640 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14641 && dwarf_version >= 4)
14642 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
14644 /* Value expression. */
14645 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
14646 if (loc_result)
14647 add_loc_descr (&loc_result,
14648 new_loc_descr (DW_OP_stack_value, 0, 0));
14650 break;
14653 return loc_result;
14656 /* We need to figure out what section we should use as the base for the
14657 address ranges where a given location is valid.
14658 1. If this particular DECL has a section associated with it, use that.
14659 2. If this function has a section associated with it, use that.
14660 3. Otherwise, use the text section.
14661 XXX: If you split a variable across multiple sections, we won't notice. */
14663 static const char *
14664 secname_for_decl (const_tree decl)
14666 const char *secname;
14668 if (VAR_OR_FUNCTION_DECL_P (decl)
14669 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
14670 && DECL_SECTION_NAME (decl))
14671 secname = DECL_SECTION_NAME (decl);
14672 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14673 secname = DECL_SECTION_NAME (current_function_decl);
14674 else if (cfun && in_cold_section_p)
14675 secname = crtl->subsections.cold_section_label;
14676 else
14677 secname = text_section_label;
14679 return secname;
14682 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
14684 static bool
14685 decl_by_reference_p (tree decl)
14687 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14688 || VAR_P (decl))
14689 && DECL_BY_REFERENCE (decl));
14692 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14693 for VARLOC. */
14695 static dw_loc_descr_ref
14696 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14697 enum var_init_status initialized)
14699 int have_address = 0;
14700 dw_loc_descr_ref descr;
14701 machine_mode mode;
14703 if (want_address != 2)
14705 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14706 /* Single part. */
14707 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14709 varloc = PAT_VAR_LOCATION_LOC (varloc);
14710 if (GET_CODE (varloc) == EXPR_LIST)
14711 varloc = XEXP (varloc, 0);
14712 mode = GET_MODE (varloc);
14713 if (MEM_P (varloc))
14715 rtx addr = XEXP (varloc, 0);
14716 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
14717 mode, initialized);
14718 if (descr)
14719 have_address = 1;
14720 else
14722 rtx x = avoid_constant_pool_reference (varloc);
14723 if (x != varloc)
14724 descr = mem_loc_descriptor (x, mode, VOIDmode,
14725 initialized);
14728 else
14729 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
14731 else
14732 return 0;
14734 else
14736 if (GET_CODE (varloc) == VAR_LOCATION)
14737 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14738 else
14739 mode = DECL_MODE (loc);
14740 descr = loc_descriptor (varloc, mode, initialized);
14741 have_address = 1;
14744 if (!descr)
14745 return 0;
14747 if (want_address == 2 && !have_address
14748 && (dwarf_version >= 4 || !dwarf_strict))
14750 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14752 expansion_failed (loc, NULL_RTX,
14753 "DWARF address size mismatch");
14754 return 0;
14756 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14757 have_address = 1;
14759 /* Show if we can't fill the request for an address. */
14760 if (want_address && !have_address)
14762 expansion_failed (loc, NULL_RTX,
14763 "Want address and only have value");
14764 return 0;
14767 /* If we've got an address and don't want one, dereference. */
14768 if (!want_address && have_address)
14770 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14771 enum dwarf_location_atom op;
14773 if (size > DWARF2_ADDR_SIZE || size == -1)
14775 expansion_failed (loc, NULL_RTX,
14776 "DWARF address size mismatch");
14777 return 0;
14779 else if (size == DWARF2_ADDR_SIZE)
14780 op = DW_OP_deref;
14781 else
14782 op = DW_OP_deref_size;
14784 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14787 return descr;
14790 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14791 if it is not possible. */
14793 static dw_loc_descr_ref
14794 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14796 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14797 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14798 else if (dwarf_version >= 3 || !dwarf_strict)
14799 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14800 else
14801 return NULL;
14804 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14805 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14807 static dw_loc_descr_ref
14808 dw_sra_loc_expr (tree decl, rtx loc)
14810 rtx p;
14811 unsigned HOST_WIDE_INT padsize = 0;
14812 dw_loc_descr_ref descr, *descr_tail;
14813 unsigned HOST_WIDE_INT decl_size;
14814 rtx varloc;
14815 enum var_init_status initialized;
14817 if (DECL_SIZE (decl) == NULL
14818 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14819 return NULL;
14821 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14822 descr = NULL;
14823 descr_tail = &descr;
14825 for (p = loc; p; p = XEXP (p, 1))
14827 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14828 rtx loc_note = *decl_piece_varloc_ptr (p);
14829 dw_loc_descr_ref cur_descr;
14830 dw_loc_descr_ref *tail, last = NULL;
14831 unsigned HOST_WIDE_INT opsize = 0;
14833 if (loc_note == NULL_RTX
14834 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14836 padsize += bitsize;
14837 continue;
14839 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14840 varloc = NOTE_VAR_LOCATION (loc_note);
14841 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14842 if (cur_descr == NULL)
14844 padsize += bitsize;
14845 continue;
14848 /* Check that cur_descr either doesn't use
14849 DW_OP_*piece operations, or their sum is equal
14850 to bitsize. Otherwise we can't embed it. */
14851 for (tail = &cur_descr; *tail != NULL;
14852 tail = &(*tail)->dw_loc_next)
14853 if ((*tail)->dw_loc_opc == DW_OP_piece)
14855 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14856 * BITS_PER_UNIT;
14857 last = *tail;
14859 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14861 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14862 last = *tail;
14865 if (last != NULL && opsize != bitsize)
14867 padsize += bitsize;
14868 /* Discard the current piece of the descriptor and release any
14869 addr_table entries it uses. */
14870 remove_loc_list_addr_table_entries (cur_descr);
14871 continue;
14874 /* If there is a hole, add DW_OP_*piece after empty DWARF
14875 expression, which means that those bits are optimized out. */
14876 if (padsize)
14878 if (padsize > decl_size)
14880 remove_loc_list_addr_table_entries (cur_descr);
14881 goto discard_descr;
14883 decl_size -= padsize;
14884 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14885 if (*descr_tail == NULL)
14887 remove_loc_list_addr_table_entries (cur_descr);
14888 goto discard_descr;
14890 descr_tail = &(*descr_tail)->dw_loc_next;
14891 padsize = 0;
14893 *descr_tail = cur_descr;
14894 descr_tail = tail;
14895 if (bitsize > decl_size)
14896 goto discard_descr;
14897 decl_size -= bitsize;
14898 if (last == NULL)
14900 HOST_WIDE_INT offset = 0;
14901 if (GET_CODE (varloc) == VAR_LOCATION
14902 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14904 varloc = PAT_VAR_LOCATION_LOC (varloc);
14905 if (GET_CODE (varloc) == EXPR_LIST)
14906 varloc = XEXP (varloc, 0);
14910 if (GET_CODE (varloc) == CONST
14911 || GET_CODE (varloc) == SIGN_EXTEND
14912 || GET_CODE (varloc) == ZERO_EXTEND)
14913 varloc = XEXP (varloc, 0);
14914 else if (GET_CODE (varloc) == SUBREG)
14915 varloc = SUBREG_REG (varloc);
14916 else
14917 break;
14919 while (1);
14920 /* DW_OP_bit_size offset should be zero for register
14921 or implicit location descriptions and empty location
14922 descriptions, but for memory addresses needs big endian
14923 adjustment. */
14924 if (MEM_P (varloc))
14926 unsigned HOST_WIDE_INT memsize
14927 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14928 if (memsize != bitsize)
14930 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14931 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14932 goto discard_descr;
14933 if (memsize < bitsize)
14934 goto discard_descr;
14935 if (BITS_BIG_ENDIAN)
14936 offset = memsize - bitsize;
14940 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14941 if (*descr_tail == NULL)
14942 goto discard_descr;
14943 descr_tail = &(*descr_tail)->dw_loc_next;
14947 /* If there were any non-empty expressions, add padding till the end of
14948 the decl. */
14949 if (descr != NULL && decl_size != 0)
14951 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14952 if (*descr_tail == NULL)
14953 goto discard_descr;
14955 return descr;
14957 discard_descr:
14958 /* Discard the descriptor and release any addr_table entries it uses. */
14959 remove_loc_list_addr_table_entries (descr);
14960 return NULL;
14963 /* Return the dwarf representation of the location list LOC_LIST of
14964 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14965 function. */
14967 static dw_loc_list_ref
14968 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14970 const char *endname, *secname;
14971 rtx varloc;
14972 enum var_init_status initialized;
14973 struct var_loc_node *node;
14974 dw_loc_descr_ref descr;
14975 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14976 dw_loc_list_ref list = NULL;
14977 dw_loc_list_ref *listp = &list;
14979 /* Now that we know what section we are using for a base,
14980 actually construct the list of locations.
14981 The first location information is what is passed to the
14982 function that creates the location list, and the remaining
14983 locations just get added on to that list.
14984 Note that we only know the start address for a location
14985 (IE location changes), so to build the range, we use
14986 the range [current location start, next location start].
14987 This means we have to special case the last node, and generate
14988 a range of [last location start, end of function label]. */
14990 secname = secname_for_decl (decl);
14992 for (node = loc_list->first; node; node = node->next)
14993 if (GET_CODE (node->loc) == EXPR_LIST
14994 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14996 if (GET_CODE (node->loc) == EXPR_LIST)
14998 /* This requires DW_OP_{,bit_}piece, which is not usable
14999 inside DWARF expressions. */
15000 if (want_address != 2)
15001 continue;
15002 descr = dw_sra_loc_expr (decl, node->loc);
15003 if (descr == NULL)
15004 continue;
15006 else
15008 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15009 varloc = NOTE_VAR_LOCATION (node->loc);
15010 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15012 if (descr)
15014 bool range_across_switch = false;
15015 /* If section switch happens in between node->label
15016 and node->next->label (or end of function) and
15017 we can't emit it as a single entry list,
15018 emit two ranges, first one ending at the end
15019 of first partition and second one starting at the
15020 beginning of second partition. */
15021 if (node == loc_list->last_before_switch
15022 && (node != loc_list->first || loc_list->first->next)
15023 && current_function_decl)
15025 endname = cfun->fde->dw_fde_end;
15026 range_across_switch = true;
15028 /* The variable has a location between NODE->LABEL and
15029 NODE->NEXT->LABEL. */
15030 else if (node->next)
15031 endname = node->next->label;
15032 /* If the variable has a location at the last label
15033 it keeps its location until the end of function. */
15034 else if (!current_function_decl)
15035 endname = text_end_label;
15036 else
15038 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15039 current_function_funcdef_no);
15040 endname = ggc_strdup (label_id);
15043 *listp = new_loc_list (descr, node->label, endname, secname);
15044 if (TREE_CODE (decl) == PARM_DECL
15045 && node == loc_list->first
15046 && NOTE_P (node->loc)
15047 && strcmp (node->label, endname) == 0)
15048 (*listp)->force = true;
15049 listp = &(*listp)->dw_loc_next;
15051 if (range_across_switch)
15053 if (GET_CODE (node->loc) == EXPR_LIST)
15054 descr = dw_sra_loc_expr (decl, node->loc);
15055 else
15057 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15058 varloc = NOTE_VAR_LOCATION (node->loc);
15059 descr = dw_loc_list_1 (decl, varloc, want_address,
15060 initialized);
15062 gcc_assert (descr);
15063 /* The variable has a location between NODE->LABEL and
15064 NODE->NEXT->LABEL. */
15065 if (node->next)
15066 endname = node->next->label;
15067 else
15068 endname = cfun->fde->dw_fde_second_end;
15069 *listp = new_loc_list (descr,
15070 cfun->fde->dw_fde_second_begin,
15071 endname, secname);
15072 listp = &(*listp)->dw_loc_next;
15077 /* Try to avoid the overhead of a location list emitting a location
15078 expression instead, but only if we didn't have more than one
15079 location entry in the first place. If some entries were not
15080 representable, we don't want to pretend a single entry that was
15081 applies to the entire scope in which the variable is
15082 available. */
15083 if (list && loc_list->first->next)
15084 gen_llsym (list);
15086 return list;
15089 /* Return if the loc_list has only single element and thus can be represented
15090 as location description. */
15092 static bool
15093 single_element_loc_list_p (dw_loc_list_ref list)
15095 gcc_assert (!list->dw_loc_next || list->ll_symbol);
15096 return !list->ll_symbol;
15099 /* To each location in list LIST add loc descr REF. */
15101 static void
15102 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15104 dw_loc_descr_ref copy;
15105 add_loc_descr (&list->expr, ref);
15106 list = list->dw_loc_next;
15107 while (list)
15109 copy = ggc_alloc<dw_loc_descr_node> ();
15110 memcpy (copy, ref, sizeof (dw_loc_descr_node));
15111 add_loc_descr (&list->expr, copy);
15112 while (copy->dw_loc_next)
15114 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
15115 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15116 copy->dw_loc_next = new_copy;
15117 copy = new_copy;
15119 list = list->dw_loc_next;
15123 /* Given two lists RET and LIST
15124 produce location list that is result of adding expression in LIST
15125 to expression in RET on each position in program.
15126 Might be destructive on both RET and LIST.
15128 TODO: We handle only simple cases of RET or LIST having at most one
15129 element. General case would inolve sorting the lists in program order
15130 and merging them that will need some additional work.
15131 Adding that will improve quality of debug info especially for SRA-ed
15132 structures. */
15134 static void
15135 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15137 if (!list)
15138 return;
15139 if (!*ret)
15141 *ret = list;
15142 return;
15144 if (!list->dw_loc_next)
15146 add_loc_descr_to_each (*ret, list->expr);
15147 return;
15149 if (!(*ret)->dw_loc_next)
15151 add_loc_descr_to_each (list, (*ret)->expr);
15152 *ret = list;
15153 return;
15155 expansion_failed (NULL_TREE, NULL_RTX,
15156 "Don't know how to merge two non-trivial"
15157 " location lists.\n");
15158 *ret = NULL;
15159 return;
15162 /* LOC is constant expression. Try a luck, look it up in constant
15163 pool and return its loc_descr of its address. */
15165 static dw_loc_descr_ref
15166 cst_pool_loc_descr (tree loc)
15168 /* Get an RTL for this, if something has been emitted. */
15169 rtx rtl = lookup_constant_def (loc);
15171 if (!rtl || !MEM_P (rtl))
15173 gcc_assert (!rtl);
15174 return 0;
15176 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15178 /* TODO: We might get more coverage if we was actually delaying expansion
15179 of all expressions till end of compilation when constant pools are fully
15180 populated. */
15181 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15183 expansion_failed (loc, NULL_RTX,
15184 "CST value in contant pool but not marked.");
15185 return 0;
15187 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15188 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15191 /* Return dw_loc_list representing address of addr_expr LOC
15192 by looking for inner INDIRECT_REF expression and turning
15193 it into simple arithmetics.
15195 See loc_list_from_tree for the meaning of CONTEXT. */
15197 static dw_loc_list_ref
15198 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
15199 const loc_descr_context *context)
15201 tree obj, offset;
15202 HOST_WIDE_INT bitsize, bitpos, bytepos;
15203 machine_mode mode;
15204 int unsignedp, reversep, volatilep = 0;
15205 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15207 obj = get_inner_reference (TREE_OPERAND (loc, 0),
15208 &bitsize, &bitpos, &offset, &mode,
15209 &unsignedp, &reversep, &volatilep);
15210 STRIP_NOPS (obj);
15211 if (bitpos % BITS_PER_UNIT)
15213 expansion_failed (loc, NULL_RTX, "bitfield access");
15214 return 0;
15216 if (!INDIRECT_REF_P (obj))
15218 expansion_failed (obj,
15219 NULL_RTX, "no indirect ref in inner refrence");
15220 return 0;
15222 if (!offset && !bitpos)
15223 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
15224 context);
15225 else if (toplev
15226 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15227 && (dwarf_version >= 4 || !dwarf_strict))
15229 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
15230 if (!list_ret)
15231 return 0;
15232 if (offset)
15234 /* Variable offset. */
15235 list_ret1 = loc_list_from_tree (offset, 0, context);
15236 if (list_ret1 == 0)
15237 return 0;
15238 add_loc_list (&list_ret, list_ret1);
15239 if (!list_ret)
15240 return 0;
15241 add_loc_descr_to_each (list_ret,
15242 new_loc_descr (DW_OP_plus, 0, 0));
15244 bytepos = bitpos / BITS_PER_UNIT;
15245 if (bytepos > 0)
15246 add_loc_descr_to_each (list_ret,
15247 new_loc_descr (DW_OP_plus_uconst,
15248 bytepos, 0));
15249 else if (bytepos < 0)
15250 loc_list_plus_const (list_ret, bytepos);
15251 add_loc_descr_to_each (list_ret,
15252 new_loc_descr (DW_OP_stack_value, 0, 0));
15254 return list_ret;
15257 /* Set LOC to the next operation that is not a DW_OP_nop operation. In the case
15258 all operations from LOC are nops, move to the last one. Insert in NOPS all
15259 operations that are skipped. */
15261 static void
15262 loc_descr_to_next_no_nop (dw_loc_descr_ref &loc,
15263 hash_set<dw_loc_descr_ref> &nops)
15265 while (loc->dw_loc_next != NULL && loc->dw_loc_opc == DW_OP_nop)
15267 nops.add (loc);
15268 loc = loc->dw_loc_next;
15272 /* Helper for loc_descr_without_nops: free the location description operation
15273 P. */
15275 bool
15276 free_loc_descr (const dw_loc_descr_ref &loc, void *data ATTRIBUTE_UNUSED)
15278 ggc_free (loc);
15279 return true;
15282 /* Remove all DW_OP_nop operations from LOC except, if it exists, the one that
15283 finishes LOC. */
15285 static void
15286 loc_descr_without_nops (dw_loc_descr_ref &loc)
15288 if (loc->dw_loc_opc == DW_OP_nop && loc->dw_loc_next == NULL)
15289 return;
15291 /* Set of all DW_OP_nop operations we remove. */
15292 hash_set<dw_loc_descr_ref> nops;
15294 /* First, strip all prefix NOP operations in order to keep the head of the
15295 operations list. */
15296 loc_descr_to_next_no_nop (loc, nops);
15298 for (dw_loc_descr_ref cur = loc; cur != NULL;)
15300 /* For control flow operations: strip "prefix" nops in destination
15301 labels. */
15302 if (cur->dw_loc_oprnd1.val_class == dw_val_class_loc)
15303 loc_descr_to_next_no_nop (cur->dw_loc_oprnd1.v.val_loc, nops);
15304 if (cur->dw_loc_oprnd2.val_class == dw_val_class_loc)
15305 loc_descr_to_next_no_nop (cur->dw_loc_oprnd2.v.val_loc, nops);
15307 /* Do the same for the operations that follow, then move to the next
15308 iteration. */
15309 if (cur->dw_loc_next != NULL)
15310 loc_descr_to_next_no_nop (cur->dw_loc_next, nops);
15311 cur = cur->dw_loc_next;
15314 nops.traverse<void *, free_loc_descr> (NULL);
15318 struct dwarf_procedure_info;
15320 /* Helper structure for location descriptions generation. */
15321 struct loc_descr_context
15323 /* The type that is implicitly referenced by DW_OP_push_object_address, or
15324 NULL_TREE if DW_OP_push_object_address in invalid for this location
15325 description. This is used when processing PLACEHOLDER_EXPR nodes. */
15326 tree context_type;
15327 /* The ..._DECL node that should be translated as a
15328 DW_OP_push_object_address operation. */
15329 tree base_decl;
15330 /* Information about the DWARF procedure we are currently generating. NULL if
15331 we are not generating a DWARF procedure. */
15332 struct dwarf_procedure_info *dpi;
15335 /* DWARF procedures generation
15337 DWARF expressions (aka. location descriptions) are used to encode variable
15338 things such as sizes or offsets. Such computations can have redundant parts
15339 that can be factorized in order to reduce the size of the output debug
15340 information. This is the whole point of DWARF procedures.
15342 Thanks to stor-layout.c, size and offset expressions in GENERIC trees are
15343 already factorized into functions ("size functions") in order to handle very
15344 big and complex types. Such functions are quite simple: they have integral
15345 arguments, they return an integral result and their body contains only a
15346 return statement with arithmetic expressions. This is the only kind of
15347 function we are interested in translating into DWARF procedures, here.
15349 DWARF expressions and DWARF procedure are executed using a stack, so we have
15350 to define some calling convention for them to interact. Let's say that:
15352 - Before calling a DWARF procedure, DWARF expressions must push on the stack
15353 all arguments in reverse order (right-to-left) so that when the DWARF
15354 procedure execution starts, the first argument is the top of the stack.
15356 - Then, when returning, the DWARF procedure must have consumed all arguments
15357 on the stack, must have pushed the result and touched nothing else.
15359 - Each integral argument and the result are integral types can be hold in a
15360 single stack slot.
15362 - We call "frame offset" the number of stack slots that are "under DWARF
15363 procedure control": it includes the arguments slots, the temporaries and
15364 the result slot. Thus, it is equal to the number of arguments when the
15365 procedure execution starts and must be equal to one (the result) when it
15366 returns. */
15368 /* Helper structure used when generating operations for a DWARF procedure. */
15369 struct dwarf_procedure_info
15371 /* The FUNCTION_DECL node corresponding to the DWARF procedure that is
15372 currently translated. */
15373 tree fndecl;
15374 /* The number of arguments FNDECL takes. */
15375 unsigned args_count;
15378 /* Return a pointer to a newly created DIE node for a DWARF procedure. Add
15379 LOCATION as its DW_AT_location attribute. If FNDECL is not NULL_TREE,
15380 equate it to this DIE. */
15382 static dw_die_ref
15383 new_dwarf_proc_die (dw_loc_descr_ref location, tree fndecl,
15384 dw_die_ref parent_die)
15386 dw_die_ref dwarf_proc_die;
15388 if ((dwarf_version < 3 && dwarf_strict)
15389 || location == NULL)
15390 return NULL;
15392 dwarf_proc_die = new_die (DW_TAG_dwarf_procedure, parent_die, fndecl);
15393 if (fndecl)
15394 equate_decl_number_to_die (fndecl, dwarf_proc_die);
15395 add_AT_loc (dwarf_proc_die, DW_AT_location, location);
15396 return dwarf_proc_die;
15399 /* Return whether TYPE is a supported type as a DWARF procedure argument
15400 type or return type (we handle only scalar types and pointer types that
15401 aren't wider than the DWARF expression evaluation stack. */
15403 static bool
15404 is_handled_procedure_type (tree type)
15406 return ((INTEGRAL_TYPE_P (type)
15407 || TREE_CODE (type) == OFFSET_TYPE
15408 || TREE_CODE (type) == POINTER_TYPE)
15409 && int_size_in_bytes (type) <= DWARF2_ADDR_SIZE);
15412 /* Helper for resolve_args_picking: do the same but stop when coming across
15413 visited nodes. For each node we visit, register in FRAME_OFFSETS the frame
15414 offset *before* evaluating the corresponding operation. */
15416 static bool
15417 resolve_args_picking_1 (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15418 struct dwarf_procedure_info *dpi,
15419 hash_map<dw_loc_descr_ref, unsigned> &frame_offsets)
15421 /* The "frame_offset" identifier is already used to name a macro... */
15422 unsigned frame_offset_ = initial_frame_offset;
15423 dw_loc_descr_ref l;
15425 for (l = loc; l != NULL;)
15427 bool existed;
15428 unsigned &l_frame_offset = frame_offsets.get_or_insert (l, &existed);
15430 /* If we already met this node, there is nothing to compute anymore. */
15431 if (existed)
15433 /* Make sure that the stack size is consistent wherever the execution
15434 flow comes from. */
15435 gcc_assert ((unsigned) l_frame_offset == frame_offset_);
15436 break;
15438 l_frame_offset = frame_offset_;
15440 /* If needed, relocate the picking offset with respect to the frame
15441 offset. */
15442 if (l->dw_loc_opc == DW_OP_pick && l->frame_offset_rel)
15444 /* frame_offset_ is the size of the current stack frame, including
15445 incoming arguments. Besides, the arguments are pushed
15446 right-to-left. Thus, in order to access the Nth argument from
15447 this operation node, the picking has to skip temporaries *plus*
15448 one stack slot per argument (0 for the first one, 1 for the second
15449 one, etc.).
15451 The targetted argument number (N) is already set as the operand,
15452 and the number of temporaries can be computed with:
15453 frame_offsets_ - dpi->args_count */
15454 l->dw_loc_oprnd1.v.val_unsigned += frame_offset_ - dpi->args_count;
15456 /* DW_OP_pick handles only offsets from 0 to 255 (inclusive)... */
15457 if (l->dw_loc_oprnd1.v.val_unsigned > 255)
15458 return false;
15461 /* Update frame_offset according to the effect the current operation has
15462 on the stack. */
15463 switch (l->dw_loc_opc)
15465 case DW_OP_deref:
15466 case DW_OP_swap:
15467 case DW_OP_rot:
15468 case DW_OP_abs:
15469 case DW_OP_neg:
15470 case DW_OP_not:
15471 case DW_OP_plus_uconst:
15472 case DW_OP_skip:
15473 case DW_OP_reg0:
15474 case DW_OP_reg1:
15475 case DW_OP_reg2:
15476 case DW_OP_reg3:
15477 case DW_OP_reg4:
15478 case DW_OP_reg5:
15479 case DW_OP_reg6:
15480 case DW_OP_reg7:
15481 case DW_OP_reg8:
15482 case DW_OP_reg9:
15483 case DW_OP_reg10:
15484 case DW_OP_reg11:
15485 case DW_OP_reg12:
15486 case DW_OP_reg13:
15487 case DW_OP_reg14:
15488 case DW_OP_reg15:
15489 case DW_OP_reg16:
15490 case DW_OP_reg17:
15491 case DW_OP_reg18:
15492 case DW_OP_reg19:
15493 case DW_OP_reg20:
15494 case DW_OP_reg21:
15495 case DW_OP_reg22:
15496 case DW_OP_reg23:
15497 case DW_OP_reg24:
15498 case DW_OP_reg25:
15499 case DW_OP_reg26:
15500 case DW_OP_reg27:
15501 case DW_OP_reg28:
15502 case DW_OP_reg29:
15503 case DW_OP_reg30:
15504 case DW_OP_reg31:
15505 case DW_OP_bregx:
15506 case DW_OP_piece:
15507 case DW_OP_deref_size:
15508 case DW_OP_nop:
15509 case DW_OP_form_tls_address:
15510 case DW_OP_bit_piece:
15511 case DW_OP_implicit_value:
15512 case DW_OP_stack_value:
15513 break;
15515 case DW_OP_addr:
15516 case DW_OP_const1u:
15517 case DW_OP_const1s:
15518 case DW_OP_const2u:
15519 case DW_OP_const2s:
15520 case DW_OP_const4u:
15521 case DW_OP_const4s:
15522 case DW_OP_const8u:
15523 case DW_OP_const8s:
15524 case DW_OP_constu:
15525 case DW_OP_consts:
15526 case DW_OP_dup:
15527 case DW_OP_over:
15528 case DW_OP_pick:
15529 case DW_OP_lit0:
15530 case DW_OP_lit1:
15531 case DW_OP_lit2:
15532 case DW_OP_lit3:
15533 case DW_OP_lit4:
15534 case DW_OP_lit5:
15535 case DW_OP_lit6:
15536 case DW_OP_lit7:
15537 case DW_OP_lit8:
15538 case DW_OP_lit9:
15539 case DW_OP_lit10:
15540 case DW_OP_lit11:
15541 case DW_OP_lit12:
15542 case DW_OP_lit13:
15543 case DW_OP_lit14:
15544 case DW_OP_lit15:
15545 case DW_OP_lit16:
15546 case DW_OP_lit17:
15547 case DW_OP_lit18:
15548 case DW_OP_lit19:
15549 case DW_OP_lit20:
15550 case DW_OP_lit21:
15551 case DW_OP_lit22:
15552 case DW_OP_lit23:
15553 case DW_OP_lit24:
15554 case DW_OP_lit25:
15555 case DW_OP_lit26:
15556 case DW_OP_lit27:
15557 case DW_OP_lit28:
15558 case DW_OP_lit29:
15559 case DW_OP_lit30:
15560 case DW_OP_lit31:
15561 case DW_OP_breg0:
15562 case DW_OP_breg1:
15563 case DW_OP_breg2:
15564 case DW_OP_breg3:
15565 case DW_OP_breg4:
15566 case DW_OP_breg5:
15567 case DW_OP_breg6:
15568 case DW_OP_breg7:
15569 case DW_OP_breg8:
15570 case DW_OP_breg9:
15571 case DW_OP_breg10:
15572 case DW_OP_breg11:
15573 case DW_OP_breg12:
15574 case DW_OP_breg13:
15575 case DW_OP_breg14:
15576 case DW_OP_breg15:
15577 case DW_OP_breg16:
15578 case DW_OP_breg17:
15579 case DW_OP_breg18:
15580 case DW_OP_breg19:
15581 case DW_OP_breg20:
15582 case DW_OP_breg21:
15583 case DW_OP_breg22:
15584 case DW_OP_breg23:
15585 case DW_OP_breg24:
15586 case DW_OP_breg25:
15587 case DW_OP_breg26:
15588 case DW_OP_breg27:
15589 case DW_OP_breg28:
15590 case DW_OP_breg29:
15591 case DW_OP_breg30:
15592 case DW_OP_breg31:
15593 case DW_OP_fbreg:
15594 case DW_OP_push_object_address:
15595 case DW_OP_call_frame_cfa:
15596 ++frame_offset_;
15597 break;
15599 case DW_OP_drop:
15600 case DW_OP_xderef:
15601 case DW_OP_and:
15602 case DW_OP_div:
15603 case DW_OP_minus:
15604 case DW_OP_mod:
15605 case DW_OP_mul:
15606 case DW_OP_or:
15607 case DW_OP_plus:
15608 case DW_OP_shl:
15609 case DW_OP_shr:
15610 case DW_OP_shra:
15611 case DW_OP_xor:
15612 case DW_OP_bra:
15613 case DW_OP_eq:
15614 case DW_OP_ge:
15615 case DW_OP_gt:
15616 case DW_OP_le:
15617 case DW_OP_lt:
15618 case DW_OP_ne:
15619 case DW_OP_regx:
15620 case DW_OP_xderef_size:
15621 --frame_offset_;
15622 break;
15624 case DW_OP_call2:
15625 case DW_OP_call4:
15626 case DW_OP_call_ref:
15628 dw_die_ref dwarf_proc = l->dw_loc_oprnd1.v.val_die_ref.die;
15629 int *stack_usage = dwarf_proc_stack_usage_map->get (dwarf_proc);
15631 if (stack_usage == NULL)
15632 return false;
15633 frame_offset_ += *stack_usage;
15634 break;
15637 case DW_OP_GNU_push_tls_address:
15638 case DW_OP_GNU_uninit:
15639 case DW_OP_GNU_encoded_addr:
15640 case DW_OP_GNU_implicit_pointer:
15641 case DW_OP_GNU_entry_value:
15642 case DW_OP_GNU_const_type:
15643 case DW_OP_GNU_regval_type:
15644 case DW_OP_GNU_deref_type:
15645 case DW_OP_GNU_convert:
15646 case DW_OP_GNU_reinterpret:
15647 case DW_OP_GNU_parameter_ref:
15648 /* loc_list_from_tree will probably not output these operations for
15649 size functions, so assume they will not appear here. */
15650 /* Fall through... */
15652 default:
15653 gcc_unreachable ();
15656 /* Now, follow the control flow (except subroutine calls). */
15657 switch (l->dw_loc_opc)
15659 case DW_OP_bra:
15660 if (!resolve_args_picking_1 (l->dw_loc_next, frame_offset_, dpi,
15661 frame_offsets))
15662 return false;
15663 /* Fall through. */
15665 case DW_OP_skip:
15666 l = l->dw_loc_oprnd1.v.val_loc;
15667 break;
15669 case DW_OP_stack_value:
15670 return true;
15672 default:
15673 l = l->dw_loc_next;
15674 break;
15678 return true;
15681 /* Make a DFS over operations reachable through LOC (i.e. follow branch
15682 operations) in order to resolve the operand of DW_OP_pick operations that
15683 target DWARF procedure arguments (DPI). INITIAL_FRAME_OFFSET is the frame
15684 offset *before* LOC is executed. Return if all relocations were
15685 successful. */
15687 static bool
15688 resolve_args_picking (dw_loc_descr_ref loc, unsigned initial_frame_offset,
15689 struct dwarf_procedure_info *dpi)
15691 /* Associate to all visited operations the frame offset *before* evaluating
15692 this operation. */
15693 hash_map<dw_loc_descr_ref, unsigned> frame_offsets;
15695 return resolve_args_picking_1 (loc, initial_frame_offset, dpi,
15696 frame_offsets);
15699 /* Try to generate a DWARF procedure that computes the same result as FNDECL.
15700 Return NULL if it is not possible. */
15702 static dw_die_ref
15703 function_to_dwarf_procedure (tree fndecl)
15705 struct loc_descr_context ctx;
15706 struct dwarf_procedure_info dpi;
15707 dw_die_ref dwarf_proc_die;
15708 tree tree_body = DECL_SAVED_TREE (fndecl);
15709 dw_loc_descr_ref loc_body, epilogue;
15711 tree cursor;
15712 unsigned i;
15714 /* Do not generate multiple DWARF procedures for the same function
15715 declaration. */
15716 dwarf_proc_die = lookup_decl_die (fndecl);
15717 if (dwarf_proc_die != NULL)
15718 return dwarf_proc_die;
15720 /* DWARF procedures are available starting with the DWARFv3 standard. */
15721 if (dwarf_version < 3 && dwarf_strict)
15722 return NULL;
15724 /* We handle only functions for which we still have a body, that return a
15725 supported type and that takes arguments with supported types. Note that
15726 there is no point translating functions that return nothing. */
15727 if (tree_body == NULL_TREE
15728 || DECL_RESULT (fndecl) == NULL_TREE
15729 || !is_handled_procedure_type (TREE_TYPE (DECL_RESULT (fndecl))))
15730 return NULL;
15732 for (cursor = DECL_ARGUMENTS (fndecl);
15733 cursor != NULL_TREE;
15734 cursor = TREE_CHAIN (cursor))
15735 if (!is_handled_procedure_type (TREE_TYPE (cursor)))
15736 return NULL;
15738 /* Match only "expr" in: RETURN_EXPR (MODIFY_EXPR (RESULT_DECL, expr)). */
15739 if (TREE_CODE (tree_body) != RETURN_EXPR)
15740 return NULL;
15741 tree_body = TREE_OPERAND (tree_body, 0);
15742 if (TREE_CODE (tree_body) != MODIFY_EXPR
15743 || TREE_OPERAND (tree_body, 0) != DECL_RESULT (fndecl))
15744 return NULL;
15745 tree_body = TREE_OPERAND (tree_body, 1);
15747 /* Try to translate the body expression itself. Note that this will probably
15748 cause an infinite recursion if its call graph has a cycle. This is very
15749 unlikely for size functions, however, so don't bother with such things at
15750 the moment. */
15751 ctx.context_type = NULL_TREE;
15752 ctx.base_decl = NULL_TREE;
15753 ctx.dpi = &dpi;
15754 dpi.fndecl = fndecl;
15755 dpi.args_count = list_length (DECL_ARGUMENTS (fndecl));
15756 loc_body = loc_descriptor_from_tree (tree_body, 0, &ctx);
15757 if (!loc_body)
15758 return NULL;
15760 /* After evaluating all operands in "loc_body", we should still have on the
15761 stack all arguments plus the desired function result (top of the stack).
15762 Generate code in order to keep only the result in our stack frame. */
15763 epilogue = NULL;
15764 for (i = 0; i < dpi.args_count; ++i)
15766 dw_loc_descr_ref op_couple = new_loc_descr (DW_OP_swap, 0, 0);
15767 op_couple->dw_loc_next = new_loc_descr (DW_OP_drop, 0, 0);
15768 op_couple->dw_loc_next->dw_loc_next = epilogue;
15769 epilogue = op_couple;
15771 add_loc_descr (&loc_body, epilogue);
15772 if (!resolve_args_picking (loc_body, dpi.args_count, &dpi))
15773 return NULL;
15775 /* Trailing nops from loc_descriptor_from_tree (if any) cannot be removed
15776 because they are considered useful. Now there is an epilogue, they are
15777 not anymore, so give it another try. */
15778 loc_descr_without_nops (loc_body);
15780 /* fndecl may be used both as a regular DW_TAG_subprogram DIE and as
15781 a DW_TAG_dwarf_procedure, so we may have a conflict, here. It's unlikely,
15782 though, given that size functions do not come from source, so they should
15783 not have a dedicated DW_TAG_subprogram DIE. */
15784 dwarf_proc_die
15785 = new_dwarf_proc_die (loc_body, fndecl,
15786 get_context_die (DECL_CONTEXT (fndecl)));
15788 /* The called DWARF procedure consumes one stack slot per argument and
15789 returns one stack slot. */
15790 dwarf_proc_stack_usage_map->put (dwarf_proc_die, 1 - dpi.args_count);
15792 return dwarf_proc_die;
15796 /* Generate Dwarf location list representing LOC.
15797 If WANT_ADDRESS is false, expression computing LOC will be computed
15798 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15799 if WANT_ADDRESS is 2, expression computing address useable in location
15800 will be returned (i.e. DW_OP_reg can be used
15801 to refer to register values).
15803 CONTEXT provides information to customize the location descriptions
15804 generation. Its context_type field specifies what type is implicitly
15805 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
15806 will not be generated.
15808 Its DPI field determines whether we are generating a DWARF expression for a
15809 DWARF procedure, so PARM_DECL references are processed specifically.
15811 If CONTEXT is NULL, the behavior is the same as if context_type, base_decl
15812 and dpi fields were null. */
15814 static dw_loc_list_ref
15815 loc_list_from_tree_1 (tree loc, int want_address,
15816 const struct loc_descr_context *context)
15818 dw_loc_descr_ref ret = NULL, ret1 = NULL;
15819 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15820 int have_address = 0;
15821 enum dwarf_location_atom op;
15823 /* ??? Most of the time we do not take proper care for sign/zero
15824 extending the values properly. Hopefully this won't be a real
15825 problem... */
15827 if (context != NULL
15828 && context->base_decl == loc
15829 && want_address == 0)
15831 if (dwarf_version >= 3 || !dwarf_strict)
15832 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
15833 NULL, NULL, NULL);
15834 else
15835 return NULL;
15838 switch (TREE_CODE (loc))
15840 case ERROR_MARK:
15841 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15842 return 0;
15844 case PLACEHOLDER_EXPR:
15845 /* This case involves extracting fields from an object to determine the
15846 position of other fields. It is supposed to appear only as the first
15847 operand of COMPONENT_REF nodes and to reference precisely the type
15848 that the context allows. */
15849 if (context != NULL
15850 && TREE_TYPE (loc) == context->context_type
15851 && want_address >= 1)
15853 if (dwarf_version >= 3 || !dwarf_strict)
15855 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
15856 have_address = 1;
15857 break;
15859 else
15860 return NULL;
15862 else
15863 expansion_failed (loc, NULL_RTX,
15864 "PLACEHOLDER_EXPR for an unexpected type");
15865 break;
15867 case CALL_EXPR:
15869 const int nargs = call_expr_nargs (loc);
15870 tree callee = get_callee_fndecl (loc);
15871 int i;
15872 dw_die_ref dwarf_proc;
15874 if (callee == NULL_TREE)
15875 goto call_expansion_failed;
15877 /* We handle only functions that return an integer. */
15878 if (!is_handled_procedure_type (TREE_TYPE (TREE_TYPE (callee))))
15879 goto call_expansion_failed;
15881 dwarf_proc = function_to_dwarf_procedure (callee);
15882 if (dwarf_proc == NULL)
15883 goto call_expansion_failed;
15885 /* Evaluate arguments right-to-left so that the first argument will
15886 be the top-most one on the stack. */
15887 for (i = nargs - 1; i >= 0; --i)
15889 dw_loc_descr_ref loc_descr
15890 = loc_descriptor_from_tree (CALL_EXPR_ARG (loc, i), 0,
15891 context);
15893 if (loc_descr == NULL)
15894 goto call_expansion_failed;
15896 add_loc_descr (&ret, loc_descr);
15899 ret1 = new_loc_descr (DW_OP_call4, 0, 0);
15900 ret1->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15901 ret1->dw_loc_oprnd1.v.val_die_ref.die = dwarf_proc;
15902 ret1->dw_loc_oprnd1.v.val_die_ref.external = 0;
15903 add_loc_descr (&ret, ret1);
15904 break;
15906 call_expansion_failed:
15907 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15908 /* There are no opcodes for these operations. */
15909 return 0;
15912 case PREINCREMENT_EXPR:
15913 case PREDECREMENT_EXPR:
15914 case POSTINCREMENT_EXPR:
15915 case POSTDECREMENT_EXPR:
15916 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15917 /* There are no opcodes for these operations. */
15918 return 0;
15920 case ADDR_EXPR:
15921 /* If we already want an address, see if there is INDIRECT_REF inside
15922 e.g. for &this->field. */
15923 if (want_address)
15925 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15926 (loc, want_address == 2, context);
15927 if (list_ret)
15928 have_address = 1;
15929 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15930 && (ret = cst_pool_loc_descr (loc)))
15931 have_address = 1;
15933 /* Otherwise, process the argument and look for the address. */
15934 if (!list_ret && !ret)
15935 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 1, context);
15936 else
15938 if (want_address)
15939 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15940 return NULL;
15942 break;
15944 case VAR_DECL:
15945 if (DECL_THREAD_LOCAL_P (loc))
15947 rtx rtl;
15948 enum dwarf_location_atom tls_op;
15949 enum dtprel_bool dtprel = dtprel_false;
15951 if (targetm.have_tls)
15953 /* If this is not defined, we have no way to emit the
15954 data. */
15955 if (!targetm.asm_out.output_dwarf_dtprel)
15956 return 0;
15958 /* The way DW_OP_GNU_push_tls_address is specified, we
15959 can only look up addresses of objects in the current
15960 module. We used DW_OP_addr as first op, but that's
15961 wrong, because DW_OP_addr is relocated by the debug
15962 info consumer, while DW_OP_GNU_push_tls_address
15963 operand shouldn't be. */
15964 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15965 return 0;
15966 dtprel = dtprel_true;
15967 tls_op = DW_OP_GNU_push_tls_address;
15969 else
15971 if (!targetm.emutls.debug_form_tls_address
15972 || !(dwarf_version >= 3 || !dwarf_strict))
15973 return 0;
15974 /* We stuffed the control variable into the DECL_VALUE_EXPR
15975 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15976 no longer appear in gimple code. We used the control
15977 variable in specific so that we could pick it up here. */
15978 loc = DECL_VALUE_EXPR (loc);
15979 tls_op = DW_OP_form_tls_address;
15982 rtl = rtl_for_decl_location (loc);
15983 if (rtl == NULL_RTX)
15984 return 0;
15986 if (!MEM_P (rtl))
15987 return 0;
15988 rtl = XEXP (rtl, 0);
15989 if (! CONSTANT_P (rtl))
15990 return 0;
15992 ret = new_addr_loc_descr (rtl, dtprel);
15993 ret1 = new_loc_descr (tls_op, 0, 0);
15994 add_loc_descr (&ret, ret1);
15996 have_address = 1;
15997 break;
15999 /* FALLTHRU */
16001 case PARM_DECL:
16002 if (context != NULL && context->dpi != NULL
16003 && DECL_CONTEXT (loc) == context->dpi->fndecl)
16005 /* We are generating code for a DWARF procedure and we want to access
16006 one of its arguments: find the appropriate argument offset and let
16007 the resolve_args_picking pass compute the offset that complies
16008 with the stack frame size. */
16009 unsigned i = 0;
16010 tree cursor;
16012 for (cursor = DECL_ARGUMENTS (context->dpi->fndecl);
16013 cursor != NULL_TREE && cursor != loc;
16014 cursor = TREE_CHAIN (cursor), ++i)
16016 /* If we are translating a DWARF procedure, all referenced parameters
16017 must belong to the current function. */
16018 gcc_assert (cursor != NULL_TREE);
16020 ret = new_loc_descr (DW_OP_pick, i, 0);
16021 ret->frame_offset_rel = 1;
16022 break;
16024 /* FALLTHRU */
16026 case RESULT_DECL:
16027 if (DECL_HAS_VALUE_EXPR_P (loc))
16028 return loc_list_from_tree_1 (DECL_VALUE_EXPR (loc),
16029 want_address, context);
16030 /* FALLTHRU */
16032 case FUNCTION_DECL:
16034 rtx rtl;
16035 var_loc_list *loc_list = lookup_decl_loc (loc);
16037 if (loc_list && loc_list->first)
16039 list_ret = dw_loc_list (loc_list, loc, want_address);
16040 have_address = want_address != 0;
16041 break;
16043 rtl = rtl_for_decl_location (loc);
16044 if (rtl == NULL_RTX)
16046 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16047 return 0;
16049 else if (CONST_INT_P (rtl))
16051 HOST_WIDE_INT val = INTVAL (rtl);
16052 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16053 val &= GET_MODE_MASK (DECL_MODE (loc));
16054 ret = int_loc_descriptor (val);
16056 else if (GET_CODE (rtl) == CONST_STRING)
16058 expansion_failed (loc, NULL_RTX, "CONST_STRING");
16059 return 0;
16061 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16062 ret = new_addr_loc_descr (rtl, dtprel_false);
16063 else
16065 machine_mode mode, mem_mode;
16067 /* Certain constructs can only be represented at top-level. */
16068 if (want_address == 2)
16070 ret = loc_descriptor (rtl, VOIDmode,
16071 VAR_INIT_STATUS_INITIALIZED);
16072 have_address = 1;
16074 else
16076 mode = GET_MODE (rtl);
16077 mem_mode = VOIDmode;
16078 if (MEM_P (rtl))
16080 mem_mode = mode;
16081 mode = get_address_mode (rtl);
16082 rtl = XEXP (rtl, 0);
16083 have_address = 1;
16085 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16086 VAR_INIT_STATUS_INITIALIZED);
16088 if (!ret)
16089 expansion_failed (loc, rtl,
16090 "failed to produce loc descriptor for rtl");
16093 break;
16095 case MEM_REF:
16096 if (!integer_zerop (TREE_OPERAND (loc, 1)))
16098 have_address = 1;
16099 goto do_plus;
16101 /* Fallthru. */
16102 case INDIRECT_REF:
16103 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16104 have_address = 1;
16105 break;
16107 case TARGET_MEM_REF:
16108 case SSA_NAME:
16109 case DEBUG_EXPR_DECL:
16110 return NULL;
16112 case COMPOUND_EXPR:
16113 return loc_list_from_tree_1 (TREE_OPERAND (loc, 1), want_address,
16114 context);
16116 CASE_CONVERT:
16117 case VIEW_CONVERT_EXPR:
16118 case SAVE_EXPR:
16119 case MODIFY_EXPR:
16120 case NON_LVALUE_EXPR:
16121 return loc_list_from_tree_1 (TREE_OPERAND (loc, 0), want_address,
16122 context);
16124 case COMPONENT_REF:
16125 case BIT_FIELD_REF:
16126 case ARRAY_REF:
16127 case ARRAY_RANGE_REF:
16128 case REALPART_EXPR:
16129 case IMAGPART_EXPR:
16131 tree obj, offset;
16132 HOST_WIDE_INT bitsize, bitpos, bytepos;
16133 machine_mode mode;
16134 int unsignedp, reversep, volatilep = 0;
16136 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16137 &unsignedp, &reversep, &volatilep);
16139 gcc_assert (obj != loc);
16141 list_ret = loc_list_from_tree_1 (obj,
16142 want_address == 2
16143 && !bitpos && !offset ? 2 : 1,
16144 context);
16145 /* TODO: We can extract value of the small expression via shifting even
16146 for nonzero bitpos. */
16147 if (list_ret == 0)
16148 return 0;
16149 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16151 expansion_failed (loc, NULL_RTX,
16152 "bitfield access");
16153 return 0;
16156 if (offset != NULL_TREE)
16158 /* Variable offset. */
16159 list_ret1 = loc_list_from_tree_1 (offset, 0, context);
16160 if (list_ret1 == 0)
16161 return 0;
16162 add_loc_list (&list_ret, list_ret1);
16163 if (!list_ret)
16164 return 0;
16165 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16168 bytepos = bitpos / BITS_PER_UNIT;
16169 if (bytepos > 0)
16170 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16171 else if (bytepos < 0)
16172 loc_list_plus_const (list_ret, bytepos);
16174 have_address = 1;
16175 break;
16178 case INTEGER_CST:
16179 if ((want_address || !tree_fits_shwi_p (loc))
16180 && (ret = cst_pool_loc_descr (loc)))
16181 have_address = 1;
16182 else if (want_address == 2
16183 && tree_fits_shwi_p (loc)
16184 && (ret = address_of_int_loc_descriptor
16185 (int_size_in_bytes (TREE_TYPE (loc)),
16186 tree_to_shwi (loc))))
16187 have_address = 1;
16188 else if (tree_fits_shwi_p (loc))
16189 ret = int_loc_descriptor (tree_to_shwi (loc));
16190 else if (tree_fits_uhwi_p (loc))
16191 ret = uint_loc_descriptor (tree_to_uhwi (loc));
16192 else
16194 expansion_failed (loc, NULL_RTX,
16195 "Integer operand is not host integer");
16196 return 0;
16198 break;
16200 case CONSTRUCTOR:
16201 case REAL_CST:
16202 case STRING_CST:
16203 case COMPLEX_CST:
16204 if ((ret = cst_pool_loc_descr (loc)))
16205 have_address = 1;
16206 else if (TREE_CODE (loc) == CONSTRUCTOR)
16208 tree type = TREE_TYPE (loc);
16209 unsigned HOST_WIDE_INT size = int_size_in_bytes (type);
16210 unsigned HOST_WIDE_INT offset = 0;
16211 unsigned HOST_WIDE_INT cnt;
16212 constructor_elt *ce;
16214 if (TREE_CODE (type) == RECORD_TYPE)
16216 /* This is very limited, but it's enough to output
16217 pointers to member functions, as long as the
16218 referenced function is defined in the current
16219 translation unit. */
16220 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (loc), cnt, ce)
16222 tree val = ce->value;
16224 tree field = ce->index;
16226 if (val)
16227 STRIP_NOPS (val);
16229 if (!field || DECL_BIT_FIELD (field))
16231 expansion_failed (loc, NULL_RTX,
16232 "bitfield in record type constructor");
16233 size = offset = (unsigned HOST_WIDE_INT)-1;
16234 ret = NULL;
16235 break;
16238 HOST_WIDE_INT fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16239 unsigned HOST_WIDE_INT pos = int_byte_position (field);
16240 gcc_assert (pos + fieldsize <= size);
16241 if (pos < offset)
16243 expansion_failed (loc, NULL_RTX,
16244 "out-of-order fields in record constructor");
16245 size = offset = (unsigned HOST_WIDE_INT)-1;
16246 ret = NULL;
16247 break;
16249 if (pos > offset)
16251 ret1 = new_loc_descr (DW_OP_piece, pos - offset, 0);
16252 add_loc_descr (&ret, ret1);
16253 offset = pos;
16255 if (val && fieldsize != 0)
16257 ret1 = loc_descriptor_from_tree (val, want_address, context);
16258 if (!ret1)
16260 expansion_failed (loc, NULL_RTX,
16261 "unsupported expression in field");
16262 size = offset = (unsigned HOST_WIDE_INT)-1;
16263 ret = NULL;
16264 break;
16266 add_loc_descr (&ret, ret1);
16268 if (fieldsize)
16270 ret1 = new_loc_descr (DW_OP_piece, fieldsize, 0);
16271 add_loc_descr (&ret, ret1);
16272 offset = pos + fieldsize;
16276 if (offset != size)
16278 ret1 = new_loc_descr (DW_OP_piece, size - offset, 0);
16279 add_loc_descr (&ret, ret1);
16280 offset = size;
16283 have_address = !!want_address;
16285 else
16286 expansion_failed (loc, NULL_RTX,
16287 "constructor of non-record type");
16289 else
16290 /* We can construct small constants here using int_loc_descriptor. */
16291 expansion_failed (loc, NULL_RTX,
16292 "constructor or constant not in constant pool");
16293 break;
16295 case TRUTH_AND_EXPR:
16296 case TRUTH_ANDIF_EXPR:
16297 case BIT_AND_EXPR:
16298 op = DW_OP_and;
16299 goto do_binop;
16301 case TRUTH_XOR_EXPR:
16302 case BIT_XOR_EXPR:
16303 op = DW_OP_xor;
16304 goto do_binop;
16306 case TRUTH_OR_EXPR:
16307 case TRUTH_ORIF_EXPR:
16308 case BIT_IOR_EXPR:
16309 op = DW_OP_or;
16310 goto do_binop;
16312 case FLOOR_DIV_EXPR:
16313 case CEIL_DIV_EXPR:
16314 case ROUND_DIV_EXPR:
16315 case TRUNC_DIV_EXPR:
16316 case EXACT_DIV_EXPR:
16317 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16318 return 0;
16319 op = DW_OP_div;
16320 goto do_binop;
16322 case MINUS_EXPR:
16323 op = DW_OP_minus;
16324 goto do_binop;
16326 case FLOOR_MOD_EXPR:
16327 case CEIL_MOD_EXPR:
16328 case ROUND_MOD_EXPR:
16329 case TRUNC_MOD_EXPR:
16330 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16332 op = DW_OP_mod;
16333 goto do_binop;
16335 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16336 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16337 if (list_ret == 0 || list_ret1 == 0)
16338 return 0;
16340 add_loc_list (&list_ret, list_ret1);
16341 if (list_ret == 0)
16342 return 0;
16343 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16344 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16345 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16346 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16347 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16348 break;
16350 case MULT_EXPR:
16351 op = DW_OP_mul;
16352 goto do_binop;
16354 case LSHIFT_EXPR:
16355 op = DW_OP_shl;
16356 goto do_binop;
16358 case RSHIFT_EXPR:
16359 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16360 goto do_binop;
16362 case POINTER_PLUS_EXPR:
16363 case PLUS_EXPR:
16364 do_plus:
16365 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
16367 /* Big unsigned numbers can fit in HOST_WIDE_INT but it may be
16368 smarter to encode their opposite. The DW_OP_plus_uconst operation
16369 takes 1 + X bytes, X being the size of the ULEB128 addend. On the
16370 other hand, a "<push literal>; DW_OP_minus" pattern takes 1 + Y
16371 bytes, Y being the size of the operation that pushes the opposite
16372 of the addend. So let's choose the smallest representation. */
16373 const tree tree_addend = TREE_OPERAND (loc, 1);
16374 offset_int wi_addend;
16375 HOST_WIDE_INT shwi_addend;
16376 dw_loc_descr_ref loc_naddend;
16378 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16379 if (list_ret == 0)
16380 return 0;
16382 /* Try to get the literal to push. It is the opposite of the addend,
16383 so as we rely on wrapping during DWARF evaluation, first decode
16384 the literal as a "DWARF-sized" signed number. */
16385 wi_addend = wi::to_offset (tree_addend);
16386 wi_addend = wi::sext (wi_addend, DWARF2_ADDR_SIZE * 8);
16387 shwi_addend = wi_addend.to_shwi ();
16388 loc_naddend = (shwi_addend != INTTYPE_MINIMUM (HOST_WIDE_INT))
16389 ? int_loc_descriptor (-shwi_addend)
16390 : NULL;
16392 if (loc_naddend != NULL
16393 && ((unsigned) size_of_uleb128 (shwi_addend)
16394 > size_of_loc_descr (loc_naddend)))
16396 add_loc_descr_to_each (list_ret, loc_naddend);
16397 add_loc_descr_to_each (list_ret,
16398 new_loc_descr (DW_OP_minus, 0, 0));
16400 else
16402 for (dw_loc_descr_ref loc_cur = loc_naddend; loc_cur != NULL; )
16404 loc_naddend = loc_cur;
16405 loc_cur = loc_cur->dw_loc_next;
16406 ggc_free (loc_naddend);
16408 loc_list_plus_const (list_ret, wi_addend.to_shwi ());
16410 break;
16413 op = DW_OP_plus;
16414 goto do_binop;
16416 case LE_EXPR:
16417 op = DW_OP_le;
16418 goto do_comp_binop;
16420 case GE_EXPR:
16421 op = DW_OP_ge;
16422 goto do_comp_binop;
16424 case LT_EXPR:
16425 op = DW_OP_lt;
16426 goto do_comp_binop;
16428 case GT_EXPR:
16429 op = DW_OP_gt;
16430 goto do_comp_binop;
16432 do_comp_binop:
16433 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16435 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
16436 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
16437 list_ret = loc_list_from_uint_comparison (list_ret, list_ret1,
16438 TREE_CODE (loc));
16439 break;
16441 else
16442 goto do_binop;
16444 case EQ_EXPR:
16445 op = DW_OP_eq;
16446 goto do_binop;
16448 case NE_EXPR:
16449 op = DW_OP_ne;
16450 goto do_binop;
16452 do_binop:
16453 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16454 list_ret1 = loc_list_from_tree_1 (TREE_OPERAND (loc, 1), 0, context);
16455 if (list_ret == 0 || list_ret1 == 0)
16456 return 0;
16458 add_loc_list (&list_ret, list_ret1);
16459 if (list_ret == 0)
16460 return 0;
16461 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16462 break;
16464 case TRUTH_NOT_EXPR:
16465 case BIT_NOT_EXPR:
16466 op = DW_OP_not;
16467 goto do_unop;
16469 case ABS_EXPR:
16470 op = DW_OP_abs;
16471 goto do_unop;
16473 case NEGATE_EXPR:
16474 op = DW_OP_neg;
16475 goto do_unop;
16477 do_unop:
16478 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16479 if (list_ret == 0)
16480 return 0;
16482 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16483 break;
16485 case MIN_EXPR:
16486 case MAX_EXPR:
16488 const enum tree_code code =
16489 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16491 loc = build3 (COND_EXPR, TREE_TYPE (loc),
16492 build2 (code, integer_type_node,
16493 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16494 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16497 /* fall through */
16499 case COND_EXPR:
16501 dw_loc_descr_ref lhs
16502 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
16503 dw_loc_list_ref rhs
16504 = loc_list_from_tree_1 (TREE_OPERAND (loc, 2), 0, context);
16505 dw_loc_descr_ref bra_node, jump_node, tmp;
16507 list_ret = loc_list_from_tree_1 (TREE_OPERAND (loc, 0), 0, context);
16508 if (list_ret == 0 || lhs == 0 || rhs == 0)
16509 return 0;
16511 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16512 add_loc_descr_to_each (list_ret, bra_node);
16514 add_loc_list (&list_ret, rhs);
16515 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16516 add_loc_descr_to_each (list_ret, jump_node);
16518 add_loc_descr_to_each (list_ret, lhs);
16519 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16520 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16522 /* ??? Need a node to point the skip at. Use a nop. */
16523 tmp = new_loc_descr (DW_OP_nop, 0, 0);
16524 add_loc_descr_to_each (list_ret, tmp);
16525 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16526 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16528 break;
16530 case FIX_TRUNC_EXPR:
16531 return 0;
16533 default:
16534 /* Leave front-end specific codes as simply unknown. This comes
16535 up, for instance, with the C STMT_EXPR. */
16536 if ((unsigned int) TREE_CODE (loc)
16537 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16539 expansion_failed (loc, NULL_RTX,
16540 "language specific tree node");
16541 return 0;
16544 /* Otherwise this is a generic code; we should just lists all of
16545 these explicitly. We forgot one. */
16546 if (flag_checking)
16547 gcc_unreachable ();
16549 /* In a release build, we want to degrade gracefully: better to
16550 generate incomplete debugging information than to crash. */
16551 return NULL;
16554 if (!ret && !list_ret)
16555 return 0;
16557 if (want_address == 2 && !have_address
16558 && (dwarf_version >= 4 || !dwarf_strict))
16560 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16562 expansion_failed (loc, NULL_RTX,
16563 "DWARF address size mismatch");
16564 return 0;
16566 if (ret)
16567 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16568 else
16569 add_loc_descr_to_each (list_ret,
16570 new_loc_descr (DW_OP_stack_value, 0, 0));
16571 have_address = 1;
16573 /* Show if we can't fill the request for an address. */
16574 if (want_address && !have_address)
16576 expansion_failed (loc, NULL_RTX,
16577 "Want address and only have value");
16578 return 0;
16581 gcc_assert (!ret || !list_ret);
16583 /* If we've got an address and don't want one, dereference. */
16584 if (!want_address && have_address)
16586 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16588 if (size > DWARF2_ADDR_SIZE || size == -1)
16590 expansion_failed (loc, NULL_RTX,
16591 "DWARF address size mismatch");
16592 return 0;
16594 else if (size == DWARF2_ADDR_SIZE)
16595 op = DW_OP_deref;
16596 else
16597 op = DW_OP_deref_size;
16599 if (ret)
16600 add_loc_descr (&ret, new_loc_descr (op, size, 0));
16601 else
16602 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16604 if (ret)
16605 list_ret = new_loc_list (ret, NULL, NULL, NULL);
16607 return list_ret;
16610 /* Likewise, but strip useless DW_OP_nop operations in the resulting
16611 expressions. */
16613 static dw_loc_list_ref
16614 loc_list_from_tree (tree loc, int want_address,
16615 const struct loc_descr_context *context)
16617 dw_loc_list_ref result = loc_list_from_tree_1 (loc, want_address, context);
16619 for (dw_loc_list_ref loc_cur = result;
16620 loc_cur != NULL; loc_cur =
16621 loc_cur->dw_loc_next)
16622 loc_descr_without_nops (loc_cur->expr);
16623 return result;
16626 /* Same as above but return only single location expression. */
16627 static dw_loc_descr_ref
16628 loc_descriptor_from_tree (tree loc, int want_address,
16629 const struct loc_descr_context *context)
16631 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
16632 if (!ret)
16633 return NULL;
16634 if (ret->dw_loc_next)
16636 expansion_failed (loc, NULL_RTX,
16637 "Location list where only loc descriptor needed");
16638 return NULL;
16640 return ret->expr;
16643 /* Given a value, round it up to the lowest multiple of `boundary'
16644 which is not less than the value itself. */
16646 static inline HOST_WIDE_INT
16647 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16649 return (((value + boundary - 1) / boundary) * boundary);
16652 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16653 pointer to the declared type for the relevant field variable, or return
16654 `integer_type_node' if the given node turns out to be an
16655 ERROR_MARK node. */
16657 static inline tree
16658 field_type (const_tree decl)
16660 tree type;
16662 if (TREE_CODE (decl) == ERROR_MARK)
16663 return integer_type_node;
16665 type = DECL_BIT_FIELD_TYPE (decl);
16666 if (type == NULL_TREE)
16667 type = TREE_TYPE (decl);
16669 return type;
16672 /* Given a pointer to a tree node, return the alignment in bits for
16673 it, or else return BITS_PER_WORD if the node actually turns out to
16674 be an ERROR_MARK node. */
16676 static inline unsigned
16677 simple_type_align_in_bits (const_tree type)
16679 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16682 static inline unsigned
16683 simple_decl_align_in_bits (const_tree decl)
16685 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16688 /* Return the result of rounding T up to ALIGN. */
16690 static inline offset_int
16691 round_up_to_align (const offset_int &t, unsigned int align)
16693 return wi::udiv_trunc (t + align - 1, align) * align;
16696 /* Compute the size of TYPE in bytes. If possible, return NULL and store the
16697 size as an integer constant in CST_SIZE. Otherwise, if possible, return a
16698 DWARF expression that computes the size. Return NULL and set CST_SIZE to -1
16699 if we fail to return the size in one of these two forms. */
16701 static dw_loc_descr_ref
16702 type_byte_size (const_tree type, HOST_WIDE_INT *cst_size)
16704 tree tree_size;
16705 struct loc_descr_context ctx;
16707 /* Return a constant integer in priority, if possible. */
16708 *cst_size = int_size_in_bytes (type);
16709 if (*cst_size != -1)
16710 return NULL;
16712 ctx.context_type = const_cast<tree> (type);
16713 ctx.base_decl = NULL_TREE;
16714 ctx.dpi = NULL;
16716 type = TYPE_MAIN_VARIANT (type);
16717 tree_size = TYPE_SIZE_UNIT (type);
16718 return ((tree_size != NULL_TREE)
16719 ? loc_descriptor_from_tree (tree_size, 0, &ctx)
16720 : NULL);
16723 /* Helper structure for RECORD_TYPE processing. */
16724 struct vlr_context
16726 /* Root RECORD_TYPE. It is needed to generate data member location
16727 descriptions in variable-length records (VLR), but also to cope with
16728 variants, which are composed of nested structures multiplexed with
16729 QUAL_UNION_TYPE nodes. Each time such a structure is passed to a
16730 function processing a FIELD_DECL, it is required to be non null. */
16731 tree struct_type;
16732 /* When generating a variant part in a RECORD_TYPE (i.e. a nested
16733 QUAL_UNION_TYPE), this holds an expression that computes the offset for
16734 this variant part as part of the root record (in storage units). For
16735 regular records, it must be NULL_TREE. */
16736 tree variant_part_offset;
16739 /* Given a pointer to a FIELD_DECL, compute the byte offset of the lowest
16740 addressed byte of the "containing object" for the given FIELD_DECL. If
16741 possible, return a native constant through CST_OFFSET (in which case NULL is
16742 returned); otherwise return a DWARF expression that computes the offset.
16744 Set *CST_OFFSET to 0 and return NULL if we are unable to determine what
16745 that offset is, either because the argument turns out to be a pointer to an
16746 ERROR_MARK node, or because the offset expression is too complex for us.
16748 CTX is required: see the comment for VLR_CONTEXT. */
16750 static dw_loc_descr_ref
16751 field_byte_offset (const_tree decl, struct vlr_context *ctx,
16752 HOST_WIDE_INT *cst_offset)
16754 offset_int object_offset_in_bits;
16755 offset_int object_offset_in_bytes;
16756 offset_int bitpos_int;
16757 bool is_byte_offset_cst, is_bit_offset_cst;
16758 tree tree_result;
16759 dw_loc_list_ref loc_result;
16761 *cst_offset = 0;
16763 if (TREE_CODE (decl) == ERROR_MARK)
16764 return NULL;
16765 else
16766 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16768 is_bit_offset_cst = TREE_CODE (DECL_FIELD_BIT_OFFSET (decl)) != INTEGER_CST;
16769 is_byte_offset_cst = TREE_CODE (DECL_FIELD_OFFSET (decl)) != INTEGER_CST;
16771 /* We cannot handle variable bit offsets at the moment, so abort if it's the
16772 case. */
16773 if (is_bit_offset_cst)
16774 return NULL;
16776 #ifdef PCC_BITFIELD_TYPE_MATTERS
16777 /* We used to handle only constant offsets in all cases. Now, we handle
16778 properly dynamic byte offsets only when PCC bitfield type doesn't
16779 matter. */
16780 if (PCC_BITFIELD_TYPE_MATTERS && is_byte_offset_cst && is_bit_offset_cst)
16782 tree type;
16783 tree field_size_tree;
16784 offset_int deepest_bitpos;
16785 offset_int field_size_in_bits;
16786 unsigned int type_align_in_bits;
16787 unsigned int decl_align_in_bits;
16788 offset_int type_size_in_bits;
16790 bitpos_int = wi::to_offset (bit_position (decl));
16791 type = field_type (decl);
16792 type_size_in_bits = offset_int_type_size_in_bits (type);
16793 type_align_in_bits = simple_type_align_in_bits (type);
16795 field_size_tree = DECL_SIZE (decl);
16797 /* The size could be unspecified if there was an error, or for
16798 a flexible array member. */
16799 if (!field_size_tree)
16800 field_size_tree = bitsize_zero_node;
16802 /* If the size of the field is not constant, use the type size. */
16803 if (TREE_CODE (field_size_tree) == INTEGER_CST)
16804 field_size_in_bits = wi::to_offset (field_size_tree);
16805 else
16806 field_size_in_bits = type_size_in_bits;
16808 decl_align_in_bits = simple_decl_align_in_bits (decl);
16810 /* The GCC front-end doesn't make any attempt to keep track of the
16811 starting bit offset (relative to the start of the containing
16812 structure type) of the hypothetical "containing object" for a
16813 bit-field. Thus, when computing the byte offset value for the
16814 start of the "containing object" of a bit-field, we must deduce
16815 this information on our own. This can be rather tricky to do in
16816 some cases. For example, handling the following structure type
16817 definition when compiling for an i386/i486 target (which only
16818 aligns long long's to 32-bit boundaries) can be very tricky:
16820 struct S { int field1; long long field2:31; };
16822 Fortunately, there is a simple rule-of-thumb which can be used
16823 in such cases. When compiling for an i386/i486, GCC will
16824 allocate 8 bytes for the structure shown above. It decides to
16825 do this based upon one simple rule for bit-field allocation.
16826 GCC allocates each "containing object" for each bit-field at
16827 the first (i.e. lowest addressed) legitimate alignment boundary
16828 (based upon the required minimum alignment for the declared
16829 type of the field) which it can possibly use, subject to the
16830 condition that there is still enough available space remaining
16831 in the containing object (when allocated at the selected point)
16832 to fully accommodate all of the bits of the bit-field itself.
16834 This simple rule makes it obvious why GCC allocates 8 bytes for
16835 each object of the structure type shown above. When looking
16836 for a place to allocate the "containing object" for `field2',
16837 the compiler simply tries to allocate a 64-bit "containing
16838 object" at each successive 32-bit boundary (starting at zero)
16839 until it finds a place to allocate that 64- bit field such that
16840 at least 31 contiguous (and previously unallocated) bits remain
16841 within that selected 64 bit field. (As it turns out, for the
16842 example above, the compiler finds it is OK to allocate the
16843 "containing object" 64-bit field at bit-offset zero within the
16844 structure type.)
16846 Here we attempt to work backwards from the limited set of facts
16847 we're given, and we try to deduce from those facts, where GCC
16848 must have believed that the containing object started (within
16849 the structure type). The value we deduce is then used (by the
16850 callers of this routine) to generate DW_AT_location and
16851 DW_AT_bit_offset attributes for fields (both bit-fields and, in
16852 the case of DW_AT_location, regular fields as well). */
16854 /* Figure out the bit-distance from the start of the structure to
16855 the "deepest" bit of the bit-field. */
16856 deepest_bitpos = bitpos_int + field_size_in_bits;
16858 /* This is the tricky part. Use some fancy footwork to deduce
16859 where the lowest addressed bit of the containing object must
16860 be. */
16861 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16863 /* Round up to type_align by default. This works best for
16864 bitfields. */
16865 object_offset_in_bits
16866 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16868 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
16870 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
16872 /* Round up to decl_align instead. */
16873 object_offset_in_bits
16874 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16877 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16879 tree_result = byte_position (decl);
16880 if (ctx->variant_part_offset != NULL_TREE)
16881 tree_result = fold (build2 (PLUS_EXPR, TREE_TYPE (tree_result),
16882 ctx->variant_part_offset, tree_result));
16884 /* If the byte offset is a constant, it's simplier to handle a native
16885 constant rather than a DWARF expression. */
16886 if (TREE_CODE (tree_result) == INTEGER_CST)
16888 *cst_offset = wi::to_offset (tree_result).to_shwi ();
16889 return NULL;
16891 struct loc_descr_context loc_ctx = {
16892 ctx->struct_type, /* context_type */
16893 NULL_TREE, /* base_decl */
16894 NULL /* dpi */
16896 loc_result = loc_list_from_tree (tree_result, 0, &loc_ctx);
16898 /* We want a DWARF expression: abort if we only have a location list with
16899 multiple elements. */
16900 if (!loc_result || !single_element_loc_list_p (loc_result))
16901 return NULL;
16902 else
16903 return loc_result->expr;
16906 /* The following routines define various Dwarf attributes and any data
16907 associated with them. */
16909 /* Add a location description attribute value to a DIE.
16911 This emits location attributes suitable for whole variables and
16912 whole parameters. Note that the location attributes for struct fields are
16913 generated by the routine `data_member_location_attribute' below. */
16915 static inline void
16916 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16917 dw_loc_list_ref descr)
16919 if (descr == 0)
16920 return;
16921 if (single_element_loc_list_p (descr))
16922 add_AT_loc (die, attr_kind, descr->expr);
16923 else
16924 add_AT_loc_list (die, attr_kind, descr);
16927 /* Add DW_AT_accessibility attribute to DIE if needed. */
16929 static void
16930 add_accessibility_attribute (dw_die_ref die, tree decl)
16932 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16933 children, otherwise the default is DW_ACCESS_public. In DWARF2
16934 the default has always been DW_ACCESS_public. */
16935 if (TREE_PROTECTED (decl))
16936 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16937 else if (TREE_PRIVATE (decl))
16939 if (dwarf_version == 2
16940 || die->die_parent == NULL
16941 || die->die_parent->die_tag != DW_TAG_class_type)
16942 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16944 else if (dwarf_version > 2
16945 && die->die_parent
16946 && die->die_parent->die_tag == DW_TAG_class_type)
16947 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16950 /* Attach the specialized form of location attribute used for data members of
16951 struct and union types. In the special case of a FIELD_DECL node which
16952 represents a bit-field, the "offset" part of this special location
16953 descriptor must indicate the distance in bytes from the lowest-addressed
16954 byte of the containing struct or union type to the lowest-addressed byte of
16955 the "containing object" for the bit-field. (See the `field_byte_offset'
16956 function above).
16958 For any given bit-field, the "containing object" is a hypothetical object
16959 (of some integral or enum type) within which the given bit-field lives. The
16960 type of this hypothetical "containing object" is always the same as the
16961 declared type of the individual bit-field itself (for GCC anyway... the
16962 DWARF spec doesn't actually mandate this). Note that it is the size (in
16963 bytes) of the hypothetical "containing object" which will be given in the
16964 DW_AT_byte_size attribute for this bit-field. (See the
16965 `byte_size_attribute' function below.) It is also used when calculating the
16966 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
16967 function below.)
16969 CTX is required: see the comment for VLR_CONTEXT. */
16971 static void
16972 add_data_member_location_attribute (dw_die_ref die,
16973 tree decl,
16974 struct vlr_context *ctx)
16976 HOST_WIDE_INT offset;
16977 dw_loc_descr_ref loc_descr = 0;
16979 if (TREE_CODE (decl) == TREE_BINFO)
16981 /* We're working on the TAG_inheritance for a base class. */
16982 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16984 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16985 aren't at a fixed offset from all (sub)objects of the same
16986 type. We need to extract the appropriate offset from our
16987 vtable. The following dwarf expression means
16989 BaseAddr = ObAddr + *((*ObAddr) - Offset)
16991 This is specific to the V3 ABI, of course. */
16993 dw_loc_descr_ref tmp;
16995 /* Make a copy of the object address. */
16996 tmp = new_loc_descr (DW_OP_dup, 0, 0);
16997 add_loc_descr (&loc_descr, tmp);
16999 /* Extract the vtable address. */
17000 tmp = new_loc_descr (DW_OP_deref, 0, 0);
17001 add_loc_descr (&loc_descr, tmp);
17003 /* Calculate the address of the offset. */
17004 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
17005 gcc_assert (offset < 0);
17007 tmp = int_loc_descriptor (-offset);
17008 add_loc_descr (&loc_descr, tmp);
17009 tmp = new_loc_descr (DW_OP_minus, 0, 0);
17010 add_loc_descr (&loc_descr, tmp);
17012 /* Extract the offset. */
17013 tmp = new_loc_descr (DW_OP_deref, 0, 0);
17014 add_loc_descr (&loc_descr, tmp);
17016 /* Add it to the object address. */
17017 tmp = new_loc_descr (DW_OP_plus, 0, 0);
17018 add_loc_descr (&loc_descr, tmp);
17020 else
17021 offset = tree_to_shwi (BINFO_OFFSET (decl));
17023 else
17025 loc_descr = field_byte_offset (decl, ctx, &offset);
17027 /* If loc_descr is available then we know the field offset is dynamic.
17028 However, GDB does not handle dynamic field offsets very well at the
17029 moment. */
17030 if (loc_descr != NULL && gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL)
17032 loc_descr = NULL;
17033 offset = 0;
17036 /* Data member location evalutation starts with the base address on the
17037 stack. Compute the field offset and add it to this base address. */
17038 else if (loc_descr != NULL)
17039 add_loc_descr (&loc_descr, new_loc_descr (DW_OP_plus, 0, 0));
17042 if (! loc_descr)
17044 if (dwarf_version > 2)
17046 /* Don't need to output a location expression, just the constant. */
17047 if (offset < 0)
17048 add_AT_int (die, DW_AT_data_member_location, offset);
17049 else
17050 add_AT_unsigned (die, DW_AT_data_member_location, offset);
17051 return;
17053 else
17055 enum dwarf_location_atom op;
17057 /* The DWARF2 standard says that we should assume that the structure
17058 address is already on the stack, so we can specify a structure
17059 field address by using DW_OP_plus_uconst. */
17060 op = DW_OP_plus_uconst;
17061 loc_descr = new_loc_descr (op, offset, 0);
17065 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17068 /* Writes integer values to dw_vec_const array. */
17070 static void
17071 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17073 while (size != 0)
17075 *dest++ = val & 0xff;
17076 val >>= 8;
17077 --size;
17081 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
17083 static HOST_WIDE_INT
17084 extract_int (const unsigned char *src, unsigned int size)
17086 HOST_WIDE_INT val = 0;
17088 src += size;
17089 while (size != 0)
17091 val <<= 8;
17092 val |= *--src & 0xff;
17093 --size;
17095 return val;
17098 /* Writes wide_int values to dw_vec_const array. */
17100 static void
17101 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
17103 int i;
17105 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
17107 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
17108 return;
17111 /* We'd have to extend this code to support odd sizes. */
17112 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
17114 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
17116 if (WORDS_BIG_ENDIAN)
17117 for (i = n - 1; i >= 0; i--)
17119 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17120 dest += sizeof (HOST_WIDE_INT);
17122 else
17123 for (i = 0; i < n; i++)
17125 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
17126 dest += sizeof (HOST_WIDE_INT);
17130 /* Writes floating point values to dw_vec_const array. */
17132 static void
17133 insert_float (const_rtx rtl, unsigned char *array)
17135 long val[4];
17136 int i;
17138 real_to_target (val, CONST_DOUBLE_REAL_VALUE (rtl), GET_MODE (rtl));
17140 /* real_to_target puts 32-bit pieces in each long. Pack them. */
17141 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17143 insert_int (val[i], 4, array);
17144 array += 4;
17148 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17149 does not have a "location" either in memory or in a register. These
17150 things can arise in GNU C when a constant is passed as an actual parameter
17151 to an inlined function. They can also arise in C++ where declared
17152 constants do not necessarily get memory "homes". */
17154 static bool
17155 add_const_value_attribute (dw_die_ref die, rtx rtl)
17157 switch (GET_CODE (rtl))
17159 case CONST_INT:
17161 HOST_WIDE_INT val = INTVAL (rtl);
17163 if (val < 0)
17164 add_AT_int (die, DW_AT_const_value, val);
17165 else
17166 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17168 return true;
17170 case CONST_WIDE_INT:
17172 wide_int w1 = std::make_pair (rtl, MAX_MODE_INT);
17173 unsigned int prec = MIN (wi::min_precision (w1, UNSIGNED),
17174 (unsigned int)CONST_WIDE_INT_NUNITS (rtl) * HOST_BITS_PER_WIDE_INT);
17175 wide_int w = wi::zext (w1, prec);
17176 add_AT_wide (die, DW_AT_const_value, w);
17178 return true;
17180 case CONST_DOUBLE:
17181 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17182 floating-point constant. A CONST_DOUBLE is used whenever the
17183 constant requires more than one word in order to be adequately
17184 represented. */
17186 machine_mode mode = GET_MODE (rtl);
17188 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
17189 add_AT_double (die, DW_AT_const_value,
17190 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17191 else
17193 unsigned int length = GET_MODE_SIZE (mode);
17194 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
17196 insert_float (rtl, array);
17197 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17200 return true;
17202 case CONST_VECTOR:
17204 machine_mode mode = GET_MODE (rtl);
17205 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17206 unsigned int length = CONST_VECTOR_NUNITS (rtl);
17207 unsigned char *array
17208 = ggc_vec_alloc<unsigned char> (length * elt_size);
17209 unsigned int i;
17210 unsigned char *p;
17211 machine_mode imode = GET_MODE_INNER (mode);
17213 switch (GET_MODE_CLASS (mode))
17215 case MODE_VECTOR_INT:
17216 for (i = 0, p = array; i < length; i++, p += elt_size)
17218 rtx elt = CONST_VECTOR_ELT (rtl, i);
17219 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
17221 break;
17223 case MODE_VECTOR_FLOAT:
17224 for (i = 0, p = array; i < length; i++, p += elt_size)
17226 rtx elt = CONST_VECTOR_ELT (rtl, i);
17227 insert_float (elt, p);
17229 break;
17231 default:
17232 gcc_unreachable ();
17235 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17237 return true;
17239 case CONST_STRING:
17240 if (dwarf_version >= 4 || !dwarf_strict)
17242 dw_loc_descr_ref loc_result;
17243 resolve_one_addr (&rtl);
17244 rtl_addr:
17245 loc_result = new_addr_loc_descr (rtl, dtprel_false);
17246 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17247 add_AT_loc (die, DW_AT_location, loc_result);
17248 vec_safe_push (used_rtx_array, rtl);
17249 return true;
17251 return false;
17253 case CONST:
17254 if (CONSTANT_P (XEXP (rtl, 0)))
17255 return add_const_value_attribute (die, XEXP (rtl, 0));
17256 /* FALLTHROUGH */
17257 case SYMBOL_REF:
17258 if (!const_ok_for_output (rtl))
17259 return false;
17260 /* FALLTHROUGH */
17261 case LABEL_REF:
17262 if (dwarf_version >= 4 || !dwarf_strict)
17263 goto rtl_addr;
17264 return false;
17266 case PLUS:
17267 /* In cases where an inlined instance of an inline function is passed
17268 the address of an `auto' variable (which is local to the caller) we
17269 can get a situation where the DECL_RTL of the artificial local
17270 variable (for the inlining) which acts as a stand-in for the
17271 corresponding formal parameter (of the inline function) will look
17272 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
17273 exactly a compile-time constant expression, but it isn't the address
17274 of the (artificial) local variable either. Rather, it represents the
17275 *value* which the artificial local variable always has during its
17276 lifetime. We currently have no way to represent such quasi-constant
17277 values in Dwarf, so for now we just punt and generate nothing. */
17278 return false;
17280 case HIGH:
17281 case CONST_FIXED:
17282 return false;
17284 case MEM:
17285 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17286 && MEM_READONLY_P (rtl)
17287 && GET_MODE (rtl) == BLKmode)
17289 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17290 return true;
17292 return false;
17294 default:
17295 /* No other kinds of rtx should be possible here. */
17296 gcc_unreachable ();
17298 return false;
17301 /* Determine whether the evaluation of EXPR references any variables
17302 or functions which aren't otherwise used (and therefore may not be
17303 output). */
17304 static tree
17305 reference_to_unused (tree * tp, int * walk_subtrees,
17306 void * data ATTRIBUTE_UNUSED)
17308 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17309 *walk_subtrees = 0;
17311 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17312 && ! TREE_ASM_WRITTEN (*tp))
17313 return *tp;
17314 /* ??? The C++ FE emits debug information for using decls, so
17315 putting gcc_unreachable here falls over. See PR31899. For now
17316 be conservative. */
17317 else if (!symtab->global_info_ready && VAR_OR_FUNCTION_DECL_P (*tp))
17318 return *tp;
17319 else if (VAR_P (*tp))
17321 varpool_node *node = varpool_node::get (*tp);
17322 if (!node || !node->definition)
17323 return *tp;
17325 else if (TREE_CODE (*tp) == FUNCTION_DECL
17326 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17328 /* The call graph machinery must have finished analyzing,
17329 optimizing and gimplifying the CU by now.
17330 So if *TP has no call graph node associated
17331 to it, it means *TP will not be emitted. */
17332 if (!cgraph_node::get (*tp))
17333 return *tp;
17335 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17336 return *tp;
17338 return NULL_TREE;
17341 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17342 for use in a later add_const_value_attribute call. */
17344 static rtx
17345 rtl_for_decl_init (tree init, tree type)
17347 rtx rtl = NULL_RTX;
17349 STRIP_NOPS (init);
17351 /* If a variable is initialized with a string constant without embedded
17352 zeros, build CONST_STRING. */
17353 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17355 tree enttype = TREE_TYPE (type);
17356 tree domain = TYPE_DOMAIN (type);
17357 machine_mode mode = TYPE_MODE (enttype);
17359 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17360 && domain
17361 && integer_zerop (TYPE_MIN_VALUE (domain))
17362 && compare_tree_int (TYPE_MAX_VALUE (domain),
17363 TREE_STRING_LENGTH (init) - 1) == 0
17364 && ((size_t) TREE_STRING_LENGTH (init)
17365 == strlen (TREE_STRING_POINTER (init)) + 1))
17367 rtl = gen_rtx_CONST_STRING (VOIDmode,
17368 ggc_strdup (TREE_STRING_POINTER (init)));
17369 rtl = gen_rtx_MEM (BLKmode, rtl);
17370 MEM_READONLY_P (rtl) = 1;
17373 /* Other aggregates, and complex values, could be represented using
17374 CONCAT: FIXME! */
17375 else if (AGGREGATE_TYPE_P (type)
17376 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17377 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17378 || TREE_CODE (type) == COMPLEX_TYPE)
17380 /* Vectors only work if their mode is supported by the target.
17381 FIXME: generic vectors ought to work too. */
17382 else if (TREE_CODE (type) == VECTOR_TYPE
17383 && !VECTOR_MODE_P (TYPE_MODE (type)))
17385 /* If the initializer is something that we know will expand into an
17386 immediate RTL constant, expand it now. We must be careful not to
17387 reference variables which won't be output. */
17388 else if (initializer_constant_valid_p (init, type)
17389 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17391 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17392 possible. */
17393 if (TREE_CODE (type) == VECTOR_TYPE)
17394 switch (TREE_CODE (init))
17396 case VECTOR_CST:
17397 break;
17398 case CONSTRUCTOR:
17399 if (TREE_CONSTANT (init))
17401 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
17402 bool constant_p = true;
17403 tree value;
17404 unsigned HOST_WIDE_INT ix;
17406 /* Even when ctor is constant, it might contain non-*_CST
17407 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17408 belong into VECTOR_CST nodes. */
17409 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17410 if (!CONSTANT_CLASS_P (value))
17412 constant_p = false;
17413 break;
17416 if (constant_p)
17418 init = build_vector_from_ctor (type, elts);
17419 break;
17422 /* FALLTHRU */
17424 default:
17425 return NULL;
17428 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17430 /* If expand_expr returns a MEM, it wasn't immediate. */
17431 gcc_assert (!rtl || !MEM_P (rtl));
17434 return rtl;
17437 /* Generate RTL for the variable DECL to represent its location. */
17439 static rtx
17440 rtl_for_decl_location (tree decl)
17442 rtx rtl;
17444 /* Here we have to decide where we are going to say the parameter "lives"
17445 (as far as the debugger is concerned). We only have a couple of
17446 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17448 DECL_RTL normally indicates where the parameter lives during most of the
17449 activation of the function. If optimization is enabled however, this
17450 could be either NULL or else a pseudo-reg. Both of those cases indicate
17451 that the parameter doesn't really live anywhere (as far as the code
17452 generation parts of GCC are concerned) during most of the function's
17453 activation. That will happen (for example) if the parameter is never
17454 referenced within the function.
17456 We could just generate a location descriptor here for all non-NULL
17457 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17458 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17459 where DECL_RTL is NULL or is a pseudo-reg.
17461 Note however that we can only get away with using DECL_INCOMING_RTL as
17462 a backup substitute for DECL_RTL in certain limited cases. In cases
17463 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17464 we can be sure that the parameter was passed using the same type as it is
17465 declared to have within the function, and that its DECL_INCOMING_RTL
17466 points us to a place where a value of that type is passed.
17468 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17469 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17470 because in these cases DECL_INCOMING_RTL points us to a value of some
17471 type which is *different* from the type of the parameter itself. Thus,
17472 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17473 such cases, the debugger would end up (for example) trying to fetch a
17474 `float' from a place which actually contains the first part of a
17475 `double'. That would lead to really incorrect and confusing
17476 output at debug-time.
17478 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17479 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
17480 are a couple of exceptions however. On little-endian machines we can
17481 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17482 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17483 an integral type that is smaller than TREE_TYPE (decl). These cases arise
17484 when (on a little-endian machine) a non-prototyped function has a
17485 parameter declared to be of type `short' or `char'. In such cases,
17486 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17487 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17488 passed `int' value. If the debugger then uses that address to fetch
17489 a `short' or a `char' (on a little-endian machine) the result will be
17490 the correct data, so we allow for such exceptional cases below.
17492 Note that our goal here is to describe the place where the given formal
17493 parameter lives during most of the function's activation (i.e. between the
17494 end of the prologue and the start of the epilogue). We'll do that as best
17495 as we can. Note however that if the given formal parameter is modified
17496 sometime during the execution of the function, then a stack backtrace (at
17497 debug-time) will show the function as having been called with the *new*
17498 value rather than the value which was originally passed in. This happens
17499 rarely enough that it is not a major problem, but it *is* a problem, and
17500 I'd like to fix it.
17502 A future version of dwarf2out.c may generate two additional attributes for
17503 any given DW_TAG_formal_parameter DIE which will describe the "passed
17504 type" and the "passed location" for the given formal parameter in addition
17505 to the attributes we now generate to indicate the "declared type" and the
17506 "active location" for each parameter. This additional set of attributes
17507 could be used by debuggers for stack backtraces. Separately, note that
17508 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17509 This happens (for example) for inlined-instances of inline function formal
17510 parameters which are never referenced. This really shouldn't be
17511 happening. All PARM_DECL nodes should get valid non-NULL
17512 DECL_INCOMING_RTL values. FIXME. */
17514 /* Use DECL_RTL as the "location" unless we find something better. */
17515 rtl = DECL_RTL_IF_SET (decl);
17517 /* When generating abstract instances, ignore everything except
17518 constants, symbols living in memory, and symbols living in
17519 fixed registers. */
17520 if (! reload_completed)
17522 if (rtl
17523 && (CONSTANT_P (rtl)
17524 || (MEM_P (rtl)
17525 && CONSTANT_P (XEXP (rtl, 0)))
17526 || (REG_P (rtl)
17527 && VAR_P (decl)
17528 && TREE_STATIC (decl))))
17530 rtl = targetm.delegitimize_address (rtl);
17531 return rtl;
17533 rtl = NULL_RTX;
17535 else if (TREE_CODE (decl) == PARM_DECL)
17537 if (rtl == NULL_RTX
17538 || is_pseudo_reg (rtl)
17539 || (MEM_P (rtl)
17540 && is_pseudo_reg (XEXP (rtl, 0))
17541 && DECL_INCOMING_RTL (decl)
17542 && MEM_P (DECL_INCOMING_RTL (decl))
17543 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17545 tree declared_type = TREE_TYPE (decl);
17546 tree passed_type = DECL_ARG_TYPE (decl);
17547 machine_mode dmode = TYPE_MODE (declared_type);
17548 machine_mode pmode = TYPE_MODE (passed_type);
17550 /* This decl represents a formal parameter which was optimized out.
17551 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17552 all cases where (rtl == NULL_RTX) just below. */
17553 if (dmode == pmode)
17554 rtl = DECL_INCOMING_RTL (decl);
17555 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17556 && SCALAR_INT_MODE_P (dmode)
17557 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17558 && DECL_INCOMING_RTL (decl))
17560 rtx inc = DECL_INCOMING_RTL (decl);
17561 if (REG_P (inc))
17562 rtl = inc;
17563 else if (MEM_P (inc))
17565 if (BYTES_BIG_ENDIAN)
17566 rtl = adjust_address_nv (inc, dmode,
17567 GET_MODE_SIZE (pmode)
17568 - GET_MODE_SIZE (dmode));
17569 else
17570 rtl = inc;
17575 /* If the parm was passed in registers, but lives on the stack, then
17576 make a big endian correction if the mode of the type of the
17577 parameter is not the same as the mode of the rtl. */
17578 /* ??? This is the same series of checks that are made in dbxout.c before
17579 we reach the big endian correction code there. It isn't clear if all
17580 of these checks are necessary here, but keeping them all is the safe
17581 thing to do. */
17582 else if (MEM_P (rtl)
17583 && XEXP (rtl, 0) != const0_rtx
17584 && ! CONSTANT_P (XEXP (rtl, 0))
17585 /* Not passed in memory. */
17586 && !MEM_P (DECL_INCOMING_RTL (decl))
17587 /* Not passed by invisible reference. */
17588 && (!REG_P (XEXP (rtl, 0))
17589 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17590 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17591 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17592 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17593 #endif
17595 /* Big endian correction check. */
17596 && BYTES_BIG_ENDIAN
17597 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17598 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17599 < UNITS_PER_WORD))
17601 machine_mode addr_mode = get_address_mode (rtl);
17602 int offset = (UNITS_PER_WORD
17603 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17605 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17606 plus_constant (addr_mode, XEXP (rtl, 0), offset));
17609 else if (VAR_P (decl)
17610 && rtl
17611 && MEM_P (rtl)
17612 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17613 && BYTES_BIG_ENDIAN)
17615 machine_mode addr_mode = get_address_mode (rtl);
17616 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17617 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17619 /* If a variable is declared "register" yet is smaller than
17620 a register, then if we store the variable to memory, it
17621 looks like we're storing a register-sized value, when in
17622 fact we are not. We need to adjust the offset of the
17623 storage location to reflect the actual value's bytes,
17624 else gdb will not be able to display it. */
17625 if (rsize > dsize)
17626 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17627 plus_constant (addr_mode, XEXP (rtl, 0),
17628 rsize - dsize));
17631 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17632 and will have been substituted directly into all expressions that use it.
17633 C does not have such a concept, but C++ and other languages do. */
17634 if (!rtl && VAR_P (decl) && DECL_INITIAL (decl))
17635 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17637 if (rtl)
17638 rtl = targetm.delegitimize_address (rtl);
17640 /* If we don't look past the constant pool, we risk emitting a
17641 reference to a constant pool entry that isn't referenced from
17642 code, and thus is not emitted. */
17643 if (rtl)
17644 rtl = avoid_constant_pool_reference (rtl);
17646 /* Try harder to get a rtl. If this symbol ends up not being emitted
17647 in the current CU, resolve_addr will remove the expression referencing
17648 it. */
17649 if (rtl == NULL_RTX
17650 && VAR_P (decl)
17651 && !DECL_EXTERNAL (decl)
17652 && TREE_STATIC (decl)
17653 && DECL_NAME (decl)
17654 && !DECL_HARD_REGISTER (decl)
17655 && DECL_MODE (decl) != VOIDmode)
17657 rtl = make_decl_rtl_for_debug (decl);
17658 if (!MEM_P (rtl)
17659 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17660 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17661 rtl = NULL_RTX;
17664 return rtl;
17667 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
17668 returned. If so, the decl for the COMMON block is returned, and the
17669 value is the offset into the common block for the symbol. */
17671 static tree
17672 fortran_common (tree decl, HOST_WIDE_INT *value)
17674 tree val_expr, cvar;
17675 machine_mode mode;
17676 HOST_WIDE_INT bitsize, bitpos;
17677 tree offset;
17678 int unsignedp, reversep, volatilep = 0;
17680 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17681 it does not have a value (the offset into the common area), or if it
17682 is thread local (as opposed to global) then it isn't common, and shouldn't
17683 be handled as such. */
17684 if (!VAR_P (decl)
17685 || !TREE_STATIC (decl)
17686 || !DECL_HAS_VALUE_EXPR_P (decl)
17687 || !is_fortran ())
17688 return NULL_TREE;
17690 val_expr = DECL_VALUE_EXPR (decl);
17691 if (TREE_CODE (val_expr) != COMPONENT_REF)
17692 return NULL_TREE;
17694 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset, &mode,
17695 &unsignedp, &reversep, &volatilep);
17697 if (cvar == NULL_TREE
17698 || !VAR_P (cvar)
17699 || DECL_ARTIFICIAL (cvar)
17700 || !TREE_PUBLIC (cvar))
17701 return NULL_TREE;
17703 *value = 0;
17704 if (offset != NULL)
17706 if (!tree_fits_shwi_p (offset))
17707 return NULL_TREE;
17708 *value = tree_to_shwi (offset);
17710 if (bitpos != 0)
17711 *value += bitpos / BITS_PER_UNIT;
17713 return cvar;
17716 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17717 data attribute for a variable or a parameter. We generate the
17718 DW_AT_const_value attribute only in those cases where the given variable
17719 or parameter does not have a true "location" either in memory or in a
17720 register. This can happen (for example) when a constant is passed as an
17721 actual argument in a call to an inline function. (It's possible that
17722 these things can crop up in other ways also.) Note that one type of
17723 constant value which can be passed into an inlined function is a constant
17724 pointer. This can happen for example if an actual argument in an inlined
17725 function call evaluates to a compile-time constant address.
17727 CACHE_P is true if it is worth caching the location list for DECL,
17728 so that future calls can reuse it rather than regenerate it from scratch.
17729 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17730 since we will need to refer to them each time the function is inlined. */
17732 static bool
17733 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p)
17735 rtx rtl;
17736 dw_loc_list_ref list;
17737 var_loc_list *loc_list;
17738 cached_dw_loc_list *cache;
17740 if (early_dwarf)
17741 return false;
17743 if (TREE_CODE (decl) == ERROR_MARK)
17744 return false;
17746 if (get_AT (die, DW_AT_location)
17747 || get_AT (die, DW_AT_const_value))
17748 return true;
17750 gcc_assert (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL
17751 || TREE_CODE (decl) == RESULT_DECL);
17753 /* Try to get some constant RTL for this decl, and use that as the value of
17754 the location. */
17756 rtl = rtl_for_decl_location (decl);
17757 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17758 && add_const_value_attribute (die, rtl))
17759 return true;
17761 /* See if we have single element location list that is equivalent to
17762 a constant value. That way we are better to use add_const_value_attribute
17763 rather than expanding constant value equivalent. */
17764 loc_list = lookup_decl_loc (decl);
17765 if (loc_list
17766 && loc_list->first
17767 && loc_list->first->next == NULL
17768 && NOTE_P (loc_list->first->loc)
17769 && NOTE_VAR_LOCATION (loc_list->first->loc)
17770 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17772 struct var_loc_node *node;
17774 node = loc_list->first;
17775 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17776 if (GET_CODE (rtl) == EXPR_LIST)
17777 rtl = XEXP (rtl, 0);
17778 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17779 && add_const_value_attribute (die, rtl))
17780 return true;
17782 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17783 list several times. See if we've already cached the contents. */
17784 list = NULL;
17785 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17786 cache_p = false;
17787 if (cache_p)
17789 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
17790 if (cache)
17791 list = cache->loc_list;
17793 if (list == NULL)
17795 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
17796 NULL);
17797 /* It is usually worth caching this result if the decl is from
17798 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
17799 if (cache_p && list && list->dw_loc_next)
17801 cached_dw_loc_list **slot
17802 = cached_dw_loc_list_table->find_slot_with_hash (decl,
17803 DECL_UID (decl),
17804 INSERT);
17805 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
17806 cache->decl_id = DECL_UID (decl);
17807 cache->loc_list = list;
17808 *slot = cache;
17811 if (list)
17813 add_AT_location_description (die, DW_AT_location, list);
17814 return true;
17816 /* None of that worked, so it must not really have a location;
17817 try adding a constant value attribute from the DECL_INITIAL. */
17818 return tree_add_const_value_attribute_for_decl (die, decl);
17821 /* Helper function for tree_add_const_value_attribute. Natively encode
17822 initializer INIT into an array. Return true if successful. */
17824 static bool
17825 native_encode_initializer (tree init, unsigned char *array, int size)
17827 tree type;
17829 if (init == NULL_TREE)
17830 return false;
17832 STRIP_NOPS (init);
17833 switch (TREE_CODE (init))
17835 case STRING_CST:
17836 type = TREE_TYPE (init);
17837 if (TREE_CODE (type) == ARRAY_TYPE)
17839 tree enttype = TREE_TYPE (type);
17840 machine_mode mode = TYPE_MODE (enttype);
17842 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17843 return false;
17844 if (int_size_in_bytes (type) != size)
17845 return false;
17846 if (size > TREE_STRING_LENGTH (init))
17848 memcpy (array, TREE_STRING_POINTER (init),
17849 TREE_STRING_LENGTH (init));
17850 memset (array + TREE_STRING_LENGTH (init),
17851 '\0', size - TREE_STRING_LENGTH (init));
17853 else
17854 memcpy (array, TREE_STRING_POINTER (init), size);
17855 return true;
17857 return false;
17858 case CONSTRUCTOR:
17859 type = TREE_TYPE (init);
17860 if (int_size_in_bytes (type) != size)
17861 return false;
17862 if (TREE_CODE (type) == ARRAY_TYPE)
17864 HOST_WIDE_INT min_index;
17865 unsigned HOST_WIDE_INT cnt;
17866 int curpos = 0, fieldsize;
17867 constructor_elt *ce;
17869 if (TYPE_DOMAIN (type) == NULL_TREE
17870 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
17871 return false;
17873 fieldsize = int_size_in_bytes (TREE_TYPE (type));
17874 if (fieldsize <= 0)
17875 return false;
17877 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
17878 memset (array, '\0', size);
17879 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17881 tree val = ce->value;
17882 tree index = ce->index;
17883 int pos = curpos;
17884 if (index && TREE_CODE (index) == RANGE_EXPR)
17885 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
17886 * fieldsize;
17887 else if (index)
17888 pos = (tree_to_shwi (index) - min_index) * fieldsize;
17890 if (val)
17892 STRIP_NOPS (val);
17893 if (!native_encode_initializer (val, array + pos, fieldsize))
17894 return false;
17896 curpos = pos + fieldsize;
17897 if (index && TREE_CODE (index) == RANGE_EXPR)
17899 int count = tree_to_shwi (TREE_OPERAND (index, 1))
17900 - tree_to_shwi (TREE_OPERAND (index, 0));
17901 while (count-- > 0)
17903 if (val)
17904 memcpy (array + curpos, array + pos, fieldsize);
17905 curpos += fieldsize;
17908 gcc_assert (curpos <= size);
17910 return true;
17912 else if (TREE_CODE (type) == RECORD_TYPE
17913 || TREE_CODE (type) == UNION_TYPE)
17915 tree field = NULL_TREE;
17916 unsigned HOST_WIDE_INT cnt;
17917 constructor_elt *ce;
17919 if (int_size_in_bytes (type) != size)
17920 return false;
17922 if (TREE_CODE (type) == RECORD_TYPE)
17923 field = TYPE_FIELDS (type);
17925 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
17927 tree val = ce->value;
17928 int pos, fieldsize;
17930 if (ce->index != 0)
17931 field = ce->index;
17933 if (val)
17934 STRIP_NOPS (val);
17936 if (field == NULL_TREE || DECL_BIT_FIELD (field))
17937 return false;
17939 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17940 && TYPE_DOMAIN (TREE_TYPE (field))
17941 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17942 return false;
17943 else if (DECL_SIZE_UNIT (field) == NULL_TREE
17944 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
17945 return false;
17946 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
17947 pos = int_byte_position (field);
17948 gcc_assert (pos + fieldsize <= size);
17949 if (val && fieldsize != 0
17950 && !native_encode_initializer (val, array + pos, fieldsize))
17951 return false;
17953 return true;
17955 return false;
17956 case VIEW_CONVERT_EXPR:
17957 case NON_LVALUE_EXPR:
17958 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17959 default:
17960 return native_encode_expr (init, array, size) == size;
17964 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17965 attribute is the const value T. */
17967 static bool
17968 tree_add_const_value_attribute (dw_die_ref die, tree t)
17970 tree init;
17971 tree type = TREE_TYPE (t);
17972 rtx rtl;
17974 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17975 return false;
17977 init = t;
17978 gcc_assert (!DECL_P (init));
17980 if (! early_dwarf)
17982 rtl = rtl_for_decl_init (init, type);
17983 if (rtl)
17984 return add_const_value_attribute (die, rtl);
17986 /* If the host and target are sane, try harder. */
17987 if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17988 && initializer_constant_valid_p (init, type))
17990 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17991 if (size > 0 && (int) size == size)
17993 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
17995 if (native_encode_initializer (init, array, size))
17997 add_AT_vec (die, DW_AT_const_value, size, 1, array);
17998 return true;
18000 ggc_free (array);
18003 return false;
18006 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
18007 attribute is the const value of T, where T is an integral constant
18008 variable with static storage duration
18009 (so it can't be a PARM_DECL or a RESULT_DECL). */
18011 static bool
18012 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
18015 if (!decl
18016 || (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
18017 || (VAR_P (decl) && !TREE_STATIC (decl)))
18018 return false;
18020 if (TREE_READONLY (decl)
18021 && ! TREE_THIS_VOLATILE (decl)
18022 && DECL_INITIAL (decl))
18023 /* OK */;
18024 else
18025 return false;
18027 /* Don't add DW_AT_const_value if abstract origin already has one. */
18028 if (get_AT (var_die, DW_AT_const_value))
18029 return false;
18031 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
18034 /* Convert the CFI instructions for the current function into a
18035 location list. This is used for DW_AT_frame_base when we targeting
18036 a dwarf2 consumer that does not support the dwarf3
18037 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
18038 expressions. */
18040 static dw_loc_list_ref
18041 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18043 int ix;
18044 dw_fde_ref fde;
18045 dw_loc_list_ref list, *list_tail;
18046 dw_cfi_ref cfi;
18047 dw_cfa_location last_cfa, next_cfa;
18048 const char *start_label, *last_label, *section;
18049 dw_cfa_location remember;
18051 fde = cfun->fde;
18052 gcc_assert (fde != NULL);
18054 section = secname_for_decl (current_function_decl);
18055 list_tail = &list;
18056 list = NULL;
18058 memset (&next_cfa, 0, sizeof (next_cfa));
18059 next_cfa.reg = INVALID_REGNUM;
18060 remember = next_cfa;
18062 start_label = fde->dw_fde_begin;
18064 /* ??? Bald assumption that the CIE opcode list does not contain
18065 advance opcodes. */
18066 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
18067 lookup_cfa_1 (cfi, &next_cfa, &remember);
18069 last_cfa = next_cfa;
18070 last_label = start_label;
18072 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18074 /* If the first partition contained no CFI adjustments, the
18075 CIE opcodes apply to the whole first partition. */
18076 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18077 fde->dw_fde_begin, fde->dw_fde_end, section);
18078 list_tail =&(*list_tail)->dw_loc_next;
18079 start_label = last_label = fde->dw_fde_second_begin;
18082 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
18084 switch (cfi->dw_cfi_opc)
18086 case DW_CFA_set_loc:
18087 case DW_CFA_advance_loc1:
18088 case DW_CFA_advance_loc2:
18089 case DW_CFA_advance_loc4:
18090 if (!cfa_equal_p (&last_cfa, &next_cfa))
18092 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18093 start_label, last_label, section);
18095 list_tail = &(*list_tail)->dw_loc_next;
18096 last_cfa = next_cfa;
18097 start_label = last_label;
18099 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18100 break;
18102 case DW_CFA_advance_loc:
18103 /* The encoding is complex enough that we should never emit this. */
18104 gcc_unreachable ();
18106 default:
18107 lookup_cfa_1 (cfi, &next_cfa, &remember);
18108 break;
18110 if (ix + 1 == fde->dw_fde_switch_cfi_index)
18112 if (!cfa_equal_p (&last_cfa, &next_cfa))
18114 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18115 start_label, last_label, section);
18117 list_tail = &(*list_tail)->dw_loc_next;
18118 last_cfa = next_cfa;
18119 start_label = last_label;
18121 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18122 start_label, fde->dw_fde_end, section);
18123 list_tail = &(*list_tail)->dw_loc_next;
18124 start_label = last_label = fde->dw_fde_second_begin;
18128 if (!cfa_equal_p (&last_cfa, &next_cfa))
18130 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18131 start_label, last_label, section);
18132 list_tail = &(*list_tail)->dw_loc_next;
18133 start_label = last_label;
18136 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18137 start_label,
18138 fde->dw_fde_second_begin
18139 ? fde->dw_fde_second_end : fde->dw_fde_end,
18140 section);
18142 if (list && list->dw_loc_next)
18143 gen_llsym (list);
18145 return list;
18148 /* Compute a displacement from the "steady-state frame pointer" to the
18149 frame base (often the same as the CFA), and store it in
18150 frame_pointer_fb_offset. OFFSET is added to the displacement
18151 before the latter is negated. */
18153 static void
18154 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18156 rtx reg, elim;
18158 #ifdef FRAME_POINTER_CFA_OFFSET
18159 reg = frame_pointer_rtx;
18160 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18161 #else
18162 reg = arg_pointer_rtx;
18163 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18164 #endif
18166 elim = (ira_use_lra_p
18167 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
18168 : eliminate_regs (reg, VOIDmode, NULL_RTX));
18169 if (GET_CODE (elim) == PLUS)
18171 offset += INTVAL (XEXP (elim, 1));
18172 elim = XEXP (elim, 0);
18175 frame_pointer_fb_offset = -offset;
18177 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18178 in which to eliminate. This is because it's stack pointer isn't
18179 directly accessible as a register within the ISA. To work around
18180 this, assume that while we cannot provide a proper value for
18181 frame_pointer_fb_offset, we won't need one either. */
18182 frame_pointer_fb_offset_valid
18183 = ((SUPPORTS_STACK_ALIGNMENT
18184 && (elim == hard_frame_pointer_rtx
18185 || elim == stack_pointer_rtx))
18186 || elim == (frame_pointer_needed
18187 ? hard_frame_pointer_rtx
18188 : stack_pointer_rtx));
18191 /* Generate a DW_AT_name attribute given some string value to be included as
18192 the value of the attribute. */
18194 static void
18195 add_name_attribute (dw_die_ref die, const char *name_string)
18197 if (name_string != NULL && *name_string != 0)
18199 if (demangle_name_func)
18200 name_string = (*demangle_name_func) (name_string);
18202 add_AT_string (die, DW_AT_name, name_string);
18206 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18207 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18208 of TYPE accordingly.
18210 ??? This is a temporary measure until after we're able to generate
18211 regular DWARF for the complex Ada type system. */
18213 static void
18214 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18215 dw_die_ref context_die)
18217 tree dtype;
18218 dw_die_ref dtype_die;
18220 if (!lang_hooks.types.descriptive_type)
18221 return;
18223 dtype = lang_hooks.types.descriptive_type (type);
18224 if (!dtype)
18225 return;
18227 dtype_die = lookup_type_die (dtype);
18228 if (!dtype_die)
18230 gen_type_die (dtype, context_die);
18231 dtype_die = lookup_type_die (dtype);
18232 gcc_assert (dtype_die);
18235 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18238 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
18240 static const char *
18241 comp_dir_string (void)
18243 const char *wd;
18244 char *wd1;
18245 static const char *cached_wd = NULL;
18247 if (cached_wd != NULL)
18248 return cached_wd;
18250 wd = get_src_pwd ();
18251 if (wd == NULL)
18252 return NULL;
18254 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18256 int wdlen;
18258 wdlen = strlen (wd);
18259 wd1 = ggc_vec_alloc<char> (wdlen + 2);
18260 strcpy (wd1, wd);
18261 wd1 [wdlen] = DIR_SEPARATOR;
18262 wd1 [wdlen + 1] = 0;
18263 wd = wd1;
18266 cached_wd = remap_debug_filename (wd);
18267 return cached_wd;
18270 /* Generate a DW_AT_comp_dir attribute for DIE. */
18272 static void
18273 add_comp_dir_attribute (dw_die_ref die)
18275 const char * wd = comp_dir_string ();
18276 if (wd != NULL)
18277 add_AT_string (die, DW_AT_comp_dir, wd);
18280 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
18281 pointer computation, ...), output a representation for that bound according
18282 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
18283 loc_list_from_tree for the meaning of CONTEXT. */
18285 static void
18286 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
18287 int forms, const struct loc_descr_context *context)
18289 dw_die_ref context_die, decl_die;
18290 dw_loc_list_ref list;
18292 bool strip_conversions = true;
18294 while (strip_conversions)
18295 switch (TREE_CODE (value))
18297 case ERROR_MARK:
18298 case SAVE_EXPR:
18299 return;
18301 CASE_CONVERT:
18302 case VIEW_CONVERT_EXPR:
18303 value = TREE_OPERAND (value, 0);
18304 break;
18306 default:
18307 strip_conversions = false;
18308 break;
18311 /* If possible and permitted, output the attribute as a constant. */
18312 if ((forms & dw_scalar_form_constant) != 0
18313 && TREE_CODE (value) == INTEGER_CST)
18315 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
18317 /* If HOST_WIDE_INT is big enough then represent the bound as
18318 a constant value. We need to choose a form based on
18319 whether the type is signed or unsigned. We cannot just
18320 call add_AT_unsigned if the value itself is positive
18321 (add_AT_unsigned might add the unsigned value encoded as
18322 DW_FORM_data[1248]). Some DWARF consumers will lookup the
18323 bounds type and then sign extend any unsigned values found
18324 for signed types. This is needed only for
18325 DW_AT_{lower,upper}_bound, since for most other attributes,
18326 consumers will treat DW_FORM_data[1248] as unsigned values,
18327 regardless of the underlying type. */
18328 if (prec <= HOST_BITS_PER_WIDE_INT
18329 || tree_fits_uhwi_p (value))
18331 if (TYPE_UNSIGNED (TREE_TYPE (value)))
18332 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
18333 else
18334 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
18336 else
18337 /* Otherwise represent the bound as an unsigned value with
18338 the precision of its type. The precision and signedness
18339 of the type will be necessary to re-interpret it
18340 unambiguously. */
18341 add_AT_wide (die, attr, value);
18342 return;
18345 /* Otherwise, if it's possible and permitted too, output a reference to
18346 another DIE. */
18347 if ((forms & dw_scalar_form_reference) != 0)
18349 tree decl = NULL_TREE;
18351 /* Some type attributes reference an outer type. For instance, the upper
18352 bound of an array may reference an embedding record (this happens in
18353 Ada). */
18354 if (TREE_CODE (value) == COMPONENT_REF
18355 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
18356 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
18357 decl = TREE_OPERAND (value, 1);
18359 else if (VAR_P (value)
18360 || TREE_CODE (value) == PARM_DECL
18361 || TREE_CODE (value) == RESULT_DECL)
18362 decl = value;
18364 if (decl != NULL_TREE)
18366 dw_die_ref decl_die = lookup_decl_die (decl);
18368 /* ??? Can this happen, or should the variable have been bound
18369 first? Probably it can, since I imagine that we try to create
18370 the types of parameters in the order in which they exist in
18371 the list, and won't have created a forward reference to a
18372 later parameter. */
18373 if (decl_die != NULL)
18375 add_AT_die_ref (die, attr, decl_die);
18376 return;
18381 /* Last chance: try to create a stack operation procedure to evaluate the
18382 value. Do nothing if even that is not possible or permitted. */
18383 if ((forms & dw_scalar_form_exprloc) == 0)
18384 return;
18386 list = loc_list_from_tree (value, 2, context);
18387 if (list == NULL || single_element_loc_list_p (list))
18389 /* If this attribute is not a reference nor constant, it is
18390 a DWARF expression rather than location description. For that
18391 loc_list_from_tree (value, 0, &context) is needed. */
18392 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
18393 if (list2 && single_element_loc_list_p (list2))
18395 add_AT_loc (die, attr, list2->expr);
18396 return;
18400 /* If that failed to give a single element location list, fall back to
18401 outputting this as a reference... still if permitted. */
18402 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
18403 return;
18405 if (current_function_decl == 0)
18406 context_die = comp_unit_die ();
18407 else
18408 context_die = lookup_decl_die (current_function_decl);
18410 decl_die = new_die (DW_TAG_variable, context_die, value);
18411 add_AT_flag (decl_die, DW_AT_artificial, 1);
18412 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, false,
18413 context_die);
18414 add_AT_location_description (decl_die, DW_AT_location, list);
18415 add_AT_die_ref (die, attr, decl_die);
18418 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18419 default. */
18421 static int
18422 lower_bound_default (void)
18424 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18426 case DW_LANG_C:
18427 case DW_LANG_C89:
18428 case DW_LANG_C99:
18429 case DW_LANG_C11:
18430 case DW_LANG_C_plus_plus:
18431 case DW_LANG_C_plus_plus_11:
18432 case DW_LANG_C_plus_plus_14:
18433 case DW_LANG_ObjC:
18434 case DW_LANG_ObjC_plus_plus:
18435 case DW_LANG_Java:
18436 return 0;
18437 case DW_LANG_Fortran77:
18438 case DW_LANG_Fortran90:
18439 case DW_LANG_Fortran95:
18440 case DW_LANG_Fortran03:
18441 case DW_LANG_Fortran08:
18442 return 1;
18443 case DW_LANG_UPC:
18444 case DW_LANG_D:
18445 case DW_LANG_Python:
18446 return dwarf_version >= 4 ? 0 : -1;
18447 case DW_LANG_Ada95:
18448 case DW_LANG_Ada83:
18449 case DW_LANG_Cobol74:
18450 case DW_LANG_Cobol85:
18451 case DW_LANG_Pascal83:
18452 case DW_LANG_Modula2:
18453 case DW_LANG_PLI:
18454 return dwarf_version >= 4 ? 1 : -1;
18455 default:
18456 return -1;
18460 /* Given a tree node describing an array bound (either lower or upper) output
18461 a representation for that bound. */
18463 static void
18464 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
18465 tree bound, const struct loc_descr_context *context)
18467 int dflt;
18469 while (1)
18470 switch (TREE_CODE (bound))
18472 /* Strip all conversions. */
18473 CASE_CONVERT:
18474 case VIEW_CONVERT_EXPR:
18475 bound = TREE_OPERAND (bound, 0);
18476 break;
18478 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
18479 are even omitted when they are the default. */
18480 case INTEGER_CST:
18481 /* If the value for this bound is the default one, we can even omit the
18482 attribute. */
18483 if (bound_attr == DW_AT_lower_bound
18484 && tree_fits_shwi_p (bound)
18485 && (dflt = lower_bound_default ()) != -1
18486 && tree_to_shwi (bound) == dflt)
18487 return;
18489 /* FALLTHRU */
18491 default:
18492 /* Because of the complex interaction there can be with other GNAT
18493 encodings, GDB isn't ready yet to handle proper DWARF description
18494 for self-referencial subrange bounds: let GNAT encodings do the
18495 magic in such a case. */
18496 if (gnat_encodings != DWARF_GNAT_ENCODINGS_MINIMAL
18497 && contains_placeholder_p (bound))
18498 return;
18500 add_scalar_info (subrange_die, bound_attr, bound,
18501 dw_scalar_form_constant
18502 | dw_scalar_form_exprloc
18503 | dw_scalar_form_reference,
18504 context);
18505 return;
18509 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18510 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18511 Note that the block of subscript information for an array type also
18512 includes information about the element type of the given array type.
18514 This function reuses previously set type and bound information if
18515 available. */
18517 static void
18518 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18520 unsigned dimension_number;
18521 tree lower, upper;
18522 dw_die_ref child = type_die->die_child;
18524 for (dimension_number = 0;
18525 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18526 type = TREE_TYPE (type), dimension_number++)
18528 tree domain = TYPE_DOMAIN (type);
18530 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18531 break;
18533 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18534 and (in GNU C only) variable bounds. Handle all three forms
18535 here. */
18537 /* Find and reuse a previously generated DW_TAG_subrange_type if
18538 available.
18540 For multi-dimensional arrays, as we iterate through the
18541 various dimensions in the enclosing for loop above, we also
18542 iterate through the DIE children and pick at each
18543 DW_TAG_subrange_type previously generated (if available).
18544 Each child DW_TAG_subrange_type DIE describes the range of
18545 the current dimension. At this point we should have as many
18546 DW_TAG_subrange_type's as we have dimensions in the
18547 array. */
18548 dw_die_ref subrange_die = NULL;
18549 if (child)
18550 while (1)
18552 child = child->die_sib;
18553 if (child->die_tag == DW_TAG_subrange_type)
18554 subrange_die = child;
18555 if (child == type_die->die_child)
18557 /* If we wrapped around, stop looking next time. */
18558 child = NULL;
18559 break;
18561 if (child->die_tag == DW_TAG_subrange_type)
18562 break;
18564 if (!subrange_die)
18565 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18567 if (domain)
18569 /* We have an array type with specified bounds. */
18570 lower = TYPE_MIN_VALUE (domain);
18571 upper = TYPE_MAX_VALUE (domain);
18573 /* Define the index type. */
18574 if (TREE_TYPE (domain)
18575 && !get_AT (subrange_die, DW_AT_type))
18577 /* ??? This is probably an Ada unnamed subrange type. Ignore the
18578 TREE_TYPE field. We can't emit debug info for this
18579 because it is an unnamed integral type. */
18580 if (TREE_CODE (domain) == INTEGER_TYPE
18581 && TYPE_NAME (domain) == NULL_TREE
18582 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18583 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18585 else
18586 add_type_attribute (subrange_die, TREE_TYPE (domain),
18587 TYPE_UNQUALIFIED, false, type_die);
18590 /* ??? If upper is NULL, the array has unspecified length,
18591 but it does have a lower bound. This happens with Fortran
18592 dimension arr(N:*)
18593 Since the debugger is definitely going to need to know N
18594 to produce useful results, go ahead and output the lower
18595 bound solo, and hope the debugger can cope. */
18597 if (!get_AT (subrange_die, DW_AT_lower_bound))
18598 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
18599 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
18600 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
18603 /* Otherwise we have an array type with an unspecified length. The
18604 DWARF-2 spec does not say how to handle this; let's just leave out the
18605 bounds. */
18609 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
18611 static void
18612 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18614 dw_die_ref decl_die;
18615 HOST_WIDE_INT size;
18616 dw_loc_descr_ref size_expr = NULL;
18618 switch (TREE_CODE (tree_node))
18620 case ERROR_MARK:
18621 size = 0;
18622 break;
18623 case ENUMERAL_TYPE:
18624 case RECORD_TYPE:
18625 case UNION_TYPE:
18626 case QUAL_UNION_TYPE:
18627 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
18628 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
18630 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
18631 return;
18633 size_expr = type_byte_size (tree_node, &size);
18634 break;
18635 case FIELD_DECL:
18636 /* For a data member of a struct or union, the DW_AT_byte_size is
18637 generally given as the number of bytes normally allocated for an
18638 object of the *declared* type of the member itself. This is true
18639 even for bit-fields. */
18640 size = int_size_in_bytes (field_type (tree_node));
18641 break;
18642 default:
18643 gcc_unreachable ();
18646 /* Support for dynamically-sized objects was introduced by DWARFv3.
18647 At the moment, GDB does not handle variable byte sizes very well,
18648 though. */
18649 if ((dwarf_version >= 3 || !dwarf_strict)
18650 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL
18651 && size_expr != NULL)
18652 add_AT_loc (die, DW_AT_byte_size, size_expr);
18654 /* Note that `size' might be -1 when we get to this point. If it is, that
18655 indicates that the byte size of the entity in question is variable and
18656 that we could not generate a DWARF expression that computes it. */
18657 if (size >= 0)
18658 add_AT_unsigned (die, DW_AT_byte_size, size);
18661 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18662 which specifies the distance in bits from the highest order bit of the
18663 "containing object" for the bit-field to the highest order bit of the
18664 bit-field itself.
18666 For any given bit-field, the "containing object" is a hypothetical object
18667 (of some integral or enum type) within which the given bit-field lives. The
18668 type of this hypothetical "containing object" is always the same as the
18669 declared type of the individual bit-field itself. The determination of the
18670 exact location of the "containing object" for a bit-field is rather
18671 complicated. It's handled by the `field_byte_offset' function (above).
18673 CTX is required: see the comment for VLR_CONTEXT.
18675 Note that it is the size (in bytes) of the hypothetical "containing object"
18676 which will be given in the DW_AT_byte_size attribute for this bit-field.
18677 (See `byte_size_attribute' above). */
18679 static inline void
18680 add_bit_offset_attribute (dw_die_ref die, tree decl, struct vlr_context *ctx)
18682 HOST_WIDE_INT object_offset_in_bytes;
18683 tree original_type = DECL_BIT_FIELD_TYPE (decl);
18684 HOST_WIDE_INT bitpos_int;
18685 HOST_WIDE_INT highest_order_object_bit_offset;
18686 HOST_WIDE_INT highest_order_field_bit_offset;
18687 HOST_WIDE_INT bit_offset;
18689 field_byte_offset (decl, ctx, &object_offset_in_bytes);
18691 /* Must be a field and a bit field. */
18692 gcc_assert (original_type && TREE_CODE (decl) == FIELD_DECL);
18694 /* We can't yet handle bit-fields whose offsets are variable, so if we
18695 encounter such things, just return without generating any attribute
18696 whatsoever. Likewise for variable or too large size. */
18697 if (! tree_fits_shwi_p (bit_position (decl))
18698 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
18699 return;
18701 bitpos_int = int_bit_position (decl);
18703 /* Note that the bit offset is always the distance (in bits) from the
18704 highest-order bit of the "containing object" to the highest-order bit of
18705 the bit-field itself. Since the "high-order end" of any object or field
18706 is different on big-endian and little-endian machines, the computation
18707 below must take account of these differences. */
18708 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18709 highest_order_field_bit_offset = bitpos_int;
18711 if (! BYTES_BIG_ENDIAN)
18713 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
18714 highest_order_object_bit_offset +=
18715 simple_type_size_in_bits (original_type);
18718 bit_offset
18719 = (! BYTES_BIG_ENDIAN
18720 ? highest_order_object_bit_offset - highest_order_field_bit_offset
18721 : highest_order_field_bit_offset - highest_order_object_bit_offset);
18723 if (bit_offset < 0)
18724 add_AT_int (die, DW_AT_bit_offset, bit_offset);
18725 else
18726 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18729 /* For a FIELD_DECL node which represents a bit field, output an attribute
18730 which specifies the length in bits of the given field. */
18732 static inline void
18733 add_bit_size_attribute (dw_die_ref die, tree decl)
18735 /* Must be a field and a bit field. */
18736 gcc_assert (TREE_CODE (decl) == FIELD_DECL
18737 && DECL_BIT_FIELD_TYPE (decl));
18739 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
18740 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
18743 /* If the compiled language is ANSI C, then add a 'prototyped'
18744 attribute, if arg types are given for the parameters of a function. */
18746 static inline void
18747 add_prototyped_attribute (dw_die_ref die, tree func_type)
18749 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18751 case DW_LANG_C:
18752 case DW_LANG_C89:
18753 case DW_LANG_C99:
18754 case DW_LANG_C11:
18755 case DW_LANG_ObjC:
18756 if (prototype_p (func_type))
18757 add_AT_flag (die, DW_AT_prototyped, 1);
18758 break;
18759 default:
18760 break;
18764 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
18765 by looking in the type declaration, the object declaration equate table or
18766 the block mapping. */
18768 static inline dw_die_ref
18769 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18771 dw_die_ref origin_die = NULL;
18773 if (TREE_CODE (origin) != FUNCTION_DECL
18774 && TREE_CODE (origin) != BLOCK)
18776 /* We may have gotten separated from the block for the inlined
18777 function, if we're in an exception handler or some such; make
18778 sure that the abstract function has been written out.
18780 Doing this for nested functions is wrong, however; functions are
18781 distinct units, and our context might not even be inline. */
18782 tree fn = origin;
18784 if (TYPE_P (fn))
18785 fn = TYPE_STUB_DECL (fn);
18787 fn = decl_function_context (fn);
18788 if (fn)
18789 dwarf2out_abstract_function (fn);
18792 if (DECL_P (origin))
18793 origin_die = lookup_decl_die (origin);
18794 else if (TYPE_P (origin))
18795 origin_die = lookup_type_die (origin);
18796 else if (TREE_CODE (origin) == BLOCK)
18797 origin_die = BLOCK_DIE (origin);
18799 /* XXX: Functions that are never lowered don't always have correct block
18800 trees (in the case of java, they simply have no block tree, in some other
18801 languages). For these functions, there is nothing we can really do to
18802 output correct debug info for inlined functions in all cases. Rather
18803 than die, we'll just produce deficient debug info now, in that we will
18804 have variables without a proper abstract origin. In the future, when all
18805 functions are lowered, we should re-add a gcc_assert (origin_die)
18806 here. */
18808 if (origin_die)
18809 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18810 return origin_die;
18813 /* We do not currently support the pure_virtual attribute. */
18815 static inline void
18816 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18818 if (DECL_VINDEX (func_decl))
18820 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18822 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
18823 add_AT_loc (die, DW_AT_vtable_elem_location,
18824 new_loc_descr (DW_OP_constu,
18825 tree_to_shwi (DECL_VINDEX (func_decl)),
18826 0));
18828 /* GNU extension: Record what type this method came from originally. */
18829 if (debug_info_level > DINFO_LEVEL_TERSE
18830 && DECL_CONTEXT (func_decl))
18831 add_AT_die_ref (die, DW_AT_containing_type,
18832 lookup_type_die (DECL_CONTEXT (func_decl)));
18836 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18837 given decl. This used to be a vendor extension until after DWARF 4
18838 standardized it. */
18840 static void
18841 add_linkage_attr (dw_die_ref die, tree decl)
18843 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18845 /* Mimic what assemble_name_raw does with a leading '*'. */
18846 if (name[0] == '*')
18847 name = &name[1];
18849 if (dwarf_version >= 4)
18850 add_AT_string (die, DW_AT_linkage_name, name);
18851 else
18852 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18855 /* Add source coordinate attributes for the given decl. */
18857 static void
18858 add_src_coords_attributes (dw_die_ref die, tree decl)
18860 expanded_location s;
18862 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
18863 return;
18864 s = expand_location (DECL_SOURCE_LOCATION (decl));
18865 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18866 add_AT_unsigned (die, DW_AT_decl_line, s.line);
18869 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
18871 static void
18872 add_linkage_name_raw (dw_die_ref die, tree decl)
18874 /* Defer until we have an assembler name set. */
18875 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18877 limbo_die_node *asm_name;
18879 asm_name = ggc_cleared_alloc<limbo_die_node> ();
18880 asm_name->die = die;
18881 asm_name->created_for = decl;
18882 asm_name->next = deferred_asm_name;
18883 deferred_asm_name = asm_name;
18885 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18886 add_linkage_attr (die, decl);
18889 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl if desired. */
18891 static void
18892 add_linkage_name (dw_die_ref die, tree decl)
18894 if (debug_info_level > DINFO_LEVEL_NONE
18895 && VAR_OR_FUNCTION_DECL_P (decl)
18896 && TREE_PUBLIC (decl)
18897 && !(VAR_P (decl) && DECL_REGISTER (decl))
18898 && die->die_tag != DW_TAG_member)
18899 add_linkage_name_raw (die, decl);
18902 /* Add a DW_AT_name attribute and source coordinate attribute for the
18903 given decl, but only if it actually has a name. */
18905 static void
18906 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18908 tree decl_name;
18910 decl_name = DECL_NAME (decl);
18911 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18913 const char *name = dwarf2_name (decl, 0);
18914 if (name)
18915 add_name_attribute (die, name);
18916 if (! DECL_ARTIFICIAL (decl))
18917 add_src_coords_attributes (die, decl);
18919 add_linkage_name (die, decl);
18922 #ifdef VMS_DEBUGGING_INFO
18923 /* Get the function's name, as described by its RTL. This may be different
18924 from the DECL_NAME name used in the source file. */
18925 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18927 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18928 XEXP (DECL_RTL (decl), 0), false);
18929 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
18931 #endif /* VMS_DEBUGGING_INFO */
18934 /* Add VALUE as a DW_AT_discr_value attribute to DIE. */
18936 static void
18937 add_discr_value (dw_die_ref die, dw_discr_value *value)
18939 dw_attr_node attr;
18941 attr.dw_attr = DW_AT_discr_value;
18942 attr.dw_attr_val.val_class = dw_val_class_discr_value;
18943 attr.dw_attr_val.val_entry = NULL;
18944 attr.dw_attr_val.v.val_discr_value.pos = value->pos;
18945 if (value->pos)
18946 attr.dw_attr_val.v.val_discr_value.v.uval = value->v.uval;
18947 else
18948 attr.dw_attr_val.v.val_discr_value.v.sval = value->v.sval;
18949 add_dwarf_attr (die, &attr);
18952 /* Add DISCR_LIST as a DW_AT_discr_list to DIE. */
18954 static void
18955 add_discr_list (dw_die_ref die, dw_discr_list_ref discr_list)
18957 dw_attr_node attr;
18959 attr.dw_attr = DW_AT_discr_list;
18960 attr.dw_attr_val.val_class = dw_val_class_discr_list;
18961 attr.dw_attr_val.val_entry = NULL;
18962 attr.dw_attr_val.v.val_discr_list = discr_list;
18963 add_dwarf_attr (die, &attr);
18966 static inline dw_discr_list_ref
18967 AT_discr_list (dw_attr_node *attr)
18969 return attr->dw_attr_val.v.val_discr_list;
18972 #ifdef VMS_DEBUGGING_INFO
18973 /* Output the debug main pointer die for VMS */
18975 void
18976 dwarf2out_vms_debug_main_pointer (void)
18978 char label[MAX_ARTIFICIAL_LABEL_BYTES];
18979 dw_die_ref die;
18981 /* Allocate the VMS debug main subprogram die. */
18982 die = ggc_cleared_alloc<die_node> ();
18983 die->die_tag = DW_TAG_subprogram;
18984 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18985 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18986 current_function_funcdef_no);
18987 add_AT_lbl_id (die, DW_AT_entry_pc, label);
18989 /* Make it the first child of comp_unit_die (). */
18990 die->die_parent = comp_unit_die ();
18991 if (comp_unit_die ()->die_child)
18993 die->die_sib = comp_unit_die ()->die_child->die_sib;
18994 comp_unit_die ()->die_child->die_sib = die;
18996 else
18998 die->die_sib = die;
18999 comp_unit_die ()->die_child = die;
19002 #endif /* VMS_DEBUGGING_INFO */
19004 /* Push a new declaration scope. */
19006 static void
19007 push_decl_scope (tree scope)
19009 vec_safe_push (decl_scope_table, scope);
19012 /* Pop a declaration scope. */
19014 static inline void
19015 pop_decl_scope (void)
19017 decl_scope_table->pop ();
19020 /* walk_tree helper function for uses_local_type, below. */
19022 static tree
19023 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
19025 if (!TYPE_P (*tp))
19026 *walk_subtrees = 0;
19027 else
19029 tree name = TYPE_NAME (*tp);
19030 if (name && DECL_P (name) && decl_function_context (name))
19031 return *tp;
19033 return NULL_TREE;
19036 /* If TYPE involves a function-local type (including a local typedef to a
19037 non-local type), returns that type; otherwise returns NULL_TREE. */
19039 static tree
19040 uses_local_type (tree type)
19042 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
19043 return used;
19046 /* Return the DIE for the scope that immediately contains this type.
19047 Non-named types that do not involve a function-local type get global
19048 scope. Named types nested in namespaces or other types get their
19049 containing scope. All other types (i.e. function-local named types) get
19050 the current active scope. */
19052 static dw_die_ref
19053 scope_die_for (tree t, dw_die_ref context_die)
19055 dw_die_ref scope_die = NULL;
19056 tree containing_scope;
19058 /* Non-types always go in the current scope. */
19059 gcc_assert (TYPE_P (t));
19061 /* Use the scope of the typedef, rather than the scope of the type
19062 it refers to. */
19063 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
19064 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
19065 else
19066 containing_scope = TYPE_CONTEXT (t);
19068 /* Use the containing namespace if there is one. */
19069 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19071 if (context_die == lookup_decl_die (containing_scope))
19072 /* OK */;
19073 else if (debug_info_level > DINFO_LEVEL_TERSE)
19074 context_die = get_context_die (containing_scope);
19075 else
19076 containing_scope = NULL_TREE;
19079 /* Ignore function type "scopes" from the C frontend. They mean that
19080 a tagged type is local to a parmlist of a function declarator, but
19081 that isn't useful to DWARF. */
19082 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19083 containing_scope = NULL_TREE;
19085 if (SCOPE_FILE_SCOPE_P (containing_scope))
19087 /* If T uses a local type keep it local as well, to avoid references
19088 to function-local DIEs from outside the function. */
19089 if (current_function_decl && uses_local_type (t))
19090 scope_die = context_die;
19091 else
19092 scope_die = comp_unit_die ();
19094 else if (TYPE_P (containing_scope))
19096 /* For types, we can just look up the appropriate DIE. */
19097 if (debug_info_level > DINFO_LEVEL_TERSE)
19098 scope_die = get_context_die (containing_scope);
19099 else
19101 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19102 if (scope_die == NULL)
19103 scope_die = comp_unit_die ();
19106 else
19107 scope_die = context_die;
19109 return scope_die;
19112 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
19114 static inline int
19115 local_scope_p (dw_die_ref context_die)
19117 for (; context_die; context_die = context_die->die_parent)
19118 if (context_die->die_tag == DW_TAG_inlined_subroutine
19119 || context_die->die_tag == DW_TAG_subprogram)
19120 return 1;
19122 return 0;
19125 /* Returns nonzero if CONTEXT_DIE is a class. */
19127 static inline int
19128 class_scope_p (dw_die_ref context_die)
19130 return (context_die
19131 && (context_die->die_tag == DW_TAG_structure_type
19132 || context_die->die_tag == DW_TAG_class_type
19133 || context_die->die_tag == DW_TAG_interface_type
19134 || context_die->die_tag == DW_TAG_union_type));
19137 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19138 whether or not to treat a DIE in this context as a declaration. */
19140 static inline int
19141 class_or_namespace_scope_p (dw_die_ref context_die)
19143 return (class_scope_p (context_die)
19144 || (context_die && context_die->die_tag == DW_TAG_namespace));
19147 /* Many forms of DIEs require a "type description" attribute. This
19148 routine locates the proper "type descriptor" die for the type given
19149 by 'type' plus any additional qualifiers given by 'cv_quals', and
19150 adds a DW_AT_type attribute below the given die. */
19152 static void
19153 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
19154 bool reverse, dw_die_ref context_die)
19156 enum tree_code code = TREE_CODE (type);
19157 dw_die_ref type_die = NULL;
19159 /* ??? If this type is an unnamed subrange type of an integral, floating-point
19160 or fixed-point type, use the inner type. This is because we have no
19161 support for unnamed types in base_type_die. This can happen if this is
19162 an Ada subrange type. Correct solution is emit a subrange type die. */
19163 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19164 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19165 type = TREE_TYPE (type), code = TREE_CODE (type);
19167 if (code == ERROR_MARK
19168 /* Handle a special case. For functions whose return type is void, we
19169 generate *no* type attribute. (Note that no object may have type
19170 `void', so this only applies to function return types). */
19171 || code == VOID_TYPE)
19172 return;
19174 type_die = modified_type_die (type,
19175 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
19176 reverse,
19177 context_die);
19179 if (type_die != NULL)
19180 add_AT_die_ref (object_die, DW_AT_type, type_die);
19183 /* Given an object die, add the calling convention attribute for the
19184 function call type. */
19185 static void
19186 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19188 enum dwarf_calling_convention value = DW_CC_normal;
19190 value = ((enum dwarf_calling_convention)
19191 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19193 if (is_fortran ()
19194 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19196 /* DWARF 2 doesn't provide a way to identify a program's source-level
19197 entry point. DW_AT_calling_convention attributes are only meant
19198 to describe functions' calling conventions. However, lacking a
19199 better way to signal the Fortran main program, we used this for
19200 a long time, following existing custom. Now, DWARF 4 has
19201 DW_AT_main_subprogram, which we add below, but some tools still
19202 rely on the old way, which we thus keep. */
19203 value = DW_CC_program;
19205 if (dwarf_version >= 4 || !dwarf_strict)
19206 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19209 /* Only add the attribute if the backend requests it, and
19210 is not DW_CC_normal. */
19211 if (value && (value != DW_CC_normal))
19212 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19215 /* Given a tree pointer to a struct, class, union, or enum type node, return
19216 a pointer to the (string) tag name for the given type, or zero if the type
19217 was declared without a tag. */
19219 static const char *
19220 type_tag (const_tree type)
19222 const char *name = 0;
19224 if (TYPE_NAME (type) != 0)
19226 tree t = 0;
19228 /* Find the IDENTIFIER_NODE for the type name. */
19229 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19230 && !TYPE_NAMELESS (type))
19231 t = TYPE_NAME (type);
19233 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19234 a TYPE_DECL node, regardless of whether or not a `typedef' was
19235 involved. */
19236 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19237 && ! DECL_IGNORED_P (TYPE_NAME (type)))
19239 /* We want to be extra verbose. Don't call dwarf_name if
19240 DECL_NAME isn't set. The default hook for decl_printable_name
19241 doesn't like that, and in this context it's correct to return
19242 0, instead of "<anonymous>" or the like. */
19243 if (DECL_NAME (TYPE_NAME (type))
19244 && !DECL_NAMELESS (TYPE_NAME (type)))
19245 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19248 /* Now get the name as a string, or invent one. */
19249 if (!name && t != 0)
19250 name = IDENTIFIER_POINTER (t);
19253 return (name == 0 || *name == '\0') ? 0 : name;
19256 /* Return the type associated with a data member, make a special check
19257 for bit field types. */
19259 static inline tree
19260 member_declared_type (const_tree member)
19262 return (DECL_BIT_FIELD_TYPE (member)
19263 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19266 /* Get the decl's label, as described by its RTL. This may be different
19267 from the DECL_NAME name used in the source file. */
19269 #if 0
19270 static const char *
19271 decl_start_label (tree decl)
19273 rtx x;
19274 const char *fnname;
19276 x = DECL_RTL (decl);
19277 gcc_assert (MEM_P (x));
19279 x = XEXP (x, 0);
19280 gcc_assert (GET_CODE (x) == SYMBOL_REF);
19282 fnname = XSTR (x, 0);
19283 return fnname;
19285 #endif
19287 /* For variable-length arrays that have been previously generated, but
19288 may be incomplete due to missing subscript info, fill the subscript
19289 info. Return TRUE if this is one of those cases. */
19290 static bool
19291 fill_variable_array_bounds (tree type)
19293 if (TREE_ASM_WRITTEN (type)
19294 && TREE_CODE (type) == ARRAY_TYPE
19295 && variably_modified_type_p (type, NULL))
19297 dw_die_ref array_die = lookup_type_die (type);
19298 if (!array_die)
19299 return false;
19300 add_subscript_info (array_die, type, !is_ada ());
19301 return true;
19303 return false;
19306 /* These routines generate the internal representation of the DIE's for
19307 the compilation unit. Debugging information is collected by walking
19308 the declaration trees passed in from dwarf2out_decl(). */
19310 static void
19311 gen_array_type_die (tree type, dw_die_ref context_die)
19313 dw_die_ref array_die;
19315 /* GNU compilers represent multidimensional array types as sequences of one
19316 dimensional array types whose element types are themselves array types.
19317 We sometimes squish that down to a single array_type DIE with multiple
19318 subscripts in the Dwarf debugging info. The draft Dwarf specification
19319 say that we are allowed to do this kind of compression in C, because
19320 there is no difference between an array of arrays and a multidimensional
19321 array. We don't do this for Ada to remain as close as possible to the
19322 actual representation, which is especially important against the language
19323 flexibilty wrt arrays of variable size. */
19325 bool collapse_nested_arrays = !is_ada ();
19327 if (fill_variable_array_bounds (type))
19328 return;
19330 dw_die_ref scope_die = scope_die_for (type, context_die);
19331 tree element_type;
19333 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19334 DW_TAG_string_type doesn't have DW_AT_type attribute). */
19335 if (TYPE_STRING_FLAG (type)
19336 && TREE_CODE (type) == ARRAY_TYPE
19337 && is_fortran ()
19338 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19340 HOST_WIDE_INT size;
19342 array_die = new_die (DW_TAG_string_type, scope_die, type);
19343 add_name_attribute (array_die, type_tag (type));
19344 equate_type_number_to_die (type, array_die);
19345 size = int_size_in_bytes (type);
19346 if (size >= 0)
19347 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19348 else if (TYPE_DOMAIN (type) != NULL_TREE
19349 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE)
19351 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19352 tree rszdecl = szdecl;
19353 HOST_WIDE_INT rsize = 0;
19355 size = int_size_in_bytes (TREE_TYPE (szdecl));
19356 if (!DECL_P (szdecl))
19358 if (TREE_CODE (szdecl) == INDIRECT_REF
19359 && DECL_P (TREE_OPERAND (szdecl, 0)))
19361 rszdecl = TREE_OPERAND (szdecl, 0);
19362 rsize = int_size_in_bytes (TREE_TYPE (rszdecl));
19363 if (rsize <= 0)
19364 size = 0;
19366 else
19367 size = 0;
19369 if (size > 0)
19371 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
19372 if (loc == NULL
19373 && early_dwarf
19374 && current_function_decl
19375 && DECL_CONTEXT (rszdecl) == current_function_decl)
19377 dw_die_ref ref = lookup_decl_die (rszdecl);
19378 dw_loc_descr_ref l = NULL;
19379 if (ref)
19381 l = new_loc_descr (DW_OP_call4, 0, 0);
19382 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19383 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
19384 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
19386 else if (TREE_CODE (rszdecl) == PARM_DECL
19387 && string_types)
19389 l = new_loc_descr (DW_OP_call4, 0, 0);
19390 l->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
19391 l->dw_loc_oprnd1.v.val_decl_ref = rszdecl;
19392 string_types->safe_push (array_die);
19394 if (l && rszdecl != szdecl)
19396 if (rsize == DWARF2_ADDR_SIZE)
19397 add_loc_descr (&l, new_loc_descr (DW_OP_deref,
19398 0, 0));
19399 else
19400 add_loc_descr (&l, new_loc_descr (DW_OP_deref_size,
19401 rsize, 0));
19403 if (l)
19404 loc = new_loc_list (l, NULL, NULL, NULL);
19406 if (loc)
19408 add_AT_location_description (array_die, DW_AT_string_length,
19409 loc);
19410 if (size != DWARF2_ADDR_SIZE)
19411 add_AT_unsigned (array_die, dwarf_version >= 5
19412 ? DW_AT_string_length_byte_size
19413 : DW_AT_byte_size, size);
19417 return;
19420 array_die = new_die (DW_TAG_array_type, scope_die, type);
19421 add_name_attribute (array_die, type_tag (type));
19422 equate_type_number_to_die (type, array_die);
19424 if (TREE_CODE (type) == VECTOR_TYPE)
19425 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19427 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
19428 if (is_fortran ()
19429 && TREE_CODE (type) == ARRAY_TYPE
19430 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19431 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19432 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19434 #if 0
19435 /* We default the array ordering. SDB will probably do
19436 the right things even if DW_AT_ordering is not present. It's not even
19437 an issue until we start to get into multidimensional arrays anyway. If
19438 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19439 then we'll have to put the DW_AT_ordering attribute back in. (But if
19440 and when we find out that we need to put these in, we will only do so
19441 for multidimensional arrays. */
19442 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19443 #endif
19445 if (TREE_CODE (type) == VECTOR_TYPE)
19447 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
19448 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19449 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
19450 add_bound_info (subrange_die, DW_AT_upper_bound,
19451 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
19453 else
19454 add_subscript_info (array_die, type, collapse_nested_arrays);
19456 /* Add representation of the type of the elements of this array type and
19457 emit the corresponding DIE if we haven't done it already. */
19458 element_type = TREE_TYPE (type);
19459 if (collapse_nested_arrays)
19460 while (TREE_CODE (element_type) == ARRAY_TYPE)
19462 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19463 break;
19464 element_type = TREE_TYPE (element_type);
19467 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED,
19468 TREE_CODE (type) == ARRAY_TYPE
19469 && TYPE_REVERSE_STORAGE_ORDER (type),
19470 context_die);
19472 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19473 if (TYPE_ARTIFICIAL (type))
19474 add_AT_flag (array_die, DW_AT_artificial, 1);
19476 if (get_AT (array_die, DW_AT_name))
19477 add_pubtype (type, array_die);
19480 /* After all arguments are created, adjust any DW_TAG_string_type
19481 DIEs DW_AT_string_length attributes. */
19483 static void
19484 adjust_string_types (void)
19486 dw_die_ref array_die;
19487 unsigned int i;
19488 FOR_EACH_VEC_ELT (*string_types, i, array_die)
19490 dw_attr_node *a = get_AT (array_die, DW_AT_string_length);
19491 if (a == NULL)
19492 continue;
19493 dw_loc_descr_ref loc = AT_loc (a);
19494 gcc_assert (loc->dw_loc_opc == DW_OP_call4
19495 && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref);
19496 dw_die_ref ref = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
19497 if (ref)
19499 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
19500 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
19501 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
19503 else
19505 remove_AT (array_die, DW_AT_string_length);
19506 remove_AT (array_die, dwarf_version >= 5
19507 ? DW_AT_string_length_byte_size
19508 : DW_AT_byte_size);
19513 /* This routine generates DIE for array with hidden descriptor, details
19514 are filled into *info by a langhook. */
19516 static void
19517 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19518 dw_die_ref context_die)
19520 const dw_die_ref scope_die = scope_die_for (type, context_die);
19521 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
19522 const struct loc_descr_context context = { type, info->base_decl, NULL };
19523 int dim;
19525 add_name_attribute (array_die, type_tag (type));
19526 equate_type_number_to_die (type, array_die);
19528 if (info->ndimensions > 1)
19529 switch (info->ordering)
19531 case array_descr_ordering_row_major:
19532 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19533 break;
19534 case array_descr_ordering_column_major:
19535 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19536 break;
19537 default:
19538 break;
19541 if (dwarf_version >= 3 || !dwarf_strict)
19543 if (info->data_location)
19544 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
19545 dw_scalar_form_exprloc, &context);
19546 if (info->associated)
19547 add_scalar_info (array_die, DW_AT_associated, info->associated,
19548 dw_scalar_form_constant
19549 | dw_scalar_form_exprloc
19550 | dw_scalar_form_reference, &context);
19551 if (info->allocated)
19552 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
19553 dw_scalar_form_constant
19554 | dw_scalar_form_exprloc
19555 | dw_scalar_form_reference, &context);
19556 if (info->stride)
19558 const enum dwarf_attribute attr
19559 = (info->stride_in_bits) ? DW_AT_bit_stride : DW_AT_byte_stride;
19560 const int forms
19561 = (info->stride_in_bits)
19562 ? dw_scalar_form_constant
19563 : (dw_scalar_form_constant
19564 | dw_scalar_form_exprloc
19565 | dw_scalar_form_reference);
19567 add_scalar_info (array_die, attr, info->stride, forms, &context);
19571 add_gnat_descriptive_type_attribute (array_die, type, context_die);
19573 for (dim = 0; dim < info->ndimensions; dim++)
19575 dw_die_ref subrange_die
19576 = new_die (DW_TAG_subrange_type, array_die, NULL);
19578 if (info->dimen[dim].bounds_type)
19579 add_type_attribute (subrange_die,
19580 info->dimen[dim].bounds_type, TYPE_UNQUALIFIED,
19581 false, context_die);
19582 if (info->dimen[dim].lower_bound)
19583 add_bound_info (subrange_die, DW_AT_lower_bound,
19584 info->dimen[dim].lower_bound, &context);
19585 if (info->dimen[dim].upper_bound)
19586 add_bound_info (subrange_die, DW_AT_upper_bound,
19587 info->dimen[dim].upper_bound, &context);
19588 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
19589 add_scalar_info (subrange_die, DW_AT_byte_stride,
19590 info->dimen[dim].stride,
19591 dw_scalar_form_constant
19592 | dw_scalar_form_exprloc
19593 | dw_scalar_form_reference,
19594 &context);
19597 gen_type_die (info->element_type, context_die);
19598 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
19599 TREE_CODE (type) == ARRAY_TYPE
19600 && TYPE_REVERSE_STORAGE_ORDER (type),
19601 context_die);
19603 if (get_AT (array_die, DW_AT_name))
19604 add_pubtype (type, array_die);
19607 #if 0
19608 static void
19609 gen_entry_point_die (tree decl, dw_die_ref context_die)
19611 tree origin = decl_ultimate_origin (decl);
19612 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19614 if (origin != NULL)
19615 add_abstract_origin_attribute (decl_die, origin);
19616 else
19618 add_name_and_src_coords_attributes (decl_die, decl);
19619 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19620 TYPE_UNQUALIFIED, false, context_die);
19623 if (DECL_ABSTRACT_P (decl))
19624 equate_decl_number_to_die (decl, decl_die);
19625 else
19626 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19628 #endif
19630 /* Walk through the list of incomplete types again, trying once more to
19631 emit full debugging info for them. */
19633 static void
19634 retry_incomplete_types (void)
19636 set_early_dwarf s;
19637 int i;
19639 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
19640 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
19641 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
19642 vec_safe_truncate (incomplete_types, 0);
19645 /* Determine what tag to use for a record type. */
19647 static enum dwarf_tag
19648 record_type_tag (tree type)
19650 if (! lang_hooks.types.classify_record)
19651 return DW_TAG_structure_type;
19653 switch (lang_hooks.types.classify_record (type))
19655 case RECORD_IS_STRUCT:
19656 return DW_TAG_structure_type;
19658 case RECORD_IS_CLASS:
19659 return DW_TAG_class_type;
19661 case RECORD_IS_INTERFACE:
19662 if (dwarf_version >= 3 || !dwarf_strict)
19663 return DW_TAG_interface_type;
19664 return DW_TAG_structure_type;
19666 default:
19667 gcc_unreachable ();
19671 /* Generate a DIE to represent an enumeration type. Note that these DIEs
19672 include all of the information about the enumeration values also. Each
19673 enumerated type name/value is listed as a child of the enumerated type
19674 DIE. */
19676 static dw_die_ref
19677 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19679 dw_die_ref type_die = lookup_type_die (type);
19681 if (type_die == NULL)
19683 type_die = new_die (DW_TAG_enumeration_type,
19684 scope_die_for (type, context_die), type);
19685 equate_type_number_to_die (type, type_die);
19686 add_name_attribute (type_die, type_tag (type));
19687 if (dwarf_version >= 4 || !dwarf_strict)
19689 if (ENUM_IS_SCOPED (type))
19690 add_AT_flag (type_die, DW_AT_enum_class, 1);
19691 if (ENUM_IS_OPAQUE (type))
19692 add_AT_flag (type_die, DW_AT_declaration, 1);
19695 else if (! TYPE_SIZE (type))
19696 return type_die;
19697 else
19698 remove_AT (type_die, DW_AT_declaration);
19700 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
19701 given enum type is incomplete, do not generate the DW_AT_byte_size
19702 attribute or the DW_AT_element_list attribute. */
19703 if (TYPE_SIZE (type))
19705 tree link;
19707 TREE_ASM_WRITTEN (type) = 1;
19708 add_byte_size_attribute (type_die, type);
19709 if (dwarf_version >= 3 || !dwarf_strict)
19711 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
19712 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED, false,
19713 context_die);
19715 if (TYPE_STUB_DECL (type) != NULL_TREE)
19717 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19718 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19721 /* If the first reference to this type was as the return type of an
19722 inline function, then it may not have a parent. Fix this now. */
19723 if (type_die->die_parent == NULL)
19724 add_child_die (scope_die_for (type, context_die), type_die);
19726 for (link = TYPE_VALUES (type);
19727 link != NULL; link = TREE_CHAIN (link))
19729 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19730 tree value = TREE_VALUE (link);
19732 add_name_attribute (enum_die,
19733 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19735 if (TREE_CODE (value) == CONST_DECL)
19736 value = DECL_INITIAL (value);
19738 if (simple_type_size_in_bits (TREE_TYPE (value))
19739 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
19741 /* For constant forms created by add_AT_unsigned DWARF
19742 consumers (GDB, elfutils, etc.) always zero extend
19743 the value. Only when the actual value is negative
19744 do we need to use add_AT_int to generate a constant
19745 form that can represent negative values. */
19746 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
19747 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
19748 add_AT_unsigned (enum_die, DW_AT_const_value,
19749 (unsigned HOST_WIDE_INT) val);
19750 else
19751 add_AT_int (enum_die, DW_AT_const_value, val);
19753 else
19754 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
19755 that here. TODO: This should be re-worked to use correct
19756 signed/unsigned double tags for all cases. */
19757 add_AT_wide (enum_die, DW_AT_const_value, value);
19760 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19761 if (TYPE_ARTIFICIAL (type))
19762 add_AT_flag (type_die, DW_AT_artificial, 1);
19764 else
19765 add_AT_flag (type_die, DW_AT_declaration, 1);
19767 add_pubtype (type, type_die);
19769 return type_die;
19772 /* Generate a DIE to represent either a real live formal parameter decl or to
19773 represent just the type of some formal parameter position in some function
19774 type.
19776 Note that this routine is a bit unusual because its argument may be a
19777 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19778 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19779 node. If it's the former then this function is being called to output a
19780 DIE to represent a formal parameter object (or some inlining thereof). If
19781 it's the latter, then this function is only being called to output a
19782 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19783 argument type of some subprogram type.
19784 If EMIT_NAME_P is true, name and source coordinate attributes
19785 are emitted. */
19787 static dw_die_ref
19788 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19789 dw_die_ref context_die)
19791 tree node_or_origin = node ? node : origin;
19792 tree ultimate_origin;
19793 dw_die_ref parm_die = NULL;
19795 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
19797 parm_die = lookup_decl_die (node);
19799 /* If the contexts differ, we may not be talking about the same
19800 thing. */
19801 if (parm_die && parm_die->die_parent != context_die)
19803 if (!DECL_ABSTRACT_P (node))
19805 /* This can happen when creating an inlined instance, in
19806 which case we need to create a new DIE that will get
19807 annotated with DW_AT_abstract_origin. */
19808 parm_die = NULL;
19810 else
19812 /* FIXME: Reuse DIE even with a differing context.
19814 This can happen when calling
19815 dwarf2out_abstract_function to build debug info for
19816 the abstract instance of a function for which we have
19817 already generated a DIE in
19818 dwarf2out_early_global_decl.
19820 Once we remove dwarf2out_abstract_function, we should
19821 have a call to gcc_unreachable here. */
19825 if (parm_die && parm_die->die_parent == NULL)
19827 /* Check that parm_die already has the right attributes that
19828 we would have added below. If any attributes are
19829 missing, fall through to add them. */
19830 if (! DECL_ABSTRACT_P (node_or_origin)
19831 && !get_AT (parm_die, DW_AT_location)
19832 && !get_AT (parm_die, DW_AT_const_value))
19833 /* We are missing location info, and are about to add it. */
19835 else
19837 add_child_die (context_die, parm_die);
19838 return parm_die;
19843 /* If we have a previously generated DIE, use it, unless this is an
19844 concrete instance (origin != NULL), in which case we need a new
19845 DIE with a corresponding DW_AT_abstract_origin. */
19846 bool reusing_die;
19847 if (parm_die && origin == NULL)
19848 reusing_die = true;
19849 else
19851 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
19852 reusing_die = false;
19855 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19857 case tcc_declaration:
19858 ultimate_origin = decl_ultimate_origin (node_or_origin);
19859 if (node || ultimate_origin)
19860 origin = ultimate_origin;
19862 if (reusing_die)
19863 goto add_location;
19865 if (origin != NULL)
19866 add_abstract_origin_attribute (parm_die, origin);
19867 else if (emit_name_p)
19868 add_name_and_src_coords_attributes (parm_die, node);
19869 if (origin == NULL
19870 || (! DECL_ABSTRACT_P (node_or_origin)
19871 && variably_modified_type_p (TREE_TYPE (node_or_origin),
19872 decl_function_context
19873 (node_or_origin))))
19875 tree type = TREE_TYPE (node_or_origin);
19876 if (decl_by_reference_p (node_or_origin))
19877 add_type_attribute (parm_die, TREE_TYPE (type),
19878 TYPE_UNQUALIFIED,
19879 false, context_die);
19880 else
19881 add_type_attribute (parm_die, type,
19882 decl_quals (node_or_origin),
19883 false, context_die);
19885 if (origin == NULL && DECL_ARTIFICIAL (node))
19886 add_AT_flag (parm_die, DW_AT_artificial, 1);
19887 add_location:
19888 if (node && node != origin)
19889 equate_decl_number_to_die (node, parm_die);
19890 if (! DECL_ABSTRACT_P (node_or_origin))
19891 add_location_or_const_value_attribute (parm_die, node_or_origin,
19892 node == NULL);
19894 break;
19896 case tcc_type:
19897 /* We were called with some kind of a ..._TYPE node. */
19898 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED, false,
19899 context_die);
19900 break;
19902 default:
19903 gcc_unreachable ();
19906 return parm_die;
19909 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19910 children DW_TAG_formal_parameter DIEs representing the arguments of the
19911 parameter pack.
19913 PARM_PACK must be a function parameter pack.
19914 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19915 must point to the subsequent arguments of the function PACK_ARG belongs to.
19916 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19917 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19918 following the last one for which a DIE was generated. */
19920 static dw_die_ref
19921 gen_formal_parameter_pack_die (tree parm_pack,
19922 tree pack_arg,
19923 dw_die_ref subr_die,
19924 tree *next_arg)
19926 tree arg;
19927 dw_die_ref parm_pack_die;
19929 gcc_assert (parm_pack
19930 && lang_hooks.function_parameter_pack_p (parm_pack)
19931 && subr_die);
19933 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19934 add_src_coords_attributes (parm_pack_die, parm_pack);
19936 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19938 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19939 parm_pack))
19940 break;
19941 gen_formal_parameter_die (arg, NULL,
19942 false /* Don't emit name attribute. */,
19943 parm_pack_die);
19945 if (next_arg)
19946 *next_arg = arg;
19947 return parm_pack_die;
19950 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19951 at the end of an (ANSI prototyped) formal parameters list. */
19953 static void
19954 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19956 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19959 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19960 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19961 parameters as specified in some function type specification (except for
19962 those which appear as part of a function *definition*). */
19964 static void
19965 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19967 tree link;
19968 tree formal_type = NULL;
19969 tree first_parm_type;
19970 tree arg;
19972 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19974 arg = DECL_ARGUMENTS (function_or_method_type);
19975 function_or_method_type = TREE_TYPE (function_or_method_type);
19977 else
19978 arg = NULL_TREE;
19980 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19982 /* Make our first pass over the list of formal parameter types and output a
19983 DW_TAG_formal_parameter DIE for each one. */
19984 for (link = first_parm_type; link; )
19986 dw_die_ref parm_die;
19988 formal_type = TREE_VALUE (link);
19989 if (formal_type == void_type_node)
19990 break;
19992 /* Output a (nameless) DIE to represent the formal parameter itself. */
19993 if (!POINTER_BOUNDS_TYPE_P (formal_type))
19995 parm_die = gen_formal_parameter_die (formal_type, NULL,
19996 true /* Emit name attribute. */,
19997 context_die);
19998 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19999 && link == first_parm_type)
20001 add_AT_flag (parm_die, DW_AT_artificial, 1);
20002 if (dwarf_version >= 3 || !dwarf_strict)
20003 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
20005 else if (arg && DECL_ARTIFICIAL (arg))
20006 add_AT_flag (parm_die, DW_AT_artificial, 1);
20009 link = TREE_CHAIN (link);
20010 if (arg)
20011 arg = DECL_CHAIN (arg);
20014 /* If this function type has an ellipsis, add a
20015 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
20016 if (formal_type != void_type_node)
20017 gen_unspecified_parameters_die (function_or_method_type, context_die);
20019 /* Make our second (and final) pass over the list of formal parameter types
20020 and output DIEs to represent those types (as necessary). */
20021 for (link = TYPE_ARG_TYPES (function_or_method_type);
20022 link && TREE_VALUE (link);
20023 link = TREE_CHAIN (link))
20024 gen_type_die (TREE_VALUE (link), context_die);
20027 /* We want to generate the DIE for TYPE so that we can generate the
20028 die for MEMBER, which has been defined; we will need to refer back
20029 to the member declaration nested within TYPE. If we're trying to
20030 generate minimal debug info for TYPE, processing TYPE won't do the
20031 trick; we need to attach the member declaration by hand. */
20033 static void
20034 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
20036 gen_type_die (type, context_die);
20038 /* If we're trying to avoid duplicate debug info, we may not have
20039 emitted the member decl for this function. Emit it now. */
20040 if (TYPE_STUB_DECL (type)
20041 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
20042 && ! lookup_decl_die (member))
20044 dw_die_ref type_die;
20045 gcc_assert (!decl_ultimate_origin (member));
20047 push_decl_scope (type);
20048 type_die = lookup_type_die_strip_naming_typedef (type);
20049 if (TREE_CODE (member) == FUNCTION_DECL)
20050 gen_subprogram_die (member, type_die);
20051 else if (TREE_CODE (member) == FIELD_DECL)
20053 /* Ignore the nameless fields that are used to skip bits but handle
20054 C++ anonymous unions and structs. */
20055 if (DECL_NAME (member) != NULL_TREE
20056 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20057 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20059 struct vlr_context vlr_ctx = {
20060 DECL_CONTEXT (member), /* struct_type */
20061 NULL_TREE /* variant_part_offset */
20063 gen_type_die (member_declared_type (member), type_die);
20064 gen_field_die (member, &vlr_ctx, type_die);
20067 else
20068 gen_variable_die (member, NULL_TREE, type_die);
20070 pop_decl_scope ();
20074 /* Forward declare these functions, because they are mutually recursive
20075 with their set_block_* pairing functions. */
20076 static void set_decl_origin_self (tree);
20077 static void set_decl_abstract_flags (tree, vec<tree> &);
20079 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
20080 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
20081 that it points to the node itself, thus indicating that the node is its
20082 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
20083 the given node is NULL, recursively descend the decl/block tree which
20084 it is the root of, and for each other ..._DECL or BLOCK node contained
20085 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
20086 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
20087 values to point to themselves. */
20089 static void
20090 set_block_origin_self (tree stmt)
20092 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
20094 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
20097 tree local_decl;
20099 for (local_decl = BLOCK_VARS (stmt);
20100 local_decl != NULL_TREE;
20101 local_decl = DECL_CHAIN (local_decl))
20102 /* Do not recurse on nested functions since the inlining status
20103 of parent and child can be different as per the DWARF spec. */
20104 if (TREE_CODE (local_decl) != FUNCTION_DECL
20105 && !DECL_EXTERNAL (local_decl))
20106 set_decl_origin_self (local_decl);
20110 tree subblock;
20112 for (subblock = BLOCK_SUBBLOCKS (stmt);
20113 subblock != NULL_TREE;
20114 subblock = BLOCK_CHAIN (subblock))
20115 set_block_origin_self (subblock); /* Recurse. */
20120 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
20121 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
20122 node to so that it points to the node itself, thus indicating that the
20123 node represents its own (abstract) origin. Additionally, if the
20124 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
20125 the decl/block tree of which the given node is the root of, and for
20126 each other ..._DECL or BLOCK node contained therein whose
20127 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
20128 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
20129 point to themselves. */
20131 static void
20132 set_decl_origin_self (tree decl)
20134 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
20136 DECL_ABSTRACT_ORIGIN (decl) = decl;
20137 if (TREE_CODE (decl) == FUNCTION_DECL)
20139 tree arg;
20141 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20142 DECL_ABSTRACT_ORIGIN (arg) = arg;
20143 if (DECL_INITIAL (decl) != NULL_TREE
20144 && DECL_INITIAL (decl) != error_mark_node)
20145 set_block_origin_self (DECL_INITIAL (decl));
20150 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
20151 and if it wasn't 1 before, push it to abstract_vec vector.
20152 For all local decls and all local sub-blocks (recursively) do it
20153 too. */
20155 static void
20156 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
20158 tree local_decl;
20159 tree subblock;
20160 unsigned int i;
20162 if (!BLOCK_ABSTRACT (stmt))
20164 abstract_vec.safe_push (stmt);
20165 BLOCK_ABSTRACT (stmt) = 1;
20168 for (local_decl = BLOCK_VARS (stmt);
20169 local_decl != NULL_TREE;
20170 local_decl = DECL_CHAIN (local_decl))
20171 if (! DECL_EXTERNAL (local_decl))
20172 set_decl_abstract_flags (local_decl, abstract_vec);
20174 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20176 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
20177 if ((VAR_P (local_decl) && !TREE_STATIC (local_decl))
20178 || TREE_CODE (local_decl) == PARM_DECL)
20179 set_decl_abstract_flags (local_decl, abstract_vec);
20182 for (subblock = BLOCK_SUBBLOCKS (stmt);
20183 subblock != NULL_TREE;
20184 subblock = BLOCK_CHAIN (subblock))
20185 set_block_abstract_flags (subblock, abstract_vec);
20188 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
20189 to 1 and if it wasn't 1 before, push to abstract_vec vector.
20190 In the case where the decl is a FUNCTION_DECL also set the abstract
20191 flags for all of the parameters, local vars, local
20192 blocks and sub-blocks (recursively). */
20194 static void
20195 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
20197 if (!DECL_ABSTRACT_P (decl))
20199 abstract_vec.safe_push (decl);
20200 DECL_ABSTRACT_P (decl) = 1;
20203 if (TREE_CODE (decl) == FUNCTION_DECL)
20205 tree arg;
20207 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
20208 if (!DECL_ABSTRACT_P (arg))
20210 abstract_vec.safe_push (arg);
20211 DECL_ABSTRACT_P (arg) = 1;
20213 if (DECL_INITIAL (decl) != NULL_TREE
20214 && DECL_INITIAL (decl) != error_mark_node)
20215 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
20219 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20220 may later generate inlined and/or out-of-line instances of.
20222 FIXME: In the early-dwarf world, this function, and most of the
20223 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
20224 the abstract instance. All we would need to do is annotate
20225 the early DIE with the appropriate DW_AT_inline in late
20226 dwarf (perhaps in gen_inlined_subroutine_die).
20228 However, we can't do this yet, because LTO streaming of DIEs
20229 has not been implemented yet. */
20231 static void
20232 dwarf2out_abstract_function (tree decl)
20234 dw_die_ref old_die;
20235 tree save_fn;
20236 tree context;
20237 hash_table<decl_loc_hasher> *old_decl_loc_table;
20238 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
20239 int old_call_site_count, old_tail_call_site_count;
20240 struct call_arg_loc_node *old_call_arg_locations;
20242 /* Make sure we have the actual abstract inline, not a clone. */
20243 decl = DECL_ORIGIN (decl);
20245 old_die = lookup_decl_die (decl);
20246 if (old_die && get_AT (old_die, DW_AT_inline))
20247 /* We've already generated the abstract instance. */
20248 return;
20250 /* We can be called while recursively when seeing block defining inlined subroutine
20251 DIE. Be sure to not clobber the outer location table nor use it or we would
20252 get locations in abstract instantces. */
20253 old_decl_loc_table = decl_loc_table;
20254 decl_loc_table = NULL;
20255 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20256 cached_dw_loc_list_table = NULL;
20257 old_call_arg_locations = call_arg_locations;
20258 call_arg_locations = NULL;
20259 old_call_site_count = call_site_count;
20260 call_site_count = -1;
20261 old_tail_call_site_count = tail_call_site_count;
20262 tail_call_site_count = -1;
20264 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20265 we don't get confused by DECL_ABSTRACT_P. */
20266 if (debug_info_level > DINFO_LEVEL_TERSE)
20268 context = decl_class_context (decl);
20269 if (context)
20270 gen_type_die_for_member
20271 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20274 /* Pretend we've just finished compiling this function. */
20275 save_fn = current_function_decl;
20276 current_function_decl = decl;
20278 auto_vec<tree, 64> abstract_vec;
20279 set_decl_abstract_flags (decl, abstract_vec);
20280 dwarf2out_decl (decl);
20281 unsigned int i;
20282 tree t;
20283 FOR_EACH_VEC_ELT (abstract_vec, i, t)
20284 if (TREE_CODE (t) == BLOCK)
20285 BLOCK_ABSTRACT (t) = 0;
20286 else
20287 DECL_ABSTRACT_P (t) = 0;
20289 current_function_decl = save_fn;
20290 decl_loc_table = old_decl_loc_table;
20291 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20292 call_arg_locations = old_call_arg_locations;
20293 call_site_count = old_call_site_count;
20294 tail_call_site_count = old_tail_call_site_count;
20297 /* Helper function of premark_used_types() which gets called through
20298 htab_traverse.
20300 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20301 marked as unused by prune_unused_types. */
20303 bool
20304 premark_used_types_helper (tree const &type, void *)
20306 dw_die_ref die;
20308 die = lookup_type_die (type);
20309 if (die != NULL)
20310 die->die_perennial_p = 1;
20311 return true;
20314 /* Helper function of premark_types_used_by_global_vars which gets called
20315 through htab_traverse.
20317 Marks the DIE of a given type in *SLOT as perennial, so it never gets
20318 marked as unused by prune_unused_types. The DIE of the type is marked
20319 only if the global variable using the type will actually be emitted. */
20322 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
20323 void *)
20325 struct types_used_by_vars_entry *entry;
20326 dw_die_ref die;
20328 entry = (struct types_used_by_vars_entry *) *slot;
20329 gcc_assert (entry->type != NULL
20330 && entry->var_decl != NULL);
20331 die = lookup_type_die (entry->type);
20332 if (die)
20334 /* Ask cgraph if the global variable really is to be emitted.
20335 If yes, then we'll keep the DIE of ENTRY->TYPE. */
20336 varpool_node *node = varpool_node::get (entry->var_decl);
20337 if (node && node->definition)
20339 die->die_perennial_p = 1;
20340 /* Keep the parent DIEs as well. */
20341 while ((die = die->die_parent) && die->die_perennial_p == 0)
20342 die->die_perennial_p = 1;
20345 return 1;
20348 /* Mark all members of used_types_hash as perennial. */
20350 static void
20351 premark_used_types (struct function *fun)
20353 if (fun && fun->used_types_hash)
20354 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
20357 /* Mark all members of types_used_by_vars_entry as perennial. */
20359 static void
20360 premark_types_used_by_global_vars (void)
20362 if (types_used_by_vars_hash)
20363 types_used_by_vars_hash
20364 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
20367 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20368 for CA_LOC call arg loc node. */
20370 static dw_die_ref
20371 gen_call_site_die (tree decl, dw_die_ref subr_die,
20372 struct call_arg_loc_node *ca_loc)
20374 dw_die_ref stmt_die = NULL, die;
20375 tree block = ca_loc->block;
20377 while (block
20378 && block != DECL_INITIAL (decl)
20379 && TREE_CODE (block) == BLOCK)
20381 stmt_die = BLOCK_DIE (block);
20382 if (stmt_die)
20383 break;
20384 block = BLOCK_SUPERCONTEXT (block);
20386 if (stmt_die == NULL)
20387 stmt_die = subr_die;
20388 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20389 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20390 if (ca_loc->tail_call_p)
20391 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20392 if (ca_loc->symbol_ref)
20394 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20395 if (tdie)
20396 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20397 else
20398 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
20400 return die;
20403 /* Generate a DIE to represent a declared function (either file-scope or
20404 block-local). */
20406 static void
20407 gen_subprogram_die (tree decl, dw_die_ref context_die)
20409 tree origin = decl_ultimate_origin (decl);
20410 dw_die_ref subr_die;
20411 dw_die_ref old_die = lookup_decl_die (decl);
20413 /* This function gets called multiple times for different stages of
20414 the debug process. For example, for func() in this code:
20416 namespace S
20418 void func() { ... }
20421 ...we get called 4 times. Twice in early debug and twice in
20422 late debug:
20424 Early debug
20425 -----------
20427 1. Once while generating func() within the namespace. This is
20428 the declaration. The declaration bit below is set, as the
20429 context is the namespace.
20431 A new DIE will be generated with DW_AT_declaration set.
20433 2. Once for func() itself. This is the specification. The
20434 declaration bit below is clear as the context is the CU.
20436 We will use the cached DIE from (1) to create a new DIE with
20437 DW_AT_specification pointing to the declaration in (1).
20439 Late debug via rest_of_handle_final()
20440 -------------------------------------
20442 3. Once generating func() within the namespace. This is also the
20443 declaration, as in (1), but this time we will early exit below
20444 as we have a cached DIE and a declaration needs no additional
20445 annotations (no locations), as the source declaration line
20446 info is enough.
20448 4. Once for func() itself. As in (2), this is the specification,
20449 but this time we will re-use the cached DIE, and just annotate
20450 it with the location information that should now be available.
20452 For something without namespaces, but with abstract instances, we
20453 are also called a multiple times:
20455 class Base
20457 public:
20458 Base (); // constructor declaration (1)
20461 Base::Base () { } // constructor specification (2)
20463 Early debug
20464 -----------
20466 1. Once for the Base() constructor by virtue of it being a
20467 member of the Base class. This is done via
20468 rest_of_type_compilation.
20470 This is a declaration, so a new DIE will be created with
20471 DW_AT_declaration.
20473 2. Once for the Base() constructor definition, but this time
20474 while generating the abstract instance of the base
20475 constructor (__base_ctor) which is being generated via early
20476 debug of reachable functions.
20478 Even though we have a cached version of the declaration (1),
20479 we will create a DW_AT_specification of the declaration DIE
20480 in (1).
20482 3. Once for the __base_ctor itself, but this time, we generate
20483 an DW_AT_abstract_origin version of the DW_AT_specification in
20484 (2).
20486 Late debug via rest_of_handle_final
20487 -----------------------------------
20489 4. One final time for the __base_ctor (which will have a cached
20490 DIE with DW_AT_abstract_origin created in (3). This time,
20491 we will just annotate the location information now
20492 available.
20494 int declaration = (current_function_decl != decl
20495 || class_or_namespace_scope_p (context_die));
20497 /* Now that the C++ front end lazily declares artificial member fns, we
20498 might need to retrofit the declaration into its class. */
20499 if (!declaration && !origin && !old_die
20500 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20501 && !class_or_namespace_scope_p (context_die)
20502 && debug_info_level > DINFO_LEVEL_TERSE)
20503 old_die = force_decl_die (decl);
20505 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
20506 if (origin != NULL)
20508 gcc_assert (!declaration || local_scope_p (context_die));
20510 /* Fixup die_parent for the abstract instance of a nested
20511 inline function. */
20512 if (old_die && old_die->die_parent == NULL)
20513 add_child_die (context_die, old_die);
20515 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
20517 /* If we have a DW_AT_abstract_origin we have a working
20518 cached version. */
20519 subr_die = old_die;
20521 else
20523 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20524 add_abstract_origin_attribute (subr_die, origin);
20525 /* This is where the actual code for a cloned function is.
20526 Let's emit linkage name attribute for it. This helps
20527 debuggers to e.g, set breakpoints into
20528 constructors/destructors when the user asks "break
20529 K::K". */
20530 add_linkage_name (subr_die, decl);
20533 /* A cached copy, possibly from early dwarf generation. Reuse as
20534 much as possible. */
20535 else if (old_die)
20537 /* A declaration that has been previously dumped needs no
20538 additional information. */
20539 if (declaration)
20540 return;
20542 if (!get_AT_flag (old_die, DW_AT_declaration)
20543 /* We can have a normal definition following an inline one in the
20544 case of redefinition of GNU C extern inlines.
20545 It seems reasonable to use AT_specification in this case. */
20546 && !get_AT (old_die, DW_AT_inline))
20548 /* Detect and ignore this case, where we are trying to output
20549 something we have already output. */
20550 if (get_AT (old_die, DW_AT_low_pc)
20551 || get_AT (old_die, DW_AT_ranges))
20552 return;
20554 /* If we have no location information, this must be a
20555 partially generated DIE from early dwarf generation.
20556 Fall through and generate it. */
20559 /* If the definition comes from the same place as the declaration,
20560 maybe use the old DIE. We always want the DIE for this function
20561 that has the *_pc attributes to be under comp_unit_die so the
20562 debugger can find it. We also need to do this for abstract
20563 instances of inlines, since the spec requires the out-of-line copy
20564 to have the same parent. For local class methods, this doesn't
20565 apply; we just use the old DIE. */
20566 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20567 struct dwarf_file_data * file_index = lookup_filename (s.file);
20568 if ((is_cu_die (old_die->die_parent)
20569 /* This condition fixes the inconsistency/ICE with the
20570 following Fortran test (or some derivative thereof) while
20571 building libgfortran:
20573 module some_m
20574 contains
20575 logical function funky (FLAG)
20576 funky = .true.
20577 end function
20578 end module
20580 || (old_die->die_parent
20581 && old_die->die_parent->die_tag == DW_TAG_module)
20582 || context_die == NULL)
20583 && (DECL_ARTIFICIAL (decl)
20584 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20585 && (get_AT_unsigned (old_die, DW_AT_decl_line)
20586 == (unsigned) s.line))))
20588 subr_die = old_die;
20590 /* Clear out the declaration attribute, but leave the
20591 parameters so they can be augmented with location
20592 information later. Unless this was a declaration, in
20593 which case, wipe out the nameless parameters and recreate
20594 them further down. */
20595 if (remove_AT (subr_die, DW_AT_declaration))
20598 remove_AT (subr_die, DW_AT_object_pointer);
20599 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20602 /* Make a specification pointing to the previously built
20603 declaration. */
20604 else
20606 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20607 add_AT_specification (subr_die, old_die);
20608 add_pubname (decl, subr_die);
20609 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20610 add_AT_file (subr_die, DW_AT_decl_file, file_index);
20611 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20612 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20614 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
20615 emit the real type on the definition die. */
20616 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
20618 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
20619 if (die == auto_die || die == decltype_auto_die)
20620 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20621 TYPE_UNQUALIFIED, false, context_die);
20624 /* When we process the method declaration, we haven't seen
20625 the out-of-class defaulted definition yet, so we have to
20626 recheck now. */
20627 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20628 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict)
20629 && !get_AT (subr_die, DW_AT_defaulted))
20630 switch (defaulted)
20632 case 2:
20633 add_AT_unsigned (subr_die, DW_AT_defaulted,
20634 DW_DEFAULTED_out_of_class);
20635 break;
20637 case 1: /* This must have been handled before. */
20638 default:
20639 gcc_unreachable ();
20643 /* Create a fresh DIE for anything else. */
20644 else
20646 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20648 if (TREE_PUBLIC (decl))
20649 add_AT_flag (subr_die, DW_AT_external, 1);
20651 add_name_and_src_coords_attributes (subr_die, decl);
20652 add_pubname (decl, subr_die);
20653 if (debug_info_level > DINFO_LEVEL_TERSE)
20655 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20656 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20657 TYPE_UNQUALIFIED, false, context_die);
20660 add_pure_or_virtual_attribute (subr_die, decl);
20661 if (DECL_ARTIFICIAL (decl))
20662 add_AT_flag (subr_die, DW_AT_artificial, 1);
20664 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
20665 add_AT_flag (subr_die, DW_AT_noreturn, 1);
20667 add_accessibility_attribute (subr_die, decl);
20670 /* Unless we have an existing non-declaration DIE, equate the new
20671 DIE. */
20672 if (!old_die || is_declaration_die (old_die))
20673 equate_decl_number_to_die (decl, subr_die);
20675 if (declaration)
20677 if (!old_die || !get_AT (old_die, DW_AT_inline))
20679 add_AT_flag (subr_die, DW_AT_declaration, 1);
20681 /* If this is an explicit function declaration then generate
20682 a DW_AT_explicit attribute. */
20683 if (lang_hooks.decls.function_decl_explicit_p (decl)
20684 && (dwarf_version >= 3 || !dwarf_strict))
20685 add_AT_flag (subr_die, DW_AT_explicit, 1);
20687 /* If this is a C++11 deleted special function member then generate
20688 a DW_AT_deleted attribute. */
20689 if (lang_hooks.decls.function_decl_deleted_p (decl)
20690 && (dwarf_version >= 5 || ! dwarf_strict))
20691 add_AT_flag (subr_die, DW_AT_deleted, 1);
20693 /* If this is a C++11 defaulted special function member then
20694 generate a DW_AT_GNU_defaulted attribute. */
20695 int defaulted = lang_hooks.decls.function_decl_defaulted (decl);
20696 if (defaulted && (dwarf_version >= 5 || ! dwarf_strict))
20697 switch (defaulted)
20699 case 1:
20700 add_AT_unsigned (subr_die, DW_AT_defaulted,
20701 DW_DEFAULTED_in_class);
20702 break;
20704 /* It is likely that this will never hit, since we
20705 don't have the out-of-class definition yet when we
20706 process the class definition and the method
20707 declaration. We recheck elsewhere, but leave it
20708 here just in case. */
20709 case 2:
20710 add_AT_unsigned (subr_die, DW_AT_defaulted,
20711 DW_DEFAULTED_out_of_class);
20712 break;
20714 default:
20715 gcc_unreachable ();
20719 /* Tag abstract instances with DW_AT_inline. */
20720 else if (DECL_ABSTRACT_P (decl))
20722 if (DECL_DECLARED_INLINE_P (decl))
20724 if (cgraph_function_possibly_inlined_p (decl))
20725 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20726 else
20727 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20729 else
20731 if (cgraph_function_possibly_inlined_p (decl))
20732 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20733 else
20734 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20737 if (DECL_DECLARED_INLINE_P (decl)
20738 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20739 add_AT_flag (subr_die, DW_AT_artificial, 1);
20741 /* For non DECL_EXTERNALs, if range information is available, fill
20742 the DIE with it. */
20743 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
20745 HOST_WIDE_INT cfa_fb_offset;
20747 struct function *fun = DECL_STRUCT_FUNCTION (decl);
20749 if (!flag_reorder_blocks_and_partition)
20751 dw_fde_ref fde = fun->fde;
20752 if (fde->dw_fde_begin)
20754 /* We have already generated the labels. */
20755 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20756 fde->dw_fde_end, false);
20758 else
20760 /* Create start/end labels and add the range. */
20761 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
20762 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
20763 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
20764 current_function_funcdef_no);
20765 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
20766 current_function_funcdef_no);
20767 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
20768 false);
20771 #if VMS_DEBUGGING_INFO
20772 /* HP OpenVMS Industry Standard 64: DWARF Extensions
20773 Section 2.3 Prologue and Epilogue Attributes:
20774 When a breakpoint is set on entry to a function, it is generally
20775 desirable for execution to be suspended, not on the very first
20776 instruction of the function, but rather at a point after the
20777 function's frame has been set up, after any language defined local
20778 declaration processing has been completed, and before execution of
20779 the first statement of the function begins. Debuggers generally
20780 cannot properly determine where this point is. Similarly for a
20781 breakpoint set on exit from a function. The prologue and epilogue
20782 attributes allow a compiler to communicate the location(s) to use. */
20785 if (fde->dw_fde_vms_end_prologue)
20786 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20787 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20789 if (fde->dw_fde_vms_begin_epilogue)
20790 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20791 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20793 #endif
20796 else
20798 /* Generate pubnames entries for the split function code ranges. */
20799 dw_fde_ref fde = fun->fde;
20801 if (fde->dw_fde_second_begin)
20803 if (dwarf_version >= 3 || !dwarf_strict)
20805 /* We should use ranges for non-contiguous code section
20806 addresses. Use the actual code range for the initial
20807 section, since the HOT/COLD labels might precede an
20808 alignment offset. */
20809 bool range_list_added = false;
20810 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20811 fde->dw_fde_end, &range_list_added,
20812 false);
20813 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20814 fde->dw_fde_second_end,
20815 &range_list_added, false);
20816 if (range_list_added)
20817 add_ranges (NULL);
20819 else
20821 /* There is no real support in DW2 for this .. so we make
20822 a work-around. First, emit the pub name for the segment
20823 containing the function label. Then make and emit a
20824 simplified subprogram DIE for the second segment with the
20825 name pre-fixed by __hot/cold_sect_of_. We use the same
20826 linkage name for the second die so that gdb will find both
20827 sections when given "b foo". */
20828 const char *name = NULL;
20829 tree decl_name = DECL_NAME (decl);
20830 dw_die_ref seg_die;
20832 /* Do the 'primary' section. */
20833 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
20834 fde->dw_fde_end, false);
20836 /* Build a minimal DIE for the secondary section. */
20837 seg_die = new_die (DW_TAG_subprogram,
20838 subr_die->die_parent, decl);
20840 if (TREE_PUBLIC (decl))
20841 add_AT_flag (seg_die, DW_AT_external, 1);
20843 if (decl_name != NULL
20844 && IDENTIFIER_POINTER (decl_name) != NULL)
20846 name = dwarf2_name (decl, 1);
20847 if (! DECL_ARTIFICIAL (decl))
20848 add_src_coords_attributes (seg_die, decl);
20850 add_linkage_name (seg_die, decl);
20852 gcc_assert (name != NULL);
20853 add_pure_or_virtual_attribute (seg_die, decl);
20854 if (DECL_ARTIFICIAL (decl))
20855 add_AT_flag (seg_die, DW_AT_artificial, 1);
20857 name = concat ("__second_sect_of_", name, NULL);
20858 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
20859 fde->dw_fde_second_end, false);
20860 add_name_attribute (seg_die, name);
20861 if (want_pubnames ())
20862 add_pubname_string (name, seg_die);
20865 else
20866 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
20867 false);
20870 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20872 /* We define the "frame base" as the function's CFA. This is more
20873 convenient for several reasons: (1) It's stable across the prologue
20874 and epilogue, which makes it better than just a frame pointer,
20875 (2) With dwarf3, there exists a one-byte encoding that allows us
20876 to reference the .debug_frame data by proxy, but failing that,
20877 (3) We can at least reuse the code inspection and interpretation
20878 code that determines the CFA position at various points in the
20879 function. */
20880 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
20882 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20883 add_AT_loc (subr_die, DW_AT_frame_base, op);
20885 else
20887 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20888 if (list->dw_loc_next)
20889 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20890 else
20891 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20894 /* Compute a displacement from the "steady-state frame pointer" to
20895 the CFA. The former is what all stack slots and argument slots
20896 will reference in the rtl; the latter is what we've told the
20897 debugger about. We'll need to adjust all frame_base references
20898 by this displacement. */
20899 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20901 if (fun->static_chain_decl)
20903 /* DWARF requires here a location expression that computes the
20904 address of the enclosing subprogram's frame base. The machinery
20905 in tree-nested.c is supposed to store this specific address in the
20906 last field of the FRAME record. */
20907 const tree frame_type
20908 = TREE_TYPE (TREE_TYPE (fun->static_chain_decl));
20909 const tree fb_decl = tree_last (TYPE_FIELDS (frame_type));
20911 tree fb_expr
20912 = build1 (INDIRECT_REF, frame_type, fun->static_chain_decl);
20913 fb_expr = build3 (COMPONENT_REF, TREE_TYPE (fb_decl),
20914 fb_expr, fb_decl, NULL_TREE);
20916 add_AT_location_description (subr_die, DW_AT_static_link,
20917 loc_list_from_tree (fb_expr, 0, NULL));
20921 /* Generate child dies for template paramaters. */
20922 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
20923 gen_generic_params_dies (decl);
20925 /* Now output descriptions of the arguments for this function. This gets
20926 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20927 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20928 `...' at the end of the formal parameter list. In order to find out if
20929 there was a trailing ellipsis or not, we must instead look at the type
20930 associated with the FUNCTION_DECL. This will be a node of type
20931 FUNCTION_TYPE. If the chain of type nodes hanging off of this
20932 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20933 an ellipsis at the end. */
20935 /* In the case where we are describing a mere function declaration, all we
20936 need to do here (and all we *can* do here) is to describe the *types* of
20937 its formal parameters. */
20938 if (debug_info_level <= DINFO_LEVEL_TERSE)
20940 else if (declaration)
20941 gen_formal_types_die (decl, subr_die);
20942 else
20944 /* Generate DIEs to represent all known formal parameters. */
20945 tree parm = DECL_ARGUMENTS (decl);
20946 tree generic_decl = early_dwarf
20947 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
20948 tree generic_decl_parm = generic_decl
20949 ? DECL_ARGUMENTS (generic_decl)
20950 : NULL;
20951 auto_vec<dw_die_ref> string_types_vec;
20952 if (string_types == NULL)
20953 string_types = &string_types_vec;
20955 /* Now we want to walk the list of parameters of the function and
20956 emit their relevant DIEs.
20958 We consider the case of DECL being an instance of a generic function
20959 as well as it being a normal function.
20961 If DECL is an instance of a generic function we walk the
20962 parameters of the generic function declaration _and_ the parameters of
20963 DECL itself. This is useful because we want to emit specific DIEs for
20964 function parameter packs and those are declared as part of the
20965 generic function declaration. In that particular case,
20966 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20967 That DIE has children DIEs representing the set of arguments
20968 of the pack. Note that the set of pack arguments can be empty.
20969 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20970 children DIE.
20972 Otherwise, we just consider the parameters of DECL. */
20973 while (generic_decl_parm || parm)
20975 if (generic_decl_parm
20976 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20977 gen_formal_parameter_pack_die (generic_decl_parm,
20978 parm, subr_die,
20979 &parm);
20980 else if (parm && !POINTER_BOUNDS_P (parm))
20982 dw_die_ref parm_die = gen_decl_die (parm, NULL, NULL, subr_die);
20984 if (parm == DECL_ARGUMENTS (decl)
20985 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20986 && parm_die
20987 && (dwarf_version >= 3 || !dwarf_strict))
20988 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20990 parm = DECL_CHAIN (parm);
20992 else if (parm)
20993 parm = DECL_CHAIN (parm);
20995 if (generic_decl_parm)
20996 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20999 /* Decide whether we need an unspecified_parameters DIE at the end.
21000 There are 2 more cases to do this for: 1) the ansi ... declaration -
21001 this is detectable when the end of the arg list is not a
21002 void_type_node 2) an unprototyped function declaration (not a
21003 definition). This just means that we have no info about the
21004 parameters at all. */
21005 if (early_dwarf)
21007 if (prototype_p (TREE_TYPE (decl)))
21009 /* This is the prototyped case, check for.... */
21010 if (stdarg_p (TREE_TYPE (decl)))
21011 gen_unspecified_parameters_die (decl, subr_die);
21013 else if (DECL_INITIAL (decl) == NULL_TREE)
21014 gen_unspecified_parameters_die (decl, subr_die);
21017 /* Adjust DW_TAG_string_type DIEs if needed, now that all arguments
21018 have DIEs. */
21019 if (string_types == &string_types_vec)
21021 adjust_string_types ();
21022 string_types = NULL;
21026 if (subr_die != old_die)
21027 /* Add the calling convention attribute if requested. */
21028 add_calling_convention_attribute (subr_die, decl);
21030 /* Output Dwarf info for all of the stuff within the body of the function
21031 (if it has one - it may be just a declaration).
21033 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
21034 a function. This BLOCK actually represents the outermost binding contour
21035 for the function, i.e. the contour in which the function's formal
21036 parameters and labels get declared. Curiously, it appears that the front
21037 end doesn't actually put the PARM_DECL nodes for the current function onto
21038 the BLOCK_VARS list for this outer scope, but are strung off of the
21039 DECL_ARGUMENTS list for the function instead.
21041 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
21042 the LABEL_DECL nodes for the function however, and we output DWARF info
21043 for those in decls_for_scope. Just within the `outer_scope' there will be
21044 a BLOCK node representing the function's outermost pair of curly braces,
21045 and any blocks used for the base and member initializers of a C++
21046 constructor function. */
21047 tree outer_scope = DECL_INITIAL (decl);
21048 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
21050 int call_site_note_count = 0;
21051 int tail_call_site_note_count = 0;
21053 /* Emit a DW_TAG_variable DIE for a named return value. */
21054 if (DECL_NAME (DECL_RESULT (decl)))
21055 gen_decl_die (DECL_RESULT (decl), NULL, NULL, subr_die);
21057 /* The first time through decls_for_scope we will generate the
21058 DIEs for the locals. The second time, we fill in the
21059 location info. */
21060 decls_for_scope (outer_scope, subr_die);
21062 if (call_arg_locations && !dwarf_strict)
21064 struct call_arg_loc_node *ca_loc;
21065 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
21067 dw_die_ref die = NULL;
21068 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
21069 rtx arg, next_arg;
21071 for (arg = (ca_loc->call_arg_loc_note != NULL_RTX
21072 ? NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note)
21073 : NULL_RTX);
21074 arg; arg = next_arg)
21076 dw_loc_descr_ref reg, val;
21077 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
21078 dw_die_ref cdie, tdie = NULL;
21080 next_arg = XEXP (arg, 1);
21081 if (REG_P (XEXP (XEXP (arg, 0), 0))
21082 && next_arg
21083 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
21084 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
21085 && REGNO (XEXP (XEXP (arg, 0), 0))
21086 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
21087 next_arg = XEXP (next_arg, 1);
21088 if (mode == VOIDmode)
21090 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
21091 if (mode == VOIDmode)
21092 mode = GET_MODE (XEXP (arg, 0));
21094 if (mode == VOIDmode || mode == BLKmode)
21095 continue;
21096 /* Get dynamic information about call target only if we
21097 have no static information: we cannot generate both
21098 DW_AT_abstract_origin and DW_AT_GNU_call_site_target
21099 attributes. */
21100 if (ca_loc->symbol_ref == NULL_RTX)
21102 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
21104 tloc = XEXP (XEXP (arg, 0), 1);
21105 continue;
21107 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
21108 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
21110 tlocc = XEXP (XEXP (arg, 0), 1);
21111 continue;
21114 reg = NULL;
21115 if (REG_P (XEXP (XEXP (arg, 0), 0)))
21116 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
21117 VAR_INIT_STATUS_INITIALIZED);
21118 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
21120 rtx mem = XEXP (XEXP (arg, 0), 0);
21121 reg = mem_loc_descriptor (XEXP (mem, 0),
21122 get_address_mode (mem),
21123 GET_MODE (mem),
21124 VAR_INIT_STATUS_INITIALIZED);
21126 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
21127 == DEBUG_PARAMETER_REF)
21129 tree tdecl
21130 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
21131 tdie = lookup_decl_die (tdecl);
21132 if (tdie == NULL)
21133 continue;
21135 else
21136 continue;
21137 if (reg == NULL
21138 && GET_CODE (XEXP (XEXP (arg, 0), 0))
21139 != DEBUG_PARAMETER_REF)
21140 continue;
21141 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
21142 VOIDmode,
21143 VAR_INIT_STATUS_INITIALIZED);
21144 if (val == NULL)
21145 continue;
21146 if (die == NULL)
21147 die = gen_call_site_die (decl, subr_die, ca_loc);
21148 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
21149 NULL_TREE);
21150 if (reg != NULL)
21151 add_AT_loc (cdie, DW_AT_location, reg);
21152 else if (tdie != NULL)
21153 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
21154 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
21155 if (next_arg != XEXP (arg, 1))
21157 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
21158 if (mode == VOIDmode)
21159 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
21160 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
21161 0), 1),
21162 mode, VOIDmode,
21163 VAR_INIT_STATUS_INITIALIZED);
21164 if (val != NULL)
21165 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
21168 if (die == NULL
21169 && (ca_loc->symbol_ref || tloc))
21170 die = gen_call_site_die (decl, subr_die, ca_loc);
21171 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
21173 dw_loc_descr_ref tval = NULL;
21175 if (tloc != NULL_RTX)
21176 tval = mem_loc_descriptor (tloc,
21177 GET_MODE (tloc) == VOIDmode
21178 ? Pmode : GET_MODE (tloc),
21179 VOIDmode,
21180 VAR_INIT_STATUS_INITIALIZED);
21181 if (tval)
21182 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
21183 else if (tlocc != NULL_RTX)
21185 tval = mem_loc_descriptor (tlocc,
21186 GET_MODE (tlocc) == VOIDmode
21187 ? Pmode : GET_MODE (tlocc),
21188 VOIDmode,
21189 VAR_INIT_STATUS_INITIALIZED);
21190 if (tval)
21191 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
21192 tval);
21195 if (die != NULL)
21197 call_site_note_count++;
21198 if (ca_loc->tail_call_p)
21199 tail_call_site_note_count++;
21203 call_arg_locations = NULL;
21204 call_arg_loc_last = NULL;
21205 if (tail_call_site_count >= 0
21206 && tail_call_site_count == tail_call_site_note_count
21207 && !dwarf_strict)
21209 if (call_site_count >= 0
21210 && call_site_count == call_site_note_count)
21211 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
21212 else
21213 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
21215 call_site_count = -1;
21216 tail_call_site_count = -1;
21219 /* Mark used types after we have created DIEs for the functions scopes. */
21220 premark_used_types (DECL_STRUCT_FUNCTION (decl));
21223 /* Returns a hash value for X (which really is a die_struct). */
21225 hashval_t
21226 block_die_hasher::hash (die_struct *d)
21228 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
21231 /* Return nonzero if decl_id and die_parent of die_struct X is the same
21232 as decl_id and die_parent of die_struct Y. */
21234 bool
21235 block_die_hasher::equal (die_struct *x, die_struct *y)
21237 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
21240 /* Return TRUE if DECL, which may have been previously generated as
21241 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
21242 true if decl (or its origin) is either an extern declaration or a
21243 class/namespace scoped declaration.
21245 The declare_in_namespace support causes us to get two DIEs for one
21246 variable, both of which are declarations. We want to avoid
21247 considering one to be a specification, so we must test for
21248 DECLARATION and DW_AT_declaration. */
21249 static inline bool
21250 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
21252 return (old_die && TREE_STATIC (decl) && !declaration
21253 && get_AT_flag (old_die, DW_AT_declaration) == 1);
21256 /* Return true if DECL is a local static. */
21258 static inline bool
21259 local_function_static (tree decl)
21261 gcc_assert (VAR_P (decl));
21262 return TREE_STATIC (decl)
21263 && DECL_CONTEXT (decl)
21264 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
21267 /* Generate a DIE to represent a declared data object.
21268 Either DECL or ORIGIN must be non-null. */
21270 static void
21271 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
21273 HOST_WIDE_INT off = 0;
21274 tree com_decl;
21275 tree decl_or_origin = decl ? decl : origin;
21276 tree ultimate_origin;
21277 dw_die_ref var_die;
21278 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
21279 dw_die_ref origin_die = NULL;
21280 bool declaration = (DECL_EXTERNAL (decl_or_origin)
21281 || class_or_namespace_scope_p (context_die));
21282 bool specialization_p = false;
21284 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21285 if (decl || ultimate_origin)
21286 origin = ultimate_origin;
21287 com_decl = fortran_common (decl_or_origin, &off);
21289 /* Symbol in common gets emitted as a child of the common block, in the form
21290 of a data member. */
21291 if (com_decl)
21293 dw_die_ref com_die;
21294 dw_loc_list_ref loc = NULL;
21295 die_node com_die_arg;
21297 var_die = lookup_decl_die (decl_or_origin);
21298 if (var_die)
21300 if (! early_dwarf && get_AT (var_die, DW_AT_location) == NULL)
21302 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
21303 if (loc)
21305 if (off)
21307 /* Optimize the common case. */
21308 if (single_element_loc_list_p (loc)
21309 && loc->expr->dw_loc_opc == DW_OP_addr
21310 && loc->expr->dw_loc_next == NULL
21311 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
21312 == SYMBOL_REF)
21314 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21315 loc->expr->dw_loc_oprnd1.v.val_addr
21316 = plus_constant (GET_MODE (x), x , off);
21318 else
21319 loc_list_plus_const (loc, off);
21321 add_AT_location_description (var_die, DW_AT_location, loc);
21322 remove_AT (var_die, DW_AT_declaration);
21325 return;
21328 if (common_block_die_table == NULL)
21329 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
21331 com_die_arg.decl_id = DECL_UID (com_decl);
21332 com_die_arg.die_parent = context_die;
21333 com_die = common_block_die_table->find (&com_die_arg);
21334 if (! early_dwarf)
21335 loc = loc_list_from_tree (com_decl, 2, NULL);
21336 if (com_die == NULL)
21338 const char *cnam
21339 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
21340 die_node **slot;
21342 com_die = new_die (DW_TAG_common_block, context_die, decl);
21343 add_name_and_src_coords_attributes (com_die, com_decl);
21344 if (loc)
21346 add_AT_location_description (com_die, DW_AT_location, loc);
21347 /* Avoid sharing the same loc descriptor between
21348 DW_TAG_common_block and DW_TAG_variable. */
21349 loc = loc_list_from_tree (com_decl, 2, NULL);
21351 else if (DECL_EXTERNAL (decl_or_origin))
21352 add_AT_flag (com_die, DW_AT_declaration, 1);
21353 if (want_pubnames ())
21354 add_pubname_string (cnam, com_die); /* ??? needed? */
21355 com_die->decl_id = DECL_UID (com_decl);
21356 slot = common_block_die_table->find_slot (com_die, INSERT);
21357 *slot = com_die;
21359 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
21361 add_AT_location_description (com_die, DW_AT_location, loc);
21362 loc = loc_list_from_tree (com_decl, 2, NULL);
21363 remove_AT (com_die, DW_AT_declaration);
21365 var_die = new_die (DW_TAG_variable, com_die, decl);
21366 add_name_and_src_coords_attributes (var_die, decl_or_origin);
21367 add_type_attribute (var_die, TREE_TYPE (decl_or_origin),
21368 decl_quals (decl_or_origin), false,
21369 context_die);
21370 add_AT_flag (var_die, DW_AT_external, 1);
21371 if (loc)
21373 if (off)
21375 /* Optimize the common case. */
21376 if (single_element_loc_list_p (loc)
21377 && loc->expr->dw_loc_opc == DW_OP_addr
21378 && loc->expr->dw_loc_next == NULL
21379 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
21381 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
21382 loc->expr->dw_loc_oprnd1.v.val_addr
21383 = plus_constant (GET_MODE (x), x, off);
21385 else
21386 loc_list_plus_const (loc, off);
21388 add_AT_location_description (var_die, DW_AT_location, loc);
21390 else if (DECL_EXTERNAL (decl_or_origin))
21391 add_AT_flag (var_die, DW_AT_declaration, 1);
21392 if (decl)
21393 equate_decl_number_to_die (decl, var_die);
21394 return;
21397 if (old_die)
21399 if (declaration)
21401 /* A declaration that has been previously dumped, needs no
21402 further annotations, since it doesn't need location on
21403 the second pass. */
21404 return;
21406 else if (decl_will_get_specification_p (old_die, decl, declaration)
21407 && !get_AT (old_die, DW_AT_specification))
21409 /* Fall-thru so we can make a new variable die along with a
21410 DW_AT_specification. */
21412 else if (origin && old_die->die_parent != context_die)
21414 /* If we will be creating an inlined instance, we need a
21415 new DIE that will get annotated with
21416 DW_AT_abstract_origin. Clear things so we can get a
21417 new DIE. */
21418 gcc_assert (!DECL_ABSTRACT_P (decl));
21419 old_die = NULL;
21421 else
21423 /* If a DIE was dumped early, it still needs location info.
21424 Skip to where we fill the location bits. */
21425 var_die = old_die;
21426 goto gen_variable_die_location;
21430 /* For static data members, the declaration in the class is supposed
21431 to have DW_TAG_member tag; the specification should still be
21432 DW_TAG_variable referencing the DW_TAG_member DIE. */
21433 if (declaration && class_scope_p (context_die))
21434 var_die = new_die (DW_TAG_member, context_die, decl);
21435 else
21436 var_die = new_die (DW_TAG_variable, context_die, decl);
21438 if (origin != NULL)
21439 origin_die = add_abstract_origin_attribute (var_die, origin);
21441 /* Loop unrolling can create multiple blocks that refer to the same
21442 static variable, so we must test for the DW_AT_declaration flag.
21444 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21445 copy decls and set the DECL_ABSTRACT_P flag on them instead of
21446 sharing them.
21448 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
21449 else if (decl_will_get_specification_p (old_die, decl, declaration))
21451 /* This is a definition of a C++ class level static. */
21452 add_AT_specification (var_die, old_die);
21453 specialization_p = true;
21454 if (DECL_NAME (decl))
21456 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21457 struct dwarf_file_data * file_index = lookup_filename (s.file);
21459 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21460 add_AT_file (var_die, DW_AT_decl_file, file_index);
21462 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21463 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21465 if (old_die->die_tag == DW_TAG_member)
21466 add_linkage_name (var_die, decl);
21469 else
21470 add_name_and_src_coords_attributes (var_die, decl);
21472 if ((origin == NULL && !specialization_p)
21473 || (origin != NULL
21474 && !DECL_ABSTRACT_P (decl_or_origin)
21475 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21476 decl_function_context
21477 (decl_or_origin))))
21479 tree type = TREE_TYPE (decl_or_origin);
21481 if (decl_by_reference_p (decl_or_origin))
21482 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21483 context_die);
21484 else
21485 add_type_attribute (var_die, type, decl_quals (decl_or_origin), false,
21486 context_die);
21489 if (origin == NULL && !specialization_p)
21491 if (TREE_PUBLIC (decl))
21492 add_AT_flag (var_die, DW_AT_external, 1);
21494 if (DECL_ARTIFICIAL (decl))
21495 add_AT_flag (var_die, DW_AT_artificial, 1);
21497 add_accessibility_attribute (var_die, decl);
21500 if (declaration)
21501 add_AT_flag (var_die, DW_AT_declaration, 1);
21503 if (decl && (DECL_ABSTRACT_P (decl)
21504 || !old_die || is_declaration_die (old_die)))
21505 equate_decl_number_to_die (decl, var_die);
21507 gen_variable_die_location:
21508 if (! declaration
21509 && (! DECL_ABSTRACT_P (decl_or_origin)
21510 /* Local static vars are shared between all clones/inlines,
21511 so emit DW_AT_location on the abstract DIE if DECL_RTL is
21512 already set. */
21513 || (VAR_P (decl_or_origin)
21514 && TREE_STATIC (decl_or_origin)
21515 && DECL_RTL_SET_P (decl_or_origin)))
21516 /* When abstract origin already has DW_AT_location attribute, no need
21517 to add it again. */
21518 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21520 if (early_dwarf)
21521 add_pubname (decl_or_origin, var_die);
21522 else
21523 add_location_or_const_value_attribute (var_die, decl_or_origin,
21524 decl == NULL);
21526 else
21527 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21530 /* Generate a DIE to represent a named constant. */
21532 static void
21533 gen_const_die (tree decl, dw_die_ref context_die)
21535 dw_die_ref const_die;
21536 tree type = TREE_TYPE (decl);
21538 const_die = lookup_decl_die (decl);
21539 if (const_die)
21540 return;
21542 const_die = new_die (DW_TAG_constant, context_die, decl);
21543 equate_decl_number_to_die (decl, const_die);
21544 add_name_and_src_coords_attributes (const_die, decl);
21545 add_type_attribute (const_die, type, TYPE_QUAL_CONST, false, context_die);
21546 if (TREE_PUBLIC (decl))
21547 add_AT_flag (const_die, DW_AT_external, 1);
21548 if (DECL_ARTIFICIAL (decl))
21549 add_AT_flag (const_die, DW_AT_artificial, 1);
21550 tree_add_const_value_attribute_for_decl (const_die, decl);
21553 /* Generate a DIE to represent a label identifier. */
21555 static void
21556 gen_label_die (tree decl, dw_die_ref context_die)
21558 tree origin = decl_ultimate_origin (decl);
21559 dw_die_ref lbl_die = lookup_decl_die (decl);
21560 rtx insn;
21561 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21563 if (!lbl_die)
21565 lbl_die = new_die (DW_TAG_label, context_die, decl);
21566 equate_decl_number_to_die (decl, lbl_die);
21568 if (origin != NULL)
21569 add_abstract_origin_attribute (lbl_die, origin);
21570 else
21571 add_name_and_src_coords_attributes (lbl_die, decl);
21574 if (DECL_ABSTRACT_P (decl))
21575 equate_decl_number_to_die (decl, lbl_die);
21576 else if (! early_dwarf)
21578 insn = DECL_RTL_IF_SET (decl);
21580 /* Deleted labels are programmer specified labels which have been
21581 eliminated because of various optimizations. We still emit them
21582 here so that it is possible to put breakpoints on them. */
21583 if (insn
21584 && (LABEL_P (insn)
21585 || ((NOTE_P (insn)
21586 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21588 /* When optimization is enabled (via -O) some parts of the compiler
21589 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21590 represent source-level labels which were explicitly declared by
21591 the user. This really shouldn't be happening though, so catch
21592 it if it ever does happen. */
21593 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
21595 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21596 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21598 else if (insn
21599 && NOTE_P (insn)
21600 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
21601 && CODE_LABEL_NUMBER (insn) != -1)
21603 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
21604 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21609 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
21610 attributes to the DIE for a block STMT, to describe where the inlined
21611 function was called from. This is similar to add_src_coords_attributes. */
21613 static inline void
21614 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21616 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21618 if (dwarf_version >= 3 || !dwarf_strict)
21620 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21621 add_AT_unsigned (die, DW_AT_call_line, s.line);
21626 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21627 Add low_pc and high_pc attributes to the DIE for a block STMT. */
21629 static inline void
21630 add_high_low_attributes (tree stmt, dw_die_ref die)
21632 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21634 if (BLOCK_FRAGMENT_CHAIN (stmt)
21635 && (dwarf_version >= 3 || !dwarf_strict))
21637 tree chain, superblock = NULL_TREE;
21638 dw_die_ref pdie;
21639 dw_attr_node *attr = NULL;
21641 if (inlined_function_outer_scope_p (stmt))
21643 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21644 BLOCK_NUMBER (stmt));
21645 add_AT_lbl_id (die, DW_AT_entry_pc, label);
21648 /* Optimize duplicate .debug_ranges lists or even tails of
21649 lists. If this BLOCK has same ranges as its supercontext,
21650 lookup DW_AT_ranges attribute in the supercontext (and
21651 recursively so), verify that the ranges_table contains the
21652 right values and use it instead of adding a new .debug_range. */
21653 for (chain = stmt, pdie = die;
21654 BLOCK_SAME_RANGE (chain);
21655 chain = BLOCK_SUPERCONTEXT (chain))
21657 dw_attr_node *new_attr;
21659 pdie = pdie->die_parent;
21660 if (pdie == NULL)
21661 break;
21662 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
21663 break;
21664 new_attr = get_AT (pdie, DW_AT_ranges);
21665 if (new_attr == NULL
21666 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
21667 break;
21668 attr = new_attr;
21669 superblock = BLOCK_SUPERCONTEXT (chain);
21671 if (attr != NULL
21672 && (ranges_table[attr->dw_attr_val.v.val_offset
21673 / 2 / DWARF2_ADDR_SIZE].num
21674 == BLOCK_NUMBER (superblock))
21675 && BLOCK_FRAGMENT_CHAIN (superblock))
21677 unsigned long off = attr->dw_attr_val.v.val_offset
21678 / 2 / DWARF2_ADDR_SIZE;
21679 unsigned long supercnt = 0, thiscnt = 0;
21680 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
21681 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21683 ++supercnt;
21684 gcc_checking_assert (ranges_table[off + supercnt].num
21685 == BLOCK_NUMBER (chain));
21687 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
21688 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
21689 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
21690 ++thiscnt;
21691 gcc_assert (supercnt >= thiscnt);
21692 add_AT_range_list (die, DW_AT_ranges,
21693 ((off + supercnt - thiscnt)
21694 * 2 * DWARF2_ADDR_SIZE),
21695 false);
21696 return;
21699 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
21701 chain = BLOCK_FRAGMENT_CHAIN (stmt);
21704 add_ranges (chain);
21705 chain = BLOCK_FRAGMENT_CHAIN (chain);
21707 while (chain);
21708 add_ranges (NULL);
21710 else
21712 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
21713 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21714 BLOCK_NUMBER (stmt));
21715 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
21716 BLOCK_NUMBER (stmt));
21717 add_AT_low_high_pc (die, label, label_high, false);
21721 /* Generate a DIE for a lexical block. */
21723 static void
21724 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
21726 dw_die_ref old_die = BLOCK_DIE (stmt);
21727 dw_die_ref stmt_die = NULL;
21728 if (!old_die)
21730 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21731 BLOCK_DIE (stmt) = stmt_die;
21734 if (BLOCK_ABSTRACT (stmt))
21736 if (old_die)
21738 /* This must have been generated early and it won't even
21739 need location information since it's a DW_AT_inline
21740 function. */
21741 if (flag_checking)
21742 for (dw_die_ref c = context_die; c; c = c->die_parent)
21743 if (c->die_tag == DW_TAG_inlined_subroutine
21744 || c->die_tag == DW_TAG_subprogram)
21746 gcc_assert (get_AT (c, DW_AT_inline));
21747 break;
21749 return;
21752 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
21754 /* If this is an inlined instance, create a new lexical die for
21755 anything below to attach DW_AT_abstract_origin to. */
21756 if (old_die)
21758 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21759 BLOCK_DIE (stmt) = stmt_die;
21760 old_die = NULL;
21763 tree origin = block_ultimate_origin (stmt);
21764 if (origin != NULL_TREE && origin != stmt)
21765 add_abstract_origin_attribute (stmt_die, origin);
21768 if (old_die)
21769 stmt_die = old_die;
21771 /* A non abstract block whose blocks have already been reordered
21772 should have the instruction range for this block. If so, set the
21773 high/low attributes. */
21774 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21776 gcc_assert (stmt_die);
21777 add_high_low_attributes (stmt, stmt_die);
21780 decls_for_scope (stmt, stmt_die);
21783 /* Generate a DIE for an inlined subprogram. */
21785 static void
21786 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
21788 tree decl;
21790 /* The instance of function that is effectively being inlined shall not
21791 be abstract. */
21792 gcc_assert (! BLOCK_ABSTRACT (stmt));
21794 decl = block_ultimate_origin (stmt);
21796 /* Make sure any inlined functions are known to be inlineable. */
21797 gcc_checking_assert (DECL_ABSTRACT_P (decl)
21798 || cgraph_function_possibly_inlined_p (decl));
21800 /* Emit info for the abstract instance first, if we haven't yet. We
21801 must emit this even if the block is abstract, otherwise when we
21802 emit the block below (or elsewhere), we may end up trying to emit
21803 a die whose origin die hasn't been emitted, and crashing. */
21804 dwarf2out_abstract_function (decl);
21806 if (! BLOCK_ABSTRACT (stmt))
21808 dw_die_ref subr_die
21809 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21811 if (call_arg_locations)
21812 BLOCK_DIE (stmt) = subr_die;
21813 add_abstract_origin_attribute (subr_die, decl);
21814 if (TREE_ASM_WRITTEN (stmt))
21815 add_high_low_attributes (stmt, subr_die);
21816 add_call_src_coords_attributes (stmt, subr_die);
21818 decls_for_scope (stmt, subr_die);
21822 /* Generate a DIE for a field in a record, or structure. CTX is required: see
21823 the comment for VLR_CONTEXT. */
21825 static void
21826 gen_field_die (tree decl, struct vlr_context *ctx, dw_die_ref context_die)
21828 dw_die_ref decl_die;
21830 if (TREE_TYPE (decl) == error_mark_node)
21831 return;
21833 decl_die = new_die (DW_TAG_member, context_die, decl);
21834 add_name_and_src_coords_attributes (decl_die, decl);
21835 add_type_attribute (decl_die, member_declared_type (decl), decl_quals (decl),
21836 TYPE_REVERSE_STORAGE_ORDER (DECL_FIELD_CONTEXT (decl)),
21837 context_die);
21839 if (DECL_BIT_FIELD_TYPE (decl))
21841 add_byte_size_attribute (decl_die, decl);
21842 add_bit_size_attribute (decl_die, decl);
21843 add_bit_offset_attribute (decl_die, decl, ctx);
21846 /* If we have a variant part offset, then we are supposed to process a member
21847 of a QUAL_UNION_TYPE, which is how we represent variant parts in
21848 trees. */
21849 gcc_assert (ctx->variant_part_offset == NULL_TREE
21850 || TREE_CODE (DECL_FIELD_CONTEXT (decl)) != QUAL_UNION_TYPE);
21851 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21852 add_data_member_location_attribute (decl_die, decl, ctx);
21854 if (DECL_ARTIFICIAL (decl))
21855 add_AT_flag (decl_die, DW_AT_artificial, 1);
21857 add_accessibility_attribute (decl_die, decl);
21859 /* Equate decl number to die, so that we can look up this decl later on. */
21860 equate_decl_number_to_die (decl, decl_die);
21863 #if 0
21864 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21865 Use modified_type_die instead.
21866 We keep this code here just in case these types of DIEs may be needed to
21867 represent certain things in other languages (e.g. Pascal) someday. */
21869 static void
21870 gen_pointer_type_die (tree type, dw_die_ref context_die)
21872 dw_die_ref ptr_die
21873 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21875 equate_type_number_to_die (type, ptr_die);
21876 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21877 context_die);
21878 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21881 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21882 Use modified_type_die instead.
21883 We keep this code here just in case these types of DIEs may be needed to
21884 represent certain things in other languages (e.g. Pascal) someday. */
21886 static void
21887 gen_reference_type_die (tree type, dw_die_ref context_die)
21889 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21891 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21892 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21893 else
21894 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21896 equate_type_number_to_die (type, ref_die);
21897 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21898 context_die);
21899 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21901 #endif
21903 /* Generate a DIE for a pointer to a member type. */
21905 static void
21906 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21908 dw_die_ref ptr_die
21909 = new_die (DW_TAG_ptr_to_member_type,
21910 scope_die_for (type, context_die), type);
21912 equate_type_number_to_die (type, ptr_die);
21913 add_AT_die_ref (ptr_die, DW_AT_containing_type,
21914 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21915 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED, false,
21916 context_die);
21919 static char *producer_string;
21921 /* Return a heap allocated producer string including command line options
21922 if -grecord-gcc-switches. */
21924 static char *
21925 gen_producer_string (void)
21927 size_t j;
21928 auto_vec<const char *> switches;
21929 const char *language_string = lang_hooks.name;
21930 char *producer, *tail;
21931 const char *p;
21932 size_t len = dwarf_record_gcc_switches ? 0 : 3;
21933 size_t plen = strlen (language_string) + 1 + strlen (version_string);
21935 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
21936 switch (save_decoded_options[j].opt_index)
21938 case OPT_o:
21939 case OPT_d:
21940 case OPT_dumpbase:
21941 case OPT_dumpdir:
21942 case OPT_auxbase:
21943 case OPT_auxbase_strip:
21944 case OPT_quiet:
21945 case OPT_version:
21946 case OPT_v:
21947 case OPT_w:
21948 case OPT_L:
21949 case OPT_D:
21950 case OPT_I:
21951 case OPT_U:
21952 case OPT_SPECIAL_unknown:
21953 case OPT_SPECIAL_ignore:
21954 case OPT_SPECIAL_program_name:
21955 case OPT_SPECIAL_input_file:
21956 case OPT_grecord_gcc_switches:
21957 case OPT_gno_record_gcc_switches:
21958 case OPT__output_pch_:
21959 case OPT_fdiagnostics_show_location_:
21960 case OPT_fdiagnostics_show_option:
21961 case OPT_fdiagnostics_show_caret:
21962 case OPT_fdiagnostics_color_:
21963 case OPT_fverbose_asm:
21964 case OPT____:
21965 case OPT__sysroot_:
21966 case OPT_nostdinc:
21967 case OPT_nostdinc__:
21968 case OPT_fpreprocessed:
21969 case OPT_fltrans_output_list_:
21970 case OPT_fresolution_:
21971 case OPT_fdebug_prefix_map_:
21972 /* Ignore these. */
21973 continue;
21974 default:
21975 if (cl_options[save_decoded_options[j].opt_index].flags
21976 & CL_NO_DWARF_RECORD)
21977 continue;
21978 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
21979 == '-');
21980 switch (save_decoded_options[j].canonical_option[0][1])
21982 case 'M':
21983 case 'i':
21984 case 'W':
21985 continue;
21986 case 'f':
21987 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
21988 "dump", 4) == 0)
21989 continue;
21990 break;
21991 default:
21992 break;
21994 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
21995 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
21996 break;
21999 producer = XNEWVEC (char, plen + 1 + len + 1);
22000 tail = producer;
22001 sprintf (tail, "%s %s", language_string, version_string);
22002 tail += plen;
22004 FOR_EACH_VEC_ELT (switches, j, p)
22006 len = strlen (p);
22007 *tail = ' ';
22008 memcpy (tail + 1, p, len);
22009 tail += len + 1;
22012 *tail = '\0';
22013 return producer;
22016 /* Given a C and/or C++ language/version string return the "highest".
22017 C++ is assumed to be "higher" than C in this case. Used for merging
22018 LTO translation unit languages. */
22019 static const char *
22020 highest_c_language (const char *lang1, const char *lang2)
22022 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
22023 return "GNU C++14";
22024 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
22025 return "GNU C++11";
22026 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
22027 return "GNU C++98";
22029 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
22030 return "GNU C11";
22031 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
22032 return "GNU C99";
22033 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
22034 return "GNU C89";
22036 gcc_unreachable ();
22040 /* Generate the DIE for the compilation unit. */
22042 static dw_die_ref
22043 gen_compile_unit_die (const char *filename)
22045 dw_die_ref die;
22046 const char *language_string = lang_hooks.name;
22047 int language;
22049 die = new_die (DW_TAG_compile_unit, NULL, NULL);
22051 if (filename)
22053 add_name_attribute (die, filename);
22054 /* Don't add cwd for <built-in>. */
22055 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
22056 add_comp_dir_attribute (die);
22059 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
22061 /* If our producer is LTO try to figure out a common language to use
22062 from the global list of translation units. */
22063 if (strcmp (language_string, "GNU GIMPLE") == 0)
22065 unsigned i;
22066 tree t;
22067 const char *common_lang = NULL;
22069 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
22071 if (!TRANSLATION_UNIT_LANGUAGE (t))
22072 continue;
22073 if (!common_lang)
22074 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
22075 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
22077 else if (strncmp (common_lang, "GNU C", 5) == 0
22078 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
22079 /* Mixing C and C++ is ok, use C++ in that case. */
22080 common_lang = highest_c_language (common_lang,
22081 TRANSLATION_UNIT_LANGUAGE (t));
22082 else
22084 /* Fall back to C. */
22085 common_lang = NULL;
22086 break;
22090 if (common_lang)
22091 language_string = common_lang;
22094 language = DW_LANG_C;
22095 if (strncmp (language_string, "GNU C", 5) == 0
22096 && ISDIGIT (language_string[5]))
22098 language = DW_LANG_C89;
22099 if (dwarf_version >= 3 || !dwarf_strict)
22101 if (strcmp (language_string, "GNU C89") != 0)
22102 language = DW_LANG_C99;
22104 if (dwarf_version >= 5 /* || !dwarf_strict */)
22105 if (strcmp (language_string, "GNU C11") == 0)
22106 language = DW_LANG_C11;
22109 else if (strncmp (language_string, "GNU C++", 7) == 0)
22111 language = DW_LANG_C_plus_plus;
22112 if (dwarf_version >= 5 /* || !dwarf_strict */)
22114 if (strcmp (language_string, "GNU C++11") == 0)
22115 language = DW_LANG_C_plus_plus_11;
22116 else if (strcmp (language_string, "GNU C++14") == 0)
22117 language = DW_LANG_C_plus_plus_14;
22120 else if (strcmp (language_string, "GNU F77") == 0)
22121 language = DW_LANG_Fortran77;
22122 else if (strcmp (language_string, "GNU Pascal") == 0)
22123 language = DW_LANG_Pascal83;
22124 else if (dwarf_version >= 3 || !dwarf_strict)
22126 if (strcmp (language_string, "GNU Ada") == 0)
22127 language = DW_LANG_Ada95;
22128 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22130 language = DW_LANG_Fortran95;
22131 if (dwarf_version >= 5 /* || !dwarf_strict */)
22133 if (strcmp (language_string, "GNU Fortran2003") == 0)
22134 language = DW_LANG_Fortran03;
22135 else if (strcmp (language_string, "GNU Fortran2008") == 0)
22136 language = DW_LANG_Fortran08;
22139 else if (strcmp (language_string, "GNU Java") == 0)
22140 language = DW_LANG_Java;
22141 else if (strcmp (language_string, "GNU Objective-C") == 0)
22142 language = DW_LANG_ObjC;
22143 else if (strcmp (language_string, "GNU Objective-C++") == 0)
22144 language = DW_LANG_ObjC_plus_plus;
22145 else if (dwarf_version >= 5 || !dwarf_strict)
22147 if (strcmp (language_string, "GNU Go") == 0)
22148 language = DW_LANG_Go;
22151 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
22152 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
22153 language = DW_LANG_Fortran90;
22155 add_AT_unsigned (die, DW_AT_language, language);
22157 switch (language)
22159 case DW_LANG_Fortran77:
22160 case DW_LANG_Fortran90:
22161 case DW_LANG_Fortran95:
22162 case DW_LANG_Fortran03:
22163 case DW_LANG_Fortran08:
22164 /* Fortran has case insensitive identifiers and the front-end
22165 lowercases everything. */
22166 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
22167 break;
22168 default:
22169 /* The default DW_ID_case_sensitive doesn't need to be specified. */
22170 break;
22172 return die;
22175 /* Generate the DIE for a base class. */
22177 static void
22178 gen_inheritance_die (tree binfo, tree access, tree type,
22179 dw_die_ref context_die)
22181 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
22182 struct vlr_context ctx = { type, NULL };
22184 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, false,
22185 context_die);
22186 add_data_member_location_attribute (die, binfo, &ctx);
22188 if (BINFO_VIRTUAL_P (binfo))
22189 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
22191 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
22192 children, otherwise the default is DW_ACCESS_public. In DWARF2
22193 the default has always been DW_ACCESS_private. */
22194 if (access == access_public_node)
22196 if (dwarf_version == 2
22197 || context_die->die_tag == DW_TAG_class_type)
22198 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
22200 else if (access == access_protected_node)
22201 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
22202 else if (dwarf_version > 2
22203 && context_die->die_tag != DW_TAG_class_type)
22204 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
22207 /* Return whether DECL is a FIELD_DECL that represents the variant part of a
22208 structure. */
22209 static bool
22210 is_variant_part (tree decl)
22212 return (TREE_CODE (decl) == FIELD_DECL
22213 && TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE);
22216 /* Check that OPERAND is a reference to a field in STRUCT_TYPE. If it is,
22217 return the FIELD_DECL. Return NULL_TREE otherwise. */
22219 static tree
22220 analyze_discr_in_predicate (tree operand, tree struct_type)
22222 bool continue_stripping = true;
22223 while (continue_stripping)
22224 switch (TREE_CODE (operand))
22226 CASE_CONVERT:
22227 operand = TREE_OPERAND (operand, 0);
22228 break;
22229 default:
22230 continue_stripping = false;
22231 break;
22234 /* Match field access to members of struct_type only. */
22235 if (TREE_CODE (operand) == COMPONENT_REF
22236 && TREE_CODE (TREE_OPERAND (operand, 0)) == PLACEHOLDER_EXPR
22237 && TREE_TYPE (TREE_OPERAND (operand, 0)) == struct_type
22238 && TREE_CODE (TREE_OPERAND (operand, 1)) == FIELD_DECL)
22239 return TREE_OPERAND (operand, 1);
22240 else
22241 return NULL_TREE;
22244 /* Check that SRC is a constant integer that can be represented as a native
22245 integer constant (either signed or unsigned). If so, store it into DEST and
22246 return true. Return false otherwise. */
22248 static bool
22249 get_discr_value (tree src, dw_discr_value *dest)
22251 bool is_unsigned = TYPE_UNSIGNED (TREE_TYPE (src));
22253 if (TREE_CODE (src) != INTEGER_CST
22254 || !(is_unsigned ? tree_fits_uhwi_p (src) : tree_fits_shwi_p (src)))
22255 return false;
22257 dest->pos = is_unsigned;
22258 if (is_unsigned)
22259 dest->v.uval = tree_to_uhwi (src);
22260 else
22261 dest->v.sval = tree_to_shwi (src);
22263 return true;
22266 /* Try to extract synthetic properties out of VARIANT_PART_DECL, which is a
22267 FIELD_DECL in STRUCT_TYPE that represents a variant part. If unsuccessful,
22268 store NULL_TREE in DISCR_DECL. Otherwise:
22270 - store the discriminant field in STRUCT_TYPE that controls the variant
22271 part to *DISCR_DECL
22273 - put in *DISCR_LISTS_P an array where for each variant, the item
22274 represents the corresponding matching list of discriminant values.
22276 - put in *DISCR_LISTS_LENGTH the number of variants, which is the size of
22277 the above array.
22279 Note that when the array is allocated (i.e. when the analysis is
22280 successful), it is up to the caller to free the array. */
22282 static void
22283 analyze_variants_discr (tree variant_part_decl,
22284 tree struct_type,
22285 tree *discr_decl,
22286 dw_discr_list_ref **discr_lists_p,
22287 unsigned *discr_lists_length)
22289 tree variant_part_type = TREE_TYPE (variant_part_decl);
22290 tree variant;
22291 dw_discr_list_ref *discr_lists;
22292 unsigned i;
22294 /* Compute how many variants there are in this variant part. */
22295 *discr_lists_length = 0;
22296 for (variant = TYPE_FIELDS (variant_part_type);
22297 variant != NULL_TREE;
22298 variant = DECL_CHAIN (variant))
22299 ++*discr_lists_length;
22301 *discr_decl = NULL_TREE;
22302 *discr_lists_p
22303 = (dw_discr_list_ref *) xcalloc (*discr_lists_length,
22304 sizeof (**discr_lists_p));
22305 discr_lists = *discr_lists_p;
22307 /* And then analyze all variants to extract discriminant information for all
22308 of them. This analysis is conservative: as soon as we detect something we
22309 do not support, abort everything and pretend we found nothing. */
22310 for (variant = TYPE_FIELDS (variant_part_type), i = 0;
22311 variant != NULL_TREE;
22312 variant = DECL_CHAIN (variant), ++i)
22314 tree match_expr = DECL_QUALIFIER (variant);
22316 /* Now, try to analyze the predicate and deduce a discriminant for
22317 it. */
22318 if (match_expr == boolean_true_node)
22319 /* Typically happens for the default variant: it matches all cases that
22320 previous variants rejected. Don't output any matching value for
22321 this one. */
22322 continue;
22324 /* The following loop tries to iterate over each discriminant
22325 possibility: single values or ranges. */
22326 while (match_expr != NULL_TREE)
22328 tree next_round_match_expr;
22329 tree candidate_discr = NULL_TREE;
22330 dw_discr_list_ref new_node = NULL;
22332 /* Possibilities are matched one after the other by nested
22333 TRUTH_ORIF_EXPR expressions. Process the current possibility and
22334 continue with the rest at next iteration. */
22335 if (TREE_CODE (match_expr) == TRUTH_ORIF_EXPR)
22337 next_round_match_expr = TREE_OPERAND (match_expr, 0);
22338 match_expr = TREE_OPERAND (match_expr, 1);
22340 else
22341 next_round_match_expr = NULL_TREE;
22343 if (match_expr == boolean_false_node)
22344 /* This sub-expression matches nothing: just wait for the next
22345 one. */
22348 else if (TREE_CODE (match_expr) == EQ_EXPR)
22350 /* We are matching: <discr_field> == <integer_cst>
22351 This sub-expression matches a single value. */
22352 tree integer_cst = TREE_OPERAND (match_expr, 1);
22354 candidate_discr
22355 = analyze_discr_in_predicate (TREE_OPERAND (match_expr, 0),
22356 struct_type);
22358 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22359 if (!get_discr_value (integer_cst,
22360 &new_node->dw_discr_lower_bound))
22361 goto abort;
22362 new_node->dw_discr_range = false;
22365 else if (TREE_CODE (match_expr) == TRUTH_ANDIF_EXPR)
22367 /* We are matching:
22368 <discr_field> > <integer_cst>
22369 && <discr_field> < <integer_cst>.
22370 This sub-expression matches the range of values between the
22371 two matched integer constants. Note that comparisons can be
22372 inclusive or exclusive. */
22373 tree candidate_discr_1, candidate_discr_2;
22374 tree lower_cst, upper_cst;
22375 bool lower_cst_included, upper_cst_included;
22376 tree lower_op = TREE_OPERAND (match_expr, 0);
22377 tree upper_op = TREE_OPERAND (match_expr, 1);
22379 /* When the comparison is exclusive, the integer constant is not
22380 the discriminant range bound we are looking for: we will have
22381 to increment or decrement it. */
22382 if (TREE_CODE (lower_op) == GE_EXPR)
22383 lower_cst_included = true;
22384 else if (TREE_CODE (lower_op) == GT_EXPR)
22385 lower_cst_included = false;
22386 else
22387 goto abort;
22389 if (TREE_CODE (upper_op) == LE_EXPR)
22390 upper_cst_included = true;
22391 else if (TREE_CODE (upper_op) == LT_EXPR)
22392 upper_cst_included = false;
22393 else
22394 goto abort;
22396 /* Extract the discriminant from the first operand and check it
22397 is consistant with the same analysis in the second
22398 operand. */
22399 candidate_discr_1
22400 = analyze_discr_in_predicate (TREE_OPERAND (lower_op, 0),
22401 struct_type);
22402 candidate_discr_2
22403 = analyze_discr_in_predicate (TREE_OPERAND (upper_op, 0),
22404 struct_type);
22405 if (candidate_discr_1 == candidate_discr_2)
22406 candidate_discr = candidate_discr_1;
22407 else
22408 goto abort;
22410 /* Extract bounds from both. */
22411 new_node = ggc_cleared_alloc<dw_discr_list_node> ();
22412 lower_cst = TREE_OPERAND (lower_op, 1);
22413 upper_cst = TREE_OPERAND (upper_op, 1);
22415 if (!lower_cst_included)
22416 lower_cst
22417 = fold (build2 (PLUS_EXPR, TREE_TYPE (lower_cst),
22418 lower_cst,
22419 build_int_cst (TREE_TYPE (lower_cst), 1)));
22420 if (!upper_cst_included)
22421 upper_cst
22422 = fold (build2 (MINUS_EXPR, TREE_TYPE (upper_cst),
22423 upper_cst,
22424 build_int_cst (TREE_TYPE (upper_cst), 1)));
22426 if (!get_discr_value (lower_cst,
22427 &new_node->dw_discr_lower_bound)
22428 || !get_discr_value (upper_cst,
22429 &new_node->dw_discr_upper_bound))
22430 goto abort;
22432 new_node->dw_discr_range = true;
22435 else
22436 /* Unsupported sub-expression: we cannot determine the set of
22437 matching discriminant values. Abort everything. */
22438 goto abort;
22440 /* If the discriminant info is not consistant with what we saw so
22441 far, consider the analysis failed and abort everything. */
22442 if (candidate_discr == NULL_TREE
22443 || (*discr_decl != NULL_TREE && candidate_discr != *discr_decl))
22444 goto abort;
22445 else
22446 *discr_decl = candidate_discr;
22448 if (new_node != NULL)
22450 new_node->dw_discr_next = discr_lists[i];
22451 discr_lists[i] = new_node;
22453 match_expr = next_round_match_expr;
22457 /* If we reach this point, we could match everything we were interested
22458 in. */
22459 return;
22461 abort:
22462 /* Clean all data structure and return no result. */
22463 free (*discr_lists_p);
22464 *discr_lists_p = NULL;
22465 *discr_decl = NULL_TREE;
22468 /* Generate a DIE to represent VARIANT_PART_DECL, a variant part that is part
22469 of STRUCT_TYPE, a record type. This new DIE is emitted as the next child
22470 under CONTEXT_DIE.
22472 Variant parts are supposed to be implemented as a FIELD_DECL whose type is a
22473 QUAL_UNION_TYPE: this is the VARIANT_PART_DECL parameter. The members for
22474 this type, which are record types, represent the available variants and each
22475 has a DECL_QUALIFIER attribute. The discriminant and the discriminant
22476 values are inferred from these attributes.
22478 In trees, the offsets for the fields inside these sub-records are relative
22479 to the variant part itself, whereas the corresponding DIEs should have
22480 offset attributes that are relative to the embedding record base address.
22481 This is why the caller must provide a VARIANT_PART_OFFSET expression: it
22482 must be an expression that computes the offset of the variant part to
22483 describe in DWARF. */
22485 static void
22486 gen_variant_part (tree variant_part_decl, struct vlr_context *vlr_ctx,
22487 dw_die_ref context_die)
22489 const tree variant_part_type = TREE_TYPE (variant_part_decl);
22490 tree variant_part_offset = vlr_ctx->variant_part_offset;
22491 struct loc_descr_context ctx = {
22492 vlr_ctx->struct_type, /* context_type */
22493 NULL_TREE, /* base_decl */
22494 NULL /* dpi */
22497 /* The FIELD_DECL node in STRUCT_TYPE that acts as the discriminant, or
22498 NULL_TREE if there is no such field. */
22499 tree discr_decl = NULL_TREE;
22500 dw_discr_list_ref *discr_lists;
22501 unsigned discr_lists_length = 0;
22502 unsigned i;
22504 dw_die_ref dwarf_proc_die = NULL;
22505 dw_die_ref variant_part_die
22506 = new_die (DW_TAG_variant_part, context_die, variant_part_type);
22508 equate_decl_number_to_die (variant_part_decl, variant_part_die);
22510 analyze_variants_discr (variant_part_decl, vlr_ctx->struct_type,
22511 &discr_decl, &discr_lists, &discr_lists_length);
22513 if (discr_decl != NULL_TREE)
22515 dw_die_ref discr_die = lookup_decl_die (discr_decl);
22517 if (discr_die)
22518 add_AT_die_ref (variant_part_die, DW_AT_discr, discr_die);
22519 else
22520 /* We have no DIE for the discriminant, so just discard all
22521 discrimimant information in the output. */
22522 discr_decl = NULL_TREE;
22525 /* If the offset for this variant part is more complex than a constant,
22526 create a DWARF procedure for it so that we will not have to generate DWARF
22527 expressions for it for each member. */
22528 if (TREE_CODE (variant_part_offset) != INTEGER_CST
22529 && (dwarf_version >= 3 || !dwarf_strict))
22531 const tree dwarf_proc_fndecl
22532 = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, NULL_TREE,
22533 build_function_type (TREE_TYPE (variant_part_offset),
22534 NULL_TREE));
22535 const tree dwarf_proc_call = build_call_expr (dwarf_proc_fndecl, 0);
22536 const dw_loc_descr_ref dwarf_proc_body
22537 = loc_descriptor_from_tree (variant_part_offset, 0, &ctx);
22539 dwarf_proc_die = new_dwarf_proc_die (dwarf_proc_body,
22540 dwarf_proc_fndecl, context_die);
22541 if (dwarf_proc_die != NULL)
22542 variant_part_offset = dwarf_proc_call;
22545 /* Output DIEs for all variants. */
22546 i = 0;
22547 for (tree variant = TYPE_FIELDS (variant_part_type);
22548 variant != NULL_TREE;
22549 variant = DECL_CHAIN (variant), ++i)
22551 tree variant_type = TREE_TYPE (variant);
22552 dw_die_ref variant_die;
22554 /* All variants (i.e. members of a variant part) are supposed to be
22555 encoded as structures. Sub-variant parts are QUAL_UNION_TYPE fields
22556 under these records. */
22557 gcc_assert (TREE_CODE (variant_type) == RECORD_TYPE);
22559 variant_die = new_die (DW_TAG_variant, variant_part_die, variant_type);
22560 equate_decl_number_to_die (variant, variant_die);
22562 /* Output discriminant values this variant matches, if any. */
22563 if (discr_decl == NULL || discr_lists[i] == NULL)
22564 /* In the case we have discriminant information at all, this is
22565 probably the default variant: as the standard says, don't
22566 output any discriminant value/list attribute. */
22568 else if (discr_lists[i]->dw_discr_next == NULL
22569 && !discr_lists[i]->dw_discr_range)
22570 /* If there is only one accepted value, don't bother outputting a
22571 list. */
22572 add_discr_value (variant_die, &discr_lists[i]->dw_discr_lower_bound);
22573 else
22574 add_discr_list (variant_die, discr_lists[i]);
22576 for (tree member = TYPE_FIELDS (variant_type);
22577 member != NULL_TREE;
22578 member = DECL_CHAIN (member))
22580 struct vlr_context vlr_sub_ctx = {
22581 vlr_ctx->struct_type, /* struct_type */
22582 NULL /* variant_part_offset */
22584 if (is_variant_part (member))
22586 /* All offsets for fields inside variant parts are relative to
22587 the top-level embedding RECORD_TYPE's base address. On the
22588 other hand, offsets in GCC's types are relative to the
22589 nested-most variant part. So we have to sum offsets each time
22590 we recurse. */
22592 vlr_sub_ctx.variant_part_offset
22593 = fold (build2 (PLUS_EXPR, TREE_TYPE (variant_part_offset),
22594 variant_part_offset, byte_position (member)));
22595 gen_variant_part (member, &vlr_sub_ctx, variant_die);
22597 else
22599 vlr_sub_ctx.variant_part_offset = variant_part_offset;
22600 gen_decl_die (member, NULL, &vlr_sub_ctx, variant_die);
22605 free (discr_lists);
22608 /* Generate a DIE for a class member. */
22610 static void
22611 gen_member_die (tree type, dw_die_ref context_die)
22613 tree member;
22614 tree binfo = TYPE_BINFO (type);
22615 dw_die_ref child;
22617 /* If this is not an incomplete type, output descriptions of each of its
22618 members. Note that as we output the DIEs necessary to represent the
22619 members of this record or union type, we will also be trying to output
22620 DIEs to represent the *types* of those members. However the `type'
22621 function (above) will specifically avoid generating type DIEs for member
22622 types *within* the list of member DIEs for this (containing) type except
22623 for those types (of members) which are explicitly marked as also being
22624 members of this (containing) type themselves. The g++ front- end can
22625 force any given type to be treated as a member of some other (containing)
22626 type by setting the TYPE_CONTEXT of the given (member) type to point to
22627 the TREE node representing the appropriate (containing) type. */
22629 /* First output info about the base classes. */
22630 if (binfo)
22632 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
22633 int i;
22634 tree base;
22636 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
22637 gen_inheritance_die (base,
22638 (accesses ? (*accesses)[i] : access_public_node),
22639 type,
22640 context_die);
22643 /* Now output info about the data members and type members. */
22644 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
22646 struct vlr_context vlr_ctx = { type, NULL_TREE };
22648 /* If we thought we were generating minimal debug info for TYPE
22649 and then changed our minds, some of the member declarations
22650 may have already been defined. Don't define them again, but
22651 do put them in the right order. */
22653 child = lookup_decl_die (member);
22654 if (child)
22655 splice_child_die (context_die, child);
22657 /* Do not generate standard DWARF for variant parts if we are generating
22658 the corresponding GNAT encodings: DIEs generated for both would
22659 conflict in our mappings. */
22660 else if (is_variant_part (member)
22661 && gnat_encodings == DWARF_GNAT_ENCODINGS_MINIMAL)
22663 vlr_ctx.variant_part_offset = byte_position (member);
22664 gen_variant_part (member, &vlr_ctx, context_die);
22666 else
22668 vlr_ctx.variant_part_offset = NULL_TREE;
22669 gen_decl_die (member, NULL, &vlr_ctx, context_die);
22673 /* We do not keep type methods in type variants. */
22674 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
22675 /* Now output info about the function members (if any). */
22676 if (TYPE_METHODS (type) != error_mark_node)
22677 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
22679 /* Don't include clones in the member list. */
22680 if (DECL_ABSTRACT_ORIGIN (member))
22681 continue;
22682 /* Nor constructors for anonymous classes. */
22683 if (DECL_ARTIFICIAL (member)
22684 && dwarf2_name (member, 0) == NULL)
22685 continue;
22687 child = lookup_decl_die (member);
22688 if (child)
22689 splice_child_die (context_die, child);
22690 else
22691 gen_decl_die (member, NULL, NULL, context_die);
22695 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
22696 is set, we pretend that the type was never defined, so we only get the
22697 member DIEs needed by later specification DIEs. */
22699 static void
22700 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
22701 enum debug_info_usage usage)
22703 if (TREE_ASM_WRITTEN (type))
22705 /* Fill in the bound of variable-length fields in late dwarf if
22706 still incomplete. */
22707 if (!early_dwarf && variably_modified_type_p (type, NULL))
22708 for (tree member = TYPE_FIELDS (type);
22709 member;
22710 member = DECL_CHAIN (member))
22711 fill_variable_array_bounds (TREE_TYPE (member));
22712 return;
22715 dw_die_ref type_die = lookup_type_die (type);
22716 dw_die_ref scope_die = 0;
22717 int nested = 0;
22718 int complete = (TYPE_SIZE (type)
22719 && (! TYPE_STUB_DECL (type)
22720 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
22721 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
22722 complete = complete && should_emit_struct_debug (type, usage);
22724 if (type_die && ! complete)
22725 return;
22727 if (TYPE_CONTEXT (type) != NULL_TREE
22728 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22729 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
22730 nested = 1;
22732 scope_die = scope_die_for (type, context_die);
22734 /* Generate child dies for template paramaters. */
22735 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
22736 schedule_generic_params_dies_gen (type);
22738 if (! type_die || (nested && is_cu_die (scope_die)))
22739 /* First occurrence of type or toplevel definition of nested class. */
22741 dw_die_ref old_die = type_die;
22743 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
22744 ? record_type_tag (type) : DW_TAG_union_type,
22745 scope_die, type);
22746 equate_type_number_to_die (type, type_die);
22747 if (old_die)
22748 add_AT_specification (type_die, old_die);
22749 else
22750 add_name_attribute (type_die, type_tag (type));
22752 else
22753 remove_AT (type_die, DW_AT_declaration);
22755 /* If this type has been completed, then give it a byte_size attribute and
22756 then give a list of members. */
22757 if (complete && !ns_decl)
22759 /* Prevent infinite recursion in cases where the type of some member of
22760 this type is expressed in terms of this type itself. */
22761 TREE_ASM_WRITTEN (type) = 1;
22762 add_byte_size_attribute (type_die, type);
22763 if (TYPE_STUB_DECL (type) != NULL_TREE)
22765 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
22766 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
22769 /* If the first reference to this type was as the return type of an
22770 inline function, then it may not have a parent. Fix this now. */
22771 if (type_die->die_parent == NULL)
22772 add_child_die (scope_die, type_die);
22774 push_decl_scope (type);
22775 gen_member_die (type, type_die);
22776 pop_decl_scope ();
22778 add_gnat_descriptive_type_attribute (type_die, type, context_die);
22779 if (TYPE_ARTIFICIAL (type))
22780 add_AT_flag (type_die, DW_AT_artificial, 1);
22782 /* GNU extension: Record what type our vtable lives in. */
22783 if (TYPE_VFIELD (type))
22785 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
22787 gen_type_die (vtype, context_die);
22788 add_AT_die_ref (type_die, DW_AT_containing_type,
22789 lookup_type_die (vtype));
22792 else
22794 add_AT_flag (type_die, DW_AT_declaration, 1);
22796 /* We don't need to do this for function-local types. */
22797 if (TYPE_STUB_DECL (type)
22798 && ! decl_function_context (TYPE_STUB_DECL (type)))
22799 vec_safe_push (incomplete_types, type);
22802 if (get_AT (type_die, DW_AT_name))
22803 add_pubtype (type, type_die);
22806 /* Generate a DIE for a subroutine _type_. */
22808 static void
22809 gen_subroutine_type_die (tree type, dw_die_ref context_die)
22811 tree return_type = TREE_TYPE (type);
22812 dw_die_ref subr_die
22813 = new_die (DW_TAG_subroutine_type,
22814 scope_die_for (type, context_die), type);
22816 equate_type_number_to_die (type, subr_die);
22817 add_prototyped_attribute (subr_die, type);
22818 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, false,
22819 context_die);
22820 gen_formal_types_die (type, subr_die);
22822 if (get_AT (subr_die, DW_AT_name))
22823 add_pubtype (type, subr_die);
22826 /* Generate a DIE for a type definition. */
22828 static void
22829 gen_typedef_die (tree decl, dw_die_ref context_die)
22831 dw_die_ref type_die;
22832 tree origin;
22834 if (TREE_ASM_WRITTEN (decl))
22836 if (DECL_ORIGINAL_TYPE (decl))
22837 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
22838 return;
22841 TREE_ASM_WRITTEN (decl) = 1;
22842 type_die = new_die (DW_TAG_typedef, context_die, decl);
22843 origin = decl_ultimate_origin (decl);
22844 if (origin != NULL)
22845 add_abstract_origin_attribute (type_die, origin);
22846 else
22848 tree type;
22850 add_name_and_src_coords_attributes (type_die, decl);
22851 if (DECL_ORIGINAL_TYPE (decl))
22853 type = DECL_ORIGINAL_TYPE (decl);
22855 if (type == error_mark_node)
22856 return;
22858 gcc_assert (type != TREE_TYPE (decl));
22859 equate_type_number_to_die (TREE_TYPE (decl), type_die);
22861 else
22863 type = TREE_TYPE (decl);
22865 if (type == error_mark_node)
22866 return;
22868 if (is_naming_typedef_decl (TYPE_NAME (type)))
22870 /* Here, we are in the case of decl being a typedef naming
22871 an anonymous type, e.g:
22872 typedef struct {...} foo;
22873 In that case TREE_TYPE (decl) is not a typedef variant
22874 type and TYPE_NAME of the anonymous type is set to the
22875 TYPE_DECL of the typedef. This construct is emitted by
22876 the C++ FE.
22878 TYPE is the anonymous struct named by the typedef
22879 DECL. As we need the DW_AT_type attribute of the
22880 DW_TAG_typedef to point to the DIE of TYPE, let's
22881 generate that DIE right away. add_type_attribute
22882 called below will then pick (via lookup_type_die) that
22883 anonymous struct DIE. */
22884 if (!TREE_ASM_WRITTEN (type))
22885 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
22887 /* This is a GNU Extension. We are adding a
22888 DW_AT_linkage_name attribute to the DIE of the
22889 anonymous struct TYPE. The value of that attribute
22890 is the name of the typedef decl naming the anonymous
22891 struct. This greatly eases the work of consumers of
22892 this debug info. */
22893 add_linkage_name_raw (lookup_type_die (type), decl);
22897 add_type_attribute (type_die, type, decl_quals (decl), false,
22898 context_die);
22900 if (is_naming_typedef_decl (decl))
22901 /* We want that all subsequent calls to lookup_type_die with
22902 TYPE in argument yield the DW_TAG_typedef we have just
22903 created. */
22904 equate_type_number_to_die (type, type_die);
22906 add_accessibility_attribute (type_die, decl);
22909 if (DECL_ABSTRACT_P (decl))
22910 equate_decl_number_to_die (decl, type_die);
22912 if (get_AT (type_die, DW_AT_name))
22913 add_pubtype (decl, type_die);
22916 /* Generate a DIE for a struct, class, enum or union type. */
22918 static void
22919 gen_tagged_type_die (tree type,
22920 dw_die_ref context_die,
22921 enum debug_info_usage usage)
22923 int need_pop;
22925 if (type == NULL_TREE
22926 || !is_tagged_type (type))
22927 return;
22929 if (TREE_ASM_WRITTEN (type))
22930 need_pop = 0;
22931 /* If this is a nested type whose containing class hasn't been written
22932 out yet, writing it out will cover this one, too. This does not apply
22933 to instantiations of member class templates; they need to be added to
22934 the containing class as they are generated. FIXME: This hurts the
22935 idea of combining type decls from multiple TUs, since we can't predict
22936 what set of template instantiations we'll get. */
22937 else if (TYPE_CONTEXT (type)
22938 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
22939 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
22941 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
22943 if (TREE_ASM_WRITTEN (type))
22944 return;
22946 /* If that failed, attach ourselves to the stub. */
22947 push_decl_scope (TYPE_CONTEXT (type));
22948 context_die = lookup_type_die (TYPE_CONTEXT (type));
22949 need_pop = 1;
22951 else if (TYPE_CONTEXT (type) != NULL_TREE
22952 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
22954 /* If this type is local to a function that hasn't been written
22955 out yet, use a NULL context for now; it will be fixed up in
22956 decls_for_scope. */
22957 context_die = lookup_decl_die (TYPE_CONTEXT (type));
22958 /* A declaration DIE doesn't count; nested types need to go in the
22959 specification. */
22960 if (context_die && is_declaration_die (context_die))
22961 context_die = NULL;
22962 need_pop = 0;
22964 else
22966 context_die = declare_in_namespace (type, context_die);
22967 need_pop = 0;
22970 if (TREE_CODE (type) == ENUMERAL_TYPE)
22972 /* This might have been written out by the call to
22973 declare_in_namespace. */
22974 if (!TREE_ASM_WRITTEN (type))
22975 gen_enumeration_type_die (type, context_die);
22977 else
22978 gen_struct_or_union_type_die (type, context_die, usage);
22980 if (need_pop)
22981 pop_decl_scope ();
22983 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
22984 it up if it is ever completed. gen_*_type_die will set it for us
22985 when appropriate. */
22988 /* Generate a type description DIE. */
22990 static void
22991 gen_type_die_with_usage (tree type, dw_die_ref context_die,
22992 enum debug_info_usage usage)
22994 struct array_descr_info info;
22996 if (type == NULL_TREE || type == error_mark_node)
22997 return;
22999 if (flag_checking && type)
23000 verify_type (type);
23002 if (TYPE_NAME (type) != NULL_TREE
23003 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
23004 && is_redundant_typedef (TYPE_NAME (type))
23005 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
23006 /* The DECL of this type is a typedef we don't want to emit debug
23007 info for but we want debug info for its underlying typedef.
23008 This can happen for e.g, the injected-class-name of a C++
23009 type. */
23010 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
23012 /* If TYPE is a typedef type variant, let's generate debug info
23013 for the parent typedef which TYPE is a type of. */
23014 if (typedef_variant_p (type))
23016 if (TREE_ASM_WRITTEN (type))
23017 return;
23019 /* Prevent broken recursion; we can't hand off to the same type. */
23020 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
23022 /* Give typedefs the right scope. */
23023 context_die = scope_die_for (type, context_die);
23025 TREE_ASM_WRITTEN (type) = 1;
23027 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23028 return;
23031 /* If type is an anonymous tagged type named by a typedef, let's
23032 generate debug info for the typedef. */
23033 if (is_naming_typedef_decl (TYPE_NAME (type)))
23035 /* Use the DIE of the containing namespace as the parent DIE of
23036 the type description DIE we want to generate. */
23037 if (DECL_CONTEXT (TYPE_NAME (type))
23038 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
23039 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
23041 gen_decl_die (TYPE_NAME (type), NULL, NULL, context_die);
23042 return;
23045 /* We are going to output a DIE to represent the unqualified version
23046 of this type (i.e. without any const or volatile qualifiers) so
23047 get the main variant (i.e. the unqualified version) of this type
23048 now. (Vectors and arrays are special because the debugging info is in the
23049 cloned type itself). */
23050 if (TREE_CODE (type) != VECTOR_TYPE
23051 && TREE_CODE (type) != ARRAY_TYPE)
23052 type = type_main_variant (type);
23054 /* If this is an array type with hidden descriptor, handle it first. */
23055 if (!TREE_ASM_WRITTEN (type)
23056 && lang_hooks.types.get_array_descr_info)
23058 memset (&info, 0, sizeof (info));
23059 if (lang_hooks.types.get_array_descr_info (type, &info))
23061 /* Fortran sometimes emits array types with no dimension. */
23062 gcc_assert (info.ndimensions >= 0
23063 && (info.ndimensions
23064 <= DWARF2OUT_ARRAY_DESCR_INFO_MAX_DIMEN));
23065 gen_descr_array_type_die (type, &info, context_die);
23066 TREE_ASM_WRITTEN (type) = 1;
23067 return;
23071 if (TREE_ASM_WRITTEN (type))
23073 /* Variable-length types may be incomplete even if
23074 TREE_ASM_WRITTEN. For such types, fall through to
23075 gen_array_type_die() and possibly fill in
23076 DW_AT_{upper,lower}_bound attributes. */
23077 if ((TREE_CODE (type) != ARRAY_TYPE
23078 && TREE_CODE (type) != RECORD_TYPE
23079 && TREE_CODE (type) != UNION_TYPE
23080 && TREE_CODE (type) != QUAL_UNION_TYPE)
23081 || !variably_modified_type_p (type, NULL))
23082 return;
23085 switch (TREE_CODE (type))
23087 case ERROR_MARK:
23088 break;
23090 case POINTER_TYPE:
23091 case REFERENCE_TYPE:
23092 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
23093 ensures that the gen_type_die recursion will terminate even if the
23094 type is recursive. Recursive types are possible in Ada. */
23095 /* ??? We could perhaps do this for all types before the switch
23096 statement. */
23097 TREE_ASM_WRITTEN (type) = 1;
23099 /* For these types, all that is required is that we output a DIE (or a
23100 set of DIEs) to represent the "basis" type. */
23101 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23102 DINFO_USAGE_IND_USE);
23103 break;
23105 case OFFSET_TYPE:
23106 /* This code is used for C++ pointer-to-data-member types.
23107 Output a description of the relevant class type. */
23108 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
23109 DINFO_USAGE_IND_USE);
23111 /* Output a description of the type of the object pointed to. */
23112 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23113 DINFO_USAGE_IND_USE);
23115 /* Now output a DIE to represent this pointer-to-data-member type
23116 itself. */
23117 gen_ptr_to_mbr_type_die (type, context_die);
23118 break;
23120 case FUNCTION_TYPE:
23121 /* Force out return type (in case it wasn't forced out already). */
23122 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23123 DINFO_USAGE_DIR_USE);
23124 gen_subroutine_type_die (type, context_die);
23125 break;
23127 case METHOD_TYPE:
23128 /* Force out return type (in case it wasn't forced out already). */
23129 gen_type_die_with_usage (TREE_TYPE (type), context_die,
23130 DINFO_USAGE_DIR_USE);
23131 gen_subroutine_type_die (type, context_die);
23132 break;
23134 case ARRAY_TYPE:
23135 case VECTOR_TYPE:
23136 gen_array_type_die (type, context_die);
23137 break;
23139 case ENUMERAL_TYPE:
23140 case RECORD_TYPE:
23141 case UNION_TYPE:
23142 case QUAL_UNION_TYPE:
23143 gen_tagged_type_die (type, context_die, usage);
23144 return;
23146 case VOID_TYPE:
23147 case INTEGER_TYPE:
23148 case REAL_TYPE:
23149 case FIXED_POINT_TYPE:
23150 case COMPLEX_TYPE:
23151 case BOOLEAN_TYPE:
23152 case POINTER_BOUNDS_TYPE:
23153 /* No DIEs needed for fundamental types. */
23154 break;
23156 case NULLPTR_TYPE:
23157 case LANG_TYPE:
23158 /* Just use DW_TAG_unspecified_type. */
23160 dw_die_ref type_die = lookup_type_die (type);
23161 if (type_die == NULL)
23163 tree name = TYPE_IDENTIFIER (type);
23164 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
23165 type);
23166 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
23167 equate_type_number_to_die (type, type_die);
23170 break;
23172 default:
23173 if (is_cxx_auto (type))
23175 tree name = TYPE_IDENTIFIER (type);
23176 dw_die_ref *die = (name == get_identifier ("auto")
23177 ? &auto_die : &decltype_auto_die);
23178 if (!*die)
23180 *die = new_die (DW_TAG_unspecified_type,
23181 comp_unit_die (), NULL_TREE);
23182 add_name_attribute (*die, IDENTIFIER_POINTER (name));
23184 equate_type_number_to_die (type, *die);
23185 break;
23187 gcc_unreachable ();
23190 TREE_ASM_WRITTEN (type) = 1;
23193 static void
23194 gen_type_die (tree type, dw_die_ref context_die)
23196 if (type != error_mark_node)
23198 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
23199 if (flag_checking)
23201 dw_die_ref die = lookup_type_die (type);
23202 if (die)
23203 check_die (die);
23208 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
23209 things which are local to the given block. */
23211 static void
23212 gen_block_die (tree stmt, dw_die_ref context_die)
23214 int must_output_die = 0;
23215 bool inlined_func;
23217 /* Ignore blocks that are NULL. */
23218 if (stmt == NULL_TREE)
23219 return;
23221 inlined_func = inlined_function_outer_scope_p (stmt);
23223 /* If the block is one fragment of a non-contiguous block, do not
23224 process the variables, since they will have been done by the
23225 origin block. Do process subblocks. */
23226 if (BLOCK_FRAGMENT_ORIGIN (stmt))
23228 tree sub;
23230 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
23231 gen_block_die (sub, context_die);
23233 return;
23236 /* Determine if we need to output any Dwarf DIEs at all to represent this
23237 block. */
23238 if (inlined_func)
23239 /* The outer scopes for inlinings *must* always be represented. We
23240 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
23241 must_output_die = 1;
23242 else
23244 /* Determine if this block directly contains any "significant"
23245 local declarations which we will need to output DIEs for. */
23246 if (debug_info_level > DINFO_LEVEL_TERSE)
23247 /* We are not in terse mode so *any* local declaration counts
23248 as being a "significant" one. */
23249 must_output_die = ((BLOCK_VARS (stmt) != NULL
23250 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
23251 && (TREE_USED (stmt)
23252 || TREE_ASM_WRITTEN (stmt)
23253 || BLOCK_ABSTRACT (stmt)));
23254 else if ((TREE_USED (stmt)
23255 || TREE_ASM_WRITTEN (stmt)
23256 || BLOCK_ABSTRACT (stmt))
23257 && !dwarf2out_ignore_block (stmt))
23258 must_output_die = 1;
23261 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
23262 DIE for any block which contains no significant local declarations at
23263 all. Rather, in such cases we just call `decls_for_scope' so that any
23264 needed Dwarf info for any sub-blocks will get properly generated. Note
23265 that in terse mode, our definition of what constitutes a "significant"
23266 local declaration gets restricted to include only inlined function
23267 instances and local (nested) function definitions. */
23268 if (must_output_die)
23270 if (inlined_func)
23272 /* If STMT block is abstract, that means we have been called
23273 indirectly from dwarf2out_abstract_function.
23274 That function rightfully marks the descendent blocks (of
23275 the abstract function it is dealing with) as being abstract,
23276 precisely to prevent us from emitting any
23277 DW_TAG_inlined_subroutine DIE as a descendent
23278 of an abstract function instance. So in that case, we should
23279 not call gen_inlined_subroutine_die.
23281 Later though, when cgraph asks dwarf2out to emit info
23282 for the concrete instance of the function decl into which
23283 the concrete instance of STMT got inlined, the later will lead
23284 to the generation of a DW_TAG_inlined_subroutine DIE. */
23285 if (! BLOCK_ABSTRACT (stmt))
23286 gen_inlined_subroutine_die (stmt, context_die);
23288 else
23289 gen_lexical_block_die (stmt, context_die);
23291 else
23292 decls_for_scope (stmt, context_die);
23295 /* Process variable DECL (or variable with origin ORIGIN) within
23296 block STMT and add it to CONTEXT_DIE. */
23297 static void
23298 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
23300 dw_die_ref die;
23301 tree decl_or_origin = decl ? decl : origin;
23303 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
23304 die = lookup_decl_die (decl_or_origin);
23305 else if (TREE_CODE (decl_or_origin) == TYPE_DECL)
23307 if (TYPE_DECL_IS_STUB (decl_or_origin))
23308 die = lookup_type_die (TREE_TYPE (decl_or_origin));
23309 else
23310 die = lookup_decl_die (decl_or_origin);
23311 /* Avoid re-creating the DIE late if it was optimized as unused early. */
23312 if (! die && ! early_dwarf)
23313 return;
23315 else
23316 die = NULL;
23318 if (die != NULL && die->die_parent == NULL)
23319 add_child_die (context_die, die);
23320 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
23322 if (early_dwarf)
23323 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
23324 stmt, context_die);
23326 else
23327 gen_decl_die (decl, origin, NULL, context_die);
23330 /* Generate all of the decls declared within a given scope and (recursively)
23331 all of its sub-blocks. */
23333 static void
23334 decls_for_scope (tree stmt, dw_die_ref context_die)
23336 tree decl;
23337 unsigned int i;
23338 tree subblocks;
23340 /* Ignore NULL blocks. */
23341 if (stmt == NULL_TREE)
23342 return;
23344 /* Output the DIEs to represent all of the data objects and typedefs
23345 declared directly within this block but not within any nested
23346 sub-blocks. Also, nested function and tag DIEs have been
23347 generated with a parent of NULL; fix that up now. We don't
23348 have to do this if we're at -g1. */
23349 if (debug_info_level > DINFO_LEVEL_TERSE)
23351 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
23352 process_scope_var (stmt, decl, NULL_TREE, context_die);
23353 /* BLOCK_NONLOCALIZED_VARs simply generate DIE stubs with abstract
23354 origin - avoid doing this twice as we have no good way to see
23355 if we've done it once already. */
23356 if (! early_dwarf)
23357 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
23358 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
23359 context_die);
23362 /* Even if we're at -g1, we need to process the subblocks in order to get
23363 inlined call information. */
23365 /* Output the DIEs to represent all sub-blocks (and the items declared
23366 therein) of this block. */
23367 for (subblocks = BLOCK_SUBBLOCKS (stmt);
23368 subblocks != NULL;
23369 subblocks = BLOCK_CHAIN (subblocks))
23370 gen_block_die (subblocks, context_die);
23373 /* Is this a typedef we can avoid emitting? */
23375 bool
23376 is_redundant_typedef (const_tree decl)
23378 if (TYPE_DECL_IS_STUB (decl))
23379 return true;
23381 if (DECL_ARTIFICIAL (decl)
23382 && DECL_CONTEXT (decl)
23383 && is_tagged_type (DECL_CONTEXT (decl))
23384 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
23385 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
23386 /* Also ignore the artificial member typedef for the class name. */
23387 return true;
23389 return false;
23392 /* Return TRUE if TYPE is a typedef that names a type for linkage
23393 purposes. This kind of typedefs is produced by the C++ FE for
23394 constructs like:
23396 typedef struct {...} foo;
23398 In that case, there is no typedef variant type produced for foo.
23399 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
23400 struct type. */
23402 static bool
23403 is_naming_typedef_decl (const_tree decl)
23405 if (decl == NULL_TREE
23406 || TREE_CODE (decl) != TYPE_DECL
23407 || DECL_NAMELESS (decl)
23408 || !is_tagged_type (TREE_TYPE (decl))
23409 || DECL_IS_BUILTIN (decl)
23410 || is_redundant_typedef (decl)
23411 /* It looks like Ada produces TYPE_DECLs that are very similar
23412 to C++ naming typedefs but that have different
23413 semantics. Let's be specific to c++ for now. */
23414 || !is_cxx ())
23415 return FALSE;
23417 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
23418 && TYPE_NAME (TREE_TYPE (decl)) == decl
23419 && (TYPE_STUB_DECL (TREE_TYPE (decl))
23420 != TYPE_NAME (TREE_TYPE (decl))));
23423 /* Looks up the DIE for a context. */
23425 static inline dw_die_ref
23426 lookup_context_die (tree context)
23428 if (context)
23430 /* Find die that represents this context. */
23431 if (TYPE_P (context))
23433 context = TYPE_MAIN_VARIANT (context);
23434 dw_die_ref ctx = lookup_type_die (context);
23435 if (!ctx)
23436 return NULL;
23437 return strip_naming_typedef (context, ctx);
23439 else
23440 return lookup_decl_die (context);
23442 return comp_unit_die ();
23445 /* Returns the DIE for a context. */
23447 static inline dw_die_ref
23448 get_context_die (tree context)
23450 if (context)
23452 /* Find die that represents this context. */
23453 if (TYPE_P (context))
23455 context = TYPE_MAIN_VARIANT (context);
23456 return strip_naming_typedef (context, force_type_die (context));
23458 else
23459 return force_decl_die (context);
23461 return comp_unit_die ();
23464 /* Returns the DIE for decl. A DIE will always be returned. */
23466 static dw_die_ref
23467 force_decl_die (tree decl)
23469 dw_die_ref decl_die;
23470 unsigned saved_external_flag;
23471 tree save_fn = NULL_TREE;
23472 decl_die = lookup_decl_die (decl);
23473 if (!decl_die)
23475 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
23477 decl_die = lookup_decl_die (decl);
23478 if (decl_die)
23479 return decl_die;
23481 switch (TREE_CODE (decl))
23483 case FUNCTION_DECL:
23484 /* Clear current_function_decl, so that gen_subprogram_die thinks
23485 that this is a declaration. At this point, we just want to force
23486 declaration die. */
23487 save_fn = current_function_decl;
23488 current_function_decl = NULL_TREE;
23489 gen_subprogram_die (decl, context_die);
23490 current_function_decl = save_fn;
23491 break;
23493 case VAR_DECL:
23494 /* Set external flag to force declaration die. Restore it after
23495 gen_decl_die() call. */
23496 saved_external_flag = DECL_EXTERNAL (decl);
23497 DECL_EXTERNAL (decl) = 1;
23498 gen_decl_die (decl, NULL, NULL, context_die);
23499 DECL_EXTERNAL (decl) = saved_external_flag;
23500 break;
23502 case NAMESPACE_DECL:
23503 if (dwarf_version >= 3 || !dwarf_strict)
23504 dwarf2out_decl (decl);
23505 else
23506 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
23507 decl_die = comp_unit_die ();
23508 break;
23510 case TRANSLATION_UNIT_DECL:
23511 decl_die = comp_unit_die ();
23512 break;
23514 default:
23515 gcc_unreachable ();
23518 /* We should be able to find the DIE now. */
23519 if (!decl_die)
23520 decl_die = lookup_decl_die (decl);
23521 gcc_assert (decl_die);
23524 return decl_die;
23527 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
23528 always returned. */
23530 static dw_die_ref
23531 force_type_die (tree type)
23533 dw_die_ref type_die;
23535 type_die = lookup_type_die (type);
23536 if (!type_die)
23538 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
23540 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
23541 false, context_die);
23542 gcc_assert (type_die);
23544 return type_die;
23547 /* Force out any required namespaces to be able to output DECL,
23548 and return the new context_die for it, if it's changed. */
23550 static dw_die_ref
23551 setup_namespace_context (tree thing, dw_die_ref context_die)
23553 tree context = (DECL_P (thing)
23554 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
23555 if (context && TREE_CODE (context) == NAMESPACE_DECL)
23556 /* Force out the namespace. */
23557 context_die = force_decl_die (context);
23559 return context_die;
23562 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
23563 type) within its namespace, if appropriate.
23565 For compatibility with older debuggers, namespace DIEs only contain
23566 declarations; all definitions are emitted at CU scope, with
23567 DW_AT_specification pointing to the declaration (like with class
23568 members). */
23570 static dw_die_ref
23571 declare_in_namespace (tree thing, dw_die_ref context_die)
23573 dw_die_ref ns_context;
23575 if (debug_info_level <= DINFO_LEVEL_TERSE)
23576 return context_die;
23578 /* External declarations in the local scope only need to be emitted
23579 once, not once in the namespace and once in the scope.
23581 This avoids declaring the `extern' below in the
23582 namespace DIE as well as in the innermost scope:
23584 namespace S
23586 int i=5;
23587 int foo()
23589 int i=8;
23590 extern int i;
23591 return i;
23595 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
23596 return context_die;
23598 /* If this decl is from an inlined function, then don't try to emit it in its
23599 namespace, as we will get confused. It would have already been emitted
23600 when the abstract instance of the inline function was emitted anyways. */
23601 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
23602 return context_die;
23604 ns_context = setup_namespace_context (thing, context_die);
23606 if (ns_context != context_die)
23608 if (is_fortran ())
23609 return ns_context;
23610 if (DECL_P (thing))
23611 gen_decl_die (thing, NULL, NULL, ns_context);
23612 else
23613 gen_type_die (thing, ns_context);
23615 return context_die;
23618 /* Generate a DIE for a namespace or namespace alias. */
23620 static void
23621 gen_namespace_die (tree decl, dw_die_ref context_die)
23623 dw_die_ref namespace_die;
23625 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
23626 they are an alias of. */
23627 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
23629 /* Output a real namespace or module. */
23630 context_die = setup_namespace_context (decl, comp_unit_die ());
23631 namespace_die = new_die (is_fortran ()
23632 ? DW_TAG_module : DW_TAG_namespace,
23633 context_die, decl);
23634 /* For Fortran modules defined in different CU don't add src coords. */
23635 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
23637 const char *name = dwarf2_name (decl, 0);
23638 if (name)
23639 add_name_attribute (namespace_die, name);
23641 else
23642 add_name_and_src_coords_attributes (namespace_die, decl);
23643 if (DECL_EXTERNAL (decl))
23644 add_AT_flag (namespace_die, DW_AT_declaration, 1);
23645 equate_decl_number_to_die (decl, namespace_die);
23647 else
23649 /* Output a namespace alias. */
23651 /* Force out the namespace we are an alias of, if necessary. */
23652 dw_die_ref origin_die
23653 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
23655 if (DECL_FILE_SCOPE_P (decl)
23656 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
23657 context_die = setup_namespace_context (decl, comp_unit_die ());
23658 /* Now create the namespace alias DIE. */
23659 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
23660 add_name_and_src_coords_attributes (namespace_die, decl);
23661 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
23662 equate_decl_number_to_die (decl, namespace_die);
23664 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
23665 if (want_pubnames ())
23666 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
23669 /* Generate Dwarf debug information for a decl described by DECL.
23670 The return value is currently only meaningful for PARM_DECLs,
23671 for all other decls it returns NULL.
23673 If DECL is a FIELD_DECL, CTX is required: see the comment for VLR_CONTEXT.
23674 It can be NULL otherwise. */
23676 static dw_die_ref
23677 gen_decl_die (tree decl, tree origin, struct vlr_context *ctx,
23678 dw_die_ref context_die)
23680 tree decl_or_origin = decl ? decl : origin;
23681 tree class_origin = NULL, ultimate_origin;
23683 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
23684 return NULL;
23686 /* Ignore pointer bounds decls. */
23687 if (DECL_P (decl_or_origin)
23688 && TREE_TYPE (decl_or_origin)
23689 && POINTER_BOUNDS_P (decl_or_origin))
23690 return NULL;
23692 switch (TREE_CODE (decl_or_origin))
23694 case ERROR_MARK:
23695 break;
23697 case CONST_DECL:
23698 if (!is_fortran () && !is_ada ())
23700 /* The individual enumerators of an enum type get output when we output
23701 the Dwarf representation of the relevant enum type itself. */
23702 break;
23705 /* Emit its type. */
23706 gen_type_die (TREE_TYPE (decl), context_die);
23708 /* And its containing namespace. */
23709 context_die = declare_in_namespace (decl, context_die);
23711 gen_const_die (decl, context_die);
23712 break;
23714 case FUNCTION_DECL:
23715 /* Don't output any DIEs to represent mere function declarations,
23716 unless they are class members or explicit block externs. */
23717 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
23718 && DECL_FILE_SCOPE_P (decl_or_origin)
23719 && (current_function_decl == NULL_TREE
23720 || DECL_ARTIFICIAL (decl_or_origin)))
23721 break;
23723 #if 0
23724 /* FIXME */
23725 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
23726 on local redeclarations of global functions. That seems broken. */
23727 if (current_function_decl != decl)
23728 /* This is only a declaration. */;
23729 #endif
23731 /* If we're emitting a clone, emit info for the abstract instance. */
23732 if (origin || DECL_ORIGIN (decl) != decl)
23733 dwarf2out_abstract_function (origin
23734 ? DECL_ORIGIN (origin)
23735 : DECL_ABSTRACT_ORIGIN (decl));
23737 /* If we're emitting an out-of-line copy of an inline function,
23738 emit info for the abstract instance and set up to refer to it. */
23739 else if (cgraph_function_possibly_inlined_p (decl)
23740 && ! DECL_ABSTRACT_P (decl)
23741 && ! class_or_namespace_scope_p (context_die)
23742 /* dwarf2out_abstract_function won't emit a die if this is just
23743 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
23744 that case, because that works only if we have a die. */
23745 && DECL_INITIAL (decl) != NULL_TREE)
23747 dwarf2out_abstract_function (decl);
23748 set_decl_origin_self (decl);
23751 /* Otherwise we're emitting the primary DIE for this decl. */
23752 else if (debug_info_level > DINFO_LEVEL_TERSE)
23754 /* Before we describe the FUNCTION_DECL itself, make sure that we
23755 have its containing type. */
23756 if (!origin)
23757 origin = decl_class_context (decl);
23758 if (origin != NULL_TREE)
23759 gen_type_die (origin, context_die);
23761 /* And its return type. */
23762 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
23764 /* And its virtual context. */
23765 if (DECL_VINDEX (decl) != NULL_TREE)
23766 gen_type_die (DECL_CONTEXT (decl), context_die);
23768 /* Make sure we have a member DIE for decl. */
23769 if (origin != NULL_TREE)
23770 gen_type_die_for_member (origin, decl, context_die);
23772 /* And its containing namespace. */
23773 context_die = declare_in_namespace (decl, context_die);
23776 /* Now output a DIE to represent the function itself. */
23777 if (decl)
23778 gen_subprogram_die (decl, context_die);
23779 break;
23781 case TYPE_DECL:
23782 /* If we are in terse mode, don't generate any DIEs to represent any
23783 actual typedefs. */
23784 if (debug_info_level <= DINFO_LEVEL_TERSE)
23785 break;
23787 /* In the special case of a TYPE_DECL node representing the declaration
23788 of some type tag, if the given TYPE_DECL is marked as having been
23789 instantiated from some other (original) TYPE_DECL node (e.g. one which
23790 was generated within the original definition of an inline function) we
23791 used to generate a special (abbreviated) DW_TAG_structure_type,
23792 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
23793 should be actually referencing those DIEs, as variable DIEs with that
23794 type would be emitted already in the abstract origin, so it was always
23795 removed during unused type prunning. Don't add anything in this
23796 case. */
23797 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
23798 break;
23800 if (is_redundant_typedef (decl))
23801 gen_type_die (TREE_TYPE (decl), context_die);
23802 else
23803 /* Output a DIE to represent the typedef itself. */
23804 gen_typedef_die (decl, context_die);
23805 break;
23807 case LABEL_DECL:
23808 if (debug_info_level >= DINFO_LEVEL_NORMAL)
23809 gen_label_die (decl, context_die);
23810 break;
23812 case VAR_DECL:
23813 case RESULT_DECL:
23814 /* If we are in terse mode, don't generate any DIEs to represent any
23815 variable declarations or definitions. */
23816 if (debug_info_level <= DINFO_LEVEL_TERSE)
23817 break;
23819 /* Output any DIEs that are needed to specify the type of this data
23820 object. */
23821 if (decl_by_reference_p (decl_or_origin))
23822 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23823 else
23824 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23826 /* And its containing type. */
23827 class_origin = decl_class_context (decl_or_origin);
23828 if (class_origin != NULL_TREE)
23829 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
23831 /* And its containing namespace. */
23832 context_die = declare_in_namespace (decl_or_origin, context_die);
23834 /* Now output the DIE to represent the data object itself. This gets
23835 complicated because of the possibility that the VAR_DECL really
23836 represents an inlined instance of a formal parameter for an inline
23837 function. */
23838 ultimate_origin = decl_ultimate_origin (decl_or_origin);
23839 if (ultimate_origin != NULL_TREE
23840 && TREE_CODE (ultimate_origin) == PARM_DECL)
23841 gen_formal_parameter_die (decl, origin,
23842 true /* Emit name attribute. */,
23843 context_die);
23844 else
23845 gen_variable_die (decl, origin, context_die);
23846 break;
23848 case FIELD_DECL:
23849 gcc_assert (ctx != NULL && ctx->struct_type != NULL);
23850 /* Ignore the nameless fields that are used to skip bits but handle C++
23851 anonymous unions and structs. */
23852 if (DECL_NAME (decl) != NULL_TREE
23853 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
23854 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
23856 gen_type_die (member_declared_type (decl), context_die);
23857 gen_field_die (decl, ctx, context_die);
23859 break;
23861 case PARM_DECL:
23862 if (DECL_BY_REFERENCE (decl_or_origin))
23863 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
23864 else
23865 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
23866 return gen_formal_parameter_die (decl, origin,
23867 true /* Emit name attribute. */,
23868 context_die);
23870 case NAMESPACE_DECL:
23871 if (dwarf_version >= 3 || !dwarf_strict)
23872 gen_namespace_die (decl, context_die);
23873 break;
23875 case IMPORTED_DECL:
23876 dwarf2out_imported_module_or_decl_1 (decl, DECL_NAME (decl),
23877 DECL_CONTEXT (decl), context_die);
23878 break;
23880 case NAMELIST_DECL:
23881 gen_namelist_decl (DECL_NAME (decl), context_die,
23882 NAMELIST_DECL_ASSOCIATED_DECL (decl));
23883 break;
23885 default:
23886 /* Probably some frontend-internal decl. Assume we don't care. */
23887 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
23888 break;
23891 return NULL;
23894 /* Output initial debug information for global DECL. Called at the
23895 end of the parsing process.
23897 This is the initial debug generation process. As such, the DIEs
23898 generated may be incomplete. A later debug generation pass
23899 (dwarf2out_late_global_decl) will augment the information generated
23900 in this pass (e.g., with complete location info). */
23902 static void
23903 dwarf2out_early_global_decl (tree decl)
23905 set_early_dwarf s;
23907 /* gen_decl_die() will set DECL_ABSTRACT because
23908 cgraph_function_possibly_inlined_p() returns true. This is in
23909 turn will cause DW_AT_inline attributes to be set.
23911 This happens because at early dwarf generation, there is no
23912 cgraph information, causing cgraph_function_possibly_inlined_p()
23913 to return true. Trick cgraph_function_possibly_inlined_p()
23914 while we generate dwarf early. */
23915 bool save = symtab->global_info_ready;
23916 symtab->global_info_ready = true;
23918 /* We don't handle TYPE_DECLs. If required, they'll be reached via
23919 other DECLs and they can point to template types or other things
23920 that dwarf2out can't handle when done via dwarf2out_decl. */
23921 if (TREE_CODE (decl) != TYPE_DECL
23922 && TREE_CODE (decl) != PARM_DECL)
23924 tree save_fndecl = current_function_decl;
23925 if (TREE_CODE (decl) == FUNCTION_DECL)
23927 /* No cfun means the symbol has no body, so there's nothing
23928 to emit. */
23929 if (!DECL_STRUCT_FUNCTION (decl))
23930 goto early_decl_exit;
23932 /* For nested functions, emit DIEs for the parents first so that all
23933 nested DIEs are generated at the proper scope in the first
23934 shot. */
23935 tree context = decl_function_context (decl);
23936 if (context != NULL)
23938 current_function_decl = context;
23939 dwarf2out_decl (context);
23942 current_function_decl = decl;
23944 dwarf2out_decl (decl);
23945 if (TREE_CODE (decl) == FUNCTION_DECL)
23946 current_function_decl = save_fndecl;
23948 early_decl_exit:
23949 symtab->global_info_ready = save;
23952 /* Output debug information for global decl DECL. Called from
23953 toplev.c after compilation proper has finished. */
23955 static void
23956 dwarf2out_late_global_decl (tree decl)
23958 /* Fill-in any location information we were unable to determine
23959 on the first pass. */
23960 if (VAR_P (decl) && !POINTER_BOUNDS_P (decl))
23962 dw_die_ref die = lookup_decl_die (decl);
23964 /* We have to generate early debug late for LTO. */
23965 if (! die && in_lto_p)
23967 dwarf2out_decl (decl);
23968 die = lookup_decl_die (decl);
23971 if (die)
23973 /* We get called during the early debug phase via the symtab
23974 code invoking late_global_decl for symbols that are optimized
23975 out. When the early phase is not finished, do not add
23976 locations. */
23977 if (! early_dwarf_finished)
23978 tree_add_const_value_attribute_for_decl (die, decl);
23979 else
23980 add_location_or_const_value_attribute (die, decl, false);
23985 /* Output debug information for type decl DECL. Called from toplev.c
23986 and from language front ends (to record built-in types). */
23987 static void
23988 dwarf2out_type_decl (tree decl, int local)
23990 if (!local)
23992 set_early_dwarf s;
23993 dwarf2out_decl (decl);
23997 /* Output debug information for imported module or decl DECL.
23998 NAME is non-NULL name in the lexical block if the decl has been renamed.
23999 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
24000 that DECL belongs to.
24001 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
24002 static void
24003 dwarf2out_imported_module_or_decl_1 (tree decl,
24004 tree name,
24005 tree lexical_block,
24006 dw_die_ref lexical_block_die)
24008 expanded_location xloc;
24009 dw_die_ref imported_die = NULL;
24010 dw_die_ref at_import_die;
24012 if (TREE_CODE (decl) == IMPORTED_DECL)
24014 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
24015 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
24016 gcc_assert (decl);
24018 else
24019 xloc = expand_location (input_location);
24021 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
24023 at_import_die = force_type_die (TREE_TYPE (decl));
24024 /* For namespace N { typedef void T; } using N::T; base_type_die
24025 returns NULL, but DW_TAG_imported_declaration requires
24026 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
24027 if (!at_import_die)
24029 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
24030 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
24031 at_import_die = lookup_type_die (TREE_TYPE (decl));
24032 gcc_assert (at_import_die);
24035 else
24037 at_import_die = lookup_decl_die (decl);
24038 if (!at_import_die)
24040 /* If we're trying to avoid duplicate debug info, we may not have
24041 emitted the member decl for this field. Emit it now. */
24042 if (TREE_CODE (decl) == FIELD_DECL)
24044 tree type = DECL_CONTEXT (decl);
24046 if (TYPE_CONTEXT (type)
24047 && TYPE_P (TYPE_CONTEXT (type))
24048 && !should_emit_struct_debug (TYPE_CONTEXT (type),
24049 DINFO_USAGE_DIR_USE))
24050 return;
24051 gen_type_die_for_member (type, decl,
24052 get_context_die (TYPE_CONTEXT (type)));
24054 if (TREE_CODE (decl) == NAMELIST_DECL)
24055 at_import_die = gen_namelist_decl (DECL_NAME (decl),
24056 get_context_die (DECL_CONTEXT (decl)),
24057 NULL_TREE);
24058 else
24059 at_import_die = force_decl_die (decl);
24063 if (TREE_CODE (decl) == NAMESPACE_DECL)
24065 if (dwarf_version >= 3 || !dwarf_strict)
24066 imported_die = new_die (DW_TAG_imported_module,
24067 lexical_block_die,
24068 lexical_block);
24069 else
24070 return;
24072 else
24073 imported_die = new_die (DW_TAG_imported_declaration,
24074 lexical_block_die,
24075 lexical_block);
24077 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
24078 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
24079 if (name)
24080 add_AT_string (imported_die, DW_AT_name,
24081 IDENTIFIER_POINTER (name));
24082 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
24085 /* Output debug information for imported module or decl DECL.
24086 NAME is non-NULL name in context if the decl has been renamed.
24087 CHILD is true if decl is one of the renamed decls as part of
24088 importing whole module. */
24090 static void
24091 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
24092 bool child)
24094 /* dw_die_ref at_import_die; */
24095 dw_die_ref scope_die;
24097 if (debug_info_level <= DINFO_LEVEL_TERSE)
24098 return;
24100 gcc_assert (decl);
24102 set_early_dwarf s;
24104 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
24105 We need decl DIE for reference and scope die. First, get DIE for the decl
24106 itself. */
24108 /* Get the scope die for decl context. Use comp_unit_die for global module
24109 or decl. If die is not found for non globals, force new die. */
24110 if (context
24111 && TYPE_P (context)
24112 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
24113 return;
24115 scope_die = get_context_die (context);
24117 if (child)
24119 /* DW_TAG_imported_module was introduced in the DWARFv3 specification, so
24120 there is nothing we can do, here. */
24121 if (dwarf_version < 3 && dwarf_strict)
24122 return;
24124 gcc_assert (scope_die->die_child);
24125 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
24126 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
24127 scope_die = scope_die->die_child;
24130 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
24131 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
24134 /* Output debug information for namelists. */
24136 static dw_die_ref
24137 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
24139 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
24140 tree value;
24141 unsigned i;
24143 if (debug_info_level <= DINFO_LEVEL_TERSE)
24144 return NULL;
24146 gcc_assert (scope_die != NULL);
24147 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
24148 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
24150 /* If there are no item_decls, we have a nondefining namelist, e.g.
24151 with USE association; hence, set DW_AT_declaration. */
24152 if (item_decls == NULL_TREE)
24154 add_AT_flag (nml_die, DW_AT_declaration, 1);
24155 return nml_die;
24158 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
24160 nml_item_ref_die = lookup_decl_die (value);
24161 if (!nml_item_ref_die)
24162 nml_item_ref_die = force_decl_die (value);
24164 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
24165 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
24167 return nml_die;
24171 /* Write the debugging output for DECL and return the DIE. */
24173 static void
24174 dwarf2out_decl (tree decl)
24176 dw_die_ref context_die = comp_unit_die ();
24178 switch (TREE_CODE (decl))
24180 case ERROR_MARK:
24181 return;
24183 case FUNCTION_DECL:
24184 /* What we would really like to do here is to filter out all mere
24185 file-scope declarations of file-scope functions which are never
24186 referenced later within this translation unit (and keep all of ones
24187 that *are* referenced later on) but we aren't clairvoyant, so we have
24188 no idea which functions will be referenced in the future (i.e. later
24189 on within the current translation unit). So here we just ignore all
24190 file-scope function declarations which are not also definitions. If
24191 and when the debugger needs to know something about these functions,
24192 it will have to hunt around and find the DWARF information associated
24193 with the definition of the function.
24195 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
24196 nodes represent definitions and which ones represent mere
24197 declarations. We have to check DECL_INITIAL instead. That's because
24198 the C front-end supports some weird semantics for "extern inline"
24199 function definitions. These can get inlined within the current
24200 translation unit (and thus, we need to generate Dwarf info for their
24201 abstract instances so that the Dwarf info for the concrete inlined
24202 instances can have something to refer to) but the compiler never
24203 generates any out-of-lines instances of such things (despite the fact
24204 that they *are* definitions).
24206 The important point is that the C front-end marks these "extern
24207 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
24208 them anyway. Note that the C++ front-end also plays some similar games
24209 for inline function definitions appearing within include files which
24210 also contain `#pragma interface' pragmas.
24212 If we are called from dwarf2out_abstract_function output a DIE
24213 anyway. We can end up here this way with early inlining and LTO
24214 where the inlined function is output in a different LTRANS unit
24215 or not at all. */
24216 if (DECL_INITIAL (decl) == NULL_TREE
24217 && ! DECL_ABSTRACT_P (decl))
24218 return;
24220 /* If we're a nested function, initially use a parent of NULL; if we're
24221 a plain function, this will be fixed up in decls_for_scope. If
24222 we're a method, it will be ignored, since we already have a DIE. */
24223 if (decl_function_context (decl)
24224 /* But if we're in terse mode, we don't care about scope. */
24225 && debug_info_level > DINFO_LEVEL_TERSE)
24226 context_die = NULL;
24227 break;
24229 case VAR_DECL:
24230 /* For local statics lookup proper context die. */
24231 if (local_function_static (decl))
24232 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24234 /* If we are in terse mode, don't generate any DIEs to represent any
24235 variable declarations or definitions. */
24236 if (debug_info_level <= DINFO_LEVEL_TERSE)
24237 return;
24238 break;
24240 case CONST_DECL:
24241 if (debug_info_level <= DINFO_LEVEL_TERSE)
24242 return;
24243 if (!is_fortran () && !is_ada ())
24244 return;
24245 if (TREE_STATIC (decl) && decl_function_context (decl))
24246 context_die = lookup_decl_die (DECL_CONTEXT (decl));
24247 break;
24249 case NAMESPACE_DECL:
24250 case IMPORTED_DECL:
24251 if (debug_info_level <= DINFO_LEVEL_TERSE)
24252 return;
24253 if (lookup_decl_die (decl) != NULL)
24254 return;
24255 break;
24257 case TYPE_DECL:
24258 /* Don't emit stubs for types unless they are needed by other DIEs. */
24259 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
24260 return;
24262 /* Don't bother trying to generate any DIEs to represent any of the
24263 normal built-in types for the language we are compiling. */
24264 if (DECL_IS_BUILTIN (decl))
24265 return;
24267 /* If we are in terse mode, don't generate any DIEs for types. */
24268 if (debug_info_level <= DINFO_LEVEL_TERSE)
24269 return;
24271 /* If we're a function-scope tag, initially use a parent of NULL;
24272 this will be fixed up in decls_for_scope. */
24273 if (decl_function_context (decl))
24274 context_die = NULL;
24276 break;
24278 case NAMELIST_DECL:
24279 break;
24281 default:
24282 return;
24285 gen_decl_die (decl, NULL, NULL, context_die);
24287 if (flag_checking)
24289 dw_die_ref die = lookup_decl_die (decl);
24290 if (die)
24291 check_die (die);
24295 /* Write the debugging output for DECL. */
24297 static void
24298 dwarf2out_function_decl (tree decl)
24300 dwarf2out_decl (decl);
24301 call_arg_locations = NULL;
24302 call_arg_loc_last = NULL;
24303 call_site_count = -1;
24304 tail_call_site_count = -1;
24305 decl_loc_table->empty ();
24306 cached_dw_loc_list_table->empty ();
24309 /* Output a marker (i.e. a label) for the beginning of the generated code for
24310 a lexical block. */
24312 static void
24313 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
24314 unsigned int blocknum)
24316 switch_to_section (current_function_section ());
24317 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
24320 /* Output a marker (i.e. a label) for the end of the generated code for a
24321 lexical block. */
24323 static void
24324 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
24326 switch_to_section (current_function_section ());
24327 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
24330 /* Returns nonzero if it is appropriate not to emit any debugging
24331 information for BLOCK, because it doesn't contain any instructions.
24333 Don't allow this for blocks with nested functions or local classes
24334 as we would end up with orphans, and in the presence of scheduling
24335 we may end up calling them anyway. */
24337 static bool
24338 dwarf2out_ignore_block (const_tree block)
24340 tree decl;
24341 unsigned int i;
24343 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
24344 if (TREE_CODE (decl) == FUNCTION_DECL
24345 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24346 return 0;
24347 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
24349 decl = BLOCK_NONLOCALIZED_VAR (block, i);
24350 if (TREE_CODE (decl) == FUNCTION_DECL
24351 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
24352 return 0;
24355 return 1;
24358 /* Hash table routines for file_hash. */
24360 bool
24361 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
24363 return filename_cmp (p1->filename, p2) == 0;
24366 hashval_t
24367 dwarf_file_hasher::hash (dwarf_file_data *p)
24369 return htab_hash_string (p->filename);
24372 /* Lookup FILE_NAME (in the list of filenames that we know about here in
24373 dwarf2out.c) and return its "index". The index of each (known) filename is
24374 just a unique number which is associated with only that one filename. We
24375 need such numbers for the sake of generating labels (in the .debug_sfnames
24376 section) and references to those files numbers (in the .debug_srcinfo
24377 and .debug_macinfo sections). If the filename given as an argument is not
24378 found in our current list, add it to the list and assign it the next
24379 available unique index number. */
24381 static struct dwarf_file_data *
24382 lookup_filename (const char *file_name)
24384 struct dwarf_file_data * created;
24386 if (!file_name)
24387 return NULL;
24389 dwarf_file_data **slot
24390 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
24391 INSERT);
24392 if (*slot)
24393 return *slot;
24395 created = ggc_alloc<dwarf_file_data> ();
24396 created->filename = file_name;
24397 created->emitted_number = 0;
24398 *slot = created;
24399 return created;
24402 /* If the assembler will construct the file table, then translate the compiler
24403 internal file table number into the assembler file table number, and emit
24404 a .file directive if we haven't already emitted one yet. The file table
24405 numbers are different because we prune debug info for unused variables and
24406 types, which may include filenames. */
24408 static int
24409 maybe_emit_file (struct dwarf_file_data * fd)
24411 if (! fd->emitted_number)
24413 if (last_emitted_file)
24414 fd->emitted_number = last_emitted_file->emitted_number + 1;
24415 else
24416 fd->emitted_number = 1;
24417 last_emitted_file = fd;
24419 if (DWARF2_ASM_LINE_DEBUG_INFO)
24421 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
24422 output_quoted_string (asm_out_file,
24423 remap_debug_filename (fd->filename));
24424 fputc ('\n', asm_out_file);
24428 return fd->emitted_number;
24431 /* Schedule generation of a DW_AT_const_value attribute to DIE.
24432 That generation should happen after function debug info has been
24433 generated. The value of the attribute is the constant value of ARG. */
24435 static void
24436 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
24438 die_arg_entry entry;
24440 if (!die || !arg)
24441 return;
24443 gcc_assert (early_dwarf);
24445 if (!tmpl_value_parm_die_table)
24446 vec_alloc (tmpl_value_parm_die_table, 32);
24448 entry.die = die;
24449 entry.arg = arg;
24450 vec_safe_push (tmpl_value_parm_die_table, entry);
24453 /* Return TRUE if T is an instance of generic type, FALSE
24454 otherwise. */
24456 static bool
24457 generic_type_p (tree t)
24459 if (t == NULL_TREE || !TYPE_P (t))
24460 return false;
24461 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
24464 /* Schedule the generation of the generic parameter dies for the
24465 instance of generic type T. The proper generation itself is later
24466 done by gen_scheduled_generic_parms_dies. */
24468 static void
24469 schedule_generic_params_dies_gen (tree t)
24471 if (!generic_type_p (t))
24472 return;
24474 gcc_assert (early_dwarf);
24476 if (!generic_type_instances)
24477 vec_alloc (generic_type_instances, 256);
24479 vec_safe_push (generic_type_instances, t);
24482 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
24483 by append_entry_to_tmpl_value_parm_die_table. This function must
24484 be called after function DIEs have been generated. */
24486 static void
24487 gen_remaining_tmpl_value_param_die_attribute (void)
24489 if (tmpl_value_parm_die_table)
24491 unsigned i, j;
24492 die_arg_entry *e;
24494 /* We do this in two phases - first get the cases we can
24495 handle during early-finish, preserving those we cannot
24496 (containing symbolic constants where we don't yet know
24497 whether we are going to output the referenced symbols).
24498 For those we try again at late-finish. */
24499 j = 0;
24500 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
24502 if (!tree_add_const_value_attribute (e->die, e->arg))
24504 dw_loc_descr_ref loc = NULL;
24505 if (! early_dwarf
24506 && (dwarf_version >= 5 || !dwarf_strict))
24507 loc = loc_descriptor_from_tree (e->arg, 2, NULL);
24508 if (loc)
24509 add_AT_loc (e->die, DW_AT_location, loc);
24510 else
24511 (*tmpl_value_parm_die_table)[j++] = *e;
24514 tmpl_value_parm_die_table->truncate (j);
24518 /* Generate generic parameters DIEs for instances of generic types
24519 that have been previously scheduled by
24520 schedule_generic_params_dies_gen. This function must be called
24521 after all the types of the CU have been laid out. */
24523 static void
24524 gen_scheduled_generic_parms_dies (void)
24526 unsigned i;
24527 tree t;
24529 if (!generic_type_instances)
24530 return;
24532 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
24533 if (COMPLETE_TYPE_P (t))
24534 gen_generic_params_dies (t);
24536 generic_type_instances = NULL;
24540 /* Replace DW_AT_name for the decl with name. */
24542 static void
24543 dwarf2out_set_name (tree decl, tree name)
24545 dw_die_ref die;
24546 dw_attr_node *attr;
24547 const char *dname;
24549 die = TYPE_SYMTAB_DIE (decl);
24550 if (!die)
24551 return;
24553 dname = dwarf2_name (name, 0);
24554 if (!dname)
24555 return;
24557 attr = get_AT (die, DW_AT_name);
24558 if (attr)
24560 struct indirect_string_node *node;
24562 node = find_AT_string (dname);
24563 /* replace the string. */
24564 attr->dw_attr_val.v.val_str = node;
24567 else
24568 add_name_attribute (die, dname);
24571 /* True if before or during processing of the first function being emitted. */
24572 static bool in_first_function_p = true;
24573 /* True if loc_note during dwarf2out_var_location call might still be
24574 before first real instruction at address equal to .Ltext0. */
24575 static bool maybe_at_text_label_p = true;
24576 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
24577 static unsigned int first_loclabel_num_not_at_text_label;
24579 /* Called by the final INSN scan whenever we see a var location. We
24580 use it to drop labels in the right places, and throw the location in
24581 our lookup table. */
24583 static void
24584 dwarf2out_var_location (rtx_insn *loc_note)
24586 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
24587 struct var_loc_node *newloc;
24588 rtx_insn *next_real, *next_note;
24589 rtx_insn *call_insn = NULL;
24590 static const char *last_label;
24591 static const char *last_postcall_label;
24592 static bool last_in_cold_section_p;
24593 static rtx_insn *expected_next_loc_note;
24594 tree decl;
24595 bool var_loc_p;
24597 if (!NOTE_P (loc_note))
24599 if (CALL_P (loc_note))
24601 call_site_count++;
24602 if (SIBLING_CALL_P (loc_note))
24603 tail_call_site_count++;
24604 if (optimize == 0 && !flag_var_tracking)
24606 /* When the var-tracking pass is not running, there is no note
24607 for indirect calls whose target is compile-time known. In this
24608 case, process such calls specifically so that we generate call
24609 sites for them anyway. */
24610 rtx x = PATTERN (loc_note);
24611 if (GET_CODE (x) == PARALLEL)
24612 x = XVECEXP (x, 0, 0);
24613 if (GET_CODE (x) == SET)
24614 x = SET_SRC (x);
24615 if (GET_CODE (x) == CALL)
24616 x = XEXP (x, 0);
24617 if (!MEM_P (x)
24618 || GET_CODE (XEXP (x, 0)) != SYMBOL_REF
24619 || !SYMBOL_REF_DECL (XEXP (x, 0))
24620 || (TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0)))
24621 != FUNCTION_DECL))
24623 call_insn = loc_note;
24624 loc_note = NULL;
24625 var_loc_p = false;
24627 next_real = next_real_insn (call_insn);
24628 next_note = NULL;
24629 cached_next_real_insn = NULL;
24630 goto create_label;
24634 return;
24637 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
24638 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
24639 return;
24641 /* Optimize processing a large consecutive sequence of location
24642 notes so we don't spend too much time in next_real_insn. If the
24643 next insn is another location note, remember the next_real_insn
24644 calculation for next time. */
24645 next_real = cached_next_real_insn;
24646 if (next_real)
24648 if (expected_next_loc_note != loc_note)
24649 next_real = NULL;
24652 next_note = NEXT_INSN (loc_note);
24653 if (! next_note
24654 || next_note->deleted ()
24655 || ! NOTE_P (next_note)
24656 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
24657 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
24658 next_note = NULL;
24660 if (! next_real)
24661 next_real = next_real_insn (loc_note);
24663 if (next_note)
24665 expected_next_loc_note = next_note;
24666 cached_next_real_insn = next_real;
24668 else
24669 cached_next_real_insn = NULL;
24671 /* If there are no instructions which would be affected by this note,
24672 don't do anything. */
24673 if (var_loc_p
24674 && next_real == NULL_RTX
24675 && !NOTE_DURING_CALL_P (loc_note))
24676 return;
24678 create_label:
24680 if (next_real == NULL_RTX)
24681 next_real = get_last_insn ();
24683 /* If there were any real insns between note we processed last time
24684 and this note (or if it is the first note), clear
24685 last_{,postcall_}label so that they are not reused this time. */
24686 if (last_var_location_insn == NULL_RTX
24687 || last_var_location_insn != next_real
24688 || last_in_cold_section_p != in_cold_section_p)
24690 last_label = NULL;
24691 last_postcall_label = NULL;
24694 if (var_loc_p)
24696 decl = NOTE_VAR_LOCATION_DECL (loc_note);
24697 newloc = add_var_loc_to_decl (decl, loc_note,
24698 NOTE_DURING_CALL_P (loc_note)
24699 ? last_postcall_label : last_label);
24700 if (newloc == NULL)
24701 return;
24703 else
24705 decl = NULL_TREE;
24706 newloc = NULL;
24709 /* If there were no real insns between note we processed last time
24710 and this note, use the label we emitted last time. Otherwise
24711 create a new label and emit it. */
24712 if (last_label == NULL)
24714 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
24715 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
24716 loclabel_num++;
24717 last_label = ggc_strdup (loclabel);
24718 /* See if loclabel might be equal to .Ltext0. If yes,
24719 bump first_loclabel_num_not_at_text_label. */
24720 if (!have_multiple_function_sections
24721 && in_first_function_p
24722 && maybe_at_text_label_p)
24724 static rtx_insn *last_start;
24725 rtx_insn *insn;
24726 for (insn = loc_note; insn; insn = previous_insn (insn))
24727 if (insn == last_start)
24728 break;
24729 else if (!NONDEBUG_INSN_P (insn))
24730 continue;
24731 else
24733 rtx body = PATTERN (insn);
24734 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
24735 continue;
24736 /* Inline asm could occupy zero bytes. */
24737 else if (GET_CODE (body) == ASM_INPUT
24738 || asm_noperands (body) >= 0)
24739 continue;
24740 #ifdef HAVE_attr_length
24741 else if (get_attr_min_length (insn) == 0)
24742 continue;
24743 #endif
24744 else
24746 /* Assume insn has non-zero length. */
24747 maybe_at_text_label_p = false;
24748 break;
24751 if (maybe_at_text_label_p)
24753 last_start = loc_note;
24754 first_loclabel_num_not_at_text_label = loclabel_num;
24759 gcc_assert ((loc_note == NULL_RTX && call_insn != NULL_RTX)
24760 || (loc_note != NULL_RTX && call_insn == NULL_RTX));
24762 if (!var_loc_p)
24764 struct call_arg_loc_node *ca_loc
24765 = ggc_cleared_alloc<call_arg_loc_node> ();
24766 rtx_insn *prev
24767 = loc_note != NULL_RTX ? prev_real_insn (loc_note) : call_insn;
24769 ca_loc->call_arg_loc_note = loc_note;
24770 ca_loc->next = NULL;
24771 ca_loc->label = last_label;
24772 gcc_assert (prev
24773 && (CALL_P (prev)
24774 || (NONJUMP_INSN_P (prev)
24775 && GET_CODE (PATTERN (prev)) == SEQUENCE
24776 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
24777 if (!CALL_P (prev))
24778 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
24779 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
24781 /* Look for a SYMBOL_REF in the "prev" instruction. */
24782 rtx x = get_call_rtx_from (PATTERN (prev));
24783 if (x)
24785 /* Try to get the call symbol, if any. */
24786 if (MEM_P (XEXP (x, 0)))
24787 x = XEXP (x, 0);
24788 /* First, look for a memory access to a symbol_ref. */
24789 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
24790 && SYMBOL_REF_DECL (XEXP (x, 0))
24791 && TREE_CODE (SYMBOL_REF_DECL (XEXP (x, 0))) == FUNCTION_DECL)
24792 ca_loc->symbol_ref = XEXP (x, 0);
24793 /* Otherwise, look at a compile-time known user-level function
24794 declaration. */
24795 else if (MEM_P (x)
24796 && MEM_EXPR (x)
24797 && TREE_CODE (MEM_EXPR (x)) == FUNCTION_DECL)
24798 ca_loc->symbol_ref = XEXP (DECL_RTL (MEM_EXPR (x)), 0);
24801 ca_loc->block = insn_scope (prev);
24802 if (call_arg_locations)
24803 call_arg_loc_last->next = ca_loc;
24804 else
24805 call_arg_locations = ca_loc;
24806 call_arg_loc_last = ca_loc;
24808 else if (loc_note != NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
24809 newloc->label = last_label;
24810 else
24812 if (!last_postcall_label)
24814 sprintf (loclabel, "%s-1", last_label);
24815 last_postcall_label = ggc_strdup (loclabel);
24817 newloc->label = last_postcall_label;
24820 last_var_location_insn = next_real;
24821 last_in_cold_section_p = in_cold_section_p;
24824 /* Called from finalize_size_functions for size functions so that their body
24825 can be encoded in the debug info to describe the layout of variable-length
24826 structures. */
24828 static void
24829 dwarf2out_size_function (tree decl)
24831 function_to_dwarf_procedure (decl);
24834 /* Note in one location list that text section has changed. */
24837 var_location_switch_text_section_1 (var_loc_list **slot, void *)
24839 var_loc_list *list = *slot;
24840 if (list->first)
24841 list->last_before_switch
24842 = list->last->next ? list->last->next : list->last;
24843 return 1;
24846 /* Note in all location lists that text section has changed. */
24848 static void
24849 var_location_switch_text_section (void)
24851 if (decl_loc_table == NULL)
24852 return;
24854 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
24857 /* Create a new line number table. */
24859 static dw_line_info_table *
24860 new_line_info_table (void)
24862 dw_line_info_table *table;
24864 table = ggc_cleared_alloc<dw_line_info_table> ();
24865 table->file_num = 1;
24866 table->line_num = 1;
24867 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
24869 return table;
24872 /* Lookup the "current" table into which we emit line info, so
24873 that we don't have to do it for every source line. */
24875 static void
24876 set_cur_line_info_table (section *sec)
24878 dw_line_info_table *table;
24880 if (sec == text_section)
24881 table = text_section_line_info;
24882 else if (sec == cold_text_section)
24884 table = cold_text_section_line_info;
24885 if (!table)
24887 cold_text_section_line_info = table = new_line_info_table ();
24888 table->end_label = cold_end_label;
24891 else
24893 const char *end_label;
24895 if (flag_reorder_blocks_and_partition)
24897 if (in_cold_section_p)
24898 end_label = crtl->subsections.cold_section_end_label;
24899 else
24900 end_label = crtl->subsections.hot_section_end_label;
24902 else
24904 char label[MAX_ARTIFICIAL_LABEL_BYTES];
24905 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
24906 current_function_funcdef_no);
24907 end_label = ggc_strdup (label);
24910 table = new_line_info_table ();
24911 table->end_label = end_label;
24913 vec_safe_push (separate_line_info, table);
24916 if (DWARF2_ASM_LINE_DEBUG_INFO)
24917 table->is_stmt = (cur_line_info_table
24918 ? cur_line_info_table->is_stmt
24919 : DWARF_LINE_DEFAULT_IS_STMT_START);
24920 cur_line_info_table = table;
24924 /* We need to reset the locations at the beginning of each
24925 function. We can't do this in the end_function hook, because the
24926 declarations that use the locations won't have been output when
24927 that hook is called. Also compute have_multiple_function_sections here. */
24929 static void
24930 dwarf2out_begin_function (tree fun)
24932 section *sec = function_section (fun);
24934 if (sec != text_section)
24935 have_multiple_function_sections = true;
24937 if (flag_reorder_blocks_and_partition && !cold_text_section)
24939 gcc_assert (current_function_decl == fun);
24940 cold_text_section = unlikely_text_section ();
24941 switch_to_section (cold_text_section);
24942 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
24943 switch_to_section (sec);
24946 dwarf2out_note_section_used ();
24947 call_site_count = 0;
24948 tail_call_site_count = 0;
24950 set_cur_line_info_table (sec);
24953 /* Helper function of dwarf2out_end_function, called only after emitting
24954 the very first function into assembly. Check if some .debug_loc range
24955 might end with a .LVL* label that could be equal to .Ltext0.
24956 In that case we must force using absolute addresses in .debug_loc ranges,
24957 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
24958 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
24959 list terminator.
24960 Set have_multiple_function_sections to true in that case and
24961 terminate htab traversal. */
24964 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
24966 var_loc_list *entry = *slot;
24967 struct var_loc_node *node;
24969 node = entry->first;
24970 if (node && node->next && node->next->label)
24972 unsigned int i;
24973 const char *label = node->next->label;
24974 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
24976 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
24978 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
24979 if (strcmp (label, loclabel) == 0)
24981 have_multiple_function_sections = true;
24982 return 0;
24986 return 1;
24989 /* Hook called after emitting a function into assembly.
24990 This does something only for the very first function emitted. */
24992 static void
24993 dwarf2out_end_function (unsigned int)
24995 if (in_first_function_p
24996 && !have_multiple_function_sections
24997 && first_loclabel_num_not_at_text_label
24998 && decl_loc_table)
24999 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
25000 in_first_function_p = false;
25001 maybe_at_text_label_p = false;
25004 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
25005 front-ends register a translation unit even before dwarf2out_init is
25006 called. */
25007 static tree main_translation_unit = NULL_TREE;
25009 /* Hook called by front-ends after they built their main translation unit.
25010 Associate comp_unit_die to UNIT. */
25012 static void
25013 dwarf2out_register_main_translation_unit (tree unit)
25015 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
25016 && main_translation_unit == NULL_TREE);
25017 main_translation_unit = unit;
25018 /* If dwarf2out_init has not been called yet, it will perform the association
25019 itself looking at main_translation_unit. */
25020 if (decl_die_table != NULL)
25021 equate_decl_number_to_die (unit, comp_unit_die ());
25024 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
25026 static void
25027 push_dw_line_info_entry (dw_line_info_table *table,
25028 enum dw_line_info_opcode opcode, unsigned int val)
25030 dw_line_info_entry e;
25031 e.opcode = opcode;
25032 e.val = val;
25033 vec_safe_push (table->entries, e);
25036 /* Output a label to mark the beginning of a source code line entry
25037 and record information relating to this source line, in
25038 'line_info_table' for later output of the .debug_line section. */
25039 /* ??? The discriminator parameter ought to be unsigned. */
25041 static void
25042 dwarf2out_source_line (unsigned int line, const char *filename,
25043 int discriminator, bool is_stmt)
25045 unsigned int file_num;
25046 dw_line_info_table *table;
25048 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
25049 return;
25051 /* The discriminator column was added in dwarf4. Simplify the below
25052 by simply removing it if we're not supposed to output it. */
25053 if (dwarf_version < 4 && dwarf_strict)
25054 discriminator = 0;
25056 table = cur_line_info_table;
25057 file_num = maybe_emit_file (lookup_filename (filename));
25059 /* ??? TODO: Elide duplicate line number entries. Traditionally,
25060 the debugger has used the second (possibly duplicate) line number
25061 at the beginning of the function to mark the end of the prologue.
25062 We could eliminate any other duplicates within the function. For
25063 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
25064 that second line number entry. */
25065 /* Recall that this end-of-prologue indication is *not* the same thing
25066 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
25067 to which the hook corresponds, follows the last insn that was
25068 emitted by gen_prologue. What we need is to precede the first insn
25069 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
25070 insn that corresponds to something the user wrote. These may be
25071 very different locations once scheduling is enabled. */
25073 if (0 && file_num == table->file_num
25074 && line == table->line_num
25075 && discriminator == table->discrim_num
25076 && is_stmt == table->is_stmt)
25077 return;
25079 switch_to_section (current_function_section ());
25081 /* If requested, emit something human-readable. */
25082 if (flag_debug_asm)
25083 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
25085 if (DWARF2_ASM_LINE_DEBUG_INFO)
25087 /* Emit the .loc directive understood by GNU as. */
25088 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
25089 file_num, line, is_stmt, discriminator */
25090 fputs ("\t.loc ", asm_out_file);
25091 fprint_ul (asm_out_file, file_num);
25092 putc (' ', asm_out_file);
25093 fprint_ul (asm_out_file, line);
25094 putc (' ', asm_out_file);
25095 putc ('0', asm_out_file);
25097 if (is_stmt != table->is_stmt)
25099 fputs (" is_stmt ", asm_out_file);
25100 putc (is_stmt ? '1' : '0', asm_out_file);
25102 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
25104 gcc_assert (discriminator > 0);
25105 fputs (" discriminator ", asm_out_file);
25106 fprint_ul (asm_out_file, (unsigned long) discriminator);
25108 putc ('\n', asm_out_file);
25110 else
25112 unsigned int label_num = ++line_info_label_num;
25114 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
25116 push_dw_line_info_entry (table, LI_set_address, label_num);
25117 if (file_num != table->file_num)
25118 push_dw_line_info_entry (table, LI_set_file, file_num);
25119 if (discriminator != table->discrim_num)
25120 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
25121 if (is_stmt != table->is_stmt)
25122 push_dw_line_info_entry (table, LI_negate_stmt, 0);
25123 push_dw_line_info_entry (table, LI_set_line, line);
25126 table->file_num = file_num;
25127 table->line_num = line;
25128 table->discrim_num = discriminator;
25129 table->is_stmt = is_stmt;
25130 table->in_use = true;
25133 /* Record the beginning of a new source file. */
25135 static void
25136 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
25138 if (flag_eliminate_dwarf2_dups)
25140 /* Record the beginning of the file for break_out_includes. */
25141 dw_die_ref bincl_die;
25143 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
25144 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
25147 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25149 macinfo_entry e;
25150 e.code = DW_MACINFO_start_file;
25151 e.lineno = lineno;
25152 e.info = ggc_strdup (filename);
25153 vec_safe_push (macinfo_table, e);
25157 /* Record the end of a source file. */
25159 static void
25160 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
25162 if (flag_eliminate_dwarf2_dups)
25163 /* Record the end of the file for break_out_includes. */
25164 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
25166 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25168 macinfo_entry e;
25169 e.code = DW_MACINFO_end_file;
25170 e.lineno = lineno;
25171 e.info = NULL;
25172 vec_safe_push (macinfo_table, e);
25176 /* Called from debug_define in toplev.c. The `buffer' parameter contains
25177 the tail part of the directive line, i.e. the part which is past the
25178 initial whitespace, #, whitespace, directive-name, whitespace part. */
25180 static void
25181 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
25182 const char *buffer ATTRIBUTE_UNUSED)
25184 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25186 macinfo_entry e;
25187 /* Insert a dummy first entry to be able to optimize the whole
25188 predefined macro block using DW_MACRO_GNU_transparent_include. */
25189 if (macinfo_table->is_empty () && lineno <= 1)
25191 e.code = 0;
25192 e.lineno = 0;
25193 e.info = NULL;
25194 vec_safe_push (macinfo_table, e);
25196 e.code = DW_MACINFO_define;
25197 e.lineno = lineno;
25198 e.info = ggc_strdup (buffer);
25199 vec_safe_push (macinfo_table, e);
25203 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
25204 the tail part of the directive line, i.e. the part which is past the
25205 initial whitespace, #, whitespace, directive-name, whitespace part. */
25207 static void
25208 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
25209 const char *buffer ATTRIBUTE_UNUSED)
25211 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25213 macinfo_entry e;
25214 /* Insert a dummy first entry to be able to optimize the whole
25215 predefined macro block using DW_MACRO_GNU_transparent_include. */
25216 if (macinfo_table->is_empty () && lineno <= 1)
25218 e.code = 0;
25219 e.lineno = 0;
25220 e.info = NULL;
25221 vec_safe_push (macinfo_table, e);
25223 e.code = DW_MACINFO_undef;
25224 e.lineno = lineno;
25225 e.info = ggc_strdup (buffer);
25226 vec_safe_push (macinfo_table, e);
25230 /* Helpers to manipulate hash table of CUs. */
25232 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
25234 static inline hashval_t hash (const macinfo_entry *);
25235 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
25238 inline hashval_t
25239 macinfo_entry_hasher::hash (const macinfo_entry *entry)
25241 return htab_hash_string (entry->info);
25244 inline bool
25245 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
25246 const macinfo_entry *entry2)
25248 return !strcmp (entry1->info, entry2->info);
25251 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
25253 /* Output a single .debug_macinfo entry. */
25255 static void
25256 output_macinfo_op (macinfo_entry *ref)
25258 int file_num;
25259 size_t len;
25260 struct indirect_string_node *node;
25261 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25262 struct dwarf_file_data *fd;
25264 switch (ref->code)
25266 case DW_MACINFO_start_file:
25267 fd = lookup_filename (ref->info);
25268 file_num = maybe_emit_file (fd);
25269 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
25270 dw2_asm_output_data_uleb128 (ref->lineno,
25271 "Included from line number %lu",
25272 (unsigned long) ref->lineno);
25273 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
25274 break;
25275 case DW_MACINFO_end_file:
25276 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
25277 break;
25278 case DW_MACINFO_define:
25279 case DW_MACINFO_undef:
25280 len = strlen (ref->info) + 1;
25281 if (!dwarf_strict
25282 && len > DWARF_OFFSET_SIZE
25283 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25284 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25286 ref->code = ref->code == DW_MACINFO_define
25287 ? DW_MACRO_GNU_define_indirect
25288 : DW_MACRO_GNU_undef_indirect;
25289 output_macinfo_op (ref);
25290 return;
25292 dw2_asm_output_data (1, ref->code,
25293 ref->code == DW_MACINFO_define
25294 ? "Define macro" : "Undefine macro");
25295 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25296 (unsigned long) ref->lineno);
25297 dw2_asm_output_nstring (ref->info, -1, "The macro");
25298 break;
25299 case DW_MACRO_GNU_define_indirect:
25300 case DW_MACRO_GNU_undef_indirect:
25301 node = find_AT_string (ref->info);
25302 gcc_assert (node
25303 && ((node->form == DW_FORM_strp)
25304 || (node->form == DW_FORM_GNU_str_index)));
25305 dw2_asm_output_data (1, ref->code,
25306 ref->code == DW_MACRO_GNU_define_indirect
25307 ? "Define macro indirect"
25308 : "Undefine macro indirect");
25309 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
25310 (unsigned long) ref->lineno);
25311 if (node->form == DW_FORM_strp)
25312 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
25313 debug_str_section, "The macro: \"%s\"",
25314 ref->info);
25315 else
25316 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
25317 ref->info);
25318 break;
25319 case DW_MACRO_GNU_transparent_include:
25320 dw2_asm_output_data (1, ref->code, "Transparent include");
25321 ASM_GENERATE_INTERNAL_LABEL (label,
25322 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
25323 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
25324 break;
25325 default:
25326 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
25327 ASM_COMMENT_START, (unsigned long) ref->code);
25328 break;
25332 /* Attempt to make a sequence of define/undef macinfo ops shareable with
25333 other compilation unit .debug_macinfo sections. IDX is the first
25334 index of a define/undef, return the number of ops that should be
25335 emitted in a comdat .debug_macinfo section and emit
25336 a DW_MACRO_GNU_transparent_include entry referencing it.
25337 If the define/undef entry should be emitted normally, return 0. */
25339 static unsigned
25340 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
25341 macinfo_hash_type **macinfo_htab)
25343 macinfo_entry *first, *second, *cur, *inc;
25344 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
25345 unsigned char checksum[16];
25346 struct md5_ctx ctx;
25347 char *grp_name, *tail;
25348 const char *base;
25349 unsigned int i, count, encoded_filename_len, linebuf_len;
25350 macinfo_entry **slot;
25352 first = &(*macinfo_table)[idx];
25353 second = &(*macinfo_table)[idx + 1];
25355 /* Optimize only if there are at least two consecutive define/undef ops,
25356 and either all of them are before first DW_MACINFO_start_file
25357 with lineno {0,1} (i.e. predefined macro block), or all of them are
25358 in some included header file. */
25359 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
25360 return 0;
25361 if (vec_safe_is_empty (files))
25363 if (first->lineno > 1 || second->lineno > 1)
25364 return 0;
25366 else if (first->lineno == 0)
25367 return 0;
25369 /* Find the last define/undef entry that can be grouped together
25370 with first and at the same time compute md5 checksum of their
25371 codes, linenumbers and strings. */
25372 md5_init_ctx (&ctx);
25373 for (i = idx; macinfo_table->iterate (i, &cur); i++)
25374 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
25375 break;
25376 else if (vec_safe_is_empty (files) && cur->lineno > 1)
25377 break;
25378 else
25380 unsigned char code = cur->code;
25381 md5_process_bytes (&code, 1, &ctx);
25382 checksum_uleb128 (cur->lineno, &ctx);
25383 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
25385 md5_finish_ctx (&ctx, checksum);
25386 count = i - idx;
25388 /* From the containing include filename (if any) pick up just
25389 usable characters from its basename. */
25390 if (vec_safe_is_empty (files))
25391 base = "";
25392 else
25393 base = lbasename (files->last ().info);
25394 for (encoded_filename_len = 0, i = 0; base[i]; i++)
25395 if (ISIDNUM (base[i]) || base[i] == '.')
25396 encoded_filename_len++;
25397 /* Count . at the end. */
25398 if (encoded_filename_len)
25399 encoded_filename_len++;
25401 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
25402 linebuf_len = strlen (linebuf);
25404 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
25405 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
25406 + 16 * 2 + 1);
25407 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
25408 tail = grp_name + 4;
25409 if (encoded_filename_len)
25411 for (i = 0; base[i]; i++)
25412 if (ISIDNUM (base[i]) || base[i] == '.')
25413 *tail++ = base[i];
25414 *tail++ = '.';
25416 memcpy (tail, linebuf, linebuf_len);
25417 tail += linebuf_len;
25418 *tail++ = '.';
25419 for (i = 0; i < 16; i++)
25420 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
25422 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
25423 in the empty vector entry before the first define/undef. */
25424 inc = &(*macinfo_table)[idx - 1];
25425 inc->code = DW_MACRO_GNU_transparent_include;
25426 inc->lineno = 0;
25427 inc->info = ggc_strdup (grp_name);
25428 if (!*macinfo_htab)
25429 *macinfo_htab = new macinfo_hash_type (10);
25430 /* Avoid emitting duplicates. */
25431 slot = (*macinfo_htab)->find_slot (inc, INSERT);
25432 if (*slot != NULL)
25434 inc->code = 0;
25435 inc->info = NULL;
25436 /* If such an entry has been used before, just emit
25437 a DW_MACRO_GNU_transparent_include op. */
25438 inc = *slot;
25439 output_macinfo_op (inc);
25440 /* And clear all macinfo_entry in the range to avoid emitting them
25441 in the second pass. */
25442 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
25444 cur->code = 0;
25445 cur->info = NULL;
25448 else
25450 *slot = inc;
25451 inc->lineno = (*macinfo_htab)->elements ();
25452 output_macinfo_op (inc);
25454 return count;
25457 /* Save any strings needed by the macinfo table in the debug str
25458 table. All strings must be collected into the table by the time
25459 index_string is called. */
25461 static void
25462 save_macinfo_strings (void)
25464 unsigned len;
25465 unsigned i;
25466 macinfo_entry *ref;
25468 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
25470 switch (ref->code)
25472 /* Match the logic in output_macinfo_op to decide on
25473 indirect strings. */
25474 case DW_MACINFO_define:
25475 case DW_MACINFO_undef:
25476 len = strlen (ref->info) + 1;
25477 if (!dwarf_strict
25478 && len > DWARF_OFFSET_SIZE
25479 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
25480 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
25481 set_indirect_string (find_AT_string (ref->info));
25482 break;
25483 case DW_MACRO_GNU_define_indirect:
25484 case DW_MACRO_GNU_undef_indirect:
25485 set_indirect_string (find_AT_string (ref->info));
25486 break;
25487 default:
25488 break;
25493 /* Output macinfo section(s). */
25495 static void
25496 output_macinfo (void)
25498 unsigned i;
25499 unsigned long length = vec_safe_length (macinfo_table);
25500 macinfo_entry *ref;
25501 vec<macinfo_entry, va_gc> *files = NULL;
25502 macinfo_hash_type *macinfo_htab = NULL;
25504 if (! length)
25505 return;
25507 /* output_macinfo* uses these interchangeably. */
25508 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
25509 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
25510 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
25511 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
25513 /* For .debug_macro emit the section header. */
25514 if (!dwarf_strict)
25516 dw2_asm_output_data (2, 4, "DWARF macro version number");
25517 if (DWARF_OFFSET_SIZE == 8)
25518 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
25519 else
25520 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
25521 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
25522 (!dwarf_split_debug_info ? debug_line_section_label
25523 : debug_skeleton_line_section_label),
25524 debug_line_section, NULL);
25527 /* In the first loop, it emits the primary .debug_macinfo section
25528 and after each emitted op the macinfo_entry is cleared.
25529 If a longer range of define/undef ops can be optimized using
25530 DW_MACRO_GNU_transparent_include, the
25531 DW_MACRO_GNU_transparent_include op is emitted and kept in
25532 the vector before the first define/undef in the range and the
25533 whole range of define/undef ops is not emitted and kept. */
25534 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25536 switch (ref->code)
25538 case DW_MACINFO_start_file:
25539 vec_safe_push (files, *ref);
25540 break;
25541 case DW_MACINFO_end_file:
25542 if (!vec_safe_is_empty (files))
25543 files->pop ();
25544 break;
25545 case DW_MACINFO_define:
25546 case DW_MACINFO_undef:
25547 if (!dwarf_strict
25548 && HAVE_COMDAT_GROUP
25549 && vec_safe_length (files) != 1
25550 && i > 0
25551 && i + 1 < length
25552 && (*macinfo_table)[i - 1].code == 0)
25554 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
25555 if (count)
25557 i += count - 1;
25558 continue;
25561 break;
25562 case 0:
25563 /* A dummy entry may be inserted at the beginning to be able
25564 to optimize the whole block of predefined macros. */
25565 if (i == 0)
25566 continue;
25567 default:
25568 break;
25570 output_macinfo_op (ref);
25571 ref->info = NULL;
25572 ref->code = 0;
25575 if (!macinfo_htab)
25576 return;
25578 delete macinfo_htab;
25579 macinfo_htab = NULL;
25581 /* If any DW_MACRO_GNU_transparent_include were used, on those
25582 DW_MACRO_GNU_transparent_include entries terminate the
25583 current chain and switch to a new comdat .debug_macinfo
25584 section and emit the define/undef entries within it. */
25585 for (i = 0; macinfo_table->iterate (i, &ref); i++)
25586 switch (ref->code)
25588 case 0:
25589 continue;
25590 case DW_MACRO_GNU_transparent_include:
25592 char label[MAX_ARTIFICIAL_LABEL_BYTES];
25593 tree comdat_key = get_identifier (ref->info);
25594 /* Terminate the previous .debug_macinfo section. */
25595 dw2_asm_output_data (1, 0, "End compilation unit");
25596 targetm.asm_out.named_section (debug_macinfo_section_name,
25597 SECTION_DEBUG
25598 | SECTION_LINKONCE,
25599 comdat_key);
25600 ASM_GENERATE_INTERNAL_LABEL (label,
25601 DEBUG_MACRO_SECTION_LABEL,
25602 ref->lineno);
25603 ASM_OUTPUT_LABEL (asm_out_file, label);
25604 ref->code = 0;
25605 ref->info = NULL;
25606 dw2_asm_output_data (2, 4, "DWARF macro version number");
25607 if (DWARF_OFFSET_SIZE == 8)
25608 dw2_asm_output_data (1, 1, "Flags: 64-bit");
25609 else
25610 dw2_asm_output_data (1, 0, "Flags: 32-bit");
25612 break;
25613 case DW_MACINFO_define:
25614 case DW_MACINFO_undef:
25615 output_macinfo_op (ref);
25616 ref->code = 0;
25617 ref->info = NULL;
25618 break;
25619 default:
25620 gcc_unreachable ();
25624 /* Initialize the various sections and labels for dwarf output. */
25626 static void
25627 init_sections_and_labels (void)
25629 if (!dwarf_split_debug_info)
25631 debug_info_section = get_section (DEBUG_INFO_SECTION,
25632 SECTION_DEBUG, NULL);
25633 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25634 SECTION_DEBUG, NULL);
25635 debug_loc_section = get_section (DEBUG_LOC_SECTION,
25636 SECTION_DEBUG, NULL);
25637 debug_macinfo_section_name
25638 = dwarf_strict ? DEBUG_MACINFO_SECTION : DEBUG_MACRO_SECTION;
25639 debug_macinfo_section = get_section (debug_macinfo_section_name,
25640 SECTION_DEBUG, NULL);
25642 else
25644 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
25645 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25646 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
25647 SECTION_DEBUG | SECTION_EXCLUDE,
25648 NULL);
25649 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
25650 SECTION_DEBUG, NULL);
25651 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
25652 SECTION_DEBUG, NULL);
25653 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
25654 SECTION_DEBUG, NULL);
25655 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
25656 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
25658 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
25659 the main .o, but the skeleton_line goes into the split off dwo. */
25660 debug_skeleton_line_section
25661 = get_section (DEBUG_DWO_LINE_SECTION,
25662 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25663 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
25664 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
25665 debug_str_offsets_section = get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
25666 SECTION_DEBUG | SECTION_EXCLUDE,
25667 NULL);
25668 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
25669 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
25670 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
25671 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
25672 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
25673 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
25674 debug_macinfo_section_name
25675 = dwarf_strict ? DEBUG_DWO_MACINFO_SECTION : DEBUG_DWO_MACRO_SECTION;
25676 debug_macinfo_section = get_section (debug_macinfo_section_name,
25677 SECTION_DEBUG | SECTION_EXCLUDE,
25678 NULL);
25680 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
25681 SECTION_DEBUG, NULL);
25682 debug_line_section = get_section (DEBUG_LINE_SECTION,
25683 SECTION_DEBUG, NULL);
25684 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
25685 SECTION_DEBUG, NULL);
25686 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
25687 SECTION_DEBUG, NULL);
25688 debug_str_section = get_section (DEBUG_STR_SECTION,
25689 DEBUG_STR_SECTION_FLAGS, NULL);
25690 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
25691 SECTION_DEBUG, NULL);
25692 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
25693 SECTION_DEBUG, NULL);
25695 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
25696 DEBUG_ABBREV_SECTION_LABEL, 0);
25697 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
25698 DEBUG_INFO_SECTION_LABEL, 0);
25699 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
25700 DEBUG_LINE_SECTION_LABEL, 0);
25701 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
25702 DEBUG_RANGES_SECTION_LABEL, 0);
25703 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
25704 DEBUG_ADDR_SECTION_LABEL, 0);
25705 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
25706 dwarf_strict
25707 ? DEBUG_MACINFO_SECTION_LABEL
25708 : DEBUG_MACRO_SECTION_LABEL, 0);
25709 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
25712 /* Set up for Dwarf output at the start of compilation. */
25714 static void
25715 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
25717 /* This option is currently broken, see (PR53118 and PR46102). */
25718 if (flag_eliminate_dwarf2_dups
25719 && strstr (lang_hooks.name, "C++"))
25721 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
25722 flag_eliminate_dwarf2_dups = 0;
25725 /* Allocate the file_table. */
25726 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
25728 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25729 /* Allocate the decl_die_table. */
25730 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
25732 /* Allocate the decl_loc_table. */
25733 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
25735 /* Allocate the cached_dw_loc_list_table. */
25736 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
25738 /* Allocate the initial hunk of the decl_scope_table. */
25739 vec_alloc (decl_scope_table, 256);
25741 /* Allocate the initial hunk of the abbrev_die_table. */
25742 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
25743 (ABBREV_DIE_TABLE_INCREMENT);
25744 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
25745 /* Zero-th entry is allocated, but unused. */
25746 abbrev_die_table_in_use = 1;
25748 /* Allocate the dwarf_proc_stack_usage_map. */
25749 dwarf_proc_stack_usage_map = new hash_map<dw_die_ref, int>;
25751 /* Allocate the pubtypes and pubnames vectors. */
25752 vec_alloc (pubname_table, 32);
25753 vec_alloc (pubtype_table, 32);
25755 vec_alloc (incomplete_types, 64);
25757 vec_alloc (used_rtx_array, 32);
25759 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25760 vec_alloc (macinfo_table, 64);
25761 #endif
25763 /* If front-ends already registered a main translation unit but we were not
25764 ready to perform the association, do this now. */
25765 if (main_translation_unit != NULL_TREE)
25766 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
25769 /* Called before compile () starts outputtting functions, variables
25770 and toplevel asms into assembly. */
25772 static void
25773 dwarf2out_assembly_start (void)
25775 #ifndef DWARF2_LINENO_DEBUGGING_INFO
25776 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
25777 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
25778 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
25779 COLD_TEXT_SECTION_LABEL, 0);
25780 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
25782 switch_to_section (text_section);
25783 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
25784 #endif
25786 /* Make sure the line number table for .text always exists. */
25787 text_section_line_info = new_line_info_table ();
25788 text_section_line_info->end_label = text_end_label;
25790 #ifdef DWARF2_LINENO_DEBUGGING_INFO
25791 cur_line_info_table = text_section_line_info;
25792 #endif
25794 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
25795 && dwarf2out_do_cfi_asm ()
25796 && (!(flag_unwind_tables || flag_exceptions)
25797 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
25798 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
25801 /* A helper function for dwarf2out_finish called through
25802 htab_traverse. Assign a string its index. All strings must be
25803 collected into the table by the time index_string is called,
25804 because the indexing code relies on htab_traverse to traverse nodes
25805 in the same order for each run. */
25808 index_string (indirect_string_node **h, unsigned int *index)
25810 indirect_string_node *node = *h;
25812 find_string_form (node);
25813 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25815 gcc_assert (node->index == NO_INDEX_ASSIGNED);
25816 node->index = *index;
25817 *index += 1;
25819 return 1;
25822 /* A helper function for output_indirect_strings called through
25823 htab_traverse. Output the offset to a string and update the
25824 current offset. */
25827 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
25829 indirect_string_node *node = *h;
25831 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25833 /* Assert that this node has been assigned an index. */
25834 gcc_assert (node->index != NO_INDEX_ASSIGNED
25835 && node->index != NOT_INDEXED);
25836 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
25837 "indexed string 0x%x: %s", node->index, node->str);
25838 *offset += strlen (node->str) + 1;
25840 return 1;
25843 /* A helper function for dwarf2out_finish called through
25844 htab_traverse. Output the indexed string. */
25847 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
25849 struct indirect_string_node *node = *h;
25851 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
25853 /* Assert that the strings are output in the same order as their
25854 indexes were assigned. */
25855 gcc_assert (*cur_idx == node->index);
25856 assemble_string (node->str, strlen (node->str) + 1);
25857 *cur_idx += 1;
25859 return 1;
25862 /* A helper function for dwarf2out_finish called through
25863 htab_traverse. Emit one queued .debug_str string. */
25866 output_indirect_string (indirect_string_node **h, void *)
25868 struct indirect_string_node *node = *h;
25870 node->form = find_string_form (node);
25871 if (node->form == DW_FORM_strp && node->refcount > 0)
25873 ASM_OUTPUT_LABEL (asm_out_file, node->label);
25874 assemble_string (node->str, strlen (node->str) + 1);
25877 return 1;
25880 /* Output the indexed string table. */
25882 static void
25883 output_indirect_strings (void)
25885 switch_to_section (debug_str_section);
25886 if (!dwarf_split_debug_info)
25887 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25888 else
25890 unsigned int offset = 0;
25891 unsigned int cur_idx = 0;
25893 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
25895 switch_to_section (debug_str_offsets_section);
25896 debug_str_hash->traverse_noresize
25897 <unsigned int *, output_index_string_offset> (&offset);
25898 switch_to_section (debug_str_dwo_section);
25899 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
25900 (&cur_idx);
25904 /* Callback for htab_traverse to assign an index to an entry in the
25905 table, and to write that entry to the .debug_addr section. */
25908 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
25910 addr_table_entry *entry = *slot;
25912 if (entry->refcount == 0)
25914 gcc_assert (entry->index == NO_INDEX_ASSIGNED
25915 || entry->index == NOT_INDEXED);
25916 return 1;
25919 gcc_assert (entry->index == *cur_index);
25920 (*cur_index)++;
25922 switch (entry->kind)
25924 case ate_kind_rtx:
25925 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
25926 "0x%x", entry->index);
25927 break;
25928 case ate_kind_rtx_dtprel:
25929 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
25930 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
25931 DWARF2_ADDR_SIZE,
25932 entry->addr.rtl);
25933 fputc ('\n', asm_out_file);
25934 break;
25935 case ate_kind_label:
25936 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
25937 "0x%x", entry->index);
25938 break;
25939 default:
25940 gcc_unreachable ();
25942 return 1;
25945 /* Produce the .debug_addr section. */
25947 static void
25948 output_addr_table (void)
25950 unsigned int index = 0;
25951 if (addr_index_table == NULL || addr_index_table->size () == 0)
25952 return;
25954 switch_to_section (debug_addr_section);
25955 addr_index_table
25956 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
25959 #if ENABLE_ASSERT_CHECKING
25960 /* Verify that all marks are clear. */
25962 static void
25963 verify_marks_clear (dw_die_ref die)
25965 dw_die_ref c;
25967 gcc_assert (! die->die_mark);
25968 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
25970 #endif /* ENABLE_ASSERT_CHECKING */
25972 /* Clear the marks for a die and its children.
25973 Be cool if the mark isn't set. */
25975 static void
25976 prune_unmark_dies (dw_die_ref die)
25978 dw_die_ref c;
25980 if (die->die_mark)
25981 die->die_mark = 0;
25982 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
25985 /* Given LOC that is referenced by a DIE we're marking as used, find all
25986 referenced DWARF procedures it references and mark them as used. */
25988 static void
25989 prune_unused_types_walk_loc_descr (dw_loc_descr_ref loc)
25991 for (; loc != NULL; loc = loc->dw_loc_next)
25992 switch (loc->dw_loc_opc)
25994 case DW_OP_GNU_implicit_pointer:
25995 case DW_OP_GNU_convert:
25996 case DW_OP_GNU_reinterpret:
25997 if (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref)
25998 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
25999 break;
26000 case DW_OP_call2:
26001 case DW_OP_call4:
26002 case DW_OP_call_ref:
26003 case DW_OP_GNU_const_type:
26004 case DW_OP_GNU_parameter_ref:
26005 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_die_ref);
26006 prune_unused_types_mark (loc->dw_loc_oprnd1.v.val_die_ref.die, 1);
26007 break;
26008 case DW_OP_GNU_regval_type:
26009 case DW_OP_GNU_deref_type:
26010 gcc_assert (loc->dw_loc_oprnd2.val_class == dw_val_class_die_ref);
26011 prune_unused_types_mark (loc->dw_loc_oprnd2.v.val_die_ref.die, 1);
26012 break;
26013 case DW_OP_GNU_entry_value:
26014 gcc_assert (loc->dw_loc_oprnd1.val_class == dw_val_class_loc);
26015 prune_unused_types_walk_loc_descr (loc->dw_loc_oprnd1.v.val_loc);
26016 break;
26017 default:
26018 break;
26022 /* Given DIE that we're marking as used, find any other dies
26023 it references as attributes and mark them as used. */
26025 static void
26026 prune_unused_types_walk_attribs (dw_die_ref die)
26028 dw_attr_node *a;
26029 unsigned ix;
26031 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26033 switch (AT_class (a))
26035 /* Make sure DWARF procedures referenced by location descriptions will
26036 get emitted. */
26037 case dw_val_class_loc:
26038 prune_unused_types_walk_loc_descr (AT_loc (a));
26039 break;
26040 case dw_val_class_loc_list:
26041 for (dw_loc_list_ref list = AT_loc_list (a);
26042 list != NULL;
26043 list = list->dw_loc_next)
26044 prune_unused_types_walk_loc_descr (list->expr);
26045 break;
26047 case dw_val_class_die_ref:
26048 /* A reference to another DIE.
26049 Make sure that it will get emitted.
26050 If it was broken out into a comdat group, don't follow it. */
26051 if (! AT_ref (a)->comdat_type_p
26052 || a->dw_attr == DW_AT_specification)
26053 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
26054 break;
26056 case dw_val_class_str:
26057 /* Set the string's refcount to 0 so that prune_unused_types_mark
26058 accounts properly for it. */
26059 a->dw_attr_val.v.val_str->refcount = 0;
26060 break;
26062 default:
26063 break;
26068 /* Mark the generic parameters and arguments children DIEs of DIE. */
26070 static void
26071 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
26073 dw_die_ref c;
26075 if (die == NULL || die->die_child == NULL)
26076 return;
26077 c = die->die_child;
26080 if (is_template_parameter (c))
26081 prune_unused_types_mark (c, 1);
26082 c = c->die_sib;
26083 } while (c && c != die->die_child);
26086 /* Mark DIE as being used. If DOKIDS is true, then walk down
26087 to DIE's children. */
26089 static void
26090 prune_unused_types_mark (dw_die_ref die, int dokids)
26092 dw_die_ref c;
26094 if (die->die_mark == 0)
26096 /* We haven't done this node yet. Mark it as used. */
26097 die->die_mark = 1;
26098 /* If this is the DIE of a generic type instantiation,
26099 mark the children DIEs that describe its generic parms and
26100 args. */
26101 prune_unused_types_mark_generic_parms_dies (die);
26103 /* We also have to mark its parents as used.
26104 (But we don't want to mark our parent's kids due to this,
26105 unless it is a class.) */
26106 if (die->die_parent)
26107 prune_unused_types_mark (die->die_parent,
26108 class_scope_p (die->die_parent));
26110 /* Mark any referenced nodes. */
26111 prune_unused_types_walk_attribs (die);
26113 /* If this node is a specification,
26114 also mark the definition, if it exists. */
26115 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
26116 prune_unused_types_mark (die->die_definition, 1);
26119 if (dokids && die->die_mark != 2)
26121 /* We need to walk the children, but haven't done so yet.
26122 Remember that we've walked the kids. */
26123 die->die_mark = 2;
26125 /* If this is an array type, we need to make sure our
26126 kids get marked, even if they're types. If we're
26127 breaking out types into comdat sections, do this
26128 for all type definitions. */
26129 if (die->die_tag == DW_TAG_array_type
26130 || (use_debug_types
26131 && is_type_die (die) && ! is_declaration_die (die)))
26132 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
26133 else
26134 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26138 /* For local classes, look if any static member functions were emitted
26139 and if so, mark them. */
26141 static void
26142 prune_unused_types_walk_local_classes (dw_die_ref die)
26144 dw_die_ref c;
26146 if (die->die_mark == 2)
26147 return;
26149 switch (die->die_tag)
26151 case DW_TAG_structure_type:
26152 case DW_TAG_union_type:
26153 case DW_TAG_class_type:
26154 break;
26156 case DW_TAG_subprogram:
26157 if (!get_AT_flag (die, DW_AT_declaration)
26158 || die->die_definition != NULL)
26159 prune_unused_types_mark (die, 1);
26160 return;
26162 default:
26163 return;
26166 /* Mark children. */
26167 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
26170 /* Walk the tree DIE and mark types that we actually use. */
26172 static void
26173 prune_unused_types_walk (dw_die_ref die)
26175 dw_die_ref c;
26177 /* Don't do anything if this node is already marked and
26178 children have been marked as well. */
26179 if (die->die_mark == 2)
26180 return;
26182 switch (die->die_tag)
26184 case DW_TAG_structure_type:
26185 case DW_TAG_union_type:
26186 case DW_TAG_class_type:
26187 if (die->die_perennial_p)
26188 break;
26190 for (c = die->die_parent; c; c = c->die_parent)
26191 if (c->die_tag == DW_TAG_subprogram)
26192 break;
26194 /* Finding used static member functions inside of classes
26195 is needed just for local classes, because for other classes
26196 static member function DIEs with DW_AT_specification
26197 are emitted outside of the DW_TAG_*_type. If we ever change
26198 it, we'd need to call this even for non-local classes. */
26199 if (c)
26200 prune_unused_types_walk_local_classes (die);
26202 /* It's a type node --- don't mark it. */
26203 return;
26205 case DW_TAG_const_type:
26206 case DW_TAG_packed_type:
26207 case DW_TAG_pointer_type:
26208 case DW_TAG_reference_type:
26209 case DW_TAG_rvalue_reference_type:
26210 case DW_TAG_volatile_type:
26211 case DW_TAG_typedef:
26212 case DW_TAG_array_type:
26213 case DW_TAG_interface_type:
26214 case DW_TAG_friend:
26215 case DW_TAG_enumeration_type:
26216 case DW_TAG_subroutine_type:
26217 case DW_TAG_string_type:
26218 case DW_TAG_set_type:
26219 case DW_TAG_subrange_type:
26220 case DW_TAG_ptr_to_member_type:
26221 case DW_TAG_file_type:
26222 /* Type nodes are useful only when other DIEs reference them --- don't
26223 mark them. */
26224 /* FALLTHROUGH */
26226 case DW_TAG_dwarf_procedure:
26227 /* Likewise for DWARF procedures. */
26229 if (die->die_perennial_p)
26230 break;
26232 return;
26234 default:
26235 /* Mark everything else. */
26236 break;
26239 if (die->die_mark == 0)
26241 die->die_mark = 1;
26243 /* Now, mark any dies referenced from here. */
26244 prune_unused_types_walk_attribs (die);
26247 die->die_mark = 2;
26249 /* Mark children. */
26250 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
26253 /* Increment the string counts on strings referred to from DIE's
26254 attributes. */
26256 static void
26257 prune_unused_types_update_strings (dw_die_ref die)
26259 dw_attr_node *a;
26260 unsigned ix;
26262 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
26263 if (AT_class (a) == dw_val_class_str)
26265 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
26266 s->refcount++;
26267 /* Avoid unnecessarily putting strings that are used less than
26268 twice in the hash table. */
26269 if (s->refcount
26270 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
26272 indirect_string_node **slot
26273 = debug_str_hash->find_slot_with_hash (s->str,
26274 htab_hash_string (s->str),
26275 INSERT);
26276 gcc_assert (*slot == NULL);
26277 *slot = s;
26282 /* Mark DIE and its children as removed. */
26284 static void
26285 mark_removed (dw_die_ref die)
26287 dw_die_ref c;
26288 die->removed = true;
26289 FOR_EACH_CHILD (die, c, mark_removed (c));
26292 /* Remove from the tree DIE any dies that aren't marked. */
26294 static void
26295 prune_unused_types_prune (dw_die_ref die)
26297 dw_die_ref c;
26299 gcc_assert (die->die_mark);
26300 prune_unused_types_update_strings (die);
26302 if (! die->die_child)
26303 return;
26305 c = die->die_child;
26306 do {
26307 dw_die_ref prev = c, next;
26308 for (c = c->die_sib; ! c->die_mark; c = next)
26309 if (c == die->die_child)
26311 /* No marked children between 'prev' and the end of the list. */
26312 if (prev == c)
26313 /* No marked children at all. */
26314 die->die_child = NULL;
26315 else
26317 prev->die_sib = c->die_sib;
26318 die->die_child = prev;
26320 c->die_sib = NULL;
26321 mark_removed (c);
26322 return;
26324 else
26326 next = c->die_sib;
26327 c->die_sib = NULL;
26328 mark_removed (c);
26331 if (c != prev->die_sib)
26332 prev->die_sib = c;
26333 prune_unused_types_prune (c);
26334 } while (c != die->die_child);
26337 /* Remove dies representing declarations that we never use. */
26339 static void
26340 prune_unused_types (void)
26342 unsigned int i;
26343 limbo_die_node *node;
26344 comdat_type_node *ctnode;
26345 pubname_entry *pub;
26346 dw_die_ref base_type;
26348 #if ENABLE_ASSERT_CHECKING
26349 /* All the marks should already be clear. */
26350 verify_marks_clear (comp_unit_die ());
26351 for (node = limbo_die_list; node; node = node->next)
26352 verify_marks_clear (node->die);
26353 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26354 verify_marks_clear (ctnode->root_die);
26355 #endif /* ENABLE_ASSERT_CHECKING */
26357 /* Mark types that are used in global variables. */
26358 premark_types_used_by_global_vars ();
26360 /* Set the mark on nodes that are actually used. */
26361 prune_unused_types_walk (comp_unit_die ());
26362 for (node = limbo_die_list; node; node = node->next)
26363 prune_unused_types_walk (node->die);
26364 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26366 prune_unused_types_walk (ctnode->root_die);
26367 prune_unused_types_mark (ctnode->type_die, 1);
26370 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
26371 are unusual in that they are pubnames that are the children of pubtypes.
26372 They should only be marked via their parent DW_TAG_enumeration_type die,
26373 not as roots in themselves. */
26374 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
26375 if (pub->die->die_tag != DW_TAG_enumerator)
26376 prune_unused_types_mark (pub->die, 1);
26377 for (i = 0; base_types.iterate (i, &base_type); i++)
26378 prune_unused_types_mark (base_type, 1);
26380 if (debug_str_hash)
26381 debug_str_hash->empty ();
26382 if (skeleton_debug_str_hash)
26383 skeleton_debug_str_hash->empty ();
26384 prune_unused_types_prune (comp_unit_die ());
26385 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
26387 node = *pnode;
26388 if (!node->die->die_mark)
26389 *pnode = node->next;
26390 else
26392 prune_unused_types_prune (node->die);
26393 pnode = &node->next;
26396 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26397 prune_unused_types_prune (ctnode->root_die);
26399 /* Leave the marks clear. */
26400 prune_unmark_dies (comp_unit_die ());
26401 for (node = limbo_die_list; node; node = node->next)
26402 prune_unmark_dies (node->die);
26403 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
26404 prune_unmark_dies (ctnode->root_die);
26407 /* Set the parameter to true if there are any relative pathnames in
26408 the file table. */
26410 file_table_relative_p (dwarf_file_data **slot, bool *p)
26412 struct dwarf_file_data *d = *slot;
26413 if (!IS_ABSOLUTE_PATH (d->filename))
26415 *p = true;
26416 return 0;
26418 return 1;
26421 /* Helpers to manipulate hash table of comdat type units. */
26423 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
26425 static inline hashval_t hash (const comdat_type_node *);
26426 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
26429 inline hashval_t
26430 comdat_type_hasher::hash (const comdat_type_node *type_node)
26432 hashval_t h;
26433 memcpy (&h, type_node->signature, sizeof (h));
26434 return h;
26437 inline bool
26438 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
26439 const comdat_type_node *type_node_2)
26441 return (! memcmp (type_node_1->signature, type_node_2->signature,
26442 DWARF_TYPE_SIGNATURE_SIZE));
26445 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
26446 to the location it would have been added, should we know its
26447 DECL_ASSEMBLER_NAME when we added other attributes. This will
26448 probably improve compactness of debug info, removing equivalent
26449 abbrevs, and hide any differences caused by deferring the
26450 computation of the assembler name, triggered by e.g. PCH. */
26452 static inline void
26453 move_linkage_attr (dw_die_ref die)
26455 unsigned ix = vec_safe_length (die->die_attr);
26456 dw_attr_node linkage = (*die->die_attr)[ix - 1];
26458 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
26459 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
26461 while (--ix > 0)
26463 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
26465 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
26466 break;
26469 if (ix != vec_safe_length (die->die_attr) - 1)
26471 die->die_attr->pop ();
26472 die->die_attr->quick_insert (ix, linkage);
26476 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
26477 referenced from typed stack ops and count how often they are used. */
26479 static void
26480 mark_base_types (dw_loc_descr_ref loc)
26482 dw_die_ref base_type = NULL;
26484 for (; loc; loc = loc->dw_loc_next)
26486 switch (loc->dw_loc_opc)
26488 case DW_OP_GNU_regval_type:
26489 case DW_OP_GNU_deref_type:
26490 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
26491 break;
26492 case DW_OP_GNU_convert:
26493 case DW_OP_GNU_reinterpret:
26494 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
26495 continue;
26496 /* FALLTHRU */
26497 case DW_OP_GNU_const_type:
26498 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
26499 break;
26500 case DW_OP_GNU_entry_value:
26501 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
26502 continue;
26503 default:
26504 continue;
26506 gcc_assert (base_type->die_parent == comp_unit_die ());
26507 if (base_type->die_mark)
26508 base_type->die_mark++;
26509 else
26511 base_types.safe_push (base_type);
26512 base_type->die_mark = 1;
26517 /* Comparison function for sorting marked base types. */
26519 static int
26520 base_type_cmp (const void *x, const void *y)
26522 dw_die_ref dx = *(const dw_die_ref *) x;
26523 dw_die_ref dy = *(const dw_die_ref *) y;
26524 unsigned int byte_size1, byte_size2;
26525 unsigned int encoding1, encoding2;
26526 if (dx->die_mark > dy->die_mark)
26527 return -1;
26528 if (dx->die_mark < dy->die_mark)
26529 return 1;
26530 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
26531 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
26532 if (byte_size1 < byte_size2)
26533 return 1;
26534 if (byte_size1 > byte_size2)
26535 return -1;
26536 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
26537 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
26538 if (encoding1 < encoding2)
26539 return 1;
26540 if (encoding1 > encoding2)
26541 return -1;
26542 return 0;
26545 /* Move base types marked by mark_base_types as early as possible
26546 in the CU, sorted by decreasing usage count both to make the
26547 uleb128 references as small as possible and to make sure they
26548 will have die_offset already computed by calc_die_sizes when
26549 sizes of typed stack loc ops is computed. */
26551 static void
26552 move_marked_base_types (void)
26554 unsigned int i;
26555 dw_die_ref base_type, die, c;
26557 if (base_types.is_empty ())
26558 return;
26560 /* Sort by decreasing usage count, they will be added again in that
26561 order later on. */
26562 base_types.qsort (base_type_cmp);
26563 die = comp_unit_die ();
26564 c = die->die_child;
26567 dw_die_ref prev = c;
26568 c = c->die_sib;
26569 while (c->die_mark)
26571 remove_child_with_prev (c, prev);
26572 /* As base types got marked, there must be at least
26573 one node other than DW_TAG_base_type. */
26574 gcc_assert (die->die_child != NULL);
26575 c = prev->die_sib;
26578 while (c != die->die_child);
26579 gcc_assert (die->die_child);
26580 c = die->die_child;
26581 for (i = 0; base_types.iterate (i, &base_type); i++)
26583 base_type->die_mark = 0;
26584 base_type->die_sib = c->die_sib;
26585 c->die_sib = base_type;
26586 c = base_type;
26590 /* Helper function for resolve_addr, attempt to resolve
26591 one CONST_STRING, return true if successful. Similarly verify that
26592 SYMBOL_REFs refer to variables emitted in the current CU. */
26594 static bool
26595 resolve_one_addr (rtx *addr)
26597 rtx rtl = *addr;
26599 if (GET_CODE (rtl) == CONST_STRING)
26601 size_t len = strlen (XSTR (rtl, 0)) + 1;
26602 tree t = build_string (len, XSTR (rtl, 0));
26603 tree tlen = size_int (len - 1);
26604 TREE_TYPE (t)
26605 = build_array_type (char_type_node, build_index_type (tlen));
26606 rtl = lookup_constant_def (t);
26607 if (!rtl || !MEM_P (rtl))
26608 return false;
26609 rtl = XEXP (rtl, 0);
26610 if (GET_CODE (rtl) == SYMBOL_REF
26611 && SYMBOL_REF_DECL (rtl)
26612 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26613 return false;
26614 vec_safe_push (used_rtx_array, rtl);
26615 *addr = rtl;
26616 return true;
26619 if (GET_CODE (rtl) == SYMBOL_REF
26620 && SYMBOL_REF_DECL (rtl))
26622 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
26624 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
26625 return false;
26627 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
26628 return false;
26631 if (GET_CODE (rtl) == CONST)
26633 subrtx_ptr_iterator::array_type array;
26634 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
26635 if (!resolve_one_addr (*iter))
26636 return false;
26639 return true;
26642 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
26643 if possible, and create DW_TAG_dwarf_procedure that can be referenced
26644 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
26646 static rtx
26647 string_cst_pool_decl (tree t)
26649 rtx rtl = output_constant_def (t, 1);
26650 unsigned char *array;
26651 dw_loc_descr_ref l;
26652 tree decl;
26653 size_t len;
26654 dw_die_ref ref;
26656 if (!rtl || !MEM_P (rtl))
26657 return NULL_RTX;
26658 rtl = XEXP (rtl, 0);
26659 if (GET_CODE (rtl) != SYMBOL_REF
26660 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
26661 return NULL_RTX;
26663 decl = SYMBOL_REF_DECL (rtl);
26664 if (!lookup_decl_die (decl))
26666 len = TREE_STRING_LENGTH (t);
26667 vec_safe_push (used_rtx_array, rtl);
26668 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
26669 array = ggc_vec_alloc<unsigned char> (len);
26670 memcpy (array, TREE_STRING_POINTER (t), len);
26671 l = new_loc_descr (DW_OP_implicit_value, len, 0);
26672 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
26673 l->dw_loc_oprnd2.v.val_vec.length = len;
26674 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
26675 l->dw_loc_oprnd2.v.val_vec.array = array;
26676 add_AT_loc (ref, DW_AT_location, l);
26677 equate_decl_number_to_die (decl, ref);
26679 return rtl;
26682 /* Helper function of resolve_addr_in_expr. LOC is
26683 a DW_OP_addr followed by DW_OP_stack_value, either at the start
26684 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
26685 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
26686 with DW_OP_GNU_implicit_pointer if possible
26687 and return true, if unsuccessful, return false. */
26689 static bool
26690 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
26692 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
26693 HOST_WIDE_INT offset = 0;
26694 dw_die_ref ref = NULL;
26695 tree decl;
26697 if (GET_CODE (rtl) == CONST
26698 && GET_CODE (XEXP (rtl, 0)) == PLUS
26699 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
26701 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
26702 rtl = XEXP (XEXP (rtl, 0), 0);
26704 if (GET_CODE (rtl) == CONST_STRING)
26706 size_t len = strlen (XSTR (rtl, 0)) + 1;
26707 tree t = build_string (len, XSTR (rtl, 0));
26708 tree tlen = size_int (len - 1);
26710 TREE_TYPE (t)
26711 = build_array_type (char_type_node, build_index_type (tlen));
26712 rtl = string_cst_pool_decl (t);
26713 if (!rtl)
26714 return false;
26716 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
26718 decl = SYMBOL_REF_DECL (rtl);
26719 if (VAR_P (decl) && !DECL_EXTERNAL (decl))
26721 ref = lookup_decl_die (decl);
26722 if (ref && (get_AT (ref, DW_AT_location)
26723 || get_AT (ref, DW_AT_const_value)))
26725 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
26726 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26727 loc->dw_loc_oprnd1.val_entry = NULL;
26728 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26729 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26730 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26731 loc->dw_loc_oprnd2.v.val_int = offset;
26732 return true;
26736 return false;
26739 /* Helper function for resolve_addr, handle one location
26740 expression, return false if at least one CONST_STRING or SYMBOL_REF in
26741 the location list couldn't be resolved. */
26743 static bool
26744 resolve_addr_in_expr (dw_loc_descr_ref loc)
26746 dw_loc_descr_ref keep = NULL;
26747 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
26748 switch (loc->dw_loc_opc)
26750 case DW_OP_addr:
26751 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26753 if ((prev == NULL
26754 || prev->dw_loc_opc == DW_OP_piece
26755 || prev->dw_loc_opc == DW_OP_bit_piece)
26756 && loc->dw_loc_next
26757 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
26758 && !dwarf_strict
26759 && optimize_one_addr_into_implicit_ptr (loc))
26760 break;
26761 return false;
26763 break;
26764 case DW_OP_GNU_addr_index:
26765 case DW_OP_GNU_const_index:
26766 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
26767 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
26769 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
26770 if (!resolve_one_addr (&rtl))
26771 return false;
26772 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
26773 loc->dw_loc_oprnd1.val_entry =
26774 add_addr_table_entry (rtl, ate_kind_rtx);
26776 break;
26777 case DW_OP_const4u:
26778 case DW_OP_const8u:
26779 if (loc->dtprel
26780 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
26781 return false;
26782 break;
26783 case DW_OP_plus_uconst:
26784 if (size_of_loc_descr (loc)
26785 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
26787 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
26789 dw_loc_descr_ref repl
26790 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
26791 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
26792 add_loc_descr (&repl, loc->dw_loc_next);
26793 *loc = *repl;
26795 break;
26796 case DW_OP_implicit_value:
26797 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
26798 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
26799 return false;
26800 break;
26801 case DW_OP_GNU_implicit_pointer:
26802 case DW_OP_GNU_parameter_ref:
26803 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
26805 dw_die_ref ref
26806 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
26807 if (ref == NULL)
26808 return false;
26809 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26810 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
26811 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
26813 break;
26814 case DW_OP_GNU_const_type:
26815 case DW_OP_GNU_regval_type:
26816 case DW_OP_GNU_deref_type:
26817 case DW_OP_GNU_convert:
26818 case DW_OP_GNU_reinterpret:
26819 while (loc->dw_loc_next
26820 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
26822 dw_die_ref base1, base2;
26823 unsigned enc1, enc2, size1, size2;
26824 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26825 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26826 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
26827 else if (loc->dw_loc_oprnd1.val_class
26828 == dw_val_class_unsigned_const)
26829 break;
26830 else
26831 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
26832 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
26833 == dw_val_class_unsigned_const)
26834 break;
26835 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
26836 gcc_assert (base1->die_tag == DW_TAG_base_type
26837 && base2->die_tag == DW_TAG_base_type);
26838 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
26839 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
26840 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
26841 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
26842 if (size1 == size2
26843 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
26844 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
26845 && loc != keep)
26846 || enc1 == enc2))
26848 /* Optimize away next DW_OP_GNU_convert after
26849 adjusting LOC's base type die reference. */
26850 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
26851 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
26852 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
26853 else
26854 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
26855 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
26856 continue;
26858 /* Don't change integer DW_OP_GNU_convert after e.g. floating
26859 point typed stack entry. */
26860 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
26861 keep = loc->dw_loc_next;
26862 break;
26864 break;
26865 default:
26866 break;
26868 return true;
26871 /* Helper function of resolve_addr. DIE had DW_AT_location of
26872 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
26873 and DW_OP_addr couldn't be resolved. resolve_addr has already
26874 removed the DW_AT_location attribute. This function attempts to
26875 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
26876 to it or DW_AT_const_value attribute, if possible. */
26878 static void
26879 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
26881 if (!VAR_P (decl)
26882 || lookup_decl_die (decl) != die
26883 || DECL_EXTERNAL (decl)
26884 || !TREE_STATIC (decl)
26885 || DECL_INITIAL (decl) == NULL_TREE
26886 || DECL_P (DECL_INITIAL (decl))
26887 || get_AT (die, DW_AT_const_value))
26888 return;
26890 tree init = DECL_INITIAL (decl);
26891 HOST_WIDE_INT offset = 0;
26892 /* For variables that have been optimized away and thus
26893 don't have a memory location, see if we can emit
26894 DW_AT_const_value instead. */
26895 if (tree_add_const_value_attribute (die, init))
26896 return;
26897 if (dwarf_strict)
26898 return;
26899 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
26900 and ADDR_EXPR refers to a decl that has DW_AT_location or
26901 DW_AT_const_value (but isn't addressable, otherwise
26902 resolving the original DW_OP_addr wouldn't fail), see if
26903 we can add DW_OP_GNU_implicit_pointer. */
26904 STRIP_NOPS (init);
26905 if (TREE_CODE (init) == POINTER_PLUS_EXPR
26906 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
26908 offset = tree_to_shwi (TREE_OPERAND (init, 1));
26909 init = TREE_OPERAND (init, 0);
26910 STRIP_NOPS (init);
26912 if (TREE_CODE (init) != ADDR_EXPR)
26913 return;
26914 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
26915 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
26916 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
26917 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
26918 && TREE_OPERAND (init, 0) != decl))
26920 dw_die_ref ref;
26921 dw_loc_descr_ref l;
26923 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
26925 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
26926 if (!rtl)
26927 return;
26928 decl = SYMBOL_REF_DECL (rtl);
26930 else
26931 decl = TREE_OPERAND (init, 0);
26932 ref = lookup_decl_die (decl);
26933 if (ref == NULL
26934 || (!get_AT (ref, DW_AT_location)
26935 && !get_AT (ref, DW_AT_const_value)))
26936 return;
26937 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
26938 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
26939 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
26940 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
26941 add_AT_loc (die, DW_AT_location, l);
26945 /* Return NULL if l is a DWARF expression, or first op that is not
26946 valid DWARF expression. */
26948 static dw_loc_descr_ref
26949 non_dwarf_expression (dw_loc_descr_ref l)
26951 while (l)
26953 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26954 return l;
26955 switch (l->dw_loc_opc)
26957 case DW_OP_regx:
26958 case DW_OP_implicit_value:
26959 case DW_OP_stack_value:
26960 case DW_OP_GNU_implicit_pointer:
26961 case DW_OP_GNU_parameter_ref:
26962 case DW_OP_piece:
26963 case DW_OP_bit_piece:
26964 return l;
26965 default:
26966 break;
26968 l = l->dw_loc_next;
26970 return NULL;
26973 /* Return adjusted copy of EXPR:
26974 If it is empty DWARF expression, return it.
26975 If it is valid non-empty DWARF expression,
26976 return copy of EXPR with copy of DEREF appended to it.
26977 If it is DWARF expression followed by DW_OP_reg{N,x}, return
26978 copy of the DWARF expression with DW_OP_breg{N,x} <0> appended
26979 and no DEREF.
26980 If it is DWARF expression followed by DW_OP_stack_value, return
26981 copy of the DWARF expression without anything appended.
26982 Otherwise, return NULL. */
26984 static dw_loc_descr_ref
26985 copy_deref_exprloc (dw_loc_descr_ref expr, dw_loc_descr_ref deref)
26988 if (expr == NULL)
26989 return NULL;
26991 dw_loc_descr_ref l = non_dwarf_expression (expr);
26992 if (l && l->dw_loc_next)
26993 return NULL;
26995 if (l)
26997 if (l->dw_loc_opc >= DW_OP_reg0 && l->dw_loc_opc <= DW_OP_reg31)
26998 deref = new_loc_descr ((enum dwarf_location_atom)
26999 (DW_OP_breg0 + (l->dw_loc_opc - DW_OP_reg0)),
27000 0, 0);
27001 else
27002 switch (l->dw_loc_opc)
27004 case DW_OP_regx:
27005 deref = new_loc_descr (DW_OP_bregx,
27006 l->dw_loc_oprnd1.v.val_unsigned, 0);
27007 break;
27008 case DW_OP_stack_value:
27009 deref = NULL;
27010 break;
27011 default:
27012 return NULL;
27015 else
27016 deref = new_loc_descr (deref->dw_loc_opc,
27017 deref->dw_loc_oprnd1.v.val_int, 0);
27019 dw_loc_descr_ref ret = NULL, *p = &ret;
27020 while (expr != l)
27022 *p = new_loc_descr (expr->dw_loc_opc, 0, 0);
27023 (*p)->dw_loc_oprnd1 = expr->dw_loc_oprnd1;
27024 (*p)->dw_loc_oprnd2 = expr->dw_loc_oprnd2;
27025 p = &(*p)->dw_loc_next;
27026 expr = expr->dw_loc_next;
27028 *p = deref;
27029 return ret;
27032 /* For DW_AT_string_length attribute with DW_OP_call4 reference to a variable
27033 or argument, adjust it if needed and return:
27034 -1 if the DW_AT_string_length attribute and DW_AT_{string_length_,}byte_size
27035 attribute if present should be removed
27036 0 keep the attribute as is if the referenced var or argument has
27037 only DWARF expression that covers all ranges
27038 1 if the attribute has been successfully adjusted. */
27040 static int
27041 optimize_string_length (dw_attr_node *a)
27043 dw_loc_descr_ref l = AT_loc (a), lv;
27044 dw_die_ref die = l->dw_loc_oprnd1.v.val_die_ref.die;
27045 dw_attr_node *av = get_AT (die, DW_AT_location);
27046 dw_loc_list_ref d;
27047 bool non_dwarf_expr = false;
27049 if (av == NULL)
27050 return -1;
27051 switch (AT_class (av))
27053 case dw_val_class_loc_list:
27054 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27055 if (d->expr && non_dwarf_expression (d->expr))
27056 non_dwarf_expr = true;
27057 break;
27058 case dw_val_class_loc:
27059 lv = AT_loc (av);
27060 if (lv == NULL)
27061 return -1;
27062 if (non_dwarf_expression (lv))
27063 non_dwarf_expr = true;
27064 break;
27065 default:
27066 return -1;
27069 /* If it is safe to keep DW_OP_call4 in, keep it. */
27070 if (!non_dwarf_expr
27071 && (l->dw_loc_next == NULL || AT_class (av) == dw_val_class_loc))
27072 return 0;
27074 /* If not dereferencing the DW_OP_call4 afterwards, we can just
27075 copy over the DW_AT_location attribute from die to a. */
27076 if (l->dw_loc_next == NULL)
27078 a->dw_attr_val = av->dw_attr_val;
27079 return 1;
27082 dw_loc_list_ref list, *p;
27083 switch (AT_class (av))
27085 case dw_val_class_loc_list:
27086 p = &list;
27087 list = NULL;
27088 for (d = AT_loc_list (av); d != NULL; d = d->dw_loc_next)
27090 lv = copy_deref_exprloc (d->expr, l->dw_loc_next);
27091 if (lv)
27093 *p = new_loc_list (lv, d->begin, d->end, d->section);
27094 p = &(*p)->dw_loc_next;
27097 if (list == NULL)
27098 return -1;
27099 a->dw_attr_val.val_class = dw_val_class_loc_list;
27100 gen_llsym (list);
27101 *AT_loc_list_ptr (a) = list;
27102 return 1;
27103 case dw_val_class_loc:
27104 lv = copy_deref_exprloc (AT_loc (av), l->dw_loc_next);
27105 if (lv == NULL)
27106 return -1;
27107 a->dw_attr_val.v.val_loc = lv;
27108 return 1;
27109 default:
27110 gcc_unreachable ();
27114 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
27115 an address in .rodata section if the string literal is emitted there,
27116 or remove the containing location list or replace DW_AT_const_value
27117 with DW_AT_location and empty location expression, if it isn't found
27118 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
27119 to something that has been emitted in the current CU. */
27121 static void
27122 resolve_addr (dw_die_ref die)
27124 dw_die_ref c;
27125 dw_attr_node *a;
27126 dw_loc_list_ref *curr, *start, loc;
27127 unsigned ix;
27128 bool remove_AT_byte_size = false;
27130 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27131 switch (AT_class (a))
27133 case dw_val_class_loc_list:
27134 start = curr = AT_loc_list_ptr (a);
27135 loc = *curr;
27136 gcc_assert (loc);
27137 /* The same list can be referenced more than once. See if we have
27138 already recorded the result from a previous pass. */
27139 if (loc->replaced)
27140 *curr = loc->dw_loc_next;
27141 else if (!loc->resolved_addr)
27143 /* As things stand, we do not expect or allow one die to
27144 reference a suffix of another die's location list chain.
27145 References must be identical or completely separate.
27146 There is therefore no need to cache the result of this
27147 pass on any list other than the first; doing so
27148 would lead to unnecessary writes. */
27149 while (*curr)
27151 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
27152 if (!resolve_addr_in_expr ((*curr)->expr))
27154 dw_loc_list_ref next = (*curr)->dw_loc_next;
27155 dw_loc_descr_ref l = (*curr)->expr;
27157 if (next && (*curr)->ll_symbol)
27159 gcc_assert (!next->ll_symbol);
27160 next->ll_symbol = (*curr)->ll_symbol;
27162 if (dwarf_split_debug_info)
27163 remove_loc_list_addr_table_entries (l);
27164 *curr = next;
27166 else
27168 mark_base_types ((*curr)->expr);
27169 curr = &(*curr)->dw_loc_next;
27172 if (loc == *start)
27173 loc->resolved_addr = 1;
27174 else
27176 loc->replaced = 1;
27177 loc->dw_loc_next = *start;
27180 if (!*start)
27182 remove_AT (die, a->dw_attr);
27183 ix--;
27185 break;
27186 case dw_val_class_loc:
27188 dw_loc_descr_ref l = AT_loc (a);
27189 /* Using DW_OP_call4 or DW_OP_call4 DW_OP_deref in
27190 DW_AT_string_length is only a rough approximation; unfortunately
27191 DW_AT_string_length can't be a reference to a DIE. DW_OP_call4
27192 needs a DWARF expression, while DW_AT_location of the referenced
27193 variable or argument might be any location description. */
27194 if (a->dw_attr == DW_AT_string_length
27195 && l
27196 && l->dw_loc_opc == DW_OP_call4
27197 && l->dw_loc_oprnd1.val_class == dw_val_class_die_ref
27198 && (l->dw_loc_next == NULL
27199 || (l->dw_loc_next->dw_loc_next == NULL
27200 && (l->dw_loc_next->dw_loc_opc == DW_OP_deref
27201 || l->dw_loc_next->dw_loc_opc != DW_OP_deref_size))))
27203 switch (optimize_string_length (a))
27205 case -1:
27206 remove_AT (die, a->dw_attr);
27207 ix--;
27208 /* If we drop DW_AT_string_length, we need to drop also
27209 DW_AT_{string_length_,}byte_size. */
27210 remove_AT_byte_size = true;
27211 continue;
27212 default:
27213 break;
27214 case 1:
27215 /* Even if we keep the optimized DW_AT_string_length,
27216 it might have changed AT_class, so process it again. */
27217 ix--;
27218 continue;
27221 /* For -gdwarf-2 don't attempt to optimize
27222 DW_AT_data_member_location containing
27223 DW_OP_plus_uconst - older consumers might
27224 rely on it being that op instead of a more complex,
27225 but shorter, location description. */
27226 if ((dwarf_version > 2
27227 || a->dw_attr != DW_AT_data_member_location
27228 || l == NULL
27229 || l->dw_loc_opc != DW_OP_plus_uconst
27230 || l->dw_loc_next != NULL)
27231 && !resolve_addr_in_expr (l))
27233 if (dwarf_split_debug_info)
27234 remove_loc_list_addr_table_entries (l);
27235 if (l != NULL
27236 && l->dw_loc_next == NULL
27237 && l->dw_loc_opc == DW_OP_addr
27238 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
27239 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
27240 && a->dw_attr == DW_AT_location)
27242 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
27243 remove_AT (die, a->dw_attr);
27244 ix--;
27245 optimize_location_into_implicit_ptr (die, decl);
27246 break;
27248 remove_AT (die, a->dw_attr);
27249 ix--;
27251 else
27252 mark_base_types (l);
27254 break;
27255 case dw_val_class_addr:
27256 if (a->dw_attr == DW_AT_const_value
27257 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
27259 if (AT_index (a) != NOT_INDEXED)
27260 remove_addr_table_entry (a->dw_attr_val.val_entry);
27261 remove_AT (die, a->dw_attr);
27262 ix--;
27264 if (die->die_tag == DW_TAG_GNU_call_site
27265 && a->dw_attr == DW_AT_abstract_origin)
27267 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
27268 dw_die_ref tdie = lookup_decl_die (tdecl);
27269 dw_die_ref cdie;
27270 if (tdie == NULL
27271 && DECL_EXTERNAL (tdecl)
27272 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
27273 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
27275 /* Creating a full DIE for tdecl is overly expensive and
27276 at this point even wrong when in the LTO phase
27277 as it can end up generating new type DIEs we didn't
27278 output and thus optimize_external_refs will crash. */
27279 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
27280 add_AT_flag (tdie, DW_AT_external, 1);
27281 add_AT_flag (tdie, DW_AT_declaration, 1);
27282 add_linkage_attr (tdie, tdecl);
27283 add_name_and_src_coords_attributes (tdie, tdecl);
27284 equate_decl_number_to_die (tdecl, tdie);
27286 if (tdie)
27288 a->dw_attr_val.val_class = dw_val_class_die_ref;
27289 a->dw_attr_val.v.val_die_ref.die = tdie;
27290 a->dw_attr_val.v.val_die_ref.external = 0;
27292 else
27294 if (AT_index (a) != NOT_INDEXED)
27295 remove_addr_table_entry (a->dw_attr_val.val_entry);
27296 remove_AT (die, a->dw_attr);
27297 ix--;
27300 break;
27301 default:
27302 break;
27305 if (remove_AT_byte_size)
27306 remove_AT (die, dwarf_version >= 5
27307 ? DW_AT_string_length_byte_size
27308 : DW_AT_byte_size);
27310 FOR_EACH_CHILD (die, c, resolve_addr (c));
27313 /* Helper routines for optimize_location_lists.
27314 This pass tries to share identical local lists in .debug_loc
27315 section. */
27317 /* Iteratively hash operands of LOC opcode into HSTATE. */
27319 static void
27320 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
27322 dw_val_ref val1 = &loc->dw_loc_oprnd1;
27323 dw_val_ref val2 = &loc->dw_loc_oprnd2;
27325 switch (loc->dw_loc_opc)
27327 case DW_OP_const4u:
27328 case DW_OP_const8u:
27329 if (loc->dtprel)
27330 goto hash_addr;
27331 /* FALLTHRU */
27332 case DW_OP_const1u:
27333 case DW_OP_const1s:
27334 case DW_OP_const2u:
27335 case DW_OP_const2s:
27336 case DW_OP_const4s:
27337 case DW_OP_const8s:
27338 case DW_OP_constu:
27339 case DW_OP_consts:
27340 case DW_OP_pick:
27341 case DW_OP_plus_uconst:
27342 case DW_OP_breg0:
27343 case DW_OP_breg1:
27344 case DW_OP_breg2:
27345 case DW_OP_breg3:
27346 case DW_OP_breg4:
27347 case DW_OP_breg5:
27348 case DW_OP_breg6:
27349 case DW_OP_breg7:
27350 case DW_OP_breg8:
27351 case DW_OP_breg9:
27352 case DW_OP_breg10:
27353 case DW_OP_breg11:
27354 case DW_OP_breg12:
27355 case DW_OP_breg13:
27356 case DW_OP_breg14:
27357 case DW_OP_breg15:
27358 case DW_OP_breg16:
27359 case DW_OP_breg17:
27360 case DW_OP_breg18:
27361 case DW_OP_breg19:
27362 case DW_OP_breg20:
27363 case DW_OP_breg21:
27364 case DW_OP_breg22:
27365 case DW_OP_breg23:
27366 case DW_OP_breg24:
27367 case DW_OP_breg25:
27368 case DW_OP_breg26:
27369 case DW_OP_breg27:
27370 case DW_OP_breg28:
27371 case DW_OP_breg29:
27372 case DW_OP_breg30:
27373 case DW_OP_breg31:
27374 case DW_OP_regx:
27375 case DW_OP_fbreg:
27376 case DW_OP_piece:
27377 case DW_OP_deref_size:
27378 case DW_OP_xderef_size:
27379 hstate.add_object (val1->v.val_int);
27380 break;
27381 case DW_OP_skip:
27382 case DW_OP_bra:
27384 int offset;
27386 gcc_assert (val1->val_class == dw_val_class_loc);
27387 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
27388 hstate.add_object (offset);
27390 break;
27391 case DW_OP_implicit_value:
27392 hstate.add_object (val1->v.val_unsigned);
27393 switch (val2->val_class)
27395 case dw_val_class_const:
27396 hstate.add_object (val2->v.val_int);
27397 break;
27398 case dw_val_class_vec:
27400 unsigned int elt_size = val2->v.val_vec.elt_size;
27401 unsigned int len = val2->v.val_vec.length;
27403 hstate.add_int (elt_size);
27404 hstate.add_int (len);
27405 hstate.add (val2->v.val_vec.array, len * elt_size);
27407 break;
27408 case dw_val_class_const_double:
27409 hstate.add_object (val2->v.val_double.low);
27410 hstate.add_object (val2->v.val_double.high);
27411 break;
27412 case dw_val_class_wide_int:
27413 hstate.add (val2->v.val_wide->get_val (),
27414 get_full_len (*val2->v.val_wide)
27415 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27416 break;
27417 case dw_val_class_addr:
27418 inchash::add_rtx (val2->v.val_addr, hstate);
27419 break;
27420 default:
27421 gcc_unreachable ();
27423 break;
27424 case DW_OP_bregx:
27425 case DW_OP_bit_piece:
27426 hstate.add_object (val1->v.val_int);
27427 hstate.add_object (val2->v.val_int);
27428 break;
27429 case DW_OP_addr:
27430 hash_addr:
27431 if (loc->dtprel)
27433 unsigned char dtprel = 0xd1;
27434 hstate.add_object (dtprel);
27436 inchash::add_rtx (val1->v.val_addr, hstate);
27437 break;
27438 case DW_OP_GNU_addr_index:
27439 case DW_OP_GNU_const_index:
27441 if (loc->dtprel)
27443 unsigned char dtprel = 0xd1;
27444 hstate.add_object (dtprel);
27446 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
27448 break;
27449 case DW_OP_GNU_implicit_pointer:
27450 hstate.add_int (val2->v.val_int);
27451 break;
27452 case DW_OP_GNU_entry_value:
27453 hstate.add_object (val1->v.val_loc);
27454 break;
27455 case DW_OP_GNU_regval_type:
27456 case DW_OP_GNU_deref_type:
27458 unsigned int byte_size
27459 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
27460 unsigned int encoding
27461 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
27462 hstate.add_object (val1->v.val_int);
27463 hstate.add_object (byte_size);
27464 hstate.add_object (encoding);
27466 break;
27467 case DW_OP_GNU_convert:
27468 case DW_OP_GNU_reinterpret:
27469 if (val1->val_class == dw_val_class_unsigned_const)
27471 hstate.add_object (val1->v.val_unsigned);
27472 break;
27474 /* FALLTHRU */
27475 case DW_OP_GNU_const_type:
27477 unsigned int byte_size
27478 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
27479 unsigned int encoding
27480 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
27481 hstate.add_object (byte_size);
27482 hstate.add_object (encoding);
27483 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
27484 break;
27485 hstate.add_object (val2->val_class);
27486 switch (val2->val_class)
27488 case dw_val_class_const:
27489 hstate.add_object (val2->v.val_int);
27490 break;
27491 case dw_val_class_vec:
27493 unsigned int elt_size = val2->v.val_vec.elt_size;
27494 unsigned int len = val2->v.val_vec.length;
27496 hstate.add_object (elt_size);
27497 hstate.add_object (len);
27498 hstate.add (val2->v.val_vec.array, len * elt_size);
27500 break;
27501 case dw_val_class_const_double:
27502 hstate.add_object (val2->v.val_double.low);
27503 hstate.add_object (val2->v.val_double.high);
27504 break;
27505 case dw_val_class_wide_int:
27506 hstate.add (val2->v.val_wide->get_val (),
27507 get_full_len (*val2->v.val_wide)
27508 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
27509 break;
27510 default:
27511 gcc_unreachable ();
27514 break;
27516 default:
27517 /* Other codes have no operands. */
27518 break;
27522 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
27524 static inline void
27525 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
27527 dw_loc_descr_ref l;
27528 bool sizes_computed = false;
27529 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
27530 size_of_locs (loc);
27532 for (l = loc; l != NULL; l = l->dw_loc_next)
27534 enum dwarf_location_atom opc = l->dw_loc_opc;
27535 hstate.add_object (opc);
27536 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
27538 size_of_locs (loc);
27539 sizes_computed = true;
27541 hash_loc_operands (l, hstate);
27545 /* Compute hash of the whole location list LIST_HEAD. */
27547 static inline void
27548 hash_loc_list (dw_loc_list_ref list_head)
27550 dw_loc_list_ref curr = list_head;
27551 inchash::hash hstate;
27553 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
27555 hstate.add (curr->begin, strlen (curr->begin) + 1);
27556 hstate.add (curr->end, strlen (curr->end) + 1);
27557 if (curr->section)
27558 hstate.add (curr->section, strlen (curr->section) + 1);
27559 hash_locs (curr->expr, hstate);
27561 list_head->hash = hstate.end ();
27564 /* Return true if X and Y opcodes have the same operands. */
27566 static inline bool
27567 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
27569 dw_val_ref valx1 = &x->dw_loc_oprnd1;
27570 dw_val_ref valx2 = &x->dw_loc_oprnd2;
27571 dw_val_ref valy1 = &y->dw_loc_oprnd1;
27572 dw_val_ref valy2 = &y->dw_loc_oprnd2;
27574 switch (x->dw_loc_opc)
27576 case DW_OP_const4u:
27577 case DW_OP_const8u:
27578 if (x->dtprel)
27579 goto hash_addr;
27580 /* FALLTHRU */
27581 case DW_OP_const1u:
27582 case DW_OP_const1s:
27583 case DW_OP_const2u:
27584 case DW_OP_const2s:
27585 case DW_OP_const4s:
27586 case DW_OP_const8s:
27587 case DW_OP_constu:
27588 case DW_OP_consts:
27589 case DW_OP_pick:
27590 case DW_OP_plus_uconst:
27591 case DW_OP_breg0:
27592 case DW_OP_breg1:
27593 case DW_OP_breg2:
27594 case DW_OP_breg3:
27595 case DW_OP_breg4:
27596 case DW_OP_breg5:
27597 case DW_OP_breg6:
27598 case DW_OP_breg7:
27599 case DW_OP_breg8:
27600 case DW_OP_breg9:
27601 case DW_OP_breg10:
27602 case DW_OP_breg11:
27603 case DW_OP_breg12:
27604 case DW_OP_breg13:
27605 case DW_OP_breg14:
27606 case DW_OP_breg15:
27607 case DW_OP_breg16:
27608 case DW_OP_breg17:
27609 case DW_OP_breg18:
27610 case DW_OP_breg19:
27611 case DW_OP_breg20:
27612 case DW_OP_breg21:
27613 case DW_OP_breg22:
27614 case DW_OP_breg23:
27615 case DW_OP_breg24:
27616 case DW_OP_breg25:
27617 case DW_OP_breg26:
27618 case DW_OP_breg27:
27619 case DW_OP_breg28:
27620 case DW_OP_breg29:
27621 case DW_OP_breg30:
27622 case DW_OP_breg31:
27623 case DW_OP_regx:
27624 case DW_OP_fbreg:
27625 case DW_OP_piece:
27626 case DW_OP_deref_size:
27627 case DW_OP_xderef_size:
27628 return valx1->v.val_int == valy1->v.val_int;
27629 case DW_OP_skip:
27630 case DW_OP_bra:
27631 /* If splitting debug info, the use of DW_OP_GNU_addr_index
27632 can cause irrelevant differences in dw_loc_addr. */
27633 gcc_assert (valx1->val_class == dw_val_class_loc
27634 && valy1->val_class == dw_val_class_loc
27635 && (dwarf_split_debug_info
27636 || x->dw_loc_addr == y->dw_loc_addr));
27637 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
27638 case DW_OP_implicit_value:
27639 if (valx1->v.val_unsigned != valy1->v.val_unsigned
27640 || valx2->val_class != valy2->val_class)
27641 return false;
27642 switch (valx2->val_class)
27644 case dw_val_class_const:
27645 return valx2->v.val_int == valy2->v.val_int;
27646 case dw_val_class_vec:
27647 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27648 && valx2->v.val_vec.length == valy2->v.val_vec.length
27649 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27650 valx2->v.val_vec.elt_size
27651 * valx2->v.val_vec.length) == 0;
27652 case dw_val_class_const_double:
27653 return valx2->v.val_double.low == valy2->v.val_double.low
27654 && valx2->v.val_double.high == valy2->v.val_double.high;
27655 case dw_val_class_wide_int:
27656 return *valx2->v.val_wide == *valy2->v.val_wide;
27657 case dw_val_class_addr:
27658 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
27659 default:
27660 gcc_unreachable ();
27662 case DW_OP_bregx:
27663 case DW_OP_bit_piece:
27664 return valx1->v.val_int == valy1->v.val_int
27665 && valx2->v.val_int == valy2->v.val_int;
27666 case DW_OP_addr:
27667 hash_addr:
27668 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
27669 case DW_OP_GNU_addr_index:
27670 case DW_OP_GNU_const_index:
27672 rtx ax1 = valx1->val_entry->addr.rtl;
27673 rtx ay1 = valy1->val_entry->addr.rtl;
27674 return rtx_equal_p (ax1, ay1);
27676 case DW_OP_GNU_implicit_pointer:
27677 return valx1->val_class == dw_val_class_die_ref
27678 && valx1->val_class == valy1->val_class
27679 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
27680 && valx2->v.val_int == valy2->v.val_int;
27681 case DW_OP_GNU_entry_value:
27682 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
27683 case DW_OP_GNU_const_type:
27684 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
27685 || valx2->val_class != valy2->val_class)
27686 return false;
27687 switch (valx2->val_class)
27689 case dw_val_class_const:
27690 return valx2->v.val_int == valy2->v.val_int;
27691 case dw_val_class_vec:
27692 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
27693 && valx2->v.val_vec.length == valy2->v.val_vec.length
27694 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
27695 valx2->v.val_vec.elt_size
27696 * valx2->v.val_vec.length) == 0;
27697 case dw_val_class_const_double:
27698 return valx2->v.val_double.low == valy2->v.val_double.low
27699 && valx2->v.val_double.high == valy2->v.val_double.high;
27700 case dw_val_class_wide_int:
27701 return *valx2->v.val_wide == *valy2->v.val_wide;
27702 default:
27703 gcc_unreachable ();
27705 case DW_OP_GNU_regval_type:
27706 case DW_OP_GNU_deref_type:
27707 return valx1->v.val_int == valy1->v.val_int
27708 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
27709 case DW_OP_GNU_convert:
27710 case DW_OP_GNU_reinterpret:
27711 if (valx1->val_class != valy1->val_class)
27712 return false;
27713 if (valx1->val_class == dw_val_class_unsigned_const)
27714 return valx1->v.val_unsigned == valy1->v.val_unsigned;
27715 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27716 case DW_OP_GNU_parameter_ref:
27717 return valx1->val_class == dw_val_class_die_ref
27718 && valx1->val_class == valy1->val_class
27719 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
27720 default:
27721 /* Other codes have no operands. */
27722 return true;
27726 /* Return true if DWARF location expressions X and Y are the same. */
27728 static inline bool
27729 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
27731 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
27732 if (x->dw_loc_opc != y->dw_loc_opc
27733 || x->dtprel != y->dtprel
27734 || !compare_loc_operands (x, y))
27735 break;
27736 return x == NULL && y == NULL;
27739 /* Hashtable helpers. */
27741 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
27743 static inline hashval_t hash (const dw_loc_list_struct *);
27744 static inline bool equal (const dw_loc_list_struct *,
27745 const dw_loc_list_struct *);
27748 /* Return precomputed hash of location list X. */
27750 inline hashval_t
27751 loc_list_hasher::hash (const dw_loc_list_struct *x)
27753 return x->hash;
27756 /* Return true if location lists A and B are the same. */
27758 inline bool
27759 loc_list_hasher::equal (const dw_loc_list_struct *a,
27760 const dw_loc_list_struct *b)
27762 if (a == b)
27763 return 1;
27764 if (a->hash != b->hash)
27765 return 0;
27766 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
27767 if (strcmp (a->begin, b->begin) != 0
27768 || strcmp (a->end, b->end) != 0
27769 || (a->section == NULL) != (b->section == NULL)
27770 || (a->section && strcmp (a->section, b->section) != 0)
27771 || !compare_locs (a->expr, b->expr))
27772 break;
27773 return a == NULL && b == NULL;
27776 typedef hash_table<loc_list_hasher> loc_list_hash_type;
27779 /* Recursively optimize location lists referenced from DIE
27780 children and share them whenever possible. */
27782 static void
27783 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
27785 dw_die_ref c;
27786 dw_attr_node *a;
27787 unsigned ix;
27788 dw_loc_list_struct **slot;
27790 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27791 if (AT_class (a) == dw_val_class_loc_list)
27793 dw_loc_list_ref list = AT_loc_list (a);
27794 /* TODO: perform some optimizations here, before hashing
27795 it and storing into the hash table. */
27796 hash_loc_list (list);
27797 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
27798 if (*slot == NULL)
27799 *slot = list;
27800 else
27801 a->dw_attr_val.v.val_loc_list = *slot;
27804 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
27808 /* Recursively assign each location list a unique index into the debug_addr
27809 section. */
27811 static void
27812 index_location_lists (dw_die_ref die)
27814 dw_die_ref c;
27815 dw_attr_node *a;
27816 unsigned ix;
27818 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
27819 if (AT_class (a) == dw_val_class_loc_list)
27821 dw_loc_list_ref list = AT_loc_list (a);
27822 dw_loc_list_ref curr;
27823 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
27825 /* Don't index an entry that has already been indexed
27826 or won't be output. */
27827 if (curr->begin_entry != NULL
27828 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
27829 continue;
27831 curr->begin_entry
27832 = add_addr_table_entry (xstrdup (curr->begin),
27833 ate_kind_label);
27837 FOR_EACH_CHILD (die, c, index_location_lists (c));
27840 /* Optimize location lists referenced from DIE
27841 children and share them whenever possible. */
27843 static void
27844 optimize_location_lists (dw_die_ref die)
27846 loc_list_hash_type htab (500);
27847 optimize_location_lists_1 (die, &htab);
27850 /* Traverse the limbo die list, and add parent/child links. The only
27851 dies without parents that should be here are concrete instances of
27852 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
27853 For concrete instances, we can get the parent die from the abstract
27854 instance. */
27856 static void
27857 flush_limbo_die_list (void)
27859 limbo_die_node *node;
27861 /* get_context_die calls force_decl_die, which can put new DIEs on the
27862 limbo list in LTO mode when nested functions are put in a different
27863 partition than that of their parent function. */
27864 while ((node = limbo_die_list))
27866 dw_die_ref die = node->die;
27867 limbo_die_list = node->next;
27869 if (die->die_parent == NULL)
27871 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
27873 if (origin && origin->die_parent)
27874 add_child_die (origin->die_parent, die);
27875 else if (is_cu_die (die))
27877 else if (seen_error ())
27878 /* It's OK to be confused by errors in the input. */
27879 add_child_die (comp_unit_die (), die);
27880 else
27882 /* In certain situations, the lexical block containing a
27883 nested function can be optimized away, which results
27884 in the nested function die being orphaned. Likewise
27885 with the return type of that nested function. Force
27886 this to be a child of the containing function.
27888 It may happen that even the containing function got fully
27889 inlined and optimized out. In that case we are lost and
27890 assign the empty child. This should not be big issue as
27891 the function is likely unreachable too. */
27892 gcc_assert (node->created_for);
27894 if (DECL_P (node->created_for))
27895 origin = get_context_die (DECL_CONTEXT (node->created_for));
27896 else if (TYPE_P (node->created_for))
27897 origin = scope_die_for (node->created_for, comp_unit_die ());
27898 else
27899 origin = comp_unit_die ();
27901 add_child_die (origin, die);
27907 /* Output stuff that dwarf requires at the end of every file,
27908 and generate the DWARF-2 debugging info. */
27910 static void
27911 dwarf2out_finish (const char *)
27913 comdat_type_node *ctnode;
27914 dw_die_ref main_comp_unit_die;
27916 /* Flush out any latecomers to the limbo party. */
27917 flush_limbo_die_list ();
27919 if (flag_checking)
27921 verify_die (comp_unit_die ());
27922 for (limbo_die_node *node = cu_die_list; node; node = node->next)
27923 verify_die (node->die);
27926 /* We shouldn't have any symbols with delayed asm names for
27927 DIEs generated after early finish. */
27928 gcc_assert (deferred_asm_name == NULL);
27930 gen_remaining_tmpl_value_param_die_attribute ();
27932 #if ENABLE_ASSERT_CHECKING
27934 dw_die_ref die = comp_unit_die (), c;
27935 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
27937 #endif
27938 resolve_addr (comp_unit_die ());
27939 move_marked_base_types ();
27941 /* Initialize sections and labels used for actual assembler output. */
27942 init_sections_and_labels ();
27944 /* Traverse the DIE's and add sibling attributes to those DIE's that
27945 have children. */
27946 add_sibling_attributes (comp_unit_die ());
27947 limbo_die_node *node;
27948 for (node = cu_die_list; node; node = node->next)
27949 add_sibling_attributes (node->die);
27950 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
27951 add_sibling_attributes (ctnode->root_die);
27953 /* When splitting DWARF info, we put some attributes in the
27954 skeleton compile_unit DIE that remains in the .o, while
27955 most attributes go in the DWO compile_unit_die. */
27956 if (dwarf_split_debug_info)
27958 limbo_die_node *cu;
27959 main_comp_unit_die = gen_compile_unit_die (NULL);
27960 cu = limbo_die_list;
27961 gcc_assert (cu->die == main_comp_unit_die);
27962 limbo_die_list = limbo_die_list->next;
27963 cu->next = cu_die_list;
27964 cu_die_list = cu;
27966 else
27967 main_comp_unit_die = comp_unit_die ();
27969 /* Output a terminator label for the .text section. */
27970 switch_to_section (text_section);
27971 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
27972 if (cold_text_section)
27974 switch_to_section (cold_text_section);
27975 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
27978 /* We can only use the low/high_pc attributes if all of the code was
27979 in .text. */
27980 if (!have_multiple_function_sections
27981 || (dwarf_version < 3 && dwarf_strict))
27983 /* Don't add if the CU has no associated code. */
27984 if (text_section_used)
27985 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
27986 text_end_label, true);
27988 else
27990 unsigned fde_idx;
27991 dw_fde_ref fde;
27992 bool range_list_added = false;
27994 if (text_section_used)
27995 add_ranges_by_labels (main_comp_unit_die, text_section_label,
27996 text_end_label, &range_list_added, true);
27997 if (cold_text_section_used)
27998 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
27999 cold_end_label, &range_list_added, true);
28001 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
28003 if (DECL_IGNORED_P (fde->decl))
28004 continue;
28005 if (!fde->in_std_section)
28006 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
28007 fde->dw_fde_end, &range_list_added,
28008 true);
28009 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
28010 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
28011 fde->dw_fde_second_end, &range_list_added,
28012 true);
28015 if (range_list_added)
28017 /* We need to give .debug_loc and .debug_ranges an appropriate
28018 "base address". Use zero so that these addresses become
28019 absolute. Historically, we've emitted the unexpected
28020 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
28021 Emit both to give time for other tools to adapt. */
28022 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
28023 if (! dwarf_strict && dwarf_version < 4)
28024 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
28026 add_ranges (NULL);
28030 if (debug_info_level >= DINFO_LEVEL_TERSE)
28031 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
28032 debug_line_section_label);
28034 if (have_macinfo)
28035 add_AT_macptr (comp_unit_die (),
28036 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
28037 macinfo_section_label);
28039 if (dwarf_split_debug_info)
28041 /* optimize_location_lists calculates the size of the lists,
28042 so index them first, and assign indices to the entries.
28043 Although optimize_location_lists will remove entries from
28044 the table, it only does so for duplicates, and therefore
28045 only reduces ref_counts to 1. */
28046 index_location_lists (comp_unit_die ());
28048 if (addr_index_table != NULL)
28050 unsigned int index = 0;
28051 addr_index_table
28052 ->traverse_noresize<unsigned int *, index_addr_table_entry>
28053 (&index);
28057 if (have_location_lists)
28058 optimize_location_lists (comp_unit_die ());
28060 save_macinfo_strings ();
28062 if (dwarf_split_debug_info)
28064 unsigned int index = 0;
28066 /* Add attributes common to skeleton compile_units and
28067 type_units. Because these attributes include strings, it
28068 must be done before freezing the string table. Top-level
28069 skeleton die attrs are added when the skeleton type unit is
28070 created, so ensure it is created by this point. */
28071 add_top_level_skeleton_die_attrs (main_comp_unit_die);
28072 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
28075 /* Output all of the compilation units. We put the main one last so that
28076 the offsets are available to output_pubnames. */
28077 for (node = cu_die_list; node; node = node->next)
28078 output_comp_unit (node->die, 0);
28080 hash_table<comdat_type_hasher> comdat_type_table (100);
28081 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
28083 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
28085 /* Don't output duplicate types. */
28086 if (*slot != HTAB_EMPTY_ENTRY)
28087 continue;
28089 /* Add a pointer to the line table for the main compilation unit
28090 so that the debugger can make sense of DW_AT_decl_file
28091 attributes. */
28092 if (debug_info_level >= DINFO_LEVEL_TERSE)
28093 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
28094 (!dwarf_split_debug_info
28095 ? debug_line_section_label
28096 : debug_skeleton_line_section_label));
28098 output_comdat_type_unit (ctnode);
28099 *slot = ctnode;
28102 /* The AT_pubnames attribute needs to go in all skeleton dies, including
28103 both the main_cu and all skeleton TUs. Making this call unconditional
28104 would end up either adding a second copy of the AT_pubnames attribute, or
28105 requiring a special case in add_top_level_skeleton_die_attrs. */
28106 if (!dwarf_split_debug_info)
28107 add_AT_pubnames (comp_unit_die ());
28109 if (dwarf_split_debug_info)
28111 int mark;
28112 unsigned char checksum[16];
28113 struct md5_ctx ctx;
28115 /* Compute a checksum of the comp_unit to use as the dwo_id. */
28116 md5_init_ctx (&ctx);
28117 mark = 0;
28118 die_checksum (comp_unit_die (), &ctx, &mark);
28119 unmark_all_dies (comp_unit_die ());
28120 md5_finish_ctx (&ctx, checksum);
28122 /* Use the first 8 bytes of the checksum as the dwo_id,
28123 and add it to both comp-unit DIEs. */
28124 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
28125 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
28127 /* Add the base offset of the ranges table to the skeleton
28128 comp-unit DIE. */
28129 if (ranges_table_in_use)
28130 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
28131 ranges_section_label);
28133 switch_to_section (debug_addr_section);
28134 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
28135 output_addr_table ();
28138 /* Output the main compilation unit if non-empty or if .debug_macinfo
28139 or .debug_macro will be emitted. */
28140 output_comp_unit (comp_unit_die (), have_macinfo);
28142 if (dwarf_split_debug_info && info_section_emitted)
28143 output_skeleton_debug_sections (main_comp_unit_die);
28145 /* Output the abbreviation table. */
28146 if (abbrev_die_table_in_use != 1)
28148 switch_to_section (debug_abbrev_section);
28149 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
28150 output_abbrev_section ();
28153 /* Output location list section if necessary. */
28154 if (have_location_lists)
28156 /* Output the location lists info. */
28157 switch_to_section (debug_loc_section);
28158 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
28159 output_location_lists (comp_unit_die ());
28162 output_pubtables ();
28164 /* Output the address range information if a CU (.debug_info section)
28165 was emitted. We output an empty table even if we had no functions
28166 to put in it. This because the consumer has no way to tell the
28167 difference between an empty table that we omitted and failure to
28168 generate a table that would have contained data. */
28169 if (info_section_emitted)
28171 switch_to_section (debug_aranges_section);
28172 output_aranges ();
28175 /* Output ranges section if necessary. */
28176 if (ranges_table_in_use)
28178 switch_to_section (debug_ranges_section);
28179 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
28180 output_ranges ();
28183 /* Have to end the macro section. */
28184 if (have_macinfo)
28186 switch_to_section (debug_macinfo_section);
28187 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
28188 output_macinfo ();
28189 dw2_asm_output_data (1, 0, "End compilation unit");
28192 /* Output the source line correspondence table. We must do this
28193 even if there is no line information. Otherwise, on an empty
28194 translation unit, we will generate a present, but empty,
28195 .debug_info section. IRIX 6.5 `nm' will then complain when
28196 examining the file. This is done late so that any filenames
28197 used by the debug_info section are marked as 'used'. */
28198 switch_to_section (debug_line_section);
28199 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
28200 if (! DWARF2_ASM_LINE_DEBUG_INFO)
28201 output_line_info (false);
28203 if (dwarf_split_debug_info && info_section_emitted)
28205 switch_to_section (debug_skeleton_line_section);
28206 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
28207 output_line_info (true);
28210 /* If we emitted any indirect strings, output the string table too. */
28211 if (debug_str_hash || skeleton_debug_str_hash)
28212 output_indirect_strings ();
28215 /* Perform any cleanups needed after the early debug generation pass
28216 has run. */
28218 static void
28219 dwarf2out_early_finish (const char *filename)
28221 set_early_dwarf s;
28223 /* PCH might result in DW_AT_producer string being restored from the
28224 header compilation, so always fill it with empty string initially
28225 and overwrite only here. */
28226 dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer);
28227 producer_string = gen_producer_string ();
28228 producer->dw_attr_val.v.val_str->refcount--;
28229 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
28231 /* Add the name for the main input file now. We delayed this from
28232 dwarf2out_init to avoid complications with PCH. */
28233 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
28234 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
28235 add_comp_dir_attribute (comp_unit_die ());
28236 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
28238 bool p = false;
28239 file_table->traverse<bool *, file_table_relative_p> (&p);
28240 if (p)
28241 add_comp_dir_attribute (comp_unit_die ());
28244 /* With LTO early dwarf was really finished at compile-time, so make
28245 sure to adjust the phase after annotating the LTRANS CU DIE. */
28246 if (in_lto_p)
28248 early_dwarf_finished = true;
28249 return;
28252 /* Walk through the list of incomplete types again, trying once more to
28253 emit full debugging info for them. */
28254 retry_incomplete_types ();
28256 /* The point here is to flush out the limbo list so that it is empty
28257 and we don't need to stream it for LTO. */
28258 flush_limbo_die_list ();
28260 gen_scheduled_generic_parms_dies ();
28261 gen_remaining_tmpl_value_param_die_attribute ();
28263 /* Add DW_AT_linkage_name for all deferred DIEs. */
28264 for (limbo_die_node *node = deferred_asm_name; node; node = node->next)
28266 tree decl = node->created_for;
28267 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
28268 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
28269 ended up in deferred_asm_name before we knew it was
28270 constant and never written to disk. */
28271 && DECL_ASSEMBLER_NAME (decl))
28273 add_linkage_attr (node->die, decl);
28274 move_linkage_attr (node->die);
28277 deferred_asm_name = NULL;
28279 if (flag_eliminate_unused_debug_types)
28280 prune_unused_types ();
28282 /* Generate separate COMDAT sections for type DIEs. */
28283 if (use_debug_types)
28285 break_out_comdat_types (comp_unit_die ());
28287 /* Each new type_unit DIE was added to the limbo die list when created.
28288 Since these have all been added to comdat_type_list, clear the
28289 limbo die list. */
28290 limbo_die_list = NULL;
28292 /* For each new comdat type unit, copy declarations for incomplete
28293 types to make the new unit self-contained (i.e., no direct
28294 references to the main compile unit). */
28295 for (comdat_type_node *ctnode = comdat_type_list;
28296 ctnode != NULL; ctnode = ctnode->next)
28297 copy_decls_for_unworthy_types (ctnode->root_die);
28298 copy_decls_for_unworthy_types (comp_unit_die ());
28300 /* In the process of copying declarations from one unit to another,
28301 we may have left some declarations behind that are no longer
28302 referenced. Prune them. */
28303 prune_unused_types ();
28306 /* Generate separate CUs for each of the include files we've seen.
28307 They will go into limbo_die_list and from there to cu_die_list. */
28308 if (flag_eliminate_dwarf2_dups)
28310 gcc_assert (limbo_die_list == NULL);
28311 break_out_includes (comp_unit_die ());
28312 limbo_die_node *cu;
28313 while ((cu = limbo_die_list))
28315 limbo_die_list = cu->next;
28316 cu->next = cu_die_list;
28317 cu_die_list = cu;
28321 /* The early debug phase is now finished. */
28322 early_dwarf_finished = true;
28325 /* Reset all state within dwarf2out.c so that we can rerun the compiler
28326 within the same process. For use by toplev::finalize. */
28328 void
28329 dwarf2out_c_finalize (void)
28331 last_var_location_insn = NULL;
28332 cached_next_real_insn = NULL;
28333 used_rtx_array = NULL;
28334 incomplete_types = NULL;
28335 decl_scope_table = NULL;
28336 debug_info_section = NULL;
28337 debug_skeleton_info_section = NULL;
28338 debug_abbrev_section = NULL;
28339 debug_skeleton_abbrev_section = NULL;
28340 debug_aranges_section = NULL;
28341 debug_addr_section = NULL;
28342 debug_macinfo_section = NULL;
28343 debug_line_section = NULL;
28344 debug_skeleton_line_section = NULL;
28345 debug_loc_section = NULL;
28346 debug_pubnames_section = NULL;
28347 debug_pubtypes_section = NULL;
28348 debug_str_section = NULL;
28349 debug_str_dwo_section = NULL;
28350 debug_str_offsets_section = NULL;
28351 debug_ranges_section = NULL;
28352 debug_frame_section = NULL;
28353 fde_vec = NULL;
28354 debug_str_hash = NULL;
28355 skeleton_debug_str_hash = NULL;
28356 dw2_string_counter = 0;
28357 have_multiple_function_sections = false;
28358 text_section_used = false;
28359 cold_text_section_used = false;
28360 cold_text_section = NULL;
28361 current_unit_personality = NULL;
28363 next_die_offset = 0;
28364 single_comp_unit_die = NULL;
28365 comdat_type_list = NULL;
28366 limbo_die_list = NULL;
28367 file_table = NULL;
28368 decl_die_table = NULL;
28369 common_block_die_table = NULL;
28370 decl_loc_table = NULL;
28371 call_arg_locations = NULL;
28372 call_arg_loc_last = NULL;
28373 call_site_count = -1;
28374 tail_call_site_count = -1;
28375 cached_dw_loc_list_table = NULL;
28376 abbrev_die_table = NULL;
28377 abbrev_die_table_allocated = 0;
28378 abbrev_die_table_in_use = 0;
28379 delete dwarf_proc_stack_usage_map;
28380 dwarf_proc_stack_usage_map = NULL;
28381 line_info_label_num = 0;
28382 cur_line_info_table = NULL;
28383 text_section_line_info = NULL;
28384 cold_text_section_line_info = NULL;
28385 separate_line_info = NULL;
28386 info_section_emitted = false;
28387 pubname_table = NULL;
28388 pubtype_table = NULL;
28389 macinfo_table = NULL;
28390 ranges_table = NULL;
28391 ranges_table_allocated = 0;
28392 ranges_table_in_use = 0;
28393 ranges_by_label = 0;
28394 ranges_by_label_allocated = 0;
28395 ranges_by_label_in_use = 0;
28396 have_location_lists = false;
28397 loclabel_num = 0;
28398 poc_label_num = 0;
28399 last_emitted_file = NULL;
28400 label_num = 0;
28401 tmpl_value_parm_die_table = NULL;
28402 generic_type_instances = NULL;
28403 frame_pointer_fb_offset = 0;
28404 frame_pointer_fb_offset_valid = false;
28405 base_types.release ();
28406 XDELETEVEC (producer_string);
28407 producer_string = NULL;
28410 #include "gt-dwarf2out.h"