Merge trunk version 214779 into gupc branch.
[official-gcc.git] / gcc / dwarf2out.c
blob400207ca5949b3266fac5758a06b14a91d304707
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2014 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 "tm.h"
62 #include "rtl.h"
63 #include "tree.h"
64 #include "tree-upc.h"
65 #include "stringpool.h"
66 #include "stor-layout.h"
67 #include "varasm.h"
68 #include "function.h"
69 #include "emit-rtl.h"
70 #include "hash-table.h"
71 #include "version.h"
72 #include "flags.h"
73 #include "hard-reg-set.h"
74 #include "regs.h"
75 #include "rtlhash.h"
76 #include "insn-config.h"
77 #include "reload.h"
78 #include "function.h"
79 #include "output.h"
80 #include "expr.h"
81 #include "except.h"
82 #include "dwarf2.h"
83 #include "dwarf2out.h"
84 #include "dwarf2asm.h"
85 #include "toplev.h"
86 #include "md5.h"
87 #include "tm_p.h"
88 #include "diagnostic.h"
89 #include "tree-pretty-print.h"
90 #include "debug.h"
91 #include "target.h"
92 #include "common/common-target.h"
93 #include "langhooks.h"
94 #include "cgraph.h"
95 #include "input.h"
96 #include "ira.h"
97 #include "lra.h"
98 #include "dumpfile.h"
99 #include "opts.h"
100 #include "tree-dfa.h"
101 #include "gdb/gdb-index.h"
102 #include "rtl-iter.h"
104 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
105 static rtx_insn *last_var_location_insn;
106 static rtx_insn *cached_next_real_insn;
107 static void dwarf2out_decl (tree);
109 #ifdef VMS_DEBUGGING_INFO
110 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
112 /* Define this macro to be a nonzero value if the directory specifications
113 which are output in the debug info should end with a separator. */
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
115 /* Define this macro to evaluate to a nonzero value if GCC should refrain
116 from generating indirect strings in DWARF2 debug information, for instance
117 if your target is stuck with an old version of GDB that is unable to
118 process them properly or uses VMS Debug. */
119 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
120 #else
121 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
122 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
123 #endif
125 /* ??? Poison these here until it can be done generically. They've been
126 totally replaced in this file; make sure it stays that way. */
127 #undef DWARF2_UNWIND_INFO
128 #undef DWARF2_FRAME_INFO
129 #if (GCC_VERSION >= 3000)
130 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
131 #endif
133 /* The size of the target's pointer type. */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
138 /* Array of RTXes referenced by the debugging information, which therefore
139 must be kept around forever. */
140 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
142 /* A pointer to the base of a list of incomplete types which might be
143 completed at some later time. incomplete_types_list needs to be a
144 vec<tree, va_gc> *because we want to tell the garbage collector about
145 it. */
146 static GTY(()) vec<tree, va_gc> *incomplete_types;
148 /* A pointer to the base of a table of references to declaration
149 scopes. This table is a display which tracks the nesting
150 of declaration scopes at the current scope and containing
151 scopes. This table is used to find the proper place to
152 define type declaration DIE's. */
153 static GTY(()) vec<tree, va_gc> *decl_scope_table;
155 /* Pointers to various DWARF2 sections. */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_skeleton_info_section;
158 static GTY(()) section *debug_abbrev_section;
159 static GTY(()) section *debug_skeleton_abbrev_section;
160 static GTY(()) section *debug_aranges_section;
161 static GTY(()) section *debug_addr_section;
162 static GTY(()) section *debug_macinfo_section;
163 static 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(()) indirect_string_node {
210 const char *str;
211 unsigned int refcount;
212 enum dwarf_form form;
213 char *label;
214 unsigned int index;
217 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
219 /* With split_debug_info, both the comp_dir and dwo_name go in the
220 main object file, rather than the dwo, similar to the force_direct
221 parameter elsewhere but with additional complications:
223 1) The string is needed in both the main object file and the dwo.
224 That is, the comp_dir and dwo_name will appear in both places.
226 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
227 DW_FORM_GNU_str_index.
229 3) GCC chooses the form to use late, depending on the size and
230 reference count.
232 Rather than forcing the all debug string handling functions and
233 callers to deal with these complications, simply use a separate,
234 special-cased string table for any attribute that should go in the
235 main object file. This limits the complexity to just the places
236 that need it. */
238 static GTY ((param_is (struct indirect_string_node)))
239 htab_t skeleton_debug_str_hash;
241 static GTY(()) int dw2_string_counter;
243 /* True if the compilation unit places functions in more than one section. */
244 static GTY(()) bool have_multiple_function_sections = false;
246 /* Whether the default text and cold text sections have been used at all. */
248 static GTY(()) bool text_section_used = false;
249 static GTY(()) bool cold_text_section_used = false;
251 /* The default cold text section. */
252 static GTY(()) section *cold_text_section;
254 /* The DIE for C++14 'auto' in a function return type. */
255 static GTY(()) dw_die_ref auto_die;
257 /* The DIE for C++14 'decltype(auto)' in a function return type. */
258 static GTY(()) dw_die_ref decltype_auto_die;
260 /* Forward declarations for functions defined in this file. */
262 static char *stripattributes (const char *);
263 static void output_call_frame_info (int);
264 static void dwarf2out_note_section_used (void);
266 /* Personality decl of current unit. Used only when assembler does not support
267 personality CFI. */
268 static GTY(()) rtx current_unit_personality;
270 /* Data and reference forms for relocatable data. */
271 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
272 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
274 #ifndef DEBUG_FRAME_SECTION
275 #define DEBUG_FRAME_SECTION ".debug_frame"
276 #endif
278 #ifndef FUNC_BEGIN_LABEL
279 #define FUNC_BEGIN_LABEL "LFB"
280 #endif
282 #ifndef FUNC_END_LABEL
283 #define FUNC_END_LABEL "LFE"
284 #endif
286 #ifndef PROLOGUE_END_LABEL
287 #define PROLOGUE_END_LABEL "LPE"
288 #endif
290 #ifndef EPILOGUE_BEGIN_LABEL
291 #define EPILOGUE_BEGIN_LABEL "LEB"
292 #endif
294 #ifndef FRAME_BEGIN_LABEL
295 #define FRAME_BEGIN_LABEL "Lframe"
296 #endif
297 #define CIE_AFTER_SIZE_LABEL "LSCIE"
298 #define CIE_END_LABEL "LECIE"
299 #define FDE_LABEL "LSFDE"
300 #define FDE_AFTER_SIZE_LABEL "LASFDE"
301 #define FDE_END_LABEL "LEFDE"
302 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
303 #define LINE_NUMBER_END_LABEL "LELT"
304 #define LN_PROLOG_AS_LABEL "LASLTP"
305 #define LN_PROLOG_END_LABEL "LELTP"
306 #define DIE_LABEL_PREFIX "DW"
308 /* Match the base name of a file to the base name of a compilation unit. */
310 static int
311 matches_main_base (const char *path)
313 /* Cache the last query. */
314 static const char *last_path = NULL;
315 static int last_match = 0;
316 if (path != last_path)
318 const char *base;
319 int length = base_of_path (path, &base);
320 last_path = path;
321 last_match = (length == main_input_baselength
322 && memcmp (base, main_input_basename, length) == 0);
324 return last_match;
327 #ifdef DEBUG_DEBUG_STRUCT
329 static int
330 dump_struct_debug (tree type, enum debug_info_usage usage,
331 enum debug_struct_file criterion, int generic,
332 int matches, int result)
334 /* Find the type name. */
335 tree type_decl = TYPE_STUB_DECL (type);
336 tree t = type_decl;
337 const char *name = 0;
338 if (TREE_CODE (t) == TYPE_DECL)
339 t = DECL_NAME (t);
340 if (t)
341 name = IDENTIFIER_POINTER (t);
343 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
344 criterion,
345 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
346 matches ? "bas" : "hdr",
347 generic ? "gen" : "ord",
348 usage == DINFO_USAGE_DFN ? ";" :
349 usage == DINFO_USAGE_DIR_USE ? "." : "*",
350 result,
351 (void*) type_decl, name);
352 return result;
354 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
355 dump_struct_debug (type, usage, criterion, generic, matches, result)
357 #else
359 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
360 (result)
362 #endif
364 /* Get the number of HOST_WIDE_INTs needed to represent the precision
365 of the number. */
367 static unsigned int
368 get_full_len (const wide_int &op)
370 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
371 / HOST_BITS_PER_WIDE_INT);
374 static bool
375 should_emit_struct_debug (tree type, enum debug_info_usage usage)
377 enum debug_struct_file criterion;
378 tree type_decl;
379 bool generic = lang_hooks.types.generic_p (type);
381 if (generic)
382 criterion = debug_struct_generic[usage];
383 else
384 criterion = debug_struct_ordinary[usage];
386 if (criterion == DINFO_STRUCT_FILE_NONE)
387 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
388 if (criterion == DINFO_STRUCT_FILE_ANY)
389 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
391 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
393 if (type_decl != NULL)
395 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
396 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
398 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
399 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
405 /* Return a pointer to a copy of the section string name S with all
406 attributes stripped off, and an asterisk prepended (for assemble_name). */
408 static inline char *
409 stripattributes (const char *s)
411 char *stripped = XNEWVEC (char, strlen (s) + 2);
412 char *p = stripped;
414 *p++ = '*';
416 while (*s && *s != ',')
417 *p++ = *s++;
419 *p = '\0';
420 return stripped;
423 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
424 switch to the data section instead, and write out a synthetic start label
425 for collect2 the first time around. */
427 static void
428 switch_to_eh_frame_section (bool back)
430 tree label;
432 #ifdef EH_FRAME_SECTION_NAME
433 if (eh_frame_section == 0)
435 int flags;
437 if (EH_TABLES_CAN_BE_READ_ONLY)
439 int fde_encoding;
440 int per_encoding;
441 int lsda_encoding;
443 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
444 /*global=*/0);
445 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
446 /*global=*/1);
447 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
448 /*global=*/0);
449 flags = ((! flag_pic
450 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
451 && (fde_encoding & 0x70) != DW_EH_PE_aligned
452 && (per_encoding & 0x70) != DW_EH_PE_absptr
453 && (per_encoding & 0x70) != DW_EH_PE_aligned
454 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
455 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
456 ? 0 : SECTION_WRITE);
458 else
459 flags = SECTION_WRITE;
460 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
462 #endif /* EH_FRAME_SECTION_NAME */
464 if (eh_frame_section)
465 switch_to_section (eh_frame_section);
466 else
468 /* We have no special eh_frame section. Put the information in
469 the data section and emit special labels to guide collect2. */
470 switch_to_section (data_section);
472 if (!back)
474 label = get_file_function_name ("F");
475 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
476 targetm.asm_out.globalize_label (asm_out_file,
477 IDENTIFIER_POINTER (label));
478 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
483 /* Switch [BACK] to the eh or debug frame table section, depending on
484 FOR_EH. */
486 static void
487 switch_to_frame_table_section (int for_eh, bool back)
489 if (for_eh)
490 switch_to_eh_frame_section (back);
491 else
493 if (!debug_frame_section)
494 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
495 SECTION_DEBUG, NULL);
496 switch_to_section (debug_frame_section);
500 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
502 enum dw_cfi_oprnd_type
503 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
505 switch (cfi)
507 case DW_CFA_nop:
508 case DW_CFA_GNU_window_save:
509 case DW_CFA_remember_state:
510 case DW_CFA_restore_state:
511 return dw_cfi_oprnd_unused;
513 case DW_CFA_set_loc:
514 case DW_CFA_advance_loc1:
515 case DW_CFA_advance_loc2:
516 case DW_CFA_advance_loc4:
517 case DW_CFA_MIPS_advance_loc8:
518 return dw_cfi_oprnd_addr;
520 case DW_CFA_offset:
521 case DW_CFA_offset_extended:
522 case DW_CFA_def_cfa:
523 case DW_CFA_offset_extended_sf:
524 case DW_CFA_def_cfa_sf:
525 case DW_CFA_restore:
526 case DW_CFA_restore_extended:
527 case DW_CFA_undefined:
528 case DW_CFA_same_value:
529 case DW_CFA_def_cfa_register:
530 case DW_CFA_register:
531 case DW_CFA_expression:
532 return dw_cfi_oprnd_reg_num;
534 case DW_CFA_def_cfa_offset:
535 case DW_CFA_GNU_args_size:
536 case DW_CFA_def_cfa_offset_sf:
537 return dw_cfi_oprnd_offset;
539 case DW_CFA_def_cfa_expression:
540 return dw_cfi_oprnd_loc;
542 default:
543 gcc_unreachable ();
547 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
549 enum dw_cfi_oprnd_type
550 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
552 switch (cfi)
554 case DW_CFA_def_cfa:
555 case DW_CFA_def_cfa_sf:
556 case DW_CFA_offset:
557 case DW_CFA_offset_extended_sf:
558 case DW_CFA_offset_extended:
559 return dw_cfi_oprnd_offset;
561 case DW_CFA_register:
562 return dw_cfi_oprnd_reg_num;
564 case DW_CFA_expression:
565 return dw_cfi_oprnd_loc;
567 default:
568 return dw_cfi_oprnd_unused;
572 /* Output one FDE. */
574 static void
575 output_fde (dw_fde_ref fde, bool for_eh, bool second,
576 char *section_start_label, int fde_encoding, char *augmentation,
577 bool any_lsda_needed, int lsda_encoding)
579 const char *begin, *end;
580 static unsigned int j;
581 char l1[20], l2[20];
583 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
584 /* empty */ 0);
585 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
586 for_eh + j);
587 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
588 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
589 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
590 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
591 " indicating 64-bit DWARF extension");
592 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
593 "FDE Length");
594 ASM_OUTPUT_LABEL (asm_out_file, l1);
596 if (for_eh)
597 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
598 else
599 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
600 debug_frame_section, "FDE CIE offset");
602 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
603 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
605 if (for_eh)
607 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
608 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
609 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
610 "FDE initial location");
611 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
612 end, begin, "FDE address range");
614 else
616 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
617 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
620 if (augmentation[0])
622 if (any_lsda_needed)
624 int size = size_of_encoded_value (lsda_encoding);
626 if (lsda_encoding == DW_EH_PE_aligned)
628 int offset = ( 4 /* Length */
629 + 4 /* CIE offset */
630 + 2 * size_of_encoded_value (fde_encoding)
631 + 1 /* Augmentation size */ );
632 int pad = -offset & (PTR_SIZE - 1);
634 size += pad;
635 gcc_assert (size_of_uleb128 (size) == 1);
638 dw2_asm_output_data_uleb128 (size, "Augmentation size");
640 if (fde->uses_eh_lsda)
642 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
643 fde->funcdef_number);
644 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
645 gen_rtx_SYMBOL_REF (Pmode, l1),
646 false,
647 "Language Specific Data Area");
649 else
651 if (lsda_encoding == DW_EH_PE_aligned)
652 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
653 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
654 "Language Specific Data Area (none)");
657 else
658 dw2_asm_output_data_uleb128 (0, "Augmentation size");
661 /* Loop through the Call Frame Instructions associated with this FDE. */
662 fde->dw_fde_current_label = begin;
664 size_t from, until, i;
666 from = 0;
667 until = vec_safe_length (fde->dw_fde_cfi);
669 if (fde->dw_fde_second_begin == NULL)
671 else if (!second)
672 until = fde->dw_fde_switch_cfi_index;
673 else
674 from = fde->dw_fde_switch_cfi_index;
676 for (i = from; i < until; i++)
677 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
680 /* If we are to emit a ref/link from function bodies to their frame tables,
681 do it now. This is typically performed to make sure that tables
682 associated with functions are dragged with them and not discarded in
683 garbage collecting links. We need to do this on a per function basis to
684 cope with -ffunction-sections. */
686 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
687 /* Switch to the function section, emit the ref to the tables, and
688 switch *back* into the table section. */
689 switch_to_section (function_section (fde->decl));
690 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
691 switch_to_frame_table_section (for_eh, true);
692 #endif
694 /* Pad the FDE out to an address sized boundary. */
695 ASM_OUTPUT_ALIGN (asm_out_file,
696 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
697 ASM_OUTPUT_LABEL (asm_out_file, l2);
699 j += 2;
702 /* Return true if frame description entry FDE is needed for EH. */
704 static bool
705 fde_needed_for_eh_p (dw_fde_ref fde)
707 if (flag_asynchronous_unwind_tables)
708 return true;
710 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
711 return true;
713 if (fde->uses_eh_lsda)
714 return true;
716 /* If exceptions are enabled, we have collected nothrow info. */
717 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
718 return false;
720 return true;
723 /* Output the call frame information used to record information
724 that relates to calculating the frame pointer, and records the
725 location of saved registers. */
727 static void
728 output_call_frame_info (int for_eh)
730 unsigned int i;
731 dw_fde_ref fde;
732 dw_cfi_ref cfi;
733 char l1[20], l2[20], section_start_label[20];
734 bool any_lsda_needed = false;
735 char augmentation[6];
736 int augmentation_size;
737 int fde_encoding = DW_EH_PE_absptr;
738 int per_encoding = DW_EH_PE_absptr;
739 int lsda_encoding = DW_EH_PE_absptr;
740 int return_reg;
741 rtx personality = NULL;
742 int dw_cie_version;
744 /* Don't emit a CIE if there won't be any FDEs. */
745 if (!fde_vec)
746 return;
748 /* Nothing to do if the assembler's doing it all. */
749 if (dwarf2out_do_cfi_asm ())
750 return;
752 /* If we don't have any functions we'll want to unwind out of, don't emit
753 any EH unwind information. If we make FDEs linkonce, we may have to
754 emit an empty label for an FDE that wouldn't otherwise be emitted. We
755 want to avoid having an FDE kept around when the function it refers to
756 is discarded. Example where this matters: a primary function template
757 in C++ requires EH information, an explicit specialization doesn't. */
758 if (for_eh)
760 bool any_eh_needed = false;
762 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
764 if (fde->uses_eh_lsda)
765 any_eh_needed = any_lsda_needed = true;
766 else if (fde_needed_for_eh_p (fde))
767 any_eh_needed = true;
768 else if (TARGET_USES_WEAK_UNWIND_INFO)
769 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
772 if (!any_eh_needed)
773 return;
776 /* We're going to be generating comments, so turn on app. */
777 if (flag_debug_asm)
778 app_enable ();
780 /* Switch to the proper frame section, first time. */
781 switch_to_frame_table_section (for_eh, false);
783 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
784 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
786 /* Output the CIE. */
787 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
788 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
789 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
790 dw2_asm_output_data (4, 0xffffffff,
791 "Initial length escape value indicating 64-bit DWARF extension");
792 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
793 "Length of Common Information Entry");
794 ASM_OUTPUT_LABEL (asm_out_file, l1);
796 /* Now that the CIE pointer is PC-relative for EH,
797 use 0 to identify the CIE. */
798 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
799 (for_eh ? 0 : DWARF_CIE_ID),
800 "CIE Identifier Tag");
802 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
803 use CIE version 1, unless that would produce incorrect results
804 due to overflowing the return register column. */
805 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
806 dw_cie_version = 1;
807 if (return_reg >= 256 || dwarf_version > 2)
808 dw_cie_version = 3;
809 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
811 augmentation[0] = 0;
812 augmentation_size = 0;
814 personality = current_unit_personality;
815 if (for_eh)
817 char *p;
819 /* Augmentation:
820 z Indicates that a uleb128 is present to size the
821 augmentation section.
822 L Indicates the encoding (and thus presence) of
823 an LSDA pointer in the FDE augmentation.
824 R Indicates a non-default pointer encoding for
825 FDE code pointers.
826 P Indicates the presence of an encoding + language
827 personality routine in the CIE augmentation. */
829 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
830 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
831 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
833 p = augmentation + 1;
834 if (personality)
836 *p++ = 'P';
837 augmentation_size += 1 + size_of_encoded_value (per_encoding);
838 assemble_external_libcall (personality);
840 if (any_lsda_needed)
842 *p++ = 'L';
843 augmentation_size += 1;
845 if (fde_encoding != DW_EH_PE_absptr)
847 *p++ = 'R';
848 augmentation_size += 1;
850 if (p > augmentation + 1)
852 augmentation[0] = 'z';
853 *p = '\0';
856 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
857 if (personality && per_encoding == DW_EH_PE_aligned)
859 int offset = ( 4 /* Length */
860 + 4 /* CIE Id */
861 + 1 /* CIE version */
862 + strlen (augmentation) + 1 /* Augmentation */
863 + size_of_uleb128 (1) /* Code alignment */
864 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
865 + 1 /* RA column */
866 + 1 /* Augmentation size */
867 + 1 /* Personality encoding */ );
868 int pad = -offset & (PTR_SIZE - 1);
870 augmentation_size += pad;
872 /* Augmentations should be small, so there's scarce need to
873 iterate for a solution. Die if we exceed one uleb128 byte. */
874 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
878 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
879 if (dw_cie_version >= 4)
881 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
882 dw2_asm_output_data (1, 0, "CIE Segment Size");
884 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
885 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
886 "CIE Data Alignment Factor");
888 if (dw_cie_version == 1)
889 dw2_asm_output_data (1, return_reg, "CIE RA Column");
890 else
891 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
893 if (augmentation[0])
895 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
896 if (personality)
898 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
899 eh_data_format_name (per_encoding));
900 dw2_asm_output_encoded_addr_rtx (per_encoding,
901 personality,
902 true, NULL);
905 if (any_lsda_needed)
906 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
907 eh_data_format_name (lsda_encoding));
909 if (fde_encoding != DW_EH_PE_absptr)
910 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
911 eh_data_format_name (fde_encoding));
914 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
915 output_cfi (cfi, NULL, for_eh);
917 /* Pad the CIE out to an address sized boundary. */
918 ASM_OUTPUT_ALIGN (asm_out_file,
919 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
920 ASM_OUTPUT_LABEL (asm_out_file, l2);
922 /* Loop through all of the FDE's. */
923 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
925 unsigned int k;
927 /* Don't emit EH unwind info for leaf functions that don't need it. */
928 if (for_eh && !fde_needed_for_eh_p (fde))
929 continue;
931 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
932 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
933 augmentation, any_lsda_needed, lsda_encoding);
936 if (for_eh && targetm.terminate_dw2_eh_frame_info)
937 dw2_asm_output_data (4, 0, "End of Table");
939 /* Turn off app to make assembly quicker. */
940 if (flag_debug_asm)
941 app_disable ();
944 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
946 static void
947 dwarf2out_do_cfi_startproc (bool second)
949 int enc;
950 rtx ref;
951 rtx personality = get_personality_function (current_function_decl);
953 fprintf (asm_out_file, "\t.cfi_startproc\n");
955 if (personality)
957 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
958 ref = personality;
960 /* ??? The GAS support isn't entirely consistent. We have to
961 handle indirect support ourselves, but PC-relative is done
962 in the assembler. Further, the assembler can't handle any
963 of the weirder relocation types. */
964 if (enc & DW_EH_PE_indirect)
965 ref = dw2_force_const_mem (ref, true);
967 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
968 output_addr_const (asm_out_file, ref);
969 fputc ('\n', asm_out_file);
972 if (crtl->uses_eh_lsda)
974 char lab[20];
976 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
977 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
978 current_function_funcdef_no);
979 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
980 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
982 if (enc & DW_EH_PE_indirect)
983 ref = dw2_force_const_mem (ref, true);
985 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
986 output_addr_const (asm_out_file, ref);
987 fputc ('\n', asm_out_file);
991 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
992 this allocation may be done before pass_final. */
994 dw_fde_ref
995 dwarf2out_alloc_current_fde (void)
997 dw_fde_ref fde;
999 fde = ggc_cleared_alloc<dw_fde_node> ();
1000 fde->decl = current_function_decl;
1001 fde->funcdef_number = current_function_funcdef_no;
1002 fde->fde_index = vec_safe_length (fde_vec);
1003 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1004 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1005 fde->nothrow = crtl->nothrow;
1006 fde->drap_reg = INVALID_REGNUM;
1007 fde->vdrap_reg = INVALID_REGNUM;
1009 /* Record the FDE associated with this function. */
1010 cfun->fde = fde;
1011 vec_safe_push (fde_vec, fde);
1013 return fde;
1016 /* Output a marker (i.e. a label) for the beginning of a function, before
1017 the prologue. */
1019 void
1020 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1021 const char *file ATTRIBUTE_UNUSED)
1023 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1024 char * dup_label;
1025 dw_fde_ref fde;
1026 section *fnsec;
1027 bool do_frame;
1029 current_function_func_begin_label = NULL;
1031 do_frame = dwarf2out_do_frame ();
1033 /* ??? current_function_func_begin_label is also used by except.c for
1034 call-site information. We must emit this label if it might be used. */
1035 if (!do_frame
1036 && (!flag_exceptions
1037 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1038 return;
1040 fnsec = function_section (current_function_decl);
1041 switch_to_section (fnsec);
1042 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1043 current_function_funcdef_no);
1044 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1045 current_function_funcdef_no);
1046 dup_label = xstrdup (label);
1047 current_function_func_begin_label = dup_label;
1049 /* We can elide the fde allocation if we're not emitting debug info. */
1050 if (!do_frame)
1051 return;
1053 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1054 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1055 would include pass_dwarf2_frame. If we've not created the FDE yet,
1056 do so now. */
1057 fde = cfun->fde;
1058 if (fde == NULL)
1059 fde = dwarf2out_alloc_current_fde ();
1061 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1062 fde->dw_fde_begin = dup_label;
1063 fde->dw_fde_current_label = dup_label;
1064 fde->in_std_section = (fnsec == text_section
1065 || (cold_text_section && fnsec == cold_text_section));
1067 /* We only want to output line number information for the genuine dwarf2
1068 prologue case, not the eh frame case. */
1069 #ifdef DWARF2_DEBUGGING_INFO
1070 if (file)
1071 dwarf2out_source_line (line, file, 0, true);
1072 #endif
1074 if (dwarf2out_do_cfi_asm ())
1075 dwarf2out_do_cfi_startproc (false);
1076 else
1078 rtx personality = get_personality_function (current_function_decl);
1079 if (!current_unit_personality)
1080 current_unit_personality = personality;
1082 /* We cannot keep a current personality per function as without CFI
1083 asm, at the point where we emit the CFI data, there is no current
1084 function anymore. */
1085 if (personality && current_unit_personality != personality)
1086 sorry ("multiple EH personalities are supported only with assemblers "
1087 "supporting .cfi_personality directive");
1091 /* Output a marker (i.e. a label) for the end of the generated code
1092 for a function prologue. This gets called *after* the prologue code has
1093 been generated. */
1095 void
1096 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1097 const char *file ATTRIBUTE_UNUSED)
1099 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1101 /* Output a label to mark the endpoint of the code generated for this
1102 function. */
1103 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1104 current_function_funcdef_no);
1105 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1106 current_function_funcdef_no);
1107 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1110 /* Output a marker (i.e. a label) for the beginning of the generated code
1111 for a function epilogue. This gets called *before* the prologue code has
1112 been generated. */
1114 void
1115 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1116 const char *file ATTRIBUTE_UNUSED)
1118 dw_fde_ref fde = cfun->fde;
1119 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1121 if (fde->dw_fde_vms_begin_epilogue)
1122 return;
1124 /* Output a label to mark the endpoint of the code generated for this
1125 function. */
1126 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1127 current_function_funcdef_no);
1128 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1129 current_function_funcdef_no);
1130 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1133 /* Output a marker (i.e. a label) for the absolute end of the generated code
1134 for a function definition. This gets called *after* the epilogue code has
1135 been generated. */
1137 void
1138 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1139 const char *file ATTRIBUTE_UNUSED)
1141 dw_fde_ref fde;
1142 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1144 last_var_location_insn = NULL;
1145 cached_next_real_insn = NULL;
1147 if (dwarf2out_do_cfi_asm ())
1148 fprintf (asm_out_file, "\t.cfi_endproc\n");
1150 /* Output a label to mark the endpoint of the code generated for this
1151 function. */
1152 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1153 current_function_funcdef_no);
1154 ASM_OUTPUT_LABEL (asm_out_file, label);
1155 fde = cfun->fde;
1156 gcc_assert (fde != NULL);
1157 if (fde->dw_fde_second_begin == NULL)
1158 fde->dw_fde_end = xstrdup (label);
1161 void
1162 dwarf2out_frame_finish (void)
1164 /* Output call frame information. */
1165 if (targetm.debug_unwind_info () == UI_DWARF2)
1166 output_call_frame_info (0);
1168 /* Output another copy for the unwinder. */
1169 if ((flag_unwind_tables || flag_exceptions)
1170 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1171 output_call_frame_info (1);
1174 /* Note that the current function section is being used for code. */
1176 static void
1177 dwarf2out_note_section_used (void)
1179 section *sec = current_function_section ();
1180 if (sec == text_section)
1181 text_section_used = true;
1182 else if (sec == cold_text_section)
1183 cold_text_section_used = true;
1186 static void var_location_switch_text_section (void);
1187 static void set_cur_line_info_table (section *);
1189 void
1190 dwarf2out_switch_text_section (void)
1192 section *sect;
1193 dw_fde_ref fde = cfun->fde;
1195 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1197 if (!in_cold_section_p)
1199 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1200 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1201 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1203 else
1205 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1206 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1207 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1209 have_multiple_function_sections = true;
1211 /* There is no need to mark used sections when not debugging. */
1212 if (cold_text_section != NULL)
1213 dwarf2out_note_section_used ();
1215 if (dwarf2out_do_cfi_asm ())
1216 fprintf (asm_out_file, "\t.cfi_endproc\n");
1218 /* Now do the real section switch. */
1219 sect = current_function_section ();
1220 switch_to_section (sect);
1222 fde->second_in_std_section
1223 = (sect == text_section
1224 || (cold_text_section && sect == cold_text_section));
1226 if (dwarf2out_do_cfi_asm ())
1227 dwarf2out_do_cfi_startproc (true);
1229 var_location_switch_text_section ();
1231 if (cold_text_section != NULL)
1232 set_cur_line_info_table (sect);
1235 /* And now, the subset of the debugging information support code necessary
1236 for emitting location expressions. */
1238 /* Data about a single source file. */
1239 struct GTY(()) dwarf_file_data {
1240 const char * filename;
1241 int emitted_number;
1244 typedef struct GTY(()) deferred_locations_struct
1246 tree variable;
1247 dw_die_ref die;
1248 } deferred_locations;
1251 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1254 /* Describe an entry into the .debug_addr section. */
1256 enum ate_kind {
1257 ate_kind_rtx,
1258 ate_kind_rtx_dtprel,
1259 ate_kind_label
1262 typedef struct GTY(()) addr_table_entry_struct {
1263 enum ate_kind kind;
1264 unsigned int refcount;
1265 unsigned int index;
1266 union addr_table_entry_struct_union
1268 rtx GTY ((tag ("0"))) rtl;
1269 char * GTY ((tag ("1"))) label;
1271 GTY ((desc ("%1.kind"))) addr;
1273 addr_table_entry;
1275 /* Location lists are ranges + location descriptions for that range,
1276 so you can track variables that are in different places over
1277 their entire life. */
1278 typedef struct GTY(()) dw_loc_list_struct {
1279 dw_loc_list_ref dw_loc_next;
1280 const char *begin; /* Label and addr_entry for start of range */
1281 addr_table_entry *begin_entry;
1282 const char *end; /* Label for end of range */
1283 char *ll_symbol; /* Label for beginning of location list.
1284 Only on head of list */
1285 const char *section; /* Section this loclist is relative to */
1286 dw_loc_descr_ref expr;
1287 hashval_t hash;
1288 /* True if all addresses in this and subsequent lists are known to be
1289 resolved. */
1290 bool resolved_addr;
1291 /* True if this list has been replaced by dw_loc_next. */
1292 bool replaced;
1293 bool emitted;
1294 /* True if the range should be emitted even if begin and end
1295 are the same. */
1296 bool force;
1297 } dw_loc_list_node;
1299 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1301 /* Convert a DWARF stack opcode into its string name. */
1303 static const char *
1304 dwarf_stack_op_name (unsigned int op)
1306 const char *name = get_DW_OP_name (op);
1308 if (name != NULL)
1309 return name;
1311 return "OP_<unknown>";
1314 /* Return a pointer to a newly allocated location description. Location
1315 descriptions are simple expression terms that can be strung
1316 together to form more complicated location (address) descriptions. */
1318 static inline dw_loc_descr_ref
1319 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1320 unsigned HOST_WIDE_INT oprnd2)
1322 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1324 descr->dw_loc_opc = op;
1325 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1326 descr->dw_loc_oprnd1.val_entry = NULL;
1327 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1328 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd2.val_entry = NULL;
1330 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1332 return descr;
1335 /* Return a pointer to a newly allocated location description for
1336 REG and OFFSET. */
1338 static inline dw_loc_descr_ref
1339 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1341 if (reg <= 31)
1342 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1343 offset, 0);
1344 else
1345 return new_loc_descr (DW_OP_bregx, reg, offset);
1348 /* Add a location description term to a location description expression. */
1350 static inline void
1351 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1353 dw_loc_descr_ref *d;
1355 /* Find the end of the chain. */
1356 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1359 *d = descr;
1362 /* Compare two location operands for exact equality. */
1364 static bool
1365 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1367 if (a->val_class != b->val_class)
1368 return false;
1369 switch (a->val_class)
1371 case dw_val_class_none:
1372 return true;
1373 case dw_val_class_addr:
1374 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1376 case dw_val_class_offset:
1377 case dw_val_class_unsigned_const:
1378 case dw_val_class_const:
1379 case dw_val_class_range_list:
1380 case dw_val_class_lineptr:
1381 case dw_val_class_macptr:
1382 /* These are all HOST_WIDE_INT, signed or unsigned. */
1383 return a->v.val_unsigned == b->v.val_unsigned;
1385 case dw_val_class_loc:
1386 return a->v.val_loc == b->v.val_loc;
1387 case dw_val_class_loc_list:
1388 return a->v.val_loc_list == b->v.val_loc_list;
1389 case dw_val_class_die_ref:
1390 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1391 case dw_val_class_fde_ref:
1392 return a->v.val_fde_index == b->v.val_fde_index;
1393 case dw_val_class_lbl_id:
1394 case dw_val_class_high_pc:
1395 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1396 case dw_val_class_str:
1397 return a->v.val_str == b->v.val_str;
1398 case dw_val_class_flag:
1399 return a->v.val_flag == b->v.val_flag;
1400 case dw_val_class_file:
1401 return a->v.val_file == b->v.val_file;
1402 case dw_val_class_decl_ref:
1403 return a->v.val_decl_ref == b->v.val_decl_ref;
1405 case dw_val_class_const_double:
1406 return (a->v.val_double.high == b->v.val_double.high
1407 && a->v.val_double.low == b->v.val_double.low);
1409 case dw_val_class_wide_int:
1410 return *a->v.val_wide == *b->v.val_wide;
1412 case dw_val_class_vec:
1414 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1415 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1417 return (a_len == b_len
1418 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1421 case dw_val_class_data8:
1422 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1424 case dw_val_class_vms_delta:
1425 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1426 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1428 gcc_unreachable ();
1431 /* Compare two location atoms for exact equality. */
1433 static bool
1434 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1436 if (a->dw_loc_opc != b->dw_loc_opc)
1437 return false;
1439 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1440 address size, but since we always allocate cleared storage it
1441 should be zero for other types of locations. */
1442 if (a->dtprel != b->dtprel)
1443 return false;
1445 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1446 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1449 /* Compare two complete location expressions for exact equality. */
1451 bool
1452 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1454 while (1)
1456 if (a == b)
1457 return true;
1458 if (a == NULL || b == NULL)
1459 return false;
1460 if (!loc_descr_equal_p_1 (a, b))
1461 return false;
1463 a = a->dw_loc_next;
1464 b = b->dw_loc_next;
1469 /* Add a constant OFFSET to a location expression. */
1471 static void
1472 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1474 dw_loc_descr_ref loc;
1475 HOST_WIDE_INT *p;
1477 gcc_assert (*list_head != NULL);
1479 if (!offset)
1480 return;
1482 /* Find the end of the chain. */
1483 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1486 p = NULL;
1487 if (loc->dw_loc_opc == DW_OP_fbreg
1488 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1489 p = &loc->dw_loc_oprnd1.v.val_int;
1490 else if (loc->dw_loc_opc == DW_OP_bregx)
1491 p = &loc->dw_loc_oprnd2.v.val_int;
1493 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1494 offset. Don't optimize if an signed integer overflow would happen. */
1495 if (p != NULL
1496 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1497 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1498 *p += offset;
1500 else if (offset > 0)
1501 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1503 else
1505 loc->dw_loc_next = int_loc_descriptor (-offset);
1506 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1510 /* Add a constant OFFSET to a location list. */
1512 static void
1513 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1515 dw_loc_list_ref d;
1516 for (d = list_head; d != NULL; d = d->dw_loc_next)
1517 loc_descr_plus_const (&d->expr, offset);
1520 #define DWARF_REF_SIZE \
1521 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1523 static unsigned long int get_base_type_offset (dw_die_ref);
1525 /* Return the size of a location descriptor. */
1527 static unsigned long
1528 size_of_loc_descr (dw_loc_descr_ref loc)
1530 unsigned long size = 1;
1532 switch (loc->dw_loc_opc)
1534 case DW_OP_addr:
1535 size += DWARF2_ADDR_SIZE;
1536 break;
1537 case DW_OP_GNU_addr_index:
1538 case DW_OP_GNU_const_index:
1539 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1540 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1541 break;
1542 case DW_OP_const1u:
1543 case DW_OP_const1s:
1544 size += 1;
1545 break;
1546 case DW_OP_const2u:
1547 case DW_OP_const2s:
1548 size += 2;
1549 break;
1550 case DW_OP_const4u:
1551 case DW_OP_const4s:
1552 size += 4;
1553 break;
1554 case DW_OP_const8u:
1555 case DW_OP_const8s:
1556 size += 8;
1557 break;
1558 case DW_OP_constu:
1559 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1560 break;
1561 case DW_OP_consts:
1562 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1563 break;
1564 case DW_OP_pick:
1565 size += 1;
1566 break;
1567 case DW_OP_plus_uconst:
1568 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1569 break;
1570 case DW_OP_skip:
1571 case DW_OP_bra:
1572 size += 2;
1573 break;
1574 case DW_OP_breg0:
1575 case DW_OP_breg1:
1576 case DW_OP_breg2:
1577 case DW_OP_breg3:
1578 case DW_OP_breg4:
1579 case DW_OP_breg5:
1580 case DW_OP_breg6:
1581 case DW_OP_breg7:
1582 case DW_OP_breg8:
1583 case DW_OP_breg9:
1584 case DW_OP_breg10:
1585 case DW_OP_breg11:
1586 case DW_OP_breg12:
1587 case DW_OP_breg13:
1588 case DW_OP_breg14:
1589 case DW_OP_breg15:
1590 case DW_OP_breg16:
1591 case DW_OP_breg17:
1592 case DW_OP_breg18:
1593 case DW_OP_breg19:
1594 case DW_OP_breg20:
1595 case DW_OP_breg21:
1596 case DW_OP_breg22:
1597 case DW_OP_breg23:
1598 case DW_OP_breg24:
1599 case DW_OP_breg25:
1600 case DW_OP_breg26:
1601 case DW_OP_breg27:
1602 case DW_OP_breg28:
1603 case DW_OP_breg29:
1604 case DW_OP_breg30:
1605 case DW_OP_breg31:
1606 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1607 break;
1608 case DW_OP_regx:
1609 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1610 break;
1611 case DW_OP_fbreg:
1612 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1613 break;
1614 case DW_OP_bregx:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1617 break;
1618 case DW_OP_piece:
1619 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1620 break;
1621 case DW_OP_bit_piece:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1624 break;
1625 case DW_OP_deref_size:
1626 case DW_OP_xderef_size:
1627 size += 1;
1628 break;
1629 case DW_OP_call2:
1630 size += 2;
1631 break;
1632 case DW_OP_call4:
1633 size += 4;
1634 break;
1635 case DW_OP_call_ref:
1636 size += DWARF_REF_SIZE;
1637 break;
1638 case DW_OP_implicit_value:
1639 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1640 + loc->dw_loc_oprnd1.v.val_unsigned;
1641 break;
1642 case DW_OP_GNU_implicit_pointer:
1643 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1644 break;
1645 case DW_OP_GNU_entry_value:
1647 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1648 size += size_of_uleb128 (op_size) + op_size;
1649 break;
1651 case DW_OP_GNU_const_type:
1653 unsigned long o
1654 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1655 size += size_of_uleb128 (o) + 1;
1656 switch (loc->dw_loc_oprnd2.val_class)
1658 case dw_val_class_vec:
1659 size += loc->dw_loc_oprnd2.v.val_vec.length
1660 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1661 break;
1662 case dw_val_class_const:
1663 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1664 break;
1665 case dw_val_class_const_double:
1666 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1667 break;
1668 case dw_val_class_wide_int:
1669 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1670 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1671 break;
1672 default:
1673 gcc_unreachable ();
1675 break;
1677 case DW_OP_GNU_regval_type:
1679 unsigned long o
1680 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1681 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1682 + size_of_uleb128 (o);
1684 break;
1685 case DW_OP_GNU_deref_type:
1687 unsigned long o
1688 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1689 size += 1 + size_of_uleb128 (o);
1691 break;
1692 case DW_OP_GNU_convert:
1693 case DW_OP_GNU_reinterpret:
1694 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1695 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1696 else
1698 unsigned long o
1699 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1700 size += size_of_uleb128 (o);
1702 break;
1703 case DW_OP_GNU_parameter_ref:
1704 size += 4;
1705 break;
1706 default:
1707 break;
1710 return size;
1713 /* Return the size of a series of location descriptors. */
1715 unsigned long
1716 size_of_locs (dw_loc_descr_ref loc)
1718 dw_loc_descr_ref l;
1719 unsigned long size;
1721 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1722 field, to avoid writing to a PCH file. */
1723 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1725 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1726 break;
1727 size += size_of_loc_descr (l);
1729 if (! l)
1730 return size;
1732 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1734 l->dw_loc_addr = size;
1735 size += size_of_loc_descr (l);
1738 return size;
1741 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1742 static void get_ref_die_offset_label (char *, dw_die_ref);
1743 static unsigned long int get_ref_die_offset (dw_die_ref);
1745 /* Output location description stack opcode's operands (if any).
1746 The for_eh_or_skip parameter controls whether register numbers are
1747 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1748 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1749 info). This should be suppressed for the cases that have not been converted
1750 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1752 static void
1753 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1755 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1756 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1758 switch (loc->dw_loc_opc)
1760 #ifdef DWARF2_DEBUGGING_INFO
1761 case DW_OP_const2u:
1762 case DW_OP_const2s:
1763 dw2_asm_output_data (2, val1->v.val_int, NULL);
1764 break;
1765 case DW_OP_const4u:
1766 if (loc->dtprel)
1768 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1769 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1770 val1->v.val_addr);
1771 fputc ('\n', asm_out_file);
1772 break;
1774 /* FALLTHRU */
1775 case DW_OP_const4s:
1776 dw2_asm_output_data (4, val1->v.val_int, NULL);
1777 break;
1778 case DW_OP_const8u:
1779 if (loc->dtprel)
1781 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1782 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1783 val1->v.val_addr);
1784 fputc ('\n', asm_out_file);
1785 break;
1787 /* FALLTHRU */
1788 case DW_OP_const8s:
1789 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1790 dw2_asm_output_data (8, val1->v.val_int, NULL);
1791 break;
1792 case DW_OP_skip:
1793 case DW_OP_bra:
1795 int offset;
1797 gcc_assert (val1->val_class == dw_val_class_loc);
1798 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1800 dw2_asm_output_data (2, offset, NULL);
1802 break;
1803 case DW_OP_implicit_value:
1804 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1805 switch (val2->val_class)
1807 case dw_val_class_const:
1808 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1809 break;
1810 case dw_val_class_vec:
1812 unsigned int elt_size = val2->v.val_vec.elt_size;
1813 unsigned int len = val2->v.val_vec.length;
1814 unsigned int i;
1815 unsigned char *p;
1817 if (elt_size > sizeof (HOST_WIDE_INT))
1819 elt_size /= 2;
1820 len *= 2;
1822 for (i = 0, p = val2->v.val_vec.array;
1823 i < len;
1824 i++, p += elt_size)
1825 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1826 "fp or vector constant word %u", i);
1828 break;
1829 case dw_val_class_const_double:
1831 unsigned HOST_WIDE_INT first, second;
1833 if (WORDS_BIG_ENDIAN)
1835 first = val2->v.val_double.high;
1836 second = val2->v.val_double.low;
1838 else
1840 first = val2->v.val_double.low;
1841 second = val2->v.val_double.high;
1843 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1844 first, NULL);
1845 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1846 second, NULL);
1848 break;
1849 case dw_val_class_wide_int:
1851 int i;
1852 int len = get_full_len (*val2->v.val_wide);
1853 if (WORDS_BIG_ENDIAN)
1854 for (i = len - 1; i >= 0; --i)
1855 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1856 val2->v.val_wide->elt (i), NULL);
1857 else
1858 for (i = 0; i < len; ++i)
1859 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1860 val2->v.val_wide->elt (i), NULL);
1862 break;
1863 case dw_val_class_addr:
1864 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1865 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1866 break;
1867 default:
1868 gcc_unreachable ();
1870 break;
1871 #else
1872 case DW_OP_const2u:
1873 case DW_OP_const2s:
1874 case DW_OP_const4u:
1875 case DW_OP_const4s:
1876 case DW_OP_const8u:
1877 case DW_OP_const8s:
1878 case DW_OP_skip:
1879 case DW_OP_bra:
1880 case DW_OP_implicit_value:
1881 /* We currently don't make any attempt to make sure these are
1882 aligned properly like we do for the main unwind info, so
1883 don't support emitting things larger than a byte if we're
1884 only doing unwinding. */
1885 gcc_unreachable ();
1886 #endif
1887 case DW_OP_const1u:
1888 case DW_OP_const1s:
1889 dw2_asm_output_data (1, val1->v.val_int, NULL);
1890 break;
1891 case DW_OP_constu:
1892 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1893 break;
1894 case DW_OP_consts:
1895 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1896 break;
1897 case DW_OP_pick:
1898 dw2_asm_output_data (1, val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_plus_uconst:
1901 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1902 break;
1903 case DW_OP_breg0:
1904 case DW_OP_breg1:
1905 case DW_OP_breg2:
1906 case DW_OP_breg3:
1907 case DW_OP_breg4:
1908 case DW_OP_breg5:
1909 case DW_OP_breg6:
1910 case DW_OP_breg7:
1911 case DW_OP_breg8:
1912 case DW_OP_breg9:
1913 case DW_OP_breg10:
1914 case DW_OP_breg11:
1915 case DW_OP_breg12:
1916 case DW_OP_breg13:
1917 case DW_OP_breg14:
1918 case DW_OP_breg15:
1919 case DW_OP_breg16:
1920 case DW_OP_breg17:
1921 case DW_OP_breg18:
1922 case DW_OP_breg19:
1923 case DW_OP_breg20:
1924 case DW_OP_breg21:
1925 case DW_OP_breg22:
1926 case DW_OP_breg23:
1927 case DW_OP_breg24:
1928 case DW_OP_breg25:
1929 case DW_OP_breg26:
1930 case DW_OP_breg27:
1931 case DW_OP_breg28:
1932 case DW_OP_breg29:
1933 case DW_OP_breg30:
1934 case DW_OP_breg31:
1935 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1936 break;
1937 case DW_OP_regx:
1939 unsigned r = val1->v.val_unsigned;
1940 if (for_eh_or_skip >= 0)
1941 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1942 gcc_assert (size_of_uleb128 (r)
1943 == size_of_uleb128 (val1->v.val_unsigned));
1944 dw2_asm_output_data_uleb128 (r, NULL);
1946 break;
1947 case DW_OP_fbreg:
1948 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1949 break;
1950 case DW_OP_bregx:
1952 unsigned r = val1->v.val_unsigned;
1953 if (for_eh_or_skip >= 0)
1954 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1955 gcc_assert (size_of_uleb128 (r)
1956 == size_of_uleb128 (val1->v.val_unsigned));
1957 dw2_asm_output_data_uleb128 (r, NULL);
1958 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1960 break;
1961 case DW_OP_piece:
1962 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1963 break;
1964 case DW_OP_bit_piece:
1965 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1966 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1967 break;
1968 case DW_OP_deref_size:
1969 case DW_OP_xderef_size:
1970 dw2_asm_output_data (1, val1->v.val_int, NULL);
1971 break;
1973 case DW_OP_addr:
1974 if (loc->dtprel)
1976 if (targetm.asm_out.output_dwarf_dtprel)
1978 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1979 DWARF2_ADDR_SIZE,
1980 val1->v.val_addr);
1981 fputc ('\n', asm_out_file);
1983 else
1984 gcc_unreachable ();
1986 else
1988 #ifdef DWARF2_DEBUGGING_INFO
1989 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1990 #else
1991 gcc_unreachable ();
1992 #endif
1994 break;
1996 case DW_OP_GNU_addr_index:
1997 case DW_OP_GNU_const_index:
1998 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1999 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2000 "(index into .debug_addr)");
2001 break;
2003 case DW_OP_GNU_implicit_pointer:
2005 char label[MAX_ARTIFICIAL_LABEL_BYTES
2006 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2007 gcc_assert (val1->val_class == dw_val_class_die_ref);
2008 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2009 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2010 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2012 break;
2014 case DW_OP_GNU_entry_value:
2015 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2016 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2017 break;
2019 case DW_OP_GNU_const_type:
2021 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2022 gcc_assert (o);
2023 dw2_asm_output_data_uleb128 (o, NULL);
2024 switch (val2->val_class)
2026 case dw_val_class_const:
2027 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2028 dw2_asm_output_data (1, l, NULL);
2029 dw2_asm_output_data (l, val2->v.val_int, NULL);
2030 break;
2031 case dw_val_class_vec:
2033 unsigned int elt_size = val2->v.val_vec.elt_size;
2034 unsigned int len = val2->v.val_vec.length;
2035 unsigned int i;
2036 unsigned char *p;
2038 l = len * elt_size;
2039 dw2_asm_output_data (1, l, NULL);
2040 if (elt_size > sizeof (HOST_WIDE_INT))
2042 elt_size /= 2;
2043 len *= 2;
2045 for (i = 0, p = val2->v.val_vec.array;
2046 i < len;
2047 i++, p += elt_size)
2048 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2049 "fp or vector constant word %u", i);
2051 break;
2052 case dw_val_class_const_double:
2054 unsigned HOST_WIDE_INT first, second;
2055 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2057 dw2_asm_output_data (1, 2 * l, NULL);
2058 if (WORDS_BIG_ENDIAN)
2060 first = val2->v.val_double.high;
2061 second = val2->v.val_double.low;
2063 else
2065 first = val2->v.val_double.low;
2066 second = val2->v.val_double.high;
2068 dw2_asm_output_data (l, first, NULL);
2069 dw2_asm_output_data (l, second, NULL);
2071 break;
2072 case dw_val_class_wide_int:
2074 int i;
2075 int len = get_full_len (*val2->v.val_wide);
2076 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2078 dw2_asm_output_data (1, len * l, NULL);
2079 if (WORDS_BIG_ENDIAN)
2080 for (i = len - 1; i >= 0; --i)
2081 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2082 else
2083 for (i = 0; i < len; ++i)
2084 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2086 break;
2087 default:
2088 gcc_unreachable ();
2091 break;
2092 case DW_OP_GNU_regval_type:
2094 unsigned r = val1->v.val_unsigned;
2095 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2096 gcc_assert (o);
2097 if (for_eh_or_skip >= 0)
2099 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2100 gcc_assert (size_of_uleb128 (r)
2101 == size_of_uleb128 (val1->v.val_unsigned));
2103 dw2_asm_output_data_uleb128 (r, NULL);
2104 dw2_asm_output_data_uleb128 (o, NULL);
2106 break;
2107 case DW_OP_GNU_deref_type:
2109 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2110 gcc_assert (o);
2111 dw2_asm_output_data (1, val1->v.val_int, NULL);
2112 dw2_asm_output_data_uleb128 (o, NULL);
2114 break;
2115 case DW_OP_GNU_convert:
2116 case DW_OP_GNU_reinterpret:
2117 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2118 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2119 else
2121 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2122 gcc_assert (o);
2123 dw2_asm_output_data_uleb128 (o, NULL);
2125 break;
2127 case DW_OP_GNU_parameter_ref:
2129 unsigned long o;
2130 gcc_assert (val1->val_class == dw_val_class_die_ref);
2131 o = get_ref_die_offset (val1->v.val_die_ref.die);
2132 dw2_asm_output_data (4, o, NULL);
2134 break;
2136 default:
2137 /* Other codes have no operands. */
2138 break;
2142 /* Output a sequence of location operations.
2143 The for_eh_or_skip parameter controls whether register numbers are
2144 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2145 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2146 info). This should be suppressed for the cases that have not been converted
2147 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2149 void
2150 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2152 for (; loc != NULL; loc = loc->dw_loc_next)
2154 enum dwarf_location_atom opc = loc->dw_loc_opc;
2155 /* Output the opcode. */
2156 if (for_eh_or_skip >= 0
2157 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2159 unsigned r = (opc - DW_OP_breg0);
2160 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2161 gcc_assert (r <= 31);
2162 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2164 else if (for_eh_or_skip >= 0
2165 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2167 unsigned r = (opc - DW_OP_reg0);
2168 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2169 gcc_assert (r <= 31);
2170 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2173 dw2_asm_output_data (1, opc,
2174 "%s", dwarf_stack_op_name (opc));
2176 /* Output the operand(s) (if any). */
2177 output_loc_operands (loc, for_eh_or_skip);
2181 /* Output location description stack opcode's operands (if any).
2182 The output is single bytes on a line, suitable for .cfi_escape. */
2184 static void
2185 output_loc_operands_raw (dw_loc_descr_ref loc)
2187 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2188 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2190 switch (loc->dw_loc_opc)
2192 case DW_OP_addr:
2193 case DW_OP_GNU_addr_index:
2194 case DW_OP_GNU_const_index:
2195 case DW_OP_implicit_value:
2196 /* We cannot output addresses in .cfi_escape, only bytes. */
2197 gcc_unreachable ();
2199 case DW_OP_const1u:
2200 case DW_OP_const1s:
2201 case DW_OP_pick:
2202 case DW_OP_deref_size:
2203 case DW_OP_xderef_size:
2204 fputc (',', asm_out_file);
2205 dw2_asm_output_data_raw (1, val1->v.val_int);
2206 break;
2208 case DW_OP_const2u:
2209 case DW_OP_const2s:
2210 fputc (',', asm_out_file);
2211 dw2_asm_output_data_raw (2, val1->v.val_int);
2212 break;
2214 case DW_OP_const4u:
2215 case DW_OP_const4s:
2216 fputc (',', asm_out_file);
2217 dw2_asm_output_data_raw (4, val1->v.val_int);
2218 break;
2220 case DW_OP_const8u:
2221 case DW_OP_const8s:
2222 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2223 fputc (',', asm_out_file);
2224 dw2_asm_output_data_raw (8, val1->v.val_int);
2225 break;
2227 case DW_OP_skip:
2228 case DW_OP_bra:
2230 int offset;
2232 gcc_assert (val1->val_class == dw_val_class_loc);
2233 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2235 fputc (',', asm_out_file);
2236 dw2_asm_output_data_raw (2, offset);
2238 break;
2240 case DW_OP_regx:
2242 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2243 gcc_assert (size_of_uleb128 (r)
2244 == size_of_uleb128 (val1->v.val_unsigned));
2245 fputc (',', asm_out_file);
2246 dw2_asm_output_data_uleb128_raw (r);
2248 break;
2250 case DW_OP_constu:
2251 case DW_OP_plus_uconst:
2252 case DW_OP_piece:
2253 fputc (',', asm_out_file);
2254 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2255 break;
2257 case DW_OP_bit_piece:
2258 fputc (',', asm_out_file);
2259 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2260 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2261 break;
2263 case DW_OP_consts:
2264 case DW_OP_breg0:
2265 case DW_OP_breg1:
2266 case DW_OP_breg2:
2267 case DW_OP_breg3:
2268 case DW_OP_breg4:
2269 case DW_OP_breg5:
2270 case DW_OP_breg6:
2271 case DW_OP_breg7:
2272 case DW_OP_breg8:
2273 case DW_OP_breg9:
2274 case DW_OP_breg10:
2275 case DW_OP_breg11:
2276 case DW_OP_breg12:
2277 case DW_OP_breg13:
2278 case DW_OP_breg14:
2279 case DW_OP_breg15:
2280 case DW_OP_breg16:
2281 case DW_OP_breg17:
2282 case DW_OP_breg18:
2283 case DW_OP_breg19:
2284 case DW_OP_breg20:
2285 case DW_OP_breg21:
2286 case DW_OP_breg22:
2287 case DW_OP_breg23:
2288 case DW_OP_breg24:
2289 case DW_OP_breg25:
2290 case DW_OP_breg26:
2291 case DW_OP_breg27:
2292 case DW_OP_breg28:
2293 case DW_OP_breg29:
2294 case DW_OP_breg30:
2295 case DW_OP_breg31:
2296 case DW_OP_fbreg:
2297 fputc (',', asm_out_file);
2298 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2299 break;
2301 case DW_OP_bregx:
2303 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2304 gcc_assert (size_of_uleb128 (r)
2305 == size_of_uleb128 (val1->v.val_unsigned));
2306 fputc (',', asm_out_file);
2307 dw2_asm_output_data_uleb128_raw (r);
2308 fputc (',', asm_out_file);
2309 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2311 break;
2313 case DW_OP_GNU_implicit_pointer:
2314 case DW_OP_GNU_entry_value:
2315 case DW_OP_GNU_const_type:
2316 case DW_OP_GNU_regval_type:
2317 case DW_OP_GNU_deref_type:
2318 case DW_OP_GNU_convert:
2319 case DW_OP_GNU_reinterpret:
2320 case DW_OP_GNU_parameter_ref:
2321 gcc_unreachable ();
2322 break;
2324 default:
2325 /* Other codes have no operands. */
2326 break;
2330 void
2331 output_loc_sequence_raw (dw_loc_descr_ref loc)
2333 while (1)
2335 enum dwarf_location_atom opc = loc->dw_loc_opc;
2336 /* Output the opcode. */
2337 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2339 unsigned r = (opc - DW_OP_breg0);
2340 r = DWARF2_FRAME_REG_OUT (r, 1);
2341 gcc_assert (r <= 31);
2342 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2344 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2346 unsigned r = (opc - DW_OP_reg0);
2347 r = DWARF2_FRAME_REG_OUT (r, 1);
2348 gcc_assert (r <= 31);
2349 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2351 /* Output the opcode. */
2352 fprintf (asm_out_file, "%#x", opc);
2353 output_loc_operands_raw (loc);
2355 if (!loc->dw_loc_next)
2356 break;
2357 loc = loc->dw_loc_next;
2359 fputc (',', asm_out_file);
2363 /* This function builds a dwarf location descriptor sequence from a
2364 dw_cfa_location, adding the given OFFSET to the result of the
2365 expression. */
2367 struct dw_loc_descr_node *
2368 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2370 struct dw_loc_descr_node *head, *tmp;
2372 offset += cfa->offset;
2374 if (cfa->indirect)
2376 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2377 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2378 head->dw_loc_oprnd1.val_entry = NULL;
2379 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2380 add_loc_descr (&head, tmp);
2381 if (offset != 0)
2383 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2384 add_loc_descr (&head, tmp);
2387 else
2388 head = new_reg_loc_descr (cfa->reg, offset);
2390 return head;
2393 /* This function builds a dwarf location descriptor sequence for
2394 the address at OFFSET from the CFA when stack is aligned to
2395 ALIGNMENT byte. */
2397 struct dw_loc_descr_node *
2398 build_cfa_aligned_loc (dw_cfa_location *cfa,
2399 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2401 struct dw_loc_descr_node *head;
2402 unsigned int dwarf_fp
2403 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2405 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2406 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2408 head = new_reg_loc_descr (dwarf_fp, 0);
2409 add_loc_descr (&head, int_loc_descriptor (alignment));
2410 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2411 loc_descr_plus_const (&head, offset);
2413 else
2414 head = new_reg_loc_descr (dwarf_fp, offset);
2415 return head;
2418 /* And now, the support for symbolic debugging information. */
2420 /* .debug_str support. */
2421 static int output_indirect_string (void **, void *);
2423 static void dwarf2out_init (const char *);
2424 static void dwarf2out_finish (const char *);
2425 static void dwarf2out_assembly_start (void);
2426 static void dwarf2out_define (unsigned int, const char *);
2427 static void dwarf2out_undef (unsigned int, const char *);
2428 static void dwarf2out_start_source_file (unsigned, const char *);
2429 static void dwarf2out_end_source_file (unsigned);
2430 static void dwarf2out_function_decl (tree);
2431 static void dwarf2out_begin_block (unsigned, unsigned);
2432 static void dwarf2out_end_block (unsigned, unsigned);
2433 static bool dwarf2out_ignore_block (const_tree);
2434 static void dwarf2out_global_decl (tree);
2435 static void dwarf2out_type_decl (tree, int);
2436 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2437 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2438 dw_die_ref);
2439 static void dwarf2out_abstract_function (tree);
2440 static void dwarf2out_var_location (rtx_insn *);
2441 static void dwarf2out_begin_function (tree);
2442 static void dwarf2out_end_function (unsigned int);
2443 static void dwarf2out_set_name (tree, tree);
2445 /* The debug hooks structure. */
2447 const struct gcc_debug_hooks dwarf2_debug_hooks =
2449 dwarf2out_init,
2450 dwarf2out_finish,
2451 dwarf2out_assembly_start,
2452 dwarf2out_define,
2453 dwarf2out_undef,
2454 dwarf2out_start_source_file,
2455 dwarf2out_end_source_file,
2456 dwarf2out_begin_block,
2457 dwarf2out_end_block,
2458 dwarf2out_ignore_block,
2459 dwarf2out_source_line,
2460 dwarf2out_begin_prologue,
2461 #if VMS_DEBUGGING_INFO
2462 dwarf2out_vms_end_prologue,
2463 dwarf2out_vms_begin_epilogue,
2464 #else
2465 debug_nothing_int_charstar,
2466 debug_nothing_int_charstar,
2467 #endif
2468 dwarf2out_end_epilogue,
2469 dwarf2out_begin_function,
2470 dwarf2out_end_function, /* end_function */
2471 dwarf2out_function_decl, /* function_decl */
2472 dwarf2out_global_decl,
2473 dwarf2out_type_decl, /* type_decl */
2474 dwarf2out_imported_module_or_decl,
2475 debug_nothing_tree, /* deferred_inline_function */
2476 /* The DWARF 2 backend tries to reduce debugging bloat by not
2477 emitting the abstract description of inline functions until
2478 something tries to reference them. */
2479 dwarf2out_abstract_function, /* outlining_inline_function */
2480 debug_nothing_rtx_code_label, /* label */
2481 debug_nothing_int, /* handle_pch */
2482 dwarf2out_var_location,
2483 dwarf2out_switch_text_section,
2484 dwarf2out_set_name,
2485 1, /* start_end_main_source_file */
2486 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2489 /* NOTE: In the comments in this file, many references are made to
2490 "Debugging Information Entries". This term is abbreviated as `DIE'
2491 throughout the remainder of this file. */
2493 /* An internal representation of the DWARF output is built, and then
2494 walked to generate the DWARF debugging info. The walk of the internal
2495 representation is done after the entire program has been compiled.
2496 The types below are used to describe the internal representation. */
2498 /* Whether to put type DIEs into their own section .debug_types instead
2499 of making them part of the .debug_info section. Only supported for
2500 Dwarf V4 or higher and the user didn't disable them through
2501 -fno-debug-types-section. It is more efficient to put them in a
2502 separate comdat sections since the linker will then be able to
2503 remove duplicates. But not all tools support .debug_types sections
2504 yet. */
2506 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2508 /* Various DIE's use offsets relative to the beginning of the
2509 .debug_info section to refer to each other. */
2511 typedef long int dw_offset;
2513 /* Define typedefs here to avoid circular dependencies. */
2515 typedef struct dw_attr_struct *dw_attr_ref;
2516 typedef struct dw_line_info_struct *dw_line_info_ref;
2517 typedef struct pubname_struct *pubname_ref;
2518 typedef struct dw_ranges_struct *dw_ranges_ref;
2519 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2520 typedef struct comdat_type_struct *comdat_type_node_ref;
2522 /* The entries in the line_info table more-or-less mirror the opcodes
2523 that are used in the real dwarf line table. Arrays of these entries
2524 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2525 supported. */
2527 enum dw_line_info_opcode {
2528 /* Emit DW_LNE_set_address; the operand is the label index. */
2529 LI_set_address,
2531 /* Emit a row to the matrix with the given line. This may be done
2532 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2533 special opcodes. */
2534 LI_set_line,
2536 /* Emit a DW_LNS_set_file. */
2537 LI_set_file,
2539 /* Emit a DW_LNS_set_column. */
2540 LI_set_column,
2542 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2543 LI_negate_stmt,
2545 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2546 LI_set_prologue_end,
2547 LI_set_epilogue_begin,
2549 /* Emit a DW_LNE_set_discriminator. */
2550 LI_set_discriminator
2553 typedef struct GTY(()) dw_line_info_struct {
2554 enum dw_line_info_opcode opcode;
2555 unsigned int val;
2556 } dw_line_info_entry;
2559 typedef struct GTY(()) dw_line_info_table_struct {
2560 /* The label that marks the end of this section. */
2561 const char *end_label;
2563 /* The values for the last row of the matrix, as collected in the table.
2564 These are used to minimize the changes to the next row. */
2565 unsigned int file_num;
2566 unsigned int line_num;
2567 unsigned int column_num;
2568 int discrim_num;
2569 bool is_stmt;
2570 bool in_use;
2572 vec<dw_line_info_entry, va_gc> *entries;
2573 } dw_line_info_table;
2575 typedef dw_line_info_table *dw_line_info_table_p;
2578 /* Each DIE attribute has a field specifying the attribute kind,
2579 a link to the next attribute in the chain, and an attribute value.
2580 Attributes are typically linked below the DIE they modify. */
2582 typedef struct GTY(()) dw_attr_struct {
2583 enum dwarf_attribute dw_attr;
2584 dw_val_node dw_attr_val;
2586 dw_attr_node;
2589 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2590 The children of each node form a circular list linked by
2591 die_sib. die_child points to the node *before* the "first" child node. */
2593 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2594 union die_symbol_or_type_node
2596 const char * GTY ((tag ("0"))) die_symbol;
2597 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2599 GTY ((desc ("%0.comdat_type_p"))) die_id;
2600 vec<dw_attr_node, va_gc> *die_attr;
2601 dw_die_ref die_parent;
2602 dw_die_ref die_child;
2603 dw_die_ref die_sib;
2604 dw_die_ref die_definition; /* ref from a specification to its definition */
2605 dw_offset die_offset;
2606 unsigned long die_abbrev;
2607 int die_mark;
2608 unsigned int decl_id;
2609 enum dwarf_tag die_tag;
2610 /* Die is used and must not be pruned as unused. */
2611 BOOL_BITFIELD die_perennial_p : 1;
2612 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2613 /* Lots of spare bits. */
2615 die_node;
2617 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2618 #define FOR_EACH_CHILD(die, c, expr) do { \
2619 c = die->die_child; \
2620 if (c) do { \
2621 c = c->die_sib; \
2622 expr; \
2623 } while (c != die->die_child); \
2624 } while (0)
2626 /* The pubname structure */
2628 typedef struct GTY(()) pubname_struct {
2629 dw_die_ref die;
2630 const char *name;
2632 pubname_entry;
2635 struct GTY(()) dw_ranges_struct {
2636 /* If this is positive, it's a block number, otherwise it's a
2637 bitwise-negated index into dw_ranges_by_label. */
2638 int num;
2641 /* A structure to hold a macinfo entry. */
2643 typedef struct GTY(()) macinfo_struct {
2644 unsigned char code;
2645 unsigned HOST_WIDE_INT lineno;
2646 const char *info;
2648 macinfo_entry;
2651 struct GTY(()) dw_ranges_by_label_struct {
2652 const char *begin;
2653 const char *end;
2656 /* The comdat type node structure. */
2657 typedef struct GTY(()) comdat_type_struct
2659 dw_die_ref root_die;
2660 dw_die_ref type_die;
2661 dw_die_ref skeleton_die;
2662 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2663 struct comdat_type_struct *next;
2665 comdat_type_node;
2667 /* The limbo die list structure. */
2668 typedef struct GTY(()) limbo_die_struct {
2669 dw_die_ref die;
2670 tree created_for;
2671 struct limbo_die_struct *next;
2673 limbo_die_node;
2675 typedef struct skeleton_chain_struct
2677 dw_die_ref old_die;
2678 dw_die_ref new_die;
2679 struct skeleton_chain_struct *parent;
2681 skeleton_chain_node;
2683 /* Define a macro which returns nonzero for a TYPE_DECL which was
2684 implicitly generated for a type.
2686 Note that, unlike the C front-end (which generates a NULL named
2687 TYPE_DECL node for each complete tagged type, each array type,
2688 and each function type node created) the C++ front-end generates
2689 a _named_ TYPE_DECL node for each tagged type node created.
2690 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2691 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2692 front-end, but for each type, tagged or not. */
2694 #define TYPE_DECL_IS_STUB(decl) \
2695 (DECL_NAME (decl) == NULL_TREE \
2696 || (DECL_ARTIFICIAL (decl) \
2697 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2698 /* This is necessary for stub decls that \
2699 appear in nested inline functions. */ \
2700 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2701 && (decl_ultimate_origin (decl) \
2702 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2704 /* Information concerning the compilation unit's programming
2705 language, and compiler version. */
2707 /* Fixed size portion of the DWARF compilation unit header. */
2708 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2709 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2711 /* Fixed size portion of the DWARF comdat type unit header. */
2712 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2713 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2714 + DWARF_OFFSET_SIZE)
2716 /* Fixed size portion of public names info. */
2717 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2719 /* Fixed size portion of the address range info. */
2720 #define DWARF_ARANGES_HEADER_SIZE \
2721 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2722 DWARF2_ADDR_SIZE * 2) \
2723 - DWARF_INITIAL_LENGTH_SIZE)
2725 /* Size of padding portion in the address range info. It must be
2726 aligned to twice the pointer size. */
2727 #define DWARF_ARANGES_PAD_SIZE \
2728 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2729 DWARF2_ADDR_SIZE * 2) \
2730 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2732 /* Use assembler line directives if available. */
2733 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2734 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2735 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2736 #else
2737 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2738 #endif
2739 #endif
2741 /* Minimum line offset in a special line info. opcode.
2742 This value was chosen to give a reasonable range of values. */
2743 #define DWARF_LINE_BASE -10
2745 /* First special line opcode - leave room for the standard opcodes. */
2746 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2748 /* Range of line offsets in a special line info. opcode. */
2749 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2751 /* Flag that indicates the initial value of the is_stmt_start flag.
2752 In the present implementation, we do not mark any lines as
2753 the beginning of a source statement, because that information
2754 is not made available by the GCC front-end. */
2755 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2757 /* Maximum number of operations per instruction bundle. */
2758 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2759 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2760 #endif
2762 /* This location is used by calc_die_sizes() to keep track
2763 the offset of each DIE within the .debug_info section. */
2764 static unsigned long next_die_offset;
2766 /* Record the root of the DIE's built for the current compilation unit. */
2767 static GTY(()) dw_die_ref single_comp_unit_die;
2769 /* A list of type DIEs that have been separated into comdat sections. */
2770 static GTY(()) comdat_type_node *comdat_type_list;
2772 /* A list of DIEs with a NULL parent waiting to be relocated. */
2773 static GTY(()) limbo_die_node *limbo_die_list;
2775 /* A list of DIEs for which we may have to generate
2776 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2777 static GTY(()) limbo_die_node *deferred_asm_name;
2779 /* Filenames referenced by this compilation unit. */
2780 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2782 /* A hash table of references to DIE's that describe declarations.
2783 The key is a DECL_UID() which is a unique number identifying each decl. */
2784 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2786 /* A hash table of references to DIE's that describe COMMON blocks.
2787 The key is DECL_UID() ^ die_parent. */
2788 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2790 typedef struct GTY(()) die_arg_entry_struct {
2791 dw_die_ref die;
2792 tree arg;
2793 } die_arg_entry;
2796 /* Node of the variable location list. */
2797 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2798 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2799 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2800 in mode of the EXPR_LIST node and first EXPR_LIST operand
2801 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2802 location or NULL for padding. For larger bitsizes,
2803 mode is 0 and first operand is a CONCAT with bitsize
2804 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2805 NULL as second operand. */
2806 rtx GTY (()) loc;
2807 const char * GTY (()) label;
2808 struct var_loc_node * GTY (()) next;
2811 /* Variable location list. */
2812 struct GTY (()) var_loc_list_def {
2813 struct var_loc_node * GTY (()) first;
2815 /* Pointer to the last but one or last element of the
2816 chained list. If the list is empty, both first and
2817 last are NULL, if the list contains just one node
2818 or the last node certainly is not redundant, it points
2819 to the last node, otherwise points to the last but one.
2820 Do not mark it for GC because it is marked through the chain. */
2821 struct var_loc_node * GTY ((skip ("%h"))) last;
2823 /* Pointer to the last element before section switch,
2824 if NULL, either sections weren't switched or first
2825 is after section switch. */
2826 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2828 /* DECL_UID of the variable decl. */
2829 unsigned int decl_id;
2831 typedef struct var_loc_list_def var_loc_list;
2833 /* Call argument location list. */
2834 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2835 rtx GTY (()) call_arg_loc_note;
2836 const char * GTY (()) label;
2837 tree GTY (()) block;
2838 bool tail_call_p;
2839 rtx GTY (()) symbol_ref;
2840 struct call_arg_loc_node * GTY (()) next;
2844 /* Table of decl location linked lists. */
2845 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2847 /* Head and tail of call_arg_loc chain. */
2848 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2849 static struct call_arg_loc_node *call_arg_loc_last;
2851 /* Number of call sites in the current function. */
2852 static int call_site_count = -1;
2853 /* Number of tail call sites in the current function. */
2854 static int tail_call_site_count = -1;
2856 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2857 DIEs. */
2858 static vec<dw_die_ref> block_map;
2860 /* A cached location list. */
2861 struct GTY (()) cached_dw_loc_list_def {
2862 /* The DECL_UID of the decl that this entry describes. */
2863 unsigned int decl_id;
2865 /* The cached location list. */
2866 dw_loc_list_ref loc_list;
2868 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2870 /* Table of cached location lists. */
2871 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2873 /* A pointer to the base of a list of references to DIE's that
2874 are uniquely identified by their tag, presence/absence of
2875 children DIE's, and list of attribute/value pairs. */
2876 static GTY((length ("abbrev_die_table_allocated")))
2877 dw_die_ref *abbrev_die_table;
2879 /* Number of elements currently allocated for abbrev_die_table. */
2880 static GTY(()) unsigned abbrev_die_table_allocated;
2882 /* Number of elements in type_die_table currently in use. */
2883 static GTY(()) unsigned abbrev_die_table_in_use;
2885 /* Size (in elements) of increments by which we may expand the
2886 abbrev_die_table. */
2887 #define ABBREV_DIE_TABLE_INCREMENT 256
2889 /* A global counter for generating labels for line number data. */
2890 static unsigned int line_info_label_num;
2892 /* The current table to which we should emit line number information
2893 for the current function. This will be set up at the beginning of
2894 assembly for the function. */
2895 static dw_line_info_table *cur_line_info_table;
2897 /* The two default tables of line number info. */
2898 static GTY(()) dw_line_info_table *text_section_line_info;
2899 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2901 /* The set of all non-default tables of line number info. */
2902 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2904 /* A flag to tell pubnames/types export if there is an info section to
2905 refer to. */
2906 static bool info_section_emitted;
2908 /* A pointer to the base of a table that contains a list of publicly
2909 accessible names. */
2910 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2912 /* A pointer to the base of a table that contains a list of publicly
2913 accessible types. */
2914 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2916 /* A pointer to the base of a table that contains a list of macro
2917 defines/undefines (and file start/end markers). */
2918 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2920 /* True if .debug_macinfo or .debug_macros section is going to be
2921 emitted. */
2922 #define have_macinfo \
2923 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2924 && !macinfo_table->is_empty ())
2926 /* Array of dies for which we should generate .debug_ranges info. */
2927 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2929 /* Number of elements currently allocated for ranges_table. */
2930 static GTY(()) unsigned ranges_table_allocated;
2932 /* Number of elements in ranges_table currently in use. */
2933 static GTY(()) unsigned ranges_table_in_use;
2935 /* Array of pairs of labels referenced in ranges_table. */
2936 static GTY ((length ("ranges_by_label_allocated")))
2937 dw_ranges_by_label_ref ranges_by_label;
2939 /* Number of elements currently allocated for ranges_by_label. */
2940 static GTY(()) unsigned ranges_by_label_allocated;
2942 /* Number of elements in ranges_by_label currently in use. */
2943 static GTY(()) unsigned ranges_by_label_in_use;
2945 /* Size (in elements) of increments by which we may expand the
2946 ranges_table. */
2947 #define RANGES_TABLE_INCREMENT 64
2949 /* Whether we have location lists that need outputting */
2950 static GTY(()) bool have_location_lists;
2952 /* Unique label counter. */
2953 static GTY(()) unsigned int loclabel_num;
2955 /* Unique label counter for point-of-call tables. */
2956 static GTY(()) unsigned int poc_label_num;
2958 /* Record whether the function being analyzed contains inlined functions. */
2959 static int current_function_has_inlines;
2961 /* The last file entry emitted by maybe_emit_file(). */
2962 static GTY(()) struct dwarf_file_data * last_emitted_file;
2964 /* Number of internal labels generated by gen_internal_sym(). */
2965 static GTY(()) int label_num;
2967 /* Cached result of previous call to lookup_filename. */
2968 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2970 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2972 /* Instances of generic types for which we need to generate debug
2973 info that describe their generic parameters and arguments. That
2974 generation needs to happen once all types are properly laid out so
2975 we do it at the end of compilation. */
2976 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2978 /* Offset from the "steady-state frame pointer" to the frame base,
2979 within the current function. */
2980 static HOST_WIDE_INT frame_pointer_fb_offset;
2981 static bool frame_pointer_fb_offset_valid;
2983 static vec<dw_die_ref> base_types;
2985 /* Forward declarations for functions defined in this file. */
2987 static int is_pseudo_reg (const_rtx);
2988 static int is_tagged_type (const_tree);
2989 static const char *dwarf_tag_name (unsigned);
2990 static const char *dwarf_attr_name (unsigned);
2991 static const char *dwarf_form_name (unsigned);
2992 static tree decl_ultimate_origin (const_tree);
2993 static tree decl_class_context (tree);
2994 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2995 static inline enum dw_val_class AT_class (dw_attr_ref);
2996 static inline unsigned int AT_index (dw_attr_ref);
2997 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2998 static inline unsigned AT_flag (dw_attr_ref);
2999 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3000 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3001 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3002 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3003 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3004 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3005 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3006 unsigned int, unsigned char *);
3007 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3008 static hashval_t debug_str_do_hash (const void *);
3009 static int debug_str_eq (const void *, const void *);
3010 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3011 static inline const char *AT_string (dw_attr_ref);
3012 static enum dwarf_form AT_string_form (dw_attr_ref);
3013 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3014 static void add_AT_specification (dw_die_ref, dw_die_ref);
3015 static inline dw_die_ref AT_ref (dw_attr_ref);
3016 static inline int AT_ref_external (dw_attr_ref);
3017 static inline void set_AT_ref_external (dw_attr_ref, int);
3018 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3019 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3020 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3021 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3022 dw_loc_list_ref);
3023 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3024 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3025 static void remove_addr_table_entry (addr_table_entry *);
3026 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3027 static inline rtx AT_addr (dw_attr_ref);
3028 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3029 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3030 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3031 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3032 unsigned HOST_WIDE_INT);
3033 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3034 unsigned long, bool);
3035 static inline const char *AT_lbl (dw_attr_ref);
3036 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3037 static const char *get_AT_low_pc (dw_die_ref);
3038 static const char *get_AT_hi_pc (dw_die_ref);
3039 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3040 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3041 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3042 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3043 static bool is_cxx (void);
3044 static bool is_fortran (void);
3045 static bool is_ada (void);
3046 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3047 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3048 static void add_child_die (dw_die_ref, dw_die_ref);
3049 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3050 static dw_die_ref lookup_type_die (tree);
3051 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3052 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3053 static void equate_type_number_to_die (tree, dw_die_ref);
3054 static hashval_t decl_die_table_hash (const void *);
3055 static int decl_die_table_eq (const void *, const void *);
3056 static dw_die_ref lookup_decl_die (tree);
3057 static hashval_t common_block_die_table_hash (const void *);
3058 static int common_block_die_table_eq (const void *, const void *);
3059 static hashval_t decl_loc_table_hash (const void *);
3060 static int decl_loc_table_eq (const void *, const void *);
3061 static var_loc_list *lookup_decl_loc (const_tree);
3062 static void equate_decl_number_to_die (tree, dw_die_ref);
3063 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3064 static void print_spaces (FILE *);
3065 static void print_die (dw_die_ref, FILE *);
3066 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3067 static dw_die_ref pop_compile_unit (dw_die_ref);
3068 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3069 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3070 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3071 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3072 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3073 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3074 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3075 struct md5_ctx *, int *);
3076 struct checksum_attributes;
3077 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3078 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3079 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3080 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3081 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3082 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3083 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3084 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3085 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3086 static void compute_section_prefix (dw_die_ref);
3087 static int is_type_die (dw_die_ref);
3088 static int is_comdat_die (dw_die_ref);
3089 static int is_symbol_die (dw_die_ref);
3090 static inline bool is_template_instantiation (dw_die_ref);
3091 static void assign_symbol_names (dw_die_ref);
3092 static void break_out_includes (dw_die_ref);
3093 static int is_declaration_die (dw_die_ref);
3094 static int should_move_die_to_comdat (dw_die_ref);
3095 static dw_die_ref clone_as_declaration (dw_die_ref);
3096 static dw_die_ref clone_die (dw_die_ref);
3097 static dw_die_ref clone_tree (dw_die_ref);
3098 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3099 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3100 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3101 static dw_die_ref generate_skeleton (dw_die_ref);
3102 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3103 dw_die_ref,
3104 dw_die_ref);
3105 static void break_out_comdat_types (dw_die_ref);
3106 static void copy_decls_for_unworthy_types (dw_die_ref);
3108 static void add_sibling_attributes (dw_die_ref);
3109 static void output_location_lists (dw_die_ref);
3110 static int constant_size (unsigned HOST_WIDE_INT);
3111 static unsigned long size_of_die (dw_die_ref);
3112 static void calc_die_sizes (dw_die_ref);
3113 static void calc_base_type_die_sizes (void);
3114 static void mark_dies (dw_die_ref);
3115 static void unmark_dies (dw_die_ref);
3116 static void unmark_all_dies (dw_die_ref);
3117 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3118 static unsigned long size_of_aranges (void);
3119 static enum dwarf_form value_format (dw_attr_ref);
3120 static void output_value_format (dw_attr_ref);
3121 static void output_abbrev_section (void);
3122 static void output_die_abbrevs (unsigned long, dw_die_ref);
3123 static void output_die_symbol (dw_die_ref);
3124 static void output_die (dw_die_ref);
3125 static void output_compilation_unit_header (void);
3126 static void output_comp_unit (dw_die_ref, int);
3127 static void output_comdat_type_unit (comdat_type_node *);
3128 static const char *dwarf2_name (tree, int);
3129 static void add_pubname (tree, dw_die_ref);
3130 static void add_enumerator_pubname (const char *, dw_die_ref);
3131 static void add_pubname_string (const char *, dw_die_ref);
3132 static void add_pubtype (tree, dw_die_ref);
3133 static void output_pubnames (vec<pubname_entry, va_gc> *);
3134 static void output_aranges (unsigned long);
3135 static unsigned int add_ranges_num (int);
3136 static unsigned int add_ranges (const_tree);
3137 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3138 bool *, bool);
3139 static void output_ranges (void);
3140 static dw_line_info_table *new_line_info_table (void);
3141 static void output_line_info (bool);
3142 static void output_file_names (void);
3143 static dw_die_ref base_type_die (tree);
3144 static int is_base_type (tree);
3145 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3146 static int decl_quals (const_tree);
3147 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3148 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3149 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3150 static int type_is_enum (const_tree);
3151 static unsigned int dbx_reg_number (const_rtx);
3152 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3153 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3154 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3155 enum var_init_status);
3156 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3157 enum var_init_status);
3158 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3159 enum var_init_status);
3160 static int is_based_loc (const_rtx);
3161 static bool resolve_one_addr (rtx *);
3162 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3163 enum var_init_status);
3164 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3165 enum var_init_status);
3166 static dw_loc_list_ref loc_list_from_tree (tree, int);
3167 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3168 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3169 static tree field_type (const_tree);
3170 static unsigned int simple_type_align_in_bits (const_tree);
3171 static unsigned int simple_decl_align_in_bits (const_tree);
3172 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3173 static HOST_WIDE_INT field_byte_offset (const_tree);
3174 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3175 dw_loc_list_ref);
3176 static void add_data_member_location_attribute (dw_die_ref, tree);
3177 static bool add_const_value_attribute (dw_die_ref, rtx);
3178 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3179 static void insert_wide_int (const wide_int &, unsigned char *, int);
3180 static void insert_float (const_rtx, unsigned char *);
3181 static rtx rtl_for_decl_location (tree);
3182 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3183 enum dwarf_attribute);
3184 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3185 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3186 static void add_name_attribute (dw_die_ref, const char *);
3187 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3188 static void add_comp_dir_attribute (dw_die_ref);
3189 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3190 static void add_subscript_info (dw_die_ref, tree, bool);
3191 static void add_byte_size_attribute (dw_die_ref, tree);
3192 static void add_bit_offset_attribute (dw_die_ref, tree);
3193 static void add_bit_size_attribute (dw_die_ref, tree);
3194 static void add_prototyped_attribute (dw_die_ref, tree);
3195 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3196 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3197 static void add_src_coords_attributes (dw_die_ref, tree);
3198 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3199 static void push_decl_scope (tree);
3200 static void pop_decl_scope (void);
3201 static dw_die_ref scope_die_for (tree, dw_die_ref);
3202 static inline int local_scope_p (dw_die_ref);
3203 static inline int class_scope_p (dw_die_ref);
3204 static inline int class_or_namespace_scope_p (dw_die_ref);
3205 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3206 static void add_calling_convention_attribute (dw_die_ref, tree);
3207 static const char *type_tag (const_tree);
3208 static tree member_declared_type (const_tree);
3209 #if 0
3210 static const char *decl_start_label (tree);
3211 #endif
3212 static void gen_array_type_die (tree, dw_die_ref);
3213 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3214 #if 0
3215 static void gen_entry_point_die (tree, dw_die_ref);
3216 #endif
3217 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3218 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3219 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3220 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3221 static void gen_formal_types_die (tree, dw_die_ref);
3222 static void gen_subprogram_die (tree, dw_die_ref);
3223 static void gen_variable_die (tree, tree, dw_die_ref);
3224 static void gen_const_die (tree, dw_die_ref);
3225 static void gen_label_die (tree, dw_die_ref);
3226 static void gen_lexical_block_die (tree, dw_die_ref, int);
3227 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3228 static void gen_field_die (tree, dw_die_ref);
3229 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3230 static dw_die_ref gen_compile_unit_die (const char *);
3231 static void gen_inheritance_die (tree, tree, dw_die_ref);
3232 static void gen_member_die (tree, dw_die_ref);
3233 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3234 enum debug_info_usage);
3235 static void gen_subroutine_type_die (tree, dw_die_ref);
3236 static void gen_typedef_die (tree, dw_die_ref);
3237 static void gen_type_die (tree, dw_die_ref);
3238 static void gen_block_die (tree, dw_die_ref, int);
3239 static void decls_for_scope (tree, dw_die_ref, int);
3240 static inline int is_redundant_typedef (const_tree);
3241 static bool is_naming_typedef_decl (const_tree);
3242 static inline dw_die_ref get_context_die (tree);
3243 static void gen_namespace_die (tree, dw_die_ref);
3244 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3245 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3246 static dw_die_ref force_decl_die (tree);
3247 static dw_die_ref force_type_die (tree);
3248 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3249 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3250 static struct dwarf_file_data * lookup_filename (const char *);
3251 static void retry_incomplete_types (void);
3252 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3253 static void gen_generic_params_dies (tree);
3254 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3255 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3256 static void splice_child_die (dw_die_ref, dw_die_ref);
3257 static int file_info_cmp (const void *, const void *);
3258 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3259 const char *, const char *);
3260 static void output_loc_list (dw_loc_list_ref);
3261 static char *gen_internal_sym (const char *);
3262 static bool want_pubnames (void);
3264 static void prune_unmark_dies (dw_die_ref);
3265 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3266 static void prune_unused_types_mark (dw_die_ref, int);
3267 static void prune_unused_types_walk (dw_die_ref);
3268 static void prune_unused_types_walk_attribs (dw_die_ref);
3269 static void prune_unused_types_prune (dw_die_ref);
3270 static void prune_unused_types (void);
3271 static int maybe_emit_file (struct dwarf_file_data *fd);
3272 static inline const char *AT_vms_delta1 (dw_attr_ref);
3273 static inline const char *AT_vms_delta2 (dw_attr_ref);
3274 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3275 const char *, const char *);
3276 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3277 static void gen_remaining_tmpl_value_param_die_attribute (void);
3278 static bool generic_type_p (tree);
3279 static void schedule_generic_params_dies_gen (tree t);
3280 static void gen_scheduled_generic_parms_dies (void);
3282 static const char *comp_dir_string (void);
3284 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3286 /* enum for tracking thread-local variables whose address is really an offset
3287 relative to the TLS pointer, which will need link-time relocation, but will
3288 not need relocation by the DWARF consumer. */
3290 enum dtprel_bool
3292 dtprel_false = 0,
3293 dtprel_true = 1
3296 /* Return the operator to use for an address of a variable. For dtprel_true, we
3297 use DW_OP_const*. For regular variables, which need both link-time
3298 relocation and consumer-level relocation (e.g., to account for shared objects
3299 loaded at a random address), we use DW_OP_addr*. */
3301 static inline enum dwarf_location_atom
3302 dw_addr_op (enum dtprel_bool dtprel)
3304 if (dtprel == dtprel_true)
3305 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3306 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3307 else
3308 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3311 /* Return a pointer to a newly allocated address location description. If
3312 dwarf_split_debug_info is true, then record the address with the appropriate
3313 relocation. */
3314 static inline dw_loc_descr_ref
3315 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3317 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3319 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3320 ref->dw_loc_oprnd1.v.val_addr = addr;
3321 ref->dtprel = dtprel;
3322 if (dwarf_split_debug_info)
3323 ref->dw_loc_oprnd1.val_entry
3324 = add_addr_table_entry (addr,
3325 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3326 else
3327 ref->dw_loc_oprnd1.val_entry = NULL;
3329 return ref;
3332 /* Section names used to hold DWARF debugging information. */
3334 #ifndef DEBUG_INFO_SECTION
3335 #define DEBUG_INFO_SECTION ".debug_info"
3336 #endif
3337 #ifndef DEBUG_DWO_INFO_SECTION
3338 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3339 #endif
3340 #ifndef DEBUG_ABBREV_SECTION
3341 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3342 #endif
3343 #ifndef DEBUG_DWO_ABBREV_SECTION
3344 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3345 #endif
3346 #ifndef DEBUG_ARANGES_SECTION
3347 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3348 #endif
3349 #ifndef DEBUG_ADDR_SECTION
3350 #define DEBUG_ADDR_SECTION ".debug_addr"
3351 #endif
3352 #ifndef DEBUG_NORM_MACINFO_SECTION
3353 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3354 #endif
3355 #ifndef DEBUG_DWO_MACINFO_SECTION
3356 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3357 #endif
3358 #ifndef DEBUG_MACINFO_SECTION
3359 #define DEBUG_MACINFO_SECTION \
3360 (!dwarf_split_debug_info \
3361 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3362 #endif
3363 #ifndef DEBUG_NORM_MACRO_SECTION
3364 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3365 #endif
3366 #ifndef DEBUG_DWO_MACRO_SECTION
3367 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3368 #endif
3369 #ifndef DEBUG_MACRO_SECTION
3370 #define DEBUG_MACRO_SECTION \
3371 (!dwarf_split_debug_info \
3372 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3373 #endif
3374 #ifndef DEBUG_LINE_SECTION
3375 #define DEBUG_LINE_SECTION ".debug_line"
3376 #endif
3377 #ifndef DEBUG_DWO_LINE_SECTION
3378 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3379 #endif
3380 #ifndef DEBUG_LOC_SECTION
3381 #define DEBUG_LOC_SECTION ".debug_loc"
3382 #endif
3383 #ifndef DEBUG_DWO_LOC_SECTION
3384 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3385 #endif
3386 #ifndef DEBUG_PUBNAMES_SECTION
3387 #define DEBUG_PUBNAMES_SECTION \
3388 ((debug_generate_pub_sections == 2) \
3389 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3390 #endif
3391 #ifndef DEBUG_PUBTYPES_SECTION
3392 #define DEBUG_PUBTYPES_SECTION \
3393 ((debug_generate_pub_sections == 2) \
3394 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3395 #endif
3396 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3397 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3398 #ifndef DEBUG_STR_OFFSETS_SECTION
3399 #define DEBUG_STR_OFFSETS_SECTION \
3400 (!dwarf_split_debug_info \
3401 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3402 #endif
3403 #ifndef DEBUG_STR_DWO_SECTION
3404 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3405 #endif
3406 #ifndef DEBUG_STR_SECTION
3407 #define DEBUG_STR_SECTION ".debug_str"
3408 #endif
3409 #ifndef DEBUG_RANGES_SECTION
3410 #define DEBUG_RANGES_SECTION ".debug_ranges"
3411 #endif
3413 /* Standard ELF section names for compiled code and data. */
3414 #ifndef TEXT_SECTION_NAME
3415 #define TEXT_SECTION_NAME ".text"
3416 #endif
3418 /* Section flags for .debug_macinfo/.debug_macro section. */
3419 #define DEBUG_MACRO_SECTION_FLAGS \
3420 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3422 /* Section flags for .debug_str section. */
3423 #define DEBUG_STR_SECTION_FLAGS \
3424 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3425 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3426 : SECTION_DEBUG)
3428 /* Section flags for .debug_str.dwo section. */
3429 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3431 /* Labels we insert at beginning sections we can reference instead of
3432 the section names themselves. */
3434 #ifndef TEXT_SECTION_LABEL
3435 #define TEXT_SECTION_LABEL "Ltext"
3436 #endif
3437 #ifndef COLD_TEXT_SECTION_LABEL
3438 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3439 #endif
3440 #ifndef DEBUG_LINE_SECTION_LABEL
3441 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3442 #endif
3443 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3444 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3445 #endif
3446 #ifndef DEBUG_INFO_SECTION_LABEL
3447 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3448 #endif
3449 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3450 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3451 #endif
3452 #ifndef DEBUG_ABBREV_SECTION_LABEL
3453 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3454 #endif
3455 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3456 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3457 #endif
3458 #ifndef DEBUG_ADDR_SECTION_LABEL
3459 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3460 #endif
3461 #ifndef DEBUG_LOC_SECTION_LABEL
3462 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3463 #endif
3464 #ifndef DEBUG_RANGES_SECTION_LABEL
3465 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3466 #endif
3467 #ifndef DEBUG_MACINFO_SECTION_LABEL
3468 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3469 #endif
3470 #ifndef DEBUG_MACRO_SECTION_LABEL
3471 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3472 #endif
3473 #define SKELETON_COMP_DIE_ABBREV 1
3474 #define SKELETON_TYPE_DIE_ABBREV 2
3476 /* Definitions of defaults for formats and names of various special
3477 (artificial) labels which may be generated within this file (when the -g
3478 options is used and DWARF2_DEBUGGING_INFO is in effect.
3479 If necessary, these may be overridden from within the tm.h file, but
3480 typically, overriding these defaults is unnecessary. */
3482 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3495 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3497 #ifndef TEXT_END_LABEL
3498 #define TEXT_END_LABEL "Letext"
3499 #endif
3500 #ifndef COLD_END_LABEL
3501 #define COLD_END_LABEL "Letext_cold"
3502 #endif
3503 #ifndef BLOCK_BEGIN_LABEL
3504 #define BLOCK_BEGIN_LABEL "LBB"
3505 #endif
3506 #ifndef BLOCK_END_LABEL
3507 #define BLOCK_END_LABEL "LBE"
3508 #endif
3509 #ifndef LINE_CODE_LABEL
3510 #define LINE_CODE_LABEL "LM"
3511 #endif
3514 /* Return the root of the DIE's built for the current compilation unit. */
3515 static dw_die_ref
3516 comp_unit_die (void)
3518 if (!single_comp_unit_die)
3519 single_comp_unit_die = gen_compile_unit_die (NULL);
3520 return single_comp_unit_die;
3523 /* We allow a language front-end to designate a function that is to be
3524 called to "demangle" any name before it is put into a DIE. */
3526 static const char *(*demangle_name_func) (const char *);
3528 void
3529 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3531 demangle_name_func = func;
3534 /* Test if rtl node points to a pseudo register. */
3536 static inline int
3537 is_pseudo_reg (const_rtx rtl)
3539 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3540 || (GET_CODE (rtl) == SUBREG
3541 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3544 /* Return a reference to a type, with its const and volatile qualifiers
3545 removed. */
3547 static inline tree
3548 type_main_variant (tree type)
3550 type = TYPE_MAIN_VARIANT (type);
3552 /* ??? There really should be only one main variant among any group of
3553 variants of a given type (and all of the MAIN_VARIANT values for all
3554 members of the group should point to that one type) but sometimes the C
3555 front-end messes this up for array types, so we work around that bug
3556 here. */
3557 if (TREE_CODE (type) == ARRAY_TYPE)
3558 while (type != TYPE_MAIN_VARIANT (type))
3559 type = TYPE_MAIN_VARIANT (type);
3561 return type;
3564 /* Return nonzero if the given type node represents a tagged type. */
3566 static inline int
3567 is_tagged_type (const_tree type)
3569 enum tree_code code = TREE_CODE (type);
3571 return (code == RECORD_TYPE || code == UNION_TYPE
3572 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3575 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3577 static void
3578 get_ref_die_offset_label (char *label, dw_die_ref ref)
3580 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3583 /* Return die_offset of a DIE reference to a base type. */
3585 static unsigned long int
3586 get_base_type_offset (dw_die_ref ref)
3588 if (ref->die_offset)
3589 return ref->die_offset;
3590 if (comp_unit_die ()->die_abbrev)
3592 calc_base_type_die_sizes ();
3593 gcc_assert (ref->die_offset);
3595 return ref->die_offset;
3598 /* Return die_offset of a DIE reference other than base type. */
3600 static unsigned long int
3601 get_ref_die_offset (dw_die_ref ref)
3603 gcc_assert (ref->die_offset);
3604 return ref->die_offset;
3607 /* Convert a DIE tag into its string name. */
3609 static const char *
3610 dwarf_tag_name (unsigned int tag)
3612 const char *name = get_DW_TAG_name (tag);
3614 if (name != NULL)
3615 return name;
3617 return "DW_TAG_<unknown>";
3620 /* Convert a DWARF attribute code into its string name. */
3622 static const char *
3623 dwarf_attr_name (unsigned int attr)
3625 const char *name;
3627 switch (attr)
3629 #if VMS_DEBUGGING_INFO
3630 case DW_AT_HP_prologue:
3631 return "DW_AT_HP_prologue";
3632 #else
3633 case DW_AT_MIPS_loop_unroll_factor:
3634 return "DW_AT_MIPS_loop_unroll_factor";
3635 #endif
3637 #if VMS_DEBUGGING_INFO
3638 case DW_AT_HP_epilogue:
3639 return "DW_AT_HP_epilogue";
3640 #else
3641 case DW_AT_MIPS_stride:
3642 return "DW_AT_MIPS_stride";
3643 #endif
3646 name = get_DW_AT_name (attr);
3648 if (name != NULL)
3649 return name;
3651 return "DW_AT_<unknown>";
3654 /* Convert a DWARF value form code into its string name. */
3656 static const char *
3657 dwarf_form_name (unsigned int form)
3659 const char *name = get_DW_FORM_name (form);
3661 if (name != NULL)
3662 return name;
3664 return "DW_FORM_<unknown>";
3667 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3668 instance of an inlined instance of a decl which is local to an inline
3669 function, so we have to trace all of the way back through the origin chain
3670 to find out what sort of node actually served as the original seed for the
3671 given block. */
3673 static tree
3674 decl_ultimate_origin (const_tree decl)
3676 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3677 return NULL_TREE;
3679 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3680 nodes in the function to point to themselves; ignore that if
3681 we're trying to output the abstract instance of this function. */
3682 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3683 return NULL_TREE;
3685 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3686 most distant ancestor, this should never happen. */
3687 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3689 return DECL_ABSTRACT_ORIGIN (decl);
3692 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3693 of a virtual function may refer to a base class, so we check the 'this'
3694 parameter. */
3696 static tree
3697 decl_class_context (tree decl)
3699 tree context = NULL_TREE;
3701 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3702 context = DECL_CONTEXT (decl);
3703 else
3704 context = TYPE_MAIN_VARIANT
3705 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3707 if (context && !TYPE_P (context))
3708 context = NULL_TREE;
3710 return context;
3713 /* Add an attribute/value pair to a DIE. */
3715 static inline void
3716 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3718 /* Maybe this should be an assert? */
3719 if (die == NULL)
3720 return;
3722 vec_safe_reserve (die->die_attr, 1);
3723 vec_safe_push (die->die_attr, *attr);
3726 static inline enum dw_val_class
3727 AT_class (dw_attr_ref a)
3729 return a->dw_attr_val.val_class;
3732 /* Return the index for any attribute that will be referenced with a
3733 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3734 are stored in dw_attr_val.v.val_str for reference counting
3735 pruning. */
3737 static inline unsigned int
3738 AT_index (dw_attr_ref a)
3740 if (AT_class (a) == dw_val_class_str)
3741 return a->dw_attr_val.v.val_str->index;
3742 else if (a->dw_attr_val.val_entry != NULL)
3743 return a->dw_attr_val.val_entry->index;
3744 return NOT_INDEXED;
3747 /* Add a flag value attribute to a DIE. */
3749 static inline void
3750 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3752 dw_attr_node attr;
3754 attr.dw_attr = attr_kind;
3755 attr.dw_attr_val.val_class = dw_val_class_flag;
3756 attr.dw_attr_val.val_entry = NULL;
3757 attr.dw_attr_val.v.val_flag = flag;
3758 add_dwarf_attr (die, &attr);
3761 static inline unsigned
3762 AT_flag (dw_attr_ref a)
3764 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3765 return a->dw_attr_val.v.val_flag;
3768 /* Add a signed integer attribute value to a DIE. */
3770 static inline void
3771 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3773 dw_attr_node attr;
3775 attr.dw_attr = attr_kind;
3776 attr.dw_attr_val.val_class = dw_val_class_const;
3777 attr.dw_attr_val.val_entry = NULL;
3778 attr.dw_attr_val.v.val_int = int_val;
3779 add_dwarf_attr (die, &attr);
3782 static inline HOST_WIDE_INT
3783 AT_int (dw_attr_ref a)
3785 gcc_assert (a && AT_class (a) == dw_val_class_const);
3786 return a->dw_attr_val.v.val_int;
3789 /* Add an unsigned integer attribute value to a DIE. */
3791 static inline void
3792 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3793 unsigned HOST_WIDE_INT unsigned_val)
3795 dw_attr_node attr;
3797 attr.dw_attr = attr_kind;
3798 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3799 attr.dw_attr_val.val_entry = NULL;
3800 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3801 add_dwarf_attr (die, &attr);
3804 static inline unsigned HOST_WIDE_INT
3805 AT_unsigned (dw_attr_ref a)
3807 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3808 return a->dw_attr_val.v.val_unsigned;
3811 /* Add an unsigned wide integer attribute value to a DIE. */
3813 static inline void
3814 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3815 const wide_int& w)
3817 dw_attr_node attr;
3819 attr.dw_attr = attr_kind;
3820 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3821 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3822 *attr.dw_attr_val.v.val_wide = w;
3823 add_dwarf_attr (die, &attr);
3826 /* Add an unsigned double integer attribute value to a DIE. */
3828 static inline void
3829 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3830 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3832 dw_attr_node attr;
3834 attr.dw_attr = attr_kind;
3835 attr.dw_attr_val.val_class = dw_val_class_const_double;
3836 attr.dw_attr_val.val_entry = NULL;
3837 attr.dw_attr_val.v.val_double.high = high;
3838 attr.dw_attr_val.v.val_double.low = low;
3839 add_dwarf_attr (die, &attr);
3842 /* Add a floating point attribute value to a DIE and return it. */
3844 static inline void
3845 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3846 unsigned int length, unsigned int elt_size, unsigned char *array)
3848 dw_attr_node attr;
3850 attr.dw_attr = attr_kind;
3851 attr.dw_attr_val.val_class = dw_val_class_vec;
3852 attr.dw_attr_val.val_entry = NULL;
3853 attr.dw_attr_val.v.val_vec.length = length;
3854 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3855 attr.dw_attr_val.v.val_vec.array = array;
3856 add_dwarf_attr (die, &attr);
3859 /* Add an 8-byte data attribute value to a DIE. */
3861 static inline void
3862 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3863 unsigned char data8[8])
3865 dw_attr_node attr;
3867 attr.dw_attr = attr_kind;
3868 attr.dw_attr_val.val_class = dw_val_class_data8;
3869 attr.dw_attr_val.val_entry = NULL;
3870 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3871 add_dwarf_attr (die, &attr);
3874 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3875 dwarf_split_debug_info, address attributes in dies destined for the
3876 final executable have force_direct set to avoid using indexed
3877 references. */
3879 static inline void
3880 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3881 bool force_direct)
3883 dw_attr_node attr;
3884 char * lbl_id;
3886 lbl_id = xstrdup (lbl_low);
3887 attr.dw_attr = DW_AT_low_pc;
3888 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3889 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3890 if (dwarf_split_debug_info && !force_direct)
3891 attr.dw_attr_val.val_entry
3892 = add_addr_table_entry (lbl_id, ate_kind_label);
3893 else
3894 attr.dw_attr_val.val_entry = NULL;
3895 add_dwarf_attr (die, &attr);
3897 attr.dw_attr = DW_AT_high_pc;
3898 if (dwarf_version < 4)
3899 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3900 else
3901 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3902 lbl_id = xstrdup (lbl_high);
3903 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3904 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3905 && dwarf_split_debug_info && !force_direct)
3906 attr.dw_attr_val.val_entry
3907 = add_addr_table_entry (lbl_id, ate_kind_label);
3908 else
3909 attr.dw_attr_val.val_entry = NULL;
3910 add_dwarf_attr (die, &attr);
3913 /* Hash and equality functions for debug_str_hash. */
3915 static hashval_t
3916 debug_str_do_hash (const void *x)
3918 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3921 static int
3922 debug_str_eq (const void *x1, const void *x2)
3924 return strcmp ((((const struct indirect_string_node *)x1)->str),
3925 (const char *)x2) == 0;
3928 /* Add STR to the given string hash table. */
3930 static struct indirect_string_node *
3931 find_AT_string_in_table (const char *str, htab_t table)
3933 struct indirect_string_node *node;
3934 void **slot;
3936 slot = htab_find_slot_with_hash (table, str,
3937 htab_hash_string (str), INSERT);
3938 if (*slot == NULL)
3940 node = ggc_cleared_alloc<indirect_string_node> ();
3941 node->str = ggc_strdup (str);
3942 *slot = node;
3944 else
3945 node = (struct indirect_string_node *) *slot;
3947 node->refcount++;
3948 return node;
3951 /* Add STR to the indirect string hash table. */
3953 static struct indirect_string_node *
3954 find_AT_string (const char *str)
3956 if (! debug_str_hash)
3957 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3958 debug_str_eq, NULL);
3960 return find_AT_string_in_table (str, debug_str_hash);
3963 /* Add a string attribute value to a DIE. */
3965 static inline void
3966 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3968 dw_attr_node attr;
3969 struct indirect_string_node *node;
3971 node = find_AT_string (str);
3973 attr.dw_attr = attr_kind;
3974 attr.dw_attr_val.val_class = dw_val_class_str;
3975 attr.dw_attr_val.val_entry = NULL;
3976 attr.dw_attr_val.v.val_str = node;
3977 add_dwarf_attr (die, &attr);
3980 static inline const char *
3981 AT_string (dw_attr_ref a)
3983 gcc_assert (a && AT_class (a) == dw_val_class_str);
3984 return a->dw_attr_val.v.val_str->str;
3987 /* Call this function directly to bypass AT_string_form's logic to put
3988 the string inline in the die. */
3990 static void
3991 set_indirect_string (struct indirect_string_node *node)
3993 char label[32];
3994 /* Already indirect is a no op. */
3995 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3997 gcc_assert (node->label);
3998 return;
4000 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4001 ++dw2_string_counter;
4002 node->label = xstrdup (label);
4004 if (!dwarf_split_debug_info)
4006 node->form = DW_FORM_strp;
4007 node->index = NOT_INDEXED;
4009 else
4011 node->form = DW_FORM_GNU_str_index;
4012 node->index = NO_INDEX_ASSIGNED;
4016 /* Find out whether a string should be output inline in DIE
4017 or out-of-line in .debug_str section. */
4019 static enum dwarf_form
4020 find_string_form (struct indirect_string_node *node)
4022 unsigned int len;
4024 if (node->form)
4025 return node->form;
4027 len = strlen (node->str) + 1;
4029 /* If the string is shorter or equal to the size of the reference, it is
4030 always better to put it inline. */
4031 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4032 return node->form = DW_FORM_string;
4034 /* If we cannot expect the linker to merge strings in .debug_str
4035 section, only put it into .debug_str if it is worth even in this
4036 single module. */
4037 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4038 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4039 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4040 return node->form = DW_FORM_string;
4042 set_indirect_string (node);
4044 return node->form;
4047 /* Find out whether the string referenced from the attribute should be
4048 output inline in DIE or out-of-line in .debug_str section. */
4050 static enum dwarf_form
4051 AT_string_form (dw_attr_ref a)
4053 gcc_assert (a && AT_class (a) == dw_val_class_str);
4054 return find_string_form (a->dw_attr_val.v.val_str);
4057 /* Add a DIE reference attribute value to a DIE. */
4059 static inline void
4060 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4062 dw_attr_node attr;
4064 #ifdef ENABLE_CHECKING
4065 gcc_assert (targ_die != NULL);
4066 #else
4067 /* With LTO we can end up trying to reference something we didn't create
4068 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4069 if (targ_die == NULL)
4070 return;
4071 #endif
4073 attr.dw_attr = attr_kind;
4074 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4075 attr.dw_attr_val.val_entry = NULL;
4076 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4077 attr.dw_attr_val.v.val_die_ref.external = 0;
4078 add_dwarf_attr (die, &attr);
4081 /* Change DIE reference REF to point to NEW_DIE instead. */
4083 static inline void
4084 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4086 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4087 ref->dw_attr_val.v.val_die_ref.die = new_die;
4088 ref->dw_attr_val.v.val_die_ref.external = 0;
4091 /* Add an AT_specification attribute to a DIE, and also make the back
4092 pointer from the specification to the definition. */
4094 static inline void
4095 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4097 add_AT_die_ref (die, DW_AT_specification, targ_die);
4098 gcc_assert (!targ_die->die_definition);
4099 targ_die->die_definition = die;
4102 static inline dw_die_ref
4103 AT_ref (dw_attr_ref a)
4105 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4106 return a->dw_attr_val.v.val_die_ref.die;
4109 static inline int
4110 AT_ref_external (dw_attr_ref a)
4112 if (a && AT_class (a) == dw_val_class_die_ref)
4113 return a->dw_attr_val.v.val_die_ref.external;
4115 return 0;
4118 static inline void
4119 set_AT_ref_external (dw_attr_ref a, int i)
4121 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4122 a->dw_attr_val.v.val_die_ref.external = i;
4125 /* Add an FDE reference attribute value to a DIE. */
4127 static inline void
4128 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4130 dw_attr_node attr;
4132 attr.dw_attr = attr_kind;
4133 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4134 attr.dw_attr_val.val_entry = NULL;
4135 attr.dw_attr_val.v.val_fde_index = targ_fde;
4136 add_dwarf_attr (die, &attr);
4139 /* Add a location description attribute value to a DIE. */
4141 static inline void
4142 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4144 dw_attr_node attr;
4146 attr.dw_attr = attr_kind;
4147 attr.dw_attr_val.val_class = dw_val_class_loc;
4148 attr.dw_attr_val.val_entry = NULL;
4149 attr.dw_attr_val.v.val_loc = loc;
4150 add_dwarf_attr (die, &attr);
4153 static inline dw_loc_descr_ref
4154 AT_loc (dw_attr_ref a)
4156 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4157 return a->dw_attr_val.v.val_loc;
4160 static inline void
4161 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4163 dw_attr_node attr;
4165 attr.dw_attr = attr_kind;
4166 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4167 attr.dw_attr_val.val_entry = NULL;
4168 attr.dw_attr_val.v.val_loc_list = loc_list;
4169 add_dwarf_attr (die, &attr);
4170 have_location_lists = true;
4173 static inline dw_loc_list_ref
4174 AT_loc_list (dw_attr_ref a)
4176 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4177 return a->dw_attr_val.v.val_loc_list;
4180 static inline dw_loc_list_ref *
4181 AT_loc_list_ptr (dw_attr_ref a)
4183 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4184 return &a->dw_attr_val.v.val_loc_list;
4187 /* Table of entries into the .debug_addr section. */
4189 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4191 /* Hash an address_table_entry. */
4193 static hashval_t
4194 addr_table_entry_do_hash (const void *x)
4196 const addr_table_entry *a = (const addr_table_entry *) x;
4197 inchash::hash hstate;
4198 switch (a->kind)
4200 case ate_kind_rtx:
4201 hstate.add_int (0);
4202 break;
4203 case ate_kind_rtx_dtprel:
4204 hstate.add_int (1);
4205 break;
4206 case ate_kind_label:
4207 return htab_hash_string (a->addr.label);
4208 default:
4209 gcc_unreachable ();
4211 inchash::add_rtx (a->addr.rtl, hstate);
4212 return hstate.end ();
4215 /* Determine equality for two address_table_entries. */
4217 static int
4218 addr_table_entry_eq (const void *x1, const void *x2)
4220 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4221 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4223 if (a1->kind != a2->kind)
4224 return 0;
4225 switch (a1->kind)
4227 case ate_kind_rtx:
4228 case ate_kind_rtx_dtprel:
4229 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4230 case ate_kind_label:
4231 return strcmp (a1->addr.label, a2->addr.label) == 0;
4232 default:
4233 gcc_unreachable ();
4237 /* Initialize an addr_table_entry. */
4239 void
4240 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4242 e->kind = kind;
4243 switch (kind)
4245 case ate_kind_rtx:
4246 case ate_kind_rtx_dtprel:
4247 e->addr.rtl = (rtx) addr;
4248 break;
4249 case ate_kind_label:
4250 e->addr.label = (char *) addr;
4251 break;
4253 e->refcount = 0;
4254 e->index = NO_INDEX_ASSIGNED;
4257 /* Add attr to the address table entry to the table. Defer setting an
4258 index until output time. */
4260 static addr_table_entry *
4261 add_addr_table_entry (void *addr, enum ate_kind kind)
4263 addr_table_entry *node;
4264 addr_table_entry finder;
4265 void **slot;
4267 gcc_assert (dwarf_split_debug_info);
4268 if (! addr_index_table)
4269 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4270 addr_table_entry_eq, NULL);
4271 init_addr_table_entry (&finder, kind, addr);
4272 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4274 if (*slot == HTAB_EMPTY_ENTRY)
4276 node = ggc_cleared_alloc<addr_table_entry> ();
4277 init_addr_table_entry (node, kind, addr);
4278 *slot = node;
4280 else
4281 node = (addr_table_entry *) *slot;
4283 node->refcount++;
4284 return node;
4287 /* Remove an entry from the addr table by decrementing its refcount.
4288 Strictly, decrementing the refcount would be enough, but the
4289 assertion that the entry is actually in the table has found
4290 bugs. */
4292 static void
4293 remove_addr_table_entry (addr_table_entry *entry)
4295 gcc_assert (dwarf_split_debug_info && addr_index_table);
4296 /* After an index is assigned, the table is frozen. */
4297 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4298 entry->refcount--;
4301 /* Given a location list, remove all addresses it refers to from the
4302 address_table. */
4304 static void
4305 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4307 for (; descr; descr = descr->dw_loc_next)
4308 if (descr->dw_loc_oprnd1.val_entry != NULL)
4310 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4311 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4315 /* A helper function for dwarf2out_finish called through
4316 htab_traverse. Assign an addr_table_entry its index. All entries
4317 must be collected into the table when this function is called,
4318 because the indexing code relies on htab_traverse to traverse nodes
4319 in the same order for each run. */
4321 static int
4322 index_addr_table_entry (void **h, void *v)
4324 addr_table_entry *node = (addr_table_entry *) *h;
4325 unsigned int *index = (unsigned int *) v;
4327 /* Don't index unreferenced nodes. */
4328 if (node->refcount == 0)
4329 return 1;
4331 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4332 node->index = *index;
4333 *index += 1;
4335 return 1;
4338 /* Add an address constant attribute value to a DIE. When using
4339 dwarf_split_debug_info, address attributes in dies destined for the
4340 final executable should be direct references--setting the parameter
4341 force_direct ensures this behavior. */
4343 static inline void
4344 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4345 bool force_direct)
4347 dw_attr_node attr;
4349 attr.dw_attr = attr_kind;
4350 attr.dw_attr_val.val_class = dw_val_class_addr;
4351 attr.dw_attr_val.v.val_addr = addr;
4352 if (dwarf_split_debug_info && !force_direct)
4353 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4354 else
4355 attr.dw_attr_val.val_entry = NULL;
4356 add_dwarf_attr (die, &attr);
4359 /* Get the RTX from to an address DIE attribute. */
4361 static inline rtx
4362 AT_addr (dw_attr_ref a)
4364 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4365 return a->dw_attr_val.v.val_addr;
4368 /* Add a file attribute value to a DIE. */
4370 static inline void
4371 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4372 struct dwarf_file_data *fd)
4374 dw_attr_node attr;
4376 attr.dw_attr = attr_kind;
4377 attr.dw_attr_val.val_class = dw_val_class_file;
4378 attr.dw_attr_val.val_entry = NULL;
4379 attr.dw_attr_val.v.val_file = fd;
4380 add_dwarf_attr (die, &attr);
4383 /* Get the dwarf_file_data from a file DIE attribute. */
4385 static inline struct dwarf_file_data *
4386 AT_file (dw_attr_ref a)
4388 gcc_assert (a && AT_class (a) == dw_val_class_file);
4389 return a->dw_attr_val.v.val_file;
4392 /* Add a vms delta attribute value to a DIE. */
4394 static inline void
4395 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4396 const char *lbl1, const char *lbl2)
4398 dw_attr_node attr;
4400 attr.dw_attr = attr_kind;
4401 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4402 attr.dw_attr_val.val_entry = NULL;
4403 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4404 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4405 add_dwarf_attr (die, &attr);
4408 /* Add a label identifier attribute value to a DIE. */
4410 static inline void
4411 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4412 const char *lbl_id)
4414 dw_attr_node attr;
4416 attr.dw_attr = attr_kind;
4417 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4418 attr.dw_attr_val.val_entry = NULL;
4419 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4420 if (dwarf_split_debug_info)
4421 attr.dw_attr_val.val_entry
4422 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4423 ate_kind_label);
4424 add_dwarf_attr (die, &attr);
4427 /* Add a section offset attribute value to a DIE, an offset into the
4428 debug_line section. */
4430 static inline void
4431 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4432 const char *label)
4434 dw_attr_node attr;
4436 attr.dw_attr = attr_kind;
4437 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4438 attr.dw_attr_val.val_entry = NULL;
4439 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4440 add_dwarf_attr (die, &attr);
4443 /* Add a section offset attribute value to a DIE, an offset into the
4444 debug_macinfo section. */
4446 static inline void
4447 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4448 const char *label)
4450 dw_attr_node attr;
4452 attr.dw_attr = attr_kind;
4453 attr.dw_attr_val.val_class = dw_val_class_macptr;
4454 attr.dw_attr_val.val_entry = NULL;
4455 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4456 add_dwarf_attr (die, &attr);
4459 /* Add an offset attribute value to a DIE. */
4461 static inline void
4462 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4463 unsigned HOST_WIDE_INT offset)
4465 dw_attr_node attr;
4467 attr.dw_attr = attr_kind;
4468 attr.dw_attr_val.val_class = dw_val_class_offset;
4469 attr.dw_attr_val.val_entry = NULL;
4470 attr.dw_attr_val.v.val_offset = offset;
4471 add_dwarf_attr (die, &attr);
4474 /* Add a range_list attribute value to a DIE. When using
4475 dwarf_split_debug_info, address attributes in dies destined for the
4476 final executable should be direct references--setting the parameter
4477 force_direct ensures this behavior. */
4479 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4480 #define RELOCATED_OFFSET (NULL)
4482 static void
4483 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4484 long unsigned int offset, bool force_direct)
4486 dw_attr_node attr;
4488 attr.dw_attr = attr_kind;
4489 attr.dw_attr_val.val_class = dw_val_class_range_list;
4490 /* For the range_list attribute, use val_entry to store whether the
4491 offset should follow split-debug-info or normal semantics. This
4492 value is read in output_range_list_offset. */
4493 if (dwarf_split_debug_info && !force_direct)
4494 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4495 else
4496 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4497 attr.dw_attr_val.v.val_offset = offset;
4498 add_dwarf_attr (die, &attr);
4501 /* Return the start label of a delta attribute. */
4503 static inline const char *
4504 AT_vms_delta1 (dw_attr_ref a)
4506 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4507 return a->dw_attr_val.v.val_vms_delta.lbl1;
4510 /* Return the end label of a delta attribute. */
4512 static inline const char *
4513 AT_vms_delta2 (dw_attr_ref a)
4515 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4516 return a->dw_attr_val.v.val_vms_delta.lbl2;
4519 static inline const char *
4520 AT_lbl (dw_attr_ref a)
4522 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4523 || AT_class (a) == dw_val_class_lineptr
4524 || AT_class (a) == dw_val_class_macptr
4525 || AT_class (a) == dw_val_class_high_pc));
4526 return a->dw_attr_val.v.val_lbl_id;
4529 /* Get the attribute of type attr_kind. */
4531 static dw_attr_ref
4532 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4534 dw_attr_ref a;
4535 unsigned ix;
4536 dw_die_ref spec = NULL;
4538 if (! die)
4539 return NULL;
4541 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4542 if (a->dw_attr == attr_kind)
4543 return a;
4544 else if (a->dw_attr == DW_AT_specification
4545 || a->dw_attr == DW_AT_abstract_origin)
4546 spec = AT_ref (a);
4548 if (spec)
4549 return get_AT (spec, attr_kind);
4551 return NULL;
4554 /* Returns the parent of the declaration of DIE. */
4556 static dw_die_ref
4557 get_die_parent (dw_die_ref die)
4559 dw_die_ref t;
4561 if (!die)
4562 return NULL;
4564 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4565 || (t = get_AT_ref (die, DW_AT_specification)))
4566 die = t;
4568 return die->die_parent;
4571 /* Return the "low pc" attribute value, typically associated with a subprogram
4572 DIE. Return null if the "low pc" attribute is either not present, or if it
4573 cannot be represented as an assembler label identifier. */
4575 static inline const char *
4576 get_AT_low_pc (dw_die_ref die)
4578 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4580 return a ? AT_lbl (a) : NULL;
4583 /* Return the "high pc" attribute value, typically associated with a subprogram
4584 DIE. Return null if the "high pc" attribute is either not present, or if it
4585 cannot be represented as an assembler label identifier. */
4587 static inline const char *
4588 get_AT_hi_pc (dw_die_ref die)
4590 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4592 return a ? AT_lbl (a) : NULL;
4595 /* Return the value of the string attribute designated by ATTR_KIND, or
4596 NULL if it is not present. */
4598 static inline const char *
4599 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4601 dw_attr_ref a = get_AT (die, attr_kind);
4603 return a ? AT_string (a) : NULL;
4606 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4607 if it is not present. */
4609 static inline int
4610 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4612 dw_attr_ref a = get_AT (die, attr_kind);
4614 return a ? AT_flag (a) : 0;
4617 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4618 if it is not present. */
4620 static inline unsigned
4621 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4623 dw_attr_ref a = get_AT (die, attr_kind);
4625 return a ? AT_unsigned (a) : 0;
4628 static inline dw_die_ref
4629 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4631 dw_attr_ref a = get_AT (die, attr_kind);
4633 return a ? AT_ref (a) : NULL;
4636 static inline struct dwarf_file_data *
4637 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4639 dw_attr_ref a = get_AT (die, attr_kind);
4641 return a ? AT_file (a) : NULL;
4644 /* Return TRUE if the language is C++. */
4646 static inline bool
4647 is_cxx (void)
4649 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4651 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4654 /* Return TRUE if the language is Java. */
4656 static inline bool
4657 is_java (void)
4659 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4661 return lang == DW_LANG_Java;
4664 /* Return TRUE if the language is Fortran. */
4666 static inline bool
4667 is_fortran (void)
4669 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4671 return (lang == DW_LANG_Fortran77
4672 || lang == DW_LANG_Fortran90
4673 || lang == DW_LANG_Fortran95);
4676 /* Return TRUE if the language is Ada. */
4678 static inline bool
4679 is_ada (void)
4681 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4683 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4686 /* Remove the specified attribute if present. */
4688 static void
4689 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4691 dw_attr_ref a;
4692 unsigned ix;
4694 if (! die)
4695 return;
4697 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4698 if (a->dw_attr == attr_kind)
4700 if (AT_class (a) == dw_val_class_str)
4701 if (a->dw_attr_val.v.val_str->refcount)
4702 a->dw_attr_val.v.val_str->refcount--;
4704 /* vec::ordered_remove should help reduce the number of abbrevs
4705 that are needed. */
4706 die->die_attr->ordered_remove (ix);
4707 return;
4711 /* Remove CHILD from its parent. PREV must have the property that
4712 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4714 static void
4715 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4717 gcc_assert (child->die_parent == prev->die_parent);
4718 gcc_assert (prev->die_sib == child);
4719 if (prev == child)
4721 gcc_assert (child->die_parent->die_child == child);
4722 prev = NULL;
4724 else
4725 prev->die_sib = child->die_sib;
4726 if (child->die_parent->die_child == child)
4727 child->die_parent->die_child = prev;
4730 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4731 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4733 static void
4734 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4736 dw_die_ref parent = old_child->die_parent;
4738 gcc_assert (parent == prev->die_parent);
4739 gcc_assert (prev->die_sib == old_child);
4741 new_child->die_parent = parent;
4742 if (prev == old_child)
4744 gcc_assert (parent->die_child == old_child);
4745 new_child->die_sib = new_child;
4747 else
4749 prev->die_sib = new_child;
4750 new_child->die_sib = old_child->die_sib;
4752 if (old_child->die_parent->die_child == old_child)
4753 old_child->die_parent->die_child = new_child;
4756 /* Move all children from OLD_PARENT to NEW_PARENT. */
4758 static void
4759 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4761 dw_die_ref c;
4762 new_parent->die_child = old_parent->die_child;
4763 old_parent->die_child = NULL;
4764 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4767 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4768 matches TAG. */
4770 static void
4771 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4773 dw_die_ref c;
4775 c = die->die_child;
4776 if (c) do {
4777 dw_die_ref prev = c;
4778 c = c->die_sib;
4779 while (c->die_tag == tag)
4781 remove_child_with_prev (c, prev);
4782 /* Might have removed every child. */
4783 if (c == c->die_sib)
4784 return;
4785 c = c->die_sib;
4787 } while (c != die->die_child);
4790 /* Add a CHILD_DIE as the last child of DIE. */
4792 static void
4793 add_child_die (dw_die_ref die, dw_die_ref child_die)
4795 /* FIXME this should probably be an assert. */
4796 if (! die || ! child_die)
4797 return;
4798 gcc_assert (die != child_die);
4800 child_die->die_parent = die;
4801 if (die->die_child)
4803 child_die->die_sib = die->die_child->die_sib;
4804 die->die_child->die_sib = child_die;
4806 else
4807 child_die->die_sib = child_die;
4808 die->die_child = child_die;
4811 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4812 is the specification, to the end of PARENT's list of children.
4813 This is done by removing and re-adding it. */
4815 static void
4816 splice_child_die (dw_die_ref parent, dw_die_ref child)
4818 dw_die_ref p;
4820 /* We want the declaration DIE from inside the class, not the
4821 specification DIE at toplevel. */
4822 if (child->die_parent != parent)
4824 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4826 if (tmp)
4827 child = tmp;
4830 gcc_assert (child->die_parent == parent
4831 || (child->die_parent
4832 == get_AT_ref (parent, DW_AT_specification)));
4834 for (p = child->die_parent->die_child; ; p = p->die_sib)
4835 if (p->die_sib == child)
4837 remove_child_with_prev (child, p);
4838 break;
4841 add_child_die (parent, child);
4844 /* Return a pointer to a newly created DIE node. */
4846 static inline dw_die_ref
4847 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4849 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4851 die->die_tag = tag_value;
4853 if (parent_die != NULL)
4854 add_child_die (parent_die, die);
4855 else
4857 limbo_die_node *limbo_node;
4859 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4860 limbo_node->die = die;
4861 limbo_node->created_for = t;
4862 limbo_node->next = limbo_die_list;
4863 limbo_die_list = limbo_node;
4866 return die;
4869 /* Return the DIE associated with the given type specifier. */
4871 static inline dw_die_ref
4872 lookup_type_die (tree type)
4874 return TYPE_SYMTAB_DIE (type);
4877 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4878 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4879 anonymous type instead the one of the naming typedef. */
4881 static inline dw_die_ref
4882 strip_naming_typedef (tree type, dw_die_ref type_die)
4884 if (type
4885 && TREE_CODE (type) == RECORD_TYPE
4886 && type_die
4887 && type_die->die_tag == DW_TAG_typedef
4888 && is_naming_typedef_decl (TYPE_NAME (type)))
4889 type_die = get_AT_ref (type_die, DW_AT_type);
4890 return type_die;
4893 /* Like lookup_type_die, but if type is an anonymous type named by a
4894 typedef[1], return the DIE of the anonymous type instead the one of
4895 the naming typedef. This is because in gen_typedef_die, we did
4896 equate the anonymous struct named by the typedef with the DIE of
4897 the naming typedef. So by default, lookup_type_die on an anonymous
4898 struct yields the DIE of the naming typedef.
4900 [1]: Read the comment of is_naming_typedef_decl to learn about what
4901 a naming typedef is. */
4903 static inline dw_die_ref
4904 lookup_type_die_strip_naming_typedef (tree type)
4906 dw_die_ref die = lookup_type_die (type);
4907 return strip_naming_typedef (type, die);
4910 /* Equate a DIE to a given type specifier. */
4912 static inline void
4913 equate_type_number_to_die (tree type, dw_die_ref type_die)
4915 TYPE_SYMTAB_DIE (type) = type_die;
4918 /* Returns a hash value for X (which really is a die_struct). */
4920 static hashval_t
4921 decl_die_table_hash (const void *x)
4923 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4926 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4928 static int
4929 decl_die_table_eq (const void *x, const void *y)
4931 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4934 /* Return the DIE associated with a given declaration. */
4936 static inline dw_die_ref
4937 lookup_decl_die (tree decl)
4939 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4942 /* Returns a hash value for X (which really is a var_loc_list). */
4944 static hashval_t
4945 decl_loc_table_hash (const void *x)
4947 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4950 /* Return nonzero if decl_id of var_loc_list X is the same as
4951 UID of decl *Y. */
4953 static int
4954 decl_loc_table_eq (const void *x, const void *y)
4956 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4959 /* Return the var_loc list associated with a given declaration. */
4961 static inline var_loc_list *
4962 lookup_decl_loc (const_tree decl)
4964 if (!decl_loc_table)
4965 return NULL;
4966 return (var_loc_list *)
4967 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4970 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4972 static hashval_t
4973 cached_dw_loc_list_table_hash (const void *x)
4975 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4978 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4979 UID of decl *Y. */
4981 static int
4982 cached_dw_loc_list_table_eq (const void *x, const void *y)
4984 return (((const cached_dw_loc_list *) x)->decl_id
4985 == DECL_UID ((const_tree) y));
4988 /* Equate a DIE to a particular declaration. */
4990 static void
4991 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4993 unsigned int decl_id = DECL_UID (decl);
4994 void **slot;
4996 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4997 *slot = decl_die;
4998 decl_die->decl_id = decl_id;
5001 /* Return how many bits covers PIECE EXPR_LIST. */
5003 static int
5004 decl_piece_bitsize (rtx piece)
5006 int ret = (int) GET_MODE (piece);
5007 if (ret)
5008 return ret;
5009 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5010 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5011 return INTVAL (XEXP (XEXP (piece, 0), 0));
5014 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5016 static rtx *
5017 decl_piece_varloc_ptr (rtx piece)
5019 if ((int) GET_MODE (piece))
5020 return &XEXP (piece, 0);
5021 else
5022 return &XEXP (XEXP (piece, 0), 1);
5025 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5026 Next is the chain of following piece nodes. */
5028 static rtx_expr_list *
5029 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5031 if (bitsize <= (int) MAX_MACHINE_MODE)
5032 return alloc_EXPR_LIST (bitsize, loc_note, next);
5033 else
5034 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5035 GEN_INT (bitsize),
5036 loc_note), next);
5039 /* Return rtx that should be stored into loc field for
5040 LOC_NOTE and BITPOS/BITSIZE. */
5042 static rtx
5043 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5044 HOST_WIDE_INT bitsize)
5046 if (bitsize != -1)
5048 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5049 if (bitpos != 0)
5050 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5052 return loc_note;
5055 /* This function either modifies location piece list *DEST in
5056 place (if SRC and INNER is NULL), or copies location piece list
5057 *SRC to *DEST while modifying it. Location BITPOS is modified
5058 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5059 not copied and if needed some padding around it is added.
5060 When modifying in place, DEST should point to EXPR_LIST where
5061 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5062 to the start of the whole list and INNER points to the EXPR_LIST
5063 where earlier pieces cover PIECE_BITPOS bits. */
5065 static void
5066 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5067 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5068 HOST_WIDE_INT bitsize, rtx loc_note)
5070 int diff;
5071 bool copy = inner != NULL;
5073 if (copy)
5075 /* First copy all nodes preceding the current bitpos. */
5076 while (src != inner)
5078 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5079 decl_piece_bitsize (*src), NULL_RTX);
5080 dest = &XEXP (*dest, 1);
5081 src = &XEXP (*src, 1);
5084 /* Add padding if needed. */
5085 if (bitpos != piece_bitpos)
5087 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5088 copy ? NULL_RTX : *dest);
5089 dest = &XEXP (*dest, 1);
5091 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5093 gcc_assert (!copy);
5094 /* A piece with correct bitpos and bitsize already exist,
5095 just update the location for it and return. */
5096 *decl_piece_varloc_ptr (*dest) = loc_note;
5097 return;
5099 /* Add the piece that changed. */
5100 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5101 dest = &XEXP (*dest, 1);
5102 /* Skip over pieces that overlap it. */
5103 diff = bitpos - piece_bitpos + bitsize;
5104 if (!copy)
5105 src = dest;
5106 while (diff > 0 && *src)
5108 rtx piece = *src;
5109 diff -= decl_piece_bitsize (piece);
5110 if (copy)
5111 src = &XEXP (piece, 1);
5112 else
5114 *src = XEXP (piece, 1);
5115 free_EXPR_LIST_node (piece);
5118 /* Add padding if needed. */
5119 if (diff < 0 && *src)
5121 if (!copy)
5122 dest = src;
5123 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5124 dest = &XEXP (*dest, 1);
5126 if (!copy)
5127 return;
5128 /* Finally copy all nodes following it. */
5129 while (*src)
5131 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5132 decl_piece_bitsize (*src), NULL_RTX);
5133 dest = &XEXP (*dest, 1);
5134 src = &XEXP (*src, 1);
5138 /* Add a variable location node to the linked list for DECL. */
5140 static struct var_loc_node *
5141 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5143 unsigned int decl_id;
5144 var_loc_list *temp;
5145 void **slot;
5146 struct var_loc_node *loc = NULL;
5147 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5149 if (TREE_CODE (decl) == VAR_DECL
5150 && DECL_HAS_DEBUG_EXPR_P (decl))
5152 tree realdecl = DECL_DEBUG_EXPR (decl);
5153 if (handled_component_p (realdecl)
5154 || (TREE_CODE (realdecl) == MEM_REF
5155 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5157 HOST_WIDE_INT maxsize;
5158 tree innerdecl;
5159 innerdecl
5160 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5161 if (!DECL_P (innerdecl)
5162 || DECL_IGNORED_P (innerdecl)
5163 || TREE_STATIC (innerdecl)
5164 || bitsize <= 0
5165 || bitpos + bitsize > 256
5166 || bitsize != maxsize)
5167 return NULL;
5168 decl = innerdecl;
5172 decl_id = DECL_UID (decl);
5173 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5174 if (*slot == NULL)
5176 temp = ggc_cleared_alloc<var_loc_list> ();
5177 temp->decl_id = decl_id;
5178 *slot = temp;
5180 else
5181 temp = (var_loc_list *) *slot;
5183 /* For PARM_DECLs try to keep around the original incoming value,
5184 even if that means we'll emit a zero-range .debug_loc entry. */
5185 if (temp->last
5186 && temp->first == temp->last
5187 && TREE_CODE (decl) == PARM_DECL
5188 && NOTE_P (temp->first->loc)
5189 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5190 && DECL_INCOMING_RTL (decl)
5191 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5192 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5193 == GET_CODE (DECL_INCOMING_RTL (decl))
5194 && prev_real_insn (temp->first->loc) == NULL_RTX
5195 && (bitsize != -1
5196 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5197 NOTE_VAR_LOCATION_LOC (loc_note))
5198 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5199 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5201 loc = ggc_cleared_alloc<var_loc_node> ();
5202 temp->first->next = loc;
5203 temp->last = loc;
5204 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5206 else if (temp->last)
5208 struct var_loc_node *last = temp->last, *unused = NULL;
5209 rtx *piece_loc = NULL, last_loc_note;
5210 int piece_bitpos = 0;
5211 if (last->next)
5213 last = last->next;
5214 gcc_assert (last->next == NULL);
5216 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5218 piece_loc = &last->loc;
5221 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5222 if (piece_bitpos + cur_bitsize > bitpos)
5223 break;
5224 piece_bitpos += cur_bitsize;
5225 piece_loc = &XEXP (*piece_loc, 1);
5227 while (*piece_loc);
5229 /* TEMP->LAST here is either pointer to the last but one or
5230 last element in the chained list, LAST is pointer to the
5231 last element. */
5232 if (label && strcmp (last->label, label) == 0)
5234 /* For SRA optimized variables if there weren't any real
5235 insns since last note, just modify the last node. */
5236 if (piece_loc != NULL)
5238 adjust_piece_list (piece_loc, NULL, NULL,
5239 bitpos, piece_bitpos, bitsize, loc_note);
5240 return NULL;
5242 /* If the last note doesn't cover any instructions, remove it. */
5243 if (temp->last != last)
5245 temp->last->next = NULL;
5246 unused = last;
5247 last = temp->last;
5248 gcc_assert (strcmp (last->label, label) != 0);
5250 else
5252 gcc_assert (temp->first == temp->last
5253 || (temp->first->next == temp->last
5254 && TREE_CODE (decl) == PARM_DECL));
5255 memset (temp->last, '\0', sizeof (*temp->last));
5256 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5257 return temp->last;
5260 if (bitsize == -1 && NOTE_P (last->loc))
5261 last_loc_note = last->loc;
5262 else if (piece_loc != NULL
5263 && *piece_loc != NULL_RTX
5264 && piece_bitpos == bitpos
5265 && decl_piece_bitsize (*piece_loc) == bitsize)
5266 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5267 else
5268 last_loc_note = NULL_RTX;
5269 /* If the current location is the same as the end of the list,
5270 and either both or neither of the locations is uninitialized,
5271 we have nothing to do. */
5272 if (last_loc_note == NULL_RTX
5273 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5274 NOTE_VAR_LOCATION_LOC (loc_note)))
5275 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5276 != NOTE_VAR_LOCATION_STATUS (loc_note))
5277 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5278 == VAR_INIT_STATUS_UNINITIALIZED)
5279 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5280 == VAR_INIT_STATUS_UNINITIALIZED))))
5282 /* Add LOC to the end of list and update LAST. If the last
5283 element of the list has been removed above, reuse its
5284 memory for the new node, otherwise allocate a new one. */
5285 if (unused)
5287 loc = unused;
5288 memset (loc, '\0', sizeof (*loc));
5290 else
5291 loc = ggc_cleared_alloc<var_loc_node> ();
5292 if (bitsize == -1 || piece_loc == NULL)
5293 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5294 else
5295 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5296 bitpos, piece_bitpos, bitsize, loc_note);
5297 last->next = loc;
5298 /* Ensure TEMP->LAST will point either to the new last but one
5299 element of the chain, or to the last element in it. */
5300 if (last != temp->last)
5301 temp->last = last;
5303 else if (unused)
5304 ggc_free (unused);
5306 else
5308 loc = ggc_cleared_alloc<var_loc_node> ();
5309 temp->first = loc;
5310 temp->last = loc;
5311 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5313 return loc;
5316 /* Keep track of the number of spaces used to indent the
5317 output of the debugging routines that print the structure of
5318 the DIE internal representation. */
5319 static int print_indent;
5321 /* Indent the line the number of spaces given by print_indent. */
5323 static inline void
5324 print_spaces (FILE *outfile)
5326 fprintf (outfile, "%*s", print_indent, "");
5329 /* Print a type signature in hex. */
5331 static inline void
5332 print_signature (FILE *outfile, char *sig)
5334 int i;
5336 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5337 fprintf (outfile, "%02x", sig[i] & 0xff);
5340 /* Print the information associated with a given DIE, and its children.
5341 This routine is a debugging aid only. */
5343 static void
5344 print_die (dw_die_ref die, FILE *outfile)
5346 dw_attr_ref a;
5347 dw_die_ref c;
5348 unsigned ix;
5350 print_spaces (outfile);
5351 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5352 die->die_offset, dwarf_tag_name (die->die_tag),
5353 (void*) die);
5354 print_spaces (outfile);
5355 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5356 fprintf (outfile, " offset: %ld", die->die_offset);
5357 fprintf (outfile, " mark: %d\n", die->die_mark);
5359 if (die->comdat_type_p)
5361 print_spaces (outfile);
5362 fprintf (outfile, " signature: ");
5363 print_signature (outfile, die->die_id.die_type_node->signature);
5364 fprintf (outfile, "\n");
5367 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5369 print_spaces (outfile);
5370 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5372 switch (AT_class (a))
5374 case dw_val_class_addr:
5375 fprintf (outfile, "address");
5376 break;
5377 case dw_val_class_offset:
5378 fprintf (outfile, "offset");
5379 break;
5380 case dw_val_class_loc:
5381 fprintf (outfile, "location descriptor");
5382 break;
5383 case dw_val_class_loc_list:
5384 fprintf (outfile, "location list -> label:%s",
5385 AT_loc_list (a)->ll_symbol);
5386 break;
5387 case dw_val_class_range_list:
5388 fprintf (outfile, "range list");
5389 break;
5390 case dw_val_class_const:
5391 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5392 break;
5393 case dw_val_class_unsigned_const:
5394 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5395 break;
5396 case dw_val_class_const_double:
5397 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5398 HOST_WIDE_INT_PRINT_UNSIGNED")",
5399 a->dw_attr_val.v.val_double.high,
5400 a->dw_attr_val.v.val_double.low);
5401 break;
5402 case dw_val_class_wide_int:
5404 int i = a->dw_attr_val.v.val_wide->get_len ();
5405 fprintf (outfile, "constant (");
5406 gcc_assert (i > 0);
5407 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5408 fprintf (outfile, "0x");
5409 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5410 a->dw_attr_val.v.val_wide->elt (--i));
5411 while (--i >= 0)
5412 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5413 a->dw_attr_val.v.val_wide->elt (i));
5414 fprintf (outfile, ")");
5415 break;
5417 case dw_val_class_vec:
5418 fprintf (outfile, "floating-point or vector constant");
5419 break;
5420 case dw_val_class_flag:
5421 fprintf (outfile, "%u", AT_flag (a));
5422 break;
5423 case dw_val_class_die_ref:
5424 if (AT_ref (a) != NULL)
5426 if (AT_ref (a)->comdat_type_p)
5428 fprintf (outfile, "die -> signature: ");
5429 print_signature (outfile,
5430 AT_ref (a)->die_id.die_type_node->signature);
5432 else if (AT_ref (a)->die_id.die_symbol)
5433 fprintf (outfile, "die -> label: %s",
5434 AT_ref (a)->die_id.die_symbol);
5435 else
5436 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5437 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5439 else
5440 fprintf (outfile, "die -> <null>");
5441 break;
5442 case dw_val_class_vms_delta:
5443 fprintf (outfile, "delta: @slotcount(%s-%s)",
5444 AT_vms_delta2 (a), AT_vms_delta1 (a));
5445 break;
5446 case dw_val_class_lbl_id:
5447 case dw_val_class_lineptr:
5448 case dw_val_class_macptr:
5449 case dw_val_class_high_pc:
5450 fprintf (outfile, "label: %s", AT_lbl (a));
5451 break;
5452 case dw_val_class_str:
5453 if (AT_string (a) != NULL)
5454 fprintf (outfile, "\"%s\"", AT_string (a));
5455 else
5456 fprintf (outfile, "<null>");
5457 break;
5458 case dw_val_class_file:
5459 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5460 AT_file (a)->emitted_number);
5461 break;
5462 case dw_val_class_data8:
5464 int i;
5466 for (i = 0; i < 8; i++)
5467 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5468 break;
5470 default:
5471 break;
5474 fprintf (outfile, "\n");
5477 if (die->die_child != NULL)
5479 print_indent += 4;
5480 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5481 print_indent -= 4;
5483 if (print_indent == 0)
5484 fprintf (outfile, "\n");
5487 /* Print the information collected for a given DIE. */
5489 DEBUG_FUNCTION void
5490 debug_dwarf_die (dw_die_ref die)
5492 print_die (die, stderr);
5495 DEBUG_FUNCTION void
5496 debug (die_struct &ref)
5498 print_die (&ref, stderr);
5501 DEBUG_FUNCTION void
5502 debug (die_struct *ptr)
5504 if (ptr)
5505 debug (*ptr);
5506 else
5507 fprintf (stderr, "<nil>\n");
5511 /* Print all DWARF information collected for the compilation unit.
5512 This routine is a debugging aid only. */
5514 DEBUG_FUNCTION void
5515 debug_dwarf (void)
5517 print_indent = 0;
5518 print_die (comp_unit_die (), stderr);
5521 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5522 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5523 DIE that marks the start of the DIEs for this include file. */
5525 static dw_die_ref
5526 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5528 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5529 dw_die_ref new_unit = gen_compile_unit_die (filename);
5531 new_unit->die_sib = old_unit;
5532 return new_unit;
5535 /* Close an include-file CU and reopen the enclosing one. */
5537 static dw_die_ref
5538 pop_compile_unit (dw_die_ref old_unit)
5540 dw_die_ref new_unit = old_unit->die_sib;
5542 old_unit->die_sib = NULL;
5543 return new_unit;
5546 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5547 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5548 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5550 /* Calculate the checksum of a location expression. */
5552 static inline void
5553 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5555 int tem;
5556 inchash::hash hstate;
5557 hashval_t hash;
5559 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5560 CHECKSUM (tem);
5561 hash_loc_operands (loc, hstate);
5562 hash = hstate.end();
5563 CHECKSUM (hash);
5566 /* Calculate the checksum of an attribute. */
5568 static void
5569 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5571 dw_loc_descr_ref loc;
5572 rtx r;
5574 CHECKSUM (at->dw_attr);
5576 /* We don't care that this was compiled with a different compiler
5577 snapshot; if the output is the same, that's what matters. */
5578 if (at->dw_attr == DW_AT_producer)
5579 return;
5581 switch (AT_class (at))
5583 case dw_val_class_const:
5584 CHECKSUM (at->dw_attr_val.v.val_int);
5585 break;
5586 case dw_val_class_unsigned_const:
5587 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5588 break;
5589 case dw_val_class_const_double:
5590 CHECKSUM (at->dw_attr_val.v.val_double);
5591 break;
5592 case dw_val_class_wide_int:
5593 CHECKSUM (*at->dw_attr_val.v.val_wide);
5594 break;
5595 case dw_val_class_vec:
5596 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5597 (at->dw_attr_val.v.val_vec.length
5598 * at->dw_attr_val.v.val_vec.elt_size));
5599 break;
5600 case dw_val_class_flag:
5601 CHECKSUM (at->dw_attr_val.v.val_flag);
5602 break;
5603 case dw_val_class_str:
5604 CHECKSUM_STRING (AT_string (at));
5605 break;
5607 case dw_val_class_addr:
5608 r = AT_addr (at);
5609 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5610 CHECKSUM_STRING (XSTR (r, 0));
5611 break;
5613 case dw_val_class_offset:
5614 CHECKSUM (at->dw_attr_val.v.val_offset);
5615 break;
5617 case dw_val_class_loc:
5618 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5619 loc_checksum (loc, ctx);
5620 break;
5622 case dw_val_class_die_ref:
5623 die_checksum (AT_ref (at), ctx, mark);
5624 break;
5626 case dw_val_class_fde_ref:
5627 case dw_val_class_vms_delta:
5628 case dw_val_class_lbl_id:
5629 case dw_val_class_lineptr:
5630 case dw_val_class_macptr:
5631 case dw_val_class_high_pc:
5632 break;
5634 case dw_val_class_file:
5635 CHECKSUM_STRING (AT_file (at)->filename);
5636 break;
5638 case dw_val_class_data8:
5639 CHECKSUM (at->dw_attr_val.v.val_data8);
5640 break;
5642 default:
5643 break;
5647 /* Calculate the checksum of a DIE. */
5649 static void
5650 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5652 dw_die_ref c;
5653 dw_attr_ref a;
5654 unsigned ix;
5656 /* To avoid infinite recursion. */
5657 if (die->die_mark)
5659 CHECKSUM (die->die_mark);
5660 return;
5662 die->die_mark = ++(*mark);
5664 CHECKSUM (die->die_tag);
5666 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5667 attr_checksum (a, ctx, mark);
5669 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5672 #undef CHECKSUM
5673 #undef CHECKSUM_BLOCK
5674 #undef CHECKSUM_STRING
5676 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5677 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5678 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5679 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5680 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5681 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5682 #define CHECKSUM_ATTR(FOO) \
5683 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5685 /* Calculate the checksum of a number in signed LEB128 format. */
5687 static void
5688 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5690 unsigned char byte;
5691 bool more;
5693 while (1)
5695 byte = (value & 0x7f);
5696 value >>= 7;
5697 more = !((value == 0 && (byte & 0x40) == 0)
5698 || (value == -1 && (byte & 0x40) != 0));
5699 if (more)
5700 byte |= 0x80;
5701 CHECKSUM (byte);
5702 if (!more)
5703 break;
5707 /* Calculate the checksum of a number in unsigned LEB128 format. */
5709 static void
5710 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5712 while (1)
5714 unsigned char byte = (value & 0x7f);
5715 value >>= 7;
5716 if (value != 0)
5717 /* More bytes to follow. */
5718 byte |= 0x80;
5719 CHECKSUM (byte);
5720 if (value == 0)
5721 break;
5725 /* Checksum the context of the DIE. This adds the names of any
5726 surrounding namespaces or structures to the checksum. */
5728 static void
5729 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5731 const char *name;
5732 dw_die_ref spec;
5733 int tag = die->die_tag;
5735 if (tag != DW_TAG_namespace
5736 && tag != DW_TAG_structure_type
5737 && tag != DW_TAG_class_type)
5738 return;
5740 name = get_AT_string (die, DW_AT_name);
5742 spec = get_AT_ref (die, DW_AT_specification);
5743 if (spec != NULL)
5744 die = spec;
5746 if (die->die_parent != NULL)
5747 checksum_die_context (die->die_parent, ctx);
5749 CHECKSUM_ULEB128 ('C');
5750 CHECKSUM_ULEB128 (tag);
5751 if (name != NULL)
5752 CHECKSUM_STRING (name);
5755 /* Calculate the checksum of a location expression. */
5757 static inline void
5758 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5760 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5761 were emitted as a DW_FORM_sdata instead of a location expression. */
5762 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5764 CHECKSUM_ULEB128 (DW_FORM_sdata);
5765 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5766 return;
5769 /* Otherwise, just checksum the raw location expression. */
5770 while (loc != NULL)
5772 inchash::hash hstate;
5773 hashval_t hash;
5775 CHECKSUM_ULEB128 (loc->dtprel);
5776 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5777 hash_loc_operands (loc, hstate);
5778 hash = hstate.end ();
5779 CHECKSUM (hash);
5780 loc = loc->dw_loc_next;
5784 /* Calculate the checksum of an attribute. */
5786 static void
5787 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5788 struct md5_ctx *ctx, int *mark)
5790 dw_loc_descr_ref loc;
5791 rtx r;
5793 if (AT_class (at) == dw_val_class_die_ref)
5795 dw_die_ref target_die = AT_ref (at);
5797 /* For pointer and reference types, we checksum only the (qualified)
5798 name of the target type (if there is a name). For friend entries,
5799 we checksum only the (qualified) name of the target type or function.
5800 This allows the checksum to remain the same whether the target type
5801 is complete or not. */
5802 if ((at->dw_attr == DW_AT_type
5803 && (tag == DW_TAG_pointer_type
5804 || tag == DW_TAG_reference_type
5805 || tag == DW_TAG_rvalue_reference_type
5806 || tag == DW_TAG_ptr_to_member_type))
5807 || (at->dw_attr == DW_AT_friend
5808 && tag == DW_TAG_friend))
5810 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5812 if (name_attr != NULL)
5814 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5816 if (decl == NULL)
5817 decl = target_die;
5818 CHECKSUM_ULEB128 ('N');
5819 CHECKSUM_ULEB128 (at->dw_attr);
5820 if (decl->die_parent != NULL)
5821 checksum_die_context (decl->die_parent, ctx);
5822 CHECKSUM_ULEB128 ('E');
5823 CHECKSUM_STRING (AT_string (name_attr));
5824 return;
5828 /* For all other references to another DIE, we check to see if the
5829 target DIE has already been visited. If it has, we emit a
5830 backward reference; if not, we descend recursively. */
5831 if (target_die->die_mark > 0)
5833 CHECKSUM_ULEB128 ('R');
5834 CHECKSUM_ULEB128 (at->dw_attr);
5835 CHECKSUM_ULEB128 (target_die->die_mark);
5837 else
5839 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5841 if (decl == NULL)
5842 decl = target_die;
5843 target_die->die_mark = ++(*mark);
5844 CHECKSUM_ULEB128 ('T');
5845 CHECKSUM_ULEB128 (at->dw_attr);
5846 if (decl->die_parent != NULL)
5847 checksum_die_context (decl->die_parent, ctx);
5848 die_checksum_ordered (target_die, ctx, mark);
5850 return;
5853 CHECKSUM_ULEB128 ('A');
5854 CHECKSUM_ULEB128 (at->dw_attr);
5856 switch (AT_class (at))
5858 case dw_val_class_const:
5859 CHECKSUM_ULEB128 (DW_FORM_sdata);
5860 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5861 break;
5863 case dw_val_class_unsigned_const:
5864 CHECKSUM_ULEB128 (DW_FORM_sdata);
5865 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5866 break;
5868 case dw_val_class_const_double:
5869 CHECKSUM_ULEB128 (DW_FORM_block);
5870 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5871 CHECKSUM (at->dw_attr_val.v.val_double);
5872 break;
5874 case dw_val_class_wide_int:
5875 CHECKSUM_ULEB128 (DW_FORM_block);
5876 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5877 CHECKSUM (*at->dw_attr_val.v.val_wide);
5878 break;
5880 case dw_val_class_vec:
5881 CHECKSUM_ULEB128 (DW_FORM_block);
5882 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5883 * at->dw_attr_val.v.val_vec.elt_size);
5884 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5885 (at->dw_attr_val.v.val_vec.length
5886 * at->dw_attr_val.v.val_vec.elt_size));
5887 break;
5889 case dw_val_class_flag:
5890 CHECKSUM_ULEB128 (DW_FORM_flag);
5891 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5892 break;
5894 case dw_val_class_str:
5895 CHECKSUM_ULEB128 (DW_FORM_string);
5896 CHECKSUM_STRING (AT_string (at));
5897 break;
5899 case dw_val_class_addr:
5900 r = AT_addr (at);
5901 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5902 CHECKSUM_ULEB128 (DW_FORM_string);
5903 CHECKSUM_STRING (XSTR (r, 0));
5904 break;
5906 case dw_val_class_offset:
5907 CHECKSUM_ULEB128 (DW_FORM_sdata);
5908 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5909 break;
5911 case dw_val_class_loc:
5912 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5913 loc_checksum_ordered (loc, ctx);
5914 break;
5916 case dw_val_class_fde_ref:
5917 case dw_val_class_lbl_id:
5918 case dw_val_class_lineptr:
5919 case dw_val_class_macptr:
5920 case dw_val_class_high_pc:
5921 break;
5923 case dw_val_class_file:
5924 CHECKSUM_ULEB128 (DW_FORM_string);
5925 CHECKSUM_STRING (AT_file (at)->filename);
5926 break;
5928 case dw_val_class_data8:
5929 CHECKSUM (at->dw_attr_val.v.val_data8);
5930 break;
5932 default:
5933 break;
5937 struct checksum_attributes
5939 dw_attr_ref at_name;
5940 dw_attr_ref at_type;
5941 dw_attr_ref at_friend;
5942 dw_attr_ref at_accessibility;
5943 dw_attr_ref at_address_class;
5944 dw_attr_ref at_allocated;
5945 dw_attr_ref at_artificial;
5946 dw_attr_ref at_associated;
5947 dw_attr_ref at_binary_scale;
5948 dw_attr_ref at_bit_offset;
5949 dw_attr_ref at_bit_size;
5950 dw_attr_ref at_bit_stride;
5951 dw_attr_ref at_byte_size;
5952 dw_attr_ref at_byte_stride;
5953 dw_attr_ref at_const_value;
5954 dw_attr_ref at_containing_type;
5955 dw_attr_ref at_count;
5956 dw_attr_ref at_data_location;
5957 dw_attr_ref at_data_member_location;
5958 dw_attr_ref at_decimal_scale;
5959 dw_attr_ref at_decimal_sign;
5960 dw_attr_ref at_default_value;
5961 dw_attr_ref at_digit_count;
5962 dw_attr_ref at_discr;
5963 dw_attr_ref at_discr_list;
5964 dw_attr_ref at_discr_value;
5965 dw_attr_ref at_encoding;
5966 dw_attr_ref at_endianity;
5967 dw_attr_ref at_explicit;
5968 dw_attr_ref at_is_optional;
5969 dw_attr_ref at_location;
5970 dw_attr_ref at_lower_bound;
5971 dw_attr_ref at_mutable;
5972 dw_attr_ref at_ordering;
5973 dw_attr_ref at_picture_string;
5974 dw_attr_ref at_prototyped;
5975 dw_attr_ref at_small;
5976 dw_attr_ref at_segment;
5977 dw_attr_ref at_string_length;
5978 dw_attr_ref at_threads_scaled;
5979 dw_attr_ref at_upper_bound;
5980 dw_attr_ref at_use_location;
5981 dw_attr_ref at_use_UTF8;
5982 dw_attr_ref at_variable_parameter;
5983 dw_attr_ref at_virtuality;
5984 dw_attr_ref at_visibility;
5985 dw_attr_ref at_vtable_elem_location;
5988 /* Collect the attributes that we will want to use for the checksum. */
5990 static void
5991 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5993 dw_attr_ref a;
5994 unsigned ix;
5996 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5998 switch (a->dw_attr)
6000 case DW_AT_name:
6001 attrs->at_name = a;
6002 break;
6003 case DW_AT_type:
6004 attrs->at_type = a;
6005 break;
6006 case DW_AT_friend:
6007 attrs->at_friend = a;
6008 break;
6009 case DW_AT_accessibility:
6010 attrs->at_accessibility = a;
6011 break;
6012 case DW_AT_address_class:
6013 attrs->at_address_class = a;
6014 break;
6015 case DW_AT_allocated:
6016 attrs->at_allocated = a;
6017 break;
6018 case DW_AT_artificial:
6019 attrs->at_artificial = a;
6020 break;
6021 case DW_AT_associated:
6022 attrs->at_associated = a;
6023 break;
6024 case DW_AT_binary_scale:
6025 attrs->at_binary_scale = a;
6026 break;
6027 case DW_AT_bit_offset:
6028 attrs->at_bit_offset = a;
6029 break;
6030 case DW_AT_bit_size:
6031 attrs->at_bit_size = a;
6032 break;
6033 case DW_AT_bit_stride:
6034 attrs->at_bit_stride = a;
6035 break;
6036 case DW_AT_byte_size:
6037 attrs->at_byte_size = a;
6038 break;
6039 case DW_AT_byte_stride:
6040 attrs->at_byte_stride = a;
6041 break;
6042 case DW_AT_const_value:
6043 attrs->at_const_value = a;
6044 break;
6045 case DW_AT_containing_type:
6046 attrs->at_containing_type = a;
6047 break;
6048 case DW_AT_count:
6049 attrs->at_count = a;
6050 break;
6051 case DW_AT_data_location:
6052 attrs->at_data_location = a;
6053 break;
6054 case DW_AT_data_member_location:
6055 attrs->at_data_member_location = a;
6056 break;
6057 case DW_AT_decimal_scale:
6058 attrs->at_decimal_scale = a;
6059 break;
6060 case DW_AT_decimal_sign:
6061 attrs->at_decimal_sign = a;
6062 break;
6063 case DW_AT_default_value:
6064 attrs->at_default_value = a;
6065 break;
6066 case DW_AT_digit_count:
6067 attrs->at_digit_count = a;
6068 break;
6069 case DW_AT_discr:
6070 attrs->at_discr = a;
6071 break;
6072 case DW_AT_discr_list:
6073 attrs->at_discr_list = a;
6074 break;
6075 case DW_AT_discr_value:
6076 attrs->at_discr_value = a;
6077 break;
6078 case DW_AT_encoding:
6079 attrs->at_encoding = a;
6080 break;
6081 case DW_AT_endianity:
6082 attrs->at_endianity = a;
6083 break;
6084 case DW_AT_explicit:
6085 attrs->at_explicit = a;
6086 break;
6087 case DW_AT_is_optional:
6088 attrs->at_is_optional = a;
6089 break;
6090 case DW_AT_location:
6091 attrs->at_location = a;
6092 break;
6093 case DW_AT_lower_bound:
6094 attrs->at_lower_bound = a;
6095 break;
6096 case DW_AT_mutable:
6097 attrs->at_mutable = a;
6098 break;
6099 case DW_AT_ordering:
6100 attrs->at_ordering = a;
6101 break;
6102 case DW_AT_picture_string:
6103 attrs->at_picture_string = a;
6104 break;
6105 case DW_AT_prototyped:
6106 attrs->at_prototyped = a;
6107 break;
6108 case DW_AT_small:
6109 attrs->at_small = a;
6110 break;
6111 case DW_AT_segment:
6112 attrs->at_segment = a;
6113 break;
6114 case DW_AT_string_length:
6115 attrs->at_string_length = a;
6116 break;
6117 case DW_AT_threads_scaled:
6118 attrs->at_threads_scaled = a;
6119 break;
6120 case DW_AT_upper_bound:
6121 attrs->at_upper_bound = a;
6122 break;
6123 case DW_AT_use_location:
6124 attrs->at_use_location = a;
6125 break;
6126 case DW_AT_use_UTF8:
6127 attrs->at_use_UTF8 = a;
6128 break;
6129 case DW_AT_variable_parameter:
6130 attrs->at_variable_parameter = a;
6131 break;
6132 case DW_AT_virtuality:
6133 attrs->at_virtuality = a;
6134 break;
6135 case DW_AT_visibility:
6136 attrs->at_visibility = a;
6137 break;
6138 case DW_AT_vtable_elem_location:
6139 attrs->at_vtable_elem_location = a;
6140 break;
6141 default:
6142 break;
6147 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6149 static void
6150 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6152 dw_die_ref c;
6153 dw_die_ref decl;
6154 struct checksum_attributes attrs;
6156 CHECKSUM_ULEB128 ('D');
6157 CHECKSUM_ULEB128 (die->die_tag);
6159 memset (&attrs, 0, sizeof (attrs));
6161 decl = get_AT_ref (die, DW_AT_specification);
6162 if (decl != NULL)
6163 collect_checksum_attributes (&attrs, decl);
6164 collect_checksum_attributes (&attrs, die);
6166 CHECKSUM_ATTR (attrs.at_name);
6167 CHECKSUM_ATTR (attrs.at_accessibility);
6168 CHECKSUM_ATTR (attrs.at_address_class);
6169 CHECKSUM_ATTR (attrs.at_allocated);
6170 CHECKSUM_ATTR (attrs.at_artificial);
6171 CHECKSUM_ATTR (attrs.at_associated);
6172 CHECKSUM_ATTR (attrs.at_binary_scale);
6173 CHECKSUM_ATTR (attrs.at_bit_offset);
6174 CHECKSUM_ATTR (attrs.at_bit_size);
6175 CHECKSUM_ATTR (attrs.at_bit_stride);
6176 CHECKSUM_ATTR (attrs.at_byte_size);
6177 CHECKSUM_ATTR (attrs.at_byte_stride);
6178 CHECKSUM_ATTR (attrs.at_const_value);
6179 CHECKSUM_ATTR (attrs.at_containing_type);
6180 CHECKSUM_ATTR (attrs.at_count);
6181 CHECKSUM_ATTR (attrs.at_data_location);
6182 CHECKSUM_ATTR (attrs.at_data_member_location);
6183 CHECKSUM_ATTR (attrs.at_decimal_scale);
6184 CHECKSUM_ATTR (attrs.at_decimal_sign);
6185 CHECKSUM_ATTR (attrs.at_default_value);
6186 CHECKSUM_ATTR (attrs.at_digit_count);
6187 CHECKSUM_ATTR (attrs.at_discr);
6188 CHECKSUM_ATTR (attrs.at_discr_list);
6189 CHECKSUM_ATTR (attrs.at_discr_value);
6190 CHECKSUM_ATTR (attrs.at_encoding);
6191 CHECKSUM_ATTR (attrs.at_endianity);
6192 CHECKSUM_ATTR (attrs.at_explicit);
6193 CHECKSUM_ATTR (attrs.at_is_optional);
6194 CHECKSUM_ATTR (attrs.at_location);
6195 CHECKSUM_ATTR (attrs.at_lower_bound);
6196 CHECKSUM_ATTR (attrs.at_mutable);
6197 CHECKSUM_ATTR (attrs.at_ordering);
6198 CHECKSUM_ATTR (attrs.at_picture_string);
6199 CHECKSUM_ATTR (attrs.at_prototyped);
6200 CHECKSUM_ATTR (attrs.at_small);
6201 CHECKSUM_ATTR (attrs.at_segment);
6202 CHECKSUM_ATTR (attrs.at_string_length);
6203 CHECKSUM_ATTR (attrs.at_threads_scaled);
6204 CHECKSUM_ATTR (attrs.at_upper_bound);
6205 CHECKSUM_ATTR (attrs.at_use_location);
6206 CHECKSUM_ATTR (attrs.at_use_UTF8);
6207 CHECKSUM_ATTR (attrs.at_variable_parameter);
6208 CHECKSUM_ATTR (attrs.at_virtuality);
6209 CHECKSUM_ATTR (attrs.at_visibility);
6210 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6211 CHECKSUM_ATTR (attrs.at_type);
6212 CHECKSUM_ATTR (attrs.at_friend);
6214 /* Checksum the child DIEs. */
6215 c = die->die_child;
6216 if (c) do {
6217 dw_attr_ref name_attr;
6219 c = c->die_sib;
6220 name_attr = get_AT (c, DW_AT_name);
6221 if (is_template_instantiation (c))
6223 /* Ignore instantiations of member type and function templates. */
6225 else if (name_attr != NULL
6226 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6228 /* Use a shallow checksum for named nested types and member
6229 functions. */
6230 CHECKSUM_ULEB128 ('S');
6231 CHECKSUM_ULEB128 (c->die_tag);
6232 CHECKSUM_STRING (AT_string (name_attr));
6234 else
6236 /* Use a deep checksum for other children. */
6237 /* Mark this DIE so it gets processed when unmarking. */
6238 if (c->die_mark == 0)
6239 c->die_mark = -1;
6240 die_checksum_ordered (c, ctx, mark);
6242 } while (c != die->die_child);
6244 CHECKSUM_ULEB128 (0);
6247 /* Add a type name and tag to a hash. */
6248 static void
6249 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6251 CHECKSUM_ULEB128 (tag);
6252 CHECKSUM_STRING (name);
6255 #undef CHECKSUM
6256 #undef CHECKSUM_STRING
6257 #undef CHECKSUM_ATTR
6258 #undef CHECKSUM_LEB128
6259 #undef CHECKSUM_ULEB128
6261 /* Generate the type signature for DIE. This is computed by generating an
6262 MD5 checksum over the DIE's tag, its relevant attributes, and its
6263 children. Attributes that are references to other DIEs are processed
6264 by recursion, using the MARK field to prevent infinite recursion.
6265 If the DIE is nested inside a namespace or another type, we also
6266 need to include that context in the signature. The lower 64 bits
6267 of the resulting MD5 checksum comprise the signature. */
6269 static void
6270 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6272 int mark;
6273 const char *name;
6274 unsigned char checksum[16];
6275 struct md5_ctx ctx;
6276 dw_die_ref decl;
6277 dw_die_ref parent;
6279 name = get_AT_string (die, DW_AT_name);
6280 decl = get_AT_ref (die, DW_AT_specification);
6281 parent = get_die_parent (die);
6283 /* First, compute a signature for just the type name (and its surrounding
6284 context, if any. This is stored in the type unit DIE for link-time
6285 ODR (one-definition rule) checking. */
6287 if (is_cxx () && name != NULL)
6289 md5_init_ctx (&ctx);
6291 /* Checksum the names of surrounding namespaces and structures. */
6292 if (parent != NULL)
6293 checksum_die_context (parent, &ctx);
6295 /* Checksum the current DIE. */
6296 die_odr_checksum (die->die_tag, name, &ctx);
6297 md5_finish_ctx (&ctx, checksum);
6299 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6302 /* Next, compute the complete type signature. */
6304 md5_init_ctx (&ctx);
6305 mark = 1;
6306 die->die_mark = mark;
6308 /* Checksum the names of surrounding namespaces and structures. */
6309 if (parent != NULL)
6310 checksum_die_context (parent, &ctx);
6312 /* Checksum the DIE and its children. */
6313 die_checksum_ordered (die, &ctx, &mark);
6314 unmark_all_dies (die);
6315 md5_finish_ctx (&ctx, checksum);
6317 /* Store the signature in the type node and link the type DIE and the
6318 type node together. */
6319 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6320 DWARF_TYPE_SIGNATURE_SIZE);
6321 die->comdat_type_p = true;
6322 die->die_id.die_type_node = type_node;
6323 type_node->type_die = die;
6325 /* If the DIE is a specification, link its declaration to the type node
6326 as well. */
6327 if (decl != NULL)
6329 decl->comdat_type_p = true;
6330 decl->die_id.die_type_node = type_node;
6334 /* Do the location expressions look same? */
6335 static inline int
6336 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6338 return loc1->dw_loc_opc == loc2->dw_loc_opc
6339 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6340 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6343 /* Do the values look the same? */
6344 static int
6345 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6347 dw_loc_descr_ref loc1, loc2;
6348 rtx r1, r2;
6350 if (v1->val_class != v2->val_class)
6351 return 0;
6353 switch (v1->val_class)
6355 case dw_val_class_const:
6356 return v1->v.val_int == v2->v.val_int;
6357 case dw_val_class_unsigned_const:
6358 return v1->v.val_unsigned == v2->v.val_unsigned;
6359 case dw_val_class_const_double:
6360 return v1->v.val_double.high == v2->v.val_double.high
6361 && v1->v.val_double.low == v2->v.val_double.low;
6362 case dw_val_class_wide_int:
6363 return *v1->v.val_wide == *v2->v.val_wide;
6364 case dw_val_class_vec:
6365 if (v1->v.val_vec.length != v2->v.val_vec.length
6366 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6367 return 0;
6368 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6369 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6370 return 0;
6371 return 1;
6372 case dw_val_class_flag:
6373 return v1->v.val_flag == v2->v.val_flag;
6374 case dw_val_class_str:
6375 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6377 case dw_val_class_addr:
6378 r1 = v1->v.val_addr;
6379 r2 = v2->v.val_addr;
6380 if (GET_CODE (r1) != GET_CODE (r2))
6381 return 0;
6382 return !rtx_equal_p (r1, r2);
6384 case dw_val_class_offset:
6385 return v1->v.val_offset == v2->v.val_offset;
6387 case dw_val_class_loc:
6388 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6389 loc1 && loc2;
6390 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6391 if (!same_loc_p (loc1, loc2, mark))
6392 return 0;
6393 return !loc1 && !loc2;
6395 case dw_val_class_die_ref:
6396 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6398 case dw_val_class_fde_ref:
6399 case dw_val_class_vms_delta:
6400 case dw_val_class_lbl_id:
6401 case dw_val_class_lineptr:
6402 case dw_val_class_macptr:
6403 case dw_val_class_high_pc:
6404 return 1;
6406 case dw_val_class_file:
6407 return v1->v.val_file == v2->v.val_file;
6409 case dw_val_class_data8:
6410 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6412 default:
6413 return 1;
6417 /* Do the attributes look the same? */
6419 static int
6420 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6422 if (at1->dw_attr != at2->dw_attr)
6423 return 0;
6425 /* We don't care that this was compiled with a different compiler
6426 snapshot; if the output is the same, that's what matters. */
6427 if (at1->dw_attr == DW_AT_producer)
6428 return 1;
6430 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6433 /* Do the dies look the same? */
6435 static int
6436 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6438 dw_die_ref c1, c2;
6439 dw_attr_ref a1;
6440 unsigned ix;
6442 /* To avoid infinite recursion. */
6443 if (die1->die_mark)
6444 return die1->die_mark == die2->die_mark;
6445 die1->die_mark = die2->die_mark = ++(*mark);
6447 if (die1->die_tag != die2->die_tag)
6448 return 0;
6450 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6451 return 0;
6453 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6454 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6455 return 0;
6457 c1 = die1->die_child;
6458 c2 = die2->die_child;
6459 if (! c1)
6461 if (c2)
6462 return 0;
6464 else
6465 for (;;)
6467 if (!same_die_p (c1, c2, mark))
6468 return 0;
6469 c1 = c1->die_sib;
6470 c2 = c2->die_sib;
6471 if (c1 == die1->die_child)
6473 if (c2 == die2->die_child)
6474 break;
6475 else
6476 return 0;
6480 return 1;
6483 /* Do the dies look the same? Wrapper around same_die_p. */
6485 static int
6486 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6488 int mark = 0;
6489 int ret = same_die_p (die1, die2, &mark);
6491 unmark_all_dies (die1);
6492 unmark_all_dies (die2);
6494 return ret;
6497 /* The prefix to attach to symbols on DIEs in the current comdat debug
6498 info section. */
6499 static const char *comdat_symbol_id;
6501 /* The index of the current symbol within the current comdat CU. */
6502 static unsigned int comdat_symbol_number;
6504 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6505 children, and set comdat_symbol_id accordingly. */
6507 static void
6508 compute_section_prefix (dw_die_ref unit_die)
6510 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6511 const char *base = die_name ? lbasename (die_name) : "anonymous";
6512 char *name = XALLOCAVEC (char, strlen (base) + 64);
6513 char *p;
6514 int i, mark;
6515 unsigned char checksum[16];
6516 struct md5_ctx ctx;
6518 /* Compute the checksum of the DIE, then append part of it as hex digits to
6519 the name filename of the unit. */
6521 md5_init_ctx (&ctx);
6522 mark = 0;
6523 die_checksum (unit_die, &ctx, &mark);
6524 unmark_all_dies (unit_die);
6525 md5_finish_ctx (&ctx, checksum);
6527 sprintf (name, "%s.", base);
6528 clean_symbol_name (name);
6530 p = name + strlen (name);
6531 for (i = 0; i < 4; i++)
6533 sprintf (p, "%.2x", checksum[i]);
6534 p += 2;
6537 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6538 comdat_symbol_number = 0;
6541 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6543 static int
6544 is_type_die (dw_die_ref die)
6546 switch (die->die_tag)
6548 case DW_TAG_array_type:
6549 case DW_TAG_class_type:
6550 case DW_TAG_interface_type:
6551 case DW_TAG_enumeration_type:
6552 case DW_TAG_pointer_type:
6553 case DW_TAG_reference_type:
6554 case DW_TAG_rvalue_reference_type:
6555 case DW_TAG_string_type:
6556 case DW_TAG_structure_type:
6557 case DW_TAG_subroutine_type:
6558 case DW_TAG_union_type:
6559 case DW_TAG_ptr_to_member_type:
6560 case DW_TAG_set_type:
6561 case DW_TAG_subrange_type:
6562 case DW_TAG_base_type:
6563 case DW_TAG_const_type:
6564 case DW_TAG_file_type:
6565 case DW_TAG_packed_type:
6566 case DW_TAG_volatile_type:
6567 case DW_TAG_typedef:
6568 return 1;
6569 default:
6570 return 0;
6574 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6575 Basically, we want to choose the bits that are likely to be shared between
6576 compilations (types) and leave out the bits that are specific to individual
6577 compilations (functions). */
6579 static int
6580 is_comdat_die (dw_die_ref c)
6582 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6583 we do for stabs. The advantage is a greater likelihood of sharing between
6584 objects that don't include headers in the same order (and therefore would
6585 put the base types in a different comdat). jason 8/28/00 */
6587 if (c->die_tag == DW_TAG_base_type)
6588 return 0;
6590 if (c->die_tag == DW_TAG_pointer_type
6591 || c->die_tag == DW_TAG_reference_type
6592 || c->die_tag == DW_TAG_rvalue_reference_type
6593 || c->die_tag == DW_TAG_const_type
6594 || c->die_tag == DW_TAG_volatile_type)
6596 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6598 return t ? is_comdat_die (t) : 0;
6601 return is_type_die (c);
6604 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6605 compilation unit. */
6607 static int
6608 is_symbol_die (dw_die_ref c)
6610 return (is_type_die (c)
6611 || is_declaration_die (c)
6612 || c->die_tag == DW_TAG_namespace
6613 || c->die_tag == DW_TAG_module);
6616 /* Returns true iff C is a compile-unit DIE. */
6618 static inline bool
6619 is_cu_die (dw_die_ref c)
6621 return c && c->die_tag == DW_TAG_compile_unit;
6624 /* Returns true iff C is a unit DIE of some sort. */
6626 static inline bool
6627 is_unit_die (dw_die_ref c)
6629 return c && (c->die_tag == DW_TAG_compile_unit
6630 || c->die_tag == DW_TAG_partial_unit
6631 || c->die_tag == DW_TAG_type_unit);
6634 /* Returns true iff C is a namespace DIE. */
6636 static inline bool
6637 is_namespace_die (dw_die_ref c)
6639 return c && c->die_tag == DW_TAG_namespace;
6642 /* Returns true iff C is a class or structure DIE. */
6644 static inline bool
6645 is_class_die (dw_die_ref c)
6647 return c && (c->die_tag == DW_TAG_class_type
6648 || c->die_tag == DW_TAG_structure_type);
6651 /* Return non-zero if this DIE is a template parameter. */
6653 static inline bool
6654 is_template_parameter (dw_die_ref die)
6656 switch (die->die_tag)
6658 case DW_TAG_template_type_param:
6659 case DW_TAG_template_value_param:
6660 case DW_TAG_GNU_template_template_param:
6661 case DW_TAG_GNU_template_parameter_pack:
6662 return true;
6663 default:
6664 return false;
6668 /* Return non-zero if this DIE represents a template instantiation. */
6670 static inline bool
6671 is_template_instantiation (dw_die_ref die)
6673 dw_die_ref c;
6675 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6676 return false;
6677 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6678 return false;
6681 static char *
6682 gen_internal_sym (const char *prefix)
6684 char buf[256];
6686 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6687 return xstrdup (buf);
6690 /* Assign symbols to all worthy DIEs under DIE. */
6692 static void
6693 assign_symbol_names (dw_die_ref die)
6695 dw_die_ref c;
6697 if (is_symbol_die (die) && !die->comdat_type_p)
6699 if (comdat_symbol_id)
6701 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6703 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6704 comdat_symbol_id, comdat_symbol_number++);
6705 die->die_id.die_symbol = xstrdup (p);
6707 else
6708 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6711 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6714 struct cu_hash_table_entry
6716 dw_die_ref cu;
6717 unsigned min_comdat_num, max_comdat_num;
6718 struct cu_hash_table_entry *next;
6721 /* Helpers to manipulate hash table of CUs. */
6723 struct cu_hash_table_entry_hasher
6725 typedef cu_hash_table_entry value_type;
6726 typedef die_struct compare_type;
6727 static inline hashval_t hash (const value_type *);
6728 static inline bool equal (const value_type *, const compare_type *);
6729 static inline void remove (value_type *);
6732 inline hashval_t
6733 cu_hash_table_entry_hasher::hash (const value_type *entry)
6735 return htab_hash_string (entry->cu->die_id.die_symbol);
6738 inline bool
6739 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6740 const compare_type *entry2)
6742 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6745 inline void
6746 cu_hash_table_entry_hasher::remove (value_type *entry)
6748 struct cu_hash_table_entry *next;
6750 while (entry)
6752 next = entry->next;
6753 free (entry);
6754 entry = next;
6758 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6760 /* Check whether we have already seen this CU and set up SYM_NUM
6761 accordingly. */
6762 static int
6763 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6765 struct cu_hash_table_entry dummy;
6766 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6768 dummy.max_comdat_num = 0;
6770 slot = htable->find_slot_with_hash (cu,
6771 htab_hash_string (cu->die_id.die_symbol),
6772 INSERT);
6773 entry = *slot;
6775 for (; entry; last = entry, entry = entry->next)
6777 if (same_die_p_wrap (cu, entry->cu))
6778 break;
6781 if (entry)
6783 *sym_num = entry->min_comdat_num;
6784 return 1;
6787 entry = XCNEW (struct cu_hash_table_entry);
6788 entry->cu = cu;
6789 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6790 entry->next = *slot;
6791 *slot = entry;
6793 return 0;
6796 /* Record SYM_NUM to record of CU in HTABLE. */
6797 static void
6798 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6799 unsigned int sym_num)
6801 struct cu_hash_table_entry **slot, *entry;
6803 slot = htable->find_slot_with_hash (cu,
6804 htab_hash_string (cu->die_id.die_symbol),
6805 NO_INSERT);
6806 entry = *slot;
6808 entry->max_comdat_num = sym_num;
6811 /* Traverse the DIE (which is always comp_unit_die), and set up
6812 additional compilation units for each of the include files we see
6813 bracketed by BINCL/EINCL. */
6815 static void
6816 break_out_includes (dw_die_ref die)
6818 dw_die_ref c;
6819 dw_die_ref unit = NULL;
6820 limbo_die_node *node, **pnode;
6822 c = die->die_child;
6823 if (c) do {
6824 dw_die_ref prev = c;
6825 c = c->die_sib;
6826 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6827 || (unit && is_comdat_die (c)))
6829 dw_die_ref next = c->die_sib;
6831 /* This DIE is for a secondary CU; remove it from the main one. */
6832 remove_child_with_prev (c, prev);
6834 if (c->die_tag == DW_TAG_GNU_BINCL)
6835 unit = push_new_compile_unit (unit, c);
6836 else if (c->die_tag == DW_TAG_GNU_EINCL)
6837 unit = pop_compile_unit (unit);
6838 else
6839 add_child_die (unit, c);
6840 c = next;
6841 if (c == die->die_child)
6842 break;
6844 } while (c != die->die_child);
6846 #if 0
6847 /* We can only use this in debugging, since the frontend doesn't check
6848 to make sure that we leave every include file we enter. */
6849 gcc_assert (!unit);
6850 #endif
6852 assign_symbol_names (die);
6853 cu_hash_type cu_hash_table (10);
6854 for (node = limbo_die_list, pnode = &limbo_die_list;
6855 node;
6856 node = node->next)
6858 int is_dupl;
6860 compute_section_prefix (node->die);
6861 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6862 &comdat_symbol_number);
6863 assign_symbol_names (node->die);
6864 if (is_dupl)
6865 *pnode = node->next;
6866 else
6868 pnode = &node->next;
6869 record_comdat_symbol_number (node->die, &cu_hash_table,
6870 comdat_symbol_number);
6875 /* Return non-zero if this DIE is a declaration. */
6877 static int
6878 is_declaration_die (dw_die_ref die)
6880 dw_attr_ref a;
6881 unsigned ix;
6883 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6884 if (a->dw_attr == DW_AT_declaration)
6885 return 1;
6887 return 0;
6890 /* Return non-zero if this DIE is nested inside a subprogram. */
6892 static int
6893 is_nested_in_subprogram (dw_die_ref die)
6895 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6897 if (decl == NULL)
6898 decl = die;
6899 return local_scope_p (decl);
6902 /* Return non-zero if this DIE contains a defining declaration of a
6903 subprogram. */
6905 static int
6906 contains_subprogram_definition (dw_die_ref die)
6908 dw_die_ref c;
6910 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6911 return 1;
6912 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6913 return 0;
6916 /* Return non-zero if this is a type DIE that should be moved to a
6917 COMDAT .debug_types section. */
6919 static int
6920 should_move_die_to_comdat (dw_die_ref die)
6922 switch (die->die_tag)
6924 case DW_TAG_class_type:
6925 case DW_TAG_structure_type:
6926 case DW_TAG_enumeration_type:
6927 case DW_TAG_union_type:
6928 /* Don't move declarations, inlined instances, types nested in a
6929 subprogram, or types that contain subprogram definitions. */
6930 if (is_declaration_die (die)
6931 || get_AT (die, DW_AT_abstract_origin)
6932 || is_nested_in_subprogram (die)
6933 || contains_subprogram_definition (die))
6934 return 0;
6935 return 1;
6936 case DW_TAG_array_type:
6937 case DW_TAG_interface_type:
6938 case DW_TAG_pointer_type:
6939 case DW_TAG_reference_type:
6940 case DW_TAG_rvalue_reference_type:
6941 case DW_TAG_string_type:
6942 case DW_TAG_subroutine_type:
6943 case DW_TAG_ptr_to_member_type:
6944 case DW_TAG_set_type:
6945 case DW_TAG_subrange_type:
6946 case DW_TAG_base_type:
6947 case DW_TAG_const_type:
6948 case DW_TAG_file_type:
6949 case DW_TAG_packed_type:
6950 case DW_TAG_volatile_type:
6951 case DW_TAG_typedef:
6952 default:
6953 return 0;
6957 /* Make a clone of DIE. */
6959 static dw_die_ref
6960 clone_die (dw_die_ref die)
6962 dw_die_ref clone;
6963 dw_attr_ref a;
6964 unsigned ix;
6966 clone = ggc_cleared_alloc<die_node> ();
6967 clone->die_tag = die->die_tag;
6969 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6970 add_dwarf_attr (clone, a);
6972 return clone;
6975 /* Make a clone of the tree rooted at DIE. */
6977 static dw_die_ref
6978 clone_tree (dw_die_ref die)
6980 dw_die_ref c;
6981 dw_die_ref clone = clone_die (die);
6983 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6985 return clone;
6988 /* Make a clone of DIE as a declaration. */
6990 static dw_die_ref
6991 clone_as_declaration (dw_die_ref die)
6993 dw_die_ref clone;
6994 dw_die_ref decl;
6995 dw_attr_ref a;
6996 unsigned ix;
6998 /* If the DIE is already a declaration, just clone it. */
6999 if (is_declaration_die (die))
7000 return clone_die (die);
7002 /* If the DIE is a specification, just clone its declaration DIE. */
7003 decl = get_AT_ref (die, DW_AT_specification);
7004 if (decl != NULL)
7006 clone = clone_die (decl);
7007 if (die->comdat_type_p)
7008 add_AT_die_ref (clone, DW_AT_signature, die);
7009 return clone;
7012 clone = ggc_cleared_alloc<die_node> ();
7013 clone->die_tag = die->die_tag;
7015 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7017 /* We don't want to copy over all attributes.
7018 For example we don't want DW_AT_byte_size because otherwise we will no
7019 longer have a declaration and GDB will treat it as a definition. */
7021 switch (a->dw_attr)
7023 case DW_AT_abstract_origin:
7024 case DW_AT_artificial:
7025 case DW_AT_containing_type:
7026 case DW_AT_external:
7027 case DW_AT_name:
7028 case DW_AT_type:
7029 case DW_AT_virtuality:
7030 case DW_AT_linkage_name:
7031 case DW_AT_MIPS_linkage_name:
7032 add_dwarf_attr (clone, a);
7033 break;
7034 case DW_AT_byte_size:
7035 default:
7036 break;
7040 if (die->comdat_type_p)
7041 add_AT_die_ref (clone, DW_AT_signature, die);
7043 add_AT_flag (clone, DW_AT_declaration, 1);
7044 return clone;
7048 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7050 struct decl_table_entry
7052 dw_die_ref orig;
7053 dw_die_ref copy;
7056 /* Helpers to manipulate hash table of copied declarations. */
7058 /* Hashtable helpers. */
7060 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7062 typedef decl_table_entry value_type;
7063 typedef die_struct compare_type;
7064 static inline hashval_t hash (const value_type *);
7065 static inline bool equal (const value_type *, const compare_type *);
7068 inline hashval_t
7069 decl_table_entry_hasher::hash (const value_type *entry)
7071 return htab_hash_pointer (entry->orig);
7074 inline bool
7075 decl_table_entry_hasher::equal (const value_type *entry1,
7076 const compare_type *entry2)
7078 return entry1->orig == entry2;
7081 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7083 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7084 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7085 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7086 to check if the ancestor has already been copied into UNIT. */
7088 static dw_die_ref
7089 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7090 decl_hash_type *decl_table)
7092 dw_die_ref parent = die->die_parent;
7093 dw_die_ref new_parent = unit;
7094 dw_die_ref copy;
7095 decl_table_entry **slot = NULL;
7096 struct decl_table_entry *entry = NULL;
7098 if (decl_table)
7100 /* Check if the entry has already been copied to UNIT. */
7101 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7102 INSERT);
7103 if (*slot != HTAB_EMPTY_ENTRY)
7105 entry = *slot;
7106 return entry->copy;
7109 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7110 entry = XCNEW (struct decl_table_entry);
7111 entry->orig = die;
7112 entry->copy = NULL;
7113 *slot = entry;
7116 if (parent != NULL)
7118 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7119 if (spec != NULL)
7120 parent = spec;
7121 if (!is_unit_die (parent))
7122 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7125 copy = clone_as_declaration (die);
7126 add_child_die (new_parent, copy);
7128 if (decl_table)
7130 /* Record the pointer to the copy. */
7131 entry->copy = copy;
7134 return copy;
7136 /* Copy the declaration context to the new type unit DIE. This includes
7137 any surrounding namespace or type declarations. If the DIE has an
7138 AT_specification attribute, it also includes attributes and children
7139 attached to the specification, and returns a pointer to the original
7140 parent of the declaration DIE. Returns NULL otherwise. */
7142 static dw_die_ref
7143 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7145 dw_die_ref decl;
7146 dw_die_ref new_decl;
7147 dw_die_ref orig_parent = NULL;
7149 decl = get_AT_ref (die, DW_AT_specification);
7150 if (decl == NULL)
7151 decl = die;
7152 else
7154 unsigned ix;
7155 dw_die_ref c;
7156 dw_attr_ref a;
7158 /* The original DIE will be changed to a declaration, and must
7159 be moved to be a child of the original declaration DIE. */
7160 orig_parent = decl->die_parent;
7162 /* Copy the type node pointer from the new DIE to the original
7163 declaration DIE so we can forward references later. */
7164 decl->comdat_type_p = true;
7165 decl->die_id.die_type_node = die->die_id.die_type_node;
7167 remove_AT (die, DW_AT_specification);
7169 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7171 if (a->dw_attr != DW_AT_name
7172 && a->dw_attr != DW_AT_declaration
7173 && a->dw_attr != DW_AT_external)
7174 add_dwarf_attr (die, a);
7177 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7180 if (decl->die_parent != NULL
7181 && !is_unit_die (decl->die_parent))
7183 new_decl = copy_ancestor_tree (unit, decl, NULL);
7184 if (new_decl != NULL)
7186 remove_AT (new_decl, DW_AT_signature);
7187 add_AT_specification (die, new_decl);
7191 return orig_parent;
7194 /* Generate the skeleton ancestor tree for the given NODE, then clone
7195 the DIE and add the clone into the tree. */
7197 static void
7198 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7200 if (node->new_die != NULL)
7201 return;
7203 node->new_die = clone_as_declaration (node->old_die);
7205 if (node->parent != NULL)
7207 generate_skeleton_ancestor_tree (node->parent);
7208 add_child_die (node->parent->new_die, node->new_die);
7212 /* Generate a skeleton tree of DIEs containing any declarations that are
7213 found in the original tree. We traverse the tree looking for declaration
7214 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7216 static void
7217 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7219 skeleton_chain_node node;
7220 dw_die_ref c;
7221 dw_die_ref first;
7222 dw_die_ref prev = NULL;
7223 dw_die_ref next = NULL;
7225 node.parent = parent;
7227 first = c = parent->old_die->die_child;
7228 if (c)
7229 next = c->die_sib;
7230 if (c) do {
7231 if (prev == NULL || prev->die_sib == c)
7232 prev = c;
7233 c = next;
7234 next = (c == first ? NULL : c->die_sib);
7235 node.old_die = c;
7236 node.new_die = NULL;
7237 if (is_declaration_die (c))
7239 if (is_template_instantiation (c))
7241 /* Instantiated templates do not need to be cloned into the
7242 type unit. Just move the DIE and its children back to
7243 the skeleton tree (in the main CU). */
7244 remove_child_with_prev (c, prev);
7245 add_child_die (parent->new_die, c);
7246 c = prev;
7248 else
7250 /* Clone the existing DIE, move the original to the skeleton
7251 tree (which is in the main CU), and put the clone, with
7252 all the original's children, where the original came from
7253 (which is about to be moved to the type unit). */
7254 dw_die_ref clone = clone_die (c);
7255 move_all_children (c, clone);
7257 /* If the original has a DW_AT_object_pointer attribute,
7258 it would now point to a child DIE just moved to the
7259 cloned tree, so we need to remove that attribute from
7260 the original. */
7261 remove_AT (c, DW_AT_object_pointer);
7263 replace_child (c, clone, prev);
7264 generate_skeleton_ancestor_tree (parent);
7265 add_child_die (parent->new_die, c);
7266 node.new_die = c;
7267 c = clone;
7270 generate_skeleton_bottom_up (&node);
7271 } while (next != NULL);
7274 /* Wrapper function for generate_skeleton_bottom_up. */
7276 static dw_die_ref
7277 generate_skeleton (dw_die_ref die)
7279 skeleton_chain_node node;
7281 node.old_die = die;
7282 node.new_die = NULL;
7283 node.parent = NULL;
7285 /* If this type definition is nested inside another type,
7286 and is not an instantiation of a template, always leave
7287 at least a declaration in its place. */
7288 if (die->die_parent != NULL
7289 && is_type_die (die->die_parent)
7290 && !is_template_instantiation (die))
7291 node.new_die = clone_as_declaration (die);
7293 generate_skeleton_bottom_up (&node);
7294 return node.new_die;
7297 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7298 declaration. The original DIE is moved to a new compile unit so that
7299 existing references to it follow it to the new location. If any of the
7300 original DIE's descendants is a declaration, we need to replace the
7301 original DIE with a skeleton tree and move the declarations back into the
7302 skeleton tree. */
7304 static dw_die_ref
7305 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7306 dw_die_ref prev)
7308 dw_die_ref skeleton, orig_parent;
7310 /* Copy the declaration context to the type unit DIE. If the returned
7311 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7312 that DIE. */
7313 orig_parent = copy_declaration_context (unit, child);
7315 skeleton = generate_skeleton (child);
7316 if (skeleton == NULL)
7317 remove_child_with_prev (child, prev);
7318 else
7320 skeleton->comdat_type_p = true;
7321 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7323 /* If the original DIE was a specification, we need to put
7324 the skeleton under the parent DIE of the declaration.
7325 This leaves the original declaration in the tree, but
7326 it will be pruned later since there are no longer any
7327 references to it. */
7328 if (orig_parent != NULL)
7330 remove_child_with_prev (child, prev);
7331 add_child_die (orig_parent, skeleton);
7333 else
7334 replace_child (child, skeleton, prev);
7337 return skeleton;
7340 /* Traverse the DIE and set up additional .debug_types sections for each
7341 type worthy of being placed in a COMDAT section. */
7343 static void
7344 break_out_comdat_types (dw_die_ref die)
7346 dw_die_ref c;
7347 dw_die_ref first;
7348 dw_die_ref prev = NULL;
7349 dw_die_ref next = NULL;
7350 dw_die_ref unit = NULL;
7352 first = c = die->die_child;
7353 if (c)
7354 next = c->die_sib;
7355 if (c) do {
7356 if (prev == NULL || prev->die_sib == c)
7357 prev = c;
7358 c = next;
7359 next = (c == first ? NULL : c->die_sib);
7360 if (should_move_die_to_comdat (c))
7362 dw_die_ref replacement;
7363 comdat_type_node_ref type_node;
7365 /* Break out nested types into their own type units. */
7366 break_out_comdat_types (c);
7368 /* Create a new type unit DIE as the root for the new tree, and
7369 add it to the list of comdat types. */
7370 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7371 add_AT_unsigned (unit, DW_AT_language,
7372 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7373 type_node = ggc_cleared_alloc<comdat_type_node> ();
7374 type_node->root_die = unit;
7375 type_node->next = comdat_type_list;
7376 comdat_type_list = type_node;
7378 /* Generate the type signature. */
7379 generate_type_signature (c, type_node);
7381 /* Copy the declaration context, attributes, and children of the
7382 declaration into the new type unit DIE, then remove this DIE
7383 from the main CU (or replace it with a skeleton if necessary). */
7384 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7385 type_node->skeleton_die = replacement;
7387 /* Add the DIE to the new compunit. */
7388 add_child_die (unit, c);
7390 if (replacement != NULL)
7391 c = replacement;
7393 else if (c->die_tag == DW_TAG_namespace
7394 || c->die_tag == DW_TAG_class_type
7395 || c->die_tag == DW_TAG_structure_type
7396 || c->die_tag == DW_TAG_union_type)
7398 /* Look for nested types that can be broken out. */
7399 break_out_comdat_types (c);
7401 } while (next != NULL);
7404 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7405 Enter all the cloned children into the hash table decl_table. */
7407 static dw_die_ref
7408 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7410 dw_die_ref c;
7411 dw_die_ref clone;
7412 struct decl_table_entry *entry;
7413 decl_table_entry **slot;
7415 if (die->die_tag == DW_TAG_subprogram)
7416 clone = clone_as_declaration (die);
7417 else
7418 clone = clone_die (die);
7420 slot = decl_table->find_slot_with_hash (die,
7421 htab_hash_pointer (die), INSERT);
7423 /* Assert that DIE isn't in the hash table yet. If it would be there
7424 before, the ancestors would be necessarily there as well, therefore
7425 clone_tree_partial wouldn't be called. */
7426 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7428 entry = XCNEW (struct decl_table_entry);
7429 entry->orig = die;
7430 entry->copy = clone;
7431 *slot = entry;
7433 if (die->die_tag != DW_TAG_subprogram)
7434 FOR_EACH_CHILD (die, c,
7435 add_child_die (clone, clone_tree_partial (c, decl_table)));
7437 return clone;
7440 /* Walk the DIE and its children, looking for references to incomplete
7441 or trivial types that are unmarked (i.e., that are not in the current
7442 type_unit). */
7444 static void
7445 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7447 dw_die_ref c;
7448 dw_attr_ref a;
7449 unsigned ix;
7451 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7453 if (AT_class (a) == dw_val_class_die_ref)
7455 dw_die_ref targ = AT_ref (a);
7456 decl_table_entry **slot;
7457 struct decl_table_entry *entry;
7459 if (targ->die_mark != 0 || targ->comdat_type_p)
7460 continue;
7462 slot = decl_table->find_slot_with_hash (targ,
7463 htab_hash_pointer (targ),
7464 INSERT);
7466 if (*slot != HTAB_EMPTY_ENTRY)
7468 /* TARG has already been copied, so we just need to
7469 modify the reference to point to the copy. */
7470 entry = *slot;
7471 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7473 else
7475 dw_die_ref parent = unit;
7476 dw_die_ref copy = clone_die (targ);
7478 /* Record in DECL_TABLE that TARG has been copied.
7479 Need to do this now, before the recursive call,
7480 because DECL_TABLE may be expanded and SLOT
7481 would no longer be a valid pointer. */
7482 entry = XCNEW (struct decl_table_entry);
7483 entry->orig = targ;
7484 entry->copy = copy;
7485 *slot = entry;
7487 /* If TARG is not a declaration DIE, we need to copy its
7488 children. */
7489 if (!is_declaration_die (targ))
7491 FOR_EACH_CHILD (
7492 targ, c,
7493 add_child_die (copy,
7494 clone_tree_partial (c, decl_table)));
7497 /* Make sure the cloned tree is marked as part of the
7498 type unit. */
7499 mark_dies (copy);
7501 /* If TARG has surrounding context, copy its ancestor tree
7502 into the new type unit. */
7503 if (targ->die_parent != NULL
7504 && !is_unit_die (targ->die_parent))
7505 parent = copy_ancestor_tree (unit, targ->die_parent,
7506 decl_table);
7508 add_child_die (parent, copy);
7509 a->dw_attr_val.v.val_die_ref.die = copy;
7511 /* Make sure the newly-copied DIE is walked. If it was
7512 installed in a previously-added context, it won't
7513 get visited otherwise. */
7514 if (parent != unit)
7516 /* Find the highest point of the newly-added tree,
7517 mark each node along the way, and walk from there. */
7518 parent->die_mark = 1;
7519 while (parent->die_parent
7520 && parent->die_parent->die_mark == 0)
7522 parent = parent->die_parent;
7523 parent->die_mark = 1;
7525 copy_decls_walk (unit, parent, decl_table);
7531 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7534 /* Copy declarations for "unworthy" types into the new comdat section.
7535 Incomplete types, modified types, and certain other types aren't broken
7536 out into comdat sections of their own, so they don't have a signature,
7537 and we need to copy the declaration into the same section so that we
7538 don't have an external reference. */
7540 static void
7541 copy_decls_for_unworthy_types (dw_die_ref unit)
7543 mark_dies (unit);
7544 decl_hash_type decl_table (10);
7545 copy_decls_walk (unit, unit, &decl_table);
7546 unmark_dies (unit);
7549 /* Traverse the DIE and add a sibling attribute if it may have the
7550 effect of speeding up access to siblings. To save some space,
7551 avoid generating sibling attributes for DIE's without children. */
7553 static void
7554 add_sibling_attributes (dw_die_ref die)
7556 dw_die_ref c;
7558 if (! die->die_child)
7559 return;
7561 if (die->die_parent && die != die->die_parent->die_child)
7562 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7564 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7567 /* Output all location lists for the DIE and its children. */
7569 static void
7570 output_location_lists (dw_die_ref die)
7572 dw_die_ref c;
7573 dw_attr_ref a;
7574 unsigned ix;
7576 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7577 if (AT_class (a) == dw_val_class_loc_list)
7578 output_loc_list (AT_loc_list (a));
7580 FOR_EACH_CHILD (die, c, output_location_lists (c));
7583 /* We want to limit the number of external references, because they are
7584 larger than local references: a relocation takes multiple words, and
7585 even a sig8 reference is always eight bytes, whereas a local reference
7586 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7587 So if we encounter multiple external references to the same type DIE, we
7588 make a local typedef stub for it and redirect all references there.
7590 This is the element of the hash table for keeping track of these
7591 references. */
7593 struct external_ref
7595 dw_die_ref type;
7596 dw_die_ref stub;
7597 unsigned n_refs;
7600 /* Hashtable helpers. */
7602 struct external_ref_hasher : typed_free_remove <external_ref>
7604 typedef external_ref value_type;
7605 typedef external_ref compare_type;
7606 static inline hashval_t hash (const value_type *);
7607 static inline bool equal (const value_type *, const compare_type *);
7610 inline hashval_t
7611 external_ref_hasher::hash (const value_type *r)
7613 dw_die_ref die = r->type;
7614 hashval_t h = 0;
7616 /* We can't use the address of the DIE for hashing, because
7617 that will make the order of the stub DIEs non-deterministic. */
7618 if (! die->comdat_type_p)
7619 /* We have a symbol; use it to compute a hash. */
7620 h = htab_hash_string (die->die_id.die_symbol);
7621 else
7623 /* We have a type signature; use a subset of the bits as the hash.
7624 The 8-byte signature is at least as large as hashval_t. */
7625 comdat_type_node_ref type_node = die->die_id.die_type_node;
7626 memcpy (&h, type_node->signature, sizeof (h));
7628 return h;
7631 inline bool
7632 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7634 return r1->type == r2->type;
7637 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7639 /* Return a pointer to the external_ref for references to DIE. */
7641 static struct external_ref *
7642 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7644 struct external_ref ref, *ref_p;
7645 external_ref **slot;
7647 ref.type = die;
7648 slot = map->find_slot (&ref, INSERT);
7649 if (*slot != HTAB_EMPTY_ENTRY)
7650 return *slot;
7652 ref_p = XCNEW (struct external_ref);
7653 ref_p->type = die;
7654 *slot = ref_p;
7655 return ref_p;
7658 /* Subroutine of optimize_external_refs, below.
7660 If we see a type skeleton, record it as our stub. If we see external
7661 references, remember how many we've seen. */
7663 static void
7664 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7666 dw_die_ref c;
7667 dw_attr_ref a;
7668 unsigned ix;
7669 struct external_ref *ref_p;
7671 if (is_type_die (die)
7672 && (c = get_AT_ref (die, DW_AT_signature)))
7674 /* This is a local skeleton; use it for local references. */
7675 ref_p = lookup_external_ref (map, c);
7676 ref_p->stub = die;
7679 /* Scan the DIE references, and remember any that refer to DIEs from
7680 other CUs (i.e. those which are not marked). */
7681 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7682 if (AT_class (a) == dw_val_class_die_ref
7683 && (c = AT_ref (a))->die_mark == 0
7684 && is_type_die (c))
7686 ref_p = lookup_external_ref (map, c);
7687 ref_p->n_refs++;
7690 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7693 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7694 points to an external_ref, DATA is the CU we're processing. If we don't
7695 already have a local stub, and we have multiple refs, build a stub. */
7698 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7700 struct external_ref *ref_p = *slot;
7702 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7704 /* We have multiple references to this type, so build a small stub.
7705 Both of these forms are a bit dodgy from the perspective of the
7706 DWARF standard, since technically they should have names. */
7707 dw_die_ref cu = data;
7708 dw_die_ref type = ref_p->type;
7709 dw_die_ref stub = NULL;
7711 if (type->comdat_type_p)
7713 /* If we refer to this type via sig8, use AT_signature. */
7714 stub = new_die (type->die_tag, cu, NULL_TREE);
7715 add_AT_die_ref (stub, DW_AT_signature, type);
7717 else
7719 /* Otherwise, use a typedef with no name. */
7720 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7721 add_AT_die_ref (stub, DW_AT_type, type);
7724 stub->die_mark++;
7725 ref_p->stub = stub;
7727 return 1;
7730 /* DIE is a unit; look through all the DIE references to see if there are
7731 any external references to types, and if so, create local stubs for
7732 them which will be applied in build_abbrev_table. This is useful because
7733 references to local DIEs are smaller. */
7735 static external_ref_hash_type *
7736 optimize_external_refs (dw_die_ref die)
7738 external_ref_hash_type *map = new external_ref_hash_type (10);
7739 optimize_external_refs_1 (die, map);
7740 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7741 return map;
7744 /* The format of each DIE (and its attribute value pairs) is encoded in an
7745 abbreviation table. This routine builds the abbreviation table and assigns
7746 a unique abbreviation id for each abbreviation entry. The children of each
7747 die are visited recursively. */
7749 static void
7750 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7752 unsigned long abbrev_id;
7753 unsigned int n_alloc;
7754 dw_die_ref c;
7755 dw_attr_ref a;
7756 unsigned ix;
7758 /* Scan the DIE references, and replace any that refer to
7759 DIEs from other CUs (i.e. those which are not marked) with
7760 the local stubs we built in optimize_external_refs. */
7761 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7762 if (AT_class (a) == dw_val_class_die_ref
7763 && (c = AT_ref (a))->die_mark == 0)
7765 struct external_ref *ref_p;
7766 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7768 ref_p = lookup_external_ref (extern_map, c);
7769 if (ref_p->stub && ref_p->stub != die)
7770 change_AT_die_ref (a, ref_p->stub);
7771 else
7772 /* We aren't changing this reference, so mark it external. */
7773 set_AT_ref_external (a, 1);
7776 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7778 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7779 dw_attr_ref die_a, abbrev_a;
7780 unsigned ix;
7781 bool ok = true;
7783 if (abbrev->die_tag != die->die_tag)
7784 continue;
7785 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7786 continue;
7788 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7789 continue;
7791 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7793 abbrev_a = &(*abbrev->die_attr)[ix];
7794 if ((abbrev_a->dw_attr != die_a->dw_attr)
7795 || (value_format (abbrev_a) != value_format (die_a)))
7797 ok = false;
7798 break;
7801 if (ok)
7802 break;
7805 if (abbrev_id >= abbrev_die_table_in_use)
7807 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7809 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7810 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7811 n_alloc);
7813 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7814 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7815 abbrev_die_table_allocated = n_alloc;
7818 ++abbrev_die_table_in_use;
7819 abbrev_die_table[abbrev_id] = die;
7822 die->die_abbrev = abbrev_id;
7823 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7826 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7828 static int
7829 constant_size (unsigned HOST_WIDE_INT value)
7831 int log;
7833 if (value == 0)
7834 log = 0;
7835 else
7836 log = floor_log2 (value);
7838 log = log / 8;
7839 log = 1 << (floor_log2 (log) + 1);
7841 return log;
7844 /* Return the size of a DIE as it is represented in the
7845 .debug_info section. */
7847 static unsigned long
7848 size_of_die (dw_die_ref die)
7850 unsigned long size = 0;
7851 dw_attr_ref a;
7852 unsigned ix;
7853 enum dwarf_form form;
7855 size += size_of_uleb128 (die->die_abbrev);
7856 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7858 switch (AT_class (a))
7860 case dw_val_class_addr:
7861 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7863 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7864 size += size_of_uleb128 (AT_index (a));
7866 else
7867 size += DWARF2_ADDR_SIZE;
7868 break;
7869 case dw_val_class_offset:
7870 size += DWARF_OFFSET_SIZE;
7871 break;
7872 case dw_val_class_loc:
7874 unsigned long lsize = size_of_locs (AT_loc (a));
7876 /* Block length. */
7877 if (dwarf_version >= 4)
7878 size += size_of_uleb128 (lsize);
7879 else
7880 size += constant_size (lsize);
7881 size += lsize;
7883 break;
7884 case dw_val_class_loc_list:
7885 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7887 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7888 size += size_of_uleb128 (AT_index (a));
7890 else
7891 size += DWARF_OFFSET_SIZE;
7892 break;
7893 case dw_val_class_range_list:
7894 size += DWARF_OFFSET_SIZE;
7895 break;
7896 case dw_val_class_const:
7897 size += size_of_sleb128 (AT_int (a));
7898 break;
7899 case dw_val_class_unsigned_const:
7901 int csize = constant_size (AT_unsigned (a));
7902 if (dwarf_version == 3
7903 && a->dw_attr == DW_AT_data_member_location
7904 && csize >= 4)
7905 size += size_of_uleb128 (AT_unsigned (a));
7906 else
7907 size += csize;
7909 break;
7910 case dw_val_class_const_double:
7911 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7912 if (HOST_BITS_PER_WIDE_INT >= 64)
7913 size++; /* block */
7914 break;
7915 case dw_val_class_wide_int:
7916 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7917 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7918 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7919 > 64)
7920 size++; /* block */
7921 break;
7922 case dw_val_class_vec:
7923 size += constant_size (a->dw_attr_val.v.val_vec.length
7924 * a->dw_attr_val.v.val_vec.elt_size)
7925 + a->dw_attr_val.v.val_vec.length
7926 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7927 break;
7928 case dw_val_class_flag:
7929 if (dwarf_version >= 4)
7930 /* Currently all add_AT_flag calls pass in 1 as last argument,
7931 so DW_FORM_flag_present can be used. If that ever changes,
7932 we'll need to use DW_FORM_flag and have some optimization
7933 in build_abbrev_table that will change those to
7934 DW_FORM_flag_present if it is set to 1 in all DIEs using
7935 the same abbrev entry. */
7936 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7937 else
7938 size += 1;
7939 break;
7940 case dw_val_class_die_ref:
7941 if (AT_ref_external (a))
7943 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7944 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7945 is sized by target address length, whereas in DWARF3
7946 it's always sized as an offset. */
7947 if (use_debug_types)
7948 size += DWARF_TYPE_SIGNATURE_SIZE;
7949 else if (dwarf_version == 2)
7950 size += DWARF2_ADDR_SIZE;
7951 else
7952 size += DWARF_OFFSET_SIZE;
7954 else
7955 size += DWARF_OFFSET_SIZE;
7956 break;
7957 case dw_val_class_fde_ref:
7958 size += DWARF_OFFSET_SIZE;
7959 break;
7960 case dw_val_class_lbl_id:
7961 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7963 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7964 size += size_of_uleb128 (AT_index (a));
7966 else
7967 size += DWARF2_ADDR_SIZE;
7968 break;
7969 case dw_val_class_lineptr:
7970 case dw_val_class_macptr:
7971 size += DWARF_OFFSET_SIZE;
7972 break;
7973 case dw_val_class_str:
7974 form = AT_string_form (a);
7975 if (form == DW_FORM_strp)
7976 size += DWARF_OFFSET_SIZE;
7977 else if (form == DW_FORM_GNU_str_index)
7978 size += size_of_uleb128 (AT_index (a));
7979 else
7980 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7981 break;
7982 case dw_val_class_file:
7983 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7984 break;
7985 case dw_val_class_data8:
7986 size += 8;
7987 break;
7988 case dw_val_class_vms_delta:
7989 size += DWARF_OFFSET_SIZE;
7990 break;
7991 case dw_val_class_high_pc:
7992 size += DWARF2_ADDR_SIZE;
7993 break;
7994 default:
7995 gcc_unreachable ();
7999 return size;
8002 /* Size the debugging information associated with a given DIE. Visits the
8003 DIE's children recursively. Updates the global variable next_die_offset, on
8004 each time through. Uses the current value of next_die_offset to update the
8005 die_offset field in each DIE. */
8007 static void
8008 calc_die_sizes (dw_die_ref die)
8010 dw_die_ref c;
8012 gcc_assert (die->die_offset == 0
8013 || (unsigned long int) die->die_offset == next_die_offset);
8014 die->die_offset = next_die_offset;
8015 next_die_offset += size_of_die (die);
8017 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8019 if (die->die_child != NULL)
8020 /* Count the null byte used to terminate sibling lists. */
8021 next_die_offset += 1;
8024 /* Size just the base type children at the start of the CU.
8025 This is needed because build_abbrev needs to size locs
8026 and sizing of type based stack ops needs to know die_offset
8027 values for the base types. */
8029 static void
8030 calc_base_type_die_sizes (void)
8032 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8033 unsigned int i;
8034 dw_die_ref base_type;
8035 #if ENABLE_ASSERT_CHECKING
8036 dw_die_ref prev = comp_unit_die ()->die_child;
8037 #endif
8039 die_offset += size_of_die (comp_unit_die ());
8040 for (i = 0; base_types.iterate (i, &base_type); i++)
8042 #if ENABLE_ASSERT_CHECKING
8043 gcc_assert (base_type->die_offset == 0
8044 && prev->die_sib == base_type
8045 && base_type->die_child == NULL
8046 && base_type->die_abbrev);
8047 prev = base_type;
8048 #endif
8049 base_type->die_offset = die_offset;
8050 die_offset += size_of_die (base_type);
8054 /* Set the marks for a die and its children. We do this so
8055 that we know whether or not a reference needs to use FORM_ref_addr; only
8056 DIEs in the same CU will be marked. We used to clear out the offset
8057 and use that as the flag, but ran into ordering problems. */
8059 static void
8060 mark_dies (dw_die_ref die)
8062 dw_die_ref c;
8064 gcc_assert (!die->die_mark);
8066 die->die_mark = 1;
8067 FOR_EACH_CHILD (die, c, mark_dies (c));
8070 /* Clear the marks for a die and its children. */
8072 static void
8073 unmark_dies (dw_die_ref die)
8075 dw_die_ref c;
8077 if (! use_debug_types)
8078 gcc_assert (die->die_mark);
8080 die->die_mark = 0;
8081 FOR_EACH_CHILD (die, c, unmark_dies (c));
8084 /* Clear the marks for a die, its children and referred dies. */
8086 static void
8087 unmark_all_dies (dw_die_ref die)
8089 dw_die_ref c;
8090 dw_attr_ref a;
8091 unsigned ix;
8093 if (!die->die_mark)
8094 return;
8095 die->die_mark = 0;
8097 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8099 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8100 if (AT_class (a) == dw_val_class_die_ref)
8101 unmark_all_dies (AT_ref (a));
8104 /* Calculate if the entry should appear in the final output file. It may be
8105 from a pruned a type. */
8107 static bool
8108 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8110 /* By limiting gnu pubnames to definitions only, gold can generate a
8111 gdb index without entries for declarations, which don't include
8112 enough information to be useful. */
8113 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8114 return false;
8116 if (table == pubname_table)
8118 /* Enumerator names are part of the pubname table, but the
8119 parent DW_TAG_enumeration_type die may have been pruned.
8120 Don't output them if that is the case. */
8121 if (p->die->die_tag == DW_TAG_enumerator &&
8122 (p->die->die_parent == NULL
8123 || !p->die->die_parent->die_perennial_p))
8124 return false;
8126 /* Everything else in the pubname table is included. */
8127 return true;
8130 /* The pubtypes table shouldn't include types that have been
8131 pruned. */
8132 return (p->die->die_offset != 0
8133 || !flag_eliminate_unused_debug_types);
8136 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8137 generated for the compilation unit. */
8139 static unsigned long
8140 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8142 unsigned long size;
8143 unsigned i;
8144 pubname_ref p;
8145 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8147 size = DWARF_PUBNAMES_HEADER_SIZE;
8148 FOR_EACH_VEC_ELT (*names, i, p)
8149 if (include_pubname_in_output (names, p))
8150 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8152 size += DWARF_OFFSET_SIZE;
8153 return size;
8156 /* Return the size of the information in the .debug_aranges section. */
8158 static unsigned long
8159 size_of_aranges (void)
8161 unsigned long size;
8163 size = DWARF_ARANGES_HEADER_SIZE;
8165 /* Count the address/length pair for this compilation unit. */
8166 if (text_section_used)
8167 size += 2 * DWARF2_ADDR_SIZE;
8168 if (cold_text_section_used)
8169 size += 2 * DWARF2_ADDR_SIZE;
8170 if (have_multiple_function_sections)
8172 unsigned fde_idx;
8173 dw_fde_ref fde;
8175 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8177 if (DECL_IGNORED_P (fde->decl))
8178 continue;
8179 if (!fde->in_std_section)
8180 size += 2 * DWARF2_ADDR_SIZE;
8181 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8182 size += 2 * DWARF2_ADDR_SIZE;
8186 /* Count the two zero words used to terminated the address range table. */
8187 size += 2 * DWARF2_ADDR_SIZE;
8188 return size;
8191 /* Select the encoding of an attribute value. */
8193 static enum dwarf_form
8194 value_format (dw_attr_ref a)
8196 switch (AT_class (a))
8198 case dw_val_class_addr:
8199 /* Only very few attributes allow DW_FORM_addr. */
8200 switch (a->dw_attr)
8202 case DW_AT_low_pc:
8203 case DW_AT_high_pc:
8204 case DW_AT_entry_pc:
8205 case DW_AT_trampoline:
8206 return (AT_index (a) == NOT_INDEXED
8207 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8208 default:
8209 break;
8211 switch (DWARF2_ADDR_SIZE)
8213 case 1:
8214 return DW_FORM_data1;
8215 case 2:
8216 return DW_FORM_data2;
8217 case 4:
8218 return DW_FORM_data4;
8219 case 8:
8220 return DW_FORM_data8;
8221 default:
8222 gcc_unreachable ();
8224 case dw_val_class_range_list:
8225 case dw_val_class_loc_list:
8226 if (dwarf_version >= 4)
8227 return DW_FORM_sec_offset;
8228 /* FALLTHRU */
8229 case dw_val_class_vms_delta:
8230 case dw_val_class_offset:
8231 switch (DWARF_OFFSET_SIZE)
8233 case 4:
8234 return DW_FORM_data4;
8235 case 8:
8236 return DW_FORM_data8;
8237 default:
8238 gcc_unreachable ();
8240 case dw_val_class_loc:
8241 if (dwarf_version >= 4)
8242 return DW_FORM_exprloc;
8243 switch (constant_size (size_of_locs (AT_loc (a))))
8245 case 1:
8246 return DW_FORM_block1;
8247 case 2:
8248 return DW_FORM_block2;
8249 case 4:
8250 return DW_FORM_block4;
8251 default:
8252 gcc_unreachable ();
8254 case dw_val_class_const:
8255 return DW_FORM_sdata;
8256 case dw_val_class_unsigned_const:
8257 switch (constant_size (AT_unsigned (a)))
8259 case 1:
8260 return DW_FORM_data1;
8261 case 2:
8262 return DW_FORM_data2;
8263 case 4:
8264 /* In DWARF3 DW_AT_data_member_location with
8265 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8266 constant, so we need to use DW_FORM_udata if we need
8267 a large constant. */
8268 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8269 return DW_FORM_udata;
8270 return DW_FORM_data4;
8271 case 8:
8272 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8273 return DW_FORM_udata;
8274 return DW_FORM_data8;
8275 default:
8276 gcc_unreachable ();
8278 case dw_val_class_const_double:
8279 switch (HOST_BITS_PER_WIDE_INT)
8281 case 8:
8282 return DW_FORM_data2;
8283 case 16:
8284 return DW_FORM_data4;
8285 case 32:
8286 return DW_FORM_data8;
8287 case 64:
8288 default:
8289 return DW_FORM_block1;
8291 case dw_val_class_wide_int:
8292 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8294 case 8:
8295 return DW_FORM_data1;
8296 case 16:
8297 return DW_FORM_data2;
8298 case 32:
8299 return DW_FORM_data4;
8300 case 64:
8301 return DW_FORM_data8;
8302 default:
8303 return DW_FORM_block1;
8305 case dw_val_class_vec:
8306 switch (constant_size (a->dw_attr_val.v.val_vec.length
8307 * a->dw_attr_val.v.val_vec.elt_size))
8309 case 1:
8310 return DW_FORM_block1;
8311 case 2:
8312 return DW_FORM_block2;
8313 case 4:
8314 return DW_FORM_block4;
8315 default:
8316 gcc_unreachable ();
8318 case dw_val_class_flag:
8319 if (dwarf_version >= 4)
8321 /* Currently all add_AT_flag calls pass in 1 as last argument,
8322 so DW_FORM_flag_present can be used. If that ever changes,
8323 we'll need to use DW_FORM_flag and have some optimization
8324 in build_abbrev_table that will change those to
8325 DW_FORM_flag_present if it is set to 1 in all DIEs using
8326 the same abbrev entry. */
8327 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8328 return DW_FORM_flag_present;
8330 return DW_FORM_flag;
8331 case dw_val_class_die_ref:
8332 if (AT_ref_external (a))
8333 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8334 else
8335 return DW_FORM_ref;
8336 case dw_val_class_fde_ref:
8337 return DW_FORM_data;
8338 case dw_val_class_lbl_id:
8339 return (AT_index (a) == NOT_INDEXED
8340 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8341 case dw_val_class_lineptr:
8342 case dw_val_class_macptr:
8343 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8344 case dw_val_class_str:
8345 return AT_string_form (a);
8346 case dw_val_class_file:
8347 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8349 case 1:
8350 return DW_FORM_data1;
8351 case 2:
8352 return DW_FORM_data2;
8353 case 4:
8354 return DW_FORM_data4;
8355 default:
8356 gcc_unreachable ();
8359 case dw_val_class_data8:
8360 return DW_FORM_data8;
8362 case dw_val_class_high_pc:
8363 switch (DWARF2_ADDR_SIZE)
8365 case 1:
8366 return DW_FORM_data1;
8367 case 2:
8368 return DW_FORM_data2;
8369 case 4:
8370 return DW_FORM_data4;
8371 case 8:
8372 return DW_FORM_data8;
8373 default:
8374 gcc_unreachable ();
8377 default:
8378 gcc_unreachable ();
8382 /* Output the encoding of an attribute value. */
8384 static void
8385 output_value_format (dw_attr_ref a)
8387 enum dwarf_form form = value_format (a);
8389 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8392 /* Given a die and id, produce the appropriate abbreviations. */
8394 static void
8395 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8397 unsigned ix;
8398 dw_attr_ref a_attr;
8400 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8401 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8402 dwarf_tag_name (abbrev->die_tag));
8404 if (abbrev->die_child != NULL)
8405 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8406 else
8407 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8409 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8411 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8412 dwarf_attr_name (a_attr->dw_attr));
8413 output_value_format (a_attr);
8416 dw2_asm_output_data (1, 0, NULL);
8417 dw2_asm_output_data (1, 0, NULL);
8421 /* Output the .debug_abbrev section which defines the DIE abbreviation
8422 table. */
8424 static void
8425 output_abbrev_section (void)
8427 unsigned long abbrev_id;
8429 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8430 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8432 /* Terminate the table. */
8433 dw2_asm_output_data (1, 0, NULL);
8436 /* Output a symbol we can use to refer to this DIE from another CU. */
8438 static inline void
8439 output_die_symbol (dw_die_ref die)
8441 const char *sym = die->die_id.die_symbol;
8443 gcc_assert (!die->comdat_type_p);
8445 if (sym == 0)
8446 return;
8448 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8449 /* We make these global, not weak; if the target doesn't support
8450 .linkonce, it doesn't support combining the sections, so debugging
8451 will break. */
8452 targetm.asm_out.globalize_label (asm_out_file, sym);
8454 ASM_OUTPUT_LABEL (asm_out_file, sym);
8457 /* Return a new location list, given the begin and end range, and the
8458 expression. */
8460 static inline dw_loc_list_ref
8461 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8462 const char *section)
8464 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8466 retlist->begin = begin;
8467 retlist->begin_entry = NULL;
8468 retlist->end = end;
8469 retlist->expr = expr;
8470 retlist->section = section;
8472 return retlist;
8475 /* Generate a new internal symbol for this location list node, if it
8476 hasn't got one yet. */
8478 static inline void
8479 gen_llsym (dw_loc_list_ref list)
8481 gcc_assert (!list->ll_symbol);
8482 list->ll_symbol = gen_internal_sym ("LLST");
8485 /* Output the location list given to us. */
8487 static void
8488 output_loc_list (dw_loc_list_ref list_head)
8490 dw_loc_list_ref curr = list_head;
8492 if (list_head->emitted)
8493 return;
8494 list_head->emitted = true;
8496 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8498 /* Walk the location list, and output each range + expression. */
8499 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8501 unsigned long size;
8502 /* Don't output an entry that starts and ends at the same address. */
8503 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8504 continue;
8505 size = size_of_locs (curr->expr);
8506 /* If the expression is too large, drop it on the floor. We could
8507 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8508 in the expression, but >= 64KB expressions for a single value
8509 in a single range are unlikely very useful. */
8510 if (size > 0xffff)
8511 continue;
8512 if (dwarf_split_debug_info)
8514 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8515 "Location list start/length entry (%s)",
8516 list_head->ll_symbol);
8517 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8518 "Location list range start index (%s)",
8519 curr->begin);
8520 /* The length field is 4 bytes. If we ever need to support
8521 an 8-byte length, we can add a new DW_LLE code or fall back
8522 to DW_LLE_GNU_start_end_entry. */
8523 dw2_asm_output_delta (4, curr->end, curr->begin,
8524 "Location list range length (%s)",
8525 list_head->ll_symbol);
8527 else if (!have_multiple_function_sections)
8529 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8530 "Location list begin address (%s)",
8531 list_head->ll_symbol);
8532 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8533 "Location list end address (%s)",
8534 list_head->ll_symbol);
8536 else
8538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8539 "Location list begin address (%s)",
8540 list_head->ll_symbol);
8541 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8542 "Location list end address (%s)",
8543 list_head->ll_symbol);
8546 /* Output the block length for this list of location operations. */
8547 gcc_assert (size <= 0xffff);
8548 dw2_asm_output_data (2, size, "%s", "Location expression size");
8550 output_loc_sequence (curr->expr, -1);
8553 if (dwarf_split_debug_info)
8554 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8555 "Location list terminator (%s)",
8556 list_head->ll_symbol);
8557 else
8559 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8560 "Location list terminator begin (%s)",
8561 list_head->ll_symbol);
8562 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8563 "Location list terminator end (%s)",
8564 list_head->ll_symbol);
8568 /* Output a range_list offset into the debug_range section. Emit a
8569 relocated reference if val_entry is NULL, otherwise, emit an
8570 indirect reference. */
8572 static void
8573 output_range_list_offset (dw_attr_ref a)
8575 const char *name = dwarf_attr_name (a->dw_attr);
8577 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8579 char *p = strchr (ranges_section_label, '\0');
8580 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8581 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8582 debug_ranges_section, "%s", name);
8583 *p = '\0';
8585 else
8586 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8587 "%s (offset from %s)", name, ranges_section_label);
8590 /* Output the offset into the debug_loc section. */
8592 static void
8593 output_loc_list_offset (dw_attr_ref a)
8595 char *sym = AT_loc_list (a)->ll_symbol;
8597 gcc_assert (sym);
8598 if (dwarf_split_debug_info)
8599 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8600 "%s", dwarf_attr_name (a->dw_attr));
8601 else
8602 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8603 "%s", dwarf_attr_name (a->dw_attr));
8606 /* Output an attribute's index or value appropriately. */
8608 static void
8609 output_attr_index_or_value (dw_attr_ref a)
8611 const char *name = dwarf_attr_name (a->dw_attr);
8613 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8615 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8616 return;
8618 switch (AT_class (a))
8620 case dw_val_class_addr:
8621 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8622 break;
8623 case dw_val_class_high_pc:
8624 case dw_val_class_lbl_id:
8625 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8626 break;
8627 case dw_val_class_loc_list:
8628 output_loc_list_offset (a);
8629 break;
8630 default:
8631 gcc_unreachable ();
8635 /* Output a type signature. */
8637 static inline void
8638 output_signature (const char *sig, const char *name)
8640 int i;
8642 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8643 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8646 /* Output the DIE and its attributes. Called recursively to generate
8647 the definitions of each child DIE. */
8649 static void
8650 output_die (dw_die_ref die)
8652 dw_attr_ref a;
8653 dw_die_ref c;
8654 unsigned long size;
8655 unsigned ix;
8657 /* If someone in another CU might refer to us, set up a symbol for
8658 them to point to. */
8659 if (! die->comdat_type_p && die->die_id.die_symbol)
8660 output_die_symbol (die);
8662 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8663 (unsigned long)die->die_offset,
8664 dwarf_tag_name (die->die_tag));
8666 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8668 const char *name = dwarf_attr_name (a->dw_attr);
8670 switch (AT_class (a))
8672 case dw_val_class_addr:
8673 output_attr_index_or_value (a);
8674 break;
8676 case dw_val_class_offset:
8677 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8678 "%s", name);
8679 break;
8681 case dw_val_class_range_list:
8682 output_range_list_offset (a);
8683 break;
8685 case dw_val_class_loc:
8686 size = size_of_locs (AT_loc (a));
8688 /* Output the block length for this list of location operations. */
8689 if (dwarf_version >= 4)
8690 dw2_asm_output_data_uleb128 (size, "%s", name);
8691 else
8692 dw2_asm_output_data (constant_size (size), size, "%s", name);
8694 output_loc_sequence (AT_loc (a), -1);
8695 break;
8697 case dw_val_class_const:
8698 /* ??? It would be slightly more efficient to use a scheme like is
8699 used for unsigned constants below, but gdb 4.x does not sign
8700 extend. Gdb 5.x does sign extend. */
8701 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8702 break;
8704 case dw_val_class_unsigned_const:
8706 int csize = constant_size (AT_unsigned (a));
8707 if (dwarf_version == 3
8708 && a->dw_attr == DW_AT_data_member_location
8709 && csize >= 4)
8710 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8711 else
8712 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8714 break;
8716 case dw_val_class_const_double:
8718 unsigned HOST_WIDE_INT first, second;
8720 if (HOST_BITS_PER_WIDE_INT >= 64)
8721 dw2_asm_output_data (1,
8722 HOST_BITS_PER_DOUBLE_INT
8723 / HOST_BITS_PER_CHAR,
8724 NULL);
8726 if (WORDS_BIG_ENDIAN)
8728 first = a->dw_attr_val.v.val_double.high;
8729 second = a->dw_attr_val.v.val_double.low;
8731 else
8733 first = a->dw_attr_val.v.val_double.low;
8734 second = a->dw_attr_val.v.val_double.high;
8737 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8738 first, "%s", name);
8739 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8740 second, NULL);
8742 break;
8744 case dw_val_class_wide_int:
8746 int i;
8747 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8748 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8749 if (len * HOST_BITS_PER_WIDE_INT > 64)
8750 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8751 NULL);
8753 if (WORDS_BIG_ENDIAN)
8754 for (i = len - 1; i >= 0; --i)
8756 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8757 name);
8758 name = NULL;
8760 else
8761 for (i = 0; i < len; ++i)
8763 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8764 name);
8765 name = NULL;
8768 break;
8770 case dw_val_class_vec:
8772 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8773 unsigned int len = a->dw_attr_val.v.val_vec.length;
8774 unsigned int i;
8775 unsigned char *p;
8777 dw2_asm_output_data (constant_size (len * elt_size),
8778 len * elt_size, "%s", name);
8779 if (elt_size > sizeof (HOST_WIDE_INT))
8781 elt_size /= 2;
8782 len *= 2;
8784 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8785 i < len;
8786 i++, p += elt_size)
8787 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8788 "fp or vector constant word %u", i);
8789 break;
8792 case dw_val_class_flag:
8793 if (dwarf_version >= 4)
8795 /* Currently all add_AT_flag calls pass in 1 as last argument,
8796 so DW_FORM_flag_present can be used. If that ever changes,
8797 we'll need to use DW_FORM_flag and have some optimization
8798 in build_abbrev_table that will change those to
8799 DW_FORM_flag_present if it is set to 1 in all DIEs using
8800 the same abbrev entry. */
8801 gcc_assert (AT_flag (a) == 1);
8802 if (flag_debug_asm)
8803 fprintf (asm_out_file, "\t\t\t%s %s\n",
8804 ASM_COMMENT_START, name);
8805 break;
8807 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8808 break;
8810 case dw_val_class_loc_list:
8811 output_attr_index_or_value (a);
8812 break;
8814 case dw_val_class_die_ref:
8815 if (AT_ref_external (a))
8817 if (AT_ref (a)->comdat_type_p)
8819 comdat_type_node_ref type_node =
8820 AT_ref (a)->die_id.die_type_node;
8822 gcc_assert (type_node);
8823 output_signature (type_node->signature, name);
8825 else
8827 const char *sym = AT_ref (a)->die_id.die_symbol;
8828 int size;
8830 gcc_assert (sym);
8831 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8832 length, whereas in DWARF3 it's always sized as an
8833 offset. */
8834 if (dwarf_version == 2)
8835 size = DWARF2_ADDR_SIZE;
8836 else
8837 size = DWARF_OFFSET_SIZE;
8838 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8839 name);
8842 else
8844 gcc_assert (AT_ref (a)->die_offset);
8845 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8846 "%s", name);
8848 break;
8850 case dw_val_class_fde_ref:
8852 char l1[20];
8854 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8855 a->dw_attr_val.v.val_fde_index * 2);
8856 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8857 "%s", name);
8859 break;
8861 case dw_val_class_vms_delta:
8862 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8863 AT_vms_delta2 (a), AT_vms_delta1 (a),
8864 "%s", name);
8865 break;
8867 case dw_val_class_lbl_id:
8868 output_attr_index_or_value (a);
8869 break;
8871 case dw_val_class_lineptr:
8872 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8873 debug_line_section, "%s", name);
8874 break;
8876 case dw_val_class_macptr:
8877 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8878 debug_macinfo_section, "%s", name);
8879 break;
8881 case dw_val_class_str:
8882 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8883 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8884 a->dw_attr_val.v.val_str->label,
8885 debug_str_section,
8886 "%s: \"%s\"", name, AT_string (a));
8887 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8888 dw2_asm_output_data_uleb128 (AT_index (a),
8889 "%s: \"%s\"", name, AT_string (a));
8890 else
8891 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8892 break;
8894 case dw_val_class_file:
8896 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8898 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8899 a->dw_attr_val.v.val_file->filename);
8900 break;
8903 case dw_val_class_data8:
8905 int i;
8907 for (i = 0; i < 8; i++)
8908 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8909 i == 0 ? "%s" : NULL, name);
8910 break;
8913 case dw_val_class_high_pc:
8914 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8915 get_AT_low_pc (die), "DW_AT_high_pc");
8916 break;
8918 default:
8919 gcc_unreachable ();
8923 FOR_EACH_CHILD (die, c, output_die (c));
8925 /* Add null byte to terminate sibling list. */
8926 if (die->die_child != NULL)
8927 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8928 (unsigned long) die->die_offset);
8931 /* Output the compilation unit that appears at the beginning of the
8932 .debug_info section, and precedes the DIE descriptions. */
8934 static void
8935 output_compilation_unit_header (void)
8937 int ver = dwarf_version;
8939 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8940 dw2_asm_output_data (4, 0xffffffff,
8941 "Initial length escape value indicating 64-bit DWARF extension");
8942 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8943 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8944 "Length of Compilation Unit Info");
8945 dw2_asm_output_data (2, ver, "DWARF version number");
8946 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8947 debug_abbrev_section,
8948 "Offset Into Abbrev. Section");
8949 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8952 /* Output the compilation unit DIE and its children. */
8954 static void
8955 output_comp_unit (dw_die_ref die, int output_if_empty)
8957 const char *secname, *oldsym;
8958 char *tmp;
8960 /* Unless we are outputting main CU, we may throw away empty ones. */
8961 if (!output_if_empty && die->die_child == NULL)
8962 return;
8964 /* Even if there are no children of this DIE, we must output the information
8965 about the compilation unit. Otherwise, on an empty translation unit, we
8966 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8967 will then complain when examining the file. First mark all the DIEs in
8968 this CU so we know which get local refs. */
8969 mark_dies (die);
8971 external_ref_hash_type *extern_map = optimize_external_refs (die);
8973 build_abbrev_table (die, extern_map);
8975 delete extern_map;
8977 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8978 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8979 calc_die_sizes (die);
8981 oldsym = die->die_id.die_symbol;
8982 if (oldsym)
8984 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8986 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8987 secname = tmp;
8988 die->die_id.die_symbol = NULL;
8989 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8991 else
8993 switch_to_section (debug_info_section);
8994 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8995 info_section_emitted = true;
8998 /* Output debugging information. */
8999 output_compilation_unit_header ();
9000 output_die (die);
9002 /* Leave the marks on the main CU, so we can check them in
9003 output_pubnames. */
9004 if (oldsym)
9006 unmark_dies (die);
9007 die->die_id.die_symbol = oldsym;
9011 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9012 and .debug_pubtypes. This is configured per-target, but can be
9013 overridden by the -gpubnames or -gno-pubnames options. */
9015 static inline bool
9016 want_pubnames (void)
9018 if (debug_info_level <= DINFO_LEVEL_TERSE)
9019 return false;
9020 if (debug_generate_pub_sections != -1)
9021 return debug_generate_pub_sections;
9022 return targetm.want_debug_pub_sections;
9025 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9027 static void
9028 add_AT_pubnames (dw_die_ref die)
9030 if (want_pubnames ())
9031 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9034 /* Add a string attribute value to a skeleton DIE. */
9036 static inline void
9037 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9038 const char *str)
9040 dw_attr_node attr;
9041 struct indirect_string_node *node;
9043 if (! skeleton_debug_str_hash)
9044 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9045 debug_str_eq, NULL);
9047 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9048 find_string_form (node);
9049 if (node->form == DW_FORM_GNU_str_index)
9050 node->form = DW_FORM_strp;
9052 attr.dw_attr = attr_kind;
9053 attr.dw_attr_val.val_class = dw_val_class_str;
9054 attr.dw_attr_val.val_entry = NULL;
9055 attr.dw_attr_val.v.val_str = node;
9056 add_dwarf_attr (die, &attr);
9059 /* Helper function to generate top-level dies for skeleton debug_info and
9060 debug_types. */
9062 static void
9063 add_top_level_skeleton_die_attrs (dw_die_ref die)
9065 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9066 const char *comp_dir = comp_dir_string ();
9068 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9069 if (comp_dir != NULL)
9070 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9071 add_AT_pubnames (die);
9072 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9075 /* Output skeleton debug sections that point to the dwo file. */
9077 static void
9078 output_skeleton_debug_sections (dw_die_ref comp_unit)
9080 /* These attributes will be found in the full debug_info section. */
9081 remove_AT (comp_unit, DW_AT_producer);
9082 remove_AT (comp_unit, DW_AT_language);
9084 switch_to_section (debug_skeleton_info_section);
9085 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9087 /* Produce the skeleton compilation-unit header. This one differs enough from
9088 a normal CU header that it's better not to call output_compilation_unit
9089 header. */
9090 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9091 dw2_asm_output_data (4, 0xffffffff,
9092 "Initial length escape value indicating 64-bit DWARF extension");
9094 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9095 DWARF_COMPILE_UNIT_HEADER_SIZE
9096 - DWARF_INITIAL_LENGTH_SIZE
9097 + size_of_die (comp_unit),
9098 "Length of Compilation Unit Info");
9099 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9100 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9101 debug_abbrev_section,
9102 "Offset Into Abbrev. Section");
9103 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9105 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9106 output_die (comp_unit);
9108 /* Build the skeleton debug_abbrev section. */
9109 switch_to_section (debug_skeleton_abbrev_section);
9110 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9112 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9114 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9117 /* Output a comdat type unit DIE and its children. */
9119 static void
9120 output_comdat_type_unit (comdat_type_node *node)
9122 const char *secname;
9123 char *tmp;
9124 int i;
9125 #if defined (OBJECT_FORMAT_ELF)
9126 tree comdat_key;
9127 #endif
9129 /* First mark all the DIEs in this CU so we know which get local refs. */
9130 mark_dies (node->root_die);
9132 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9134 build_abbrev_table (node->root_die, extern_map);
9136 delete extern_map;
9137 extern_map = NULL;
9139 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9140 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9141 calc_die_sizes (node->root_die);
9143 #if defined (OBJECT_FORMAT_ELF)
9144 if (!dwarf_split_debug_info)
9145 secname = ".debug_types";
9146 else
9147 secname = ".debug_types.dwo";
9149 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9150 sprintf (tmp, "wt.");
9151 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9152 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9153 comdat_key = get_identifier (tmp);
9154 targetm.asm_out.named_section (secname,
9155 SECTION_DEBUG | SECTION_LINKONCE,
9156 comdat_key);
9157 #else
9158 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9159 sprintf (tmp, ".gnu.linkonce.wt.");
9160 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9161 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9162 secname = tmp;
9163 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9164 #endif
9166 /* Output debugging information. */
9167 output_compilation_unit_header ();
9168 output_signature (node->signature, "Type Signature");
9169 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9170 "Offset to Type DIE");
9171 output_die (node->root_die);
9173 unmark_dies (node->root_die);
9176 /* Return the DWARF2/3 pubname associated with a decl. */
9178 static const char *
9179 dwarf2_name (tree decl, int scope)
9181 if (DECL_NAMELESS (decl))
9182 return NULL;
9183 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9186 /* Add a new entry to .debug_pubnames if appropriate. */
9188 static void
9189 add_pubname_string (const char *str, dw_die_ref die)
9191 pubname_entry e;
9193 e.die = die;
9194 e.name = xstrdup (str);
9195 vec_safe_push (pubname_table, e);
9198 static void
9199 add_pubname (tree decl, dw_die_ref die)
9201 if (!want_pubnames ())
9202 return;
9204 /* Don't add items to the table when we expect that the consumer will have
9205 just read the enclosing die. For example, if the consumer is looking at a
9206 class_member, it will either be inside the class already, or will have just
9207 looked up the class to find the member. Either way, searching the class is
9208 faster than searching the index. */
9209 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9210 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9212 const char *name = dwarf2_name (decl, 1);
9214 if (name)
9215 add_pubname_string (name, die);
9219 /* Add an enumerator to the pubnames section. */
9221 static void
9222 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9224 pubname_entry e;
9226 gcc_assert (scope_name);
9227 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9228 e.die = die;
9229 vec_safe_push (pubname_table, e);
9232 /* Add a new entry to .debug_pubtypes if appropriate. */
9234 static void
9235 add_pubtype (tree decl, dw_die_ref die)
9237 pubname_entry e;
9239 if (!want_pubnames ())
9240 return;
9242 if ((TREE_PUBLIC (decl)
9243 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9244 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9246 tree scope = NULL;
9247 const char *scope_name = "";
9248 const char *sep = is_cxx () ? "::" : ".";
9249 const char *name;
9251 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9252 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9254 scope_name = lang_hooks.dwarf_name (scope, 1);
9255 if (scope_name != NULL && scope_name[0] != '\0')
9256 scope_name = concat (scope_name, sep, NULL);
9257 else
9258 scope_name = "";
9261 if (TYPE_P (decl))
9262 name = type_tag (decl);
9263 else
9264 name = lang_hooks.dwarf_name (decl, 1);
9266 /* If we don't have a name for the type, there's no point in adding
9267 it to the table. */
9268 if (name != NULL && name[0] != '\0')
9270 e.die = die;
9271 e.name = concat (scope_name, name, NULL);
9272 vec_safe_push (pubtype_table, e);
9275 /* Although it might be more consistent to add the pubinfo for the
9276 enumerators as their dies are created, they should only be added if the
9277 enum type meets the criteria above. So rather than re-check the parent
9278 enum type whenever an enumerator die is created, just output them all
9279 here. This isn't protected by the name conditional because anonymous
9280 enums don't have names. */
9281 if (die->die_tag == DW_TAG_enumeration_type)
9283 dw_die_ref c;
9285 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9290 /* Output a single entry in the pubnames table. */
9292 static void
9293 output_pubname (dw_offset die_offset, pubname_entry *entry)
9295 dw_die_ref die = entry->die;
9296 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9298 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9300 if (debug_generate_pub_sections == 2)
9302 /* This logic follows gdb's method for determining the value of the flag
9303 byte. */
9304 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9305 switch (die->die_tag)
9307 case DW_TAG_typedef:
9308 case DW_TAG_base_type:
9309 case DW_TAG_subrange_type:
9310 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9311 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9312 break;
9313 case DW_TAG_enumerator:
9314 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9315 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9316 if (!is_cxx () && !is_java ())
9317 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9318 break;
9319 case DW_TAG_subprogram:
9320 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9321 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9322 if (!is_ada ())
9323 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9324 break;
9325 case DW_TAG_constant:
9326 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9327 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9328 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9329 break;
9330 case DW_TAG_variable:
9331 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9332 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9333 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9334 break;
9335 case DW_TAG_namespace:
9336 case DW_TAG_imported_declaration:
9337 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9338 break;
9339 case DW_TAG_class_type:
9340 case DW_TAG_interface_type:
9341 case DW_TAG_structure_type:
9342 case DW_TAG_union_type:
9343 case DW_TAG_enumeration_type:
9344 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9345 if (!is_cxx () && !is_java ())
9346 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9347 break;
9348 default:
9349 /* An unusual tag. Leave the flag-byte empty. */
9350 break;
9352 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9353 "GDB-index flags");
9356 dw2_asm_output_nstring (entry->name, -1, "external name");
9360 /* Output the public names table used to speed up access to externally
9361 visible names; or the public types table used to find type definitions. */
9363 static void
9364 output_pubnames (vec<pubname_entry, va_gc> *names)
9366 unsigned i;
9367 unsigned long pubnames_length = size_of_pubnames (names);
9368 pubname_ref pub;
9370 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9371 dw2_asm_output_data (4, 0xffffffff,
9372 "Initial length escape value indicating 64-bit DWARF extension");
9373 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9375 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9376 dw2_asm_output_data (2, 2, "DWARF Version");
9378 if (dwarf_split_debug_info)
9379 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9380 debug_skeleton_info_section,
9381 "Offset of Compilation Unit Info");
9382 else
9383 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9384 debug_info_section,
9385 "Offset of Compilation Unit Info");
9386 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9387 "Compilation Unit Length");
9389 FOR_EACH_VEC_ELT (*names, i, pub)
9391 if (include_pubname_in_output (names, pub))
9393 dw_offset die_offset = pub->die->die_offset;
9395 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9396 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9397 gcc_assert (pub->die->die_mark);
9399 /* If we're putting types in their own .debug_types sections,
9400 the .debug_pubtypes table will still point to the compile
9401 unit (not the type unit), so we want to use the offset of
9402 the skeleton DIE (if there is one). */
9403 if (pub->die->comdat_type_p && names == pubtype_table)
9405 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9407 if (type_node != NULL)
9408 die_offset = (type_node->skeleton_die != NULL
9409 ? type_node->skeleton_die->die_offset
9410 : comp_unit_die ()->die_offset);
9413 output_pubname (die_offset, pub);
9417 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9420 /* Output public names and types tables if necessary. */
9422 static void
9423 output_pubtables (void)
9425 if (!want_pubnames () || !info_section_emitted)
9426 return;
9428 switch_to_section (debug_pubnames_section);
9429 output_pubnames (pubname_table);
9430 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9431 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9432 simply won't look for the section. */
9433 switch_to_section (debug_pubtypes_section);
9434 output_pubnames (pubtype_table);
9438 /* Output the information that goes into the .debug_aranges table.
9439 Namely, define the beginning and ending address range of the
9440 text section generated for this compilation unit. */
9442 static void
9443 output_aranges (unsigned long aranges_length)
9445 unsigned i;
9447 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9448 dw2_asm_output_data (4, 0xffffffff,
9449 "Initial length escape value indicating 64-bit DWARF extension");
9450 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9451 "Length of Address Ranges Info");
9452 /* Version number for aranges is still 2, even in DWARF3. */
9453 dw2_asm_output_data (2, 2, "DWARF Version");
9454 if (dwarf_split_debug_info)
9455 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9456 debug_skeleton_info_section,
9457 "Offset of Compilation Unit Info");
9458 else
9459 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9460 debug_info_section,
9461 "Offset of Compilation Unit Info");
9462 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9463 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9465 /* We need to align to twice the pointer size here. */
9466 if (DWARF_ARANGES_PAD_SIZE)
9468 /* Pad using a 2 byte words so that padding is correct for any
9469 pointer size. */
9470 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9471 2 * DWARF2_ADDR_SIZE);
9472 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9473 dw2_asm_output_data (2, 0, NULL);
9476 /* It is necessary not to output these entries if the sections were
9477 not used; if the sections were not used, the length will be 0 and
9478 the address may end up as 0 if the section is discarded by ld
9479 --gc-sections, leaving an invalid (0, 0) entry that can be
9480 confused with the terminator. */
9481 if (text_section_used)
9483 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9484 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9485 text_section_label, "Length");
9487 if (cold_text_section_used)
9489 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9490 "Address");
9491 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9492 cold_text_section_label, "Length");
9495 if (have_multiple_function_sections)
9497 unsigned fde_idx;
9498 dw_fde_ref fde;
9500 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9502 if (DECL_IGNORED_P (fde->decl))
9503 continue;
9504 if (!fde->in_std_section)
9506 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9507 "Address");
9508 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9509 fde->dw_fde_begin, "Length");
9511 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9513 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9514 "Address");
9515 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9516 fde->dw_fde_second_begin, "Length");
9521 /* Output the terminator words. */
9522 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9523 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9526 /* Add a new entry to .debug_ranges. Return the offset at which it
9527 was placed. */
9529 static unsigned int
9530 add_ranges_num (int num)
9532 unsigned int in_use = ranges_table_in_use;
9534 if (in_use == ranges_table_allocated)
9536 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9537 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9538 ranges_table_allocated);
9539 memset (ranges_table + ranges_table_in_use, 0,
9540 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9543 ranges_table[in_use].num = num;
9544 ranges_table_in_use = in_use + 1;
9546 return in_use * 2 * DWARF2_ADDR_SIZE;
9549 /* Add a new entry to .debug_ranges corresponding to a block, or a
9550 range terminator if BLOCK is NULL. */
9552 static unsigned int
9553 add_ranges (const_tree block)
9555 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9558 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9559 When using dwarf_split_debug_info, address attributes in dies destined
9560 for the final executable should be direct references--setting the
9561 parameter force_direct ensures this behavior. */
9563 static void
9564 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9565 bool *added, bool force_direct)
9567 unsigned int in_use = ranges_by_label_in_use;
9568 unsigned int offset;
9570 if (in_use == ranges_by_label_allocated)
9572 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9573 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9574 ranges_by_label,
9575 ranges_by_label_allocated);
9576 memset (ranges_by_label + ranges_by_label_in_use, 0,
9577 RANGES_TABLE_INCREMENT
9578 * sizeof (struct dw_ranges_by_label_struct));
9581 ranges_by_label[in_use].begin = begin;
9582 ranges_by_label[in_use].end = end;
9583 ranges_by_label_in_use = in_use + 1;
9585 offset = add_ranges_num (-(int)in_use - 1);
9586 if (!*added)
9588 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9589 *added = true;
9593 static void
9594 output_ranges (void)
9596 unsigned i;
9597 static const char *const start_fmt = "Offset %#x";
9598 const char *fmt = start_fmt;
9600 for (i = 0; i < ranges_table_in_use; i++)
9602 int block_num = ranges_table[i].num;
9604 if (block_num > 0)
9606 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9607 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9609 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9610 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9612 /* If all code is in the text section, then the compilation
9613 unit base address defaults to DW_AT_low_pc, which is the
9614 base of the text section. */
9615 if (!have_multiple_function_sections)
9617 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9618 text_section_label,
9619 fmt, i * 2 * DWARF2_ADDR_SIZE);
9620 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9621 text_section_label, NULL);
9624 /* Otherwise, the compilation unit base address is zero,
9625 which allows us to use absolute addresses, and not worry
9626 about whether the target supports cross-section
9627 arithmetic. */
9628 else
9630 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9631 fmt, i * 2 * DWARF2_ADDR_SIZE);
9632 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9635 fmt = NULL;
9638 /* Negative block_num stands for an index into ranges_by_label. */
9639 else if (block_num < 0)
9641 int lab_idx = - block_num - 1;
9643 if (!have_multiple_function_sections)
9645 gcc_unreachable ();
9646 #if 0
9647 /* If we ever use add_ranges_by_labels () for a single
9648 function section, all we have to do is to take out
9649 the #if 0 above. */
9650 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9651 ranges_by_label[lab_idx].begin,
9652 text_section_label,
9653 fmt, i * 2 * DWARF2_ADDR_SIZE);
9654 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9655 ranges_by_label[lab_idx].end,
9656 text_section_label, NULL);
9657 #endif
9659 else
9661 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9662 ranges_by_label[lab_idx].begin,
9663 fmt, i * 2 * DWARF2_ADDR_SIZE);
9664 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9665 ranges_by_label[lab_idx].end,
9666 NULL);
9669 else
9671 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9672 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9673 fmt = start_fmt;
9678 /* Data structure containing information about input files. */
9679 struct file_info
9681 const char *path; /* Complete file name. */
9682 const char *fname; /* File name part. */
9683 int length; /* Length of entire string. */
9684 struct dwarf_file_data * file_idx; /* Index in input file table. */
9685 int dir_idx; /* Index in directory table. */
9688 /* Data structure containing information about directories with source
9689 files. */
9690 struct dir_info
9692 const char *path; /* Path including directory name. */
9693 int length; /* Path length. */
9694 int prefix; /* Index of directory entry which is a prefix. */
9695 int count; /* Number of files in this directory. */
9696 int dir_idx; /* Index of directory used as base. */
9699 /* Callback function for file_info comparison. We sort by looking at
9700 the directories in the path. */
9702 static int
9703 file_info_cmp (const void *p1, const void *p2)
9705 const struct file_info *const s1 = (const struct file_info *) p1;
9706 const struct file_info *const s2 = (const struct file_info *) p2;
9707 const unsigned char *cp1;
9708 const unsigned char *cp2;
9710 /* Take care of file names without directories. We need to make sure that
9711 we return consistent values to qsort since some will get confused if
9712 we return the same value when identical operands are passed in opposite
9713 orders. So if neither has a directory, return 0 and otherwise return
9714 1 or -1 depending on which one has the directory. */
9715 if ((s1->path == s1->fname || s2->path == s2->fname))
9716 return (s2->path == s2->fname) - (s1->path == s1->fname);
9718 cp1 = (const unsigned char *) s1->path;
9719 cp2 = (const unsigned char *) s2->path;
9721 while (1)
9723 ++cp1;
9724 ++cp2;
9725 /* Reached the end of the first path? If so, handle like above. */
9726 if ((cp1 == (const unsigned char *) s1->fname)
9727 || (cp2 == (const unsigned char *) s2->fname))
9728 return ((cp2 == (const unsigned char *) s2->fname)
9729 - (cp1 == (const unsigned char *) s1->fname));
9731 /* Character of current path component the same? */
9732 else if (*cp1 != *cp2)
9733 return *cp1 - *cp2;
9737 struct file_name_acquire_data
9739 struct file_info *files;
9740 int used_files;
9741 int max_files;
9744 /* Traversal function for the hash table. */
9746 static int
9747 file_name_acquire (void ** slot, void *data)
9749 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9750 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9751 struct file_info *fi;
9752 const char *f;
9754 gcc_assert (fnad->max_files >= d->emitted_number);
9756 if (! d->emitted_number)
9757 return 1;
9759 gcc_assert (fnad->max_files != fnad->used_files);
9761 fi = fnad->files + fnad->used_files++;
9763 /* Skip all leading "./". */
9764 f = d->filename;
9765 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9766 f += 2;
9768 /* Create a new array entry. */
9769 fi->path = f;
9770 fi->length = strlen (f);
9771 fi->file_idx = d;
9773 /* Search for the file name part. */
9774 f = strrchr (f, DIR_SEPARATOR);
9775 #if defined (DIR_SEPARATOR_2)
9777 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9779 if (g != NULL)
9781 if (f == NULL || f < g)
9782 f = g;
9785 #endif
9787 fi->fname = f == NULL ? fi->path : f + 1;
9788 return 1;
9791 /* Output the directory table and the file name table. We try to minimize
9792 the total amount of memory needed. A heuristic is used to avoid large
9793 slowdowns with many input files. */
9795 static void
9796 output_file_names (void)
9798 struct file_name_acquire_data fnad;
9799 int numfiles;
9800 struct file_info *files;
9801 struct dir_info *dirs;
9802 int *saved;
9803 int *savehere;
9804 int *backmap;
9805 int ndirs;
9806 int idx_offset;
9807 int i;
9809 if (!last_emitted_file)
9811 dw2_asm_output_data (1, 0, "End directory table");
9812 dw2_asm_output_data (1, 0, "End file name table");
9813 return;
9816 numfiles = last_emitted_file->emitted_number;
9818 /* Allocate the various arrays we need. */
9819 files = XALLOCAVEC (struct file_info, numfiles);
9820 dirs = XALLOCAVEC (struct dir_info, numfiles);
9822 fnad.files = files;
9823 fnad.used_files = 0;
9824 fnad.max_files = numfiles;
9825 htab_traverse (file_table, file_name_acquire, &fnad);
9826 gcc_assert (fnad.used_files == fnad.max_files);
9828 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9830 /* Find all the different directories used. */
9831 dirs[0].path = files[0].path;
9832 dirs[0].length = files[0].fname - files[0].path;
9833 dirs[0].prefix = -1;
9834 dirs[0].count = 1;
9835 dirs[0].dir_idx = 0;
9836 files[0].dir_idx = 0;
9837 ndirs = 1;
9839 for (i = 1; i < numfiles; i++)
9840 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9841 && memcmp (dirs[ndirs - 1].path, files[i].path,
9842 dirs[ndirs - 1].length) == 0)
9844 /* Same directory as last entry. */
9845 files[i].dir_idx = ndirs - 1;
9846 ++dirs[ndirs - 1].count;
9848 else
9850 int j;
9852 /* This is a new directory. */
9853 dirs[ndirs].path = files[i].path;
9854 dirs[ndirs].length = files[i].fname - files[i].path;
9855 dirs[ndirs].count = 1;
9856 dirs[ndirs].dir_idx = ndirs;
9857 files[i].dir_idx = ndirs;
9859 /* Search for a prefix. */
9860 dirs[ndirs].prefix = -1;
9861 for (j = 0; j < ndirs; j++)
9862 if (dirs[j].length < dirs[ndirs].length
9863 && dirs[j].length > 1
9864 && (dirs[ndirs].prefix == -1
9865 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9866 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9867 dirs[ndirs].prefix = j;
9869 ++ndirs;
9872 /* Now to the actual work. We have to find a subset of the directories which
9873 allow expressing the file name using references to the directory table
9874 with the least amount of characters. We do not do an exhaustive search
9875 where we would have to check out every combination of every single
9876 possible prefix. Instead we use a heuristic which provides nearly optimal
9877 results in most cases and never is much off. */
9878 saved = XALLOCAVEC (int, ndirs);
9879 savehere = XALLOCAVEC (int, ndirs);
9881 memset (saved, '\0', ndirs * sizeof (saved[0]));
9882 for (i = 0; i < ndirs; i++)
9884 int j;
9885 int total;
9887 /* We can always save some space for the current directory. But this
9888 does not mean it will be enough to justify adding the directory. */
9889 savehere[i] = dirs[i].length;
9890 total = (savehere[i] - saved[i]) * dirs[i].count;
9892 for (j = i + 1; j < ndirs; j++)
9894 savehere[j] = 0;
9895 if (saved[j] < dirs[i].length)
9897 /* Determine whether the dirs[i] path is a prefix of the
9898 dirs[j] path. */
9899 int k;
9901 k = dirs[j].prefix;
9902 while (k != -1 && k != (int) i)
9903 k = dirs[k].prefix;
9905 if (k == (int) i)
9907 /* Yes it is. We can possibly save some memory by
9908 writing the filenames in dirs[j] relative to
9909 dirs[i]. */
9910 savehere[j] = dirs[i].length;
9911 total += (savehere[j] - saved[j]) * dirs[j].count;
9916 /* Check whether we can save enough to justify adding the dirs[i]
9917 directory. */
9918 if (total > dirs[i].length + 1)
9920 /* It's worthwhile adding. */
9921 for (j = i; j < ndirs; j++)
9922 if (savehere[j] > 0)
9924 /* Remember how much we saved for this directory so far. */
9925 saved[j] = savehere[j];
9927 /* Remember the prefix directory. */
9928 dirs[j].dir_idx = i;
9933 /* Emit the directory name table. */
9934 idx_offset = dirs[0].length > 0 ? 1 : 0;
9935 for (i = 1 - idx_offset; i < ndirs; i++)
9936 dw2_asm_output_nstring (dirs[i].path,
9937 dirs[i].length
9938 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9939 "Directory Entry: %#x", i + idx_offset);
9941 dw2_asm_output_data (1, 0, "End directory table");
9943 /* We have to emit them in the order of emitted_number since that's
9944 used in the debug info generation. To do this efficiently we
9945 generate a back-mapping of the indices first. */
9946 backmap = XALLOCAVEC (int, numfiles);
9947 for (i = 0; i < numfiles; i++)
9948 backmap[files[i].file_idx->emitted_number - 1] = i;
9950 /* Now write all the file names. */
9951 for (i = 0; i < numfiles; i++)
9953 int file_idx = backmap[i];
9954 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9956 #ifdef VMS_DEBUGGING_INFO
9957 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9959 /* Setting these fields can lead to debugger miscomparisons,
9960 but VMS Debug requires them to be set correctly. */
9962 int ver;
9963 long long cdt;
9964 long siz;
9965 int maxfilelen = strlen (files[file_idx].path)
9966 + dirs[dir_idx].length
9967 + MAX_VMS_VERSION_LEN + 1;
9968 char *filebuf = XALLOCAVEC (char, maxfilelen);
9970 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9971 snprintf (filebuf, maxfilelen, "%s;%d",
9972 files[file_idx].path + dirs[dir_idx].length, ver);
9974 dw2_asm_output_nstring
9975 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9977 /* Include directory index. */
9978 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9980 /* Modification time. */
9981 dw2_asm_output_data_uleb128
9982 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9983 ? cdt : 0,
9984 NULL);
9986 /* File length in bytes. */
9987 dw2_asm_output_data_uleb128
9988 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9989 ? siz : 0,
9990 NULL);
9991 #else
9992 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9993 "File Entry: %#x", (unsigned) i + 1);
9995 /* Include directory index. */
9996 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9998 /* Modification time. */
9999 dw2_asm_output_data_uleb128 (0, NULL);
10001 /* File length in bytes. */
10002 dw2_asm_output_data_uleb128 (0, NULL);
10003 #endif /* VMS_DEBUGGING_INFO */
10006 dw2_asm_output_data (1, 0, "End file name table");
10010 /* Output one line number table into the .debug_line section. */
10012 static void
10013 output_one_line_info_table (dw_line_info_table *table)
10015 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10016 unsigned int current_line = 1;
10017 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10018 dw_line_info_entry *ent;
10019 size_t i;
10021 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10023 switch (ent->opcode)
10025 case LI_set_address:
10026 /* ??? Unfortunately, we have little choice here currently, and
10027 must always use the most general form. GCC does not know the
10028 address delta itself, so we can't use DW_LNS_advance_pc. Many
10029 ports do have length attributes which will give an upper bound
10030 on the address range. We could perhaps use length attributes
10031 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10032 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10034 /* This can handle any delta. This takes
10035 4+DWARF2_ADDR_SIZE bytes. */
10036 dw2_asm_output_data (1, 0, "set address %s", line_label);
10037 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10038 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10039 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10040 break;
10042 case LI_set_line:
10043 if (ent->val == current_line)
10045 /* We still need to start a new row, so output a copy insn. */
10046 dw2_asm_output_data (1, DW_LNS_copy,
10047 "copy line %u", current_line);
10049 else
10051 int line_offset = ent->val - current_line;
10052 int line_delta = line_offset - DWARF_LINE_BASE;
10054 current_line = ent->val;
10055 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10057 /* This can handle deltas from -10 to 234, using the current
10058 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10059 This takes 1 byte. */
10060 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10061 "line %u", current_line);
10063 else
10065 /* This can handle any delta. This takes at least 4 bytes,
10066 depending on the value being encoded. */
10067 dw2_asm_output_data (1, DW_LNS_advance_line,
10068 "advance to line %u", current_line);
10069 dw2_asm_output_data_sleb128 (line_offset, NULL);
10070 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10073 break;
10075 case LI_set_file:
10076 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10077 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10078 break;
10080 case LI_set_column:
10081 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10082 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10083 break;
10085 case LI_negate_stmt:
10086 current_is_stmt = !current_is_stmt;
10087 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10088 "is_stmt %d", current_is_stmt);
10089 break;
10091 case LI_set_prologue_end:
10092 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10093 "set prologue end");
10094 break;
10096 case LI_set_epilogue_begin:
10097 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10098 "set epilogue begin");
10099 break;
10101 case LI_set_discriminator:
10102 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10103 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10104 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10105 dw2_asm_output_data_uleb128 (ent->val, NULL);
10106 break;
10110 /* Emit debug info for the address of the end of the table. */
10111 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10112 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10113 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10114 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10116 dw2_asm_output_data (1, 0, "end sequence");
10117 dw2_asm_output_data_uleb128 (1, NULL);
10118 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10121 /* Output the source line number correspondence information. This
10122 information goes into the .debug_line section. */
10124 static void
10125 output_line_info (bool prologue_only)
10127 char l1[20], l2[20], p1[20], p2[20];
10128 int ver = dwarf_version;
10129 bool saw_one = false;
10130 int opc;
10132 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10133 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10134 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10135 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10137 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10138 dw2_asm_output_data (4, 0xffffffff,
10139 "Initial length escape value indicating 64-bit DWARF extension");
10140 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10141 "Length of Source Line Info");
10142 ASM_OUTPUT_LABEL (asm_out_file, l1);
10144 dw2_asm_output_data (2, ver, "DWARF Version");
10145 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10146 ASM_OUTPUT_LABEL (asm_out_file, p1);
10148 /* Define the architecture-dependent minimum instruction length (in bytes).
10149 In this implementation of DWARF, this field is used for information
10150 purposes only. Since GCC generates assembly language, we have no
10151 a priori knowledge of how many instruction bytes are generated for each
10152 source line, and therefore can use only the DW_LNE_set_address and
10153 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10154 this as '1', which is "correct enough" for all architectures,
10155 and don't let the target override. */
10156 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10158 if (ver >= 4)
10159 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10160 "Maximum Operations Per Instruction");
10161 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10162 "Default is_stmt_start flag");
10163 dw2_asm_output_data (1, DWARF_LINE_BASE,
10164 "Line Base Value (Special Opcodes)");
10165 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10166 "Line Range Value (Special Opcodes)");
10167 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10168 "Special Opcode Base");
10170 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10172 int n_op_args;
10173 switch (opc)
10175 case DW_LNS_advance_pc:
10176 case DW_LNS_advance_line:
10177 case DW_LNS_set_file:
10178 case DW_LNS_set_column:
10179 case DW_LNS_fixed_advance_pc:
10180 case DW_LNS_set_isa:
10181 n_op_args = 1;
10182 break;
10183 default:
10184 n_op_args = 0;
10185 break;
10188 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10189 opc, n_op_args);
10192 /* Write out the information about the files we use. */
10193 output_file_names ();
10194 ASM_OUTPUT_LABEL (asm_out_file, p2);
10195 if (prologue_only)
10197 /* Output the marker for the end of the line number info. */
10198 ASM_OUTPUT_LABEL (asm_out_file, l2);
10199 return;
10202 if (separate_line_info)
10204 dw_line_info_table *table;
10205 size_t i;
10207 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10208 if (table->in_use)
10210 output_one_line_info_table (table);
10211 saw_one = true;
10214 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10216 output_one_line_info_table (cold_text_section_line_info);
10217 saw_one = true;
10220 /* ??? Some Darwin linkers crash on a .debug_line section with no
10221 sequences. Further, merely a DW_LNE_end_sequence entry is not
10222 sufficient -- the address column must also be initialized.
10223 Make sure to output at least one set_address/end_sequence pair,
10224 choosing .text since that section is always present. */
10225 if (text_section_line_info->in_use || !saw_one)
10226 output_one_line_info_table (text_section_line_info);
10228 /* Output the marker for the end of the line number info. */
10229 ASM_OUTPUT_LABEL (asm_out_file, l2);
10232 /* Given a pointer to a tree node for some base type, return a pointer to
10233 a DIE that describes the given type.
10235 This routine must only be called for GCC type nodes that correspond to
10236 Dwarf base (fundamental) types. */
10238 static dw_die_ref
10239 base_type_die (tree type)
10241 dw_die_ref base_type_result;
10242 enum dwarf_type encoding;
10244 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10245 return 0;
10247 /* If this is a subtype that should not be emitted as a subrange type,
10248 use the base type. See subrange_type_for_debug_p. */
10249 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10250 type = TREE_TYPE (type);
10252 switch (TREE_CODE (type))
10254 case INTEGER_TYPE:
10255 if ((dwarf_version >= 4 || !dwarf_strict)
10256 && TYPE_NAME (type)
10257 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10258 && DECL_IS_BUILTIN (TYPE_NAME (type))
10259 && DECL_NAME (TYPE_NAME (type)))
10261 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10262 if (strcmp (name, "char16_t") == 0
10263 || strcmp (name, "char32_t") == 0)
10265 encoding = DW_ATE_UTF;
10266 break;
10269 if (TYPE_STRING_FLAG (type))
10271 if (TYPE_UNSIGNED (type))
10272 encoding = DW_ATE_unsigned_char;
10273 else
10274 encoding = DW_ATE_signed_char;
10276 else if (TYPE_UNSIGNED (type))
10277 encoding = DW_ATE_unsigned;
10278 else
10279 encoding = DW_ATE_signed;
10280 break;
10282 case REAL_TYPE:
10283 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10285 if (dwarf_version >= 3 || !dwarf_strict)
10286 encoding = DW_ATE_decimal_float;
10287 else
10288 encoding = DW_ATE_lo_user;
10290 else
10291 encoding = DW_ATE_float;
10292 break;
10294 case FIXED_POINT_TYPE:
10295 if (!(dwarf_version >= 3 || !dwarf_strict))
10296 encoding = DW_ATE_lo_user;
10297 else if (TYPE_UNSIGNED (type))
10298 encoding = DW_ATE_unsigned_fixed;
10299 else
10300 encoding = DW_ATE_signed_fixed;
10301 break;
10303 /* Dwarf2 doesn't know anything about complex ints, so use
10304 a user defined type for it. */
10305 case COMPLEX_TYPE:
10306 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10307 encoding = DW_ATE_complex_float;
10308 else
10309 encoding = DW_ATE_lo_user;
10310 break;
10312 case BOOLEAN_TYPE:
10313 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10314 encoding = DW_ATE_boolean;
10315 break;
10317 default:
10318 /* No other TREE_CODEs are Dwarf fundamental types. */
10319 gcc_unreachable ();
10322 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10324 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10325 int_size_in_bytes (type));
10326 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10327 add_pubtype (type, base_type_result);
10329 return base_type_result;
10332 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10333 named 'auto' in its type: return true for it, false otherwise. */
10335 static inline bool
10336 is_cxx_auto (tree type)
10338 if (is_cxx ())
10340 tree name = TYPE_IDENTIFIER (type);
10341 if (name == get_identifier ("auto")
10342 || name == get_identifier ("decltype(auto)"))
10343 return true;
10345 return false;
10348 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10349 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10351 static inline int
10352 is_base_type (tree type)
10354 switch (TREE_CODE (type))
10356 case ERROR_MARK:
10357 case VOID_TYPE:
10358 case INTEGER_TYPE:
10359 case REAL_TYPE:
10360 case FIXED_POINT_TYPE:
10361 case COMPLEX_TYPE:
10362 case BOOLEAN_TYPE:
10363 return 1;
10365 case ARRAY_TYPE:
10366 case RECORD_TYPE:
10367 case UNION_TYPE:
10368 case QUAL_UNION_TYPE:
10369 case ENUMERAL_TYPE:
10370 case FUNCTION_TYPE:
10371 case METHOD_TYPE:
10372 case POINTER_TYPE:
10373 case REFERENCE_TYPE:
10374 case NULLPTR_TYPE:
10375 case OFFSET_TYPE:
10376 case LANG_TYPE:
10377 case VECTOR_TYPE:
10378 return 0;
10380 default:
10381 if (is_cxx_auto (type))
10382 return 0;
10383 gcc_unreachable ();
10386 return 0;
10389 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10390 node, return the size in bits for the type if it is a constant, or else
10391 return the alignment for the type if the type's size is not constant, or
10392 else return BITS_PER_WORD if the type actually turns out to be an
10393 ERROR_MARK node. */
10395 static inline unsigned HOST_WIDE_INT
10396 simple_type_size_in_bits (const_tree type)
10398 if (TREE_CODE (type) == ERROR_MARK)
10399 return BITS_PER_WORD;
10400 else if (TYPE_SIZE (type) == NULL_TREE)
10401 return 0;
10402 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10403 return tree_to_uhwi (TYPE_SIZE (type));
10404 else
10405 return TYPE_ALIGN (type);
10408 /* Similarly, but return an offset_int instead of UHWI. */
10410 static inline offset_int
10411 offset_int_type_size_in_bits (const_tree type)
10413 if (TREE_CODE (type) == ERROR_MARK)
10414 return BITS_PER_WORD;
10415 else if (TYPE_SIZE (type) == NULL_TREE)
10416 return 0;
10417 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10418 return wi::to_offset (TYPE_SIZE (type));
10419 else
10420 return TYPE_ALIGN (type);
10423 /* Given a pointer to a tree node for a subrange type, return a pointer
10424 to a DIE that describes the given type. */
10426 static dw_die_ref
10427 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10429 dw_die_ref subrange_die;
10430 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10432 if (context_die == NULL)
10433 context_die = comp_unit_die ();
10435 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10437 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10439 /* The size of the subrange type and its base type do not match,
10440 so we need to generate a size attribute for the subrange type. */
10441 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10444 if (low)
10445 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10446 if (high)
10447 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10449 return subrange_die;
10452 /* Returns the (const and/or volatile) cv_qualifiers associated with
10453 the decl node. This will normally be augmented with the
10454 cv_qualifiers of the underlying type in add_type_attribute. */
10456 static int
10457 decl_quals (const_tree decl)
10459 return ((TREE_READONLY (decl)
10460 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10461 | (TREE_THIS_VOLATILE (decl)
10462 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10465 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10466 entry that chains various modifiers in front of the given type. */
10468 static dw_die_ref
10469 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10471 const int is_const_type = ((cv_quals & TYPE_QUAL_CONST) != 0);
10472 const int is_volatile_type = ((cv_quals & TYPE_QUAL_VOLATILE) != 0);
10473 enum tree_code code = TREE_CODE (type);
10474 dw_die_ref mod_type_die;
10475 dw_die_ref sub_die = NULL;
10476 tree item_type = NULL;
10477 tree qualified_type;
10478 tree name, low, high;
10479 dw_die_ref mod_scope;
10481 if (code == ERROR_MARK)
10482 return NULL;
10484 /* Only these cv-qualifiers are currently handled. */
10485 cv_quals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT);
10487 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10488 tag modifier (and not an attribute) old consumers won't be able
10489 to handle it. */
10490 if (dwarf_version < 3)
10491 cv_quals &= ~TYPE_QUAL_RESTRICT;
10493 /* See if we already have the appropriately qualified variant of
10494 this type. */
10495 qualified_type = get_qualified_type (type, cv_quals);
10497 if (qualified_type == sizetype
10498 && TYPE_NAME (qualified_type)
10499 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10501 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10503 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10504 && TYPE_PRECISION (t)
10505 == TYPE_PRECISION (qualified_type)
10506 && TYPE_UNSIGNED (t)
10507 == TYPE_UNSIGNED (qualified_type));
10508 qualified_type = t;
10511 /* If we do, then we can just use its DIE, if it exists. */
10512 if (qualified_type)
10514 mod_type_die = lookup_type_die (qualified_type);
10515 if (mod_type_die)
10516 return mod_type_die;
10519 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10521 /* Handle C typedef types. */
10522 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10523 && !DECL_ARTIFICIAL (name))
10525 tree dtype = TREE_TYPE (name);
10527 if (qualified_type == dtype)
10529 /* For a named type, use the typedef. */
10530 gen_type_die (qualified_type, context_die);
10531 return lookup_type_die (qualified_type);
10533 else
10535 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10536 dquals &= (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT
10537 | TYPE_QUAL_UPC_SHARED | TYPE_QUAL_UPC_STRICT
10538 | TYPE_QUAL_UPC_RELAXED);
10540 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10541 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10542 /* cv-unqualified version of named type. Just use
10543 the unnamed type to which it refers. */
10544 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10545 cv_quals, context_die);
10546 /* Else cv-qualified version of named type; fall through. */
10550 mod_scope = scope_die_for (type, context_die);
10552 if ((cv_quals & TYPE_QUAL_CONST)
10553 /* If there are multiple type modifiers, prefer a path which
10554 leads to a qualified type. */
10555 && (((cv_quals & ~TYPE_QUAL_CONST) == TYPE_UNQUALIFIED)
10556 || get_qualified_type (type, cv_quals) == NULL_TREE
10557 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_CONST)
10558 != NULL_TREE)))
10560 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10561 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_CONST,
10562 context_die);
10564 else if ((cv_quals & TYPE_QUAL_VOLATILE)
10565 && (((cv_quals & ~TYPE_QUAL_VOLATILE) == TYPE_UNQUALIFIED)
10566 || get_qualified_type (type, cv_quals) == NULL_TREE
10567 || (get_qualified_type (type, cv_quals & ~TYPE_QUAL_VOLATILE)
10568 != NULL_TREE)))
10570 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10571 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_VOLATILE,
10572 context_die);
10574 else if (cv_quals & TYPE_QUAL_RESTRICT)
10576 mod_type_die = new_die (DW_TAG_restrict_type, mod_scope, type);
10577 sub_die = modified_type_die (type, cv_quals & ~TYPE_QUAL_RESTRICT,
10578 context_die);
10580 else if (use_upc_dwarf2_extensions
10581 && (cv_quals & TYPE_QUAL_UPC_SHARED))
10583 HOST_WIDE_INT block_factor = 1;
10585 /* Inside the compiler,
10586 "shared int x;" TYPE_UPC_BLOCK_FACTOR is null.
10587 "shared [] int *p;" TYPE_UPC_BLOCK_FACTOR is zero.
10588 "shared [10] int x[50];" TYPE_UPC_BLOCK_FACTOR is 10 * bitsize(int)
10589 The DWARF2 encoding is as follows:
10590 "shared int x;" DW_AT_count: 1
10591 "shared [] int *p;" <no DW_AT_count attribute>
10592 "shared [10] int x[50];" DW_AT_count: 10
10593 The logic below handles thse various contingencies. */
10595 mod_type_die = new_die (DW_TAG_upc_shared_type,
10596 comp_unit_die (), type);
10598 if (TYPE_HAS_UPC_BLOCK_FACTOR (type))
10599 block_factor = TREE_INT_CST_LOW (TYPE_UPC_BLOCK_FACTOR (type));
10601 if (block_factor != 0)
10602 add_AT_unsigned (mod_type_die, DW_AT_count, block_factor);
10604 sub_die = modified_type_die (type,
10605 cv_quals & ~TYPE_QUAL_UPC_SHARED,
10606 context_die);
10608 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_STRICT)
10610 mod_type_die = new_die (DW_TAG_upc_strict_type,
10611 comp_unit_die (), type);
10612 sub_die = modified_type_die (type,
10613 cv_quals & ~TYPE_QUAL_UPC_STRICT,
10614 context_die);
10616 else if (use_upc_dwarf2_extensions && cv_quals & TYPE_QUAL_UPC_RELAXED)
10618 mod_type_die = new_die (DW_TAG_upc_relaxed_type,
10619 comp_unit_die (), type);
10620 sub_die = modified_type_die (type,
10621 cv_quals & ~TYPE_QUAL_UPC_RELAXED,
10622 context_die);
10624 else if (code == POINTER_TYPE)
10626 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10627 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10628 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10629 item_type = TREE_TYPE (type);
10630 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10631 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10632 TYPE_ADDR_SPACE (item_type));
10634 else if (code == REFERENCE_TYPE)
10636 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10637 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10638 type);
10639 else
10640 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10641 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10642 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10643 item_type = TREE_TYPE (type);
10644 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10645 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10646 TYPE_ADDR_SPACE (item_type));
10648 else if (code == INTEGER_TYPE
10649 && TREE_TYPE (type) != NULL_TREE
10650 && subrange_type_for_debug_p (type, &low, &high))
10652 mod_type_die = subrange_type_die (type, low, high, context_die);
10653 item_type = TREE_TYPE (type);
10655 else if (is_base_type (type))
10656 mod_type_die = base_type_die (type);
10657 else
10659 gen_type_die (type, context_die);
10661 /* We have to get the type_main_variant here (and pass that to the
10662 `lookup_type_die' routine) because the ..._TYPE node we have
10663 might simply be a *copy* of some original type node (where the
10664 copy was created to help us keep track of typedef names) and
10665 that copy might have a different TYPE_UID from the original
10666 ..._TYPE node. */
10667 if (TREE_CODE (type) != VECTOR_TYPE)
10668 return lookup_type_die (type_main_variant (type));
10669 else
10670 /* Vectors have the debugging information in the type,
10671 not the main variant. */
10672 return lookup_type_die (type);
10675 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10676 don't output a DW_TAG_typedef, since there isn't one in the
10677 user's program; just attach a DW_AT_name to the type.
10678 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10679 if the base type already has the same name. */
10680 if (name
10681 && ((TREE_CODE (name) != TYPE_DECL
10682 && (qualified_type == TYPE_MAIN_VARIANT (type)
10683 || (cv_quals == TYPE_UNQUALIFIED)))
10684 || (TREE_CODE (name) == TYPE_DECL
10685 && TREE_TYPE (name) == qualified_type
10686 && DECL_NAME (name))))
10688 if (TREE_CODE (name) == TYPE_DECL)
10689 /* Could just call add_name_and_src_coords_attributes here,
10690 but since this is a builtin type it doesn't have any
10691 useful source coordinates anyway. */
10692 name = DECL_NAME (name);
10693 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10695 /* This probably indicates a bug. */
10696 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10698 name = TYPE_IDENTIFIER (type);
10699 add_name_attribute (mod_type_die,
10700 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10703 if (qualified_type)
10704 equate_type_number_to_die (qualified_type, mod_type_die);
10706 if (item_type)
10707 /* We must do this after the equate_type_number_to_die call, in case
10708 this is a recursive type. This ensures that the modified_type_die
10709 recursion will terminate even if the type is recursive. Recursive
10710 types are possible in Ada. */
10711 sub_die = modified_type_die (item_type,
10712 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10713 context_die);
10715 if (sub_die != NULL)
10716 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10718 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10719 if (TYPE_ARTIFICIAL (type))
10720 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10722 return mod_type_die;
10725 /* Generate DIEs for the generic parameters of T.
10726 T must be either a generic type or a generic function.
10727 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10729 static void
10730 gen_generic_params_dies (tree t)
10732 tree parms, args;
10733 int parms_num, i;
10734 dw_die_ref die = NULL;
10735 int non_default;
10737 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10738 return;
10740 if (TYPE_P (t))
10741 die = lookup_type_die (t);
10742 else if (DECL_P (t))
10743 die = lookup_decl_die (t);
10745 gcc_assert (die);
10747 parms = lang_hooks.get_innermost_generic_parms (t);
10748 if (!parms)
10749 /* T has no generic parameter. It means T is neither a generic type
10750 or function. End of story. */
10751 return;
10753 parms_num = TREE_VEC_LENGTH (parms);
10754 args = lang_hooks.get_innermost_generic_args (t);
10755 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10756 non_default = int_cst_value (TREE_CHAIN (args));
10757 else
10758 non_default = TREE_VEC_LENGTH (args);
10759 for (i = 0; i < parms_num; i++)
10761 tree parm, arg, arg_pack_elems;
10762 dw_die_ref parm_die;
10764 parm = TREE_VEC_ELT (parms, i);
10765 arg = TREE_VEC_ELT (args, i);
10766 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10767 gcc_assert (parm && TREE_VALUE (parm) && arg);
10769 if (parm && TREE_VALUE (parm) && arg)
10771 /* If PARM represents a template parameter pack,
10772 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10773 by DW_TAG_template_*_parameter DIEs for the argument
10774 pack elements of ARG. Note that ARG would then be
10775 an argument pack. */
10776 if (arg_pack_elems)
10777 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10778 arg_pack_elems,
10779 die);
10780 else
10781 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10782 true /* emit name */, die);
10783 if (i >= non_default)
10784 add_AT_flag (parm_die, DW_AT_default_value, 1);
10789 /* Create and return a DIE for PARM which should be
10790 the representation of a generic type parameter.
10791 For instance, in the C++ front end, PARM would be a template parameter.
10792 ARG is the argument to PARM.
10793 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10794 name of the PARM.
10795 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10796 as a child node. */
10798 static dw_die_ref
10799 generic_parameter_die (tree parm, tree arg,
10800 bool emit_name_p,
10801 dw_die_ref parent_die)
10803 dw_die_ref tmpl_die = NULL;
10804 const char *name = NULL;
10806 if (!parm || !DECL_NAME (parm) || !arg)
10807 return NULL;
10809 /* We support non-type generic parameters and arguments,
10810 type generic parameters and arguments, as well as
10811 generic generic parameters (a.k.a. template template parameters in C++)
10812 and arguments. */
10813 if (TREE_CODE (parm) == PARM_DECL)
10814 /* PARM is a nontype generic parameter */
10815 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10816 else if (TREE_CODE (parm) == TYPE_DECL)
10817 /* PARM is a type generic parameter. */
10818 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10819 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10820 /* PARM is a generic generic parameter.
10821 Its DIE is a GNU extension. It shall have a
10822 DW_AT_name attribute to represent the name of the template template
10823 parameter, and a DW_AT_GNU_template_name attribute to represent the
10824 name of the template template argument. */
10825 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10826 parent_die, parm);
10827 else
10828 gcc_unreachable ();
10830 if (tmpl_die)
10832 tree tmpl_type;
10834 /* If PARM is a generic parameter pack, it means we are
10835 emitting debug info for a template argument pack element.
10836 In other terms, ARG is a template argument pack element.
10837 In that case, we don't emit any DW_AT_name attribute for
10838 the die. */
10839 if (emit_name_p)
10841 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10842 gcc_assert (name);
10843 add_AT_string (tmpl_die, DW_AT_name, name);
10846 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10848 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10849 TMPL_DIE should have a child DW_AT_type attribute that is set
10850 to the type of the argument to PARM, which is ARG.
10851 If PARM is a type generic parameter, TMPL_DIE should have a
10852 child DW_AT_type that is set to ARG. */
10853 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10854 add_type_attribute (tmpl_die, tmpl_type,
10855 (TREE_THIS_VOLATILE (tmpl_type)
10856 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10857 parent_die);
10859 else
10861 /* So TMPL_DIE is a DIE representing a
10862 a generic generic template parameter, a.k.a template template
10863 parameter in C++ and arg is a template. */
10865 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10866 to the name of the argument. */
10867 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10868 if (name)
10869 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10872 if (TREE_CODE (parm) == PARM_DECL)
10873 /* So PARM is a non-type generic parameter.
10874 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10875 attribute of TMPL_DIE which value represents the value
10876 of ARG.
10877 We must be careful here:
10878 The value of ARG might reference some function decls.
10879 We might currently be emitting debug info for a generic
10880 type and types are emitted before function decls, we don't
10881 know if the function decls referenced by ARG will actually be
10882 emitted after cgraph computations.
10883 So must defer the generation of the DW_AT_const_value to
10884 after cgraph is ready. */
10885 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10888 return tmpl_die;
10891 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10892 PARM_PACK must be a template parameter pack. The returned DIE
10893 will be child DIE of PARENT_DIE. */
10895 static dw_die_ref
10896 template_parameter_pack_die (tree parm_pack,
10897 tree parm_pack_args,
10898 dw_die_ref parent_die)
10900 dw_die_ref die;
10901 int j;
10903 gcc_assert (parent_die && parm_pack);
10905 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10906 add_name_and_src_coords_attributes (die, parm_pack);
10907 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10908 generic_parameter_die (parm_pack,
10909 TREE_VEC_ELT (parm_pack_args, j),
10910 false /* Don't emit DW_AT_name */,
10911 die);
10912 return die;
10915 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10916 an enumerated type. */
10918 static inline int
10919 type_is_enum (const_tree type)
10921 return TREE_CODE (type) == ENUMERAL_TYPE;
10924 /* Return the DBX register number described by a given RTL node. */
10926 static unsigned int
10927 dbx_reg_number (const_rtx rtl)
10929 unsigned regno = REGNO (rtl);
10931 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10933 #ifdef LEAF_REG_REMAP
10934 if (crtl->uses_only_leaf_regs)
10936 int leaf_reg = LEAF_REG_REMAP (regno);
10937 if (leaf_reg != -1)
10938 regno = (unsigned) leaf_reg;
10940 #endif
10942 regno = DBX_REGISTER_NUMBER (regno);
10943 gcc_assert (regno != INVALID_REGNUM);
10944 return regno;
10947 /* Optionally add a DW_OP_piece term to a location description expression.
10948 DW_OP_piece is only added if the location description expression already
10949 doesn't end with DW_OP_piece. */
10951 static void
10952 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10954 dw_loc_descr_ref loc;
10956 if (*list_head != NULL)
10958 /* Find the end of the chain. */
10959 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10962 if (loc->dw_loc_opc != DW_OP_piece)
10963 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10967 /* Return a location descriptor that designates a machine register or
10968 zero if there is none. */
10970 static dw_loc_descr_ref
10971 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10973 rtx regs;
10975 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10976 return 0;
10978 /* We only use "frame base" when we're sure we're talking about the
10979 post-prologue local stack frame. We do this by *not* running
10980 register elimination until this point, and recognizing the special
10981 argument pointer and soft frame pointer rtx's.
10982 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10983 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10984 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10986 dw_loc_descr_ref result = NULL;
10988 if (dwarf_version >= 4 || !dwarf_strict)
10990 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10991 initialized);
10992 if (result)
10993 add_loc_descr (&result,
10994 new_loc_descr (DW_OP_stack_value, 0, 0));
10996 return result;
10999 regs = targetm.dwarf_register_span (rtl);
11001 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11002 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11003 else
11005 unsigned int dbx_regnum = dbx_reg_number (rtl);
11006 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11007 return 0;
11008 return one_reg_loc_descriptor (dbx_regnum, initialized);
11012 /* Return a location descriptor that designates a machine register for
11013 a given hard register number. */
11015 static dw_loc_descr_ref
11016 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11018 dw_loc_descr_ref reg_loc_descr;
11020 if (regno <= 31)
11021 reg_loc_descr
11022 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11023 else
11024 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11026 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11027 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11029 return reg_loc_descr;
11032 /* Given an RTL of a register, return a location descriptor that
11033 designates a value that spans more than one register. */
11035 static dw_loc_descr_ref
11036 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11037 enum var_init_status initialized)
11039 int size, i;
11040 dw_loc_descr_ref loc_result = NULL;
11042 /* Simple, contiguous registers. */
11043 if (regs == NULL_RTX)
11045 unsigned reg = REGNO (rtl);
11046 int nregs;
11048 #ifdef LEAF_REG_REMAP
11049 if (crtl->uses_only_leaf_regs)
11051 int leaf_reg = LEAF_REG_REMAP (reg);
11052 if (leaf_reg != -1)
11053 reg = (unsigned) leaf_reg;
11055 #endif
11057 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11058 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11060 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11062 loc_result = NULL;
11063 while (nregs--)
11065 dw_loc_descr_ref t;
11067 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11068 VAR_INIT_STATUS_INITIALIZED);
11069 add_loc_descr (&loc_result, t);
11070 add_loc_descr_op_piece (&loc_result, size);
11071 ++reg;
11073 return loc_result;
11076 /* Now onto stupid register sets in non contiguous locations. */
11078 gcc_assert (GET_CODE (regs) == PARALLEL);
11080 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11081 loc_result = NULL;
11083 for (i = 0; i < XVECLEN (regs, 0); ++i)
11085 dw_loc_descr_ref t;
11087 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11088 VAR_INIT_STATUS_INITIALIZED);
11089 add_loc_descr (&loc_result, t);
11090 add_loc_descr_op_piece (&loc_result, size);
11093 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11094 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11095 return loc_result;
11098 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11100 /* Return a location descriptor that designates a constant i,
11101 as a compound operation from constant (i >> shift), constant shift
11102 and DW_OP_shl. */
11104 static dw_loc_descr_ref
11105 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11107 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11108 add_loc_descr (&ret, int_loc_descriptor (shift));
11109 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11110 return ret;
11113 /* Return a location descriptor that designates a constant. */
11115 static dw_loc_descr_ref
11116 int_loc_descriptor (HOST_WIDE_INT i)
11118 enum dwarf_location_atom op;
11120 /* Pick the smallest representation of a constant, rather than just
11121 defaulting to the LEB encoding. */
11122 if (i >= 0)
11124 int clz = clz_hwi (i);
11125 int ctz = ctz_hwi (i);
11126 if (i <= 31)
11127 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11128 else if (i <= 0xff)
11129 op = DW_OP_const1u;
11130 else if (i <= 0xffff)
11131 op = DW_OP_const2u;
11132 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11133 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11134 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11135 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11136 while DW_OP_const4u is 5 bytes. */
11137 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11138 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11139 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11140 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11141 while DW_OP_const4u is 5 bytes. */
11142 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11143 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11144 op = DW_OP_const4u;
11145 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11146 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11147 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11148 while DW_OP_constu of constant >= 0x100000000 takes at least
11149 6 bytes. */
11150 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11151 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11152 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11153 >= HOST_BITS_PER_WIDE_INT)
11154 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11155 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11156 while DW_OP_constu takes in this case at least 6 bytes. */
11157 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11158 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11159 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11160 && size_of_uleb128 (i) > 6)
11161 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11162 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11163 else
11164 op = DW_OP_constu;
11166 else
11168 if (i >= -0x80)
11169 op = DW_OP_const1s;
11170 else if (i >= -0x8000)
11171 op = DW_OP_const2s;
11172 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11174 if (size_of_int_loc_descriptor (i) < 5)
11176 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11177 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11178 return ret;
11180 op = DW_OP_const4s;
11182 else
11184 if (size_of_int_loc_descriptor (i)
11185 < (unsigned long) 1 + size_of_sleb128 (i))
11187 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11188 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11189 return ret;
11191 op = DW_OP_consts;
11195 return new_loc_descr (op, i, 0);
11198 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11199 without actually allocating it. */
11201 static unsigned long
11202 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11204 return size_of_int_loc_descriptor (i >> shift)
11205 + size_of_int_loc_descriptor (shift)
11206 + 1;
11209 /* Return size_of_locs (int_loc_descriptor (i)) without
11210 actually allocating it. */
11212 static unsigned long
11213 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11215 unsigned long s;
11217 if (i >= 0)
11219 int clz, ctz;
11220 if (i <= 31)
11221 return 1;
11222 else if (i <= 0xff)
11223 return 2;
11224 else if (i <= 0xffff)
11225 return 3;
11226 clz = clz_hwi (i);
11227 ctz = ctz_hwi (i);
11228 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11229 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11230 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11231 - clz - 5);
11232 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11233 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11234 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11235 - clz - 8);
11236 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11237 return 5;
11238 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11239 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11240 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11241 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11242 - clz - 8);
11243 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11244 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11245 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11246 - clz - 16);
11247 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11248 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11249 && s > 6)
11250 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11251 - clz - 32);
11252 else
11253 return 1 + s;
11255 else
11257 if (i >= -0x80)
11258 return 2;
11259 else if (i >= -0x8000)
11260 return 3;
11261 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11263 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11265 s = size_of_int_loc_descriptor (-i) + 1;
11266 if (s < 5)
11267 return s;
11269 return 5;
11271 else
11273 unsigned long r = 1 + size_of_sleb128 (i);
11274 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11276 s = size_of_int_loc_descriptor (-i) + 1;
11277 if (s < r)
11278 return s;
11280 return r;
11285 /* Return loc description representing "address" of integer value.
11286 This can appear only as toplevel expression. */
11288 static dw_loc_descr_ref
11289 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11291 int litsize;
11292 dw_loc_descr_ref loc_result = NULL;
11294 if (!(dwarf_version >= 4 || !dwarf_strict))
11295 return NULL;
11297 litsize = size_of_int_loc_descriptor (i);
11298 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11299 is more compact. For DW_OP_stack_value we need:
11300 litsize + 1 (DW_OP_stack_value)
11301 and for DW_OP_implicit_value:
11302 1 (DW_OP_implicit_value) + 1 (length) + size. */
11303 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11305 loc_result = int_loc_descriptor (i);
11306 add_loc_descr (&loc_result,
11307 new_loc_descr (DW_OP_stack_value, 0, 0));
11308 return loc_result;
11311 loc_result = new_loc_descr (DW_OP_implicit_value,
11312 size, 0);
11313 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11314 loc_result->dw_loc_oprnd2.v.val_int = i;
11315 return loc_result;
11318 /* Return a location descriptor that designates a base+offset location. */
11320 static dw_loc_descr_ref
11321 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11322 enum var_init_status initialized)
11324 unsigned int regno;
11325 dw_loc_descr_ref result;
11326 dw_fde_ref fde = cfun->fde;
11328 /* We only use "frame base" when we're sure we're talking about the
11329 post-prologue local stack frame. We do this by *not* running
11330 register elimination until this point, and recognizing the special
11331 argument pointer and soft frame pointer rtx's. */
11332 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11334 rtx elim = (ira_use_lra_p
11335 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11336 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11338 if (elim != reg)
11340 if (GET_CODE (elim) == PLUS)
11342 offset += INTVAL (XEXP (elim, 1));
11343 elim = XEXP (elim, 0);
11345 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11346 && (elim == hard_frame_pointer_rtx
11347 || elim == stack_pointer_rtx))
11348 || elim == (frame_pointer_needed
11349 ? hard_frame_pointer_rtx
11350 : stack_pointer_rtx));
11352 /* If drap register is used to align stack, use frame
11353 pointer + offset to access stack variables. If stack
11354 is aligned without drap, use stack pointer + offset to
11355 access stack variables. */
11356 if (crtl->stack_realign_tried
11357 && reg == frame_pointer_rtx)
11359 int base_reg
11360 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11361 ? HARD_FRAME_POINTER_REGNUM
11362 : REGNO (elim));
11363 return new_reg_loc_descr (base_reg, offset);
11366 gcc_assert (frame_pointer_fb_offset_valid);
11367 offset += frame_pointer_fb_offset;
11368 return new_loc_descr (DW_OP_fbreg, offset, 0);
11372 regno = REGNO (reg);
11373 #ifdef LEAF_REG_REMAP
11374 if (crtl->uses_only_leaf_regs)
11376 int leaf_reg = LEAF_REG_REMAP (regno);
11377 if (leaf_reg != -1)
11378 regno = (unsigned) leaf_reg;
11380 #endif
11381 regno = DWARF_FRAME_REGNUM (regno);
11383 if (!optimize && fde
11384 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11386 /* Use cfa+offset to represent the location of arguments passed
11387 on the stack when drap is used to align stack.
11388 Only do this when not optimizing, for optimized code var-tracking
11389 is supposed to track where the arguments live and the register
11390 used as vdrap or drap in some spot might be used for something
11391 else in other part of the routine. */
11392 return new_loc_descr (DW_OP_fbreg, offset, 0);
11395 if (regno <= 31)
11396 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11397 offset, 0);
11398 else
11399 result = new_loc_descr (DW_OP_bregx, regno, offset);
11401 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11402 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11404 return result;
11407 /* Return true if this RTL expression describes a base+offset calculation. */
11409 static inline int
11410 is_based_loc (const_rtx rtl)
11412 return (GET_CODE (rtl) == PLUS
11413 && ((REG_P (XEXP (rtl, 0))
11414 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11415 && CONST_INT_P (XEXP (rtl, 1)))));
11418 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11419 failed. */
11421 static dw_loc_descr_ref
11422 tls_mem_loc_descriptor (rtx mem)
11424 tree base;
11425 dw_loc_descr_ref loc_result;
11427 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11428 return NULL;
11430 base = get_base_address (MEM_EXPR (mem));
11431 if (base == NULL
11432 || TREE_CODE (base) != VAR_DECL
11433 || !DECL_THREAD_LOCAL_P (base))
11434 return NULL;
11436 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11437 if (loc_result == NULL)
11438 return NULL;
11440 if (MEM_OFFSET (mem))
11441 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11443 return loc_result;
11446 /* Output debug info about reason why we failed to expand expression as dwarf
11447 expression. */
11449 static void
11450 expansion_failed (tree expr, rtx rtl, char const *reason)
11452 if (dump_file && (dump_flags & TDF_DETAILS))
11454 fprintf (dump_file, "Failed to expand as dwarf: ");
11455 if (expr)
11456 print_generic_expr (dump_file, expr, dump_flags);
11457 if (rtl)
11459 fprintf (dump_file, "\n");
11460 print_rtl (dump_file, rtl);
11462 fprintf (dump_file, "\nReason: %s\n", reason);
11466 /* Helper function for const_ok_for_output. */
11468 static bool
11469 const_ok_for_output_1 (rtx rtl)
11471 if (GET_CODE (rtl) == UNSPEC)
11473 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11474 we can't express it in the debug info. */
11475 #ifdef ENABLE_CHECKING
11476 /* Don't complain about TLS UNSPECs, those are just too hard to
11477 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11478 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11479 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11480 if (XVECLEN (rtl, 0) == 0
11481 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11482 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11483 inform (current_function_decl
11484 ? DECL_SOURCE_LOCATION (current_function_decl)
11485 : UNKNOWN_LOCATION,
11486 #if NUM_UNSPEC_VALUES > 0
11487 "non-delegitimized UNSPEC %s (%d) found in variable location",
11488 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11489 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11490 XINT (rtl, 1));
11491 #else
11492 "non-delegitimized UNSPEC %d found in variable location",
11493 XINT (rtl, 1));
11494 #endif
11495 #endif
11496 expansion_failed (NULL_TREE, rtl,
11497 "UNSPEC hasn't been delegitimized.\n");
11498 return false;
11501 if (targetm.const_not_ok_for_debug_p (rtl))
11503 expansion_failed (NULL_TREE, rtl,
11504 "Expression rejected for debug by the backend.\n");
11505 return false;
11508 /* FIXME: Refer to PR60655. It is possible for simplification
11509 of rtl expressions in var tracking to produce such expressions.
11510 We should really identify / validate expressions
11511 enclosed in CONST that can be handled by assemblers on various
11512 targets and only handle legitimate cases here. */
11513 if (GET_CODE (rtl) != SYMBOL_REF)
11515 if (GET_CODE (rtl) == NOT)
11516 return false;
11517 return true;
11520 if (CONSTANT_POOL_ADDRESS_P (rtl))
11522 bool marked;
11523 get_pool_constant_mark (rtl, &marked);
11524 /* If all references to this pool constant were optimized away,
11525 it was not output and thus we can't represent it. */
11526 if (!marked)
11528 expansion_failed (NULL_TREE, rtl,
11529 "Constant was removed from constant pool.\n");
11530 return false;
11534 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11535 return false;
11537 /* Avoid references to external symbols in debug info, on several targets
11538 the linker might even refuse to link when linking a shared library,
11539 and in many other cases the relocations for .debug_info/.debug_loc are
11540 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11541 to be defined within the same shared library or executable are fine. */
11542 if (SYMBOL_REF_EXTERNAL_P (rtl))
11544 tree decl = SYMBOL_REF_DECL (rtl);
11546 if (decl == NULL || !targetm.binds_local_p (decl))
11548 expansion_failed (NULL_TREE, rtl,
11549 "Symbol not defined in current TU.\n");
11550 return false;
11554 return true;
11557 /* Return true if constant RTL can be emitted in DW_OP_addr or
11558 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11559 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11561 static bool
11562 const_ok_for_output (rtx rtl)
11564 if (GET_CODE (rtl) == SYMBOL_REF)
11565 return const_ok_for_output_1 (rtl);
11567 if (GET_CODE (rtl) == CONST)
11569 subrtx_var_iterator::array_type array;
11570 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11571 if (!const_ok_for_output_1 (*iter))
11572 return false;
11573 return true;
11576 return true;
11579 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11580 if possible, NULL otherwise. */
11582 static dw_die_ref
11583 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11585 dw_die_ref type_die;
11586 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11588 if (type == NULL)
11589 return NULL;
11590 switch (TREE_CODE (type))
11592 case INTEGER_TYPE:
11593 case REAL_TYPE:
11594 break;
11595 default:
11596 return NULL;
11598 type_die = lookup_type_die (type);
11599 if (!type_die)
11600 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11601 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11602 return NULL;
11603 return type_die;
11606 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11607 type matching MODE, or, if MODE is narrower than or as wide as
11608 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11609 possible. */
11611 static dw_loc_descr_ref
11612 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11614 enum machine_mode outer_mode = mode;
11615 dw_die_ref type_die;
11616 dw_loc_descr_ref cvt;
11618 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11620 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11621 return op;
11623 type_die = base_type_for_mode (outer_mode, 1);
11624 if (type_die == NULL)
11625 return NULL;
11626 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11627 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11628 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11629 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11630 add_loc_descr (&op, cvt);
11631 return op;
11634 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11636 static dw_loc_descr_ref
11637 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11638 dw_loc_descr_ref op1)
11640 dw_loc_descr_ref ret = op0;
11641 add_loc_descr (&ret, op1);
11642 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11643 if (STORE_FLAG_VALUE != 1)
11645 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11646 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11648 return ret;
11651 /* Return location descriptor for signed comparison OP RTL. */
11653 static dw_loc_descr_ref
11654 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11655 enum machine_mode mem_mode)
11657 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11658 dw_loc_descr_ref op0, op1;
11659 int shift;
11661 if (op_mode == VOIDmode)
11662 op_mode = GET_MODE (XEXP (rtl, 1));
11663 if (op_mode == VOIDmode)
11664 return NULL;
11666 if (dwarf_strict
11667 && (GET_MODE_CLASS (op_mode) != MODE_INT
11668 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11669 return NULL;
11671 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11672 VAR_INIT_STATUS_INITIALIZED);
11673 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11674 VAR_INIT_STATUS_INITIALIZED);
11676 if (op0 == NULL || op1 == NULL)
11677 return NULL;
11679 if (GET_MODE_CLASS (op_mode) != MODE_INT
11680 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11681 return compare_loc_descriptor (op, op0, op1);
11683 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11685 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11686 dw_loc_descr_ref cvt;
11688 if (type_die == NULL)
11689 return NULL;
11690 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11691 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11692 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11693 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11694 add_loc_descr (&op0, cvt);
11695 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11696 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11697 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11698 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11699 add_loc_descr (&op1, cvt);
11700 return compare_loc_descriptor (op, op0, op1);
11703 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11704 /* For eq/ne, if the operands are known to be zero-extended,
11705 there is no need to do the fancy shifting up. */
11706 if (op == DW_OP_eq || op == DW_OP_ne)
11708 dw_loc_descr_ref last0, last1;
11709 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11711 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11713 /* deref_size zero extends, and for constants we can check
11714 whether they are zero extended or not. */
11715 if (((last0->dw_loc_opc == DW_OP_deref_size
11716 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11717 || (CONST_INT_P (XEXP (rtl, 0))
11718 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11719 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11720 && ((last1->dw_loc_opc == DW_OP_deref_size
11721 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11722 || (CONST_INT_P (XEXP (rtl, 1))
11723 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11724 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11725 return compare_loc_descriptor (op, op0, op1);
11727 /* EQ/NE comparison against constant in narrower type than
11728 DWARF2_ADDR_SIZE can be performed either as
11729 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11730 DW_OP_{eq,ne}
11732 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11733 DW_OP_{eq,ne}. Pick whatever is shorter. */
11734 if (CONST_INT_P (XEXP (rtl, 1))
11735 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11736 && (size_of_int_loc_descriptor (shift) + 1
11737 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11738 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11739 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11740 & GET_MODE_MASK (op_mode))))
11742 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11743 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11744 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11745 & GET_MODE_MASK (op_mode));
11746 return compare_loc_descriptor (op, op0, op1);
11749 add_loc_descr (&op0, int_loc_descriptor (shift));
11750 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11751 if (CONST_INT_P (XEXP (rtl, 1)))
11752 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11753 else
11755 add_loc_descr (&op1, int_loc_descriptor (shift));
11756 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11758 return compare_loc_descriptor (op, op0, op1);
11761 /* Return location descriptor for unsigned comparison OP RTL. */
11763 static dw_loc_descr_ref
11764 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11765 enum machine_mode mem_mode)
11767 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11768 dw_loc_descr_ref op0, op1;
11770 if (op_mode == VOIDmode)
11771 op_mode = GET_MODE (XEXP (rtl, 1));
11772 if (op_mode == VOIDmode)
11773 return NULL;
11774 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11775 return NULL;
11777 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11778 return NULL;
11780 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11781 VAR_INIT_STATUS_INITIALIZED);
11782 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11783 VAR_INIT_STATUS_INITIALIZED);
11785 if (op0 == NULL || op1 == NULL)
11786 return NULL;
11788 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11790 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11791 dw_loc_descr_ref last0, last1;
11792 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11794 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11796 if (CONST_INT_P (XEXP (rtl, 0)))
11797 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11798 /* deref_size zero extends, so no need to mask it again. */
11799 else if (last0->dw_loc_opc != DW_OP_deref_size
11800 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11802 add_loc_descr (&op0, int_loc_descriptor (mask));
11803 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11805 if (CONST_INT_P (XEXP (rtl, 1)))
11806 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11807 /* deref_size zero extends, so no need to mask it again. */
11808 else if (last1->dw_loc_opc != DW_OP_deref_size
11809 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11811 add_loc_descr (&op1, int_loc_descriptor (mask));
11812 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11815 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11817 HOST_WIDE_INT bias = 1;
11818 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11819 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11820 if (CONST_INT_P (XEXP (rtl, 1)))
11821 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11822 + INTVAL (XEXP (rtl, 1)));
11823 else
11824 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11825 bias, 0));
11827 return compare_loc_descriptor (op, op0, op1);
11830 /* Return location descriptor for {U,S}{MIN,MAX}. */
11832 static dw_loc_descr_ref
11833 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11834 enum machine_mode mem_mode)
11836 enum dwarf_location_atom op;
11837 dw_loc_descr_ref op0, op1, ret;
11838 dw_loc_descr_ref bra_node, drop_node;
11840 if (dwarf_strict
11841 && (GET_MODE_CLASS (mode) != MODE_INT
11842 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11843 return NULL;
11845 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11846 VAR_INIT_STATUS_INITIALIZED);
11847 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11848 VAR_INIT_STATUS_INITIALIZED);
11850 if (op0 == NULL || op1 == NULL)
11851 return NULL;
11853 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11854 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11855 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11856 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11858 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11860 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11861 add_loc_descr (&op0, int_loc_descriptor (mask));
11862 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11863 add_loc_descr (&op1, int_loc_descriptor (mask));
11864 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11866 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11868 HOST_WIDE_INT bias = 1;
11869 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11870 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11871 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11874 else if (GET_MODE_CLASS (mode) == MODE_INT
11875 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11877 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11878 add_loc_descr (&op0, int_loc_descriptor (shift));
11879 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11880 add_loc_descr (&op1, int_loc_descriptor (shift));
11881 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11883 else if (GET_MODE_CLASS (mode) == MODE_INT
11884 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11886 dw_die_ref type_die = base_type_for_mode (mode, 0);
11887 dw_loc_descr_ref cvt;
11888 if (type_die == NULL)
11889 return NULL;
11890 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11891 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11892 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11893 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11894 add_loc_descr (&op0, cvt);
11895 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11896 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11897 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11898 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11899 add_loc_descr (&op1, cvt);
11902 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11903 op = DW_OP_lt;
11904 else
11905 op = DW_OP_gt;
11906 ret = op0;
11907 add_loc_descr (&ret, op1);
11908 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11909 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11910 add_loc_descr (&ret, bra_node);
11911 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11912 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11913 add_loc_descr (&ret, drop_node);
11914 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11915 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11916 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11917 && GET_MODE_CLASS (mode) == MODE_INT
11918 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11919 ret = convert_descriptor_to_mode (mode, ret);
11920 return ret;
11923 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11924 but after converting arguments to type_die, afterwards
11925 convert back to unsigned. */
11927 static dw_loc_descr_ref
11928 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11929 enum machine_mode mode, enum machine_mode mem_mode)
11931 dw_loc_descr_ref cvt, op0, op1;
11933 if (type_die == NULL)
11934 return NULL;
11935 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11936 VAR_INIT_STATUS_INITIALIZED);
11937 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11938 VAR_INIT_STATUS_INITIALIZED);
11939 if (op0 == NULL || op1 == NULL)
11940 return NULL;
11941 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11942 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11943 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11944 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11945 add_loc_descr (&op0, cvt);
11946 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11947 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11948 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11949 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11950 add_loc_descr (&op1, cvt);
11951 add_loc_descr (&op0, op1);
11952 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11953 return convert_descriptor_to_mode (mode, op0);
11956 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11957 const0 is DW_OP_lit0 or corresponding typed constant,
11958 const1 is DW_OP_lit1 or corresponding typed constant
11959 and constMSB is constant with just the MSB bit set
11960 for the mode):
11961 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11962 L1: const0 DW_OP_swap
11963 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11964 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11965 L3: DW_OP_drop
11966 L4: DW_OP_nop
11968 CTZ is similar:
11969 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11970 L1: const0 DW_OP_swap
11971 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11972 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11973 L3: DW_OP_drop
11974 L4: DW_OP_nop
11976 FFS is similar:
11977 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11978 L1: const1 DW_OP_swap
11979 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11980 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11981 L3: DW_OP_drop
11982 L4: DW_OP_nop */
11984 static dw_loc_descr_ref
11985 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11986 enum machine_mode mem_mode)
11988 dw_loc_descr_ref op0, ret, tmp;
11989 HOST_WIDE_INT valv;
11990 dw_loc_descr_ref l1jump, l1label;
11991 dw_loc_descr_ref l2jump, l2label;
11992 dw_loc_descr_ref l3jump, l3label;
11993 dw_loc_descr_ref l4jump, l4label;
11994 rtx msb;
11996 if (GET_MODE_CLASS (mode) != MODE_INT
11997 || GET_MODE (XEXP (rtl, 0)) != mode)
11998 return NULL;
12000 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12001 VAR_INIT_STATUS_INITIALIZED);
12002 if (op0 == NULL)
12003 return NULL;
12004 ret = op0;
12005 if (GET_CODE (rtl) == CLZ)
12007 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12008 valv = GET_MODE_BITSIZE (mode);
12010 else if (GET_CODE (rtl) == FFS)
12011 valv = 0;
12012 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12013 valv = GET_MODE_BITSIZE (mode);
12014 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12015 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12016 add_loc_descr (&ret, l1jump);
12017 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12018 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12019 VAR_INIT_STATUS_INITIALIZED);
12020 if (tmp == NULL)
12021 return NULL;
12022 add_loc_descr (&ret, tmp);
12023 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12024 add_loc_descr (&ret, l4jump);
12025 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12026 ? const1_rtx : const0_rtx,
12027 mode, mem_mode,
12028 VAR_INIT_STATUS_INITIALIZED);
12029 if (l1label == NULL)
12030 return NULL;
12031 add_loc_descr (&ret, l1label);
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12034 add_loc_descr (&ret, l2label);
12035 if (GET_CODE (rtl) != CLZ)
12036 msb = const1_rtx;
12037 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12038 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12039 << (GET_MODE_BITSIZE (mode) - 1));
12040 else
12041 msb = immed_wide_int_const
12042 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12043 GET_MODE_PRECISION (mode)), mode);
12044 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12045 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12046 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12047 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12048 else
12049 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12050 VAR_INIT_STATUS_INITIALIZED);
12051 if (tmp == NULL)
12052 return NULL;
12053 add_loc_descr (&ret, tmp);
12054 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12055 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12056 add_loc_descr (&ret, l3jump);
12057 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12058 VAR_INIT_STATUS_INITIALIZED);
12059 if (tmp == NULL)
12060 return NULL;
12061 add_loc_descr (&ret, tmp);
12062 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12063 ? DW_OP_shl : DW_OP_shr, 0, 0));
12064 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12066 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12067 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12068 add_loc_descr (&ret, l2jump);
12069 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12070 add_loc_descr (&ret, l3label);
12071 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12072 add_loc_descr (&ret, l4label);
12073 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12074 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12075 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12076 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12077 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12078 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12079 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12080 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12081 return ret;
12084 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12085 const1 is DW_OP_lit1 or corresponding typed constant):
12086 const0 DW_OP_swap
12087 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12088 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12089 L2: DW_OP_drop
12091 PARITY is similar:
12092 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12093 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12094 L2: DW_OP_drop */
12096 static dw_loc_descr_ref
12097 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12098 enum machine_mode mem_mode)
12100 dw_loc_descr_ref op0, ret, tmp;
12101 dw_loc_descr_ref l1jump, l1label;
12102 dw_loc_descr_ref l2jump, l2label;
12104 if (GET_MODE_CLASS (mode) != MODE_INT
12105 || GET_MODE (XEXP (rtl, 0)) != mode)
12106 return NULL;
12108 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12109 VAR_INIT_STATUS_INITIALIZED);
12110 if (op0 == NULL)
12111 return NULL;
12112 ret = op0;
12113 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12114 VAR_INIT_STATUS_INITIALIZED);
12115 if (tmp == NULL)
12116 return NULL;
12117 add_loc_descr (&ret, tmp);
12118 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12119 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12120 add_loc_descr (&ret, l1label);
12121 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12122 add_loc_descr (&ret, l2jump);
12123 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12124 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12125 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12126 VAR_INIT_STATUS_INITIALIZED);
12127 if (tmp == NULL)
12128 return NULL;
12129 add_loc_descr (&ret, tmp);
12130 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12131 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12132 ? DW_OP_plus : DW_OP_xor, 0, 0));
12133 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12134 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12135 VAR_INIT_STATUS_INITIALIZED);
12136 add_loc_descr (&ret, tmp);
12137 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12138 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12139 add_loc_descr (&ret, l1jump);
12140 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12141 add_loc_descr (&ret, l2label);
12142 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12143 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12144 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12145 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12146 return ret;
12149 /* BSWAP (constS is initial shift count, either 56 or 24):
12150 constS const0
12151 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12152 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12153 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12154 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12155 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12157 static dw_loc_descr_ref
12158 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12159 enum machine_mode mem_mode)
12161 dw_loc_descr_ref op0, ret, tmp;
12162 dw_loc_descr_ref l1jump, l1label;
12163 dw_loc_descr_ref l2jump, l2label;
12165 if (GET_MODE_CLASS (mode) != MODE_INT
12166 || BITS_PER_UNIT != 8
12167 || (GET_MODE_BITSIZE (mode) != 32
12168 && GET_MODE_BITSIZE (mode) != 64))
12169 return NULL;
12171 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12172 VAR_INIT_STATUS_INITIALIZED);
12173 if (op0 == NULL)
12174 return NULL;
12176 ret = op0;
12177 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12178 mode, mem_mode,
12179 VAR_INIT_STATUS_INITIALIZED);
12180 if (tmp == NULL)
12181 return NULL;
12182 add_loc_descr (&ret, tmp);
12183 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12184 VAR_INIT_STATUS_INITIALIZED);
12185 if (tmp == NULL)
12186 return NULL;
12187 add_loc_descr (&ret, tmp);
12188 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12189 add_loc_descr (&ret, l1label);
12190 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12191 mode, mem_mode,
12192 VAR_INIT_STATUS_INITIALIZED);
12193 add_loc_descr (&ret, tmp);
12194 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12195 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12196 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12197 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12198 VAR_INIT_STATUS_INITIALIZED);
12199 if (tmp == NULL)
12200 return NULL;
12201 add_loc_descr (&ret, tmp);
12202 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12203 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12204 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12205 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12207 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12208 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12209 VAR_INIT_STATUS_INITIALIZED);
12210 add_loc_descr (&ret, tmp);
12211 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12212 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12213 add_loc_descr (&ret, l2jump);
12214 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12215 VAR_INIT_STATUS_INITIALIZED);
12216 add_loc_descr (&ret, tmp);
12217 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12218 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12219 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12220 add_loc_descr (&ret, l1jump);
12221 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12222 add_loc_descr (&ret, l2label);
12223 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12224 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12225 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12226 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12227 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12228 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12229 return ret;
12232 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12233 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12234 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12235 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12237 ROTATERT is similar:
12238 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12239 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12240 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12242 static dw_loc_descr_ref
12243 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12244 enum machine_mode mem_mode)
12246 rtx rtlop1 = XEXP (rtl, 1);
12247 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12248 int i;
12250 if (GET_MODE_CLASS (mode) != MODE_INT)
12251 return NULL;
12253 if (GET_MODE (rtlop1) != VOIDmode
12254 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12255 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12256 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12259 VAR_INIT_STATUS_INITIALIZED);
12260 if (op0 == NULL || op1 == NULL)
12261 return NULL;
12262 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12263 for (i = 0; i < 2; i++)
12265 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12266 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12267 mode, mem_mode,
12268 VAR_INIT_STATUS_INITIALIZED);
12269 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12270 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12271 ? DW_OP_const4u
12272 : HOST_BITS_PER_WIDE_INT == 64
12273 ? DW_OP_const8u : DW_OP_constu,
12274 GET_MODE_MASK (mode), 0);
12275 else
12276 mask[i] = NULL;
12277 if (mask[i] == NULL)
12278 return NULL;
12279 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12281 ret = op0;
12282 add_loc_descr (&ret, op1);
12283 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12284 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12285 if (GET_CODE (rtl) == ROTATERT)
12287 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12288 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12289 GET_MODE_BITSIZE (mode), 0));
12291 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12292 if (mask[0] != NULL)
12293 add_loc_descr (&ret, mask[0]);
12294 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12295 if (mask[1] != NULL)
12297 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12298 add_loc_descr (&ret, mask[1]);
12299 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12301 if (GET_CODE (rtl) == ROTATE)
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12304 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12305 GET_MODE_BITSIZE (mode), 0));
12307 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12309 return ret;
12312 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12313 for DEBUG_PARAMETER_REF RTL. */
12315 static dw_loc_descr_ref
12316 parameter_ref_descriptor (rtx rtl)
12318 dw_loc_descr_ref ret;
12319 dw_die_ref ref;
12321 if (dwarf_strict)
12322 return NULL;
12323 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12324 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12325 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12326 if (ref)
12328 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12329 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12330 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12332 else
12334 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12335 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12337 return ret;
12340 /* The following routine converts the RTL for a variable or parameter
12341 (resident in memory) into an equivalent Dwarf representation of a
12342 mechanism for getting the address of that same variable onto the top of a
12343 hypothetical "address evaluation" stack.
12345 When creating memory location descriptors, we are effectively transforming
12346 the RTL for a memory-resident object into its Dwarf postfix expression
12347 equivalent. This routine recursively descends an RTL tree, turning
12348 it into Dwarf postfix code as it goes.
12350 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12352 MEM_MODE is the mode of the memory reference, needed to handle some
12353 autoincrement addressing modes.
12355 Return 0 if we can't represent the location. */
12357 dw_loc_descr_ref
12358 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12359 enum machine_mode mem_mode,
12360 enum var_init_status initialized)
12362 dw_loc_descr_ref mem_loc_result = NULL;
12363 enum dwarf_location_atom op;
12364 dw_loc_descr_ref op0, op1;
12365 rtx inner = NULL_RTX;
12367 if (mode == VOIDmode)
12368 mode = GET_MODE (rtl);
12370 /* Note that for a dynamically sized array, the location we will generate a
12371 description of here will be the lowest numbered location which is
12372 actually within the array. That's *not* necessarily the same as the
12373 zeroth element of the array. */
12375 rtl = targetm.delegitimize_address (rtl);
12377 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12378 return NULL;
12380 switch (GET_CODE (rtl))
12382 case POST_INC:
12383 case POST_DEC:
12384 case POST_MODIFY:
12385 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12387 case SUBREG:
12388 /* The case of a subreg may arise when we have a local (register)
12389 variable or a formal (register) parameter which doesn't quite fill
12390 up an entire register. For now, just assume that it is
12391 legitimate to make the Dwarf info refer to the whole register which
12392 contains the given subreg. */
12393 if (!subreg_lowpart_p (rtl))
12394 break;
12395 inner = SUBREG_REG (rtl);
12396 case TRUNCATE:
12397 if (inner == NULL_RTX)
12398 inner = XEXP (rtl, 0);
12399 if (GET_MODE_CLASS (mode) == MODE_INT
12400 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12401 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12402 #ifdef POINTERS_EXTEND_UNSIGNED
12403 || (mode == Pmode && mem_mode != VOIDmode)
12404 #endif
12406 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12408 mem_loc_result = mem_loc_descriptor (inner,
12409 GET_MODE (inner),
12410 mem_mode, initialized);
12411 break;
12413 if (dwarf_strict)
12414 break;
12415 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12416 break;
12417 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12418 && (GET_MODE_CLASS (mode) != MODE_INT
12419 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12420 break;
12421 else
12423 dw_die_ref type_die;
12424 dw_loc_descr_ref cvt;
12426 mem_loc_result = mem_loc_descriptor (inner,
12427 GET_MODE (inner),
12428 mem_mode, initialized);
12429 if (mem_loc_result == NULL)
12430 break;
12431 type_die = base_type_for_mode (mode,
12432 GET_MODE_CLASS (mode) == MODE_INT);
12433 if (type_die == NULL)
12435 mem_loc_result = NULL;
12436 break;
12438 if (GET_MODE_SIZE (mode)
12439 != GET_MODE_SIZE (GET_MODE (inner)))
12440 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12441 else
12442 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12443 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12444 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12445 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12446 add_loc_descr (&mem_loc_result, cvt);
12448 break;
12450 case REG:
12451 if (GET_MODE_CLASS (mode) != MODE_INT
12452 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12453 && rtl != arg_pointer_rtx
12454 && rtl != frame_pointer_rtx
12455 #ifdef POINTERS_EXTEND_UNSIGNED
12456 && (mode != Pmode || mem_mode == VOIDmode)
12457 #endif
12460 dw_die_ref type_die;
12461 unsigned int dbx_regnum;
12463 if (dwarf_strict)
12464 break;
12465 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12466 break;
12467 type_die = base_type_for_mode (mode,
12468 GET_MODE_CLASS (mode) == MODE_INT);
12469 if (type_die == NULL)
12470 break;
12472 dbx_regnum = dbx_reg_number (rtl);
12473 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12474 break;
12475 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12476 dbx_regnum, 0);
12477 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12478 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12479 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12480 break;
12482 /* Whenever a register number forms a part of the description of the
12483 method for calculating the (dynamic) address of a memory resident
12484 object, DWARF rules require the register number be referred to as
12485 a "base register". This distinction is not based in any way upon
12486 what category of register the hardware believes the given register
12487 belongs to. This is strictly DWARF terminology we're dealing with
12488 here. Note that in cases where the location of a memory-resident
12489 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12490 OP_CONST (0)) the actual DWARF location descriptor that we generate
12491 may just be OP_BASEREG (basereg). This may look deceptively like
12492 the object in question was allocated to a register (rather than in
12493 memory) so DWARF consumers need to be aware of the subtle
12494 distinction between OP_REG and OP_BASEREG. */
12495 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12496 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12497 else if (stack_realign_drap
12498 && crtl->drap_reg
12499 && crtl->args.internal_arg_pointer == rtl
12500 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12502 /* If RTL is internal_arg_pointer, which has been optimized
12503 out, use DRAP instead. */
12504 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12505 VAR_INIT_STATUS_INITIALIZED);
12507 break;
12509 case SIGN_EXTEND:
12510 case ZERO_EXTEND:
12511 if (GET_MODE_CLASS (mode) != MODE_INT)
12512 break;
12513 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12514 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12515 if (op0 == 0)
12516 break;
12517 else if (GET_CODE (rtl) == ZERO_EXTEND
12518 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12519 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12520 < HOST_BITS_PER_WIDE_INT
12521 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12522 to expand zero extend as two shifts instead of
12523 masking. */
12524 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12526 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12527 mem_loc_result = op0;
12528 add_loc_descr (&mem_loc_result,
12529 int_loc_descriptor (GET_MODE_MASK (imode)));
12530 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12532 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12534 int shift = DWARF2_ADDR_SIZE
12535 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12536 shift *= BITS_PER_UNIT;
12537 if (GET_CODE (rtl) == SIGN_EXTEND)
12538 op = DW_OP_shra;
12539 else
12540 op = DW_OP_shr;
12541 mem_loc_result = op0;
12542 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12543 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12544 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12545 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12547 else if (!dwarf_strict)
12549 dw_die_ref type_die1, type_die2;
12550 dw_loc_descr_ref cvt;
12552 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12553 GET_CODE (rtl) == ZERO_EXTEND);
12554 if (type_die1 == NULL)
12555 break;
12556 type_die2 = base_type_for_mode (mode, 1);
12557 if (type_die2 == NULL)
12558 break;
12559 mem_loc_result = op0;
12560 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12561 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12562 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12563 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12564 add_loc_descr (&mem_loc_result, cvt);
12565 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12566 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12567 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12568 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12569 add_loc_descr (&mem_loc_result, cvt);
12571 break;
12573 case MEM:
12575 rtx new_rtl = avoid_constant_pool_reference (rtl);
12576 if (new_rtl != rtl)
12578 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12579 initialized);
12580 if (mem_loc_result != NULL)
12581 return mem_loc_result;
12584 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12585 get_address_mode (rtl), mode,
12586 VAR_INIT_STATUS_INITIALIZED);
12587 if (mem_loc_result == NULL)
12588 mem_loc_result = tls_mem_loc_descriptor (rtl);
12589 if (mem_loc_result != NULL)
12591 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12592 || GET_MODE_CLASS (mode) != MODE_INT)
12594 dw_die_ref type_die;
12595 dw_loc_descr_ref deref;
12597 if (dwarf_strict)
12598 return NULL;
12599 type_die
12600 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12601 if (type_die == NULL)
12602 return NULL;
12603 deref = new_loc_descr (DW_OP_GNU_deref_type,
12604 GET_MODE_SIZE (mode), 0);
12605 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12606 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12607 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12608 add_loc_descr (&mem_loc_result, deref);
12610 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12611 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12612 else
12613 add_loc_descr (&mem_loc_result,
12614 new_loc_descr (DW_OP_deref_size,
12615 GET_MODE_SIZE (mode), 0));
12617 break;
12619 case LO_SUM:
12620 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12622 case LABEL_REF:
12623 /* Some ports can transform a symbol ref into a label ref, because
12624 the symbol ref is too far away and has to be dumped into a constant
12625 pool. */
12626 case CONST:
12627 case SYMBOL_REF:
12628 if ((GET_MODE_CLASS (mode) != MODE_INT
12629 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12630 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12631 #ifdef POINTERS_EXTEND_UNSIGNED
12632 && (mode != Pmode || mem_mode == VOIDmode)
12633 #endif
12635 break;
12636 if (GET_CODE (rtl) == SYMBOL_REF
12637 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12639 dw_loc_descr_ref temp;
12641 /* If this is not defined, we have no way to emit the data. */
12642 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12643 break;
12645 temp = new_addr_loc_descr (rtl, dtprel_true);
12647 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12648 add_loc_descr (&mem_loc_result, temp);
12650 break;
12653 if (!const_ok_for_output (rtl))
12654 break;
12656 symref:
12657 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12658 vec_safe_push (used_rtx_array, rtl);
12659 break;
12661 case CONCAT:
12662 case CONCATN:
12663 case VAR_LOCATION:
12664 case DEBUG_IMPLICIT_PTR:
12665 expansion_failed (NULL_TREE, rtl,
12666 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12667 return 0;
12669 case ENTRY_VALUE:
12670 if (dwarf_strict)
12671 return NULL;
12672 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12674 if (GET_MODE_CLASS (mode) != MODE_INT
12675 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12676 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12677 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12678 else
12680 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12681 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12682 return NULL;
12683 op0 = one_reg_loc_descriptor (dbx_regnum,
12684 VAR_INIT_STATUS_INITIALIZED);
12687 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12688 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12690 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12691 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12692 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12693 return NULL;
12695 else
12696 gcc_unreachable ();
12697 if (op0 == NULL)
12698 return NULL;
12699 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12700 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12701 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12702 break;
12704 case DEBUG_PARAMETER_REF:
12705 mem_loc_result = parameter_ref_descriptor (rtl);
12706 break;
12708 case PRE_MODIFY:
12709 /* Extract the PLUS expression nested inside and fall into
12710 PLUS code below. */
12711 rtl = XEXP (rtl, 1);
12712 goto plus;
12714 case PRE_INC:
12715 case PRE_DEC:
12716 /* Turn these into a PLUS expression and fall into the PLUS code
12717 below. */
12718 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12719 gen_int_mode (GET_CODE (rtl) == PRE_INC
12720 ? GET_MODE_UNIT_SIZE (mem_mode)
12721 : -GET_MODE_UNIT_SIZE (mem_mode),
12722 mode));
12724 /* ... fall through ... */
12726 case PLUS:
12727 plus:
12728 if (is_based_loc (rtl)
12729 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12730 || XEXP (rtl, 0) == arg_pointer_rtx
12731 || XEXP (rtl, 0) == frame_pointer_rtx)
12732 && GET_MODE_CLASS (mode) == MODE_INT)
12733 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12734 INTVAL (XEXP (rtl, 1)),
12735 VAR_INIT_STATUS_INITIALIZED);
12736 else
12738 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12739 VAR_INIT_STATUS_INITIALIZED);
12740 if (mem_loc_result == 0)
12741 break;
12743 if (CONST_INT_P (XEXP (rtl, 1))
12744 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12745 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12746 else
12748 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12749 VAR_INIT_STATUS_INITIALIZED);
12750 if (op1 == 0)
12751 break;
12752 add_loc_descr (&mem_loc_result, op1);
12753 add_loc_descr (&mem_loc_result,
12754 new_loc_descr (DW_OP_plus, 0, 0));
12757 break;
12759 /* If a pseudo-reg is optimized away, it is possible for it to
12760 be replaced with a MEM containing a multiply or shift. */
12761 case MINUS:
12762 op = DW_OP_minus;
12763 goto do_binop;
12765 case MULT:
12766 op = DW_OP_mul;
12767 goto do_binop;
12769 case DIV:
12770 if (!dwarf_strict
12771 && GET_MODE_CLASS (mode) == MODE_INT
12772 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12774 mem_loc_result = typed_binop (DW_OP_div, rtl,
12775 base_type_for_mode (mode, 0),
12776 mode, mem_mode);
12777 break;
12779 op = DW_OP_div;
12780 goto do_binop;
12782 case UMOD:
12783 op = DW_OP_mod;
12784 goto do_binop;
12786 case ASHIFT:
12787 op = DW_OP_shl;
12788 goto do_shift;
12790 case ASHIFTRT:
12791 op = DW_OP_shra;
12792 goto do_shift;
12794 case LSHIFTRT:
12795 op = DW_OP_shr;
12796 goto do_shift;
12798 do_shift:
12799 if (GET_MODE_CLASS (mode) != MODE_INT)
12800 break;
12801 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12802 VAR_INIT_STATUS_INITIALIZED);
12804 rtx rtlop1 = XEXP (rtl, 1);
12805 if (GET_MODE (rtlop1) != VOIDmode
12806 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12807 < GET_MODE_BITSIZE (mode))
12808 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12809 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12810 VAR_INIT_STATUS_INITIALIZED);
12813 if (op0 == 0 || op1 == 0)
12814 break;
12816 mem_loc_result = op0;
12817 add_loc_descr (&mem_loc_result, op1);
12818 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12819 break;
12821 case AND:
12822 op = DW_OP_and;
12823 goto do_binop;
12825 case IOR:
12826 op = DW_OP_or;
12827 goto do_binop;
12829 case XOR:
12830 op = DW_OP_xor;
12831 goto do_binop;
12833 do_binop:
12834 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12835 VAR_INIT_STATUS_INITIALIZED);
12836 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12837 VAR_INIT_STATUS_INITIALIZED);
12839 if (op0 == 0 || op1 == 0)
12840 break;
12842 mem_loc_result = op0;
12843 add_loc_descr (&mem_loc_result, op1);
12844 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12845 break;
12847 case MOD:
12848 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12850 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12851 base_type_for_mode (mode, 0),
12852 mode, mem_mode);
12853 break;
12856 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12857 VAR_INIT_STATUS_INITIALIZED);
12858 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12859 VAR_INIT_STATUS_INITIALIZED);
12861 if (op0 == 0 || op1 == 0)
12862 break;
12864 mem_loc_result = op0;
12865 add_loc_descr (&mem_loc_result, op1);
12866 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12867 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12868 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12869 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12870 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12871 break;
12873 case UDIV:
12874 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12876 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12878 op = DW_OP_div;
12879 goto do_binop;
12881 mem_loc_result = typed_binop (DW_OP_div, rtl,
12882 base_type_for_mode (mode, 1),
12883 mode, mem_mode);
12885 break;
12887 case NOT:
12888 op = DW_OP_not;
12889 goto do_unop;
12891 case ABS:
12892 op = DW_OP_abs;
12893 goto do_unop;
12895 case NEG:
12896 op = DW_OP_neg;
12897 goto do_unop;
12899 do_unop:
12900 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12901 VAR_INIT_STATUS_INITIALIZED);
12903 if (op0 == 0)
12904 break;
12906 mem_loc_result = op0;
12907 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12908 break;
12910 case CONST_INT:
12911 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12912 #ifdef POINTERS_EXTEND_UNSIGNED
12913 || (mode == Pmode
12914 && mem_mode != VOIDmode
12915 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12916 #endif
12919 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12920 break;
12922 if (!dwarf_strict
12923 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12924 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12926 dw_die_ref type_die = base_type_for_mode (mode, 1);
12927 enum machine_mode amode;
12928 if (type_die == NULL)
12929 return NULL;
12930 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12931 MODE_INT, 0);
12932 if (INTVAL (rtl) >= 0
12933 && amode != BLKmode
12934 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12935 /* const DW_OP_GNU_convert <XXX> vs.
12936 DW_OP_GNU_const_type <XXX, 1, const>. */
12937 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12938 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12940 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12941 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12942 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12943 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12944 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12945 add_loc_descr (&mem_loc_result, op0);
12946 return mem_loc_result;
12948 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12949 INTVAL (rtl));
12950 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12951 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12952 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12953 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12954 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12955 else
12957 mem_loc_result->dw_loc_oprnd2.val_class
12958 = dw_val_class_const_double;
12959 mem_loc_result->dw_loc_oprnd2.v.val_double
12960 = double_int::from_shwi (INTVAL (rtl));
12963 break;
12965 case CONST_DOUBLE:
12966 if (!dwarf_strict)
12968 dw_die_ref type_die;
12970 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12971 CONST_DOUBLE rtx could represent either a large integer
12972 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12973 the value is always a floating point constant.
12975 When it is an integer, a CONST_DOUBLE is used whenever
12976 the constant requires 2 HWIs to be adequately represented.
12977 We output CONST_DOUBLEs as blocks. */
12978 if (mode == VOIDmode
12979 || (GET_MODE (rtl) == VOIDmode
12980 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12981 break;
12982 type_die = base_type_for_mode (mode,
12983 GET_MODE_CLASS (mode) == MODE_INT);
12984 if (type_die == NULL)
12985 return NULL;
12986 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12987 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12988 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12989 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12990 #if TARGET_SUPPORTS_WIDE_INT == 0
12991 if (!SCALAR_FLOAT_MODE_P (mode))
12993 mem_loc_result->dw_loc_oprnd2.val_class
12994 = dw_val_class_const_double;
12995 mem_loc_result->dw_loc_oprnd2.v.val_double
12996 = rtx_to_double_int (rtl);
12998 else
12999 #endif
13001 unsigned int length = GET_MODE_SIZE (mode);
13002 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13004 insert_float (rtl, array);
13005 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13006 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13007 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13008 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13011 break;
13013 case CONST_WIDE_INT:
13014 if (!dwarf_strict)
13016 dw_die_ref type_die;
13018 type_die = base_type_for_mode (mode,
13019 GET_MODE_CLASS (mode) == MODE_INT);
13020 if (type_die == NULL)
13021 return NULL;
13022 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13023 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13024 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13025 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13026 mem_loc_result->dw_loc_oprnd2.val_class
13027 = dw_val_class_wide_int;
13028 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13029 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13031 break;
13033 case EQ:
13034 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13035 break;
13037 case GE:
13038 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13039 break;
13041 case GT:
13042 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13043 break;
13045 case LE:
13046 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13047 break;
13049 case LT:
13050 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13051 break;
13053 case NE:
13054 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13055 break;
13057 case GEU:
13058 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13059 break;
13061 case GTU:
13062 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13063 break;
13065 case LEU:
13066 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13067 break;
13069 case LTU:
13070 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13071 break;
13073 case UMIN:
13074 case UMAX:
13075 if (GET_MODE_CLASS (mode) != MODE_INT)
13076 break;
13077 /* FALLTHRU */
13078 case SMIN:
13079 case SMAX:
13080 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13081 break;
13083 case ZERO_EXTRACT:
13084 case SIGN_EXTRACT:
13085 if (CONST_INT_P (XEXP (rtl, 1))
13086 && CONST_INT_P (XEXP (rtl, 2))
13087 && ((unsigned) INTVAL (XEXP (rtl, 1))
13088 + (unsigned) INTVAL (XEXP (rtl, 2))
13089 <= GET_MODE_BITSIZE (mode))
13090 && GET_MODE_CLASS (mode) == MODE_INT
13091 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13092 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13094 int shift, size;
13095 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13096 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13097 if (op0 == 0)
13098 break;
13099 if (GET_CODE (rtl) == SIGN_EXTRACT)
13100 op = DW_OP_shra;
13101 else
13102 op = DW_OP_shr;
13103 mem_loc_result = op0;
13104 size = INTVAL (XEXP (rtl, 1));
13105 shift = INTVAL (XEXP (rtl, 2));
13106 if (BITS_BIG_ENDIAN)
13107 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13108 - shift - size;
13109 if (shift + size != (int) DWARF2_ADDR_SIZE)
13111 add_loc_descr (&mem_loc_result,
13112 int_loc_descriptor (DWARF2_ADDR_SIZE
13113 - shift - size));
13114 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13116 if (size != (int) DWARF2_ADDR_SIZE)
13118 add_loc_descr (&mem_loc_result,
13119 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13120 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13123 break;
13125 case IF_THEN_ELSE:
13127 dw_loc_descr_ref op2, bra_node, drop_node;
13128 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13129 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13130 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13131 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13132 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13133 VAR_INIT_STATUS_INITIALIZED);
13134 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13135 VAR_INIT_STATUS_INITIALIZED);
13136 if (op0 == NULL || op1 == NULL || op2 == NULL)
13137 break;
13139 mem_loc_result = op1;
13140 add_loc_descr (&mem_loc_result, op2);
13141 add_loc_descr (&mem_loc_result, op0);
13142 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13143 add_loc_descr (&mem_loc_result, bra_node);
13144 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13145 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13146 add_loc_descr (&mem_loc_result, drop_node);
13147 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13148 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13150 break;
13152 case FLOAT_EXTEND:
13153 case FLOAT_TRUNCATE:
13154 case FLOAT:
13155 case UNSIGNED_FLOAT:
13156 case FIX:
13157 case UNSIGNED_FIX:
13158 if (!dwarf_strict)
13160 dw_die_ref type_die;
13161 dw_loc_descr_ref cvt;
13163 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13164 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13165 if (op0 == NULL)
13166 break;
13167 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13168 && (GET_CODE (rtl) == FLOAT
13169 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13170 <= DWARF2_ADDR_SIZE))
13172 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13173 GET_CODE (rtl) == UNSIGNED_FLOAT);
13174 if (type_die == NULL)
13175 break;
13176 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13177 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13178 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13179 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13180 add_loc_descr (&op0, cvt);
13182 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13183 if (type_die == NULL)
13184 break;
13185 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13186 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13187 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13188 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13189 add_loc_descr (&op0, cvt);
13190 if (GET_MODE_CLASS (mode) == MODE_INT
13191 && (GET_CODE (rtl) == FIX
13192 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13194 op0 = convert_descriptor_to_mode (mode, op0);
13195 if (op0 == NULL)
13196 break;
13198 mem_loc_result = op0;
13200 break;
13202 case CLZ:
13203 case CTZ:
13204 case FFS:
13205 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13206 break;
13208 case POPCOUNT:
13209 case PARITY:
13210 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13211 break;
13213 case BSWAP:
13214 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13215 break;
13217 case ROTATE:
13218 case ROTATERT:
13219 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13220 break;
13222 case COMPARE:
13223 /* In theory, we could implement the above. */
13224 /* DWARF cannot represent the unsigned compare operations
13225 natively. */
13226 case SS_MULT:
13227 case US_MULT:
13228 case SS_DIV:
13229 case US_DIV:
13230 case SS_PLUS:
13231 case US_PLUS:
13232 case SS_MINUS:
13233 case US_MINUS:
13234 case SS_NEG:
13235 case US_NEG:
13236 case SS_ABS:
13237 case SS_ASHIFT:
13238 case US_ASHIFT:
13239 case SS_TRUNCATE:
13240 case US_TRUNCATE:
13241 case UNORDERED:
13242 case ORDERED:
13243 case UNEQ:
13244 case UNGE:
13245 case UNGT:
13246 case UNLE:
13247 case UNLT:
13248 case LTGT:
13249 case FRACT_CONVERT:
13250 case UNSIGNED_FRACT_CONVERT:
13251 case SAT_FRACT:
13252 case UNSIGNED_SAT_FRACT:
13253 case SQRT:
13254 case ASM_OPERANDS:
13255 case VEC_MERGE:
13256 case VEC_SELECT:
13257 case VEC_CONCAT:
13258 case VEC_DUPLICATE:
13259 case UNSPEC:
13260 case HIGH:
13261 case FMA:
13262 case STRICT_LOW_PART:
13263 case CONST_VECTOR:
13264 case CONST_FIXED:
13265 case CLRSB:
13266 case CLOBBER:
13267 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13268 can't express it in the debug info. This can happen e.g. with some
13269 TLS UNSPECs. */
13270 break;
13272 case CONST_STRING:
13273 resolve_one_addr (&rtl);
13274 goto symref;
13276 default:
13277 #ifdef ENABLE_CHECKING
13278 print_rtl (stderr, rtl);
13279 gcc_unreachable ();
13280 #else
13281 break;
13282 #endif
13285 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13286 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13288 return mem_loc_result;
13291 /* Return a descriptor that describes the concatenation of two locations.
13292 This is typically a complex variable. */
13294 static dw_loc_descr_ref
13295 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13297 dw_loc_descr_ref cc_loc_result = NULL;
13298 dw_loc_descr_ref x0_ref
13299 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13300 dw_loc_descr_ref x1_ref
13301 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13303 if (x0_ref == 0 || x1_ref == 0)
13304 return 0;
13306 cc_loc_result = x0_ref;
13307 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13309 add_loc_descr (&cc_loc_result, x1_ref);
13310 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13312 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13313 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13315 return cc_loc_result;
13318 /* Return a descriptor that describes the concatenation of N
13319 locations. */
13321 static dw_loc_descr_ref
13322 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13324 unsigned int i;
13325 dw_loc_descr_ref cc_loc_result = NULL;
13326 unsigned int n = XVECLEN (concatn, 0);
13328 for (i = 0; i < n; ++i)
13330 dw_loc_descr_ref ref;
13331 rtx x = XVECEXP (concatn, 0, i);
13333 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13334 if (ref == NULL)
13335 return NULL;
13337 add_loc_descr (&cc_loc_result, ref);
13338 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13341 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13342 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13344 return cc_loc_result;
13347 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13348 for DEBUG_IMPLICIT_PTR RTL. */
13350 static dw_loc_descr_ref
13351 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13353 dw_loc_descr_ref ret;
13354 dw_die_ref ref;
13356 if (dwarf_strict)
13357 return NULL;
13358 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13359 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13360 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13361 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13362 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13363 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13364 if (ref)
13366 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13367 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13368 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13370 else
13372 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13373 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13375 return ret;
13378 /* Output a proper Dwarf location descriptor for a variable or parameter
13379 which is either allocated in a register or in a memory location. For a
13380 register, we just generate an OP_REG and the register number. For a
13381 memory location we provide a Dwarf postfix expression describing how to
13382 generate the (dynamic) address of the object onto the address stack.
13384 MODE is mode of the decl if this loc_descriptor is going to be used in
13385 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13386 allowed, VOIDmode otherwise.
13388 If we don't know how to describe it, return 0. */
13390 static dw_loc_descr_ref
13391 loc_descriptor (rtx rtl, enum machine_mode mode,
13392 enum var_init_status initialized)
13394 dw_loc_descr_ref loc_result = NULL;
13396 switch (GET_CODE (rtl))
13398 case SUBREG:
13399 /* The case of a subreg may arise when we have a local (register)
13400 variable or a formal (register) parameter which doesn't quite fill
13401 up an entire register. For now, just assume that it is
13402 legitimate to make the Dwarf info refer to the whole register which
13403 contains the given subreg. */
13404 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13405 loc_result = loc_descriptor (SUBREG_REG (rtl),
13406 GET_MODE (SUBREG_REG (rtl)), initialized);
13407 else
13408 goto do_default;
13409 break;
13411 case REG:
13412 loc_result = reg_loc_descriptor (rtl, initialized);
13413 break;
13415 case MEM:
13416 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13417 GET_MODE (rtl), initialized);
13418 if (loc_result == NULL)
13419 loc_result = tls_mem_loc_descriptor (rtl);
13420 if (loc_result == NULL)
13422 rtx new_rtl = avoid_constant_pool_reference (rtl);
13423 if (new_rtl != rtl)
13424 loc_result = loc_descriptor (new_rtl, mode, initialized);
13426 break;
13428 case CONCAT:
13429 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13430 initialized);
13431 break;
13433 case CONCATN:
13434 loc_result = concatn_loc_descriptor (rtl, initialized);
13435 break;
13437 case VAR_LOCATION:
13438 /* Single part. */
13439 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13441 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13442 if (GET_CODE (loc) == EXPR_LIST)
13443 loc = XEXP (loc, 0);
13444 loc_result = loc_descriptor (loc, mode, initialized);
13445 break;
13448 rtl = XEXP (rtl, 1);
13449 /* FALLTHRU */
13451 case PARALLEL:
13453 rtvec par_elems = XVEC (rtl, 0);
13454 int num_elem = GET_NUM_ELEM (par_elems);
13455 enum machine_mode mode;
13456 int i;
13458 /* Create the first one, so we have something to add to. */
13459 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13460 VOIDmode, initialized);
13461 if (loc_result == NULL)
13462 return NULL;
13463 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13464 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13465 for (i = 1; i < num_elem; i++)
13467 dw_loc_descr_ref temp;
13469 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13470 VOIDmode, initialized);
13471 if (temp == NULL)
13472 return NULL;
13473 add_loc_descr (&loc_result, temp);
13474 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13475 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13478 break;
13480 case CONST_INT:
13481 if (mode != VOIDmode && mode != BLKmode)
13482 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13483 INTVAL (rtl));
13484 break;
13486 case CONST_DOUBLE:
13487 if (mode == VOIDmode)
13488 mode = GET_MODE (rtl);
13490 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13492 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13494 /* Note that a CONST_DOUBLE rtx could represent either an integer
13495 or a floating-point constant. A CONST_DOUBLE is used whenever
13496 the constant requires more than one word in order to be
13497 adequately represented. We output CONST_DOUBLEs as blocks. */
13498 loc_result = new_loc_descr (DW_OP_implicit_value,
13499 GET_MODE_SIZE (mode), 0);
13500 #if TARGET_SUPPORTS_WIDE_INT == 0
13501 if (!SCALAR_FLOAT_MODE_P (mode))
13503 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13504 loc_result->dw_loc_oprnd2.v.val_double
13505 = rtx_to_double_int (rtl);
13507 else
13508 #endif
13510 unsigned int length = GET_MODE_SIZE (mode);
13511 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13513 insert_float (rtl, array);
13514 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13515 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13516 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13517 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13520 break;
13522 case CONST_WIDE_INT:
13523 if (mode == VOIDmode)
13524 mode = GET_MODE (rtl);
13526 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13528 loc_result = new_loc_descr (DW_OP_implicit_value,
13529 GET_MODE_SIZE (mode), 0);
13530 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13531 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13532 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13534 break;
13536 case CONST_VECTOR:
13537 if (mode == VOIDmode)
13538 mode = GET_MODE (rtl);
13540 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13542 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13543 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13544 unsigned char *array
13545 = ggc_vec_alloc<unsigned char> (length * elt_size);
13546 unsigned int i;
13547 unsigned char *p;
13548 enum machine_mode imode = GET_MODE_INNER (mode);
13550 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13551 switch (GET_MODE_CLASS (mode))
13553 case MODE_VECTOR_INT:
13554 for (i = 0, p = array; i < length; i++, p += elt_size)
13556 rtx elt = CONST_VECTOR_ELT (rtl, i);
13557 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13559 break;
13561 case MODE_VECTOR_FLOAT:
13562 for (i = 0, p = array; i < length; i++, p += elt_size)
13564 rtx elt = CONST_VECTOR_ELT (rtl, i);
13565 insert_float (elt, p);
13567 break;
13569 default:
13570 gcc_unreachable ();
13573 loc_result = new_loc_descr (DW_OP_implicit_value,
13574 length * elt_size, 0);
13575 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13576 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13577 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13578 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13580 break;
13582 case CONST:
13583 if (mode == VOIDmode
13584 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13585 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13586 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13588 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13589 break;
13591 /* FALLTHROUGH */
13592 case SYMBOL_REF:
13593 if (!const_ok_for_output (rtl))
13594 break;
13595 case LABEL_REF:
13596 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13597 && (dwarf_version >= 4 || !dwarf_strict))
13599 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13600 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13601 vec_safe_push (used_rtx_array, rtl);
13603 break;
13605 case DEBUG_IMPLICIT_PTR:
13606 loc_result = implicit_ptr_descriptor (rtl, 0);
13607 break;
13609 case PLUS:
13610 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13611 && CONST_INT_P (XEXP (rtl, 1)))
13613 loc_result
13614 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13615 break;
13617 /* FALLTHRU */
13618 do_default:
13619 default:
13620 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13621 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13622 && dwarf_version >= 4)
13623 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13625 /* Value expression. */
13626 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13627 if (loc_result)
13628 add_loc_descr (&loc_result,
13629 new_loc_descr (DW_OP_stack_value, 0, 0));
13631 break;
13634 return loc_result;
13637 /* We need to figure out what section we should use as the base for the
13638 address ranges where a given location is valid.
13639 1. If this particular DECL has a section associated with it, use that.
13640 2. If this function has a section associated with it, use that.
13641 3. Otherwise, use the text section.
13642 XXX: If you split a variable across multiple sections, we won't notice. */
13644 static const char *
13645 secname_for_decl (const_tree decl)
13647 const char *secname;
13649 if (VAR_OR_FUNCTION_DECL_P (decl)
13650 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13651 && DECL_SECTION_NAME (decl))
13652 secname = DECL_SECTION_NAME (decl);
13653 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13654 secname = DECL_SECTION_NAME (current_function_decl);
13655 else if (cfun && in_cold_section_p)
13656 secname = crtl->subsections.cold_section_label;
13657 else
13658 secname = text_section_label;
13660 return secname;
13663 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13665 static bool
13666 decl_by_reference_p (tree decl)
13668 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13669 || TREE_CODE (decl) == VAR_DECL)
13670 && DECL_BY_REFERENCE (decl));
13673 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13674 for VARLOC. */
13676 static dw_loc_descr_ref
13677 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13678 enum var_init_status initialized)
13680 int have_address = 0;
13681 dw_loc_descr_ref descr;
13682 enum machine_mode mode;
13684 if (want_address != 2)
13686 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13687 /* Single part. */
13688 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13690 varloc = PAT_VAR_LOCATION_LOC (varloc);
13691 if (GET_CODE (varloc) == EXPR_LIST)
13692 varloc = XEXP (varloc, 0);
13693 mode = GET_MODE (varloc);
13694 if (MEM_P (varloc))
13696 rtx addr = XEXP (varloc, 0);
13697 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13698 mode, initialized);
13699 if (descr)
13700 have_address = 1;
13701 else
13703 rtx x = avoid_constant_pool_reference (varloc);
13704 if (x != varloc)
13705 descr = mem_loc_descriptor (x, mode, VOIDmode,
13706 initialized);
13709 else
13710 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13712 else
13713 return 0;
13715 else
13717 if (GET_CODE (varloc) == VAR_LOCATION)
13718 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13719 else
13720 mode = DECL_MODE (loc);
13721 descr = loc_descriptor (varloc, mode, initialized);
13722 have_address = 1;
13725 if (!descr)
13726 return 0;
13728 if (want_address == 2 && !have_address
13729 && (dwarf_version >= 4 || !dwarf_strict))
13731 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13733 expansion_failed (loc, NULL_RTX,
13734 "DWARF address size mismatch");
13735 return 0;
13737 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13738 have_address = 1;
13740 /* Show if we can't fill the request for an address. */
13741 if (want_address && !have_address)
13743 expansion_failed (loc, NULL_RTX,
13744 "Want address and only have value");
13745 return 0;
13748 /* If we've got an address and don't want one, dereference. */
13749 if (!want_address && have_address)
13751 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13752 enum dwarf_location_atom op;
13754 if (size > DWARF2_ADDR_SIZE || size == -1)
13756 expansion_failed (loc, NULL_RTX,
13757 "DWARF address size mismatch");
13758 return 0;
13760 else if (size == DWARF2_ADDR_SIZE)
13761 op = DW_OP_deref;
13762 else
13763 op = DW_OP_deref_size;
13765 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13768 return descr;
13771 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13772 if it is not possible. */
13774 static dw_loc_descr_ref
13775 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13777 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13778 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13779 else if (dwarf_version >= 3 || !dwarf_strict)
13780 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13781 else
13782 return NULL;
13785 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13786 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13788 static dw_loc_descr_ref
13789 dw_sra_loc_expr (tree decl, rtx loc)
13791 rtx p;
13792 unsigned int padsize = 0;
13793 dw_loc_descr_ref descr, *descr_tail;
13794 unsigned HOST_WIDE_INT decl_size;
13795 rtx varloc;
13796 enum var_init_status initialized;
13798 if (DECL_SIZE (decl) == NULL
13799 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13800 return NULL;
13802 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13803 descr = NULL;
13804 descr_tail = &descr;
13806 for (p = loc; p; p = XEXP (p, 1))
13808 unsigned int bitsize = decl_piece_bitsize (p);
13809 rtx loc_note = *decl_piece_varloc_ptr (p);
13810 dw_loc_descr_ref cur_descr;
13811 dw_loc_descr_ref *tail, last = NULL;
13812 unsigned int opsize = 0;
13814 if (loc_note == NULL_RTX
13815 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13817 padsize += bitsize;
13818 continue;
13820 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13821 varloc = NOTE_VAR_LOCATION (loc_note);
13822 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13823 if (cur_descr == NULL)
13825 padsize += bitsize;
13826 continue;
13829 /* Check that cur_descr either doesn't use
13830 DW_OP_*piece operations, or their sum is equal
13831 to bitsize. Otherwise we can't embed it. */
13832 for (tail = &cur_descr; *tail != NULL;
13833 tail = &(*tail)->dw_loc_next)
13834 if ((*tail)->dw_loc_opc == DW_OP_piece)
13836 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13837 * BITS_PER_UNIT;
13838 last = *tail;
13840 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13842 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13843 last = *tail;
13846 if (last != NULL && opsize != bitsize)
13848 padsize += bitsize;
13849 /* Discard the current piece of the descriptor and release any
13850 addr_table entries it uses. */
13851 remove_loc_list_addr_table_entries (cur_descr);
13852 continue;
13855 /* If there is a hole, add DW_OP_*piece after empty DWARF
13856 expression, which means that those bits are optimized out. */
13857 if (padsize)
13859 if (padsize > decl_size)
13861 remove_loc_list_addr_table_entries (cur_descr);
13862 goto discard_descr;
13864 decl_size -= padsize;
13865 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13866 if (*descr_tail == NULL)
13868 remove_loc_list_addr_table_entries (cur_descr);
13869 goto discard_descr;
13871 descr_tail = &(*descr_tail)->dw_loc_next;
13872 padsize = 0;
13874 *descr_tail = cur_descr;
13875 descr_tail = tail;
13876 if (bitsize > decl_size)
13877 goto discard_descr;
13878 decl_size -= bitsize;
13879 if (last == NULL)
13881 HOST_WIDE_INT offset = 0;
13882 if (GET_CODE (varloc) == VAR_LOCATION
13883 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13885 varloc = PAT_VAR_LOCATION_LOC (varloc);
13886 if (GET_CODE (varloc) == EXPR_LIST)
13887 varloc = XEXP (varloc, 0);
13891 if (GET_CODE (varloc) == CONST
13892 || GET_CODE (varloc) == SIGN_EXTEND
13893 || GET_CODE (varloc) == ZERO_EXTEND)
13894 varloc = XEXP (varloc, 0);
13895 else if (GET_CODE (varloc) == SUBREG)
13896 varloc = SUBREG_REG (varloc);
13897 else
13898 break;
13900 while (1);
13901 /* DW_OP_bit_size offset should be zero for register
13902 or implicit location descriptions and empty location
13903 descriptions, but for memory addresses needs big endian
13904 adjustment. */
13905 if (MEM_P (varloc))
13907 unsigned HOST_WIDE_INT memsize
13908 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13909 if (memsize != bitsize)
13911 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13912 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13913 goto discard_descr;
13914 if (memsize < bitsize)
13915 goto discard_descr;
13916 if (BITS_BIG_ENDIAN)
13917 offset = memsize - bitsize;
13921 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13922 if (*descr_tail == NULL)
13923 goto discard_descr;
13924 descr_tail = &(*descr_tail)->dw_loc_next;
13928 /* If there were any non-empty expressions, add padding till the end of
13929 the decl. */
13930 if (descr != NULL && decl_size != 0)
13932 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13933 if (*descr_tail == NULL)
13934 goto discard_descr;
13936 return descr;
13938 discard_descr:
13939 /* Discard the descriptor and release any addr_table entries it uses. */
13940 remove_loc_list_addr_table_entries (descr);
13941 return NULL;
13944 /* Return the dwarf representation of the location list LOC_LIST of
13945 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13946 function. */
13948 static dw_loc_list_ref
13949 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13951 const char *endname, *secname;
13952 rtx varloc;
13953 enum var_init_status initialized;
13954 struct var_loc_node *node;
13955 dw_loc_descr_ref descr;
13956 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13957 dw_loc_list_ref list = NULL;
13958 dw_loc_list_ref *listp = &list;
13960 /* Now that we know what section we are using for a base,
13961 actually construct the list of locations.
13962 The first location information is what is passed to the
13963 function that creates the location list, and the remaining
13964 locations just get added on to that list.
13965 Note that we only know the start address for a location
13966 (IE location changes), so to build the range, we use
13967 the range [current location start, next location start].
13968 This means we have to special case the last node, and generate
13969 a range of [last location start, end of function label]. */
13971 secname = secname_for_decl (decl);
13973 for (node = loc_list->first; node; node = node->next)
13974 if (GET_CODE (node->loc) == EXPR_LIST
13975 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13977 if (GET_CODE (node->loc) == EXPR_LIST)
13979 /* This requires DW_OP_{,bit_}piece, which is not usable
13980 inside DWARF expressions. */
13981 if (want_address != 2)
13982 continue;
13983 descr = dw_sra_loc_expr (decl, node->loc);
13984 if (descr == NULL)
13985 continue;
13987 else
13989 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13990 varloc = NOTE_VAR_LOCATION (node->loc);
13991 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13993 if (descr)
13995 bool range_across_switch = false;
13996 /* If section switch happens in between node->label
13997 and node->next->label (or end of function) and
13998 we can't emit it as a single entry list,
13999 emit two ranges, first one ending at the end
14000 of first partition and second one starting at the
14001 beginning of second partition. */
14002 if (node == loc_list->last_before_switch
14003 && (node != loc_list->first || loc_list->first->next)
14004 && current_function_decl)
14006 endname = cfun->fde->dw_fde_end;
14007 range_across_switch = true;
14009 /* The variable has a location between NODE->LABEL and
14010 NODE->NEXT->LABEL. */
14011 else if (node->next)
14012 endname = node->next->label;
14013 /* If the variable has a location at the last label
14014 it keeps its location until the end of function. */
14015 else if (!current_function_decl)
14016 endname = text_end_label;
14017 else
14019 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14020 current_function_funcdef_no);
14021 endname = ggc_strdup (label_id);
14024 *listp = new_loc_list (descr, node->label, endname, secname);
14025 if (TREE_CODE (decl) == PARM_DECL
14026 && node == loc_list->first
14027 && NOTE_P (node->loc)
14028 && strcmp (node->label, endname) == 0)
14029 (*listp)->force = true;
14030 listp = &(*listp)->dw_loc_next;
14032 if (range_across_switch)
14034 if (GET_CODE (node->loc) == EXPR_LIST)
14035 descr = dw_sra_loc_expr (decl, node->loc);
14036 else
14038 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14039 varloc = NOTE_VAR_LOCATION (node->loc);
14040 descr = dw_loc_list_1 (decl, varloc, want_address,
14041 initialized);
14043 gcc_assert (descr);
14044 /* The variable has a location between NODE->LABEL and
14045 NODE->NEXT->LABEL. */
14046 if (node->next)
14047 endname = node->next->label;
14048 else
14049 endname = cfun->fde->dw_fde_second_end;
14050 *listp = new_loc_list (descr,
14051 cfun->fde->dw_fde_second_begin,
14052 endname, secname);
14053 listp = &(*listp)->dw_loc_next;
14058 /* Try to avoid the overhead of a location list emitting a location
14059 expression instead, but only if we didn't have more than one
14060 location entry in the first place. If some entries were not
14061 representable, we don't want to pretend a single entry that was
14062 applies to the entire scope in which the variable is
14063 available. */
14064 if (list && loc_list->first->next)
14065 gen_llsym (list);
14067 return list;
14070 /* Return if the loc_list has only single element and thus can be represented
14071 as location description. */
14073 static bool
14074 single_element_loc_list_p (dw_loc_list_ref list)
14076 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14077 return !list->ll_symbol;
14080 /* To each location in list LIST add loc descr REF. */
14082 static void
14083 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14085 dw_loc_descr_ref copy;
14086 add_loc_descr (&list->expr, ref);
14087 list = list->dw_loc_next;
14088 while (list)
14090 copy = ggc_alloc<dw_loc_descr_node> ();
14091 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14092 add_loc_descr (&list->expr, copy);
14093 while (copy->dw_loc_next)
14095 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14096 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14097 copy->dw_loc_next = new_copy;
14098 copy = new_copy;
14100 list = list->dw_loc_next;
14104 /* Given two lists RET and LIST
14105 produce location list that is result of adding expression in LIST
14106 to expression in RET on each position in program.
14107 Might be destructive on both RET and LIST.
14109 TODO: We handle only simple cases of RET or LIST having at most one
14110 element. General case would inolve sorting the lists in program order
14111 and merging them that will need some additional work.
14112 Adding that will improve quality of debug info especially for SRA-ed
14113 structures. */
14115 static void
14116 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14118 if (!list)
14119 return;
14120 if (!*ret)
14122 *ret = list;
14123 return;
14125 if (!list->dw_loc_next)
14127 add_loc_descr_to_each (*ret, list->expr);
14128 return;
14130 if (!(*ret)->dw_loc_next)
14132 add_loc_descr_to_each (list, (*ret)->expr);
14133 *ret = list;
14134 return;
14136 expansion_failed (NULL_TREE, NULL_RTX,
14137 "Don't know how to merge two non-trivial"
14138 " location lists.\n");
14139 *ret = NULL;
14140 return;
14143 /* LOC is constant expression. Try a luck, look it up in constant
14144 pool and return its loc_descr of its address. */
14146 static dw_loc_descr_ref
14147 cst_pool_loc_descr (tree loc)
14149 /* Get an RTL for this, if something has been emitted. */
14150 rtx rtl = lookup_constant_def (loc);
14152 if (!rtl || !MEM_P (rtl))
14154 gcc_assert (!rtl);
14155 return 0;
14157 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14159 /* TODO: We might get more coverage if we was actually delaying expansion
14160 of all expressions till end of compilation when constant pools are fully
14161 populated. */
14162 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14164 expansion_failed (loc, NULL_RTX,
14165 "CST value in contant pool but not marked.");
14166 return 0;
14168 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14169 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14172 /* Return dw_loc_list representing address of addr_expr LOC
14173 by looking for inner INDIRECT_REF expression and turning
14174 it into simple arithmetics. */
14176 static dw_loc_list_ref
14177 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14179 tree obj, offset;
14180 HOST_WIDE_INT bitsize, bitpos, bytepos;
14181 enum machine_mode mode;
14182 int unsignedp, volatilep = 0;
14183 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14185 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14186 &bitsize, &bitpos, &offset, &mode,
14187 &unsignedp, &volatilep, false);
14188 STRIP_NOPS (obj);
14189 if (bitpos % BITS_PER_UNIT)
14191 expansion_failed (loc, NULL_RTX, "bitfield access");
14192 return 0;
14194 if (!INDIRECT_REF_P (obj))
14196 expansion_failed (obj,
14197 NULL_RTX, "no indirect ref in inner refrence");
14198 return 0;
14200 if (!offset && !bitpos)
14201 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14202 else if (toplev
14203 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14204 && (dwarf_version >= 4 || !dwarf_strict))
14206 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14207 if (!list_ret)
14208 return 0;
14209 if (offset)
14211 /* Variable offset. */
14212 list_ret1 = loc_list_from_tree (offset, 0);
14213 if (list_ret1 == 0)
14214 return 0;
14215 add_loc_list (&list_ret, list_ret1);
14216 if (!list_ret)
14217 return 0;
14218 add_loc_descr_to_each (list_ret,
14219 new_loc_descr (DW_OP_plus, 0, 0));
14221 bytepos = bitpos / BITS_PER_UNIT;
14222 if (bytepos > 0)
14223 add_loc_descr_to_each (list_ret,
14224 new_loc_descr (DW_OP_plus_uconst,
14225 bytepos, 0));
14226 else if (bytepos < 0)
14227 loc_list_plus_const (list_ret, bytepos);
14228 add_loc_descr_to_each (list_ret,
14229 new_loc_descr (DW_OP_stack_value, 0, 0));
14231 return list_ret;
14235 /* Generate Dwarf location list representing LOC.
14236 If WANT_ADDRESS is false, expression computing LOC will be computed
14237 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14238 if WANT_ADDRESS is 2, expression computing address useable in location
14239 will be returned (i.e. DW_OP_reg can be used
14240 to refer to register values). */
14242 static dw_loc_list_ref
14243 loc_list_from_tree (tree loc, int want_address)
14245 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14246 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14247 int have_address = 0;
14248 enum dwarf_location_atom op;
14250 /* ??? Most of the time we do not take proper care for sign/zero
14251 extending the values properly. Hopefully this won't be a real
14252 problem... */
14254 switch (TREE_CODE (loc))
14256 case ERROR_MARK:
14257 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14258 return 0;
14260 case PLACEHOLDER_EXPR:
14261 /* This case involves extracting fields from an object to determine the
14262 position of other fields. We don't try to encode this here. The
14263 only user of this is Ada, which encodes the needed information using
14264 the names of types. */
14265 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14266 return 0;
14268 case CALL_EXPR:
14269 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14270 /* There are no opcodes for these operations. */
14271 return 0;
14273 case PREINCREMENT_EXPR:
14274 case PREDECREMENT_EXPR:
14275 case POSTINCREMENT_EXPR:
14276 case POSTDECREMENT_EXPR:
14277 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14278 /* There are no opcodes for these operations. */
14279 return 0;
14281 case ADDR_EXPR:
14282 /* If we already want an address, see if there is INDIRECT_REF inside
14283 e.g. for &this->field. */
14284 if (want_address)
14286 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14287 (loc, want_address == 2);
14288 if (list_ret)
14289 have_address = 1;
14290 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14291 && (ret = cst_pool_loc_descr (loc)))
14292 have_address = 1;
14294 /* Otherwise, process the argument and look for the address. */
14295 if (!list_ret && !ret)
14296 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14297 else
14299 if (want_address)
14300 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14301 return NULL;
14303 break;
14305 case VAR_DECL:
14306 if (DECL_THREAD_LOCAL_P (loc))
14308 rtx rtl;
14309 enum dwarf_location_atom tls_op;
14310 enum dtprel_bool dtprel = dtprel_false;
14312 if (targetm.have_tls)
14314 /* If this is not defined, we have no way to emit the
14315 data. */
14316 if (!targetm.asm_out.output_dwarf_dtprel)
14317 return 0;
14319 /* The way DW_OP_GNU_push_tls_address is specified, we
14320 can only look up addresses of objects in the current
14321 module. We used DW_OP_addr as first op, but that's
14322 wrong, because DW_OP_addr is relocated by the debug
14323 info consumer, while DW_OP_GNU_push_tls_address
14324 operand shouldn't be. */
14325 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14326 return 0;
14327 dtprel = dtprel_true;
14328 tls_op = DW_OP_GNU_push_tls_address;
14330 else
14332 if (!targetm.emutls.debug_form_tls_address
14333 || !(dwarf_version >= 3 || !dwarf_strict))
14334 return 0;
14335 /* We stuffed the control variable into the DECL_VALUE_EXPR
14336 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14337 no longer appear in gimple code. We used the control
14338 variable in specific so that we could pick it up here. */
14339 loc = DECL_VALUE_EXPR (loc);
14340 tls_op = DW_OP_form_tls_address;
14343 rtl = rtl_for_decl_location (loc);
14344 if (rtl == NULL_RTX)
14345 return 0;
14347 if (!MEM_P (rtl))
14348 return 0;
14349 rtl = XEXP (rtl, 0);
14350 if (! CONSTANT_P (rtl))
14351 return 0;
14353 ret = new_addr_loc_descr (rtl, dtprel);
14354 ret1 = new_loc_descr (tls_op, 0, 0);
14355 add_loc_descr (&ret, ret1);
14357 have_address = 1;
14358 break;
14360 /* FALLTHRU */
14362 case PARM_DECL:
14363 case RESULT_DECL:
14364 if (DECL_HAS_VALUE_EXPR_P (loc))
14365 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14366 want_address);
14367 /* FALLTHRU */
14369 case FUNCTION_DECL:
14371 rtx rtl;
14372 var_loc_list *loc_list = lookup_decl_loc (loc);
14374 if (loc_list && loc_list->first)
14376 list_ret = dw_loc_list (loc_list, loc, want_address);
14377 have_address = want_address != 0;
14378 break;
14380 rtl = rtl_for_decl_location (loc);
14381 if (rtl == NULL_RTX)
14383 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14384 return 0;
14386 else if (CONST_INT_P (rtl))
14388 HOST_WIDE_INT val = INTVAL (rtl);
14389 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14390 val &= GET_MODE_MASK (DECL_MODE (loc));
14391 ret = int_loc_descriptor (val);
14393 else if (GET_CODE (rtl) == CONST_STRING)
14395 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14396 return 0;
14398 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14399 ret = new_addr_loc_descr (rtl, dtprel_false);
14400 else
14402 enum machine_mode mode, mem_mode;
14404 /* Certain constructs can only be represented at top-level. */
14405 if (want_address == 2)
14407 ret = loc_descriptor (rtl, VOIDmode,
14408 VAR_INIT_STATUS_INITIALIZED);
14409 have_address = 1;
14411 else
14413 mode = GET_MODE (rtl);
14414 mem_mode = VOIDmode;
14415 if (MEM_P (rtl))
14417 mem_mode = mode;
14418 mode = get_address_mode (rtl);
14419 rtl = XEXP (rtl, 0);
14420 have_address = 1;
14422 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14423 VAR_INIT_STATUS_INITIALIZED);
14425 if (!ret)
14426 expansion_failed (loc, rtl,
14427 "failed to produce loc descriptor for rtl");
14430 break;
14432 case MEM_REF:
14433 /* ??? FIXME. */
14434 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14435 return 0;
14436 /* Fallthru. */
14437 case INDIRECT_REF:
14438 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14439 have_address = 1;
14440 break;
14442 case COMPOUND_EXPR:
14443 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14445 CASE_CONVERT:
14446 case VIEW_CONVERT_EXPR:
14447 case SAVE_EXPR:
14448 case MODIFY_EXPR:
14449 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14451 case COMPONENT_REF:
14452 case BIT_FIELD_REF:
14453 case ARRAY_REF:
14454 case ARRAY_RANGE_REF:
14455 case REALPART_EXPR:
14456 case IMAGPART_EXPR:
14458 tree obj, offset;
14459 HOST_WIDE_INT bitsize, bitpos, bytepos;
14460 enum machine_mode mode;
14461 int unsignedp, volatilep = 0;
14463 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14464 &unsignedp, &volatilep, false);
14466 gcc_assert (obj != loc);
14468 list_ret = loc_list_from_tree (obj,
14469 want_address == 2
14470 && !bitpos && !offset ? 2 : 1);
14471 /* TODO: We can extract value of the small expression via shifting even
14472 for nonzero bitpos. */
14473 if (list_ret == 0)
14474 return 0;
14475 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14477 expansion_failed (loc, NULL_RTX,
14478 "bitfield access");
14479 return 0;
14482 if (offset != NULL_TREE)
14484 /* Variable offset. */
14485 list_ret1 = loc_list_from_tree (offset, 0);
14486 if (list_ret1 == 0)
14487 return 0;
14488 add_loc_list (&list_ret, list_ret1);
14489 if (!list_ret)
14490 return 0;
14491 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14494 bytepos = bitpos / BITS_PER_UNIT;
14495 if (bytepos > 0)
14496 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14497 else if (bytepos < 0)
14498 loc_list_plus_const (list_ret, bytepos);
14500 have_address = 1;
14501 break;
14504 case INTEGER_CST:
14505 if ((want_address || !tree_fits_shwi_p (loc))
14506 && (ret = cst_pool_loc_descr (loc)))
14507 have_address = 1;
14508 else if (want_address == 2
14509 && tree_fits_shwi_p (loc)
14510 && (ret = address_of_int_loc_descriptor
14511 (int_size_in_bytes (TREE_TYPE (loc)),
14512 tree_to_shwi (loc))))
14513 have_address = 1;
14514 else if (tree_fits_shwi_p (loc))
14515 ret = int_loc_descriptor (tree_to_shwi (loc));
14516 else
14518 expansion_failed (loc, NULL_RTX,
14519 "Integer operand is not host integer");
14520 return 0;
14522 break;
14524 case CONSTRUCTOR:
14525 case REAL_CST:
14526 case STRING_CST:
14527 case COMPLEX_CST:
14528 if ((ret = cst_pool_loc_descr (loc)))
14529 have_address = 1;
14530 else
14531 /* We can construct small constants here using int_loc_descriptor. */
14532 expansion_failed (loc, NULL_RTX,
14533 "constructor or constant not in constant pool");
14534 break;
14536 case TRUTH_AND_EXPR:
14537 case TRUTH_ANDIF_EXPR:
14538 case BIT_AND_EXPR:
14539 op = DW_OP_and;
14540 goto do_binop;
14542 case TRUTH_XOR_EXPR:
14543 case BIT_XOR_EXPR:
14544 op = DW_OP_xor;
14545 goto do_binop;
14547 case TRUTH_OR_EXPR:
14548 case TRUTH_ORIF_EXPR:
14549 case BIT_IOR_EXPR:
14550 op = DW_OP_or;
14551 goto do_binop;
14553 case FLOOR_DIV_EXPR:
14554 case CEIL_DIV_EXPR:
14555 case ROUND_DIV_EXPR:
14556 case TRUNC_DIV_EXPR:
14557 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14558 return 0;
14559 op = DW_OP_div;
14560 goto do_binop;
14562 case MINUS_EXPR:
14563 op = DW_OP_minus;
14564 goto do_binop;
14566 case FLOOR_MOD_EXPR:
14567 case CEIL_MOD_EXPR:
14568 case ROUND_MOD_EXPR:
14569 case TRUNC_MOD_EXPR:
14570 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14572 op = DW_OP_mod;
14573 goto do_binop;
14575 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14576 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14577 if (list_ret == 0 || list_ret1 == 0)
14578 return 0;
14580 add_loc_list (&list_ret, list_ret1);
14581 if (list_ret == 0)
14582 return 0;
14583 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14584 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14585 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14586 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14587 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14588 break;
14590 case MULT_EXPR:
14591 op = DW_OP_mul;
14592 goto do_binop;
14594 case LSHIFT_EXPR:
14595 op = DW_OP_shl;
14596 goto do_binop;
14598 case RSHIFT_EXPR:
14599 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14600 goto do_binop;
14602 case POINTER_PLUS_EXPR:
14603 case PLUS_EXPR:
14604 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14606 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14607 if (list_ret == 0)
14608 return 0;
14610 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14611 break;
14614 op = DW_OP_plus;
14615 goto do_binop;
14617 case LE_EXPR:
14618 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14619 return 0;
14621 op = DW_OP_le;
14622 goto do_binop;
14624 case GE_EXPR:
14625 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14626 return 0;
14628 op = DW_OP_ge;
14629 goto do_binop;
14631 case LT_EXPR:
14632 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14633 return 0;
14635 op = DW_OP_lt;
14636 goto do_binop;
14638 case GT_EXPR:
14639 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14640 return 0;
14642 op = DW_OP_gt;
14643 goto do_binop;
14645 case EQ_EXPR:
14646 op = DW_OP_eq;
14647 goto do_binop;
14649 case NE_EXPR:
14650 op = DW_OP_ne;
14651 goto do_binop;
14653 do_binop:
14654 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14655 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14656 if (list_ret == 0 || list_ret1 == 0)
14657 return 0;
14659 add_loc_list (&list_ret, list_ret1);
14660 if (list_ret == 0)
14661 return 0;
14662 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14663 break;
14665 case TRUTH_NOT_EXPR:
14666 case BIT_NOT_EXPR:
14667 op = DW_OP_not;
14668 goto do_unop;
14670 case ABS_EXPR:
14671 op = DW_OP_abs;
14672 goto do_unop;
14674 case NEGATE_EXPR:
14675 op = DW_OP_neg;
14676 goto do_unop;
14678 do_unop:
14679 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14680 if (list_ret == 0)
14681 return 0;
14683 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14684 break;
14686 case MIN_EXPR:
14687 case MAX_EXPR:
14689 const enum tree_code code =
14690 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14692 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14693 build2 (code, integer_type_node,
14694 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14695 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14698 /* ... fall through ... */
14700 case COND_EXPR:
14702 dw_loc_descr_ref lhs
14703 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14704 dw_loc_list_ref rhs
14705 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14706 dw_loc_descr_ref bra_node, jump_node, tmp;
14708 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14709 if (list_ret == 0 || lhs == 0 || rhs == 0)
14710 return 0;
14712 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14713 add_loc_descr_to_each (list_ret, bra_node);
14715 add_loc_list (&list_ret, rhs);
14716 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14717 add_loc_descr_to_each (list_ret, jump_node);
14719 add_loc_descr_to_each (list_ret, lhs);
14720 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14721 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14723 /* ??? Need a node to point the skip at. Use a nop. */
14724 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14725 add_loc_descr_to_each (list_ret, tmp);
14726 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14727 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14729 break;
14731 case FIX_TRUNC_EXPR:
14732 return 0;
14734 default:
14735 /* Leave front-end specific codes as simply unknown. This comes
14736 up, for instance, with the C STMT_EXPR. */
14737 if ((unsigned int) TREE_CODE (loc)
14738 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14740 expansion_failed (loc, NULL_RTX,
14741 "language specific tree node");
14742 return 0;
14745 #ifdef ENABLE_CHECKING
14746 /* Otherwise this is a generic code; we should just lists all of
14747 these explicitly. We forgot one. */
14748 gcc_unreachable ();
14749 #else
14750 /* In a release build, we want to degrade gracefully: better to
14751 generate incomplete debugging information than to crash. */
14752 return NULL;
14753 #endif
14756 if (!ret && !list_ret)
14757 return 0;
14759 if (want_address == 2 && !have_address
14760 && (dwarf_version >= 4 || !dwarf_strict))
14762 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14764 expansion_failed (loc, NULL_RTX,
14765 "DWARF address size mismatch");
14766 return 0;
14768 if (ret)
14769 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14770 else
14771 add_loc_descr_to_each (list_ret,
14772 new_loc_descr (DW_OP_stack_value, 0, 0));
14773 have_address = 1;
14775 /* Show if we can't fill the request for an address. */
14776 if (want_address && !have_address)
14778 expansion_failed (loc, NULL_RTX,
14779 "Want address and only have value");
14780 return 0;
14783 gcc_assert (!ret || !list_ret);
14785 /* If we've got an address and don't want one, dereference. */
14786 if (!want_address && have_address)
14788 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14790 if (size > DWARF2_ADDR_SIZE || size == -1)
14792 expansion_failed (loc, NULL_RTX,
14793 "DWARF address size mismatch");
14794 return 0;
14796 else if (size == DWARF2_ADDR_SIZE)
14797 op = DW_OP_deref;
14798 else
14799 op = DW_OP_deref_size;
14801 if (ret)
14802 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14803 else
14804 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14806 if (ret)
14807 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14809 return list_ret;
14812 /* Same as above but return only single location expression. */
14813 static dw_loc_descr_ref
14814 loc_descriptor_from_tree (tree loc, int want_address)
14816 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14817 if (!ret)
14818 return NULL;
14819 if (ret->dw_loc_next)
14821 expansion_failed (loc, NULL_RTX,
14822 "Location list where only loc descriptor needed");
14823 return NULL;
14825 return ret->expr;
14828 /* Given a value, round it up to the lowest multiple of `boundary'
14829 which is not less than the value itself. */
14831 static inline HOST_WIDE_INT
14832 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14834 return (((value + boundary - 1) / boundary) * boundary);
14837 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14838 pointer to the declared type for the relevant field variable, or return
14839 `integer_type_node' if the given node turns out to be an
14840 ERROR_MARK node. */
14842 static inline tree
14843 field_type (const_tree decl)
14845 tree type;
14847 if (TREE_CODE (decl) == ERROR_MARK)
14848 return integer_type_node;
14850 type = DECL_BIT_FIELD_TYPE (decl);
14851 if (type == NULL_TREE)
14852 type = TREE_TYPE (decl);
14854 return type;
14857 /* Given a pointer to a tree node, return the alignment in bits for
14858 it, or else return BITS_PER_WORD if the node actually turns out to
14859 be an ERROR_MARK node. */
14861 static inline unsigned
14862 simple_type_align_in_bits (const_tree type)
14864 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14867 static inline unsigned
14868 simple_decl_align_in_bits (const_tree decl)
14870 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14873 /* Return the result of rounding T up to ALIGN. */
14875 static inline offset_int
14876 round_up_to_align (const offset_int &t, unsigned int align)
14878 return wi::udiv_trunc (t + align - 1, align) * align;
14881 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14882 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14883 or return 0 if we are unable to determine what that offset is, either
14884 because the argument turns out to be a pointer to an ERROR_MARK node, or
14885 because the offset is actually variable. (We can't handle the latter case
14886 just yet). */
14888 static HOST_WIDE_INT
14889 field_byte_offset (const_tree decl)
14891 offset_int object_offset_in_bits;
14892 offset_int object_offset_in_bytes;
14893 offset_int bitpos_int;
14895 if (TREE_CODE (decl) == ERROR_MARK)
14896 return 0;
14898 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14900 /* We cannot yet cope with fields whose positions are variable, so
14901 for now, when we see such things, we simply return 0. Someday, we may
14902 be able to handle such cases, but it will be damn difficult. */
14903 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14904 return 0;
14906 bitpos_int = wi::to_offset (bit_position (decl));
14908 #ifdef PCC_BITFIELD_TYPE_MATTERS
14909 if (PCC_BITFIELD_TYPE_MATTERS)
14911 tree type;
14912 tree field_size_tree;
14913 offset_int deepest_bitpos;
14914 offset_int field_size_in_bits;
14915 unsigned int type_align_in_bits;
14916 unsigned int decl_align_in_bits;
14917 offset_int type_size_in_bits;
14919 type = field_type (decl);
14920 type_size_in_bits = offset_int_type_size_in_bits (type);
14921 type_align_in_bits = simple_type_align_in_bits (type);
14923 field_size_tree = DECL_SIZE (decl);
14925 /* The size could be unspecified if there was an error, or for
14926 a flexible array member. */
14927 if (!field_size_tree)
14928 field_size_tree = bitsize_zero_node;
14930 /* If the size of the field is not constant, use the type size. */
14931 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14932 field_size_in_bits = wi::to_offset (field_size_tree);
14933 else
14934 field_size_in_bits = type_size_in_bits;
14936 decl_align_in_bits = simple_decl_align_in_bits (decl);
14938 /* The GCC front-end doesn't make any attempt to keep track of the
14939 starting bit offset (relative to the start of the containing
14940 structure type) of the hypothetical "containing object" for a
14941 bit-field. Thus, when computing the byte offset value for the
14942 start of the "containing object" of a bit-field, we must deduce
14943 this information on our own. This can be rather tricky to do in
14944 some cases. For example, handling the following structure type
14945 definition when compiling for an i386/i486 target (which only
14946 aligns long long's to 32-bit boundaries) can be very tricky:
14948 struct S { int field1; long long field2:31; };
14950 Fortunately, there is a simple rule-of-thumb which can be used
14951 in such cases. When compiling for an i386/i486, GCC will
14952 allocate 8 bytes for the structure shown above. It decides to
14953 do this based upon one simple rule for bit-field allocation.
14954 GCC allocates each "containing object" for each bit-field at
14955 the first (i.e. lowest addressed) legitimate alignment boundary
14956 (based upon the required minimum alignment for the declared
14957 type of the field) which it can possibly use, subject to the
14958 condition that there is still enough available space remaining
14959 in the containing object (when allocated at the selected point)
14960 to fully accommodate all of the bits of the bit-field itself.
14962 This simple rule makes it obvious why GCC allocates 8 bytes for
14963 each object of the structure type shown above. When looking
14964 for a place to allocate the "containing object" for `field2',
14965 the compiler simply tries to allocate a 64-bit "containing
14966 object" at each successive 32-bit boundary (starting at zero)
14967 until it finds a place to allocate that 64- bit field such that
14968 at least 31 contiguous (and previously unallocated) bits remain
14969 within that selected 64 bit field. (As it turns out, for the
14970 example above, the compiler finds it is OK to allocate the
14971 "containing object" 64-bit field at bit-offset zero within the
14972 structure type.)
14974 Here we attempt to work backwards from the limited set of facts
14975 we're given, and we try to deduce from those facts, where GCC
14976 must have believed that the containing object started (within
14977 the structure type). The value we deduce is then used (by the
14978 callers of this routine) to generate DW_AT_location and
14979 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14980 the case of DW_AT_location, regular fields as well). */
14982 /* Figure out the bit-distance from the start of the structure to
14983 the "deepest" bit of the bit-field. */
14984 deepest_bitpos = bitpos_int + field_size_in_bits;
14986 /* This is the tricky part. Use some fancy footwork to deduce
14987 where the lowest addressed bit of the containing object must
14988 be. */
14989 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14991 /* Round up to type_align by default. This works best for
14992 bitfields. */
14993 object_offset_in_bits
14994 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14996 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14998 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15000 /* Round up to decl_align instead. */
15001 object_offset_in_bits
15002 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15005 else
15006 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15007 object_offset_in_bits = bitpos_int;
15009 object_offset_in_bytes
15010 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15011 return object_offset_in_bytes.to_shwi ();
15014 /* The following routines define various Dwarf attributes and any data
15015 associated with them. */
15017 /* Add a location description attribute value to a DIE.
15019 This emits location attributes suitable for whole variables and
15020 whole parameters. Note that the location attributes for struct fields are
15021 generated by the routine `data_member_location_attribute' below. */
15023 static inline void
15024 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15025 dw_loc_list_ref descr)
15027 if (descr == 0)
15028 return;
15029 if (single_element_loc_list_p (descr))
15030 add_AT_loc (die, attr_kind, descr->expr);
15031 else
15032 add_AT_loc_list (die, attr_kind, descr);
15035 /* Add DW_AT_accessibility attribute to DIE if needed. */
15037 static void
15038 add_accessibility_attribute (dw_die_ref die, tree decl)
15040 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15041 children, otherwise the default is DW_ACCESS_public. In DWARF2
15042 the default has always been DW_ACCESS_public. */
15043 if (TREE_PROTECTED (decl))
15044 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15045 else if (TREE_PRIVATE (decl))
15047 if (dwarf_version == 2
15048 || die->die_parent == NULL
15049 || die->die_parent->die_tag != DW_TAG_class_type)
15050 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15052 else if (dwarf_version > 2
15053 && die->die_parent
15054 && die->die_parent->die_tag == DW_TAG_class_type)
15055 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15058 /* Attach the specialized form of location attribute used for data members of
15059 struct and union types. In the special case of a FIELD_DECL node which
15060 represents a bit-field, the "offset" part of this special location
15061 descriptor must indicate the distance in bytes from the lowest-addressed
15062 byte of the containing struct or union type to the lowest-addressed byte of
15063 the "containing object" for the bit-field. (See the `field_byte_offset'
15064 function above).
15066 For any given bit-field, the "containing object" is a hypothetical object
15067 (of some integral or enum type) within which the given bit-field lives. The
15068 type of this hypothetical "containing object" is always the same as the
15069 declared type of the individual bit-field itself (for GCC anyway... the
15070 DWARF spec doesn't actually mandate this). Note that it is the size (in
15071 bytes) of the hypothetical "containing object" which will be given in the
15072 DW_AT_byte_size attribute for this bit-field. (See the
15073 `byte_size_attribute' function below.) It is also used when calculating the
15074 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15075 function below.) */
15077 static void
15078 add_data_member_location_attribute (dw_die_ref die, tree decl)
15080 HOST_WIDE_INT offset;
15081 dw_loc_descr_ref loc_descr = 0;
15083 if (TREE_CODE (decl) == TREE_BINFO)
15085 /* We're working on the TAG_inheritance for a base class. */
15086 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15088 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15089 aren't at a fixed offset from all (sub)objects of the same
15090 type. We need to extract the appropriate offset from our
15091 vtable. The following dwarf expression means
15093 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15095 This is specific to the V3 ABI, of course. */
15097 dw_loc_descr_ref tmp;
15099 /* Make a copy of the object address. */
15100 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15101 add_loc_descr (&loc_descr, tmp);
15103 /* Extract the vtable address. */
15104 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15105 add_loc_descr (&loc_descr, tmp);
15107 /* Calculate the address of the offset. */
15108 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15109 gcc_assert (offset < 0);
15111 tmp = int_loc_descriptor (-offset);
15112 add_loc_descr (&loc_descr, tmp);
15113 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15114 add_loc_descr (&loc_descr, tmp);
15116 /* Extract the offset. */
15117 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15118 add_loc_descr (&loc_descr, tmp);
15120 /* Add it to the object address. */
15121 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15122 add_loc_descr (&loc_descr, tmp);
15124 else
15125 offset = tree_to_shwi (BINFO_OFFSET (decl));
15127 else
15128 offset = field_byte_offset (decl);
15130 if (! loc_descr)
15132 if (dwarf_version > 2)
15134 /* Don't need to output a location expression, just the constant. */
15135 if (offset < 0)
15136 add_AT_int (die, DW_AT_data_member_location, offset);
15137 else
15138 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15139 return;
15141 else
15143 enum dwarf_location_atom op;
15145 /* The DWARF2 standard says that we should assume that the structure
15146 address is already on the stack, so we can specify a structure
15147 field address by using DW_OP_plus_uconst. */
15148 op = DW_OP_plus_uconst;
15149 loc_descr = new_loc_descr (op, offset, 0);
15153 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15156 /* Writes integer values to dw_vec_const array. */
15158 static void
15159 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15161 while (size != 0)
15163 *dest++ = val & 0xff;
15164 val >>= 8;
15165 --size;
15169 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15171 static HOST_WIDE_INT
15172 extract_int (const unsigned char *src, unsigned int size)
15174 HOST_WIDE_INT val = 0;
15176 src += size;
15177 while (size != 0)
15179 val <<= 8;
15180 val |= *--src & 0xff;
15181 --size;
15183 return val;
15186 /* Writes wide_int values to dw_vec_const array. */
15188 static void
15189 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15191 int i;
15193 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15195 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15196 return;
15199 /* We'd have to extend this code to support odd sizes. */
15200 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15202 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15204 if (WORDS_BIG_ENDIAN)
15205 for (i = n - 1; i >= 0; i--)
15207 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15208 dest += sizeof (HOST_WIDE_INT);
15210 else
15211 for (i = 0; i < n; i++)
15213 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15214 dest += sizeof (HOST_WIDE_INT);
15218 /* Writes floating point values to dw_vec_const array. */
15220 static void
15221 insert_float (const_rtx rtl, unsigned char *array)
15223 REAL_VALUE_TYPE rv;
15224 long val[4];
15225 int i;
15227 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15228 real_to_target (val, &rv, GET_MODE (rtl));
15230 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15231 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15233 insert_int (val[i], 4, array);
15234 array += 4;
15238 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15239 does not have a "location" either in memory or in a register. These
15240 things can arise in GNU C when a constant is passed as an actual parameter
15241 to an inlined function. They can also arise in C++ where declared
15242 constants do not necessarily get memory "homes". */
15244 static bool
15245 add_const_value_attribute (dw_die_ref die, rtx rtl)
15247 switch (GET_CODE (rtl))
15249 case CONST_INT:
15251 HOST_WIDE_INT val = INTVAL (rtl);
15253 if (val < 0)
15254 add_AT_int (die, DW_AT_const_value, val);
15255 else
15256 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15258 return true;
15260 case CONST_WIDE_INT:
15261 add_AT_wide (die, DW_AT_const_value,
15262 std::make_pair (rtl, GET_MODE (rtl)));
15263 return true;
15265 case CONST_DOUBLE:
15266 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15267 floating-point constant. A CONST_DOUBLE is used whenever the
15268 constant requires more than one word in order to be adequately
15269 represented. */
15271 enum machine_mode mode = GET_MODE (rtl);
15273 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15274 add_AT_double (die, DW_AT_const_value,
15275 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15276 else
15278 unsigned int length = GET_MODE_SIZE (mode);
15279 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15281 insert_float (rtl, array);
15282 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15285 return true;
15287 case CONST_VECTOR:
15289 enum machine_mode mode = GET_MODE (rtl);
15290 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15291 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15292 unsigned char *array
15293 = ggc_vec_alloc<unsigned char> (length * elt_size);
15294 unsigned int i;
15295 unsigned char *p;
15296 enum machine_mode imode = GET_MODE_INNER (mode);
15298 switch (GET_MODE_CLASS (mode))
15300 case MODE_VECTOR_INT:
15301 for (i = 0, p = array; i < length; i++, p += elt_size)
15303 rtx elt = CONST_VECTOR_ELT (rtl, i);
15304 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15306 break;
15308 case MODE_VECTOR_FLOAT:
15309 for (i = 0, p = array; i < length; i++, p += elt_size)
15311 rtx elt = CONST_VECTOR_ELT (rtl, i);
15312 insert_float (elt, p);
15314 break;
15316 default:
15317 gcc_unreachable ();
15320 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15322 return true;
15324 case CONST_STRING:
15325 if (dwarf_version >= 4 || !dwarf_strict)
15327 dw_loc_descr_ref loc_result;
15328 resolve_one_addr (&rtl);
15329 rtl_addr:
15330 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15331 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15332 add_AT_loc (die, DW_AT_location, loc_result);
15333 vec_safe_push (used_rtx_array, rtl);
15334 return true;
15336 return false;
15338 case CONST:
15339 if (CONSTANT_P (XEXP (rtl, 0)))
15340 return add_const_value_attribute (die, XEXP (rtl, 0));
15341 /* FALLTHROUGH */
15342 case SYMBOL_REF:
15343 if (!const_ok_for_output (rtl))
15344 return false;
15345 case LABEL_REF:
15346 if (dwarf_version >= 4 || !dwarf_strict)
15347 goto rtl_addr;
15348 return false;
15350 case PLUS:
15351 /* In cases where an inlined instance of an inline function is passed
15352 the address of an `auto' variable (which is local to the caller) we
15353 can get a situation where the DECL_RTL of the artificial local
15354 variable (for the inlining) which acts as a stand-in for the
15355 corresponding formal parameter (of the inline function) will look
15356 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15357 exactly a compile-time constant expression, but it isn't the address
15358 of the (artificial) local variable either. Rather, it represents the
15359 *value* which the artificial local variable always has during its
15360 lifetime. We currently have no way to represent such quasi-constant
15361 values in Dwarf, so for now we just punt and generate nothing. */
15362 return false;
15364 case HIGH:
15365 case CONST_FIXED:
15366 return false;
15368 case MEM:
15369 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15370 && MEM_READONLY_P (rtl)
15371 && GET_MODE (rtl) == BLKmode)
15373 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15374 return true;
15376 return false;
15378 default:
15379 /* No other kinds of rtx should be possible here. */
15380 gcc_unreachable ();
15382 return false;
15385 /* Determine whether the evaluation of EXPR references any variables
15386 or functions which aren't otherwise used (and therefore may not be
15387 output). */
15388 static tree
15389 reference_to_unused (tree * tp, int * walk_subtrees,
15390 void * data ATTRIBUTE_UNUSED)
15392 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15393 *walk_subtrees = 0;
15395 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15396 && ! TREE_ASM_WRITTEN (*tp))
15397 return *tp;
15398 /* ??? The C++ FE emits debug information for using decls, so
15399 putting gcc_unreachable here falls over. See PR31899. For now
15400 be conservative. */
15401 else if (!symtab->global_info_ready
15402 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15403 return *tp;
15404 else if (TREE_CODE (*tp) == VAR_DECL)
15406 varpool_node *node = varpool_node::get (*tp);
15407 if (!node || !node->definition)
15408 return *tp;
15410 else if (TREE_CODE (*tp) == FUNCTION_DECL
15411 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15413 /* The call graph machinery must have finished analyzing,
15414 optimizing and gimplifying the CU by now.
15415 So if *TP has no call graph node associated
15416 to it, it means *TP will not be emitted. */
15417 if (!cgraph_node::get (*tp))
15418 return *tp;
15420 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15421 return *tp;
15423 return NULL_TREE;
15426 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15427 for use in a later add_const_value_attribute call. */
15429 static rtx
15430 rtl_for_decl_init (tree init, tree type)
15432 rtx rtl = NULL_RTX;
15434 STRIP_NOPS (init);
15436 /* If a variable is initialized with a string constant without embedded
15437 zeros, build CONST_STRING. */
15438 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15440 tree enttype = TREE_TYPE (type);
15441 tree domain = TYPE_DOMAIN (type);
15442 enum machine_mode mode = TYPE_MODE (enttype);
15444 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15445 && domain
15446 && integer_zerop (TYPE_MIN_VALUE (domain))
15447 && compare_tree_int (TYPE_MAX_VALUE (domain),
15448 TREE_STRING_LENGTH (init) - 1) == 0
15449 && ((size_t) TREE_STRING_LENGTH (init)
15450 == strlen (TREE_STRING_POINTER (init)) + 1))
15452 rtl = gen_rtx_CONST_STRING (VOIDmode,
15453 ggc_strdup (TREE_STRING_POINTER (init)));
15454 rtl = gen_rtx_MEM (BLKmode, rtl);
15455 MEM_READONLY_P (rtl) = 1;
15458 /* Other aggregates, and complex values, could be represented using
15459 CONCAT: FIXME! */
15460 else if (AGGREGATE_TYPE_P (type)
15461 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15462 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15463 || TREE_CODE (type) == COMPLEX_TYPE)
15465 /* Vectors only work if their mode is supported by the target.
15466 FIXME: generic vectors ought to work too. */
15467 else if (TREE_CODE (type) == VECTOR_TYPE
15468 && !VECTOR_MODE_P (TYPE_MODE (type)))
15470 /* If the initializer is something that we know will expand into an
15471 immediate RTL constant, expand it now. We must be careful not to
15472 reference variables which won't be output. */
15473 else if (initializer_constant_valid_p (init, type)
15474 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15476 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15477 possible. */
15478 if (TREE_CODE (type) == VECTOR_TYPE)
15479 switch (TREE_CODE (init))
15481 case VECTOR_CST:
15482 break;
15483 case CONSTRUCTOR:
15484 if (TREE_CONSTANT (init))
15486 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15487 bool constant_p = true;
15488 tree value;
15489 unsigned HOST_WIDE_INT ix;
15491 /* Even when ctor is constant, it might contain non-*_CST
15492 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15493 belong into VECTOR_CST nodes. */
15494 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15495 if (!CONSTANT_CLASS_P (value))
15497 constant_p = false;
15498 break;
15501 if (constant_p)
15503 init = build_vector_from_ctor (type, elts);
15504 break;
15507 /* FALLTHRU */
15509 default:
15510 return NULL;
15513 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15515 /* If expand_expr returns a MEM, it wasn't immediate. */
15516 gcc_assert (!rtl || !MEM_P (rtl));
15519 return rtl;
15522 /* Generate RTL for the variable DECL to represent its location. */
15524 static rtx
15525 rtl_for_decl_location (tree decl)
15527 rtx rtl;
15529 /* Here we have to decide where we are going to say the parameter "lives"
15530 (as far as the debugger is concerned). We only have a couple of
15531 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15533 DECL_RTL normally indicates where the parameter lives during most of the
15534 activation of the function. If optimization is enabled however, this
15535 could be either NULL or else a pseudo-reg. Both of those cases indicate
15536 that the parameter doesn't really live anywhere (as far as the code
15537 generation parts of GCC are concerned) during most of the function's
15538 activation. That will happen (for example) if the parameter is never
15539 referenced within the function.
15541 We could just generate a location descriptor here for all non-NULL
15542 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15543 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15544 where DECL_RTL is NULL or is a pseudo-reg.
15546 Note however that we can only get away with using DECL_INCOMING_RTL as
15547 a backup substitute for DECL_RTL in certain limited cases. In cases
15548 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15549 we can be sure that the parameter was passed using the same type as it is
15550 declared to have within the function, and that its DECL_INCOMING_RTL
15551 points us to a place where a value of that type is passed.
15553 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15554 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15555 because in these cases DECL_INCOMING_RTL points us to a value of some
15556 type which is *different* from the type of the parameter itself. Thus,
15557 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15558 such cases, the debugger would end up (for example) trying to fetch a
15559 `float' from a place which actually contains the first part of a
15560 `double'. That would lead to really incorrect and confusing
15561 output at debug-time.
15563 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15564 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15565 are a couple of exceptions however. On little-endian machines we can
15566 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15567 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15568 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15569 when (on a little-endian machine) a non-prototyped function has a
15570 parameter declared to be of type `short' or `char'. In such cases,
15571 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15572 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15573 passed `int' value. If the debugger then uses that address to fetch
15574 a `short' or a `char' (on a little-endian machine) the result will be
15575 the correct data, so we allow for such exceptional cases below.
15577 Note that our goal here is to describe the place where the given formal
15578 parameter lives during most of the function's activation (i.e. between the
15579 end of the prologue and the start of the epilogue). We'll do that as best
15580 as we can. Note however that if the given formal parameter is modified
15581 sometime during the execution of the function, then a stack backtrace (at
15582 debug-time) will show the function as having been called with the *new*
15583 value rather than the value which was originally passed in. This happens
15584 rarely enough that it is not a major problem, but it *is* a problem, and
15585 I'd like to fix it.
15587 A future version of dwarf2out.c may generate two additional attributes for
15588 any given DW_TAG_formal_parameter DIE which will describe the "passed
15589 type" and the "passed location" for the given formal parameter in addition
15590 to the attributes we now generate to indicate the "declared type" and the
15591 "active location" for each parameter. This additional set of attributes
15592 could be used by debuggers for stack backtraces. Separately, note that
15593 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15594 This happens (for example) for inlined-instances of inline function formal
15595 parameters which are never referenced. This really shouldn't be
15596 happening. All PARM_DECL nodes should get valid non-NULL
15597 DECL_INCOMING_RTL values. FIXME. */
15599 /* Use DECL_RTL as the "location" unless we find something better. */
15600 rtl = DECL_RTL_IF_SET (decl);
15602 /* When generating abstract instances, ignore everything except
15603 constants, symbols living in memory, and symbols living in
15604 fixed registers. */
15605 if (! reload_completed)
15607 if (rtl
15608 && (CONSTANT_P (rtl)
15609 || (MEM_P (rtl)
15610 && CONSTANT_P (XEXP (rtl, 0)))
15611 || (REG_P (rtl)
15612 && TREE_CODE (decl) == VAR_DECL
15613 && TREE_STATIC (decl))))
15615 rtl = targetm.delegitimize_address (rtl);
15616 return rtl;
15618 rtl = NULL_RTX;
15620 else if (TREE_CODE (decl) == PARM_DECL)
15622 if (rtl == NULL_RTX
15623 || is_pseudo_reg (rtl)
15624 || (MEM_P (rtl)
15625 && is_pseudo_reg (XEXP (rtl, 0))
15626 && DECL_INCOMING_RTL (decl)
15627 && MEM_P (DECL_INCOMING_RTL (decl))
15628 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15630 tree declared_type = TREE_TYPE (decl);
15631 tree passed_type = DECL_ARG_TYPE (decl);
15632 enum machine_mode dmode = TYPE_MODE (declared_type);
15633 enum machine_mode pmode = TYPE_MODE (passed_type);
15635 /* This decl represents a formal parameter which was optimized out.
15636 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15637 all cases where (rtl == NULL_RTX) just below. */
15638 if (dmode == pmode)
15639 rtl = DECL_INCOMING_RTL (decl);
15640 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15641 && SCALAR_INT_MODE_P (dmode)
15642 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15643 && DECL_INCOMING_RTL (decl))
15645 rtx inc = DECL_INCOMING_RTL (decl);
15646 if (REG_P (inc))
15647 rtl = inc;
15648 else if (MEM_P (inc))
15650 if (BYTES_BIG_ENDIAN)
15651 rtl = adjust_address_nv (inc, dmode,
15652 GET_MODE_SIZE (pmode)
15653 - GET_MODE_SIZE (dmode));
15654 else
15655 rtl = inc;
15660 /* If the parm was passed in registers, but lives on the stack, then
15661 make a big endian correction if the mode of the type of the
15662 parameter is not the same as the mode of the rtl. */
15663 /* ??? This is the same series of checks that are made in dbxout.c before
15664 we reach the big endian correction code there. It isn't clear if all
15665 of these checks are necessary here, but keeping them all is the safe
15666 thing to do. */
15667 else if (MEM_P (rtl)
15668 && XEXP (rtl, 0) != const0_rtx
15669 && ! CONSTANT_P (XEXP (rtl, 0))
15670 /* Not passed in memory. */
15671 && !MEM_P (DECL_INCOMING_RTL (decl))
15672 /* Not passed by invisible reference. */
15673 && (!REG_P (XEXP (rtl, 0))
15674 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15675 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15676 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15677 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15678 #endif
15680 /* Big endian correction check. */
15681 && BYTES_BIG_ENDIAN
15682 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15683 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15684 < UNITS_PER_WORD))
15686 enum machine_mode addr_mode = get_address_mode (rtl);
15687 int offset = (UNITS_PER_WORD
15688 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15690 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15691 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15694 else if (TREE_CODE (decl) == VAR_DECL
15695 && rtl
15696 && MEM_P (rtl)
15697 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15698 && BYTES_BIG_ENDIAN)
15700 enum machine_mode addr_mode = get_address_mode (rtl);
15701 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15702 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15704 /* If a variable is declared "register" yet is smaller than
15705 a register, then if we store the variable to memory, it
15706 looks like we're storing a register-sized value, when in
15707 fact we are not. We need to adjust the offset of the
15708 storage location to reflect the actual value's bytes,
15709 else gdb will not be able to display it. */
15710 if (rsize > dsize)
15711 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15712 plus_constant (addr_mode, XEXP (rtl, 0),
15713 rsize - dsize));
15716 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15717 and will have been substituted directly into all expressions that use it.
15718 C does not have such a concept, but C++ and other languages do. */
15719 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15720 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15722 if (rtl)
15723 rtl = targetm.delegitimize_address (rtl);
15725 /* If we don't look past the constant pool, we risk emitting a
15726 reference to a constant pool entry that isn't referenced from
15727 code, and thus is not emitted. */
15728 if (rtl)
15729 rtl = avoid_constant_pool_reference (rtl);
15731 /* Try harder to get a rtl. If this symbol ends up not being emitted
15732 in the current CU, resolve_addr will remove the expression referencing
15733 it. */
15734 if (rtl == NULL_RTX
15735 && TREE_CODE (decl) == VAR_DECL
15736 && !DECL_EXTERNAL (decl)
15737 && TREE_STATIC (decl)
15738 && DECL_NAME (decl)
15739 && !DECL_HARD_REGISTER (decl)
15740 && DECL_MODE (decl) != VOIDmode)
15742 rtl = make_decl_rtl_for_debug (decl);
15743 if (!MEM_P (rtl)
15744 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15745 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15746 rtl = NULL_RTX;
15749 return rtl;
15752 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15753 returned. If so, the decl for the COMMON block is returned, and the
15754 value is the offset into the common block for the symbol. */
15756 static tree
15757 fortran_common (tree decl, HOST_WIDE_INT *value)
15759 tree val_expr, cvar;
15760 enum machine_mode mode;
15761 HOST_WIDE_INT bitsize, bitpos;
15762 tree offset;
15763 int unsignedp, volatilep = 0;
15765 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15766 it does not have a value (the offset into the common area), or if it
15767 is thread local (as opposed to global) then it isn't common, and shouldn't
15768 be handled as such. */
15769 if (TREE_CODE (decl) != VAR_DECL
15770 || !TREE_STATIC (decl)
15771 || !DECL_HAS_VALUE_EXPR_P (decl)
15772 || !is_fortran ())
15773 return NULL_TREE;
15775 val_expr = DECL_VALUE_EXPR (decl);
15776 if (TREE_CODE (val_expr) != COMPONENT_REF)
15777 return NULL_TREE;
15779 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15780 &mode, &unsignedp, &volatilep, true);
15782 if (cvar == NULL_TREE
15783 || TREE_CODE (cvar) != VAR_DECL
15784 || DECL_ARTIFICIAL (cvar)
15785 || !TREE_PUBLIC (cvar))
15786 return NULL_TREE;
15788 *value = 0;
15789 if (offset != NULL)
15791 if (!tree_fits_shwi_p (offset))
15792 return NULL_TREE;
15793 *value = tree_to_shwi (offset);
15795 if (bitpos != 0)
15796 *value += bitpos / BITS_PER_UNIT;
15798 return cvar;
15801 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15802 data attribute for a variable or a parameter. We generate the
15803 DW_AT_const_value attribute only in those cases where the given variable
15804 or parameter does not have a true "location" either in memory or in a
15805 register. This can happen (for example) when a constant is passed as an
15806 actual argument in a call to an inline function. (It's possible that
15807 these things can crop up in other ways also.) Note that one type of
15808 constant value which can be passed into an inlined function is a constant
15809 pointer. This can happen for example if an actual argument in an inlined
15810 function call evaluates to a compile-time constant address.
15812 CACHE_P is true if it is worth caching the location list for DECL,
15813 so that future calls can reuse it rather than regenerate it from scratch.
15814 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15815 since we will need to refer to them each time the function is inlined. */
15817 static bool
15818 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15819 enum dwarf_attribute attr)
15821 rtx rtl;
15822 dw_loc_list_ref list;
15823 var_loc_list *loc_list;
15824 cached_dw_loc_list *cache;
15825 void **slot;
15827 if (TREE_CODE (decl) == ERROR_MARK)
15828 return false;
15830 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15831 || TREE_CODE (decl) == RESULT_DECL);
15833 /* Try to get some constant RTL for this decl, and use that as the value of
15834 the location. */
15836 rtl = rtl_for_decl_location (decl);
15837 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15838 && add_const_value_attribute (die, rtl))
15839 return true;
15841 /* See if we have single element location list that is equivalent to
15842 a constant value. That way we are better to use add_const_value_attribute
15843 rather than expanding constant value equivalent. */
15844 loc_list = lookup_decl_loc (decl);
15845 if (loc_list
15846 && loc_list->first
15847 && loc_list->first->next == NULL
15848 && NOTE_P (loc_list->first->loc)
15849 && NOTE_VAR_LOCATION (loc_list->first->loc)
15850 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15852 struct var_loc_node *node;
15854 node = loc_list->first;
15855 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15856 if (GET_CODE (rtl) == EXPR_LIST)
15857 rtl = XEXP (rtl, 0);
15858 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15859 && add_const_value_attribute (die, rtl))
15860 return true;
15862 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15863 list several times. See if we've already cached the contents. */
15864 list = NULL;
15865 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15866 cache_p = false;
15867 if (cache_p)
15869 cache = (cached_dw_loc_list *)
15870 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15871 if (cache)
15872 list = cache->loc_list;
15874 if (list == NULL)
15876 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15877 /* It is usually worth caching this result if the decl is from
15878 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15879 if (cache_p && list && list->dw_loc_next)
15881 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15882 DECL_UID (decl), INSERT);
15883 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15884 cache->decl_id = DECL_UID (decl);
15885 cache->loc_list = list;
15886 *slot = cache;
15889 if (list)
15891 add_AT_location_description (die, attr, list);
15892 return true;
15894 /* None of that worked, so it must not really have a location;
15895 try adding a constant value attribute from the DECL_INITIAL. */
15896 return tree_add_const_value_attribute_for_decl (die, decl);
15899 /* Add VARIABLE and DIE into deferred locations list. */
15901 static void
15902 defer_location (tree variable, dw_die_ref die)
15904 deferred_locations entry;
15905 entry.variable = variable;
15906 entry.die = die;
15907 vec_safe_push (deferred_locations_list, entry);
15910 /* Helper function for tree_add_const_value_attribute. Natively encode
15911 initializer INIT into an array. Return true if successful. */
15913 static bool
15914 native_encode_initializer (tree init, unsigned char *array, int size)
15916 tree type;
15918 if (init == NULL_TREE)
15919 return false;
15921 STRIP_NOPS (init);
15922 switch (TREE_CODE (init))
15924 case STRING_CST:
15925 type = TREE_TYPE (init);
15926 if (TREE_CODE (type) == ARRAY_TYPE)
15928 tree enttype = TREE_TYPE (type);
15929 enum machine_mode mode = TYPE_MODE (enttype);
15931 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15932 return false;
15933 if (int_size_in_bytes (type) != size)
15934 return false;
15935 if (size > TREE_STRING_LENGTH (init))
15937 memcpy (array, TREE_STRING_POINTER (init),
15938 TREE_STRING_LENGTH (init));
15939 memset (array + TREE_STRING_LENGTH (init),
15940 '\0', size - TREE_STRING_LENGTH (init));
15942 else
15943 memcpy (array, TREE_STRING_POINTER (init), size);
15944 return true;
15946 return false;
15947 case CONSTRUCTOR:
15948 type = TREE_TYPE (init);
15949 if (int_size_in_bytes (type) != size)
15950 return false;
15951 if (TREE_CODE (type) == ARRAY_TYPE)
15953 HOST_WIDE_INT min_index;
15954 unsigned HOST_WIDE_INT cnt;
15955 int curpos = 0, fieldsize;
15956 constructor_elt *ce;
15958 if (TYPE_DOMAIN (type) == NULL_TREE
15959 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15960 return false;
15962 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15963 if (fieldsize <= 0)
15964 return false;
15966 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15967 memset (array, '\0', size);
15968 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15970 tree val = ce->value;
15971 tree index = ce->index;
15972 int pos = curpos;
15973 if (index && TREE_CODE (index) == RANGE_EXPR)
15974 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15975 * fieldsize;
15976 else if (index)
15977 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15979 if (val)
15981 STRIP_NOPS (val);
15982 if (!native_encode_initializer (val, array + pos, fieldsize))
15983 return false;
15985 curpos = pos + fieldsize;
15986 if (index && TREE_CODE (index) == RANGE_EXPR)
15988 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15989 - tree_to_shwi (TREE_OPERAND (index, 0));
15990 while (count-- > 0)
15992 if (val)
15993 memcpy (array + curpos, array + pos, fieldsize);
15994 curpos += fieldsize;
15997 gcc_assert (curpos <= size);
15999 return true;
16001 else if (TREE_CODE (type) == RECORD_TYPE
16002 || TREE_CODE (type) == UNION_TYPE)
16004 tree field = NULL_TREE;
16005 unsigned HOST_WIDE_INT cnt;
16006 constructor_elt *ce;
16008 if (int_size_in_bytes (type) != size)
16009 return false;
16011 if (TREE_CODE (type) == RECORD_TYPE)
16012 field = TYPE_FIELDS (type);
16014 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16016 tree val = ce->value;
16017 int pos, fieldsize;
16019 if (ce->index != 0)
16020 field = ce->index;
16022 if (val)
16023 STRIP_NOPS (val);
16025 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16026 return false;
16028 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16029 && TYPE_DOMAIN (TREE_TYPE (field))
16030 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16031 return false;
16032 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16033 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16034 return false;
16035 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16036 pos = int_byte_position (field);
16037 gcc_assert (pos + fieldsize <= size);
16038 if (val
16039 && !native_encode_initializer (val, array + pos, fieldsize))
16040 return false;
16042 return true;
16044 return false;
16045 case VIEW_CONVERT_EXPR:
16046 case NON_LVALUE_EXPR:
16047 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16048 default:
16049 return native_encode_expr (init, array, size) == size;
16053 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16054 attribute is the const value T. */
16056 static bool
16057 tree_add_const_value_attribute (dw_die_ref die, tree t)
16059 tree init;
16060 tree type = TREE_TYPE (t);
16061 rtx rtl;
16063 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16064 return false;
16066 init = t;
16067 gcc_assert (!DECL_P (init));
16069 rtl = rtl_for_decl_init (init, type);
16070 if (rtl)
16071 return add_const_value_attribute (die, rtl);
16072 /* If the host and target are sane, try harder. */
16073 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16074 && initializer_constant_valid_p (init, type))
16076 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16077 if (size > 0 && (int) size == size)
16079 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16081 if (native_encode_initializer (init, array, size))
16083 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16084 return true;
16086 ggc_free (array);
16089 return false;
16092 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16093 attribute is the const value of T, where T is an integral constant
16094 variable with static storage duration
16095 (so it can't be a PARM_DECL or a RESULT_DECL). */
16097 static bool
16098 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16101 if (!decl
16102 || (TREE_CODE (decl) != VAR_DECL
16103 && TREE_CODE (decl) != CONST_DECL)
16104 || (TREE_CODE (decl) == VAR_DECL
16105 && !TREE_STATIC (decl)))
16106 return false;
16108 if (TREE_READONLY (decl)
16109 && ! TREE_THIS_VOLATILE (decl)
16110 && DECL_INITIAL (decl))
16111 /* OK */;
16112 else
16113 return false;
16115 /* Don't add DW_AT_const_value if abstract origin already has one. */
16116 if (get_AT (var_die, DW_AT_const_value))
16117 return false;
16119 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16122 /* Convert the CFI instructions for the current function into a
16123 location list. This is used for DW_AT_frame_base when we targeting
16124 a dwarf2 consumer that does not support the dwarf3
16125 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16126 expressions. */
16128 static dw_loc_list_ref
16129 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16131 int ix;
16132 dw_fde_ref fde;
16133 dw_loc_list_ref list, *list_tail;
16134 dw_cfi_ref cfi;
16135 dw_cfa_location last_cfa, next_cfa;
16136 const char *start_label, *last_label, *section;
16137 dw_cfa_location remember;
16139 fde = cfun->fde;
16140 gcc_assert (fde != NULL);
16142 section = secname_for_decl (current_function_decl);
16143 list_tail = &list;
16144 list = NULL;
16146 memset (&next_cfa, 0, sizeof (next_cfa));
16147 next_cfa.reg = INVALID_REGNUM;
16148 remember = next_cfa;
16150 start_label = fde->dw_fde_begin;
16152 /* ??? Bald assumption that the CIE opcode list does not contain
16153 advance opcodes. */
16154 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16155 lookup_cfa_1 (cfi, &next_cfa, &remember);
16157 last_cfa = next_cfa;
16158 last_label = start_label;
16160 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16162 /* If the first partition contained no CFI adjustments, the
16163 CIE opcodes apply to the whole first partition. */
16164 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16165 fde->dw_fde_begin, fde->dw_fde_end, section);
16166 list_tail =&(*list_tail)->dw_loc_next;
16167 start_label = last_label = fde->dw_fde_second_begin;
16170 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16172 switch (cfi->dw_cfi_opc)
16174 case DW_CFA_set_loc:
16175 case DW_CFA_advance_loc1:
16176 case DW_CFA_advance_loc2:
16177 case DW_CFA_advance_loc4:
16178 if (!cfa_equal_p (&last_cfa, &next_cfa))
16180 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16181 start_label, last_label, section);
16183 list_tail = &(*list_tail)->dw_loc_next;
16184 last_cfa = next_cfa;
16185 start_label = last_label;
16187 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16188 break;
16190 case DW_CFA_advance_loc:
16191 /* The encoding is complex enough that we should never emit this. */
16192 gcc_unreachable ();
16194 default:
16195 lookup_cfa_1 (cfi, &next_cfa, &remember);
16196 break;
16198 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16200 if (!cfa_equal_p (&last_cfa, &next_cfa))
16202 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16203 start_label, last_label, section);
16205 list_tail = &(*list_tail)->dw_loc_next;
16206 last_cfa = next_cfa;
16207 start_label = last_label;
16209 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16210 start_label, fde->dw_fde_end, section);
16211 list_tail = &(*list_tail)->dw_loc_next;
16212 start_label = last_label = fde->dw_fde_second_begin;
16216 if (!cfa_equal_p (&last_cfa, &next_cfa))
16218 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16219 start_label, last_label, section);
16220 list_tail = &(*list_tail)->dw_loc_next;
16221 start_label = last_label;
16224 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16225 start_label,
16226 fde->dw_fde_second_begin
16227 ? fde->dw_fde_second_end : fde->dw_fde_end,
16228 section);
16230 if (list && list->dw_loc_next)
16231 gen_llsym (list);
16233 return list;
16236 /* Compute a displacement from the "steady-state frame pointer" to the
16237 frame base (often the same as the CFA), and store it in
16238 frame_pointer_fb_offset. OFFSET is added to the displacement
16239 before the latter is negated. */
16241 static void
16242 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16244 rtx reg, elim;
16246 #ifdef FRAME_POINTER_CFA_OFFSET
16247 reg = frame_pointer_rtx;
16248 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16249 #else
16250 reg = arg_pointer_rtx;
16251 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16252 #endif
16254 elim = (ira_use_lra_p
16255 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16256 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16257 if (GET_CODE (elim) == PLUS)
16259 offset += INTVAL (XEXP (elim, 1));
16260 elim = XEXP (elim, 0);
16263 frame_pointer_fb_offset = -offset;
16265 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16266 in which to eliminate. This is because it's stack pointer isn't
16267 directly accessible as a register within the ISA. To work around
16268 this, assume that while we cannot provide a proper value for
16269 frame_pointer_fb_offset, we won't need one either. */
16270 frame_pointer_fb_offset_valid
16271 = ((SUPPORTS_STACK_ALIGNMENT
16272 && (elim == hard_frame_pointer_rtx
16273 || elim == stack_pointer_rtx))
16274 || elim == (frame_pointer_needed
16275 ? hard_frame_pointer_rtx
16276 : stack_pointer_rtx));
16279 /* Generate a DW_AT_name attribute given some string value to be included as
16280 the value of the attribute. */
16282 static void
16283 add_name_attribute (dw_die_ref die, const char *name_string)
16285 if (name_string != NULL && *name_string != 0)
16287 if (demangle_name_func)
16288 name_string = (*demangle_name_func) (name_string);
16290 add_AT_string (die, DW_AT_name, name_string);
16294 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16295 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16296 of TYPE accordingly.
16298 ??? This is a temporary measure until after we're able to generate
16299 regular DWARF for the complex Ada type system. */
16301 static void
16302 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16303 dw_die_ref context_die)
16305 tree dtype;
16306 dw_die_ref dtype_die;
16308 if (!lang_hooks.types.descriptive_type)
16309 return;
16311 dtype = lang_hooks.types.descriptive_type (type);
16312 if (!dtype)
16313 return;
16315 dtype_die = lookup_type_die (dtype);
16316 if (!dtype_die)
16318 gen_type_die (dtype, context_die);
16319 dtype_die = lookup_type_die (dtype);
16320 gcc_assert (dtype_die);
16323 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16326 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16328 static const char *
16329 comp_dir_string (void)
16331 const char *wd;
16332 char *wd1;
16333 static const char *cached_wd = NULL;
16335 if (cached_wd != NULL)
16336 return cached_wd;
16338 wd = get_src_pwd ();
16339 if (wd == NULL)
16340 return NULL;
16342 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16344 int wdlen;
16346 wdlen = strlen (wd);
16347 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16348 strcpy (wd1, wd);
16349 wd1 [wdlen] = DIR_SEPARATOR;
16350 wd1 [wdlen + 1] = 0;
16351 wd = wd1;
16354 cached_wd = remap_debug_filename (wd);
16355 return cached_wd;
16358 /* Generate a DW_AT_comp_dir attribute for DIE. */
16360 static void
16361 add_comp_dir_attribute (dw_die_ref die)
16363 const char * wd = comp_dir_string ();
16364 if (wd != NULL)
16365 add_AT_string (die, DW_AT_comp_dir, wd);
16368 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16369 default. */
16371 static int
16372 lower_bound_default (void)
16374 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16376 case DW_LANG_C:
16377 case DW_LANG_C89:
16378 case DW_LANG_C99:
16379 case DW_LANG_C_plus_plus:
16380 case DW_LANG_ObjC:
16381 case DW_LANG_ObjC_plus_plus:
16382 case DW_LANG_Java:
16383 return 0;
16384 case DW_LANG_Fortran77:
16385 case DW_LANG_Fortran90:
16386 case DW_LANG_Fortran95:
16387 return 1;
16388 case DW_LANG_UPC:
16389 case DW_LANG_D:
16390 case DW_LANG_Python:
16391 return dwarf_version >= 4 ? 0 : -1;
16392 case DW_LANG_Ada95:
16393 case DW_LANG_Ada83:
16394 case DW_LANG_Cobol74:
16395 case DW_LANG_Cobol85:
16396 case DW_LANG_Pascal83:
16397 case DW_LANG_Modula2:
16398 case DW_LANG_PLI:
16399 return dwarf_version >= 4 ? 1 : -1;
16400 default:
16401 return -1;
16405 /* Given a tree node describing an array bound (either lower or upper) output
16406 a representation for that bound. */
16408 static void
16409 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16411 switch (TREE_CODE (bound))
16413 case ERROR_MARK:
16414 return;
16416 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16417 case INTEGER_CST:
16419 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16420 int dflt;
16422 /* Use the default if possible. */
16423 if (bound_attr == DW_AT_lower_bound
16424 && tree_fits_shwi_p (bound)
16425 && (dflt = lower_bound_default ()) != -1
16426 && tree_to_shwi (bound) == dflt)
16429 /* If HOST_WIDE_INT is big enough then represent the bound as
16430 a constant value. We need to choose a form based on
16431 whether the type is signed or unsigned. We cannot just
16432 call add_AT_unsigned if the value itself is positive
16433 (add_AT_unsigned might add the unsigned value encoded as
16434 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16435 bounds type and then sign extend any unsigned values found
16436 for signed types. This is needed only for
16437 DW_AT_{lower,upper}_bound, since for most other attributes,
16438 consumers will treat DW_FORM_data[1248] as unsigned values,
16439 regardless of the underlying type. */
16440 else if (prec <= HOST_BITS_PER_WIDE_INT
16441 || tree_fits_uhwi_p (bound))
16443 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16444 add_AT_unsigned (subrange_die, bound_attr,
16445 TREE_INT_CST_LOW (bound));
16446 else
16447 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16449 else
16450 /* Otherwise represent the bound as an unsigned value with
16451 the precision of its type. The precision and signedness
16452 of the type will be necessary to re-interpret it
16453 unambiguously. */
16454 add_AT_wide (subrange_die, bound_attr, bound);
16456 break;
16458 CASE_CONVERT:
16459 case VIEW_CONVERT_EXPR:
16460 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16461 break;
16463 case SAVE_EXPR:
16464 break;
16466 case VAR_DECL:
16467 case PARM_DECL:
16468 case RESULT_DECL:
16470 dw_die_ref decl_die = lookup_decl_die (bound);
16472 /* ??? Can this happen, or should the variable have been bound
16473 first? Probably it can, since I imagine that we try to create
16474 the types of parameters in the order in which they exist in
16475 the list, and won't have created a forward reference to a
16476 later parameter. */
16477 if (decl_die != NULL)
16479 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16480 break;
16483 /* FALLTHRU */
16485 default:
16487 /* Otherwise try to create a stack operation procedure to
16488 evaluate the value of the array bound. */
16490 dw_die_ref ctx, decl_die;
16491 dw_loc_list_ref list;
16493 list = loc_list_from_tree (bound, 2);
16494 if (list == NULL || single_element_loc_list_p (list))
16496 /* If DW_AT_*bound is not a reference nor constant, it is
16497 a DWARF expression rather than location description.
16498 For that loc_list_from_tree (bound, 0) is needed.
16499 If that fails to give a single element list,
16500 fall back to outputting this as a reference anyway. */
16501 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16502 if (list2 && single_element_loc_list_p (list2))
16504 add_AT_loc (subrange_die, bound_attr, list2->expr);
16505 break;
16508 if (list == NULL)
16509 break;
16511 if (current_function_decl == 0)
16512 ctx = comp_unit_die ();
16513 else
16514 ctx = lookup_decl_die (current_function_decl);
16516 decl_die = new_die (DW_TAG_variable, ctx, bound);
16517 add_AT_flag (decl_die, DW_AT_artificial, 1);
16518 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16519 add_AT_location_description (decl_die, DW_AT_location, list);
16520 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16521 break;
16526 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16527 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16528 Note that the block of subscript information for an array type also
16529 includes information about the element type of the given array type. */
16531 static void
16532 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16534 unsigned dimension_number;
16535 tree lower, upper;
16536 dw_die_ref subrange_die;
16538 for (dimension_number = 0;
16539 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16540 type = TREE_TYPE (type), dimension_number++)
16542 tree domain = TYPE_DOMAIN (type);
16544 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16545 break;
16547 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16548 and (in GNU C only) variable bounds. Handle all three forms
16549 here. */
16550 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16552 if (use_upc_dwarf2_extensions && TYPE_HAS_THREADS_FACTOR (type))
16554 add_AT_flag (subrange_die, DW_AT_upc_threads_scaled, 1);
16557 if (domain)
16559 /* We have an array type with specified bounds. */
16560 lower = TYPE_MIN_VALUE (domain);
16561 upper = TYPE_MAX_VALUE (domain);
16563 /* Define the index type. */
16564 if (TREE_TYPE (domain))
16566 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16567 TREE_TYPE field. We can't emit debug info for this
16568 because it is an unnamed integral type. */
16569 if (TREE_CODE (domain) == INTEGER_TYPE
16570 && TYPE_NAME (domain) == NULL_TREE
16571 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16572 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16574 else
16575 add_type_attribute (subrange_die, TREE_TYPE (domain),
16576 TYPE_UNQUALIFIED, type_die);
16579 /* ??? If upper is NULL, the array has unspecified length,
16580 but it does have a lower bound. This happens with Fortran
16581 dimension arr(N:*)
16582 Since the debugger is definitely going to need to know N
16583 to produce useful results, go ahead and output the lower
16584 bound solo, and hope the debugger can cope. */
16586 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16587 if (upper)
16588 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16591 /* Otherwise we have an array type with an unspecified length. The
16592 DWARF-2 spec does not say how to handle this; let's just leave out the
16593 bounds. */
16597 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16599 static void
16600 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16602 dw_die_ref decl_die;
16603 HOST_WIDE_INT size;
16605 switch (TREE_CODE (tree_node))
16607 case ERROR_MARK:
16608 size = 0;
16609 break;
16610 case ENUMERAL_TYPE:
16611 case RECORD_TYPE:
16612 case UNION_TYPE:
16613 case QUAL_UNION_TYPE:
16614 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16615 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16617 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16618 return;
16620 size = int_size_in_bytes (tree_node);
16621 break;
16622 case FIELD_DECL:
16623 /* For a data member of a struct or union, the DW_AT_byte_size is
16624 generally given as the number of bytes normally allocated for an
16625 object of the *declared* type of the member itself. This is true
16626 even for bit-fields. */
16627 size = int_size_in_bytes (field_type (tree_node));
16628 break;
16629 default:
16630 gcc_unreachable ();
16633 /* Note that `size' might be -1 when we get to this point. If it is, that
16634 indicates that the byte size of the entity in question is variable. We
16635 have no good way of expressing this fact in Dwarf at the present time,
16636 when location description was not used by the caller code instead. */
16637 if (size >= 0)
16638 add_AT_unsigned (die, DW_AT_byte_size, size);
16641 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16642 which specifies the distance in bits from the highest order bit of the
16643 "containing object" for the bit-field to the highest order bit of the
16644 bit-field itself.
16646 For any given bit-field, the "containing object" is a hypothetical object
16647 (of some integral or enum type) within which the given bit-field lives. The
16648 type of this hypothetical "containing object" is always the same as the
16649 declared type of the individual bit-field itself. The determination of the
16650 exact location of the "containing object" for a bit-field is rather
16651 complicated. It's handled by the `field_byte_offset' function (above).
16653 Note that it is the size (in bytes) of the hypothetical "containing object"
16654 which will be given in the DW_AT_byte_size attribute for this bit-field.
16655 (See `byte_size_attribute' above). */
16657 static inline void
16658 add_bit_offset_attribute (dw_die_ref die, tree decl)
16660 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16661 tree type = DECL_BIT_FIELD_TYPE (decl);
16662 HOST_WIDE_INT bitpos_int;
16663 HOST_WIDE_INT highest_order_object_bit_offset;
16664 HOST_WIDE_INT highest_order_field_bit_offset;
16665 HOST_WIDE_INT bit_offset;
16667 /* Must be a field and a bit field. */
16668 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16670 /* We can't yet handle bit-fields whose offsets are variable, so if we
16671 encounter such things, just return without generating any attribute
16672 whatsoever. Likewise for variable or too large size. */
16673 if (! tree_fits_shwi_p (bit_position (decl))
16674 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16675 return;
16677 bitpos_int = int_bit_position (decl);
16679 /* Note that the bit offset is always the distance (in bits) from the
16680 highest-order bit of the "containing object" to the highest-order bit of
16681 the bit-field itself. Since the "high-order end" of any object or field
16682 is different on big-endian and little-endian machines, the computation
16683 below must take account of these differences. */
16684 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16685 highest_order_field_bit_offset = bitpos_int;
16687 if (! BYTES_BIG_ENDIAN)
16689 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16690 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16693 bit_offset
16694 = (! BYTES_BIG_ENDIAN
16695 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16696 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16698 if (bit_offset < 0)
16699 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16700 else
16701 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16704 /* For a FIELD_DECL node which represents a bit field, output an attribute
16705 which specifies the length in bits of the given field. */
16707 static inline void
16708 add_bit_size_attribute (dw_die_ref die, tree decl)
16710 /* Must be a field and a bit field. */
16711 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16712 && DECL_BIT_FIELD_TYPE (decl));
16714 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16715 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16718 /* If the compiled language is ANSI C, then add a 'prototyped'
16719 attribute, if arg types are given for the parameters of a function. */
16721 static inline void
16722 add_prototyped_attribute (dw_die_ref die, tree func_type)
16724 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16725 && prototype_p (func_type))
16726 add_AT_flag (die, DW_AT_prototyped, 1);
16729 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16730 by looking in either the type declaration or object declaration
16731 equate table. */
16733 static inline dw_die_ref
16734 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16736 dw_die_ref origin_die = NULL;
16738 if (TREE_CODE (origin) != FUNCTION_DECL)
16740 /* We may have gotten separated from the block for the inlined
16741 function, if we're in an exception handler or some such; make
16742 sure that the abstract function has been written out.
16744 Doing this for nested functions is wrong, however; functions are
16745 distinct units, and our context might not even be inline. */
16746 tree fn = origin;
16748 if (TYPE_P (fn))
16749 fn = TYPE_STUB_DECL (fn);
16751 fn = decl_function_context (fn);
16752 if (fn)
16753 dwarf2out_abstract_function (fn);
16756 if (DECL_P (origin))
16757 origin_die = lookup_decl_die (origin);
16758 else if (TYPE_P (origin))
16759 origin_die = lookup_type_die (origin);
16761 /* XXX: Functions that are never lowered don't always have correct block
16762 trees (in the case of java, they simply have no block tree, in some other
16763 languages). For these functions, there is nothing we can really do to
16764 output correct debug info for inlined functions in all cases. Rather
16765 than die, we'll just produce deficient debug info now, in that we will
16766 have variables without a proper abstract origin. In the future, when all
16767 functions are lowered, we should re-add a gcc_assert (origin_die)
16768 here. */
16770 if (origin_die)
16771 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16772 return origin_die;
16775 /* We do not currently support the pure_virtual attribute. */
16777 static inline void
16778 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16780 if (DECL_VINDEX (func_decl))
16782 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16784 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16785 add_AT_loc (die, DW_AT_vtable_elem_location,
16786 new_loc_descr (DW_OP_constu,
16787 tree_to_shwi (DECL_VINDEX (func_decl)),
16788 0));
16790 /* GNU extension: Record what type this method came from originally. */
16791 if (debug_info_level > DINFO_LEVEL_TERSE
16792 && DECL_CONTEXT (func_decl))
16793 add_AT_die_ref (die, DW_AT_containing_type,
16794 lookup_type_die (DECL_CONTEXT (func_decl)));
16798 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16799 given decl. This used to be a vendor extension until after DWARF 4
16800 standardized it. */
16802 static void
16803 add_linkage_attr (dw_die_ref die, tree decl)
16805 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16807 /* Mimic what assemble_name_raw does with a leading '*'. */
16808 if (name[0] == '*')
16809 name = &name[1];
16811 if (dwarf_version >= 4)
16812 add_AT_string (die, DW_AT_linkage_name, name);
16813 else
16814 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16817 /* Add source coordinate attributes for the given decl. */
16819 static void
16820 add_src_coords_attributes (dw_die_ref die, tree decl)
16822 expanded_location s;
16824 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16825 return;
16826 s = expand_location (DECL_SOURCE_LOCATION (decl));
16827 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16828 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16831 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16833 static void
16834 add_linkage_name (dw_die_ref die, tree decl)
16836 if (debug_info_level > DINFO_LEVEL_NONE
16837 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16838 && TREE_PUBLIC (decl)
16839 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16840 && die->die_tag != DW_TAG_member)
16842 /* Defer until we have an assembler name set. */
16843 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16845 limbo_die_node *asm_name;
16847 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16848 asm_name->die = die;
16849 asm_name->created_for = decl;
16850 asm_name->next = deferred_asm_name;
16851 deferred_asm_name = asm_name;
16853 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16854 add_linkage_attr (die, decl);
16858 /* Add a DW_AT_name attribute and source coordinate attribute for the
16859 given decl, but only if it actually has a name. */
16861 static void
16862 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16864 tree decl_name;
16866 decl_name = DECL_NAME (decl);
16867 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16869 const char *name = dwarf2_name (decl, 0);
16870 if (name)
16871 add_name_attribute (die, name);
16872 if (! DECL_ARTIFICIAL (decl))
16873 add_src_coords_attributes (die, decl);
16875 add_linkage_name (die, decl);
16878 #ifdef VMS_DEBUGGING_INFO
16879 /* Get the function's name, as described by its RTL. This may be different
16880 from the DECL_NAME name used in the source file. */
16881 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16883 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16884 XEXP (DECL_RTL (decl), 0), false);
16885 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16887 #endif /* VMS_DEBUGGING_INFO */
16890 #ifdef VMS_DEBUGGING_INFO
16891 /* Output the debug main pointer die for VMS */
16893 void
16894 dwarf2out_vms_debug_main_pointer (void)
16896 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16897 dw_die_ref die;
16899 /* Allocate the VMS debug main subprogram die. */
16900 die = ggc_cleared_alloc<die_node> ();
16901 die->die_tag = DW_TAG_subprogram;
16902 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16903 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16904 current_function_funcdef_no);
16905 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16907 /* Make it the first child of comp_unit_die (). */
16908 die->die_parent = comp_unit_die ();
16909 if (comp_unit_die ()->die_child)
16911 die->die_sib = comp_unit_die ()->die_child->die_sib;
16912 comp_unit_die ()->die_child->die_sib = die;
16914 else
16916 die->die_sib = die;
16917 comp_unit_die ()->die_child = die;
16920 #endif /* VMS_DEBUGGING_INFO */
16922 /* Push a new declaration scope. */
16924 static void
16925 push_decl_scope (tree scope)
16927 vec_safe_push (decl_scope_table, scope);
16930 /* Pop a declaration scope. */
16932 static inline void
16933 pop_decl_scope (void)
16935 decl_scope_table->pop ();
16938 /* walk_tree helper function for uses_local_type, below. */
16940 static tree
16941 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16943 if (!TYPE_P (*tp))
16944 *walk_subtrees = 0;
16945 else
16947 tree name = TYPE_NAME (*tp);
16948 if (name && DECL_P (name) && decl_function_context (name))
16949 return *tp;
16951 return NULL_TREE;
16954 /* If TYPE involves a function-local type (including a local typedef to a
16955 non-local type), returns that type; otherwise returns NULL_TREE. */
16957 static tree
16958 uses_local_type (tree type)
16960 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16961 return used;
16964 /* Return the DIE for the scope that immediately contains this type.
16965 Non-named types that do not involve a function-local type get global
16966 scope. Named types nested in namespaces or other types get their
16967 containing scope. All other types (i.e. function-local named types) get
16968 the current active scope. */
16970 static dw_die_ref
16971 scope_die_for (tree t, dw_die_ref context_die)
16973 dw_die_ref scope_die = NULL;
16974 tree containing_scope;
16976 /* Non-types always go in the current scope. */
16977 gcc_assert (TYPE_P (t));
16979 /* Use the scope of the typedef, rather than the scope of the type
16980 it refers to. */
16981 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16982 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16983 else
16984 containing_scope = TYPE_CONTEXT (t);
16986 /* Use the containing namespace if there is one. */
16987 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16989 if (context_die == lookup_decl_die (containing_scope))
16990 /* OK */;
16991 else if (debug_info_level > DINFO_LEVEL_TERSE)
16992 context_die = get_context_die (containing_scope);
16993 else
16994 containing_scope = NULL_TREE;
16997 /* Ignore function type "scopes" from the C frontend. They mean that
16998 a tagged type is local to a parmlist of a function declarator, but
16999 that isn't useful to DWARF. */
17000 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17001 containing_scope = NULL_TREE;
17003 if (SCOPE_FILE_SCOPE_P (containing_scope))
17005 /* If T uses a local type keep it local as well, to avoid references
17006 to function-local DIEs from outside the function. */
17007 if (current_function_decl && uses_local_type (t))
17008 scope_die = context_die;
17009 else
17010 scope_die = comp_unit_die ();
17012 else if (TYPE_P (containing_scope))
17014 /* For types, we can just look up the appropriate DIE. */
17015 if (debug_info_level > DINFO_LEVEL_TERSE)
17016 scope_die = get_context_die (containing_scope);
17017 else
17019 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17020 if (scope_die == NULL)
17021 scope_die = comp_unit_die ();
17024 else
17025 scope_die = context_die;
17027 return scope_die;
17030 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17032 static inline int
17033 local_scope_p (dw_die_ref context_die)
17035 for (; context_die; context_die = context_die->die_parent)
17036 if (context_die->die_tag == DW_TAG_inlined_subroutine
17037 || context_die->die_tag == DW_TAG_subprogram)
17038 return 1;
17040 return 0;
17043 /* Returns nonzero if CONTEXT_DIE is a class. */
17045 static inline int
17046 class_scope_p (dw_die_ref context_die)
17048 return (context_die
17049 && (context_die->die_tag == DW_TAG_structure_type
17050 || context_die->die_tag == DW_TAG_class_type
17051 || context_die->die_tag == DW_TAG_interface_type
17052 || context_die->die_tag == DW_TAG_union_type));
17055 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17056 whether or not to treat a DIE in this context as a declaration. */
17058 static inline int
17059 class_or_namespace_scope_p (dw_die_ref context_die)
17061 return (class_scope_p (context_die)
17062 || (context_die && context_die->die_tag == DW_TAG_namespace));
17065 /* Many forms of DIEs require a "type description" attribute. This
17066 routine locates the proper "type descriptor" die for the type given
17067 by 'type' plus any additional qualifiers given by 'cv_quals', and
17068 adds a DW_AT_type attribute below the given die. */
17070 static void
17071 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17072 dw_die_ref context_die)
17074 enum tree_code code = TREE_CODE (type);
17075 dw_die_ref type_die = NULL;
17076 int type_quals;
17078 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17079 or fixed-point type, use the inner type. This is because we have no
17080 support for unnamed types in base_type_die. This can happen if this is
17081 an Ada subrange type. Correct solution is emit a subrange type die. */
17082 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17083 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17084 type = TREE_TYPE (type), code = TREE_CODE (type);
17086 if (code == ERROR_MARK
17087 /* Handle a special case. For functions whose return type is void, we
17088 generate *no* type attribute. (Note that no object may have type
17089 `void', so this only applies to function return types). */
17090 || code == VOID_TYPE)
17091 return;
17093 type_die = modified_type_die (type,
17094 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17095 context_die);
17097 if (type_die != NULL)
17098 add_AT_die_ref (object_die, DW_AT_type, type_die);
17101 /* Given an object die, add the calling convention attribute for the
17102 function call type. */
17103 static void
17104 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17106 enum dwarf_calling_convention value = DW_CC_normal;
17108 value = ((enum dwarf_calling_convention)
17109 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17111 if (is_fortran ()
17112 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17114 /* DWARF 2 doesn't provide a way to identify a program's source-level
17115 entry point. DW_AT_calling_convention attributes are only meant
17116 to describe functions' calling conventions. However, lacking a
17117 better way to signal the Fortran main program, we used this for
17118 a long time, following existing custom. Now, DWARF 4 has
17119 DW_AT_main_subprogram, which we add below, but some tools still
17120 rely on the old way, which we thus keep. */
17121 value = DW_CC_program;
17123 if (dwarf_version >= 4 || !dwarf_strict)
17124 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17127 /* Only add the attribute if the backend requests it, and
17128 is not DW_CC_normal. */
17129 if (value && (value != DW_CC_normal))
17130 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17133 /* Given a tree pointer to a struct, class, union, or enum type node, return
17134 a pointer to the (string) tag name for the given type, or zero if the type
17135 was declared without a tag. */
17137 static const char *
17138 type_tag (const_tree type)
17140 const char *name = 0;
17142 if (TYPE_NAME (type) != 0)
17144 tree t = 0;
17146 /* Find the IDENTIFIER_NODE for the type name. */
17147 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17148 && !TYPE_NAMELESS (type))
17149 t = TYPE_NAME (type);
17151 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17152 a TYPE_DECL node, regardless of whether or not a `typedef' was
17153 involved. */
17154 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17155 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17157 /* We want to be extra verbose. Don't call dwarf_name if
17158 DECL_NAME isn't set. The default hook for decl_printable_name
17159 doesn't like that, and in this context it's correct to return
17160 0, instead of "<anonymous>" or the like. */
17161 if (DECL_NAME (TYPE_NAME (type))
17162 && !DECL_NAMELESS (TYPE_NAME (type)))
17163 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17166 /* Now get the name as a string, or invent one. */
17167 if (!name && t != 0)
17168 name = IDENTIFIER_POINTER (t);
17171 return (name == 0 || *name == '\0') ? 0 : name;
17174 /* Return the type associated with a data member, make a special check
17175 for bit field types. */
17177 static inline tree
17178 member_declared_type (const_tree member)
17180 return (DECL_BIT_FIELD_TYPE (member)
17181 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17184 /* Get the decl's label, as described by its RTL. This may be different
17185 from the DECL_NAME name used in the source file. */
17187 #if 0
17188 static const char *
17189 decl_start_label (tree decl)
17191 rtx x;
17192 const char *fnname;
17194 x = DECL_RTL (decl);
17195 gcc_assert (MEM_P (x));
17197 x = XEXP (x, 0);
17198 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17200 fnname = XSTR (x, 0);
17201 return fnname;
17203 #endif
17205 /* These routines generate the internal representation of the DIE's for
17206 the compilation unit. Debugging information is collected by walking
17207 the declaration trees passed in from dwarf2out_decl(). */
17209 static void
17210 gen_array_type_die (tree type, dw_die_ref context_die)
17212 dw_die_ref scope_die = scope_die_for (type, context_die);
17213 dw_die_ref array_die;
17215 /* GNU compilers represent multidimensional array types as sequences of one
17216 dimensional array types whose element types are themselves array types.
17217 We sometimes squish that down to a single array_type DIE with multiple
17218 subscripts in the Dwarf debugging info. The draft Dwarf specification
17219 say that we are allowed to do this kind of compression in C, because
17220 there is no difference between an array of arrays and a multidimensional
17221 array. We don't do this for Ada to remain as close as possible to the
17222 actual representation, which is especially important against the language
17223 flexibilty wrt arrays of variable size. */
17225 bool collapse_nested_arrays = !is_ada ();
17226 tree element_type;
17228 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17229 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17230 if (TYPE_STRING_FLAG (type)
17231 && TREE_CODE (type) == ARRAY_TYPE
17232 && is_fortran ()
17233 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17235 HOST_WIDE_INT size;
17237 array_die = new_die (DW_TAG_string_type, scope_die, type);
17238 add_name_attribute (array_die, type_tag (type));
17239 equate_type_number_to_die (type, array_die);
17240 size = int_size_in_bytes (type);
17241 if (size >= 0)
17242 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17243 else if (TYPE_DOMAIN (type) != NULL_TREE
17244 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17245 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17247 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17248 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17250 size = int_size_in_bytes (TREE_TYPE (szdecl));
17251 if (loc && size > 0)
17253 add_AT_location_description (array_die, DW_AT_string_length, loc);
17254 if (size != DWARF2_ADDR_SIZE)
17255 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17258 return;
17261 array_die = new_die (DW_TAG_array_type, scope_die, type);
17262 add_name_attribute (array_die, type_tag (type));
17263 equate_type_number_to_die (type, array_die);
17265 if (TREE_CODE (type) == VECTOR_TYPE)
17266 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17268 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17269 if (is_fortran ()
17270 && TREE_CODE (type) == ARRAY_TYPE
17271 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17272 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17273 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17275 #if 0
17276 /* We default the array ordering. SDB will probably do
17277 the right things even if DW_AT_ordering is not present. It's not even
17278 an issue until we start to get into multidimensional arrays anyway. If
17279 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17280 then we'll have to put the DW_AT_ordering attribute back in. (But if
17281 and when we find out that we need to put these in, we will only do so
17282 for multidimensional arrays. */
17283 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17284 #endif
17286 if (TREE_CODE (type) == VECTOR_TYPE)
17288 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17289 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17290 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17291 add_bound_info (subrange_die, DW_AT_upper_bound,
17292 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17294 else
17295 add_subscript_info (array_die, type, collapse_nested_arrays);
17297 /* Add representation of the type of the elements of this array type and
17298 emit the corresponding DIE if we haven't done it already. */
17299 element_type = TREE_TYPE (type);
17300 if (collapse_nested_arrays)
17301 while (TREE_CODE (element_type) == ARRAY_TYPE)
17303 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17304 break;
17305 element_type = TREE_TYPE (element_type);
17308 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17310 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17311 if (TYPE_ARTIFICIAL (type))
17312 add_AT_flag (array_die, DW_AT_artificial, 1);
17314 if (get_AT (array_die, DW_AT_name))
17315 add_pubtype (type, array_die);
17318 static dw_loc_descr_ref
17319 descr_info_loc (tree val, tree base_decl)
17321 HOST_WIDE_INT size;
17322 dw_loc_descr_ref loc, loc2;
17323 enum dwarf_location_atom op;
17325 if (val == base_decl)
17326 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17328 switch (TREE_CODE (val))
17330 CASE_CONVERT:
17331 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17332 case VAR_DECL:
17333 return loc_descriptor_from_tree (val, 0);
17334 case INTEGER_CST:
17335 if (tree_fits_shwi_p (val))
17336 return int_loc_descriptor (tree_to_shwi (val));
17337 break;
17338 case INDIRECT_REF:
17339 size = int_size_in_bytes (TREE_TYPE (val));
17340 if (size < 0)
17341 break;
17342 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17343 if (!loc)
17344 break;
17345 if (size == DWARF2_ADDR_SIZE)
17346 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17347 else
17348 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17349 return loc;
17350 case POINTER_PLUS_EXPR:
17351 case PLUS_EXPR:
17352 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17353 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17355 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17356 if (!loc)
17357 break;
17358 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17360 else
17362 op = DW_OP_plus;
17363 do_binop:
17364 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17365 if (!loc)
17366 break;
17367 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17368 if (!loc2)
17369 break;
17370 add_loc_descr (&loc, loc2);
17371 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17373 return loc;
17374 case MINUS_EXPR:
17375 op = DW_OP_minus;
17376 goto do_binop;
17377 case MULT_EXPR:
17378 op = DW_OP_mul;
17379 goto do_binop;
17380 case EQ_EXPR:
17381 op = DW_OP_eq;
17382 goto do_binop;
17383 case NE_EXPR:
17384 op = DW_OP_ne;
17385 goto do_binop;
17386 default:
17387 break;
17389 return NULL;
17392 static void
17393 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17394 tree val, tree base_decl)
17396 dw_loc_descr_ref loc;
17398 if (tree_fits_shwi_p (val))
17400 add_AT_unsigned (die, attr, tree_to_shwi (val));
17401 return;
17404 loc = descr_info_loc (val, base_decl);
17405 if (!loc)
17406 return;
17408 add_AT_loc (die, attr, loc);
17411 /* This routine generates DIE for array with hidden descriptor, details
17412 are filled into *info by a langhook. */
17414 static void
17415 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17416 dw_die_ref context_die)
17418 dw_die_ref scope_die = scope_die_for (type, context_die);
17419 dw_die_ref array_die;
17420 int dim;
17422 array_die = new_die (DW_TAG_array_type, scope_die, type);
17423 add_name_attribute (array_die, type_tag (type));
17424 equate_type_number_to_die (type, array_die);
17426 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17427 if (is_fortran ()
17428 && info->ndimensions >= 2)
17429 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17431 if (info->data_location)
17432 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17433 info->base_decl);
17434 if (info->associated)
17435 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17436 info->base_decl);
17437 if (info->allocated)
17438 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17439 info->base_decl);
17441 for (dim = 0; dim < info->ndimensions; dim++)
17443 dw_die_ref subrange_die
17444 = new_die (DW_TAG_subrange_type, array_die, NULL);
17446 if (info->dimen[dim].lower_bound)
17448 /* If it is the default value, omit it. */
17449 int dflt;
17451 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17452 && (dflt = lower_bound_default ()) != -1
17453 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17455 else
17456 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17457 info->dimen[dim].lower_bound,
17458 info->base_decl);
17460 if (info->dimen[dim].upper_bound)
17461 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17462 info->dimen[dim].upper_bound,
17463 info->base_decl);
17464 if (info->dimen[dim].stride)
17465 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17466 info->dimen[dim].stride,
17467 info->base_decl);
17470 gen_type_die (info->element_type, context_die);
17471 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17472 context_die);
17474 if (get_AT (array_die, DW_AT_name))
17475 add_pubtype (type, array_die);
17478 #if 0
17479 static void
17480 gen_entry_point_die (tree decl, dw_die_ref context_die)
17482 tree origin = decl_ultimate_origin (decl);
17483 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17485 if (origin != NULL)
17486 add_abstract_origin_attribute (decl_die, origin);
17487 else
17489 add_name_and_src_coords_attributes (decl_die, decl);
17490 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17491 TYPE_UNQUALIFIED, context_die);
17494 if (DECL_ABSTRACT (decl))
17495 equate_decl_number_to_die (decl, decl_die);
17496 else
17497 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17499 #endif
17501 /* Walk through the list of incomplete types again, trying once more to
17502 emit full debugging info for them. */
17504 static void
17505 retry_incomplete_types (void)
17507 int i;
17509 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17510 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17511 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17514 /* Determine what tag to use for a record type. */
17516 static enum dwarf_tag
17517 record_type_tag (tree type)
17519 if (! lang_hooks.types.classify_record)
17520 return DW_TAG_structure_type;
17522 switch (lang_hooks.types.classify_record (type))
17524 case RECORD_IS_STRUCT:
17525 return DW_TAG_structure_type;
17527 case RECORD_IS_CLASS:
17528 return DW_TAG_class_type;
17530 case RECORD_IS_INTERFACE:
17531 if (dwarf_version >= 3 || !dwarf_strict)
17532 return DW_TAG_interface_type;
17533 return DW_TAG_structure_type;
17535 default:
17536 gcc_unreachable ();
17540 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17541 include all of the information about the enumeration values also. Each
17542 enumerated type name/value is listed as a child of the enumerated type
17543 DIE. */
17545 static dw_die_ref
17546 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17548 dw_die_ref type_die = lookup_type_die (type);
17550 if (type_die == NULL)
17552 type_die = new_die (DW_TAG_enumeration_type,
17553 scope_die_for (type, context_die), type);
17554 equate_type_number_to_die (type, type_die);
17555 add_name_attribute (type_die, type_tag (type));
17556 if (dwarf_version >= 4 || !dwarf_strict)
17558 if (ENUM_IS_SCOPED (type))
17559 add_AT_flag (type_die, DW_AT_enum_class, 1);
17560 if (ENUM_IS_OPAQUE (type))
17561 add_AT_flag (type_die, DW_AT_declaration, 1);
17564 else if (! TYPE_SIZE (type))
17565 return type_die;
17566 else
17567 remove_AT (type_die, DW_AT_declaration);
17569 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17570 given enum type is incomplete, do not generate the DW_AT_byte_size
17571 attribute or the DW_AT_element_list attribute. */
17572 if (TYPE_SIZE (type))
17574 tree link;
17576 TREE_ASM_WRITTEN (type) = 1;
17577 add_byte_size_attribute (type_die, type);
17578 if (dwarf_version >= 3 || !dwarf_strict)
17580 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17581 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17582 context_die);
17584 if (TYPE_STUB_DECL (type) != NULL_TREE)
17586 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17587 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17590 /* If the first reference to this type was as the return type of an
17591 inline function, then it may not have a parent. Fix this now. */
17592 if (type_die->die_parent == NULL)
17593 add_child_die (scope_die_for (type, context_die), type_die);
17595 for (link = TYPE_VALUES (type);
17596 link != NULL; link = TREE_CHAIN (link))
17598 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17599 tree value = TREE_VALUE (link);
17601 add_name_attribute (enum_die,
17602 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17604 if (TREE_CODE (value) == CONST_DECL)
17605 value = DECL_INITIAL (value);
17607 if (simple_type_size_in_bits (TREE_TYPE (value))
17608 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17610 /* For constant forms created by add_AT_unsigned DWARF
17611 consumers (GDB, elfutils, etc.) always zero extend
17612 the value. Only when the actual value is negative
17613 do we need to use add_AT_int to generate a constant
17614 form that can represent negative values. */
17615 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17616 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17617 add_AT_unsigned (enum_die, DW_AT_const_value,
17618 (unsigned HOST_WIDE_INT) val);
17619 else
17620 add_AT_int (enum_die, DW_AT_const_value, val);
17622 else
17623 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17624 that here. TODO: This should be re-worked to use correct
17625 signed/unsigned double tags for all cases. */
17626 add_AT_wide (enum_die, DW_AT_const_value, value);
17629 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17630 if (TYPE_ARTIFICIAL (type))
17631 add_AT_flag (type_die, DW_AT_artificial, 1);
17633 else
17634 add_AT_flag (type_die, DW_AT_declaration, 1);
17636 add_pubtype (type, type_die);
17638 return type_die;
17641 /* Generate a DIE to represent either a real live formal parameter decl or to
17642 represent just the type of some formal parameter position in some function
17643 type.
17645 Note that this routine is a bit unusual because its argument may be a
17646 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17647 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17648 node. If it's the former then this function is being called to output a
17649 DIE to represent a formal parameter object (or some inlining thereof). If
17650 it's the latter, then this function is only being called to output a
17651 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17652 argument type of some subprogram type.
17653 If EMIT_NAME_P is true, name and source coordinate attributes
17654 are emitted. */
17656 static dw_die_ref
17657 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17658 dw_die_ref context_die)
17660 tree node_or_origin = node ? node : origin;
17661 tree ultimate_origin;
17662 dw_die_ref parm_die
17663 = new_die (DW_TAG_formal_parameter, context_die, node);
17665 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17667 case tcc_declaration:
17668 ultimate_origin = decl_ultimate_origin (node_or_origin);
17669 if (node || ultimate_origin)
17670 origin = ultimate_origin;
17671 if (origin != NULL)
17672 add_abstract_origin_attribute (parm_die, origin);
17673 else if (emit_name_p)
17674 add_name_and_src_coords_attributes (parm_die, node);
17675 if (origin == NULL
17676 || (! DECL_ABSTRACT (node_or_origin)
17677 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17678 decl_function_context
17679 (node_or_origin))))
17681 tree type = TREE_TYPE (node_or_origin);
17682 if (decl_by_reference_p (node_or_origin))
17683 add_type_attribute (parm_die, TREE_TYPE (type),
17684 TYPE_UNQUALIFIED, context_die);
17685 else
17686 add_type_attribute (parm_die, type,
17687 decl_quals (node_or_origin),
17688 context_die);
17690 if (origin == NULL && DECL_ARTIFICIAL (node))
17691 add_AT_flag (parm_die, DW_AT_artificial, 1);
17693 if (node && node != origin)
17694 equate_decl_number_to_die (node, parm_die);
17695 if (! DECL_ABSTRACT (node_or_origin))
17696 add_location_or_const_value_attribute (parm_die, node_or_origin,
17697 node == NULL, DW_AT_location);
17699 break;
17701 case tcc_type:
17702 /* We were called with some kind of a ..._TYPE node. */
17703 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17704 context_die);
17705 break;
17707 default:
17708 gcc_unreachable ();
17711 return parm_die;
17714 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17715 children DW_TAG_formal_parameter DIEs representing the arguments of the
17716 parameter pack.
17718 PARM_PACK must be a function parameter pack.
17719 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17720 must point to the subsequent arguments of the function PACK_ARG belongs to.
17721 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17722 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17723 following the last one for which a DIE was generated. */
17725 static dw_die_ref
17726 gen_formal_parameter_pack_die (tree parm_pack,
17727 tree pack_arg,
17728 dw_die_ref subr_die,
17729 tree *next_arg)
17731 tree arg;
17732 dw_die_ref parm_pack_die;
17734 gcc_assert (parm_pack
17735 && lang_hooks.function_parameter_pack_p (parm_pack)
17736 && subr_die);
17738 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17739 add_src_coords_attributes (parm_pack_die, parm_pack);
17741 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17743 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17744 parm_pack))
17745 break;
17746 gen_formal_parameter_die (arg, NULL,
17747 false /* Don't emit name attribute. */,
17748 parm_pack_die);
17750 if (next_arg)
17751 *next_arg = arg;
17752 return parm_pack_die;
17755 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17756 at the end of an (ANSI prototyped) formal parameters list. */
17758 static void
17759 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17761 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17764 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17765 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17766 parameters as specified in some function type specification (except for
17767 those which appear as part of a function *definition*). */
17769 static void
17770 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17772 tree link;
17773 tree formal_type = NULL;
17774 tree first_parm_type;
17775 tree arg;
17777 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17779 arg = DECL_ARGUMENTS (function_or_method_type);
17780 function_or_method_type = TREE_TYPE (function_or_method_type);
17782 else
17783 arg = NULL_TREE;
17785 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17787 /* Make our first pass over the list of formal parameter types and output a
17788 DW_TAG_formal_parameter DIE for each one. */
17789 for (link = first_parm_type; link; )
17791 dw_die_ref parm_die;
17793 formal_type = TREE_VALUE (link);
17794 if (formal_type == void_type_node)
17795 break;
17797 /* Output a (nameless) DIE to represent the formal parameter itself. */
17798 parm_die = gen_formal_parameter_die (formal_type, NULL,
17799 true /* Emit name attribute. */,
17800 context_die);
17801 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17802 && link == first_parm_type)
17804 add_AT_flag (parm_die, DW_AT_artificial, 1);
17805 if (dwarf_version >= 3 || !dwarf_strict)
17806 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17808 else if (arg && DECL_ARTIFICIAL (arg))
17809 add_AT_flag (parm_die, DW_AT_artificial, 1);
17811 link = TREE_CHAIN (link);
17812 if (arg)
17813 arg = DECL_CHAIN (arg);
17816 /* If this function type has an ellipsis, add a
17817 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17818 if (formal_type != void_type_node)
17819 gen_unspecified_parameters_die (function_or_method_type, context_die);
17821 /* Make our second (and final) pass over the list of formal parameter types
17822 and output DIEs to represent those types (as necessary). */
17823 for (link = TYPE_ARG_TYPES (function_or_method_type);
17824 link && TREE_VALUE (link);
17825 link = TREE_CHAIN (link))
17826 gen_type_die (TREE_VALUE (link), context_die);
17829 /* We want to generate the DIE for TYPE so that we can generate the
17830 die for MEMBER, which has been defined; we will need to refer back
17831 to the member declaration nested within TYPE. If we're trying to
17832 generate minimal debug info for TYPE, processing TYPE won't do the
17833 trick; we need to attach the member declaration by hand. */
17835 static void
17836 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17838 gen_type_die (type, context_die);
17840 /* If we're trying to avoid duplicate debug info, we may not have
17841 emitted the member decl for this function. Emit it now. */
17842 if (TYPE_STUB_DECL (type)
17843 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17844 && ! lookup_decl_die (member))
17846 dw_die_ref type_die;
17847 gcc_assert (!decl_ultimate_origin (member));
17849 push_decl_scope (type);
17850 type_die = lookup_type_die_strip_naming_typedef (type);
17851 if (TREE_CODE (member) == FUNCTION_DECL)
17852 gen_subprogram_die (member, type_die);
17853 else if (TREE_CODE (member) == FIELD_DECL)
17855 /* Ignore the nameless fields that are used to skip bits but handle
17856 C++ anonymous unions and structs. */
17857 if (DECL_NAME (member) != NULL_TREE
17858 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17859 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17861 gen_type_die (member_declared_type (member), type_die);
17862 gen_field_die (member, type_die);
17865 else
17866 gen_variable_die (member, NULL_TREE, type_die);
17868 pop_decl_scope ();
17872 /* Forward declare these functions, because they are mutually recursive
17873 with their set_block_* pairing functions. */
17874 static void set_decl_origin_self (tree);
17875 static void set_decl_abstract_flags (tree, int);
17877 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17878 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17879 that it points to the node itself, thus indicating that the node is its
17880 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17881 the given node is NULL, recursively descend the decl/block tree which
17882 it is the root of, and for each other ..._DECL or BLOCK node contained
17883 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17884 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17885 values to point to themselves. */
17887 static void
17888 set_block_origin_self (tree stmt)
17890 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17892 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17895 tree local_decl;
17897 for (local_decl = BLOCK_VARS (stmt);
17898 local_decl != NULL_TREE;
17899 local_decl = DECL_CHAIN (local_decl))
17900 if (! DECL_EXTERNAL (local_decl))
17901 set_decl_origin_self (local_decl); /* Potential recursion. */
17905 tree subblock;
17907 for (subblock = BLOCK_SUBBLOCKS (stmt);
17908 subblock != NULL_TREE;
17909 subblock = BLOCK_CHAIN (subblock))
17910 set_block_origin_self (subblock); /* Recurse. */
17915 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17916 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17917 node to so that it points to the node itself, thus indicating that the
17918 node represents its own (abstract) origin. Additionally, if the
17919 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17920 the decl/block tree of which the given node is the root of, and for
17921 each other ..._DECL or BLOCK node contained therein whose
17922 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17923 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17924 point to themselves. */
17926 static void
17927 set_decl_origin_self (tree decl)
17929 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17931 DECL_ABSTRACT_ORIGIN (decl) = decl;
17932 if (TREE_CODE (decl) == FUNCTION_DECL)
17934 tree arg;
17936 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17937 DECL_ABSTRACT_ORIGIN (arg) = arg;
17938 if (DECL_INITIAL (decl) != NULL_TREE
17939 && DECL_INITIAL (decl) != error_mark_node)
17940 set_block_origin_self (DECL_INITIAL (decl));
17945 /* Given a pointer to some BLOCK node, and a boolean value to set the
17946 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17947 the given block, and for all local decls and all local sub-blocks
17948 (recursively) which are contained therein. */
17950 static void
17951 set_block_abstract_flags (tree stmt, int setting)
17953 tree local_decl;
17954 tree subblock;
17955 unsigned int i;
17957 BLOCK_ABSTRACT (stmt) = setting;
17959 for (local_decl = BLOCK_VARS (stmt);
17960 local_decl != NULL_TREE;
17961 local_decl = DECL_CHAIN (local_decl))
17962 if (! DECL_EXTERNAL (local_decl))
17963 set_decl_abstract_flags (local_decl, setting);
17965 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17967 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17968 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17969 || TREE_CODE (local_decl) == PARM_DECL)
17970 set_decl_abstract_flags (local_decl, setting);
17973 for (subblock = BLOCK_SUBBLOCKS (stmt);
17974 subblock != NULL_TREE;
17975 subblock = BLOCK_CHAIN (subblock))
17976 set_block_abstract_flags (subblock, setting);
17979 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17980 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17981 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17982 set the abstract flags for all of the parameters, local vars, local
17983 blocks and sub-blocks (recursively) to the same setting. */
17985 static void
17986 set_decl_abstract_flags (tree decl, int setting)
17988 DECL_ABSTRACT (decl) = setting;
17989 if (TREE_CODE (decl) == FUNCTION_DECL)
17991 tree arg;
17993 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17994 DECL_ABSTRACT (arg) = setting;
17995 if (DECL_INITIAL (decl) != NULL_TREE
17996 && DECL_INITIAL (decl) != error_mark_node)
17997 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18001 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18002 may later generate inlined and/or out-of-line instances of. */
18004 static void
18005 dwarf2out_abstract_function (tree decl)
18007 dw_die_ref old_die;
18008 tree save_fn;
18009 tree context;
18010 int was_abstract;
18011 htab_t old_decl_loc_table;
18012 htab_t old_cached_dw_loc_list_table;
18013 int old_call_site_count, old_tail_call_site_count;
18014 struct call_arg_loc_node *old_call_arg_locations;
18016 /* Make sure we have the actual abstract inline, not a clone. */
18017 decl = DECL_ORIGIN (decl);
18019 old_die = lookup_decl_die (decl);
18020 if (old_die && get_AT (old_die, DW_AT_inline))
18021 /* We've already generated the abstract instance. */
18022 return;
18024 /* We can be called while recursively when seeing block defining inlined subroutine
18025 DIE. Be sure to not clobber the outer location table nor use it or we would
18026 get locations in abstract instantces. */
18027 old_decl_loc_table = decl_loc_table;
18028 decl_loc_table = NULL;
18029 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18030 cached_dw_loc_list_table = NULL;
18031 old_call_arg_locations = call_arg_locations;
18032 call_arg_locations = NULL;
18033 old_call_site_count = call_site_count;
18034 call_site_count = -1;
18035 old_tail_call_site_count = tail_call_site_count;
18036 tail_call_site_count = -1;
18038 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18039 we don't get confused by DECL_ABSTRACT. */
18040 if (debug_info_level > DINFO_LEVEL_TERSE)
18042 context = decl_class_context (decl);
18043 if (context)
18044 gen_type_die_for_member
18045 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18048 /* Pretend we've just finished compiling this function. */
18049 save_fn = current_function_decl;
18050 current_function_decl = decl;
18052 was_abstract = DECL_ABSTRACT (decl);
18053 set_decl_abstract_flags (decl, 1);
18054 dwarf2out_decl (decl);
18055 if (! was_abstract)
18056 set_decl_abstract_flags (decl, 0);
18058 current_function_decl = save_fn;
18059 decl_loc_table = old_decl_loc_table;
18060 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18061 call_arg_locations = old_call_arg_locations;
18062 call_site_count = old_call_site_count;
18063 tail_call_site_count = old_tail_call_site_count;
18066 /* Helper function of premark_used_types() which gets called through
18067 htab_traverse.
18069 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18070 marked as unused by prune_unused_types. */
18072 static int
18073 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18075 tree type;
18076 dw_die_ref die;
18078 type = (tree) *slot;
18079 die = lookup_type_die (type);
18080 if (die != NULL)
18081 die->die_perennial_p = 1;
18082 return 1;
18085 /* Helper function of premark_types_used_by_global_vars which gets called
18086 through htab_traverse.
18088 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18089 marked as unused by prune_unused_types. The DIE of the type is marked
18090 only if the global variable using the type will actually be emitted. */
18092 static int
18093 premark_types_used_by_global_vars_helper (void **slot,
18094 void *data ATTRIBUTE_UNUSED)
18096 struct types_used_by_vars_entry *entry;
18097 dw_die_ref die;
18099 entry = (struct types_used_by_vars_entry *) *slot;
18100 gcc_assert (entry->type != NULL
18101 && entry->var_decl != NULL);
18102 die = lookup_type_die (entry->type);
18103 if (die)
18105 /* Ask cgraph if the global variable really is to be emitted.
18106 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18107 varpool_node *node = varpool_node::get (entry->var_decl);
18108 if (node && node->definition)
18110 die->die_perennial_p = 1;
18111 /* Keep the parent DIEs as well. */
18112 while ((die = die->die_parent) && die->die_perennial_p == 0)
18113 die->die_perennial_p = 1;
18116 return 1;
18119 /* Mark all members of used_types_hash as perennial. */
18121 static void
18122 premark_used_types (struct function *fun)
18124 if (fun && fun->used_types_hash)
18125 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18128 /* Mark all members of types_used_by_vars_entry as perennial. */
18130 static void
18131 premark_types_used_by_global_vars (void)
18133 if (types_used_by_vars_hash)
18134 htab_traverse (types_used_by_vars_hash,
18135 premark_types_used_by_global_vars_helper, NULL);
18138 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18139 for CA_LOC call arg loc node. */
18141 static dw_die_ref
18142 gen_call_site_die (tree decl, dw_die_ref subr_die,
18143 struct call_arg_loc_node *ca_loc)
18145 dw_die_ref stmt_die = NULL, die;
18146 tree block = ca_loc->block;
18148 while (block
18149 && block != DECL_INITIAL (decl)
18150 && TREE_CODE (block) == BLOCK)
18152 if (block_map.length () > BLOCK_NUMBER (block))
18153 stmt_die = block_map[BLOCK_NUMBER (block)];
18154 if (stmt_die)
18155 break;
18156 block = BLOCK_SUPERCONTEXT (block);
18158 if (stmt_die == NULL)
18159 stmt_die = subr_die;
18160 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18161 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18162 if (ca_loc->tail_call_p)
18163 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18164 if (ca_loc->symbol_ref)
18166 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18167 if (tdie)
18168 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18169 else
18170 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18172 return die;
18175 /* Generate a DIE to represent a declared function (either file-scope or
18176 block-local). */
18178 static void
18179 gen_subprogram_die (tree decl, dw_die_ref context_die)
18181 tree origin = decl_ultimate_origin (decl);
18182 dw_die_ref subr_die;
18183 tree outer_scope;
18184 dw_die_ref old_die = lookup_decl_die (decl);
18185 int declaration = (current_function_decl != decl
18186 || class_or_namespace_scope_p (context_die));
18188 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18190 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18191 started to generate the abstract instance of an inline, decided to output
18192 its containing class, and proceeded to emit the declaration of the inline
18193 from the member list for the class. If so, DECLARATION takes priority;
18194 we'll get back to the abstract instance when done with the class. */
18196 /* The class-scope declaration DIE must be the primary DIE. */
18197 if (origin && declaration && class_or_namespace_scope_p (context_die))
18199 origin = NULL;
18200 gcc_assert (!old_die);
18203 /* Now that the C++ front end lazily declares artificial member fns, we
18204 might need to retrofit the declaration into its class. */
18205 if (!declaration && !origin && !old_die
18206 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18207 && !class_or_namespace_scope_p (context_die)
18208 && debug_info_level > DINFO_LEVEL_TERSE)
18209 old_die = force_decl_die (decl);
18211 if (origin != NULL)
18213 gcc_assert (!declaration || local_scope_p (context_die));
18215 /* Fixup die_parent for the abstract instance of a nested
18216 inline function. */
18217 if (old_die && old_die->die_parent == NULL)
18218 add_child_die (context_die, old_die);
18220 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18221 add_abstract_origin_attribute (subr_die, origin);
18222 /* This is where the actual code for a cloned function is.
18223 Let's emit linkage name attribute for it. This helps
18224 debuggers to e.g, set breakpoints into
18225 constructors/destructors when the user asks "break
18226 K::K". */
18227 add_linkage_name (subr_die, decl);
18229 else if (old_die)
18231 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18232 struct dwarf_file_data * file_index = lookup_filename (s.file);
18234 if (!get_AT_flag (old_die, DW_AT_declaration)
18235 /* We can have a normal definition following an inline one in the
18236 case of redefinition of GNU C extern inlines.
18237 It seems reasonable to use AT_specification in this case. */
18238 && !get_AT (old_die, DW_AT_inline))
18240 /* Detect and ignore this case, where we are trying to output
18241 something we have already output. */
18242 return;
18245 /* If the definition comes from the same place as the declaration,
18246 maybe use the old DIE. We always want the DIE for this function
18247 that has the *_pc attributes to be under comp_unit_die so the
18248 debugger can find it. We also need to do this for abstract
18249 instances of inlines, since the spec requires the out-of-line copy
18250 to have the same parent. For local class methods, this doesn't
18251 apply; we just use the old DIE. */
18252 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18253 && (DECL_ARTIFICIAL (decl)
18254 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18255 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18256 == (unsigned) s.line))))
18258 subr_die = old_die;
18260 /* Clear out the declaration attribute and the formal parameters.
18261 Do not remove all children, because it is possible that this
18262 declaration die was forced using force_decl_die(). In such
18263 cases die that forced declaration die (e.g. TAG_imported_module)
18264 is one of the children that we do not want to remove. */
18265 remove_AT (subr_die, DW_AT_declaration);
18266 remove_AT (subr_die, DW_AT_object_pointer);
18267 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18269 else
18271 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18272 add_AT_specification (subr_die, old_die);
18273 add_pubname (decl, subr_die);
18274 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18275 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18276 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18277 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18279 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18280 emit the real type on the definition die. */
18281 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18283 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18284 if (die == auto_die || die == decltype_auto_die)
18285 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18286 TYPE_UNQUALIFIED, context_die);
18290 else
18292 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18294 if (TREE_PUBLIC (decl))
18295 add_AT_flag (subr_die, DW_AT_external, 1);
18297 add_name_and_src_coords_attributes (subr_die, decl);
18298 add_pubname (decl, subr_die);
18299 if (debug_info_level > DINFO_LEVEL_TERSE)
18301 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18302 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18303 TYPE_UNQUALIFIED, context_die);
18306 add_pure_or_virtual_attribute (subr_die, decl);
18307 if (DECL_ARTIFICIAL (decl))
18308 add_AT_flag (subr_die, DW_AT_artificial, 1);
18310 add_accessibility_attribute (subr_die, decl);
18313 if (declaration)
18315 if (!old_die || !get_AT (old_die, DW_AT_inline))
18317 add_AT_flag (subr_die, DW_AT_declaration, 1);
18319 /* If this is an explicit function declaration then generate
18320 a DW_AT_explicit attribute. */
18321 if (lang_hooks.decls.function_decl_explicit_p (decl)
18322 && (dwarf_version >= 3 || !dwarf_strict))
18323 add_AT_flag (subr_die, DW_AT_explicit, 1);
18325 /* The first time we see a member function, it is in the context of
18326 the class to which it belongs. We make sure of this by emitting
18327 the class first. The next time is the definition, which is
18328 handled above. The two may come from the same source text.
18330 Note that force_decl_die() forces function declaration die. It is
18331 later reused to represent definition. */
18332 equate_decl_number_to_die (decl, subr_die);
18335 else if (DECL_ABSTRACT (decl))
18337 if (DECL_DECLARED_INLINE_P (decl))
18339 if (cgraph_function_possibly_inlined_p (decl))
18340 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18341 else
18342 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18344 else
18346 if (cgraph_function_possibly_inlined_p (decl))
18347 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18348 else
18349 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18352 if (DECL_DECLARED_INLINE_P (decl)
18353 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18354 add_AT_flag (subr_die, DW_AT_artificial, 1);
18356 equate_decl_number_to_die (decl, subr_die);
18358 else if (!DECL_EXTERNAL (decl))
18360 HOST_WIDE_INT cfa_fb_offset;
18361 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18363 if (!old_die || !get_AT (old_die, DW_AT_inline))
18364 equate_decl_number_to_die (decl, subr_die);
18366 gcc_checking_assert (fun);
18367 if (!flag_reorder_blocks_and_partition)
18369 dw_fde_ref fde = fun->fde;
18370 if (fde->dw_fde_begin)
18372 /* We have already generated the labels. */
18373 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18374 fde->dw_fde_end, false);
18376 else
18378 /* Create start/end labels and add the range. */
18379 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18380 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18381 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18382 current_function_funcdef_no);
18383 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18384 current_function_funcdef_no);
18385 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18386 false);
18389 #if VMS_DEBUGGING_INFO
18390 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18391 Section 2.3 Prologue and Epilogue Attributes:
18392 When a breakpoint is set on entry to a function, it is generally
18393 desirable for execution to be suspended, not on the very first
18394 instruction of the function, but rather at a point after the
18395 function's frame has been set up, after any language defined local
18396 declaration processing has been completed, and before execution of
18397 the first statement of the function begins. Debuggers generally
18398 cannot properly determine where this point is. Similarly for a
18399 breakpoint set on exit from a function. The prologue and epilogue
18400 attributes allow a compiler to communicate the location(s) to use. */
18403 if (fde->dw_fde_vms_end_prologue)
18404 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18405 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18407 if (fde->dw_fde_vms_begin_epilogue)
18408 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18409 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18411 #endif
18414 else
18416 /* Generate pubnames entries for the split function code ranges. */
18417 dw_fde_ref fde = fun->fde;
18419 if (fde->dw_fde_second_begin)
18421 if (dwarf_version >= 3 || !dwarf_strict)
18423 /* We should use ranges for non-contiguous code section
18424 addresses. Use the actual code range for the initial
18425 section, since the HOT/COLD labels might precede an
18426 alignment offset. */
18427 bool range_list_added = false;
18428 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18429 fde->dw_fde_end, &range_list_added,
18430 false);
18431 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18432 fde->dw_fde_second_end,
18433 &range_list_added, false);
18434 if (range_list_added)
18435 add_ranges (NULL);
18437 else
18439 /* There is no real support in DW2 for this .. so we make
18440 a work-around. First, emit the pub name for the segment
18441 containing the function label. Then make and emit a
18442 simplified subprogram DIE for the second segment with the
18443 name pre-fixed by __hot/cold_sect_of_. We use the same
18444 linkage name for the second die so that gdb will find both
18445 sections when given "b foo". */
18446 const char *name = NULL;
18447 tree decl_name = DECL_NAME (decl);
18448 dw_die_ref seg_die;
18450 /* Do the 'primary' section. */
18451 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18452 fde->dw_fde_end, false);
18454 /* Build a minimal DIE for the secondary section. */
18455 seg_die = new_die (DW_TAG_subprogram,
18456 subr_die->die_parent, decl);
18458 if (TREE_PUBLIC (decl))
18459 add_AT_flag (seg_die, DW_AT_external, 1);
18461 if (decl_name != NULL
18462 && IDENTIFIER_POINTER (decl_name) != NULL)
18464 name = dwarf2_name (decl, 1);
18465 if (! DECL_ARTIFICIAL (decl))
18466 add_src_coords_attributes (seg_die, decl);
18468 add_linkage_name (seg_die, decl);
18470 gcc_assert (name != NULL);
18471 add_pure_or_virtual_attribute (seg_die, decl);
18472 if (DECL_ARTIFICIAL (decl))
18473 add_AT_flag (seg_die, DW_AT_artificial, 1);
18475 name = concat ("__second_sect_of_", name, NULL);
18476 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18477 fde->dw_fde_second_end, false);
18478 add_name_attribute (seg_die, name);
18479 if (want_pubnames ())
18480 add_pubname_string (name, seg_die);
18483 else
18484 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18485 false);
18488 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18490 /* We define the "frame base" as the function's CFA. This is more
18491 convenient for several reasons: (1) It's stable across the prologue
18492 and epilogue, which makes it better than just a frame pointer,
18493 (2) With dwarf3, there exists a one-byte encoding that allows us
18494 to reference the .debug_frame data by proxy, but failing that,
18495 (3) We can at least reuse the code inspection and interpretation
18496 code that determines the CFA position at various points in the
18497 function. */
18498 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18500 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18501 add_AT_loc (subr_die, DW_AT_frame_base, op);
18503 else
18505 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18506 if (list->dw_loc_next)
18507 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18508 else
18509 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18512 /* Compute a displacement from the "steady-state frame pointer" to
18513 the CFA. The former is what all stack slots and argument slots
18514 will reference in the rtl; the latter is what we've told the
18515 debugger about. We'll need to adjust all frame_base references
18516 by this displacement. */
18517 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18519 if (fun->static_chain_decl)
18520 add_AT_location_description (subr_die, DW_AT_static_link,
18521 loc_list_from_tree (fun->static_chain_decl, 2));
18524 /* Generate child dies for template paramaters. */
18525 if (debug_info_level > DINFO_LEVEL_TERSE)
18526 gen_generic_params_dies (decl);
18528 /* Now output descriptions of the arguments for this function. This gets
18529 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18530 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18531 `...' at the end of the formal parameter list. In order to find out if
18532 there was a trailing ellipsis or not, we must instead look at the type
18533 associated with the FUNCTION_DECL. This will be a node of type
18534 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18535 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18536 an ellipsis at the end. */
18538 /* In the case where we are describing a mere function declaration, all we
18539 need to do here (and all we *can* do here) is to describe the *types* of
18540 its formal parameters. */
18541 if (debug_info_level <= DINFO_LEVEL_TERSE)
18543 else if (declaration)
18544 gen_formal_types_die (decl, subr_die);
18545 else
18547 /* Generate DIEs to represent all known formal parameters. */
18548 tree parm = DECL_ARGUMENTS (decl);
18549 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18550 tree generic_decl_parm = generic_decl
18551 ? DECL_ARGUMENTS (generic_decl)
18552 : NULL;
18554 /* Now we want to walk the list of parameters of the function and
18555 emit their relevant DIEs.
18557 We consider the case of DECL being an instance of a generic function
18558 as well as it being a normal function.
18560 If DECL is an instance of a generic function we walk the
18561 parameters of the generic function declaration _and_ the parameters of
18562 DECL itself. This is useful because we want to emit specific DIEs for
18563 function parameter packs and those are declared as part of the
18564 generic function declaration. In that particular case,
18565 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18566 That DIE has children DIEs representing the set of arguments
18567 of the pack. Note that the set of pack arguments can be empty.
18568 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18569 children DIE.
18571 Otherwise, we just consider the parameters of DECL. */
18572 while (generic_decl_parm || parm)
18574 if (generic_decl_parm
18575 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18576 gen_formal_parameter_pack_die (generic_decl_parm,
18577 parm, subr_die,
18578 &parm);
18579 else if (parm)
18581 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18583 if (parm == DECL_ARGUMENTS (decl)
18584 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18585 && parm_die
18586 && (dwarf_version >= 3 || !dwarf_strict))
18587 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18589 parm = DECL_CHAIN (parm);
18592 if (generic_decl_parm)
18593 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18596 /* Decide whether we need an unspecified_parameters DIE at the end.
18597 There are 2 more cases to do this for: 1) the ansi ... declaration -
18598 this is detectable when the end of the arg list is not a
18599 void_type_node 2) an unprototyped function declaration (not a
18600 definition). This just means that we have no info about the
18601 parameters at all. */
18602 if (prototype_p (TREE_TYPE (decl)))
18604 /* This is the prototyped case, check for.... */
18605 if (stdarg_p (TREE_TYPE (decl)))
18606 gen_unspecified_parameters_die (decl, subr_die);
18608 else if (DECL_INITIAL (decl) == NULL_TREE)
18609 gen_unspecified_parameters_die (decl, subr_die);
18612 /* Output Dwarf info for all of the stuff within the body of the function
18613 (if it has one - it may be just a declaration). */
18614 outer_scope = DECL_INITIAL (decl);
18616 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18617 a function. This BLOCK actually represents the outermost binding contour
18618 for the function, i.e. the contour in which the function's formal
18619 parameters and labels get declared. Curiously, it appears that the front
18620 end doesn't actually put the PARM_DECL nodes for the current function onto
18621 the BLOCK_VARS list for this outer scope, but are strung off of the
18622 DECL_ARGUMENTS list for the function instead.
18624 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18625 the LABEL_DECL nodes for the function however, and we output DWARF info
18626 for those in decls_for_scope. Just within the `outer_scope' there will be
18627 a BLOCK node representing the function's outermost pair of curly braces,
18628 and any blocks used for the base and member initializers of a C++
18629 constructor function. */
18630 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18632 int call_site_note_count = 0;
18633 int tail_call_site_note_count = 0;
18635 /* Emit a DW_TAG_variable DIE for a named return value. */
18636 if (DECL_NAME (DECL_RESULT (decl)))
18637 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18639 current_function_has_inlines = 0;
18640 decls_for_scope (outer_scope, subr_die, 0);
18642 if (call_arg_locations && !dwarf_strict)
18644 struct call_arg_loc_node *ca_loc;
18645 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18647 dw_die_ref die = NULL;
18648 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18649 rtx arg, next_arg;
18651 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18652 arg; arg = next_arg)
18654 dw_loc_descr_ref reg, val;
18655 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18656 dw_die_ref cdie, tdie = NULL;
18658 next_arg = XEXP (arg, 1);
18659 if (REG_P (XEXP (XEXP (arg, 0), 0))
18660 && next_arg
18661 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18662 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18663 && REGNO (XEXP (XEXP (arg, 0), 0))
18664 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18665 next_arg = XEXP (next_arg, 1);
18666 if (mode == VOIDmode)
18668 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18669 if (mode == VOIDmode)
18670 mode = GET_MODE (XEXP (arg, 0));
18672 if (mode == VOIDmode || mode == BLKmode)
18673 continue;
18674 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18676 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18677 tloc = XEXP (XEXP (arg, 0), 1);
18678 continue;
18680 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18681 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18683 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18684 tlocc = XEXP (XEXP (arg, 0), 1);
18685 continue;
18687 reg = NULL;
18688 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18689 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18690 VAR_INIT_STATUS_INITIALIZED);
18691 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18693 rtx mem = XEXP (XEXP (arg, 0), 0);
18694 reg = mem_loc_descriptor (XEXP (mem, 0),
18695 get_address_mode (mem),
18696 GET_MODE (mem),
18697 VAR_INIT_STATUS_INITIALIZED);
18699 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18700 == DEBUG_PARAMETER_REF)
18702 tree tdecl
18703 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18704 tdie = lookup_decl_die (tdecl);
18705 if (tdie == NULL)
18706 continue;
18708 else
18709 continue;
18710 if (reg == NULL
18711 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18712 != DEBUG_PARAMETER_REF)
18713 continue;
18714 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18715 VOIDmode,
18716 VAR_INIT_STATUS_INITIALIZED);
18717 if (val == NULL)
18718 continue;
18719 if (die == NULL)
18720 die = gen_call_site_die (decl, subr_die, ca_loc);
18721 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18722 NULL_TREE);
18723 if (reg != NULL)
18724 add_AT_loc (cdie, DW_AT_location, reg);
18725 else if (tdie != NULL)
18726 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18727 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18728 if (next_arg != XEXP (arg, 1))
18730 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18731 if (mode == VOIDmode)
18732 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18733 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18734 0), 1),
18735 mode, VOIDmode,
18736 VAR_INIT_STATUS_INITIALIZED);
18737 if (val != NULL)
18738 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18741 if (die == NULL
18742 && (ca_loc->symbol_ref || tloc))
18743 die = gen_call_site_die (decl, subr_die, ca_loc);
18744 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18746 dw_loc_descr_ref tval = NULL;
18748 if (tloc != NULL_RTX)
18749 tval = mem_loc_descriptor (tloc,
18750 GET_MODE (tloc) == VOIDmode
18751 ? Pmode : GET_MODE (tloc),
18752 VOIDmode,
18753 VAR_INIT_STATUS_INITIALIZED);
18754 if (tval)
18755 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18756 else if (tlocc != NULL_RTX)
18758 tval = mem_loc_descriptor (tlocc,
18759 GET_MODE (tlocc) == VOIDmode
18760 ? Pmode : GET_MODE (tlocc),
18761 VOIDmode,
18762 VAR_INIT_STATUS_INITIALIZED);
18763 if (tval)
18764 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18765 tval);
18768 if (die != NULL)
18770 call_site_note_count++;
18771 if (ca_loc->tail_call_p)
18772 tail_call_site_note_count++;
18776 call_arg_locations = NULL;
18777 call_arg_loc_last = NULL;
18778 if (tail_call_site_count >= 0
18779 && tail_call_site_count == tail_call_site_note_count
18780 && !dwarf_strict)
18782 if (call_site_count >= 0
18783 && call_site_count == call_site_note_count)
18784 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18785 else
18786 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18788 call_site_count = -1;
18789 tail_call_site_count = -1;
18792 if (subr_die != old_die)
18793 /* Add the calling convention attribute if requested. */
18794 add_calling_convention_attribute (subr_die, decl);
18797 /* Returns a hash value for X (which really is a die_struct). */
18799 static hashval_t
18800 common_block_die_table_hash (const void *x)
18802 const_dw_die_ref d = (const_dw_die_ref) x;
18803 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18806 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18807 as decl_id and die_parent of die_struct Y. */
18809 static int
18810 common_block_die_table_eq (const void *x, const void *y)
18812 const_dw_die_ref d = (const_dw_die_ref) x;
18813 const_dw_die_ref e = (const_dw_die_ref) y;
18814 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18817 /* Generate a DIE to represent a declared data object.
18818 Either DECL or ORIGIN must be non-null. */
18820 static void
18821 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18823 HOST_WIDE_INT off = 0;
18824 tree com_decl;
18825 tree decl_or_origin = decl ? decl : origin;
18826 tree ultimate_origin;
18827 dw_die_ref var_die;
18828 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18829 dw_die_ref origin_die;
18830 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18831 || class_or_namespace_scope_p (context_die));
18832 bool specialization_p = false;
18834 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18835 if (decl || ultimate_origin)
18836 origin = ultimate_origin;
18837 com_decl = fortran_common (decl_or_origin, &off);
18839 /* Symbol in common gets emitted as a child of the common block, in the form
18840 of a data member. */
18841 if (com_decl)
18843 dw_die_ref com_die;
18844 dw_loc_list_ref loc;
18845 die_node com_die_arg;
18847 var_die = lookup_decl_die (decl_or_origin);
18848 if (var_die)
18850 if (get_AT (var_die, DW_AT_location) == NULL)
18852 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18853 if (loc)
18855 if (off)
18857 /* Optimize the common case. */
18858 if (single_element_loc_list_p (loc)
18859 && loc->expr->dw_loc_opc == DW_OP_addr
18860 && loc->expr->dw_loc_next == NULL
18861 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18862 == SYMBOL_REF)
18864 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18865 loc->expr->dw_loc_oprnd1.v.val_addr
18866 = plus_constant (GET_MODE (x), x , off);
18868 else
18869 loc_list_plus_const (loc, off);
18871 add_AT_location_description (var_die, DW_AT_location, loc);
18872 remove_AT (var_die, DW_AT_declaration);
18875 return;
18878 if (common_block_die_table == NULL)
18879 common_block_die_table
18880 = htab_create_ggc (10, common_block_die_table_hash,
18881 common_block_die_table_eq, NULL);
18883 com_die_arg.decl_id = DECL_UID (com_decl);
18884 com_die_arg.die_parent = context_die;
18885 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18886 loc = loc_list_from_tree (com_decl, 2);
18887 if (com_die == NULL)
18889 const char *cnam
18890 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18891 void **slot;
18893 com_die = new_die (DW_TAG_common_block, context_die, decl);
18894 add_name_and_src_coords_attributes (com_die, com_decl);
18895 if (loc)
18897 add_AT_location_description (com_die, DW_AT_location, loc);
18898 /* Avoid sharing the same loc descriptor between
18899 DW_TAG_common_block and DW_TAG_variable. */
18900 loc = loc_list_from_tree (com_decl, 2);
18902 else if (DECL_EXTERNAL (decl))
18903 add_AT_flag (com_die, DW_AT_declaration, 1);
18904 if (want_pubnames ())
18905 add_pubname_string (cnam, com_die); /* ??? needed? */
18906 com_die->decl_id = DECL_UID (com_decl);
18907 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18908 *slot = (void *) com_die;
18910 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18912 add_AT_location_description (com_die, DW_AT_location, loc);
18913 loc = loc_list_from_tree (com_decl, 2);
18914 remove_AT (com_die, DW_AT_declaration);
18916 var_die = new_die (DW_TAG_variable, com_die, decl);
18917 add_name_and_src_coords_attributes (var_die, decl);
18918 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18919 context_die);
18920 add_AT_flag (var_die, DW_AT_external, 1);
18921 if (loc)
18923 if (off)
18925 /* Optimize the common case. */
18926 if (single_element_loc_list_p (loc)
18927 && loc->expr->dw_loc_opc == DW_OP_addr
18928 && loc->expr->dw_loc_next == NULL
18929 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18931 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18932 loc->expr->dw_loc_oprnd1.v.val_addr
18933 = plus_constant (GET_MODE (x), x, off);
18935 else
18936 loc_list_plus_const (loc, off);
18938 add_AT_location_description (var_die, DW_AT_location, loc);
18940 else if (DECL_EXTERNAL (decl))
18941 add_AT_flag (var_die, DW_AT_declaration, 1);
18942 equate_decl_number_to_die (decl, var_die);
18943 return;
18946 /* If the compiler emitted a definition for the DECL declaration
18947 and if we already emitted a DIE for it, don't emit a second
18948 DIE for it again. Allow re-declarations of DECLs that are
18949 inside functions, though. */
18950 if (old_die && declaration && !local_scope_p (context_die))
18951 return;
18953 /* For static data members, the declaration in the class is supposed
18954 to have DW_TAG_member tag; the specification should still be
18955 DW_TAG_variable referencing the DW_TAG_member DIE. */
18956 if (declaration && class_scope_p (context_die))
18957 var_die = new_die (DW_TAG_member, context_die, decl);
18958 else
18959 var_die = new_die (DW_TAG_variable, context_die, decl);
18961 origin_die = NULL;
18962 if (origin != NULL)
18963 origin_die = add_abstract_origin_attribute (var_die, origin);
18965 /* Loop unrolling can create multiple blocks that refer to the same
18966 static variable, so we must test for the DW_AT_declaration flag.
18968 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18969 copy decls and set the DECL_ABSTRACT flag on them instead of
18970 sharing them.
18972 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18974 ??? The declare_in_namespace support causes us to get two DIEs for one
18975 variable, both of which are declarations. We want to avoid considering
18976 one to be a specification, so we must test that this DIE is not a
18977 declaration. */
18978 else if (old_die && TREE_STATIC (decl) && ! declaration
18979 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18981 /* This is a definition of a C++ class level static. */
18982 add_AT_specification (var_die, old_die);
18983 specialization_p = true;
18984 if (DECL_NAME (decl))
18986 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18987 struct dwarf_file_data * file_index = lookup_filename (s.file);
18989 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18990 add_AT_file (var_die, DW_AT_decl_file, file_index);
18992 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18993 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18995 if (old_die->die_tag == DW_TAG_member)
18996 add_linkage_name (var_die, decl);
18999 else
19000 add_name_and_src_coords_attributes (var_die, decl);
19002 if ((origin == NULL && !specialization_p)
19003 || (origin != NULL
19004 && !DECL_ABSTRACT (decl_or_origin)
19005 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19006 decl_function_context
19007 (decl_or_origin))))
19009 tree type = TREE_TYPE (decl_or_origin);
19011 if (decl_by_reference_p (decl_or_origin))
19012 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19013 context_die);
19014 else
19015 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19016 context_die);
19019 if (origin == NULL && !specialization_p)
19021 if (TREE_PUBLIC (decl))
19022 add_AT_flag (var_die, DW_AT_external, 1);
19024 if (DECL_ARTIFICIAL (decl))
19025 add_AT_flag (var_die, DW_AT_artificial, 1);
19027 add_accessibility_attribute (var_die, decl);
19030 if (declaration)
19031 add_AT_flag (var_die, DW_AT_declaration, 1);
19033 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19034 equate_decl_number_to_die (decl, var_die);
19036 if (! declaration
19037 && (! DECL_ABSTRACT (decl_or_origin)
19038 /* Local static vars are shared between all clones/inlines,
19039 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19040 already set. */
19041 || (TREE_CODE (decl_or_origin) == VAR_DECL
19042 && TREE_STATIC (decl_or_origin)
19043 && DECL_RTL_SET_P (decl_or_origin)))
19044 /* When abstract origin already has DW_AT_location attribute, no need
19045 to add it again. */
19046 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19048 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19049 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19050 defer_location (decl_or_origin, var_die);
19051 else
19052 add_location_or_const_value_attribute (var_die, decl_or_origin,
19053 decl == NULL, DW_AT_location);
19054 add_pubname (decl_or_origin, var_die);
19056 else
19057 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19060 /* Generate a DIE to represent a named constant. */
19062 static void
19063 gen_const_die (tree decl, dw_die_ref context_die)
19065 dw_die_ref const_die;
19066 tree type = TREE_TYPE (decl);
19068 const_die = new_die (DW_TAG_constant, context_die, decl);
19069 add_name_and_src_coords_attributes (const_die, decl);
19070 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19071 if (TREE_PUBLIC (decl))
19072 add_AT_flag (const_die, DW_AT_external, 1);
19073 if (DECL_ARTIFICIAL (decl))
19074 add_AT_flag (const_die, DW_AT_artificial, 1);
19075 tree_add_const_value_attribute_for_decl (const_die, decl);
19078 /* Generate a DIE to represent a label identifier. */
19080 static void
19081 gen_label_die (tree decl, dw_die_ref context_die)
19083 tree origin = decl_ultimate_origin (decl);
19084 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19085 rtx insn;
19086 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19088 if (origin != NULL)
19089 add_abstract_origin_attribute (lbl_die, origin);
19090 else
19091 add_name_and_src_coords_attributes (lbl_die, decl);
19093 if (DECL_ABSTRACT (decl))
19094 equate_decl_number_to_die (decl, lbl_die);
19095 else
19097 insn = DECL_RTL_IF_SET (decl);
19099 /* Deleted labels are programmer specified labels which have been
19100 eliminated because of various optimizations. We still emit them
19101 here so that it is possible to put breakpoints on them. */
19102 if (insn
19103 && (LABEL_P (insn)
19104 || ((NOTE_P (insn)
19105 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19107 /* When optimization is enabled (via -O) some parts of the compiler
19108 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19109 represent source-level labels which were explicitly declared by
19110 the user. This really shouldn't be happening though, so catch
19111 it if it ever does happen. */
19112 gcc_assert (!INSN_DELETED_P (insn));
19114 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19115 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19117 else if (insn
19118 && NOTE_P (insn)
19119 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19120 && CODE_LABEL_NUMBER (insn) != -1)
19122 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19123 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19128 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19129 attributes to the DIE for a block STMT, to describe where the inlined
19130 function was called from. This is similar to add_src_coords_attributes. */
19132 static inline void
19133 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19135 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19137 if (dwarf_version >= 3 || !dwarf_strict)
19139 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19140 add_AT_unsigned (die, DW_AT_call_line, s.line);
19145 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19146 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19148 static inline void
19149 add_high_low_attributes (tree stmt, dw_die_ref die)
19151 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19153 if (BLOCK_FRAGMENT_CHAIN (stmt)
19154 && (dwarf_version >= 3 || !dwarf_strict))
19156 tree chain, superblock = NULL_TREE;
19157 dw_die_ref pdie;
19158 dw_attr_ref attr = NULL;
19160 if (inlined_function_outer_scope_p (stmt))
19162 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19163 BLOCK_NUMBER (stmt));
19164 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19167 /* Optimize duplicate .debug_ranges lists or even tails of
19168 lists. If this BLOCK has same ranges as its supercontext,
19169 lookup DW_AT_ranges attribute in the supercontext (and
19170 recursively so), verify that the ranges_table contains the
19171 right values and use it instead of adding a new .debug_range. */
19172 for (chain = stmt, pdie = die;
19173 BLOCK_SAME_RANGE (chain);
19174 chain = BLOCK_SUPERCONTEXT (chain))
19176 dw_attr_ref new_attr;
19178 pdie = pdie->die_parent;
19179 if (pdie == NULL)
19180 break;
19181 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19182 break;
19183 new_attr = get_AT (pdie, DW_AT_ranges);
19184 if (new_attr == NULL
19185 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19186 break;
19187 attr = new_attr;
19188 superblock = BLOCK_SUPERCONTEXT (chain);
19190 if (attr != NULL
19191 && (ranges_table[attr->dw_attr_val.v.val_offset
19192 / 2 / DWARF2_ADDR_SIZE].num
19193 == BLOCK_NUMBER (superblock))
19194 && BLOCK_FRAGMENT_CHAIN (superblock))
19196 unsigned long off = attr->dw_attr_val.v.val_offset
19197 / 2 / DWARF2_ADDR_SIZE;
19198 unsigned long supercnt = 0, thiscnt = 0;
19199 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19200 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19202 ++supercnt;
19203 gcc_checking_assert (ranges_table[off + supercnt].num
19204 == BLOCK_NUMBER (chain));
19206 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19207 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19208 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19209 ++thiscnt;
19210 gcc_assert (supercnt >= thiscnt);
19211 add_AT_range_list (die, DW_AT_ranges,
19212 ((off + supercnt - thiscnt)
19213 * 2 * DWARF2_ADDR_SIZE),
19214 false);
19215 return;
19218 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19220 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19223 add_ranges (chain);
19224 chain = BLOCK_FRAGMENT_CHAIN (chain);
19226 while (chain);
19227 add_ranges (NULL);
19229 else
19231 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19232 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19233 BLOCK_NUMBER (stmt));
19234 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19235 BLOCK_NUMBER (stmt));
19236 add_AT_low_high_pc (die, label, label_high, false);
19240 /* Generate a DIE for a lexical block. */
19242 static void
19243 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19245 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19247 if (call_arg_locations)
19249 if (block_map.length () <= BLOCK_NUMBER (stmt))
19250 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19251 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19254 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19255 add_high_low_attributes (stmt, stmt_die);
19257 decls_for_scope (stmt, stmt_die, depth);
19260 /* Generate a DIE for an inlined subprogram. */
19262 static void
19263 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19265 tree decl;
19267 /* The instance of function that is effectively being inlined shall not
19268 be abstract. */
19269 gcc_assert (! BLOCK_ABSTRACT (stmt));
19271 decl = block_ultimate_origin (stmt);
19273 /* Emit info for the abstract instance first, if we haven't yet. We
19274 must emit this even if the block is abstract, otherwise when we
19275 emit the block below (or elsewhere), we may end up trying to emit
19276 a die whose origin die hasn't been emitted, and crashing. */
19277 dwarf2out_abstract_function (decl);
19279 if (! BLOCK_ABSTRACT (stmt))
19281 dw_die_ref subr_die
19282 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19284 if (call_arg_locations)
19286 if (block_map.length () <= BLOCK_NUMBER (stmt))
19287 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19288 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19290 add_abstract_origin_attribute (subr_die, decl);
19291 if (TREE_ASM_WRITTEN (stmt))
19292 add_high_low_attributes (stmt, subr_die);
19293 add_call_src_coords_attributes (stmt, subr_die);
19295 decls_for_scope (stmt, subr_die, depth);
19296 current_function_has_inlines = 1;
19300 /* Generate a DIE for a field in a record, or structure. */
19302 static void
19303 gen_field_die (tree decl, dw_die_ref context_die)
19305 dw_die_ref decl_die;
19307 if (TREE_TYPE (decl) == error_mark_node)
19308 return;
19310 decl_die = new_die (DW_TAG_member, context_die, decl);
19311 add_name_and_src_coords_attributes (decl_die, decl);
19312 add_type_attribute (decl_die, member_declared_type (decl),
19313 decl_quals (decl), context_die);
19315 if (DECL_BIT_FIELD_TYPE (decl))
19317 add_byte_size_attribute (decl_die, decl);
19318 add_bit_size_attribute (decl_die, decl);
19319 add_bit_offset_attribute (decl_die, decl);
19322 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19323 add_data_member_location_attribute (decl_die, decl);
19325 if (DECL_ARTIFICIAL (decl))
19326 add_AT_flag (decl_die, DW_AT_artificial, 1);
19328 add_accessibility_attribute (decl_die, decl);
19330 /* Equate decl number to die, so that we can look up this decl later on. */
19331 equate_decl_number_to_die (decl, decl_die);
19334 #if 0
19335 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19336 Use modified_type_die instead.
19337 We keep this code here just in case these types of DIEs may be needed to
19338 represent certain things in other languages (e.g. Pascal) someday. */
19340 static void
19341 gen_pointer_type_die (tree type, dw_die_ref context_die)
19343 dw_die_ref ptr_die
19344 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19346 equate_type_number_to_die (type, ptr_die);
19347 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19348 context_die);
19349 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19352 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19353 Use modified_type_die instead.
19354 We keep this code here just in case these types of DIEs may be needed to
19355 represent certain things in other languages (e.g. Pascal) someday. */
19357 static void
19358 gen_reference_type_die (tree type, dw_die_ref context_die)
19360 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19362 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19363 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19364 else
19365 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19367 equate_type_number_to_die (type, ref_die);
19368 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19369 context_die);
19370 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19372 #endif
19374 /* Generate a DIE for a pointer to a member type. */
19376 static void
19377 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19379 dw_die_ref ptr_die
19380 = new_die (DW_TAG_ptr_to_member_type,
19381 scope_die_for (type, context_die), type);
19383 equate_type_number_to_die (type, ptr_die);
19384 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19385 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19386 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19387 context_die);
19390 typedef const char *dchar_p; /* For DEF_VEC_P. */
19392 static char *producer_string;
19394 /* Return a heap allocated producer string including command line options
19395 if -grecord-gcc-switches. */
19397 static char *
19398 gen_producer_string (void)
19400 size_t j;
19401 auto_vec<dchar_p> switches;
19402 const char *language_string = lang_hooks.name;
19403 char *producer, *tail;
19404 const char *p;
19405 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19406 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19408 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19409 switch (save_decoded_options[j].opt_index)
19411 case OPT_o:
19412 case OPT_d:
19413 case OPT_dumpbase:
19414 case OPT_dumpdir:
19415 case OPT_auxbase:
19416 case OPT_auxbase_strip:
19417 case OPT_quiet:
19418 case OPT_version:
19419 case OPT_v:
19420 case OPT_w:
19421 case OPT_L:
19422 case OPT_D:
19423 case OPT_I:
19424 case OPT_U:
19425 case OPT_SPECIAL_unknown:
19426 case OPT_SPECIAL_ignore:
19427 case OPT_SPECIAL_program_name:
19428 case OPT_SPECIAL_input_file:
19429 case OPT_grecord_gcc_switches:
19430 case OPT_gno_record_gcc_switches:
19431 case OPT__output_pch_:
19432 case OPT_fdiagnostics_show_location_:
19433 case OPT_fdiagnostics_show_option:
19434 case OPT_fdiagnostics_show_caret:
19435 case OPT_fdiagnostics_color_:
19436 case OPT_fverbose_asm:
19437 case OPT____:
19438 case OPT__sysroot_:
19439 case OPT_nostdinc:
19440 case OPT_nostdinc__:
19441 /* Ignore these. */
19442 continue;
19443 default:
19444 if (cl_options[save_decoded_options[j].opt_index].flags
19445 & CL_NO_DWARF_RECORD)
19446 continue;
19447 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19448 == '-');
19449 switch (save_decoded_options[j].canonical_option[0][1])
19451 case 'M':
19452 case 'i':
19453 case 'W':
19454 continue;
19455 case 'f':
19456 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19457 "dump", 4) == 0)
19458 continue;
19459 break;
19460 default:
19461 break;
19463 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19464 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19465 break;
19468 producer = XNEWVEC (char, plen + 1 + len + 1);
19469 tail = producer;
19470 sprintf (tail, "%s %s", language_string, version_string);
19471 tail += plen;
19473 FOR_EACH_VEC_ELT (switches, j, p)
19475 len = strlen (p);
19476 *tail = ' ';
19477 memcpy (tail + 1, p, len);
19478 tail += len + 1;
19481 *tail = '\0';
19482 return producer;
19485 /* Generate the DIE for the compilation unit. */
19487 static dw_die_ref
19488 gen_compile_unit_die (const char *filename)
19490 dw_die_ref die;
19491 const char *language_string = lang_hooks.name;
19492 int language;
19494 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19496 if (filename)
19498 add_name_attribute (die, filename);
19499 /* Don't add cwd for <built-in>. */
19500 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19501 add_comp_dir_attribute (die);
19504 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19506 /* If our producer is LTO try to figure out a common language to use
19507 from the global list of translation units. */
19508 if (strcmp (language_string, "GNU GIMPLE") == 0)
19510 unsigned i;
19511 tree t;
19512 const char *common_lang = NULL;
19514 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19516 if (!TRANSLATION_UNIT_LANGUAGE (t))
19517 continue;
19518 if (!common_lang)
19519 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19520 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19522 else if (strncmp (common_lang, "GNU C", 5) == 0
19523 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19524 /* Mixing C and C++ is ok, use C++ in that case. */
19525 common_lang = "GNU C++";
19526 else
19528 /* Fall back to C. */
19529 common_lang = NULL;
19530 break;
19534 if (common_lang)
19535 language_string = common_lang;
19538 language = DW_LANG_C89;
19539 if (strcmp (language_string, "GNU C++") == 0)
19540 language = DW_LANG_C_plus_plus;
19541 else if (strcmp (language_string, "GNU F77") == 0)
19542 language = DW_LANG_Fortran77;
19543 else if (strcmp (language_string, "GNU Pascal") == 0)
19544 language = DW_LANG_Pascal83;
19545 else if (use_upc_dwarf2_extensions
19546 && (strcmp (language_string, "GNU UPC") == 0))
19547 language = DW_LANG_Upc;
19548 else if (dwarf_version >= 3 || !dwarf_strict)
19550 if (strcmp (language_string, "GNU Ada") == 0)
19551 language = DW_LANG_Ada95;
19552 else if (strcmp (language_string, "GNU Fortran") == 0)
19553 language = DW_LANG_Fortran95;
19554 else if (strcmp (language_string, "GNU Java") == 0)
19555 language = DW_LANG_Java;
19556 else if (strcmp (language_string, "GNU Objective-C") == 0)
19557 language = DW_LANG_ObjC;
19558 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19559 language = DW_LANG_ObjC_plus_plus;
19560 else if (dwarf_version >= 5 || !dwarf_strict)
19562 if (strcmp (language_string, "GNU Go") == 0)
19563 language = DW_LANG_Go;
19566 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19567 else if (strcmp (language_string, "GNU Fortran") == 0)
19568 language = DW_LANG_Fortran90;
19570 add_AT_unsigned (die, DW_AT_language, language);
19572 switch (language)
19574 case DW_LANG_Fortran77:
19575 case DW_LANG_Fortran90:
19576 case DW_LANG_Fortran95:
19577 /* Fortran has case insensitive identifiers and the front-end
19578 lowercases everything. */
19579 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19580 break;
19581 default:
19582 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19583 break;
19585 return die;
19588 /* Generate the DIE for a base class. */
19590 static void
19591 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19593 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19595 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19596 add_data_member_location_attribute (die, binfo);
19598 if (BINFO_VIRTUAL_P (binfo))
19599 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19601 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19602 children, otherwise the default is DW_ACCESS_public. In DWARF2
19603 the default has always been DW_ACCESS_private. */
19604 if (access == access_public_node)
19606 if (dwarf_version == 2
19607 || context_die->die_tag == DW_TAG_class_type)
19608 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19610 else if (access == access_protected_node)
19611 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19612 else if (dwarf_version > 2
19613 && context_die->die_tag != DW_TAG_class_type)
19614 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19617 /* Generate a DIE for a class member. */
19619 static void
19620 gen_member_die (tree type, dw_die_ref context_die)
19622 tree member;
19623 tree binfo = TYPE_BINFO (type);
19624 dw_die_ref child;
19626 /* If this is not an incomplete type, output descriptions of each of its
19627 members. Note that as we output the DIEs necessary to represent the
19628 members of this record or union type, we will also be trying to output
19629 DIEs to represent the *types* of those members. However the `type'
19630 function (above) will specifically avoid generating type DIEs for member
19631 types *within* the list of member DIEs for this (containing) type except
19632 for those types (of members) which are explicitly marked as also being
19633 members of this (containing) type themselves. The g++ front- end can
19634 force any given type to be treated as a member of some other (containing)
19635 type by setting the TYPE_CONTEXT of the given (member) type to point to
19636 the TREE node representing the appropriate (containing) type. */
19638 /* First output info about the base classes. */
19639 if (binfo)
19641 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19642 int i;
19643 tree base;
19645 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19646 gen_inheritance_die (base,
19647 (accesses ? (*accesses)[i] : access_public_node),
19648 context_die);
19651 /* Now output info about the data members and type members. */
19652 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19654 /* If we thought we were generating minimal debug info for TYPE
19655 and then changed our minds, some of the member declarations
19656 may have already been defined. Don't define them again, but
19657 do put them in the right order. */
19659 child = lookup_decl_die (member);
19660 if (child)
19661 splice_child_die (context_die, child);
19662 else
19663 gen_decl_die (member, NULL, context_die);
19666 /* Now output info about the function members (if any). */
19667 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19669 /* Don't include clones in the member list. */
19670 if (DECL_ABSTRACT_ORIGIN (member))
19671 continue;
19673 child = lookup_decl_die (member);
19674 if (child)
19675 splice_child_die (context_die, child);
19676 else
19677 gen_decl_die (member, NULL, context_die);
19681 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19682 is set, we pretend that the type was never defined, so we only get the
19683 member DIEs needed by later specification DIEs. */
19685 static void
19686 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19687 enum debug_info_usage usage)
19689 dw_die_ref type_die = lookup_type_die (type);
19690 dw_die_ref scope_die = 0;
19691 int nested = 0;
19692 int complete = (TYPE_SIZE (type)
19693 && (! TYPE_STUB_DECL (type)
19694 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19695 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19696 complete = complete && should_emit_struct_debug (type, usage);
19698 if (type_die && ! complete)
19699 return;
19701 if (TYPE_CONTEXT (type) != NULL_TREE
19702 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19703 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19704 nested = 1;
19706 scope_die = scope_die_for (type, context_die);
19708 /* Generate child dies for template paramaters. */
19709 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19710 schedule_generic_params_dies_gen (type);
19712 if (! type_die || (nested && is_cu_die (scope_die)))
19713 /* First occurrence of type or toplevel definition of nested class. */
19715 dw_die_ref old_die = type_die;
19717 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19718 ? record_type_tag (type) : DW_TAG_union_type,
19719 scope_die, type);
19720 equate_type_number_to_die (type, type_die);
19721 if (old_die)
19722 add_AT_specification (type_die, old_die);
19723 else
19724 add_name_attribute (type_die, type_tag (type));
19726 else
19727 remove_AT (type_die, DW_AT_declaration);
19729 /* If this type has been completed, then give it a byte_size attribute and
19730 then give a list of members. */
19731 if (complete && !ns_decl)
19733 /* Prevent infinite recursion in cases where the type of some member of
19734 this type is expressed in terms of this type itself. */
19735 TREE_ASM_WRITTEN (type) = 1;
19736 add_byte_size_attribute (type_die, type);
19737 if (TYPE_STUB_DECL (type) != NULL_TREE)
19739 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19740 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19743 /* If the first reference to this type was as the return type of an
19744 inline function, then it may not have a parent. Fix this now. */
19745 if (type_die->die_parent == NULL)
19746 add_child_die (scope_die, type_die);
19748 push_decl_scope (type);
19749 gen_member_die (type, type_die);
19750 pop_decl_scope ();
19752 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19753 if (TYPE_ARTIFICIAL (type))
19754 add_AT_flag (type_die, DW_AT_artificial, 1);
19756 /* GNU extension: Record what type our vtable lives in. */
19757 if (TYPE_VFIELD (type))
19759 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19761 gen_type_die (vtype, context_die);
19762 add_AT_die_ref (type_die, DW_AT_containing_type,
19763 lookup_type_die (vtype));
19766 else
19768 add_AT_flag (type_die, DW_AT_declaration, 1);
19770 /* We don't need to do this for function-local types. */
19771 if (TYPE_STUB_DECL (type)
19772 && ! decl_function_context (TYPE_STUB_DECL (type)))
19773 vec_safe_push (incomplete_types, type);
19776 if (get_AT (type_die, DW_AT_name))
19777 add_pubtype (type, type_die);
19780 /* Generate a DIE for a subroutine _type_. */
19782 static void
19783 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19785 tree return_type = TREE_TYPE (type);
19786 dw_die_ref subr_die
19787 = new_die (DW_TAG_subroutine_type,
19788 scope_die_for (type, context_die), type);
19790 equate_type_number_to_die (type, subr_die);
19791 add_prototyped_attribute (subr_die, type);
19792 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19793 gen_formal_types_die (type, subr_die);
19795 if (get_AT (subr_die, DW_AT_name))
19796 add_pubtype (type, subr_die);
19799 /* Generate a DIE for a type definition. */
19801 static void
19802 gen_typedef_die (tree decl, dw_die_ref context_die)
19804 dw_die_ref type_die;
19805 tree origin;
19807 if (TREE_ASM_WRITTEN (decl))
19808 return;
19810 TREE_ASM_WRITTEN (decl) = 1;
19811 type_die = new_die (DW_TAG_typedef, context_die, decl);
19812 origin = decl_ultimate_origin (decl);
19813 if (origin != NULL)
19814 add_abstract_origin_attribute (type_die, origin);
19815 else
19817 tree type;
19819 add_name_and_src_coords_attributes (type_die, decl);
19820 if (DECL_ORIGINAL_TYPE (decl))
19822 type = DECL_ORIGINAL_TYPE (decl);
19824 gcc_assert (type != TREE_TYPE (decl));
19825 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19827 else
19829 type = TREE_TYPE (decl);
19831 if (is_naming_typedef_decl (TYPE_NAME (type)))
19833 /* Here, we are in the case of decl being a typedef naming
19834 an anonymous type, e.g:
19835 typedef struct {...} foo;
19836 In that case TREE_TYPE (decl) is not a typedef variant
19837 type and TYPE_NAME of the anonymous type is set to the
19838 TYPE_DECL of the typedef. This construct is emitted by
19839 the C++ FE.
19841 TYPE is the anonymous struct named by the typedef
19842 DECL. As we need the DW_AT_type attribute of the
19843 DW_TAG_typedef to point to the DIE of TYPE, let's
19844 generate that DIE right away. add_type_attribute
19845 called below will then pick (via lookup_type_die) that
19846 anonymous struct DIE. */
19847 if (!TREE_ASM_WRITTEN (type))
19848 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19850 /* This is a GNU Extension. We are adding a
19851 DW_AT_linkage_name attribute to the DIE of the
19852 anonymous struct TYPE. The value of that attribute
19853 is the name of the typedef decl naming the anonymous
19854 struct. This greatly eases the work of consumers of
19855 this debug info. */
19856 add_linkage_attr (lookup_type_die (type), decl);
19860 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19862 if (is_naming_typedef_decl (decl))
19863 /* We want that all subsequent calls to lookup_type_die with
19864 TYPE in argument yield the DW_TAG_typedef we have just
19865 created. */
19866 equate_type_number_to_die (type, type_die);
19868 add_accessibility_attribute (type_die, decl);
19871 if (DECL_ABSTRACT (decl))
19872 equate_decl_number_to_die (decl, type_die);
19874 if (get_AT (type_die, DW_AT_name))
19875 add_pubtype (decl, type_die);
19878 /* Generate a DIE for a struct, class, enum or union type. */
19880 static void
19881 gen_tagged_type_die (tree type,
19882 dw_die_ref context_die,
19883 enum debug_info_usage usage)
19885 int need_pop;
19887 if (type == NULL_TREE
19888 || !is_tagged_type (type))
19889 return;
19891 /* If this is a nested type whose containing class hasn't been written
19892 out yet, writing it out will cover this one, too. This does not apply
19893 to instantiations of member class templates; they need to be added to
19894 the containing class as they are generated. FIXME: This hurts the
19895 idea of combining type decls from multiple TUs, since we can't predict
19896 what set of template instantiations we'll get. */
19897 if (TYPE_CONTEXT (type)
19898 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19899 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19901 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19903 if (TREE_ASM_WRITTEN (type))
19904 return;
19906 /* If that failed, attach ourselves to the stub. */
19907 push_decl_scope (TYPE_CONTEXT (type));
19908 context_die = lookup_type_die (TYPE_CONTEXT (type));
19909 need_pop = 1;
19911 else if (TYPE_CONTEXT (type) != NULL_TREE
19912 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19914 /* If this type is local to a function that hasn't been written
19915 out yet, use a NULL context for now; it will be fixed up in
19916 decls_for_scope. */
19917 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19918 /* A declaration DIE doesn't count; nested types need to go in the
19919 specification. */
19920 if (context_die && is_declaration_die (context_die))
19921 context_die = NULL;
19922 need_pop = 0;
19924 else
19926 context_die = declare_in_namespace (type, context_die);
19927 need_pop = 0;
19930 if (TREE_CODE (type) == ENUMERAL_TYPE)
19932 /* This might have been written out by the call to
19933 declare_in_namespace. */
19934 if (!TREE_ASM_WRITTEN (type))
19935 gen_enumeration_type_die (type, context_die);
19937 else
19938 gen_struct_or_union_type_die (type, context_die, usage);
19940 if (need_pop)
19941 pop_decl_scope ();
19943 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19944 it up if it is ever completed. gen_*_type_die will set it for us
19945 when appropriate. */
19948 /* Generate a type description DIE. */
19950 static void
19951 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19952 enum debug_info_usage usage)
19954 struct array_descr_info info;
19956 if (type == NULL_TREE || type == error_mark_node)
19957 return;
19959 if (TYPE_NAME (type) != NULL_TREE
19960 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19961 && is_redundant_typedef (TYPE_NAME (type))
19962 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19963 /* The DECL of this type is a typedef we don't want to emit debug
19964 info for but we want debug info for its underlying typedef.
19965 This can happen for e.g, the injected-class-name of a C++
19966 type. */
19967 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19969 /* If TYPE is a typedef type variant, let's generate debug info
19970 for the parent typedef which TYPE is a type of. */
19971 if (typedef_variant_p (type))
19973 if (TREE_ASM_WRITTEN (type))
19974 return;
19976 /* Prevent broken recursion; we can't hand off to the same type. */
19977 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19979 /* Give typedefs the right scope. */
19980 context_die = scope_die_for (type, context_die);
19982 TREE_ASM_WRITTEN (type) = 1;
19984 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19985 return;
19988 /* If type is an anonymous tagged type named by a typedef, let's
19989 generate debug info for the typedef. */
19990 if (is_naming_typedef_decl (TYPE_NAME (type)))
19992 /* Use the DIE of the containing namespace as the parent DIE of
19993 the type description DIE we want to generate. */
19994 if (DECL_CONTEXT (TYPE_NAME (type))
19995 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19996 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19998 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19999 return;
20002 /* If this is an array type with hidden descriptor, handle it first. */
20003 if (!TREE_ASM_WRITTEN (type)
20004 && lang_hooks.types.get_array_descr_info
20005 && lang_hooks.types.get_array_descr_info (type, &info)
20006 && (dwarf_version >= 3 || !dwarf_strict))
20008 gen_descr_array_type_die (type, &info, context_die);
20009 TREE_ASM_WRITTEN (type) = 1;
20010 return;
20013 /* We are going to output a DIE to represent the unqualified version
20014 of this type (i.e. without any const or volatile qualifiers) so
20015 get the main variant (i.e. the unqualified version) of this type
20016 now. (Vectors are special because the debugging info is in the
20017 cloned type itself). */
20018 if (TREE_CODE (type) != VECTOR_TYPE)
20019 type = type_main_variant (type);
20021 if (TREE_ASM_WRITTEN (type))
20022 return;
20024 switch (TREE_CODE (type))
20026 case ERROR_MARK:
20027 break;
20029 case POINTER_TYPE:
20030 case REFERENCE_TYPE:
20031 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20032 ensures that the gen_type_die recursion will terminate even if the
20033 type is recursive. Recursive types are possible in Ada. */
20034 /* ??? We could perhaps do this for all types before the switch
20035 statement. */
20036 TREE_ASM_WRITTEN (type) = 1;
20038 /* For these types, all that is required is that we output a DIE (or a
20039 set of DIEs) to represent the "basis" type. */
20040 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20041 DINFO_USAGE_IND_USE);
20042 break;
20044 case OFFSET_TYPE:
20045 /* This code is used for C++ pointer-to-data-member types.
20046 Output a description of the relevant class type. */
20047 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20048 DINFO_USAGE_IND_USE);
20050 /* Output a description of the type of the object pointed to. */
20051 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20052 DINFO_USAGE_IND_USE);
20054 /* Now output a DIE to represent this pointer-to-data-member type
20055 itself. */
20056 gen_ptr_to_mbr_type_die (type, context_die);
20057 break;
20059 case FUNCTION_TYPE:
20060 /* Force out return type (in case it wasn't forced out already). */
20061 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20062 DINFO_USAGE_DIR_USE);
20063 gen_subroutine_type_die (type, context_die);
20064 break;
20066 case METHOD_TYPE:
20067 /* Force out return type (in case it wasn't forced out already). */
20068 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20069 DINFO_USAGE_DIR_USE);
20070 gen_subroutine_type_die (type, context_die);
20071 break;
20073 case ARRAY_TYPE:
20074 gen_array_type_die (type, context_die);
20075 break;
20077 case VECTOR_TYPE:
20078 gen_array_type_die (type, context_die);
20079 break;
20081 case ENUMERAL_TYPE:
20082 case RECORD_TYPE:
20083 case UNION_TYPE:
20084 case QUAL_UNION_TYPE:
20085 gen_tagged_type_die (type, context_die, usage);
20086 return;
20088 case VOID_TYPE:
20089 case INTEGER_TYPE:
20090 case REAL_TYPE:
20091 case FIXED_POINT_TYPE:
20092 case COMPLEX_TYPE:
20093 case BOOLEAN_TYPE:
20094 /* No DIEs needed for fundamental types. */
20095 break;
20097 case NULLPTR_TYPE:
20098 case LANG_TYPE:
20099 /* Just use DW_TAG_unspecified_type. */
20101 dw_die_ref type_die = lookup_type_die (type);
20102 if (type_die == NULL)
20104 tree name = TYPE_IDENTIFIER (type);
20105 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20106 type);
20107 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20108 equate_type_number_to_die (type, type_die);
20111 break;
20113 default:
20114 if (is_cxx_auto (type))
20116 tree name = TYPE_IDENTIFIER (type);
20117 dw_die_ref *die = (name == get_identifier ("auto")
20118 ? &auto_die : &decltype_auto_die);
20119 if (!*die)
20121 *die = new_die (DW_TAG_unspecified_type,
20122 comp_unit_die (), NULL_TREE);
20123 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20125 equate_type_number_to_die (type, *die);
20126 break;
20128 gcc_unreachable ();
20131 TREE_ASM_WRITTEN (type) = 1;
20134 static void
20135 gen_type_die (tree type, dw_die_ref context_die)
20137 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20140 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20141 things which are local to the given block. */
20143 static void
20144 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20146 int must_output_die = 0;
20147 bool inlined_func;
20149 /* Ignore blocks that are NULL. */
20150 if (stmt == NULL_TREE)
20151 return;
20153 inlined_func = inlined_function_outer_scope_p (stmt);
20155 /* If the block is one fragment of a non-contiguous block, do not
20156 process the variables, since they will have been done by the
20157 origin block. Do process subblocks. */
20158 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20160 tree sub;
20162 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20163 gen_block_die (sub, context_die, depth + 1);
20165 return;
20168 /* Determine if we need to output any Dwarf DIEs at all to represent this
20169 block. */
20170 if (inlined_func)
20171 /* The outer scopes for inlinings *must* always be represented. We
20172 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20173 must_output_die = 1;
20174 else
20176 /* Determine if this block directly contains any "significant"
20177 local declarations which we will need to output DIEs for. */
20178 if (debug_info_level > DINFO_LEVEL_TERSE)
20179 /* We are not in terse mode so *any* local declaration counts
20180 as being a "significant" one. */
20181 must_output_die = ((BLOCK_VARS (stmt) != NULL
20182 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20183 && (TREE_USED (stmt)
20184 || TREE_ASM_WRITTEN (stmt)
20185 || BLOCK_ABSTRACT (stmt)));
20186 else if ((TREE_USED (stmt)
20187 || TREE_ASM_WRITTEN (stmt)
20188 || BLOCK_ABSTRACT (stmt))
20189 && !dwarf2out_ignore_block (stmt))
20190 must_output_die = 1;
20193 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20194 DIE for any block which contains no significant local declarations at
20195 all. Rather, in such cases we just call `decls_for_scope' so that any
20196 needed Dwarf info for any sub-blocks will get properly generated. Note
20197 that in terse mode, our definition of what constitutes a "significant"
20198 local declaration gets restricted to include only inlined function
20199 instances and local (nested) function definitions. */
20200 if (must_output_die)
20202 if (inlined_func)
20204 /* If STMT block is abstract, that means we have been called
20205 indirectly from dwarf2out_abstract_function.
20206 That function rightfully marks the descendent blocks (of
20207 the abstract function it is dealing with) as being abstract,
20208 precisely to prevent us from emitting any
20209 DW_TAG_inlined_subroutine DIE as a descendent
20210 of an abstract function instance. So in that case, we should
20211 not call gen_inlined_subroutine_die.
20213 Later though, when cgraph asks dwarf2out to emit info
20214 for the concrete instance of the function decl into which
20215 the concrete instance of STMT got inlined, the later will lead
20216 to the generation of a DW_TAG_inlined_subroutine DIE. */
20217 if (! BLOCK_ABSTRACT (stmt))
20218 gen_inlined_subroutine_die (stmt, context_die, depth);
20220 else
20221 gen_lexical_block_die (stmt, context_die, depth);
20223 else
20224 decls_for_scope (stmt, context_die, depth);
20227 /* Process variable DECL (or variable with origin ORIGIN) within
20228 block STMT and add it to CONTEXT_DIE. */
20229 static void
20230 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20232 dw_die_ref die;
20233 tree decl_or_origin = decl ? decl : origin;
20235 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20236 die = lookup_decl_die (decl_or_origin);
20237 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20238 && TYPE_DECL_IS_STUB (decl_or_origin))
20239 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20240 else
20241 die = NULL;
20243 if (die != NULL && die->die_parent == NULL)
20244 add_child_die (context_die, die);
20245 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20246 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20247 stmt, context_die);
20248 else
20249 gen_decl_die (decl, origin, context_die);
20252 /* Generate all of the decls declared within a given scope and (recursively)
20253 all of its sub-blocks. */
20255 static void
20256 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20258 tree decl;
20259 unsigned int i;
20260 tree subblocks;
20262 /* Ignore NULL blocks. */
20263 if (stmt == NULL_TREE)
20264 return;
20266 /* Output the DIEs to represent all of the data objects and typedefs
20267 declared directly within this block but not within any nested
20268 sub-blocks. Also, nested function and tag DIEs have been
20269 generated with a parent of NULL; fix that up now. We don't
20270 have to do this if we're at -g1. */
20271 if (debug_info_level > DINFO_LEVEL_TERSE)
20273 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20274 process_scope_var (stmt, decl, NULL_TREE, context_die);
20275 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20276 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20277 context_die);
20280 /* Even if we're at -g1, we need to process the subblocks in order to get
20281 inlined call information. */
20283 /* Output the DIEs to represent all sub-blocks (and the items declared
20284 therein) of this block. */
20285 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20286 subblocks != NULL;
20287 subblocks = BLOCK_CHAIN (subblocks))
20288 gen_block_die (subblocks, context_die, depth + 1);
20291 /* Is this a typedef we can avoid emitting? */
20293 static inline int
20294 is_redundant_typedef (const_tree decl)
20296 if (TYPE_DECL_IS_STUB (decl))
20297 return 1;
20299 if (DECL_ARTIFICIAL (decl)
20300 && DECL_CONTEXT (decl)
20301 && is_tagged_type (DECL_CONTEXT (decl))
20302 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20303 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20304 /* Also ignore the artificial member typedef for the class name. */
20305 return 1;
20307 return 0;
20310 /* Return TRUE if TYPE is a typedef that names a type for linkage
20311 purposes. This kind of typedefs is produced by the C++ FE for
20312 constructs like:
20314 typedef struct {...} foo;
20316 In that case, there is no typedef variant type produced for foo.
20317 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20318 struct type. */
20320 static bool
20321 is_naming_typedef_decl (const_tree decl)
20323 if (decl == NULL_TREE
20324 || TREE_CODE (decl) != TYPE_DECL
20325 || !is_tagged_type (TREE_TYPE (decl))
20326 || DECL_IS_BUILTIN (decl)
20327 || is_redundant_typedef (decl)
20328 /* It looks like Ada produces TYPE_DECLs that are very similar
20329 to C++ naming typedefs but that have different
20330 semantics. Let's be specific to c++ for now. */
20331 || !is_cxx ())
20332 return FALSE;
20334 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20335 && TYPE_NAME (TREE_TYPE (decl)) == decl
20336 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20337 != TYPE_NAME (TREE_TYPE (decl))));
20340 /* Returns the DIE for a context. */
20342 static inline dw_die_ref
20343 get_context_die (tree context)
20345 if (context)
20347 /* Find die that represents this context. */
20348 if (TYPE_P (context))
20350 context = TYPE_MAIN_VARIANT (context);
20351 return strip_naming_typedef (context, force_type_die (context));
20353 else
20354 return force_decl_die (context);
20356 return comp_unit_die ();
20359 /* Returns the DIE for decl. A DIE will always be returned. */
20361 static dw_die_ref
20362 force_decl_die (tree decl)
20364 dw_die_ref decl_die;
20365 unsigned saved_external_flag;
20366 tree save_fn = NULL_TREE;
20367 decl_die = lookup_decl_die (decl);
20368 if (!decl_die)
20370 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20372 decl_die = lookup_decl_die (decl);
20373 if (decl_die)
20374 return decl_die;
20376 switch (TREE_CODE (decl))
20378 case FUNCTION_DECL:
20379 /* Clear current_function_decl, so that gen_subprogram_die thinks
20380 that this is a declaration. At this point, we just want to force
20381 declaration die. */
20382 save_fn = current_function_decl;
20383 current_function_decl = NULL_TREE;
20384 gen_subprogram_die (decl, context_die);
20385 current_function_decl = save_fn;
20386 break;
20388 case VAR_DECL:
20389 /* Set external flag to force declaration die. Restore it after
20390 gen_decl_die() call. */
20391 saved_external_flag = DECL_EXTERNAL (decl);
20392 DECL_EXTERNAL (decl) = 1;
20393 gen_decl_die (decl, NULL, context_die);
20394 DECL_EXTERNAL (decl) = saved_external_flag;
20395 break;
20397 case NAMESPACE_DECL:
20398 if (dwarf_version >= 3 || !dwarf_strict)
20399 dwarf2out_decl (decl);
20400 else
20401 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20402 decl_die = comp_unit_die ();
20403 break;
20405 case TRANSLATION_UNIT_DECL:
20406 decl_die = comp_unit_die ();
20407 break;
20409 default:
20410 gcc_unreachable ();
20413 /* We should be able to find the DIE now. */
20414 if (!decl_die)
20415 decl_die = lookup_decl_die (decl);
20416 gcc_assert (decl_die);
20419 return decl_die;
20422 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20423 always returned. */
20425 static dw_die_ref
20426 force_type_die (tree type)
20428 dw_die_ref type_die;
20430 type_die = lookup_type_die (type);
20431 if (!type_die)
20433 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20435 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20436 context_die);
20437 gcc_assert (type_die);
20439 return type_die;
20442 /* Force out any required namespaces to be able to output DECL,
20443 and return the new context_die for it, if it's changed. */
20445 static dw_die_ref
20446 setup_namespace_context (tree thing, dw_die_ref context_die)
20448 tree context = (DECL_P (thing)
20449 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20450 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20451 /* Force out the namespace. */
20452 context_die = force_decl_die (context);
20454 return context_die;
20457 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20458 type) within its namespace, if appropriate.
20460 For compatibility with older debuggers, namespace DIEs only contain
20461 declarations; all definitions are emitted at CU scope. */
20463 static dw_die_ref
20464 declare_in_namespace (tree thing, dw_die_ref context_die)
20466 dw_die_ref ns_context;
20468 if (debug_info_level <= DINFO_LEVEL_TERSE)
20469 return context_die;
20471 /* If this decl is from an inlined function, then don't try to emit it in its
20472 namespace, as we will get confused. It would have already been emitted
20473 when the abstract instance of the inline function was emitted anyways. */
20474 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20475 return context_die;
20477 ns_context = setup_namespace_context (thing, context_die);
20479 if (ns_context != context_die)
20481 if (is_fortran ())
20482 return ns_context;
20483 if (DECL_P (thing))
20484 gen_decl_die (thing, NULL, ns_context);
20485 else
20486 gen_type_die (thing, ns_context);
20488 return context_die;
20491 /* Generate a DIE for a namespace or namespace alias. */
20493 static void
20494 gen_namespace_die (tree decl, dw_die_ref context_die)
20496 dw_die_ref namespace_die;
20498 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20499 they are an alias of. */
20500 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20502 /* Output a real namespace or module. */
20503 context_die = setup_namespace_context (decl, comp_unit_die ());
20504 namespace_die = new_die (is_fortran ()
20505 ? DW_TAG_module : DW_TAG_namespace,
20506 context_die, decl);
20507 /* For Fortran modules defined in different CU don't add src coords. */
20508 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20510 const char *name = dwarf2_name (decl, 0);
20511 if (name)
20512 add_name_attribute (namespace_die, name);
20514 else
20515 add_name_and_src_coords_attributes (namespace_die, decl);
20516 if (DECL_EXTERNAL (decl))
20517 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20518 equate_decl_number_to_die (decl, namespace_die);
20520 else
20522 /* Output a namespace alias. */
20524 /* Force out the namespace we are an alias of, if necessary. */
20525 dw_die_ref origin_die
20526 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20528 if (DECL_FILE_SCOPE_P (decl)
20529 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20530 context_die = setup_namespace_context (decl, comp_unit_die ());
20531 /* Now create the namespace alias DIE. */
20532 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20533 add_name_and_src_coords_attributes (namespace_die, decl);
20534 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20535 equate_decl_number_to_die (decl, namespace_die);
20537 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20538 if (want_pubnames ())
20539 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20542 /* Generate Dwarf debug information for a decl described by DECL.
20543 The return value is currently only meaningful for PARM_DECLs,
20544 for all other decls it returns NULL. */
20546 static dw_die_ref
20547 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20549 tree decl_or_origin = decl ? decl : origin;
20550 tree class_origin = NULL, ultimate_origin;
20552 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20553 return NULL;
20555 switch (TREE_CODE (decl_or_origin))
20557 case ERROR_MARK:
20558 break;
20560 case CONST_DECL:
20561 if (!is_fortran () && !is_ada ())
20563 /* The individual enumerators of an enum type get output when we output
20564 the Dwarf representation of the relevant enum type itself. */
20565 break;
20568 /* Emit its type. */
20569 gen_type_die (TREE_TYPE (decl), context_die);
20571 /* And its containing namespace. */
20572 context_die = declare_in_namespace (decl, context_die);
20574 gen_const_die (decl, context_die);
20575 break;
20577 case FUNCTION_DECL:
20578 /* Don't output any DIEs to represent mere function declarations,
20579 unless they are class members or explicit block externs. */
20580 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20581 && DECL_FILE_SCOPE_P (decl_or_origin)
20582 && (current_function_decl == NULL_TREE
20583 || DECL_ARTIFICIAL (decl_or_origin)))
20584 break;
20586 #if 0
20587 /* FIXME */
20588 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20589 on local redeclarations of global functions. That seems broken. */
20590 if (current_function_decl != decl)
20591 /* This is only a declaration. */;
20592 #endif
20594 /* If we're emitting a clone, emit info for the abstract instance. */
20595 if (origin || DECL_ORIGIN (decl) != decl)
20596 dwarf2out_abstract_function (origin
20597 ? DECL_ORIGIN (origin)
20598 : DECL_ABSTRACT_ORIGIN (decl));
20600 /* If we're emitting an out-of-line copy of an inline function,
20601 emit info for the abstract instance and set up to refer to it. */
20602 else if (cgraph_function_possibly_inlined_p (decl)
20603 && ! DECL_ABSTRACT (decl)
20604 && ! class_or_namespace_scope_p (context_die)
20605 /* dwarf2out_abstract_function won't emit a die if this is just
20606 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20607 that case, because that works only if we have a die. */
20608 && DECL_INITIAL (decl) != NULL_TREE)
20610 dwarf2out_abstract_function (decl);
20611 set_decl_origin_self (decl);
20614 /* Otherwise we're emitting the primary DIE for this decl. */
20615 else if (debug_info_level > DINFO_LEVEL_TERSE)
20617 /* Before we describe the FUNCTION_DECL itself, make sure that we
20618 have its containing type. */
20619 if (!origin)
20620 origin = decl_class_context (decl);
20621 if (origin != NULL_TREE)
20622 gen_type_die (origin, context_die);
20624 /* And its return type. */
20625 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20627 /* And its virtual context. */
20628 if (DECL_VINDEX (decl) != NULL_TREE)
20629 gen_type_die (DECL_CONTEXT (decl), context_die);
20631 /* Make sure we have a member DIE for decl. */
20632 if (origin != NULL_TREE)
20633 gen_type_die_for_member (origin, decl, context_die);
20635 /* And its containing namespace. */
20636 context_die = declare_in_namespace (decl, context_die);
20639 /* Now output a DIE to represent the function itself. */
20640 if (decl)
20641 gen_subprogram_die (decl, context_die);
20642 break;
20644 case TYPE_DECL:
20645 /* If we are in terse mode, don't generate any DIEs to represent any
20646 actual typedefs. */
20647 if (debug_info_level <= DINFO_LEVEL_TERSE)
20648 break;
20650 /* In the special case of a TYPE_DECL node representing the declaration
20651 of some type tag, if the given TYPE_DECL is marked as having been
20652 instantiated from some other (original) TYPE_DECL node (e.g. one which
20653 was generated within the original definition of an inline function) we
20654 used to generate a special (abbreviated) DW_TAG_structure_type,
20655 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20656 should be actually referencing those DIEs, as variable DIEs with that
20657 type would be emitted already in the abstract origin, so it was always
20658 removed during unused type prunning. Don't add anything in this
20659 case. */
20660 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20661 break;
20663 if (is_redundant_typedef (decl))
20664 gen_type_die (TREE_TYPE (decl), context_die);
20665 else
20666 /* Output a DIE to represent the typedef itself. */
20667 gen_typedef_die (decl, context_die);
20668 break;
20670 case LABEL_DECL:
20671 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20672 gen_label_die (decl, context_die);
20673 break;
20675 case VAR_DECL:
20676 case RESULT_DECL:
20677 /* If we are in terse mode, don't generate any DIEs to represent any
20678 variable declarations or definitions. */
20679 if (debug_info_level <= DINFO_LEVEL_TERSE)
20680 break;
20682 /* Output any DIEs that are needed to specify the type of this data
20683 object. */
20684 if (decl_by_reference_p (decl_or_origin))
20685 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20686 else
20687 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20689 /* And its containing type. */
20690 class_origin = decl_class_context (decl_or_origin);
20691 if (class_origin != NULL_TREE)
20692 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20694 /* And its containing namespace. */
20695 context_die = declare_in_namespace (decl_or_origin, context_die);
20697 /* Now output the DIE to represent the data object itself. This gets
20698 complicated because of the possibility that the VAR_DECL really
20699 represents an inlined instance of a formal parameter for an inline
20700 function. */
20701 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20702 if (ultimate_origin != NULL_TREE
20703 && TREE_CODE (ultimate_origin) == PARM_DECL)
20704 gen_formal_parameter_die (decl, origin,
20705 true /* Emit name attribute. */,
20706 context_die);
20707 else
20708 gen_variable_die (decl, origin, context_die);
20709 break;
20711 case FIELD_DECL:
20712 /* Ignore the nameless fields that are used to skip bits but handle C++
20713 anonymous unions and structs. */
20714 if (DECL_NAME (decl) != NULL_TREE
20715 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20716 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20718 gen_type_die (member_declared_type (decl), context_die);
20719 gen_field_die (decl, context_die);
20721 break;
20723 case PARM_DECL:
20724 if (DECL_BY_REFERENCE (decl_or_origin))
20725 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20726 else
20727 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20728 return gen_formal_parameter_die (decl, origin,
20729 true /* Emit name attribute. */,
20730 context_die);
20732 case NAMESPACE_DECL:
20733 case IMPORTED_DECL:
20734 if (dwarf_version >= 3 || !dwarf_strict)
20735 gen_namespace_die (decl, context_die);
20736 break;
20738 case NAMELIST_DECL:
20739 gen_namelist_decl (DECL_NAME (decl), context_die,
20740 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20741 break;
20743 default:
20744 /* Probably some frontend-internal decl. Assume we don't care. */
20745 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20746 break;
20749 return NULL;
20752 /* Output debug information for global decl DECL. Called from toplev.c after
20753 compilation proper has finished. */
20755 static void
20756 dwarf2out_global_decl (tree decl)
20758 /* Output DWARF2 information for file-scope tentative data object
20759 declarations, file-scope (extern) function declarations (which
20760 had no corresponding body) and file-scope tagged type declarations
20761 and definitions which have not yet been forced out. */
20762 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20763 dwarf2out_decl (decl);
20766 /* Output debug information for type decl DECL. Called from toplev.c
20767 and from language front ends (to record built-in types). */
20768 static void
20769 dwarf2out_type_decl (tree decl, int local)
20771 if (!local)
20772 dwarf2out_decl (decl);
20775 /* Output debug information for imported module or decl DECL.
20776 NAME is non-NULL name in the lexical block if the decl has been renamed.
20777 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20778 that DECL belongs to.
20779 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20780 static void
20781 dwarf2out_imported_module_or_decl_1 (tree decl,
20782 tree name,
20783 tree lexical_block,
20784 dw_die_ref lexical_block_die)
20786 expanded_location xloc;
20787 dw_die_ref imported_die = NULL;
20788 dw_die_ref at_import_die;
20790 if (TREE_CODE (decl) == IMPORTED_DECL)
20792 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20793 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20794 gcc_assert (decl);
20796 else
20797 xloc = expand_location (input_location);
20799 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20801 at_import_die = force_type_die (TREE_TYPE (decl));
20802 /* For namespace N { typedef void T; } using N::T; base_type_die
20803 returns NULL, but DW_TAG_imported_declaration requires
20804 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20805 if (!at_import_die)
20807 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20808 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20809 at_import_die = lookup_type_die (TREE_TYPE (decl));
20810 gcc_assert (at_import_die);
20813 else
20815 at_import_die = lookup_decl_die (decl);
20816 if (!at_import_die)
20818 /* If we're trying to avoid duplicate debug info, we may not have
20819 emitted the member decl for this field. Emit it now. */
20820 if (TREE_CODE (decl) == FIELD_DECL)
20822 tree type = DECL_CONTEXT (decl);
20824 if (TYPE_CONTEXT (type)
20825 && TYPE_P (TYPE_CONTEXT (type))
20826 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20827 DINFO_USAGE_DIR_USE))
20828 return;
20829 gen_type_die_for_member (type, decl,
20830 get_context_die (TYPE_CONTEXT (type)));
20832 if (TREE_CODE (decl) == NAMELIST_DECL)
20833 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20834 get_context_die (DECL_CONTEXT (decl)),
20835 NULL_TREE);
20836 else
20837 at_import_die = force_decl_die (decl);
20841 if (TREE_CODE (decl) == NAMESPACE_DECL)
20843 if (dwarf_version >= 3 || !dwarf_strict)
20844 imported_die = new_die (DW_TAG_imported_module,
20845 lexical_block_die,
20846 lexical_block);
20847 else
20848 return;
20850 else
20851 imported_die = new_die (DW_TAG_imported_declaration,
20852 lexical_block_die,
20853 lexical_block);
20855 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20856 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20857 if (name)
20858 add_AT_string (imported_die, DW_AT_name,
20859 IDENTIFIER_POINTER (name));
20860 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20863 /* Output debug information for imported module or decl DECL.
20864 NAME is non-NULL name in context if the decl has been renamed.
20865 CHILD is true if decl is one of the renamed decls as part of
20866 importing whole module. */
20868 static void
20869 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20870 bool child)
20872 /* dw_die_ref at_import_die; */
20873 dw_die_ref scope_die;
20875 if (debug_info_level <= DINFO_LEVEL_TERSE)
20876 return;
20878 gcc_assert (decl);
20880 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20881 We need decl DIE for reference and scope die. First, get DIE for the decl
20882 itself. */
20884 /* Get the scope die for decl context. Use comp_unit_die for global module
20885 or decl. If die is not found for non globals, force new die. */
20886 if (context
20887 && TYPE_P (context)
20888 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20889 return;
20891 if (!(dwarf_version >= 3 || !dwarf_strict))
20892 return;
20894 scope_die = get_context_die (context);
20896 if (child)
20898 gcc_assert (scope_die->die_child);
20899 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20900 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20901 scope_die = scope_die->die_child;
20904 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20905 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20909 /* Output debug information for namelists. */
20911 static dw_die_ref
20912 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20914 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20915 tree value;
20916 unsigned i;
20918 if (debug_info_level <= DINFO_LEVEL_TERSE)
20919 return NULL;
20921 gcc_assert (scope_die != NULL);
20922 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20923 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20925 /* If there are no item_decls, we have a nondefining namelist, e.g.
20926 with USE association; hence, set DW_AT_declaration. */
20927 if (item_decls == NULL_TREE)
20929 add_AT_flag (nml_die, DW_AT_declaration, 1);
20930 return nml_die;
20933 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20935 nml_item_ref_die = lookup_decl_die (value);
20936 if (!nml_item_ref_die)
20937 nml_item_ref_die = force_decl_die (value);
20939 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20940 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20942 return nml_die;
20946 /* Write the debugging output for DECL. */
20948 static void
20949 dwarf2out_decl (tree decl)
20951 dw_die_ref context_die = comp_unit_die ();
20953 switch (TREE_CODE (decl))
20955 case ERROR_MARK:
20956 return;
20958 case FUNCTION_DECL:
20959 /* What we would really like to do here is to filter out all mere
20960 file-scope declarations of file-scope functions which are never
20961 referenced later within this translation unit (and keep all of ones
20962 that *are* referenced later on) but we aren't clairvoyant, so we have
20963 no idea which functions will be referenced in the future (i.e. later
20964 on within the current translation unit). So here we just ignore all
20965 file-scope function declarations which are not also definitions. If
20966 and when the debugger needs to know something about these functions,
20967 it will have to hunt around and find the DWARF information associated
20968 with the definition of the function.
20970 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20971 nodes represent definitions and which ones represent mere
20972 declarations. We have to check DECL_INITIAL instead. That's because
20973 the C front-end supports some weird semantics for "extern inline"
20974 function definitions. These can get inlined within the current
20975 translation unit (and thus, we need to generate Dwarf info for their
20976 abstract instances so that the Dwarf info for the concrete inlined
20977 instances can have something to refer to) but the compiler never
20978 generates any out-of-lines instances of such things (despite the fact
20979 that they *are* definitions).
20981 The important point is that the C front-end marks these "extern
20982 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20983 them anyway. Note that the C++ front-end also plays some similar games
20984 for inline function definitions appearing within include files which
20985 also contain `#pragma interface' pragmas.
20987 If we are called from dwarf2out_abstract_function output a DIE
20988 anyway. We can end up here this way with early inlining and LTO
20989 where the inlined function is output in a different LTRANS unit
20990 or not at all. */
20991 if (DECL_INITIAL (decl) == NULL_TREE
20992 && ! DECL_ABSTRACT (decl))
20993 return;
20995 /* If we're a nested function, initially use a parent of NULL; if we're
20996 a plain function, this will be fixed up in decls_for_scope. If
20997 we're a method, it will be ignored, since we already have a DIE. */
20998 if (decl_function_context (decl)
20999 /* But if we're in terse mode, we don't care about scope. */
21000 && debug_info_level > DINFO_LEVEL_TERSE)
21001 context_die = NULL;
21002 break;
21004 case VAR_DECL:
21005 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21006 declaration and if the declaration was never even referenced from
21007 within this entire compilation unit. We suppress these DIEs in
21008 order to save space in the .debug section (by eliminating entries
21009 which are probably useless). Note that we must not suppress
21010 block-local extern declarations (whether used or not) because that
21011 would screw-up the debugger's name lookup mechanism and cause it to
21012 miss things which really ought to be in scope at a given point. */
21013 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21014 return;
21016 /* For local statics lookup proper context die. */
21017 if (TREE_STATIC (decl)
21018 && DECL_CONTEXT (decl)
21019 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21020 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21022 /* If we are in terse mode, don't generate any DIEs to represent any
21023 variable declarations or definitions. */
21024 if (debug_info_level <= DINFO_LEVEL_TERSE)
21025 return;
21026 break;
21028 case CONST_DECL:
21029 if (debug_info_level <= DINFO_LEVEL_TERSE)
21030 return;
21031 if (!is_fortran () && !is_ada ())
21032 return;
21033 if (TREE_STATIC (decl) && decl_function_context (decl))
21034 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21035 break;
21037 case NAMESPACE_DECL:
21038 case IMPORTED_DECL:
21039 if (debug_info_level <= DINFO_LEVEL_TERSE)
21040 return;
21041 if (lookup_decl_die (decl) != NULL)
21042 return;
21043 break;
21045 case TYPE_DECL:
21046 /* Don't emit stubs for types unless they are needed by other DIEs. */
21047 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21048 return;
21050 /* Don't bother trying to generate any DIEs to represent any of the
21051 normal built-in types for the language we are compiling. */
21052 if (DECL_IS_BUILTIN (decl))
21053 return;
21055 /* If we are in terse mode, don't generate any DIEs for types. */
21056 if (debug_info_level <= DINFO_LEVEL_TERSE)
21057 return;
21059 /* If we're a function-scope tag, initially use a parent of NULL;
21060 this will be fixed up in decls_for_scope. */
21061 if (decl_function_context (decl))
21062 context_die = NULL;
21064 break;
21066 case NAMELIST_DECL:
21067 break;
21069 default:
21070 return;
21073 gen_decl_die (decl, NULL, context_die);
21076 /* Write the debugging output for DECL. */
21078 static void
21079 dwarf2out_function_decl (tree decl)
21081 dwarf2out_decl (decl);
21082 call_arg_locations = NULL;
21083 call_arg_loc_last = NULL;
21084 call_site_count = -1;
21085 tail_call_site_count = -1;
21086 block_map.release ();
21087 htab_empty (decl_loc_table);
21088 htab_empty (cached_dw_loc_list_table);
21091 /* Output a marker (i.e. a label) for the beginning of the generated code for
21092 a lexical block. */
21094 static void
21095 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21096 unsigned int blocknum)
21098 switch_to_section (current_function_section ());
21099 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21102 /* Output a marker (i.e. a label) for the end of the generated code for a
21103 lexical block. */
21105 static void
21106 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21108 switch_to_section (current_function_section ());
21109 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21112 /* Returns nonzero if it is appropriate not to emit any debugging
21113 information for BLOCK, because it doesn't contain any instructions.
21115 Don't allow this for blocks with nested functions or local classes
21116 as we would end up with orphans, and in the presence of scheduling
21117 we may end up calling them anyway. */
21119 static bool
21120 dwarf2out_ignore_block (const_tree block)
21122 tree decl;
21123 unsigned int i;
21125 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21126 if (TREE_CODE (decl) == FUNCTION_DECL
21127 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21128 return 0;
21129 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21131 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21132 if (TREE_CODE (decl) == FUNCTION_DECL
21133 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21134 return 0;
21137 return 1;
21140 /* Hash table routines for file_hash. */
21142 static int
21143 file_table_eq (const void *p1_p, const void *p2_p)
21145 const struct dwarf_file_data *const p1 =
21146 (const struct dwarf_file_data *) p1_p;
21147 const char *const p2 = (const char *) p2_p;
21148 return filename_cmp (p1->filename, p2) == 0;
21151 static hashval_t
21152 file_table_hash (const void *p_p)
21154 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21155 return htab_hash_string (p->filename);
21158 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21159 dwarf2out.c) and return its "index". The index of each (known) filename is
21160 just a unique number which is associated with only that one filename. We
21161 need such numbers for the sake of generating labels (in the .debug_sfnames
21162 section) and references to those files numbers (in the .debug_srcinfo
21163 and.debug_macinfo sections). If the filename given as an argument is not
21164 found in our current list, add it to the list and assign it the next
21165 available unique index number. In order to speed up searches, we remember
21166 the index of the filename was looked up last. This handles the majority of
21167 all searches. */
21169 static struct dwarf_file_data *
21170 lookup_filename (const char *file_name)
21172 void ** slot;
21173 struct dwarf_file_data * created;
21175 /* Check to see if the file name that was searched on the previous
21176 call matches this file name. If so, return the index. */
21177 if (file_table_last_lookup
21178 && (file_name == file_table_last_lookup->filename
21179 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21180 return file_table_last_lookup;
21182 /* Didn't match the previous lookup, search the table. */
21183 slot = htab_find_slot_with_hash (file_table, file_name,
21184 htab_hash_string (file_name), INSERT);
21185 if (*slot)
21186 return (struct dwarf_file_data *) *slot;
21188 created = ggc_alloc<dwarf_file_data> ();
21189 created->filename = file_name;
21190 created->emitted_number = 0;
21191 *slot = created;
21192 return created;
21195 /* If the assembler will construct the file table, then translate the compiler
21196 internal file table number into the assembler file table number, and emit
21197 a .file directive if we haven't already emitted one yet. The file table
21198 numbers are different because we prune debug info for unused variables and
21199 types, which may include filenames. */
21201 static int
21202 maybe_emit_file (struct dwarf_file_data * fd)
21204 if (! fd->emitted_number)
21206 if (last_emitted_file)
21207 fd->emitted_number = last_emitted_file->emitted_number + 1;
21208 else
21209 fd->emitted_number = 1;
21210 last_emitted_file = fd;
21212 if (DWARF2_ASM_LINE_DEBUG_INFO)
21214 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21215 output_quoted_string (asm_out_file,
21216 remap_debug_filename (fd->filename));
21217 fputc ('\n', asm_out_file);
21221 return fd->emitted_number;
21224 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21225 That generation should happen after function debug info has been
21226 generated. The value of the attribute is the constant value of ARG. */
21228 static void
21229 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21231 die_arg_entry entry;
21233 if (!die || !arg)
21234 return;
21236 if (!tmpl_value_parm_die_table)
21237 vec_alloc (tmpl_value_parm_die_table, 32);
21239 entry.die = die;
21240 entry.arg = arg;
21241 vec_safe_push (tmpl_value_parm_die_table, entry);
21244 /* Return TRUE if T is an instance of generic type, FALSE
21245 otherwise. */
21247 static bool
21248 generic_type_p (tree t)
21250 if (t == NULL_TREE || !TYPE_P (t))
21251 return false;
21252 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21255 /* Schedule the generation of the generic parameter dies for the
21256 instance of generic type T. The proper generation itself is later
21257 done by gen_scheduled_generic_parms_dies. */
21259 static void
21260 schedule_generic_params_dies_gen (tree t)
21262 if (!generic_type_p (t))
21263 return;
21265 if (!generic_type_instances)
21266 vec_alloc (generic_type_instances, 256);
21268 vec_safe_push (generic_type_instances, t);
21271 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21272 by append_entry_to_tmpl_value_parm_die_table. This function must
21273 be called after function DIEs have been generated. */
21275 static void
21276 gen_remaining_tmpl_value_param_die_attribute (void)
21278 if (tmpl_value_parm_die_table)
21280 unsigned i;
21281 die_arg_entry *e;
21283 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21284 tree_add_const_value_attribute (e->die, e->arg);
21288 /* Generate generic parameters DIEs for instances of generic types
21289 that have been previously scheduled by
21290 schedule_generic_params_dies_gen. This function must be called
21291 after all the types of the CU have been laid out. */
21293 static void
21294 gen_scheduled_generic_parms_dies (void)
21296 unsigned i;
21297 tree t;
21299 if (!generic_type_instances)
21300 return;
21302 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21303 if (COMPLETE_TYPE_P (t))
21304 gen_generic_params_dies (t);
21308 /* Replace DW_AT_name for the decl with name. */
21310 static void
21311 dwarf2out_set_name (tree decl, tree name)
21313 dw_die_ref die;
21314 dw_attr_ref attr;
21315 const char *dname;
21317 die = TYPE_SYMTAB_DIE (decl);
21318 if (!die)
21319 return;
21321 dname = dwarf2_name (name, 0);
21322 if (!dname)
21323 return;
21325 attr = get_AT (die, DW_AT_name);
21326 if (attr)
21328 struct indirect_string_node *node;
21330 node = find_AT_string (dname);
21331 /* replace the string. */
21332 attr->dw_attr_val.v.val_str = node;
21335 else
21336 add_name_attribute (die, dname);
21339 /* True if before or during processing of the first function being emitted. */
21340 static bool in_first_function_p = true;
21341 /* True if loc_note during dwarf2out_var_location call might still be
21342 before first real instruction at address equal to .Ltext0. */
21343 static bool maybe_at_text_label_p = true;
21344 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21345 static unsigned int first_loclabel_num_not_at_text_label;
21347 /* Called by the final INSN scan whenever we see a var location. We
21348 use it to drop labels in the right places, and throw the location in
21349 our lookup table. */
21351 static void
21352 dwarf2out_var_location (rtx_insn *loc_note)
21354 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21355 struct var_loc_node *newloc;
21356 rtx_insn *next_real, *next_note;
21357 static const char *last_label;
21358 static const char *last_postcall_label;
21359 static bool last_in_cold_section_p;
21360 static rtx_insn *expected_next_loc_note;
21361 tree decl;
21362 bool var_loc_p;
21364 if (!NOTE_P (loc_note))
21366 if (CALL_P (loc_note))
21368 call_site_count++;
21369 if (SIBLING_CALL_P (loc_note))
21370 tail_call_site_count++;
21372 return;
21375 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21376 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21377 return;
21379 /* Optimize processing a large consecutive sequence of location
21380 notes so we don't spend too much time in next_real_insn. If the
21381 next insn is another location note, remember the next_real_insn
21382 calculation for next time. */
21383 next_real = cached_next_real_insn;
21384 if (next_real)
21386 if (expected_next_loc_note != loc_note)
21387 next_real = NULL;
21390 next_note = NEXT_INSN (loc_note);
21391 if (! next_note
21392 || INSN_DELETED_P (next_note)
21393 || ! NOTE_P (next_note)
21394 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21395 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21396 next_note = NULL;
21398 if (! next_real)
21399 next_real = next_real_insn (loc_note);
21401 if (next_note)
21403 expected_next_loc_note = next_note;
21404 cached_next_real_insn = next_real;
21406 else
21407 cached_next_real_insn = NULL;
21409 /* If there are no instructions which would be affected by this note,
21410 don't do anything. */
21411 if (var_loc_p
21412 && next_real == NULL_RTX
21413 && !NOTE_DURING_CALL_P (loc_note))
21414 return;
21416 if (next_real == NULL_RTX)
21417 next_real = get_last_insn ();
21419 /* If there were any real insns between note we processed last time
21420 and this note (or if it is the first note), clear
21421 last_{,postcall_}label so that they are not reused this time. */
21422 if (last_var_location_insn == NULL_RTX
21423 || last_var_location_insn != next_real
21424 || last_in_cold_section_p != in_cold_section_p)
21426 last_label = NULL;
21427 last_postcall_label = NULL;
21430 if (var_loc_p)
21432 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21433 newloc = add_var_loc_to_decl (decl, loc_note,
21434 NOTE_DURING_CALL_P (loc_note)
21435 ? last_postcall_label : last_label);
21436 if (newloc == NULL)
21437 return;
21439 else
21441 decl = NULL_TREE;
21442 newloc = NULL;
21445 /* If there were no real insns between note we processed last time
21446 and this note, use the label we emitted last time. Otherwise
21447 create a new label and emit it. */
21448 if (last_label == NULL)
21450 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21451 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21452 loclabel_num++;
21453 last_label = ggc_strdup (loclabel);
21454 /* See if loclabel might be equal to .Ltext0. If yes,
21455 bump first_loclabel_num_not_at_text_label. */
21456 if (!have_multiple_function_sections
21457 && in_first_function_p
21458 && maybe_at_text_label_p)
21460 static rtx_insn *last_start;
21461 rtx_insn *insn;
21462 for (insn = loc_note; insn; insn = previous_insn (insn))
21463 if (insn == last_start)
21464 break;
21465 else if (!NONDEBUG_INSN_P (insn))
21466 continue;
21467 else
21469 rtx body = PATTERN (insn);
21470 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21471 continue;
21472 /* Inline asm could occupy zero bytes. */
21473 else if (GET_CODE (body) == ASM_INPUT
21474 || asm_noperands (body) >= 0)
21475 continue;
21476 #ifdef HAVE_attr_length
21477 else if (get_attr_min_length (insn) == 0)
21478 continue;
21479 #endif
21480 else
21482 /* Assume insn has non-zero length. */
21483 maybe_at_text_label_p = false;
21484 break;
21487 if (maybe_at_text_label_p)
21489 last_start = loc_note;
21490 first_loclabel_num_not_at_text_label = loclabel_num;
21495 if (!var_loc_p)
21497 struct call_arg_loc_node *ca_loc
21498 = ggc_cleared_alloc<call_arg_loc_node> ();
21499 rtx prev = prev_real_insn (loc_note), x;
21500 ca_loc->call_arg_loc_note = loc_note;
21501 ca_loc->next = NULL;
21502 ca_loc->label = last_label;
21503 gcc_assert (prev
21504 && (CALL_P (prev)
21505 || (NONJUMP_INSN_P (prev)
21506 && GET_CODE (PATTERN (prev)) == SEQUENCE
21507 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21508 if (!CALL_P (prev))
21509 prev = XVECEXP (PATTERN (prev), 0, 0);
21510 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21511 x = get_call_rtx_from (PATTERN (prev));
21512 if (x)
21514 x = XEXP (XEXP (x, 0), 0);
21515 if (GET_CODE (x) == SYMBOL_REF
21516 && SYMBOL_REF_DECL (x)
21517 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21518 ca_loc->symbol_ref = x;
21520 ca_loc->block = insn_scope (prev);
21521 if (call_arg_locations)
21522 call_arg_loc_last->next = ca_loc;
21523 else
21524 call_arg_locations = ca_loc;
21525 call_arg_loc_last = ca_loc;
21527 else if (!NOTE_DURING_CALL_P (loc_note))
21528 newloc->label = last_label;
21529 else
21531 if (!last_postcall_label)
21533 sprintf (loclabel, "%s-1", last_label);
21534 last_postcall_label = ggc_strdup (loclabel);
21536 newloc->label = last_postcall_label;
21539 last_var_location_insn = next_real;
21540 last_in_cold_section_p = in_cold_section_p;
21543 /* Note in one location list that text section has changed. */
21545 static int
21546 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21548 var_loc_list *list = (var_loc_list *) *slot;
21549 if (list->first)
21550 list->last_before_switch
21551 = list->last->next ? list->last->next : list->last;
21552 return 1;
21555 /* Note in all location lists that text section has changed. */
21557 static void
21558 var_location_switch_text_section (void)
21560 if (decl_loc_table == NULL)
21561 return;
21563 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21566 /* Create a new line number table. */
21568 static dw_line_info_table *
21569 new_line_info_table (void)
21571 dw_line_info_table *table;
21573 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21574 table->file_num = 1;
21575 table->line_num = 1;
21576 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21578 return table;
21581 /* Lookup the "current" table into which we emit line info, so
21582 that we don't have to do it for every source line. */
21584 static void
21585 set_cur_line_info_table (section *sec)
21587 dw_line_info_table *table;
21589 if (sec == text_section)
21590 table = text_section_line_info;
21591 else if (sec == cold_text_section)
21593 table = cold_text_section_line_info;
21594 if (!table)
21596 cold_text_section_line_info = table = new_line_info_table ();
21597 table->end_label = cold_end_label;
21600 else
21602 const char *end_label;
21604 if (flag_reorder_blocks_and_partition)
21606 if (in_cold_section_p)
21607 end_label = crtl->subsections.cold_section_end_label;
21608 else
21609 end_label = crtl->subsections.hot_section_end_label;
21611 else
21613 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21614 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21615 current_function_funcdef_no);
21616 end_label = ggc_strdup (label);
21619 table = new_line_info_table ();
21620 table->end_label = end_label;
21622 vec_safe_push (separate_line_info, table);
21625 if (DWARF2_ASM_LINE_DEBUG_INFO)
21626 table->is_stmt = (cur_line_info_table
21627 ? cur_line_info_table->is_stmt
21628 : DWARF_LINE_DEFAULT_IS_STMT_START);
21629 cur_line_info_table = table;
21633 /* We need to reset the locations at the beginning of each
21634 function. We can't do this in the end_function hook, because the
21635 declarations that use the locations won't have been output when
21636 that hook is called. Also compute have_multiple_function_sections here. */
21638 static void
21639 dwarf2out_begin_function (tree fun)
21641 section *sec = function_section (fun);
21643 if (sec != text_section)
21644 have_multiple_function_sections = true;
21646 if (flag_reorder_blocks_and_partition && !cold_text_section)
21648 gcc_assert (current_function_decl == fun);
21649 cold_text_section = unlikely_text_section ();
21650 switch_to_section (cold_text_section);
21651 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21652 switch_to_section (sec);
21655 dwarf2out_note_section_used ();
21656 call_site_count = 0;
21657 tail_call_site_count = 0;
21659 set_cur_line_info_table (sec);
21662 /* Helper function of dwarf2out_end_function, called only after emitting
21663 the very first function into assembly. Check if some .debug_loc range
21664 might end with a .LVL* label that could be equal to .Ltext0.
21665 In that case we must force using absolute addresses in .debug_loc ranges,
21666 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21667 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21668 list terminator.
21669 Set have_multiple_function_sections to true in that case and
21670 terminate htab traversal. */
21672 static int
21673 find_empty_loc_ranges_at_text_label (void **slot, void *)
21675 var_loc_list *entry;
21676 struct var_loc_node *node;
21678 entry = (var_loc_list *) *slot;
21679 node = entry->first;
21680 if (node && node->next && node->next->label)
21682 unsigned int i;
21683 const char *label = node->next->label;
21684 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21686 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21688 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21689 if (strcmp (label, loclabel) == 0)
21691 have_multiple_function_sections = true;
21692 return 0;
21696 return 1;
21699 /* Hook called after emitting a function into assembly.
21700 This does something only for the very first function emitted. */
21702 static void
21703 dwarf2out_end_function (unsigned int)
21705 if (in_first_function_p
21706 && !have_multiple_function_sections
21707 && first_loclabel_num_not_at_text_label
21708 && decl_loc_table)
21709 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21710 NULL);
21711 in_first_function_p = false;
21712 maybe_at_text_label_p = false;
21715 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21717 static void
21718 push_dw_line_info_entry (dw_line_info_table *table,
21719 enum dw_line_info_opcode opcode, unsigned int val)
21721 dw_line_info_entry e;
21722 e.opcode = opcode;
21723 e.val = val;
21724 vec_safe_push (table->entries, e);
21727 /* Output a label to mark the beginning of a source code line entry
21728 and record information relating to this source line, in
21729 'line_info_table' for later output of the .debug_line section. */
21730 /* ??? The discriminator parameter ought to be unsigned. */
21732 static void
21733 dwarf2out_source_line (unsigned int line, const char *filename,
21734 int discriminator, bool is_stmt)
21736 unsigned int file_num;
21737 dw_line_info_table *table;
21739 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21740 return;
21742 /* The discriminator column was added in dwarf4. Simplify the below
21743 by simply removing it if we're not supposed to output it. */
21744 if (dwarf_version < 4 && dwarf_strict)
21745 discriminator = 0;
21747 table = cur_line_info_table;
21748 file_num = maybe_emit_file (lookup_filename (filename));
21750 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21751 the debugger has used the second (possibly duplicate) line number
21752 at the beginning of the function to mark the end of the prologue.
21753 We could eliminate any other duplicates within the function. For
21754 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21755 that second line number entry. */
21756 /* Recall that this end-of-prologue indication is *not* the same thing
21757 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21758 to which the hook corresponds, follows the last insn that was
21759 emitted by gen_prologue. What we need is to precede the first insn
21760 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21761 insn that corresponds to something the user wrote. These may be
21762 very different locations once scheduling is enabled. */
21764 if (0 && file_num == table->file_num
21765 && line == table->line_num
21766 && discriminator == table->discrim_num
21767 && is_stmt == table->is_stmt)
21768 return;
21770 switch_to_section (current_function_section ());
21772 /* If requested, emit something human-readable. */
21773 if (flag_debug_asm)
21774 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21776 if (DWARF2_ASM_LINE_DEBUG_INFO)
21778 /* Emit the .loc directive understood by GNU as. */
21779 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21780 file_num, line, is_stmt, discriminator */
21781 fputs ("\t.loc ", asm_out_file);
21782 fprint_ul (asm_out_file, file_num);
21783 putc (' ', asm_out_file);
21784 fprint_ul (asm_out_file, line);
21785 putc (' ', asm_out_file);
21786 putc ('0', asm_out_file);
21788 if (is_stmt != table->is_stmt)
21790 fputs (" is_stmt ", asm_out_file);
21791 putc (is_stmt ? '1' : '0', asm_out_file);
21793 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21795 gcc_assert (discriminator > 0);
21796 fputs (" discriminator ", asm_out_file);
21797 fprint_ul (asm_out_file, (unsigned long) discriminator);
21799 putc ('\n', asm_out_file);
21801 else
21803 unsigned int label_num = ++line_info_label_num;
21805 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21807 push_dw_line_info_entry (table, LI_set_address, label_num);
21808 if (file_num != table->file_num)
21809 push_dw_line_info_entry (table, LI_set_file, file_num);
21810 if (discriminator != table->discrim_num)
21811 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21812 if (is_stmt != table->is_stmt)
21813 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21814 push_dw_line_info_entry (table, LI_set_line, line);
21817 table->file_num = file_num;
21818 table->line_num = line;
21819 table->discrim_num = discriminator;
21820 table->is_stmt = is_stmt;
21821 table->in_use = true;
21824 /* Record the beginning of a new source file. */
21826 static void
21827 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21829 if (flag_eliminate_dwarf2_dups)
21831 /* Record the beginning of the file for break_out_includes. */
21832 dw_die_ref bincl_die;
21834 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21835 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21838 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21840 macinfo_entry e;
21841 e.code = DW_MACINFO_start_file;
21842 e.lineno = lineno;
21843 e.info = ggc_strdup (filename);
21844 vec_safe_push (macinfo_table, e);
21848 /* Record the end of a source file. */
21850 static void
21851 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21853 if (flag_eliminate_dwarf2_dups)
21854 /* Record the end of the file for break_out_includes. */
21855 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21857 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21859 macinfo_entry e;
21860 e.code = DW_MACINFO_end_file;
21861 e.lineno = lineno;
21862 e.info = NULL;
21863 vec_safe_push (macinfo_table, e);
21867 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21868 the tail part of the directive line, i.e. the part which is past the
21869 initial whitespace, #, whitespace, directive-name, whitespace part. */
21871 static void
21872 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21873 const char *buffer ATTRIBUTE_UNUSED)
21875 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21877 macinfo_entry e;
21878 /* Insert a dummy first entry to be able to optimize the whole
21879 predefined macro block using DW_MACRO_GNU_transparent_include. */
21880 if (macinfo_table->is_empty () && lineno <= 1)
21882 e.code = 0;
21883 e.lineno = 0;
21884 e.info = NULL;
21885 vec_safe_push (macinfo_table, e);
21887 e.code = DW_MACINFO_define;
21888 e.lineno = lineno;
21889 e.info = ggc_strdup (buffer);
21890 vec_safe_push (macinfo_table, e);
21894 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21895 the tail part of the directive line, i.e. the part which is past the
21896 initial whitespace, #, whitespace, directive-name, whitespace part. */
21898 static void
21899 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21900 const char *buffer ATTRIBUTE_UNUSED)
21902 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21904 macinfo_entry e;
21905 /* Insert a dummy first entry to be able to optimize the whole
21906 predefined macro block using DW_MACRO_GNU_transparent_include. */
21907 if (macinfo_table->is_empty () && lineno <= 1)
21909 e.code = 0;
21910 e.lineno = 0;
21911 e.info = NULL;
21912 vec_safe_push (macinfo_table, e);
21914 e.code = DW_MACINFO_undef;
21915 e.lineno = lineno;
21916 e.info = ggc_strdup (buffer);
21917 vec_safe_push (macinfo_table, e);
21921 /* Helpers to manipulate hash table of CUs. */
21923 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21925 typedef macinfo_entry value_type;
21926 typedef macinfo_entry compare_type;
21927 static inline hashval_t hash (const value_type *);
21928 static inline bool equal (const value_type *, const compare_type *);
21931 inline hashval_t
21932 macinfo_entry_hasher::hash (const value_type *entry)
21934 return htab_hash_string (entry->info);
21937 inline bool
21938 macinfo_entry_hasher::equal (const value_type *entry1,
21939 const compare_type *entry2)
21941 return !strcmp (entry1->info, entry2->info);
21944 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21946 /* Output a single .debug_macinfo entry. */
21948 static void
21949 output_macinfo_op (macinfo_entry *ref)
21951 int file_num;
21952 size_t len;
21953 struct indirect_string_node *node;
21954 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21955 struct dwarf_file_data *fd;
21957 switch (ref->code)
21959 case DW_MACINFO_start_file:
21960 fd = lookup_filename (ref->info);
21961 file_num = maybe_emit_file (fd);
21962 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21963 dw2_asm_output_data_uleb128 (ref->lineno,
21964 "Included from line number %lu",
21965 (unsigned long) ref->lineno);
21966 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21967 break;
21968 case DW_MACINFO_end_file:
21969 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21970 break;
21971 case DW_MACINFO_define:
21972 case DW_MACINFO_undef:
21973 len = strlen (ref->info) + 1;
21974 if (!dwarf_strict
21975 && len > DWARF_OFFSET_SIZE
21976 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21977 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21979 ref->code = ref->code == DW_MACINFO_define
21980 ? DW_MACRO_GNU_define_indirect
21981 : DW_MACRO_GNU_undef_indirect;
21982 output_macinfo_op (ref);
21983 return;
21985 dw2_asm_output_data (1, ref->code,
21986 ref->code == DW_MACINFO_define
21987 ? "Define macro" : "Undefine macro");
21988 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21989 (unsigned long) ref->lineno);
21990 dw2_asm_output_nstring (ref->info, -1, "The macro");
21991 break;
21992 case DW_MACRO_GNU_define_indirect:
21993 case DW_MACRO_GNU_undef_indirect:
21994 node = find_AT_string (ref->info);
21995 gcc_assert (node
21996 && ((node->form == DW_FORM_strp)
21997 || (node->form == DW_FORM_GNU_str_index)));
21998 dw2_asm_output_data (1, ref->code,
21999 ref->code == DW_MACRO_GNU_define_indirect
22000 ? "Define macro indirect"
22001 : "Undefine macro indirect");
22002 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22003 (unsigned long) ref->lineno);
22004 if (node->form == DW_FORM_strp)
22005 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22006 debug_str_section, "The macro: \"%s\"",
22007 ref->info);
22008 else
22009 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22010 ref->info);
22011 break;
22012 case DW_MACRO_GNU_transparent_include:
22013 dw2_asm_output_data (1, ref->code, "Transparent include");
22014 ASM_GENERATE_INTERNAL_LABEL (label,
22015 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22016 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22017 break;
22018 default:
22019 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22020 ASM_COMMENT_START, (unsigned long) ref->code);
22021 break;
22025 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22026 other compilation unit .debug_macinfo sections. IDX is the first
22027 index of a define/undef, return the number of ops that should be
22028 emitted in a comdat .debug_macinfo section and emit
22029 a DW_MACRO_GNU_transparent_include entry referencing it.
22030 If the define/undef entry should be emitted normally, return 0. */
22032 static unsigned
22033 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22034 macinfo_hash_type **macinfo_htab)
22036 macinfo_entry *first, *second, *cur, *inc;
22037 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22038 unsigned char checksum[16];
22039 struct md5_ctx ctx;
22040 char *grp_name, *tail;
22041 const char *base;
22042 unsigned int i, count, encoded_filename_len, linebuf_len;
22043 macinfo_entry **slot;
22045 first = &(*macinfo_table)[idx];
22046 second = &(*macinfo_table)[idx + 1];
22048 /* Optimize only if there are at least two consecutive define/undef ops,
22049 and either all of them are before first DW_MACINFO_start_file
22050 with lineno {0,1} (i.e. predefined macro block), or all of them are
22051 in some included header file. */
22052 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22053 return 0;
22054 if (vec_safe_is_empty (files))
22056 if (first->lineno > 1 || second->lineno > 1)
22057 return 0;
22059 else if (first->lineno == 0)
22060 return 0;
22062 /* Find the last define/undef entry that can be grouped together
22063 with first and at the same time compute md5 checksum of their
22064 codes, linenumbers and strings. */
22065 md5_init_ctx (&ctx);
22066 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22067 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22068 break;
22069 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22070 break;
22071 else
22073 unsigned char code = cur->code;
22074 md5_process_bytes (&code, 1, &ctx);
22075 checksum_uleb128 (cur->lineno, &ctx);
22076 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22078 md5_finish_ctx (&ctx, checksum);
22079 count = i - idx;
22081 /* From the containing include filename (if any) pick up just
22082 usable characters from its basename. */
22083 if (vec_safe_is_empty (files))
22084 base = "";
22085 else
22086 base = lbasename (files->last ().info);
22087 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22088 if (ISIDNUM (base[i]) || base[i] == '.')
22089 encoded_filename_len++;
22090 /* Count . at the end. */
22091 if (encoded_filename_len)
22092 encoded_filename_len++;
22094 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22095 linebuf_len = strlen (linebuf);
22097 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22098 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22099 + 16 * 2 + 1);
22100 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22101 tail = grp_name + 4;
22102 if (encoded_filename_len)
22104 for (i = 0; base[i]; i++)
22105 if (ISIDNUM (base[i]) || base[i] == '.')
22106 *tail++ = base[i];
22107 *tail++ = '.';
22109 memcpy (tail, linebuf, linebuf_len);
22110 tail += linebuf_len;
22111 *tail++ = '.';
22112 for (i = 0; i < 16; i++)
22113 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22115 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22116 in the empty vector entry before the first define/undef. */
22117 inc = &(*macinfo_table)[idx - 1];
22118 inc->code = DW_MACRO_GNU_transparent_include;
22119 inc->lineno = 0;
22120 inc->info = ggc_strdup (grp_name);
22121 if (!*macinfo_htab)
22122 *macinfo_htab = new macinfo_hash_type (10);
22123 /* Avoid emitting duplicates. */
22124 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22125 if (*slot != NULL)
22127 inc->code = 0;
22128 inc->info = NULL;
22129 /* If such an entry has been used before, just emit
22130 a DW_MACRO_GNU_transparent_include op. */
22131 inc = *slot;
22132 output_macinfo_op (inc);
22133 /* And clear all macinfo_entry in the range to avoid emitting them
22134 in the second pass. */
22135 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22137 cur->code = 0;
22138 cur->info = NULL;
22141 else
22143 *slot = inc;
22144 inc->lineno = (*macinfo_htab)->elements ();
22145 output_macinfo_op (inc);
22147 return count;
22150 /* Save any strings needed by the macinfo table in the debug str
22151 table. All strings must be collected into the table by the time
22152 index_string is called. */
22154 static void
22155 save_macinfo_strings (void)
22157 unsigned len;
22158 unsigned i;
22159 macinfo_entry *ref;
22161 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22163 switch (ref->code)
22165 /* Match the logic in output_macinfo_op to decide on
22166 indirect strings. */
22167 case DW_MACINFO_define:
22168 case DW_MACINFO_undef:
22169 len = strlen (ref->info) + 1;
22170 if (!dwarf_strict
22171 && len > DWARF_OFFSET_SIZE
22172 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22173 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22174 set_indirect_string (find_AT_string (ref->info));
22175 break;
22176 case DW_MACRO_GNU_define_indirect:
22177 case DW_MACRO_GNU_undef_indirect:
22178 set_indirect_string (find_AT_string (ref->info));
22179 break;
22180 default:
22181 break;
22186 /* Output macinfo section(s). */
22188 static void
22189 output_macinfo (void)
22191 unsigned i;
22192 unsigned long length = vec_safe_length (macinfo_table);
22193 macinfo_entry *ref;
22194 vec<macinfo_entry, va_gc> *files = NULL;
22195 macinfo_hash_type *macinfo_htab = NULL;
22197 if (! length)
22198 return;
22200 /* output_macinfo* uses these interchangeably. */
22201 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22202 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22203 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22204 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22206 /* For .debug_macro emit the section header. */
22207 if (!dwarf_strict)
22209 dw2_asm_output_data (2, 4, "DWARF macro version number");
22210 if (DWARF_OFFSET_SIZE == 8)
22211 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22212 else
22213 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22214 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22215 (!dwarf_split_debug_info ? debug_line_section_label
22216 : debug_skeleton_line_section_label),
22217 debug_line_section, NULL);
22220 /* In the first loop, it emits the primary .debug_macinfo section
22221 and after each emitted op the macinfo_entry is cleared.
22222 If a longer range of define/undef ops can be optimized using
22223 DW_MACRO_GNU_transparent_include, the
22224 DW_MACRO_GNU_transparent_include op is emitted and kept in
22225 the vector before the first define/undef in the range and the
22226 whole range of define/undef ops is not emitted and kept. */
22227 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22229 switch (ref->code)
22231 case DW_MACINFO_start_file:
22232 vec_safe_push (files, *ref);
22233 break;
22234 case DW_MACINFO_end_file:
22235 if (!vec_safe_is_empty (files))
22236 files->pop ();
22237 break;
22238 case DW_MACINFO_define:
22239 case DW_MACINFO_undef:
22240 if (!dwarf_strict
22241 && HAVE_COMDAT_GROUP
22242 && vec_safe_length (files) != 1
22243 && i > 0
22244 && i + 1 < length
22245 && (*macinfo_table)[i - 1].code == 0)
22247 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22248 if (count)
22250 i += count - 1;
22251 continue;
22254 break;
22255 case 0:
22256 /* A dummy entry may be inserted at the beginning to be able
22257 to optimize the whole block of predefined macros. */
22258 if (i == 0)
22259 continue;
22260 default:
22261 break;
22263 output_macinfo_op (ref);
22264 ref->info = NULL;
22265 ref->code = 0;
22268 if (!macinfo_htab)
22269 return;
22271 delete macinfo_htab;
22272 macinfo_htab = NULL;
22274 /* If any DW_MACRO_GNU_transparent_include were used, on those
22275 DW_MACRO_GNU_transparent_include entries terminate the
22276 current chain and switch to a new comdat .debug_macinfo
22277 section and emit the define/undef entries within it. */
22278 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22279 switch (ref->code)
22281 case 0:
22282 continue;
22283 case DW_MACRO_GNU_transparent_include:
22285 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22286 tree comdat_key = get_identifier (ref->info);
22287 /* Terminate the previous .debug_macinfo section. */
22288 dw2_asm_output_data (1, 0, "End compilation unit");
22289 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22290 SECTION_DEBUG
22291 | SECTION_LINKONCE,
22292 comdat_key);
22293 ASM_GENERATE_INTERNAL_LABEL (label,
22294 DEBUG_MACRO_SECTION_LABEL,
22295 ref->lineno);
22296 ASM_OUTPUT_LABEL (asm_out_file, label);
22297 ref->code = 0;
22298 ref->info = NULL;
22299 dw2_asm_output_data (2, 4, "DWARF macro version number");
22300 if (DWARF_OFFSET_SIZE == 8)
22301 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22302 else
22303 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22305 break;
22306 case DW_MACINFO_define:
22307 case DW_MACINFO_undef:
22308 output_macinfo_op (ref);
22309 ref->code = 0;
22310 ref->info = NULL;
22311 break;
22312 default:
22313 gcc_unreachable ();
22317 /* Set up for Dwarf output at the start of compilation. */
22319 static void
22320 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22322 /* Allocate the file_table. */
22323 file_table = htab_create_ggc (50, file_table_hash,
22324 file_table_eq, NULL);
22326 /* Allocate the decl_die_table. */
22327 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22328 decl_die_table_eq, NULL);
22330 /* Allocate the decl_loc_table. */
22331 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22332 decl_loc_table_eq, NULL);
22334 /* Allocate the cached_dw_loc_list_table. */
22335 cached_dw_loc_list_table
22336 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22337 cached_dw_loc_list_table_eq, NULL);
22339 /* Allocate the initial hunk of the decl_scope_table. */
22340 vec_alloc (decl_scope_table, 256);
22342 /* Allocate the initial hunk of the abbrev_die_table. */
22343 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22344 (ABBREV_DIE_TABLE_INCREMENT);
22345 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22346 /* Zero-th entry is allocated, but unused. */
22347 abbrev_die_table_in_use = 1;
22349 /* Allocate the pubtypes and pubnames vectors. */
22350 vec_alloc (pubname_table, 32);
22351 vec_alloc (pubtype_table, 32);
22353 vec_alloc (incomplete_types, 64);
22355 vec_alloc (used_rtx_array, 32);
22357 if (!dwarf_split_debug_info)
22359 debug_info_section = get_section (DEBUG_INFO_SECTION,
22360 SECTION_DEBUG, NULL);
22361 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22362 SECTION_DEBUG, NULL);
22363 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22364 SECTION_DEBUG, NULL);
22366 else
22368 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22369 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22370 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22371 SECTION_DEBUG | SECTION_EXCLUDE,
22372 NULL);
22373 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22374 SECTION_DEBUG, NULL);
22375 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22376 SECTION_DEBUG, NULL);
22377 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22378 SECTION_DEBUG, NULL);
22379 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22380 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22382 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22383 the main .o, but the skeleton_line goes into the split off dwo. */
22384 debug_skeleton_line_section
22385 = get_section (DEBUG_DWO_LINE_SECTION,
22386 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22387 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22388 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22389 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22390 SECTION_DEBUG | SECTION_EXCLUDE,
22391 NULL);
22392 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22393 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22394 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22395 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22396 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22397 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22399 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22400 SECTION_DEBUG, NULL);
22401 debug_macinfo_section = get_section (dwarf_strict
22402 ? DEBUG_MACINFO_SECTION
22403 : DEBUG_MACRO_SECTION,
22404 DEBUG_MACRO_SECTION_FLAGS, NULL);
22405 debug_line_section = get_section (DEBUG_LINE_SECTION,
22406 SECTION_DEBUG, NULL);
22407 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22408 SECTION_DEBUG, NULL);
22409 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22410 SECTION_DEBUG, NULL);
22411 debug_str_section = get_section (DEBUG_STR_SECTION,
22412 DEBUG_STR_SECTION_FLAGS, NULL);
22413 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22414 SECTION_DEBUG, NULL);
22415 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22416 SECTION_DEBUG, NULL);
22418 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22419 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22420 DEBUG_ABBREV_SECTION_LABEL, 0);
22421 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22422 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22423 COLD_TEXT_SECTION_LABEL, 0);
22424 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22426 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22427 DEBUG_INFO_SECTION_LABEL, 0);
22428 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22429 DEBUG_LINE_SECTION_LABEL, 0);
22430 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22431 DEBUG_RANGES_SECTION_LABEL, 0);
22432 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22433 DEBUG_ADDR_SECTION_LABEL, 0);
22434 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22435 dwarf_strict
22436 ? DEBUG_MACINFO_SECTION_LABEL
22437 : DEBUG_MACRO_SECTION_LABEL, 0);
22438 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22440 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22441 vec_alloc (macinfo_table, 64);
22443 switch_to_section (text_section);
22444 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22446 /* Make sure the line number table for .text always exists. */
22447 text_section_line_info = new_line_info_table ();
22448 text_section_line_info->end_label = text_end_label;
22451 /* Called before compile () starts outputtting functions, variables
22452 and toplevel asms into assembly. */
22454 static void
22455 dwarf2out_assembly_start (void)
22457 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22458 && dwarf2out_do_cfi_asm ()
22459 && (!(flag_unwind_tables || flag_exceptions)
22460 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22461 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22464 /* A helper function for dwarf2out_finish called through
22465 htab_traverse. Assign a string its index. All strings must be
22466 collected into the table by the time index_string is called,
22467 because the indexing code relies on htab_traverse to traverse nodes
22468 in the same order for each run. */
22470 static int
22471 index_string (void **h, void *v)
22473 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22474 unsigned int *index = (unsigned int *) v;
22476 find_string_form (node);
22477 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22479 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22480 node->index = *index;
22481 *index += 1;
22483 return 1;
22486 /* A helper function for output_indirect_strings called through
22487 htab_traverse. Output the offset to a string and update the
22488 current offset. */
22490 static int
22491 output_index_string_offset (void **h, void *v)
22493 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22494 unsigned int *offset = (unsigned int *) v;
22496 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22498 /* Assert that this node has been assigned an index. */
22499 gcc_assert (node->index != NO_INDEX_ASSIGNED
22500 && node->index != NOT_INDEXED);
22501 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22502 "indexed string 0x%x: %s", node->index, node->str);
22503 *offset += strlen (node->str) + 1;
22505 return 1;
22508 /* A helper function for dwarf2out_finish called through
22509 htab_traverse. Output the indexed string. */
22511 static int
22512 output_index_string (void **h, void *v)
22514 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22515 unsigned int *cur_idx = (unsigned int *) v;
22517 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22519 /* Assert that the strings are output in the same order as their
22520 indexes were assigned. */
22521 gcc_assert (*cur_idx == node->index);
22522 assemble_string (node->str, strlen (node->str) + 1);
22523 *cur_idx += 1;
22525 return 1;
22528 /* A helper function for dwarf2out_finish called through
22529 htab_traverse. Emit one queued .debug_str string. */
22531 static int
22532 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22534 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22536 node->form = find_string_form (node);
22537 if (node->form == DW_FORM_strp && node->refcount > 0)
22539 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22540 assemble_string (node->str, strlen (node->str) + 1);
22543 return 1;
22546 /* Output the indexed string table. */
22548 static void
22549 output_indirect_strings (void)
22551 switch_to_section (debug_str_section);
22552 if (!dwarf_split_debug_info)
22553 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22554 else
22556 unsigned int offset = 0;
22557 unsigned int cur_idx = 0;
22559 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22561 switch_to_section (debug_str_offsets_section);
22562 htab_traverse_noresize (debug_str_hash,
22563 output_index_string_offset,
22564 &offset);
22565 switch_to_section (debug_str_dwo_section);
22566 htab_traverse_noresize (debug_str_hash,
22567 output_index_string,
22568 &cur_idx);
22572 /* Callback for htab_traverse to assign an index to an entry in the
22573 table, and to write that entry to the .debug_addr section. */
22575 static int
22576 output_addr_table_entry (void **slot, void *data)
22578 addr_table_entry *entry = (addr_table_entry *) *slot;
22579 unsigned int *cur_index = (unsigned int *)data;
22581 if (entry->refcount == 0)
22583 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22584 || entry->index == NOT_INDEXED);
22585 return 1;
22588 gcc_assert (entry->index == *cur_index);
22589 (*cur_index)++;
22591 switch (entry->kind)
22593 case ate_kind_rtx:
22594 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22595 "0x%x", entry->index);
22596 break;
22597 case ate_kind_rtx_dtprel:
22598 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22599 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22600 DWARF2_ADDR_SIZE,
22601 entry->addr.rtl);
22602 fputc ('\n', asm_out_file);
22603 break;
22604 case ate_kind_label:
22605 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22606 "0x%x", entry->index);
22607 break;
22608 default:
22609 gcc_unreachable ();
22611 return 1;
22614 /* Produce the .debug_addr section. */
22616 static void
22617 output_addr_table (void)
22619 unsigned int index = 0;
22620 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22621 return;
22623 switch_to_section (debug_addr_section);
22624 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22627 #if ENABLE_ASSERT_CHECKING
22628 /* Verify that all marks are clear. */
22630 static void
22631 verify_marks_clear (dw_die_ref die)
22633 dw_die_ref c;
22635 gcc_assert (! die->die_mark);
22636 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22638 #endif /* ENABLE_ASSERT_CHECKING */
22640 /* Clear the marks for a die and its children.
22641 Be cool if the mark isn't set. */
22643 static void
22644 prune_unmark_dies (dw_die_ref die)
22646 dw_die_ref c;
22648 if (die->die_mark)
22649 die->die_mark = 0;
22650 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22653 /* Given DIE that we're marking as used, find any other dies
22654 it references as attributes and mark them as used. */
22656 static void
22657 prune_unused_types_walk_attribs (dw_die_ref die)
22659 dw_attr_ref a;
22660 unsigned ix;
22662 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22664 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22666 /* A reference to another DIE.
22667 Make sure that it will get emitted.
22668 If it was broken out into a comdat group, don't follow it. */
22669 if (! AT_ref (a)->comdat_type_p
22670 || a->dw_attr == DW_AT_specification)
22671 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22673 /* Set the string's refcount to 0 so that prune_unused_types_mark
22674 accounts properly for it. */
22675 if (AT_class (a) == dw_val_class_str)
22676 a->dw_attr_val.v.val_str->refcount = 0;
22680 /* Mark the generic parameters and arguments children DIEs of DIE. */
22682 static void
22683 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22685 dw_die_ref c;
22687 if (die == NULL || die->die_child == NULL)
22688 return;
22689 c = die->die_child;
22692 if (is_template_parameter (c))
22693 prune_unused_types_mark (c, 1);
22694 c = c->die_sib;
22695 } while (c && c != die->die_child);
22698 /* Mark DIE as being used. If DOKIDS is true, then walk down
22699 to DIE's children. */
22701 static void
22702 prune_unused_types_mark (dw_die_ref die, int dokids)
22704 dw_die_ref c;
22706 if (die->die_mark == 0)
22708 /* We haven't done this node yet. Mark it as used. */
22709 die->die_mark = 1;
22710 /* If this is the DIE of a generic type instantiation,
22711 mark the children DIEs that describe its generic parms and
22712 args. */
22713 prune_unused_types_mark_generic_parms_dies (die);
22715 /* We also have to mark its parents as used.
22716 (But we don't want to mark our parent's kids due to this,
22717 unless it is a class.) */
22718 if (die->die_parent)
22719 prune_unused_types_mark (die->die_parent,
22720 class_scope_p (die->die_parent));
22722 /* Mark any referenced nodes. */
22723 prune_unused_types_walk_attribs (die);
22725 /* If this node is a specification,
22726 also mark the definition, if it exists. */
22727 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22728 prune_unused_types_mark (die->die_definition, 1);
22731 if (dokids && die->die_mark != 2)
22733 /* We need to walk the children, but haven't done so yet.
22734 Remember that we've walked the kids. */
22735 die->die_mark = 2;
22737 /* If this is an array type, we need to make sure our
22738 kids get marked, even if they're types. If we're
22739 breaking out types into comdat sections, do this
22740 for all type definitions. */
22741 if (die->die_tag == DW_TAG_array_type
22742 || (use_debug_types
22743 && is_type_die (die) && ! is_declaration_die (die)))
22744 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22745 else
22746 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22750 /* For local classes, look if any static member functions were emitted
22751 and if so, mark them. */
22753 static void
22754 prune_unused_types_walk_local_classes (dw_die_ref die)
22756 dw_die_ref c;
22758 if (die->die_mark == 2)
22759 return;
22761 switch (die->die_tag)
22763 case DW_TAG_structure_type:
22764 case DW_TAG_union_type:
22765 case DW_TAG_class_type:
22766 break;
22768 case DW_TAG_subprogram:
22769 if (!get_AT_flag (die, DW_AT_declaration)
22770 || die->die_definition != NULL)
22771 prune_unused_types_mark (die, 1);
22772 return;
22774 default:
22775 return;
22778 /* Mark children. */
22779 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22782 /* Walk the tree DIE and mark types that we actually use. */
22784 static void
22785 prune_unused_types_walk (dw_die_ref die)
22787 dw_die_ref c;
22789 /* Don't do anything if this node is already marked and
22790 children have been marked as well. */
22791 if (die->die_mark == 2)
22792 return;
22794 switch (die->die_tag)
22796 case DW_TAG_structure_type:
22797 case DW_TAG_union_type:
22798 case DW_TAG_class_type:
22799 if (die->die_perennial_p)
22800 break;
22802 for (c = die->die_parent; c; c = c->die_parent)
22803 if (c->die_tag == DW_TAG_subprogram)
22804 break;
22806 /* Finding used static member functions inside of classes
22807 is needed just for local classes, because for other classes
22808 static member function DIEs with DW_AT_specification
22809 are emitted outside of the DW_TAG_*_type. If we ever change
22810 it, we'd need to call this even for non-local classes. */
22811 if (c)
22812 prune_unused_types_walk_local_classes (die);
22814 /* It's a type node --- don't mark it. */
22815 return;
22817 case DW_TAG_const_type:
22818 case DW_TAG_packed_type:
22819 case DW_TAG_pointer_type:
22820 case DW_TAG_reference_type:
22821 case DW_TAG_rvalue_reference_type:
22822 case DW_TAG_volatile_type:
22823 case DW_TAG_typedef:
22824 case DW_TAG_array_type:
22825 case DW_TAG_interface_type:
22826 case DW_TAG_friend:
22827 case DW_TAG_variant_part:
22828 case DW_TAG_enumeration_type:
22829 case DW_TAG_subroutine_type:
22830 case DW_TAG_string_type:
22831 case DW_TAG_set_type:
22832 case DW_TAG_subrange_type:
22833 case DW_TAG_ptr_to_member_type:
22834 case DW_TAG_file_type:
22835 if (die->die_perennial_p)
22836 break;
22838 /* It's a type node --- don't mark it. */
22839 return;
22841 default:
22842 /* Mark everything else. */
22843 break;
22846 if (die->die_mark == 0)
22848 die->die_mark = 1;
22850 /* Now, mark any dies referenced from here. */
22851 prune_unused_types_walk_attribs (die);
22854 die->die_mark = 2;
22856 /* Mark children. */
22857 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22860 /* Increment the string counts on strings referred to from DIE's
22861 attributes. */
22863 static void
22864 prune_unused_types_update_strings (dw_die_ref die)
22866 dw_attr_ref a;
22867 unsigned ix;
22869 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22870 if (AT_class (a) == dw_val_class_str)
22872 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22873 s->refcount++;
22874 /* Avoid unnecessarily putting strings that are used less than
22875 twice in the hash table. */
22876 if (s->refcount
22877 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22879 void ** slot;
22880 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22881 htab_hash_string (s->str),
22882 INSERT);
22883 gcc_assert (*slot == NULL);
22884 *slot = s;
22889 /* Remove from the tree DIE any dies that aren't marked. */
22891 static void
22892 prune_unused_types_prune (dw_die_ref die)
22894 dw_die_ref c;
22896 gcc_assert (die->die_mark);
22897 prune_unused_types_update_strings (die);
22899 if (! die->die_child)
22900 return;
22902 c = die->die_child;
22903 do {
22904 dw_die_ref prev = c;
22905 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22906 if (c == die->die_child)
22908 /* No marked children between 'prev' and the end of the list. */
22909 if (prev == c)
22910 /* No marked children at all. */
22911 die->die_child = NULL;
22912 else
22914 prev->die_sib = c->die_sib;
22915 die->die_child = prev;
22917 return;
22920 if (c != prev->die_sib)
22921 prev->die_sib = c;
22922 prune_unused_types_prune (c);
22923 } while (c != die->die_child);
22926 /* Remove dies representing declarations that we never use. */
22928 static void
22929 prune_unused_types (void)
22931 unsigned int i;
22932 limbo_die_node *node;
22933 comdat_type_node *ctnode;
22934 pubname_ref pub;
22935 dw_die_ref base_type;
22937 #if ENABLE_ASSERT_CHECKING
22938 /* All the marks should already be clear. */
22939 verify_marks_clear (comp_unit_die ());
22940 for (node = limbo_die_list; node; node = node->next)
22941 verify_marks_clear (node->die);
22942 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22943 verify_marks_clear (ctnode->root_die);
22944 #endif /* ENABLE_ASSERT_CHECKING */
22946 /* Mark types that are used in global variables. */
22947 premark_types_used_by_global_vars ();
22949 /* Set the mark on nodes that are actually used. */
22950 prune_unused_types_walk (comp_unit_die ());
22951 for (node = limbo_die_list; node; node = node->next)
22952 prune_unused_types_walk (node->die);
22953 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22955 prune_unused_types_walk (ctnode->root_die);
22956 prune_unused_types_mark (ctnode->type_die, 1);
22959 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22960 are unusual in that they are pubnames that are the children of pubtypes.
22961 They should only be marked via their parent DW_TAG_enumeration_type die,
22962 not as roots in themselves. */
22963 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22964 if (pub->die->die_tag != DW_TAG_enumerator)
22965 prune_unused_types_mark (pub->die, 1);
22966 for (i = 0; base_types.iterate (i, &base_type); i++)
22967 prune_unused_types_mark (base_type, 1);
22969 if (debug_str_hash)
22970 htab_empty (debug_str_hash);
22971 if (skeleton_debug_str_hash)
22972 htab_empty (skeleton_debug_str_hash);
22973 prune_unused_types_prune (comp_unit_die ());
22974 for (node = limbo_die_list; node; node = node->next)
22975 prune_unused_types_prune (node->die);
22976 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22977 prune_unused_types_prune (ctnode->root_die);
22979 /* Leave the marks clear. */
22980 prune_unmark_dies (comp_unit_die ());
22981 for (node = limbo_die_list; node; node = node->next)
22982 prune_unmark_dies (node->die);
22983 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22984 prune_unmark_dies (ctnode->root_die);
22987 /* Set the parameter to true if there are any relative pathnames in
22988 the file table. */
22989 static int
22990 file_table_relative_p (void ** slot, void *param)
22992 bool *p = (bool *) param;
22993 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22994 if (!IS_ABSOLUTE_PATH (d->filename))
22996 *p = true;
22997 return 0;
22999 return 1;
23002 /* Helpers to manipulate hash table of comdat type units. */
23004 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
23006 typedef comdat_type_node value_type;
23007 typedef comdat_type_node compare_type;
23008 static inline hashval_t hash (const value_type *);
23009 static inline bool equal (const value_type *, const compare_type *);
23012 inline hashval_t
23013 comdat_type_hasher::hash (const value_type *type_node)
23015 hashval_t h;
23016 memcpy (&h, type_node->signature, sizeof (h));
23017 return h;
23020 inline bool
23021 comdat_type_hasher::equal (const value_type *type_node_1,
23022 const compare_type *type_node_2)
23024 return (! memcmp (type_node_1->signature, type_node_2->signature,
23025 DWARF_TYPE_SIGNATURE_SIZE));
23028 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23029 to the location it would have been added, should we know its
23030 DECL_ASSEMBLER_NAME when we added other attributes. This will
23031 probably improve compactness of debug info, removing equivalent
23032 abbrevs, and hide any differences caused by deferring the
23033 computation of the assembler name, triggered by e.g. PCH. */
23035 static inline void
23036 move_linkage_attr (dw_die_ref die)
23038 unsigned ix = vec_safe_length (die->die_attr);
23039 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23041 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23042 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23044 while (--ix > 0)
23046 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23048 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23049 break;
23052 if (ix != vec_safe_length (die->die_attr) - 1)
23054 die->die_attr->pop ();
23055 die->die_attr->quick_insert (ix, linkage);
23059 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23060 referenced from typed stack ops and count how often they are used. */
23062 static void
23063 mark_base_types (dw_loc_descr_ref loc)
23065 dw_die_ref base_type = NULL;
23067 for (; loc; loc = loc->dw_loc_next)
23069 switch (loc->dw_loc_opc)
23071 case DW_OP_GNU_regval_type:
23072 case DW_OP_GNU_deref_type:
23073 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23074 break;
23075 case DW_OP_GNU_convert:
23076 case DW_OP_GNU_reinterpret:
23077 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23078 continue;
23079 /* FALLTHRU */
23080 case DW_OP_GNU_const_type:
23081 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23082 break;
23083 case DW_OP_GNU_entry_value:
23084 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23085 continue;
23086 default:
23087 continue;
23089 gcc_assert (base_type->die_parent == comp_unit_die ());
23090 if (base_type->die_mark)
23091 base_type->die_mark++;
23092 else
23094 base_types.safe_push (base_type);
23095 base_type->die_mark = 1;
23100 /* Comparison function for sorting marked base types. */
23102 static int
23103 base_type_cmp (const void *x, const void *y)
23105 dw_die_ref dx = *(const dw_die_ref *) x;
23106 dw_die_ref dy = *(const dw_die_ref *) y;
23107 unsigned int byte_size1, byte_size2;
23108 unsigned int encoding1, encoding2;
23109 if (dx->die_mark > dy->die_mark)
23110 return -1;
23111 if (dx->die_mark < dy->die_mark)
23112 return 1;
23113 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23114 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23115 if (byte_size1 < byte_size2)
23116 return 1;
23117 if (byte_size1 > byte_size2)
23118 return -1;
23119 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23120 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23121 if (encoding1 < encoding2)
23122 return 1;
23123 if (encoding1 > encoding2)
23124 return -1;
23125 return 0;
23128 /* Move base types marked by mark_base_types as early as possible
23129 in the CU, sorted by decreasing usage count both to make the
23130 uleb128 references as small as possible and to make sure they
23131 will have die_offset already computed by calc_die_sizes when
23132 sizes of typed stack loc ops is computed. */
23134 static void
23135 move_marked_base_types (void)
23137 unsigned int i;
23138 dw_die_ref base_type, die, c;
23140 if (base_types.is_empty ())
23141 return;
23143 /* Sort by decreasing usage count, they will be added again in that
23144 order later on. */
23145 base_types.qsort (base_type_cmp);
23146 die = comp_unit_die ();
23147 c = die->die_child;
23150 dw_die_ref prev = c;
23151 c = c->die_sib;
23152 while (c->die_mark)
23154 remove_child_with_prev (c, prev);
23155 /* As base types got marked, there must be at least
23156 one node other than DW_TAG_base_type. */
23157 gcc_assert (c != c->die_sib);
23158 c = c->die_sib;
23161 while (c != die->die_child);
23162 gcc_assert (die->die_child);
23163 c = die->die_child;
23164 for (i = 0; base_types.iterate (i, &base_type); i++)
23166 base_type->die_mark = 0;
23167 base_type->die_sib = c->die_sib;
23168 c->die_sib = base_type;
23169 c = base_type;
23173 /* Helper function for resolve_addr, attempt to resolve
23174 one CONST_STRING, return true if successful. Similarly verify that
23175 SYMBOL_REFs refer to variables emitted in the current CU. */
23177 static bool
23178 resolve_one_addr (rtx *addr)
23180 rtx rtl = *addr;
23182 if (GET_CODE (rtl) == CONST_STRING)
23184 size_t len = strlen (XSTR (rtl, 0)) + 1;
23185 tree t = build_string (len, XSTR (rtl, 0));
23186 tree tlen = size_int (len - 1);
23187 TREE_TYPE (t)
23188 = build_array_type (char_type_node, build_index_type (tlen));
23189 rtl = lookup_constant_def (t);
23190 if (!rtl || !MEM_P (rtl))
23191 return false;
23192 rtl = XEXP (rtl, 0);
23193 if (GET_CODE (rtl) == SYMBOL_REF
23194 && SYMBOL_REF_DECL (rtl)
23195 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23196 return false;
23197 vec_safe_push (used_rtx_array, rtl);
23198 *addr = rtl;
23199 return true;
23202 if (GET_CODE (rtl) == SYMBOL_REF
23203 && SYMBOL_REF_DECL (rtl))
23205 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23207 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23208 return false;
23210 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23211 return false;
23214 if (GET_CODE (rtl) == CONST)
23216 subrtx_ptr_iterator::array_type array;
23217 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23218 if (!resolve_one_addr (*iter))
23219 return false;
23222 return true;
23225 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23226 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23227 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23229 static rtx
23230 string_cst_pool_decl (tree t)
23232 rtx rtl = output_constant_def (t, 1);
23233 unsigned char *array;
23234 dw_loc_descr_ref l;
23235 tree decl;
23236 size_t len;
23237 dw_die_ref ref;
23239 if (!rtl || !MEM_P (rtl))
23240 return NULL_RTX;
23241 rtl = XEXP (rtl, 0);
23242 if (GET_CODE (rtl) != SYMBOL_REF
23243 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23244 return NULL_RTX;
23246 decl = SYMBOL_REF_DECL (rtl);
23247 if (!lookup_decl_die (decl))
23249 len = TREE_STRING_LENGTH (t);
23250 vec_safe_push (used_rtx_array, rtl);
23251 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23252 array = ggc_vec_alloc<unsigned char> (len);
23253 memcpy (array, TREE_STRING_POINTER (t), len);
23254 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23255 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23256 l->dw_loc_oprnd2.v.val_vec.length = len;
23257 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23258 l->dw_loc_oprnd2.v.val_vec.array = array;
23259 add_AT_loc (ref, DW_AT_location, l);
23260 equate_decl_number_to_die (decl, ref);
23262 return rtl;
23265 /* Helper function of resolve_addr_in_expr. LOC is
23266 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23267 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23268 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23269 with DW_OP_GNU_implicit_pointer if possible
23270 and return true, if unsuccessful, return false. */
23272 static bool
23273 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23275 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23276 HOST_WIDE_INT offset = 0;
23277 dw_die_ref ref = NULL;
23278 tree decl;
23280 if (GET_CODE (rtl) == CONST
23281 && GET_CODE (XEXP (rtl, 0)) == PLUS
23282 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23284 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23285 rtl = XEXP (XEXP (rtl, 0), 0);
23287 if (GET_CODE (rtl) == CONST_STRING)
23289 size_t len = strlen (XSTR (rtl, 0)) + 1;
23290 tree t = build_string (len, XSTR (rtl, 0));
23291 tree tlen = size_int (len - 1);
23293 TREE_TYPE (t)
23294 = build_array_type (char_type_node, build_index_type (tlen));
23295 rtl = string_cst_pool_decl (t);
23296 if (!rtl)
23297 return false;
23299 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23301 decl = SYMBOL_REF_DECL (rtl);
23302 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23304 ref = lookup_decl_die (decl);
23305 if (ref && (get_AT (ref, DW_AT_location)
23306 || get_AT (ref, DW_AT_const_value)))
23308 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23309 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23310 loc->dw_loc_oprnd1.val_entry = NULL;
23311 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23312 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23313 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23314 loc->dw_loc_oprnd2.v.val_int = offset;
23315 return true;
23319 return false;
23322 /* Helper function for resolve_addr, handle one location
23323 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23324 the location list couldn't be resolved. */
23326 static bool
23327 resolve_addr_in_expr (dw_loc_descr_ref loc)
23329 dw_loc_descr_ref keep = NULL;
23330 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23331 switch (loc->dw_loc_opc)
23333 case DW_OP_addr:
23334 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23336 if ((prev == NULL
23337 || prev->dw_loc_opc == DW_OP_piece
23338 || prev->dw_loc_opc == DW_OP_bit_piece)
23339 && loc->dw_loc_next
23340 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23341 && !dwarf_strict
23342 && optimize_one_addr_into_implicit_ptr (loc))
23343 break;
23344 return false;
23346 break;
23347 case DW_OP_GNU_addr_index:
23348 case DW_OP_GNU_const_index:
23349 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23350 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23352 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23353 if (!resolve_one_addr (&rtl))
23354 return false;
23355 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23356 loc->dw_loc_oprnd1.val_entry =
23357 add_addr_table_entry (rtl, ate_kind_rtx);
23359 break;
23360 case DW_OP_const4u:
23361 case DW_OP_const8u:
23362 if (loc->dtprel
23363 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23364 return false;
23365 break;
23366 case DW_OP_plus_uconst:
23367 if (size_of_loc_descr (loc)
23368 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23370 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23372 dw_loc_descr_ref repl
23373 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23374 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23375 add_loc_descr (&repl, loc->dw_loc_next);
23376 *loc = *repl;
23378 break;
23379 case DW_OP_implicit_value:
23380 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23381 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23382 return false;
23383 break;
23384 case DW_OP_GNU_implicit_pointer:
23385 case DW_OP_GNU_parameter_ref:
23386 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23388 dw_die_ref ref
23389 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23390 if (ref == NULL)
23391 return false;
23392 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23393 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23394 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23396 break;
23397 case DW_OP_GNU_const_type:
23398 case DW_OP_GNU_regval_type:
23399 case DW_OP_GNU_deref_type:
23400 case DW_OP_GNU_convert:
23401 case DW_OP_GNU_reinterpret:
23402 while (loc->dw_loc_next
23403 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23405 dw_die_ref base1, base2;
23406 unsigned enc1, enc2, size1, size2;
23407 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23408 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23409 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23410 else if (loc->dw_loc_oprnd1.val_class
23411 == dw_val_class_unsigned_const)
23412 break;
23413 else
23414 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23415 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23416 == dw_val_class_unsigned_const)
23417 break;
23418 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23419 gcc_assert (base1->die_tag == DW_TAG_base_type
23420 && base2->die_tag == DW_TAG_base_type);
23421 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23422 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23423 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23424 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23425 if (size1 == size2
23426 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23427 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23428 && loc != keep)
23429 || enc1 == enc2))
23431 /* Optimize away next DW_OP_GNU_convert after
23432 adjusting LOC's base type die reference. */
23433 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23434 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23435 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23436 else
23437 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23438 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23439 continue;
23441 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23442 point typed stack entry. */
23443 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23444 keep = loc->dw_loc_next;
23445 break;
23447 break;
23448 default:
23449 break;
23451 return true;
23454 /* Helper function of resolve_addr. DIE had DW_AT_location of
23455 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23456 and DW_OP_addr couldn't be resolved. resolve_addr has already
23457 removed the DW_AT_location attribute. This function attempts to
23458 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23459 to it or DW_AT_const_value attribute, if possible. */
23461 static void
23462 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23464 if (TREE_CODE (decl) != VAR_DECL
23465 || lookup_decl_die (decl) != die
23466 || DECL_EXTERNAL (decl)
23467 || !TREE_STATIC (decl)
23468 || DECL_INITIAL (decl) == NULL_TREE
23469 || DECL_P (DECL_INITIAL (decl))
23470 || get_AT (die, DW_AT_const_value))
23471 return;
23473 tree init = DECL_INITIAL (decl);
23474 HOST_WIDE_INT offset = 0;
23475 /* For variables that have been optimized away and thus
23476 don't have a memory location, see if we can emit
23477 DW_AT_const_value instead. */
23478 if (tree_add_const_value_attribute (die, init))
23479 return;
23480 if (dwarf_strict)
23481 return;
23482 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23483 and ADDR_EXPR refers to a decl that has DW_AT_location or
23484 DW_AT_const_value (but isn't addressable, otherwise
23485 resolving the original DW_OP_addr wouldn't fail), see if
23486 we can add DW_OP_GNU_implicit_pointer. */
23487 STRIP_NOPS (init);
23488 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23489 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23491 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23492 init = TREE_OPERAND (init, 0);
23493 STRIP_NOPS (init);
23495 if (TREE_CODE (init) != ADDR_EXPR)
23496 return;
23497 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23498 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23499 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23500 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23501 && TREE_OPERAND (init, 0) != decl))
23503 dw_die_ref ref;
23504 dw_loc_descr_ref l;
23506 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23508 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23509 if (!rtl)
23510 return;
23511 decl = SYMBOL_REF_DECL (rtl);
23513 else
23514 decl = TREE_OPERAND (init, 0);
23515 ref = lookup_decl_die (decl);
23516 if (ref == NULL
23517 || (!get_AT (ref, DW_AT_location)
23518 && !get_AT (ref, DW_AT_const_value)))
23519 return;
23520 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23521 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23522 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23523 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23524 add_AT_loc (die, DW_AT_location, l);
23528 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23529 an address in .rodata section if the string literal is emitted there,
23530 or remove the containing location list or replace DW_AT_const_value
23531 with DW_AT_location and empty location expression, if it isn't found
23532 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23533 to something that has been emitted in the current CU. */
23535 static void
23536 resolve_addr (dw_die_ref die)
23538 dw_die_ref c;
23539 dw_attr_ref a;
23540 dw_loc_list_ref *curr, *start, loc;
23541 unsigned ix;
23543 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23544 switch (AT_class (a))
23546 case dw_val_class_loc_list:
23547 start = curr = AT_loc_list_ptr (a);
23548 loc = *curr;
23549 gcc_assert (loc);
23550 /* The same list can be referenced more than once. See if we have
23551 already recorded the result from a previous pass. */
23552 if (loc->replaced)
23553 *curr = loc->dw_loc_next;
23554 else if (!loc->resolved_addr)
23556 /* As things stand, we do not expect or allow one die to
23557 reference a suffix of another die's location list chain.
23558 References must be identical or completely separate.
23559 There is therefore no need to cache the result of this
23560 pass on any list other than the first; doing so
23561 would lead to unnecessary writes. */
23562 while (*curr)
23564 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23565 if (!resolve_addr_in_expr ((*curr)->expr))
23567 dw_loc_list_ref next = (*curr)->dw_loc_next;
23568 dw_loc_descr_ref l = (*curr)->expr;
23570 if (next && (*curr)->ll_symbol)
23572 gcc_assert (!next->ll_symbol);
23573 next->ll_symbol = (*curr)->ll_symbol;
23575 if (dwarf_split_debug_info)
23576 remove_loc_list_addr_table_entries (l);
23577 *curr = next;
23579 else
23581 mark_base_types ((*curr)->expr);
23582 curr = &(*curr)->dw_loc_next;
23585 if (loc == *start)
23586 loc->resolved_addr = 1;
23587 else
23589 loc->replaced = 1;
23590 loc->dw_loc_next = *start;
23593 if (!*start)
23595 remove_AT (die, a->dw_attr);
23596 ix--;
23598 break;
23599 case dw_val_class_loc:
23601 dw_loc_descr_ref l = AT_loc (a);
23602 /* For -gdwarf-2 don't attempt to optimize
23603 DW_AT_data_member_location containing
23604 DW_OP_plus_uconst - older consumers might
23605 rely on it being that op instead of a more complex,
23606 but shorter, location description. */
23607 if ((dwarf_version > 2
23608 || a->dw_attr != DW_AT_data_member_location
23609 || l == NULL
23610 || l->dw_loc_opc != DW_OP_plus_uconst
23611 || l->dw_loc_next != NULL)
23612 && !resolve_addr_in_expr (l))
23614 if (dwarf_split_debug_info)
23615 remove_loc_list_addr_table_entries (l);
23616 if (l != NULL
23617 && l->dw_loc_next == NULL
23618 && l->dw_loc_opc == DW_OP_addr
23619 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23620 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23621 && a->dw_attr == DW_AT_location)
23623 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23624 remove_AT (die, a->dw_attr);
23625 ix--;
23626 optimize_location_into_implicit_ptr (die, decl);
23627 break;
23629 remove_AT (die, a->dw_attr);
23630 ix--;
23632 else
23633 mark_base_types (l);
23635 break;
23636 case dw_val_class_addr:
23637 if (a->dw_attr == DW_AT_const_value
23638 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23640 if (AT_index (a) != NOT_INDEXED)
23641 remove_addr_table_entry (a->dw_attr_val.val_entry);
23642 remove_AT (die, a->dw_attr);
23643 ix--;
23645 if (die->die_tag == DW_TAG_GNU_call_site
23646 && a->dw_attr == DW_AT_abstract_origin)
23648 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23649 dw_die_ref tdie = lookup_decl_die (tdecl);
23650 if (tdie == NULL
23651 && DECL_EXTERNAL (tdecl)
23652 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23654 force_decl_die (tdecl);
23655 tdie = lookup_decl_die (tdecl);
23657 if (tdie)
23659 a->dw_attr_val.val_class = dw_val_class_die_ref;
23660 a->dw_attr_val.v.val_die_ref.die = tdie;
23661 a->dw_attr_val.v.val_die_ref.external = 0;
23663 else
23665 if (AT_index (a) != NOT_INDEXED)
23666 remove_addr_table_entry (a->dw_attr_val.val_entry);
23667 remove_AT (die, a->dw_attr);
23668 ix--;
23671 break;
23672 default:
23673 break;
23676 FOR_EACH_CHILD (die, c, resolve_addr (c));
23679 /* Helper routines for optimize_location_lists.
23680 This pass tries to share identical local lists in .debug_loc
23681 section. */
23683 /* Iteratively hash operands of LOC opcode into HSTATE. */
23685 static void
23686 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23688 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23689 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23691 switch (loc->dw_loc_opc)
23693 case DW_OP_const4u:
23694 case DW_OP_const8u:
23695 if (loc->dtprel)
23696 goto hash_addr;
23697 /* FALLTHRU */
23698 case DW_OP_const1u:
23699 case DW_OP_const1s:
23700 case DW_OP_const2u:
23701 case DW_OP_const2s:
23702 case DW_OP_const4s:
23703 case DW_OP_const8s:
23704 case DW_OP_constu:
23705 case DW_OP_consts:
23706 case DW_OP_pick:
23707 case DW_OP_plus_uconst:
23708 case DW_OP_breg0:
23709 case DW_OP_breg1:
23710 case DW_OP_breg2:
23711 case DW_OP_breg3:
23712 case DW_OP_breg4:
23713 case DW_OP_breg5:
23714 case DW_OP_breg6:
23715 case DW_OP_breg7:
23716 case DW_OP_breg8:
23717 case DW_OP_breg9:
23718 case DW_OP_breg10:
23719 case DW_OP_breg11:
23720 case DW_OP_breg12:
23721 case DW_OP_breg13:
23722 case DW_OP_breg14:
23723 case DW_OP_breg15:
23724 case DW_OP_breg16:
23725 case DW_OP_breg17:
23726 case DW_OP_breg18:
23727 case DW_OP_breg19:
23728 case DW_OP_breg20:
23729 case DW_OP_breg21:
23730 case DW_OP_breg22:
23731 case DW_OP_breg23:
23732 case DW_OP_breg24:
23733 case DW_OP_breg25:
23734 case DW_OP_breg26:
23735 case DW_OP_breg27:
23736 case DW_OP_breg28:
23737 case DW_OP_breg29:
23738 case DW_OP_breg30:
23739 case DW_OP_breg31:
23740 case DW_OP_regx:
23741 case DW_OP_fbreg:
23742 case DW_OP_piece:
23743 case DW_OP_deref_size:
23744 case DW_OP_xderef_size:
23745 hstate.add_object (val1->v.val_int);
23746 break;
23747 case DW_OP_skip:
23748 case DW_OP_bra:
23750 int offset;
23752 gcc_assert (val1->val_class == dw_val_class_loc);
23753 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23754 hstate.add_object (offset);
23756 break;
23757 case DW_OP_implicit_value:
23758 hstate.add_object (val1->v.val_unsigned);
23759 switch (val2->val_class)
23761 case dw_val_class_const:
23762 hstate.add_object (val2->v.val_int);
23763 break;
23764 case dw_val_class_vec:
23766 unsigned int elt_size = val2->v.val_vec.elt_size;
23767 unsigned int len = val2->v.val_vec.length;
23769 hstate.add_int (elt_size);
23770 hstate.add_int (len);
23771 hstate.add (val2->v.val_vec.array, len * elt_size);
23773 break;
23774 case dw_val_class_const_double:
23775 hstate.add_object (val2->v.val_double.low);
23776 hstate.add_object (val2->v.val_double.high);
23777 break;
23778 case dw_val_class_wide_int:
23779 hstate.add_object (*val2->v.val_wide);
23780 break;
23781 case dw_val_class_addr:
23782 inchash::add_rtx (val2->v.val_addr, hstate);
23783 break;
23784 default:
23785 gcc_unreachable ();
23787 break;
23788 case DW_OP_bregx:
23789 case DW_OP_bit_piece:
23790 hstate.add_object (val1->v.val_int);
23791 hstate.add_object (val2->v.val_int);
23792 break;
23793 case DW_OP_addr:
23794 hash_addr:
23795 if (loc->dtprel)
23797 unsigned char dtprel = 0xd1;
23798 hstate.add_object (dtprel);
23800 inchash::add_rtx (val1->v.val_addr, hstate);
23801 break;
23802 case DW_OP_GNU_addr_index:
23803 case DW_OP_GNU_const_index:
23805 if (loc->dtprel)
23807 unsigned char dtprel = 0xd1;
23808 hstate.add_object (dtprel);
23810 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23812 break;
23813 case DW_OP_GNU_implicit_pointer:
23814 hstate.add_int (val2->v.val_int);
23815 break;
23816 case DW_OP_GNU_entry_value:
23817 hstate.add_object (val1->v.val_loc);
23818 break;
23819 case DW_OP_GNU_regval_type:
23820 case DW_OP_GNU_deref_type:
23822 unsigned int byte_size
23823 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23824 unsigned int encoding
23825 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23826 hstate.add_object (val1->v.val_int);
23827 hstate.add_object (byte_size);
23828 hstate.add_object (encoding);
23830 break;
23831 case DW_OP_GNU_convert:
23832 case DW_OP_GNU_reinterpret:
23833 if (val1->val_class == dw_val_class_unsigned_const)
23835 hstate.add_object (val1->v.val_unsigned);
23836 break;
23838 /* FALLTHRU */
23839 case DW_OP_GNU_const_type:
23841 unsigned int byte_size
23842 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23843 unsigned int encoding
23844 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23845 hstate.add_object (byte_size);
23846 hstate.add_object (encoding);
23847 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23848 break;
23849 hstate.add_object (val2->val_class);
23850 switch (val2->val_class)
23852 case dw_val_class_const:
23853 hstate.add_object (val2->v.val_int);
23854 break;
23855 case dw_val_class_vec:
23857 unsigned int elt_size = val2->v.val_vec.elt_size;
23858 unsigned int len = val2->v.val_vec.length;
23860 hstate.add_object (elt_size);
23861 hstate.add_object (len);
23862 hstate.add (val2->v.val_vec.array, len * elt_size);
23864 break;
23865 case dw_val_class_const_double:
23866 hstate.add_object (val2->v.val_double.low);
23867 hstate.add_object (val2->v.val_double.high);
23868 break;
23869 case dw_val_class_wide_int:
23870 hstate.add_object (*val2->v.val_wide);
23871 break;
23872 default:
23873 gcc_unreachable ();
23876 break;
23878 default:
23879 /* Other codes have no operands. */
23880 break;
23884 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23886 static inline void
23887 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23889 dw_loc_descr_ref l;
23890 bool sizes_computed = false;
23891 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23892 size_of_locs (loc);
23894 for (l = loc; l != NULL; l = l->dw_loc_next)
23896 enum dwarf_location_atom opc = l->dw_loc_opc;
23897 hstate.add_object (opc);
23898 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23900 size_of_locs (loc);
23901 sizes_computed = true;
23903 hash_loc_operands (l, hstate);
23907 /* Compute hash of the whole location list LIST_HEAD. */
23909 static inline void
23910 hash_loc_list (dw_loc_list_ref list_head)
23912 dw_loc_list_ref curr = list_head;
23913 inchash::hash hstate;
23915 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23917 hstate.add (curr->begin, strlen (curr->begin) + 1);
23918 hstate.add (curr->end, strlen (curr->end) + 1);
23919 if (curr->section)
23920 hstate.add (curr->section, strlen (curr->section) + 1);
23921 hash_locs (curr->expr, hstate);
23923 list_head->hash = hstate.end ();
23926 /* Return true if X and Y opcodes have the same operands. */
23928 static inline bool
23929 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23931 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23932 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23933 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23934 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23936 switch (x->dw_loc_opc)
23938 case DW_OP_const4u:
23939 case DW_OP_const8u:
23940 if (x->dtprel)
23941 goto hash_addr;
23942 /* FALLTHRU */
23943 case DW_OP_const1u:
23944 case DW_OP_const1s:
23945 case DW_OP_const2u:
23946 case DW_OP_const2s:
23947 case DW_OP_const4s:
23948 case DW_OP_const8s:
23949 case DW_OP_constu:
23950 case DW_OP_consts:
23951 case DW_OP_pick:
23952 case DW_OP_plus_uconst:
23953 case DW_OP_breg0:
23954 case DW_OP_breg1:
23955 case DW_OP_breg2:
23956 case DW_OP_breg3:
23957 case DW_OP_breg4:
23958 case DW_OP_breg5:
23959 case DW_OP_breg6:
23960 case DW_OP_breg7:
23961 case DW_OP_breg8:
23962 case DW_OP_breg9:
23963 case DW_OP_breg10:
23964 case DW_OP_breg11:
23965 case DW_OP_breg12:
23966 case DW_OP_breg13:
23967 case DW_OP_breg14:
23968 case DW_OP_breg15:
23969 case DW_OP_breg16:
23970 case DW_OP_breg17:
23971 case DW_OP_breg18:
23972 case DW_OP_breg19:
23973 case DW_OP_breg20:
23974 case DW_OP_breg21:
23975 case DW_OP_breg22:
23976 case DW_OP_breg23:
23977 case DW_OP_breg24:
23978 case DW_OP_breg25:
23979 case DW_OP_breg26:
23980 case DW_OP_breg27:
23981 case DW_OP_breg28:
23982 case DW_OP_breg29:
23983 case DW_OP_breg30:
23984 case DW_OP_breg31:
23985 case DW_OP_regx:
23986 case DW_OP_fbreg:
23987 case DW_OP_piece:
23988 case DW_OP_deref_size:
23989 case DW_OP_xderef_size:
23990 return valx1->v.val_int == valy1->v.val_int;
23991 case DW_OP_skip:
23992 case DW_OP_bra:
23993 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23994 can cause irrelevant differences in dw_loc_addr. */
23995 gcc_assert (valx1->val_class == dw_val_class_loc
23996 && valy1->val_class == dw_val_class_loc
23997 && (dwarf_split_debug_info
23998 || x->dw_loc_addr == y->dw_loc_addr));
23999 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24000 case DW_OP_implicit_value:
24001 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24002 || valx2->val_class != valy2->val_class)
24003 return false;
24004 switch (valx2->val_class)
24006 case dw_val_class_const:
24007 return valx2->v.val_int == valy2->v.val_int;
24008 case dw_val_class_vec:
24009 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24010 && valx2->v.val_vec.length == valy2->v.val_vec.length
24011 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24012 valx2->v.val_vec.elt_size
24013 * valx2->v.val_vec.length) == 0;
24014 case dw_val_class_const_double:
24015 return valx2->v.val_double.low == valy2->v.val_double.low
24016 && valx2->v.val_double.high == valy2->v.val_double.high;
24017 case dw_val_class_wide_int:
24018 return *valx2->v.val_wide == *valy2->v.val_wide;
24019 case dw_val_class_addr:
24020 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24021 default:
24022 gcc_unreachable ();
24024 case DW_OP_bregx:
24025 case DW_OP_bit_piece:
24026 return valx1->v.val_int == valy1->v.val_int
24027 && valx2->v.val_int == valy2->v.val_int;
24028 case DW_OP_addr:
24029 hash_addr:
24030 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24031 case DW_OP_GNU_addr_index:
24032 case DW_OP_GNU_const_index:
24034 rtx ax1 = valx1->val_entry->addr.rtl;
24035 rtx ay1 = valy1->val_entry->addr.rtl;
24036 return rtx_equal_p (ax1, ay1);
24038 case DW_OP_GNU_implicit_pointer:
24039 return valx1->val_class == dw_val_class_die_ref
24040 && valx1->val_class == valy1->val_class
24041 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24042 && valx2->v.val_int == valy2->v.val_int;
24043 case DW_OP_GNU_entry_value:
24044 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24045 case DW_OP_GNU_const_type:
24046 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24047 || valx2->val_class != valy2->val_class)
24048 return false;
24049 switch (valx2->val_class)
24051 case dw_val_class_const:
24052 return valx2->v.val_int == valy2->v.val_int;
24053 case dw_val_class_vec:
24054 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24055 && valx2->v.val_vec.length == valy2->v.val_vec.length
24056 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24057 valx2->v.val_vec.elt_size
24058 * valx2->v.val_vec.length) == 0;
24059 case dw_val_class_const_double:
24060 return valx2->v.val_double.low == valy2->v.val_double.low
24061 && valx2->v.val_double.high == valy2->v.val_double.high;
24062 case dw_val_class_wide_int:
24063 return *valx2->v.val_wide == *valy2->v.val_wide;
24064 default:
24065 gcc_unreachable ();
24067 case DW_OP_GNU_regval_type:
24068 case DW_OP_GNU_deref_type:
24069 return valx1->v.val_int == valy1->v.val_int
24070 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24071 case DW_OP_GNU_convert:
24072 case DW_OP_GNU_reinterpret:
24073 if (valx1->val_class != valy1->val_class)
24074 return false;
24075 if (valx1->val_class == dw_val_class_unsigned_const)
24076 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24077 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24078 case DW_OP_GNU_parameter_ref:
24079 return valx1->val_class == dw_val_class_die_ref
24080 && valx1->val_class == valy1->val_class
24081 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24082 default:
24083 /* Other codes have no operands. */
24084 return true;
24088 /* Return true if DWARF location expressions X and Y are the same. */
24090 static inline bool
24091 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24093 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24094 if (x->dw_loc_opc != y->dw_loc_opc
24095 || x->dtprel != y->dtprel
24096 || !compare_loc_operands (x, y))
24097 break;
24098 return x == NULL && y == NULL;
24101 /* Hashtable helpers. */
24103 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24105 typedef dw_loc_list_struct value_type;
24106 typedef dw_loc_list_struct compare_type;
24107 static inline hashval_t hash (const value_type *);
24108 static inline bool equal (const value_type *, const compare_type *);
24111 /* Return precomputed hash of location list X. */
24113 inline hashval_t
24114 loc_list_hasher::hash (const value_type *x)
24116 return x->hash;
24119 /* Return true if location lists A and B are the same. */
24121 inline bool
24122 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24124 if (a == b)
24125 return 1;
24126 if (a->hash != b->hash)
24127 return 0;
24128 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24129 if (strcmp (a->begin, b->begin) != 0
24130 || strcmp (a->end, b->end) != 0
24131 || (a->section == NULL) != (b->section == NULL)
24132 || (a->section && strcmp (a->section, b->section) != 0)
24133 || !compare_locs (a->expr, b->expr))
24134 break;
24135 return a == NULL && b == NULL;
24138 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24141 /* Recursively optimize location lists referenced from DIE
24142 children and share them whenever possible. */
24144 static void
24145 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24147 dw_die_ref c;
24148 dw_attr_ref a;
24149 unsigned ix;
24150 dw_loc_list_struct **slot;
24152 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24153 if (AT_class (a) == dw_val_class_loc_list)
24155 dw_loc_list_ref list = AT_loc_list (a);
24156 /* TODO: perform some optimizations here, before hashing
24157 it and storing into the hash table. */
24158 hash_loc_list (list);
24159 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24160 if (*slot == NULL)
24161 *slot = list;
24162 else
24163 a->dw_attr_val.v.val_loc_list = *slot;
24166 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24170 /* Recursively assign each location list a unique index into the debug_addr
24171 section. */
24173 static void
24174 index_location_lists (dw_die_ref die)
24176 dw_die_ref c;
24177 dw_attr_ref a;
24178 unsigned ix;
24180 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24181 if (AT_class (a) == dw_val_class_loc_list)
24183 dw_loc_list_ref list = AT_loc_list (a);
24184 dw_loc_list_ref curr;
24185 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24187 /* Don't index an entry that has already been indexed
24188 or won't be output. */
24189 if (curr->begin_entry != NULL
24190 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24191 continue;
24193 curr->begin_entry
24194 = add_addr_table_entry (xstrdup (curr->begin),
24195 ate_kind_label);
24199 FOR_EACH_CHILD (die, c, index_location_lists (c));
24202 /* Optimize location lists referenced from DIE
24203 children and share them whenever possible. */
24205 static void
24206 optimize_location_lists (dw_die_ref die)
24208 loc_list_hash_type htab (500);
24209 optimize_location_lists_1 (die, &htab);
24212 /* Output stuff that dwarf requires at the end of every file,
24213 and generate the DWARF-2 debugging info. */
24215 static void
24216 dwarf2out_finish (const char *filename)
24218 limbo_die_node *node, *next_node;
24219 comdat_type_node *ctnode;
24220 unsigned int i;
24221 dw_die_ref main_comp_unit_die;
24223 /* PCH might result in DW_AT_producer string being restored from the
24224 header compilation, so always fill it with empty string initially
24225 and overwrite only here. */
24226 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24227 producer_string = gen_producer_string ();
24228 producer->dw_attr_val.v.val_str->refcount--;
24229 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24231 gen_scheduled_generic_parms_dies ();
24232 gen_remaining_tmpl_value_param_die_attribute ();
24234 /* Add the name for the main input file now. We delayed this from
24235 dwarf2out_init to avoid complications with PCH. */
24236 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24237 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24238 add_comp_dir_attribute (comp_unit_die ());
24239 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24241 bool p = false;
24242 htab_traverse (file_table, file_table_relative_p, &p);
24243 if (p)
24244 add_comp_dir_attribute (comp_unit_die ());
24247 if (deferred_locations_list)
24248 for (i = 0; i < deferred_locations_list->length (); i++)
24250 add_location_or_const_value_attribute (
24251 (*deferred_locations_list)[i].die,
24252 (*deferred_locations_list)[i].variable,
24253 false,
24254 DW_AT_location);
24257 /* Traverse the limbo die list, and add parent/child links. The only
24258 dies without parents that should be here are concrete instances of
24259 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24260 For concrete instances, we can get the parent die from the abstract
24261 instance. */
24262 for (node = limbo_die_list; node; node = next_node)
24264 dw_die_ref die = node->die;
24265 next_node = node->next;
24267 if (die->die_parent == NULL)
24269 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24271 if (origin && origin->die_parent)
24272 add_child_die (origin->die_parent, die);
24273 else if (is_cu_die (die))
24275 else if (seen_error ())
24276 /* It's OK to be confused by errors in the input. */
24277 add_child_die (comp_unit_die (), die);
24278 else
24280 /* In certain situations, the lexical block containing a
24281 nested function can be optimized away, which results
24282 in the nested function die being orphaned. Likewise
24283 with the return type of that nested function. Force
24284 this to be a child of the containing function.
24286 It may happen that even the containing function got fully
24287 inlined and optimized out. In that case we are lost and
24288 assign the empty child. This should not be big issue as
24289 the function is likely unreachable too. */
24290 gcc_assert (node->created_for);
24292 if (DECL_P (node->created_for))
24293 origin = get_context_die (DECL_CONTEXT (node->created_for));
24294 else if (TYPE_P (node->created_for))
24295 origin = scope_die_for (node->created_for, comp_unit_die ());
24296 else
24297 origin = comp_unit_die ();
24299 add_child_die (origin, die);
24304 limbo_die_list = NULL;
24306 #if ENABLE_ASSERT_CHECKING
24308 dw_die_ref die = comp_unit_die (), c;
24309 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24311 #endif
24312 resolve_addr (comp_unit_die ());
24313 move_marked_base_types ();
24315 for (node = deferred_asm_name; node; node = node->next)
24317 tree decl = node->created_for;
24318 /* When generating LTO bytecode we can not generate new assembler
24319 names at this point and all important decls got theirs via
24320 free-lang-data. */
24321 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24322 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24324 add_linkage_attr (node->die, decl);
24325 move_linkage_attr (node->die);
24329 deferred_asm_name = NULL;
24331 /* Walk through the list of incomplete types again, trying once more to
24332 emit full debugging info for them. */
24333 retry_incomplete_types ();
24335 if (flag_eliminate_unused_debug_types)
24336 prune_unused_types ();
24338 /* Generate separate COMDAT sections for type DIEs. */
24339 if (use_debug_types)
24341 break_out_comdat_types (comp_unit_die ());
24343 /* Each new type_unit DIE was added to the limbo die list when created.
24344 Since these have all been added to comdat_type_list, clear the
24345 limbo die list. */
24346 limbo_die_list = NULL;
24348 /* For each new comdat type unit, copy declarations for incomplete
24349 types to make the new unit self-contained (i.e., no direct
24350 references to the main compile unit). */
24351 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24352 copy_decls_for_unworthy_types (ctnode->root_die);
24353 copy_decls_for_unworthy_types (comp_unit_die ());
24355 /* In the process of copying declarations from one unit to another,
24356 we may have left some declarations behind that are no longer
24357 referenced. Prune them. */
24358 prune_unused_types ();
24361 /* Generate separate CUs for each of the include files we've seen.
24362 They will go into limbo_die_list. */
24363 if (flag_eliminate_dwarf2_dups)
24364 break_out_includes (comp_unit_die ());
24366 /* Traverse the DIE's and add add sibling attributes to those DIE's
24367 that have children. */
24368 add_sibling_attributes (comp_unit_die ());
24369 for (node = limbo_die_list; node; node = node->next)
24370 add_sibling_attributes (node->die);
24371 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24372 add_sibling_attributes (ctnode->root_die);
24374 /* When splitting DWARF info, we put some attributes in the
24375 skeleton compile_unit DIE that remains in the .o, while
24376 most attributes go in the DWO compile_unit_die. */
24377 if (dwarf_split_debug_info)
24378 main_comp_unit_die = gen_compile_unit_die (NULL);
24379 else
24380 main_comp_unit_die = comp_unit_die ();
24382 /* Output a terminator label for the .text section. */
24383 switch_to_section (text_section);
24384 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24385 if (cold_text_section)
24387 switch_to_section (cold_text_section);
24388 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24391 /* We can only use the low/high_pc attributes if all of the code was
24392 in .text. */
24393 if (!have_multiple_function_sections
24394 || (dwarf_version < 3 && dwarf_strict))
24396 /* Don't add if the CU has no associated code. */
24397 if (text_section_used)
24398 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24399 text_end_label, true);
24401 else
24403 unsigned fde_idx;
24404 dw_fde_ref fde;
24405 bool range_list_added = false;
24407 if (text_section_used)
24408 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24409 text_end_label, &range_list_added, true);
24410 if (cold_text_section_used)
24411 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24412 cold_end_label, &range_list_added, true);
24414 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24416 if (DECL_IGNORED_P (fde->decl))
24417 continue;
24418 if (!fde->in_std_section)
24419 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24420 fde->dw_fde_end, &range_list_added,
24421 true);
24422 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24423 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24424 fde->dw_fde_second_end, &range_list_added,
24425 true);
24428 if (range_list_added)
24430 /* We need to give .debug_loc and .debug_ranges an appropriate
24431 "base address". Use zero so that these addresses become
24432 absolute. Historically, we've emitted the unexpected
24433 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24434 Emit both to give time for other tools to adapt. */
24435 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24436 if (! dwarf_strict && dwarf_version < 4)
24437 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24439 add_ranges (NULL);
24443 if (debug_info_level >= DINFO_LEVEL_TERSE)
24444 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24445 debug_line_section_label);
24447 if (have_macinfo)
24448 add_AT_macptr (comp_unit_die (),
24449 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24450 macinfo_section_label);
24452 if (dwarf_split_debug_info)
24454 /* optimize_location_lists calculates the size of the lists,
24455 so index them first, and assign indices to the entries.
24456 Although optimize_location_lists will remove entries from
24457 the table, it only does so for duplicates, and therefore
24458 only reduces ref_counts to 1. */
24459 index_location_lists (comp_unit_die ());
24461 if (addr_index_table != NULL)
24463 unsigned int index = 0;
24464 htab_traverse_noresize (addr_index_table,
24465 index_addr_table_entry, &index);
24469 if (have_location_lists)
24470 optimize_location_lists (comp_unit_die ());
24472 save_macinfo_strings ();
24474 if (dwarf_split_debug_info)
24476 unsigned int index = 0;
24478 /* Add attributes common to skeleton compile_units and
24479 type_units. Because these attributes include strings, it
24480 must be done before freezing the string table. Top-level
24481 skeleton die attrs are added when the skeleton type unit is
24482 created, so ensure it is created by this point. */
24483 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24484 htab_traverse_noresize (debug_str_hash, index_string, &index);
24487 /* Output all of the compilation units. We put the main one last so that
24488 the offsets are available to output_pubnames. */
24489 for (node = limbo_die_list; node; node = node->next)
24490 output_comp_unit (node->die, 0);
24492 hash_table<comdat_type_hasher> comdat_type_table (100);
24493 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24495 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24497 /* Don't output duplicate types. */
24498 if (*slot != HTAB_EMPTY_ENTRY)
24499 continue;
24501 /* Add a pointer to the line table for the main compilation unit
24502 so that the debugger can make sense of DW_AT_decl_file
24503 attributes. */
24504 if (debug_info_level >= DINFO_LEVEL_TERSE)
24505 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24506 (!dwarf_split_debug_info
24507 ? debug_line_section_label
24508 : debug_skeleton_line_section_label));
24510 output_comdat_type_unit (ctnode);
24511 *slot = ctnode;
24514 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24515 both the main_cu and all skeleton TUs. Making this call unconditional
24516 would end up either adding a second copy of the AT_pubnames attribute, or
24517 requiring a special case in add_top_level_skeleton_die_attrs. */
24518 if (!dwarf_split_debug_info)
24519 add_AT_pubnames (comp_unit_die ());
24521 if (dwarf_split_debug_info)
24523 int mark;
24524 unsigned char checksum[16];
24525 struct md5_ctx ctx;
24527 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24528 md5_init_ctx (&ctx);
24529 mark = 0;
24530 die_checksum (comp_unit_die (), &ctx, &mark);
24531 unmark_all_dies (comp_unit_die ());
24532 md5_finish_ctx (&ctx, checksum);
24534 /* Use the first 8 bytes of the checksum as the dwo_id,
24535 and add it to both comp-unit DIEs. */
24536 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24537 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24539 /* Add the base offset of the ranges table to the skeleton
24540 comp-unit DIE. */
24541 if (ranges_table_in_use)
24542 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24543 ranges_section_label);
24545 switch_to_section (debug_addr_section);
24546 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24547 output_addr_table ();
24550 /* Output the main compilation unit if non-empty or if .debug_macinfo
24551 or .debug_macro will be emitted. */
24552 output_comp_unit (comp_unit_die (), have_macinfo);
24554 if (dwarf_split_debug_info && info_section_emitted)
24555 output_skeleton_debug_sections (main_comp_unit_die);
24557 /* Output the abbreviation table. */
24558 if (abbrev_die_table_in_use != 1)
24560 switch_to_section (debug_abbrev_section);
24561 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24562 output_abbrev_section ();
24565 /* Output location list section if necessary. */
24566 if (have_location_lists)
24568 /* Output the location lists info. */
24569 switch_to_section (debug_loc_section);
24570 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24571 output_location_lists (comp_unit_die ());
24574 output_pubtables ();
24576 /* Output the address range information if a CU (.debug_info section)
24577 was emitted. We output an empty table even if we had no functions
24578 to put in it. This because the consumer has no way to tell the
24579 difference between an empty table that we omitted and failure to
24580 generate a table that would have contained data. */
24581 if (info_section_emitted)
24583 unsigned long aranges_length = size_of_aranges ();
24585 switch_to_section (debug_aranges_section);
24586 output_aranges (aranges_length);
24589 /* Output ranges section if necessary. */
24590 if (ranges_table_in_use)
24592 switch_to_section (debug_ranges_section);
24593 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24594 output_ranges ();
24597 /* Have to end the macro section. */
24598 if (have_macinfo)
24600 switch_to_section (debug_macinfo_section);
24601 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24602 output_macinfo ();
24603 dw2_asm_output_data (1, 0, "End compilation unit");
24606 /* Output the source line correspondence table. We must do this
24607 even if there is no line information. Otherwise, on an empty
24608 translation unit, we will generate a present, but empty,
24609 .debug_info section. IRIX 6.5 `nm' will then complain when
24610 examining the file. This is done late so that any filenames
24611 used by the debug_info section are marked as 'used'. */
24612 switch_to_section (debug_line_section);
24613 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24614 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24615 output_line_info (false);
24617 if (dwarf_split_debug_info && info_section_emitted)
24619 switch_to_section (debug_skeleton_line_section);
24620 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24621 output_line_info (true);
24624 /* If we emitted any indirect strings, output the string table too. */
24625 if (debug_str_hash || skeleton_debug_str_hash)
24626 output_indirect_strings ();
24629 #include "gt-dwarf2out.h"