PR target/61423
[official-gcc.git] / gcc / dwarf2out.c
blob7c930746e7b384cc2700ada807dbb9397f3ed0b4
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 "stringpool.h"
65 #include "stor-layout.h"
66 #include "varasm.h"
67 #include "function.h"
68 #include "emit-rtl.h"
69 #include "hash-table.h"
70 #include "version.h"
71 #include "flags.h"
72 #include "hard-reg-set.h"
73 #include "regs.h"
74 #include "insn-config.h"
75 #include "reload.h"
76 #include "function.h"
77 #include "output.h"
78 #include "expr.h"
79 #include "except.h"
80 #include "dwarf2.h"
81 #include "dwarf2out.h"
82 #include "dwarf2asm.h"
83 #include "toplev.h"
84 #include "md5.h"
85 #include "tm_p.h"
86 #include "diagnostic.h"
87 #include "tree-pretty-print.h"
88 #include "debug.h"
89 #include "target.h"
90 #include "common/common-target.h"
91 #include "langhooks.h"
92 #include "cgraph.h"
93 #include "input.h"
94 #include "ira.h"
95 #include "lra.h"
96 #include "dumpfile.h"
97 #include "opts.h"
98 #include "tree-dfa.h"
99 #include "gdb/gdb-index.h"
101 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
102 static rtx last_var_location_insn;
103 static rtx cached_next_real_insn;
105 #ifdef VMS_DEBUGGING_INFO
106 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
108 /* Define this macro to be a nonzero value if the directory specifications
109 which are output in the debug info should end with a separator. */
110 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
111 /* Define this macro to evaluate to a nonzero value if GCC should refrain
112 from generating indirect strings in DWARF2 debug information, for instance
113 if your target is stuck with an old version of GDB that is unable to
114 process them properly or uses VMS Debug. */
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
116 #else
117 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
119 #endif
121 /* ??? Poison these here until it can be done generically. They've been
122 totally replaced in this file; make sure it stays that way. */
123 #undef DWARF2_UNWIND_INFO
124 #undef DWARF2_FRAME_INFO
125 #if (GCC_VERSION >= 3000)
126 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
127 #endif
129 /* The size of the target's pointer type. */
130 #ifndef PTR_SIZE
131 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
132 #endif
134 /* Array of RTXes referenced by the debugging information, which therefore
135 must be kept around forever. */
136 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
138 /* A pointer to the base of a list of incomplete types which might be
139 completed at some later time. incomplete_types_list needs to be a
140 vec<tree, va_gc> *because we want to tell the garbage collector about
141 it. */
142 static GTY(()) vec<tree, va_gc> *incomplete_types;
144 /* A pointer to the base of a table of references to declaration
145 scopes. This table is a display which tracks the nesting
146 of declaration scopes at the current scope and containing
147 scopes. This table is used to find the proper place to
148 define type declaration DIE's. */
149 static GTY(()) vec<tree, va_gc> *decl_scope_table;
151 /* Pointers to various DWARF2 sections. */
152 static GTY(()) section *debug_info_section;
153 static GTY(()) section *debug_skeleton_info_section;
154 static GTY(()) section *debug_abbrev_section;
155 static GTY(()) section *debug_skeleton_abbrev_section;
156 static GTY(()) section *debug_aranges_section;
157 static GTY(()) section *debug_addr_section;
158 static GTY(()) section *debug_macinfo_section;
159 static GTY(()) section *debug_line_section;
160 static GTY(()) section *debug_skeleton_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_str_dwo_section;
166 static GTY(()) section *debug_str_offsets_section;
167 static GTY(()) section *debug_ranges_section;
168 static GTY(()) section *debug_frame_section;
170 /* Maximum size (in bytes) of an artificially generated label. */
171 #define MAX_ARTIFICIAL_LABEL_BYTES 30
173 /* According to the (draft) DWARF 3 specification, the initial length
174 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
175 bytes are 0xffffffff, followed by the length stored in the next 8
176 bytes.
178 However, the SGI/MIPS ABI uses an initial length which is equal to
179 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
181 #ifndef DWARF_INITIAL_LENGTH_SIZE
182 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
183 #endif
185 /* Round SIZE up to the nearest BOUNDARY. */
186 #define DWARF_ROUND(SIZE,BOUNDARY) \
187 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
189 /* CIE identifier. */
190 #if HOST_BITS_PER_WIDE_INT >= 64
191 #define DWARF_CIE_ID \
192 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
193 #else
194 #define DWARF_CIE_ID DW_CIE_ID
195 #endif
198 /* A vector for a table that contains frame description
199 information for each routine. */
200 #define NOT_INDEXED (-1U)
201 #define NO_INDEX_ASSIGNED (-2U)
203 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
205 struct GTY(()) indirect_string_node {
206 const char *str;
207 unsigned int refcount;
208 enum dwarf_form form;
209 char *label;
210 unsigned int index;
213 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
215 /* With split_debug_info, both the comp_dir and dwo_name go in the
216 main object file, rather than the dwo, similar to the force_direct
217 parameter elsewhere but with additional complications:
219 1) The string is needed in both the main object file and the dwo.
220 That is, the comp_dir and dwo_name will appear in both places.
222 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
223 DW_FORM_GNU_str_index.
225 3) GCC chooses the form to use late, depending on the size and
226 reference count.
228 Rather than forcing the all debug string handling functions and
229 callers to deal with these complications, simply use a separate,
230 special-cased string table for any attribute that should go in the
231 main object file. This limits the complexity to just the places
232 that need it. */
234 static GTY ((param_is (struct indirect_string_node)))
235 htab_t skeleton_debug_str_hash;
237 static GTY(()) int dw2_string_counter;
239 /* True if the compilation unit places functions in more than one section. */
240 static GTY(()) bool have_multiple_function_sections = false;
242 /* Whether the default text and cold text sections have been used at all. */
244 static GTY(()) bool text_section_used = false;
245 static GTY(()) bool cold_text_section_used = false;
247 /* The default cold text section. */
248 static GTY(()) section *cold_text_section;
250 /* The DIE for C++1y 'auto' in a function return type. */
251 static GTY(()) dw_die_ref auto_die;
253 /* The DIE for C++1y 'decltype(auto)' in a function return type. */
254 static GTY(()) dw_die_ref decltype_auto_die;
256 /* Forward declarations for functions defined in this file. */
258 static char *stripattributes (const char *);
259 static void output_call_frame_info (int);
260 static void dwarf2out_note_section_used (void);
262 /* Personality decl of current unit. Used only when assembler does not support
263 personality CFI. */
264 static GTY(()) rtx current_unit_personality;
266 /* Data and reference forms for relocatable data. */
267 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
268 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
270 #ifndef DEBUG_FRAME_SECTION
271 #define DEBUG_FRAME_SECTION ".debug_frame"
272 #endif
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL "LFB"
276 #endif
278 #ifndef FUNC_END_LABEL
279 #define FUNC_END_LABEL "LFE"
280 #endif
282 #ifndef PROLOGUE_END_LABEL
283 #define PROLOGUE_END_LABEL "LPE"
284 #endif
286 #ifndef EPILOGUE_BEGIN_LABEL
287 #define EPILOGUE_BEGIN_LABEL "LEB"
288 #endif
290 #ifndef FRAME_BEGIN_LABEL
291 #define FRAME_BEGIN_LABEL "Lframe"
292 #endif
293 #define CIE_AFTER_SIZE_LABEL "LSCIE"
294 #define CIE_END_LABEL "LECIE"
295 #define FDE_LABEL "LSFDE"
296 #define FDE_AFTER_SIZE_LABEL "LASFDE"
297 #define FDE_END_LABEL "LEFDE"
298 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
299 #define LINE_NUMBER_END_LABEL "LELT"
300 #define LN_PROLOG_AS_LABEL "LASLTP"
301 #define LN_PROLOG_END_LABEL "LELTP"
302 #define DIE_LABEL_PREFIX "DW"
304 /* Match the base name of a file to the base name of a compilation unit. */
306 static int
307 matches_main_base (const char *path)
309 /* Cache the last query. */
310 static const char *last_path = NULL;
311 static int last_match = 0;
312 if (path != last_path)
314 const char *base;
315 int length = base_of_path (path, &base);
316 last_path = path;
317 last_match = (length == main_input_baselength
318 && memcmp (base, main_input_basename, length) == 0);
320 return last_match;
323 #ifdef DEBUG_DEBUG_STRUCT
325 static int
326 dump_struct_debug (tree type, enum debug_info_usage usage,
327 enum debug_struct_file criterion, int generic,
328 int matches, int result)
330 /* Find the type name. */
331 tree type_decl = TYPE_STUB_DECL (type);
332 tree t = type_decl;
333 const char *name = 0;
334 if (TREE_CODE (t) == TYPE_DECL)
335 t = DECL_NAME (t);
336 if (t)
337 name = IDENTIFIER_POINTER (t);
339 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
340 criterion,
341 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
342 matches ? "bas" : "hdr",
343 generic ? "gen" : "ord",
344 usage == DINFO_USAGE_DFN ? ";" :
345 usage == DINFO_USAGE_DIR_USE ? "." : "*",
346 result,
347 (void*) type_decl, name);
348 return result;
350 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
351 dump_struct_debug (type, usage, criterion, generic, matches, result)
353 #else
355 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
356 (result)
358 #endif
360 /* Get the number of HOST_WIDE_INTs needed to represent the precision
361 of the number. */
363 static unsigned int
364 get_full_len (const wide_int &op)
366 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
367 / HOST_BITS_PER_WIDE_INT);
370 static bool
371 should_emit_struct_debug (tree type, enum debug_info_usage usage)
373 enum debug_struct_file criterion;
374 tree type_decl;
375 bool generic = lang_hooks.types.generic_p (type);
377 if (generic)
378 criterion = debug_struct_generic[usage];
379 else
380 criterion = debug_struct_ordinary[usage];
382 if (criterion == DINFO_STRUCT_FILE_NONE)
383 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
384 if (criterion == DINFO_STRUCT_FILE_ANY)
385 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
387 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
389 if (type_decl != NULL)
391 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
392 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
394 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
395 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
398 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 /* Return a pointer to a copy of the section string name S with all
402 attributes stripped off, and an asterisk prepended (for assemble_name). */
404 static inline char *
405 stripattributes (const char *s)
407 char *stripped = XNEWVEC (char, strlen (s) + 2);
408 char *p = stripped;
410 *p++ = '*';
412 while (*s && *s != ',')
413 *p++ = *s++;
415 *p = '\0';
416 return stripped;
419 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
420 switch to the data section instead, and write out a synthetic start label
421 for collect2 the first time around. */
423 static void
424 switch_to_eh_frame_section (bool back)
426 tree label;
428 #ifdef EH_FRAME_SECTION_NAME
429 if (eh_frame_section == 0)
431 int flags;
433 if (EH_TABLES_CAN_BE_READ_ONLY)
435 int fde_encoding;
436 int per_encoding;
437 int lsda_encoding;
439 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
440 /*global=*/0);
441 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
442 /*global=*/1);
443 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
444 /*global=*/0);
445 flags = ((! flag_pic
446 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
447 && (fde_encoding & 0x70) != DW_EH_PE_aligned
448 && (per_encoding & 0x70) != DW_EH_PE_absptr
449 && (per_encoding & 0x70) != DW_EH_PE_aligned
450 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
451 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
452 ? 0 : SECTION_WRITE);
454 else
455 flags = SECTION_WRITE;
456 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
458 #endif /* EH_FRAME_SECTION_NAME */
460 if (eh_frame_section)
461 switch_to_section (eh_frame_section);
462 else
464 /* We have no special eh_frame section. Put the information in
465 the data section and emit special labels to guide collect2. */
466 switch_to_section (data_section);
468 if (!back)
470 label = get_file_function_name ("F");
471 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
472 targetm.asm_out.globalize_label (asm_out_file,
473 IDENTIFIER_POINTER (label));
474 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
479 /* Switch [BACK] to the eh or debug frame table section, depending on
480 FOR_EH. */
482 static void
483 switch_to_frame_table_section (int for_eh, bool back)
485 if (for_eh)
486 switch_to_eh_frame_section (back);
487 else
489 if (!debug_frame_section)
490 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
491 SECTION_DEBUG, NULL);
492 switch_to_section (debug_frame_section);
496 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
498 enum dw_cfi_oprnd_type
499 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
501 switch (cfi)
503 case DW_CFA_nop:
504 case DW_CFA_GNU_window_save:
505 case DW_CFA_remember_state:
506 case DW_CFA_restore_state:
507 return dw_cfi_oprnd_unused;
509 case DW_CFA_set_loc:
510 case DW_CFA_advance_loc1:
511 case DW_CFA_advance_loc2:
512 case DW_CFA_advance_loc4:
513 case DW_CFA_MIPS_advance_loc8:
514 return dw_cfi_oprnd_addr;
516 case DW_CFA_offset:
517 case DW_CFA_offset_extended:
518 case DW_CFA_def_cfa:
519 case DW_CFA_offset_extended_sf:
520 case DW_CFA_def_cfa_sf:
521 case DW_CFA_restore:
522 case DW_CFA_restore_extended:
523 case DW_CFA_undefined:
524 case DW_CFA_same_value:
525 case DW_CFA_def_cfa_register:
526 case DW_CFA_register:
527 case DW_CFA_expression:
528 return dw_cfi_oprnd_reg_num;
530 case DW_CFA_def_cfa_offset:
531 case DW_CFA_GNU_args_size:
532 case DW_CFA_def_cfa_offset_sf:
533 return dw_cfi_oprnd_offset;
535 case DW_CFA_def_cfa_expression:
536 return dw_cfi_oprnd_loc;
538 default:
539 gcc_unreachable ();
543 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
545 enum dw_cfi_oprnd_type
546 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
548 switch (cfi)
550 case DW_CFA_def_cfa:
551 case DW_CFA_def_cfa_sf:
552 case DW_CFA_offset:
553 case DW_CFA_offset_extended_sf:
554 case DW_CFA_offset_extended:
555 return dw_cfi_oprnd_offset;
557 case DW_CFA_register:
558 return dw_cfi_oprnd_reg_num;
560 case DW_CFA_expression:
561 return dw_cfi_oprnd_loc;
563 default:
564 return dw_cfi_oprnd_unused;
568 /* Output one FDE. */
570 static void
571 output_fde (dw_fde_ref fde, bool for_eh, bool second,
572 char *section_start_label, int fde_encoding, char *augmentation,
573 bool any_lsda_needed, int lsda_encoding)
575 const char *begin, *end;
576 static unsigned int j;
577 char l1[20], l2[20];
579 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
580 /* empty */ 0);
581 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
582 for_eh + j);
583 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
584 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
585 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
586 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
587 " indicating 64-bit DWARF extension");
588 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
589 "FDE Length");
590 ASM_OUTPUT_LABEL (asm_out_file, l1);
592 if (for_eh)
593 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
594 else
595 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
596 debug_frame_section, "FDE CIE offset");
598 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
599 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
601 if (for_eh)
603 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
604 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
605 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
606 "FDE initial location");
607 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
608 end, begin, "FDE address range");
610 else
612 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
613 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
616 if (augmentation[0])
618 if (any_lsda_needed)
620 int size = size_of_encoded_value (lsda_encoding);
622 if (lsda_encoding == DW_EH_PE_aligned)
624 int offset = ( 4 /* Length */
625 + 4 /* CIE offset */
626 + 2 * size_of_encoded_value (fde_encoding)
627 + 1 /* Augmentation size */ );
628 int pad = -offset & (PTR_SIZE - 1);
630 size += pad;
631 gcc_assert (size_of_uleb128 (size) == 1);
634 dw2_asm_output_data_uleb128 (size, "Augmentation size");
636 if (fde->uses_eh_lsda)
638 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
639 fde->funcdef_number);
640 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
641 gen_rtx_SYMBOL_REF (Pmode, l1),
642 false,
643 "Language Specific Data Area");
645 else
647 if (lsda_encoding == DW_EH_PE_aligned)
648 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
649 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
650 "Language Specific Data Area (none)");
653 else
654 dw2_asm_output_data_uleb128 (0, "Augmentation size");
657 /* Loop through the Call Frame Instructions associated with this FDE. */
658 fde->dw_fde_current_label = begin;
660 size_t from, until, i;
662 from = 0;
663 until = vec_safe_length (fde->dw_fde_cfi);
665 if (fde->dw_fde_second_begin == NULL)
667 else if (!second)
668 until = fde->dw_fde_switch_cfi_index;
669 else
670 from = fde->dw_fde_switch_cfi_index;
672 for (i = from; i < until; i++)
673 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
676 /* If we are to emit a ref/link from function bodies to their frame tables,
677 do it now. This is typically performed to make sure that tables
678 associated with functions are dragged with them and not discarded in
679 garbage collecting links. We need to do this on a per function basis to
680 cope with -ffunction-sections. */
682 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
683 /* Switch to the function section, emit the ref to the tables, and
684 switch *back* into the table section. */
685 switch_to_section (function_section (fde->decl));
686 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
687 switch_to_frame_table_section (for_eh, true);
688 #endif
690 /* Pad the FDE out to an address sized boundary. */
691 ASM_OUTPUT_ALIGN (asm_out_file,
692 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
693 ASM_OUTPUT_LABEL (asm_out_file, l2);
695 j += 2;
698 /* Return true if frame description entry FDE is needed for EH. */
700 static bool
701 fde_needed_for_eh_p (dw_fde_ref fde)
703 if (flag_asynchronous_unwind_tables)
704 return true;
706 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
707 return true;
709 if (fde->uses_eh_lsda)
710 return true;
712 /* If exceptions are enabled, we have collected nothrow info. */
713 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
714 return false;
716 return true;
719 /* Output the call frame information used to record information
720 that relates to calculating the frame pointer, and records the
721 location of saved registers. */
723 static void
724 output_call_frame_info (int for_eh)
726 unsigned int i;
727 dw_fde_ref fde;
728 dw_cfi_ref cfi;
729 char l1[20], l2[20], section_start_label[20];
730 bool any_lsda_needed = false;
731 char augmentation[6];
732 int augmentation_size;
733 int fde_encoding = DW_EH_PE_absptr;
734 int per_encoding = DW_EH_PE_absptr;
735 int lsda_encoding = DW_EH_PE_absptr;
736 int return_reg;
737 rtx personality = NULL;
738 int dw_cie_version;
740 /* Don't emit a CIE if there won't be any FDEs. */
741 if (!fde_vec)
742 return;
744 /* Nothing to do if the assembler's doing it all. */
745 if (dwarf2out_do_cfi_asm ())
746 return;
748 /* If we don't have any functions we'll want to unwind out of, don't emit
749 any EH unwind information. If we make FDEs linkonce, we may have to
750 emit an empty label for an FDE that wouldn't otherwise be emitted. We
751 want to avoid having an FDE kept around when the function it refers to
752 is discarded. Example where this matters: a primary function template
753 in C++ requires EH information, an explicit specialization doesn't. */
754 if (for_eh)
756 bool any_eh_needed = false;
758 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
760 if (fde->uses_eh_lsda)
761 any_eh_needed = any_lsda_needed = true;
762 else if (fde_needed_for_eh_p (fde))
763 any_eh_needed = true;
764 else if (TARGET_USES_WEAK_UNWIND_INFO)
765 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
768 if (!any_eh_needed)
769 return;
772 /* We're going to be generating comments, so turn on app. */
773 if (flag_debug_asm)
774 app_enable ();
776 /* Switch to the proper frame section, first time. */
777 switch_to_frame_table_section (for_eh, false);
779 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
780 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
782 /* Output the CIE. */
783 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
784 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
785 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
786 dw2_asm_output_data (4, 0xffffffff,
787 "Initial length escape value indicating 64-bit DWARF extension");
788 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
789 "Length of Common Information Entry");
790 ASM_OUTPUT_LABEL (asm_out_file, l1);
792 /* Now that the CIE pointer is PC-relative for EH,
793 use 0 to identify the CIE. */
794 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
795 (for_eh ? 0 : DWARF_CIE_ID),
796 "CIE Identifier Tag");
798 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
799 use CIE version 1, unless that would produce incorrect results
800 due to overflowing the return register column. */
801 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
802 dw_cie_version = 1;
803 if (return_reg >= 256 || dwarf_version > 2)
804 dw_cie_version = 3;
805 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
807 augmentation[0] = 0;
808 augmentation_size = 0;
810 personality = current_unit_personality;
811 if (for_eh)
813 char *p;
815 /* Augmentation:
816 z Indicates that a uleb128 is present to size the
817 augmentation section.
818 L Indicates the encoding (and thus presence) of
819 an LSDA pointer in the FDE augmentation.
820 R Indicates a non-default pointer encoding for
821 FDE code pointers.
822 P Indicates the presence of an encoding + language
823 personality routine in the CIE augmentation. */
825 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
826 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
827 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
829 p = augmentation + 1;
830 if (personality)
832 *p++ = 'P';
833 augmentation_size += 1 + size_of_encoded_value (per_encoding);
834 assemble_external_libcall (personality);
836 if (any_lsda_needed)
838 *p++ = 'L';
839 augmentation_size += 1;
841 if (fde_encoding != DW_EH_PE_absptr)
843 *p++ = 'R';
844 augmentation_size += 1;
846 if (p > augmentation + 1)
848 augmentation[0] = 'z';
849 *p = '\0';
852 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
853 if (personality && per_encoding == DW_EH_PE_aligned)
855 int offset = ( 4 /* Length */
856 + 4 /* CIE Id */
857 + 1 /* CIE version */
858 + strlen (augmentation) + 1 /* Augmentation */
859 + size_of_uleb128 (1) /* Code alignment */
860 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
861 + 1 /* RA column */
862 + 1 /* Augmentation size */
863 + 1 /* Personality encoding */ );
864 int pad = -offset & (PTR_SIZE - 1);
866 augmentation_size += pad;
868 /* Augmentations should be small, so there's scarce need to
869 iterate for a solution. Die if we exceed one uleb128 byte. */
870 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
874 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
875 if (dw_cie_version >= 4)
877 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
878 dw2_asm_output_data (1, 0, "CIE Segment Size");
880 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
881 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
882 "CIE Data Alignment Factor");
884 if (dw_cie_version == 1)
885 dw2_asm_output_data (1, return_reg, "CIE RA Column");
886 else
887 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
889 if (augmentation[0])
891 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
892 if (personality)
894 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
895 eh_data_format_name (per_encoding));
896 dw2_asm_output_encoded_addr_rtx (per_encoding,
897 personality,
898 true, NULL);
901 if (any_lsda_needed)
902 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
903 eh_data_format_name (lsda_encoding));
905 if (fde_encoding != DW_EH_PE_absptr)
906 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
907 eh_data_format_name (fde_encoding));
910 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
911 output_cfi (cfi, NULL, for_eh);
913 /* Pad the CIE out to an address sized boundary. */
914 ASM_OUTPUT_ALIGN (asm_out_file,
915 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
916 ASM_OUTPUT_LABEL (asm_out_file, l2);
918 /* Loop through all of the FDE's. */
919 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
921 unsigned int k;
923 /* Don't emit EH unwind info for leaf functions that don't need it. */
924 if (for_eh && !fde_needed_for_eh_p (fde))
925 continue;
927 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
928 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
929 augmentation, any_lsda_needed, lsda_encoding);
932 if (for_eh && targetm.terminate_dw2_eh_frame_info)
933 dw2_asm_output_data (4, 0, "End of Table");
935 /* Turn off app to make assembly quicker. */
936 if (flag_debug_asm)
937 app_disable ();
940 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
942 static void
943 dwarf2out_do_cfi_startproc (bool second)
945 int enc;
946 rtx ref;
947 rtx personality = get_personality_function (current_function_decl);
949 fprintf (asm_out_file, "\t.cfi_startproc\n");
951 if (personality)
953 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
954 ref = personality;
956 /* ??? The GAS support isn't entirely consistent. We have to
957 handle indirect support ourselves, but PC-relative is done
958 in the assembler. Further, the assembler can't handle any
959 of the weirder relocation types. */
960 if (enc & DW_EH_PE_indirect)
961 ref = dw2_force_const_mem (ref, true);
963 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
964 output_addr_const (asm_out_file, ref);
965 fputc ('\n', asm_out_file);
968 if (crtl->uses_eh_lsda)
970 char lab[20];
972 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
973 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
974 current_function_funcdef_no);
975 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
976 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
978 if (enc & DW_EH_PE_indirect)
979 ref = dw2_force_const_mem (ref, true);
981 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
982 output_addr_const (asm_out_file, ref);
983 fputc ('\n', asm_out_file);
987 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
988 this allocation may be done before pass_final. */
990 dw_fde_ref
991 dwarf2out_alloc_current_fde (void)
993 dw_fde_ref fde;
995 fde = ggc_cleared_alloc<dw_fde_node> ();
996 fde->decl = current_function_decl;
997 fde->funcdef_number = current_function_funcdef_no;
998 fde->fde_index = vec_safe_length (fde_vec);
999 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1000 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1001 fde->nothrow = crtl->nothrow;
1002 fde->drap_reg = INVALID_REGNUM;
1003 fde->vdrap_reg = INVALID_REGNUM;
1005 /* Record the FDE associated with this function. */
1006 cfun->fde = fde;
1007 vec_safe_push (fde_vec, fde);
1009 return fde;
1012 /* Output a marker (i.e. a label) for the beginning of a function, before
1013 the prologue. */
1015 void
1016 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1017 const char *file ATTRIBUTE_UNUSED)
1019 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1020 char * dup_label;
1021 dw_fde_ref fde;
1022 section *fnsec;
1023 bool do_frame;
1025 current_function_func_begin_label = NULL;
1027 do_frame = dwarf2out_do_frame ();
1029 /* ??? current_function_func_begin_label is also used by except.c for
1030 call-site information. We must emit this label if it might be used. */
1031 if (!do_frame
1032 && (!flag_exceptions
1033 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1034 return;
1036 fnsec = function_section (current_function_decl);
1037 switch_to_section (fnsec);
1038 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1039 current_function_funcdef_no);
1040 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1041 current_function_funcdef_no);
1042 dup_label = xstrdup (label);
1043 current_function_func_begin_label = dup_label;
1045 /* We can elide the fde allocation if we're not emitting debug info. */
1046 if (!do_frame)
1047 return;
1049 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1050 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1051 would include pass_dwarf2_frame. If we've not created the FDE yet,
1052 do so now. */
1053 fde = cfun->fde;
1054 if (fde == NULL)
1055 fde = dwarf2out_alloc_current_fde ();
1057 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1058 fde->dw_fde_begin = dup_label;
1059 fde->dw_fde_current_label = dup_label;
1060 fde->in_std_section = (fnsec == text_section
1061 || (cold_text_section && fnsec == cold_text_section));
1063 /* We only want to output line number information for the genuine dwarf2
1064 prologue case, not the eh frame case. */
1065 #ifdef DWARF2_DEBUGGING_INFO
1066 if (file)
1067 dwarf2out_source_line (line, file, 0, true);
1068 #endif
1070 if (dwarf2out_do_cfi_asm ())
1071 dwarf2out_do_cfi_startproc (false);
1072 else
1074 rtx personality = get_personality_function (current_function_decl);
1075 if (!current_unit_personality)
1076 current_unit_personality = personality;
1078 /* We cannot keep a current personality per function as without CFI
1079 asm, at the point where we emit the CFI data, there is no current
1080 function anymore. */
1081 if (personality && current_unit_personality != personality)
1082 sorry ("multiple EH personalities are supported only with assemblers "
1083 "supporting .cfi_personality directive");
1087 /* Output a marker (i.e. a label) for the end of the generated code
1088 for a function prologue. This gets called *after* the prologue code has
1089 been generated. */
1091 void
1092 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1093 const char *file ATTRIBUTE_UNUSED)
1095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1097 /* Output a label to mark the endpoint of the code generated for this
1098 function. */
1099 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1100 current_function_funcdef_no);
1101 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1102 current_function_funcdef_no);
1103 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1106 /* Output a marker (i.e. a label) for the beginning of the generated code
1107 for a function epilogue. This gets called *before* the prologue code has
1108 been generated. */
1110 void
1111 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1112 const char *file ATTRIBUTE_UNUSED)
1114 dw_fde_ref fde = cfun->fde;
1115 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1117 if (fde->dw_fde_vms_begin_epilogue)
1118 return;
1120 /* Output a label to mark the endpoint of the code generated for this
1121 function. */
1122 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1123 current_function_funcdef_no);
1124 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1125 current_function_funcdef_no);
1126 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1129 /* Output a marker (i.e. a label) for the absolute end of the generated code
1130 for a function definition. This gets called *after* the epilogue code has
1131 been generated. */
1133 void
1134 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1135 const char *file ATTRIBUTE_UNUSED)
1137 dw_fde_ref fde;
1138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1140 last_var_location_insn = NULL_RTX;
1141 cached_next_real_insn = NULL_RTX;
1143 if (dwarf2out_do_cfi_asm ())
1144 fprintf (asm_out_file, "\t.cfi_endproc\n");
1146 /* Output a label to mark the endpoint of the code generated for this
1147 function. */
1148 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1149 current_function_funcdef_no);
1150 ASM_OUTPUT_LABEL (asm_out_file, label);
1151 fde = cfun->fde;
1152 gcc_assert (fde != NULL);
1153 if (fde->dw_fde_second_begin == NULL)
1154 fde->dw_fde_end = xstrdup (label);
1157 void
1158 dwarf2out_frame_finish (void)
1160 /* Output call frame information. */
1161 if (targetm.debug_unwind_info () == UI_DWARF2)
1162 output_call_frame_info (0);
1164 /* Output another copy for the unwinder. */
1165 if ((flag_unwind_tables || flag_exceptions)
1166 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1167 output_call_frame_info (1);
1170 /* Note that the current function section is being used for code. */
1172 static void
1173 dwarf2out_note_section_used (void)
1175 section *sec = current_function_section ();
1176 if (sec == text_section)
1177 text_section_used = true;
1178 else if (sec == cold_text_section)
1179 cold_text_section_used = true;
1182 static void var_location_switch_text_section (void);
1183 static void set_cur_line_info_table (section *);
1185 void
1186 dwarf2out_switch_text_section (void)
1188 section *sect;
1189 dw_fde_ref fde = cfun->fde;
1191 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1193 if (!in_cold_section_p)
1195 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1196 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1197 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1199 else
1201 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1202 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1203 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1205 have_multiple_function_sections = true;
1207 /* There is no need to mark used sections when not debugging. */
1208 if (cold_text_section != NULL)
1209 dwarf2out_note_section_used ();
1211 if (dwarf2out_do_cfi_asm ())
1212 fprintf (asm_out_file, "\t.cfi_endproc\n");
1214 /* Now do the real section switch. */
1215 sect = current_function_section ();
1216 switch_to_section (sect);
1218 fde->second_in_std_section
1219 = (sect == text_section
1220 || (cold_text_section && sect == cold_text_section));
1222 if (dwarf2out_do_cfi_asm ())
1223 dwarf2out_do_cfi_startproc (true);
1225 var_location_switch_text_section ();
1227 if (cold_text_section != NULL)
1228 set_cur_line_info_table (sect);
1231 /* And now, the subset of the debugging information support code necessary
1232 for emitting location expressions. */
1234 /* Data about a single source file. */
1235 struct GTY(()) dwarf_file_data {
1236 const char * filename;
1237 int emitted_number;
1240 typedef struct GTY(()) deferred_locations_struct
1242 tree variable;
1243 dw_die_ref die;
1244 } deferred_locations;
1247 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1250 /* Describe an entry into the .debug_addr section. */
1252 enum ate_kind {
1253 ate_kind_rtx,
1254 ate_kind_rtx_dtprel,
1255 ate_kind_label
1258 typedef struct GTY(()) addr_table_entry_struct {
1259 enum ate_kind kind;
1260 unsigned int refcount;
1261 unsigned int index;
1262 union addr_table_entry_struct_union
1264 rtx GTY ((tag ("0"))) rtl;
1265 char * GTY ((tag ("1"))) label;
1267 GTY ((desc ("%1.kind"))) addr;
1269 addr_table_entry;
1271 /* Location lists are ranges + location descriptions for that range,
1272 so you can track variables that are in different places over
1273 their entire life. */
1274 typedef struct GTY(()) dw_loc_list_struct {
1275 dw_loc_list_ref dw_loc_next;
1276 const char *begin; /* Label and addr_entry for start of range */
1277 addr_table_entry *begin_entry;
1278 const char *end; /* Label for end of range */
1279 char *ll_symbol; /* Label for beginning of location list.
1280 Only on head of list */
1281 const char *section; /* Section this loclist is relative to */
1282 dw_loc_descr_ref expr;
1283 hashval_t hash;
1284 /* True if all addresses in this and subsequent lists are known to be
1285 resolved. */
1286 bool resolved_addr;
1287 /* True if this list has been replaced by dw_loc_next. */
1288 bool replaced;
1289 bool emitted;
1290 /* True if the range should be emitted even if begin and end
1291 are the same. */
1292 bool force;
1293 } dw_loc_list_node;
1295 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1297 /* Convert a DWARF stack opcode into its string name. */
1299 static const char *
1300 dwarf_stack_op_name (unsigned int op)
1302 const char *name = get_DW_OP_name (op);
1304 if (name != NULL)
1305 return name;
1307 return "OP_<unknown>";
1310 /* Return a pointer to a newly allocated location description. Location
1311 descriptions are simple expression terms that can be strung
1312 together to form more complicated location (address) descriptions. */
1314 static inline dw_loc_descr_ref
1315 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1316 unsigned HOST_WIDE_INT oprnd2)
1318 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1320 descr->dw_loc_opc = op;
1321 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1322 descr->dw_loc_oprnd1.val_entry = NULL;
1323 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1324 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1325 descr->dw_loc_oprnd2.val_entry = NULL;
1326 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1328 return descr;
1331 /* Return a pointer to a newly allocated location description for
1332 REG and OFFSET. */
1334 static inline dw_loc_descr_ref
1335 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1337 if (reg <= 31)
1338 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1339 offset, 0);
1340 else
1341 return new_loc_descr (DW_OP_bregx, reg, offset);
1344 /* Add a location description term to a location description expression. */
1346 static inline void
1347 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1349 dw_loc_descr_ref *d;
1351 /* Find the end of the chain. */
1352 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1355 *d = descr;
1358 /* Compare two location operands for exact equality. */
1360 static bool
1361 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1363 if (a->val_class != b->val_class)
1364 return false;
1365 switch (a->val_class)
1367 case dw_val_class_none:
1368 return true;
1369 case dw_val_class_addr:
1370 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1372 case dw_val_class_offset:
1373 case dw_val_class_unsigned_const:
1374 case dw_val_class_const:
1375 case dw_val_class_range_list:
1376 case dw_val_class_lineptr:
1377 case dw_val_class_macptr:
1378 /* These are all HOST_WIDE_INT, signed or unsigned. */
1379 return a->v.val_unsigned == b->v.val_unsigned;
1381 case dw_val_class_loc:
1382 return a->v.val_loc == b->v.val_loc;
1383 case dw_val_class_loc_list:
1384 return a->v.val_loc_list == b->v.val_loc_list;
1385 case dw_val_class_die_ref:
1386 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1387 case dw_val_class_fde_ref:
1388 return a->v.val_fde_index == b->v.val_fde_index;
1389 case dw_val_class_lbl_id:
1390 case dw_val_class_high_pc:
1391 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1392 case dw_val_class_str:
1393 return a->v.val_str == b->v.val_str;
1394 case dw_val_class_flag:
1395 return a->v.val_flag == b->v.val_flag;
1396 case dw_val_class_file:
1397 return a->v.val_file == b->v.val_file;
1398 case dw_val_class_decl_ref:
1399 return a->v.val_decl_ref == b->v.val_decl_ref;
1401 case dw_val_class_const_double:
1402 return (a->v.val_double.high == b->v.val_double.high
1403 && a->v.val_double.low == b->v.val_double.low);
1405 case dw_val_class_wide_int:
1406 return *a->v.val_wide == *b->v.val_wide;
1408 case dw_val_class_vec:
1410 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1411 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1413 return (a_len == b_len
1414 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1417 case dw_val_class_data8:
1418 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1420 case dw_val_class_vms_delta:
1421 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1422 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1424 gcc_unreachable ();
1427 /* Compare two location atoms for exact equality. */
1429 static bool
1430 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1432 if (a->dw_loc_opc != b->dw_loc_opc)
1433 return false;
1435 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1436 address size, but since we always allocate cleared storage it
1437 should be zero for other types of locations. */
1438 if (a->dtprel != b->dtprel)
1439 return false;
1441 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1442 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1445 /* Compare two complete location expressions for exact equality. */
1447 bool
1448 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1450 while (1)
1452 if (a == b)
1453 return true;
1454 if (a == NULL || b == NULL)
1455 return false;
1456 if (!loc_descr_equal_p_1 (a, b))
1457 return false;
1459 a = a->dw_loc_next;
1460 b = b->dw_loc_next;
1465 /* Add a constant OFFSET to a location expression. */
1467 static void
1468 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1470 dw_loc_descr_ref loc;
1471 HOST_WIDE_INT *p;
1473 gcc_assert (*list_head != NULL);
1475 if (!offset)
1476 return;
1478 /* Find the end of the chain. */
1479 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1482 p = NULL;
1483 if (loc->dw_loc_opc == DW_OP_fbreg
1484 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1485 p = &loc->dw_loc_oprnd1.v.val_int;
1486 else if (loc->dw_loc_opc == DW_OP_bregx)
1487 p = &loc->dw_loc_oprnd2.v.val_int;
1489 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1490 offset. Don't optimize if an signed integer overflow would happen. */
1491 if (p != NULL
1492 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1493 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1494 *p += offset;
1496 else if (offset > 0)
1497 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1499 else
1501 loc->dw_loc_next = int_loc_descriptor (-offset);
1502 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1506 /* Add a constant OFFSET to a location list. */
1508 static void
1509 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1511 dw_loc_list_ref d;
1512 for (d = list_head; d != NULL; d = d->dw_loc_next)
1513 loc_descr_plus_const (&d->expr, offset);
1516 #define DWARF_REF_SIZE \
1517 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1519 static unsigned long int get_base_type_offset (dw_die_ref);
1521 /* Return the size of a location descriptor. */
1523 static unsigned long
1524 size_of_loc_descr (dw_loc_descr_ref loc)
1526 unsigned long size = 1;
1528 switch (loc->dw_loc_opc)
1530 case DW_OP_addr:
1531 size += DWARF2_ADDR_SIZE;
1532 break;
1533 case DW_OP_GNU_addr_index:
1534 case DW_OP_GNU_const_index:
1535 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1536 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1537 break;
1538 case DW_OP_const1u:
1539 case DW_OP_const1s:
1540 size += 1;
1541 break;
1542 case DW_OP_const2u:
1543 case DW_OP_const2s:
1544 size += 2;
1545 break;
1546 case DW_OP_const4u:
1547 case DW_OP_const4s:
1548 size += 4;
1549 break;
1550 case DW_OP_const8u:
1551 case DW_OP_const8s:
1552 size += 8;
1553 break;
1554 case DW_OP_constu:
1555 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1556 break;
1557 case DW_OP_consts:
1558 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1559 break;
1560 case DW_OP_pick:
1561 size += 1;
1562 break;
1563 case DW_OP_plus_uconst:
1564 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565 break;
1566 case DW_OP_skip:
1567 case DW_OP_bra:
1568 size += 2;
1569 break;
1570 case DW_OP_breg0:
1571 case DW_OP_breg1:
1572 case DW_OP_breg2:
1573 case DW_OP_breg3:
1574 case DW_OP_breg4:
1575 case DW_OP_breg5:
1576 case DW_OP_breg6:
1577 case DW_OP_breg7:
1578 case DW_OP_breg8:
1579 case DW_OP_breg9:
1580 case DW_OP_breg10:
1581 case DW_OP_breg11:
1582 case DW_OP_breg12:
1583 case DW_OP_breg13:
1584 case DW_OP_breg14:
1585 case DW_OP_breg15:
1586 case DW_OP_breg16:
1587 case DW_OP_breg17:
1588 case DW_OP_breg18:
1589 case DW_OP_breg19:
1590 case DW_OP_breg20:
1591 case DW_OP_breg21:
1592 case DW_OP_breg22:
1593 case DW_OP_breg23:
1594 case DW_OP_breg24:
1595 case DW_OP_breg25:
1596 case DW_OP_breg26:
1597 case DW_OP_breg27:
1598 case DW_OP_breg28:
1599 case DW_OP_breg29:
1600 case DW_OP_breg30:
1601 case DW_OP_breg31:
1602 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1603 break;
1604 case DW_OP_regx:
1605 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1606 break;
1607 case DW_OP_fbreg:
1608 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1609 break;
1610 case DW_OP_bregx:
1611 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1612 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1613 break;
1614 case DW_OP_piece:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 break;
1617 case DW_OP_bit_piece:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1620 break;
1621 case DW_OP_deref_size:
1622 case DW_OP_xderef_size:
1623 size += 1;
1624 break;
1625 case DW_OP_call2:
1626 size += 2;
1627 break;
1628 case DW_OP_call4:
1629 size += 4;
1630 break;
1631 case DW_OP_call_ref:
1632 size += DWARF_REF_SIZE;
1633 break;
1634 case DW_OP_implicit_value:
1635 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1636 + loc->dw_loc_oprnd1.v.val_unsigned;
1637 break;
1638 case DW_OP_GNU_implicit_pointer:
1639 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1640 break;
1641 case DW_OP_GNU_entry_value:
1643 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1644 size += size_of_uleb128 (op_size) + op_size;
1645 break;
1647 case DW_OP_GNU_const_type:
1649 unsigned long o
1650 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1651 size += size_of_uleb128 (o) + 1;
1652 switch (loc->dw_loc_oprnd2.val_class)
1654 case dw_val_class_vec:
1655 size += loc->dw_loc_oprnd2.v.val_vec.length
1656 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1657 break;
1658 case dw_val_class_const:
1659 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1660 break;
1661 case dw_val_class_const_double:
1662 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1663 break;
1664 case dw_val_class_wide_int:
1665 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1666 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1667 break;
1668 default:
1669 gcc_unreachable ();
1671 break;
1673 case DW_OP_GNU_regval_type:
1675 unsigned long o
1676 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1677 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1678 + size_of_uleb128 (o);
1680 break;
1681 case DW_OP_GNU_deref_type:
1683 unsigned long o
1684 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1685 size += 1 + size_of_uleb128 (o);
1687 break;
1688 case DW_OP_GNU_convert:
1689 case DW_OP_GNU_reinterpret:
1690 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1691 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1692 else
1694 unsigned long o
1695 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1696 size += size_of_uleb128 (o);
1698 break;
1699 case DW_OP_GNU_parameter_ref:
1700 size += 4;
1701 break;
1702 default:
1703 break;
1706 return size;
1709 /* Return the size of a series of location descriptors. */
1711 unsigned long
1712 size_of_locs (dw_loc_descr_ref loc)
1714 dw_loc_descr_ref l;
1715 unsigned long size;
1717 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1718 field, to avoid writing to a PCH file. */
1719 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1721 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1722 break;
1723 size += size_of_loc_descr (l);
1725 if (! l)
1726 return size;
1728 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1730 l->dw_loc_addr = size;
1731 size += size_of_loc_descr (l);
1734 return size;
1737 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1738 static void get_ref_die_offset_label (char *, dw_die_ref);
1739 static unsigned long int get_ref_die_offset (dw_die_ref);
1741 /* Output location description stack opcode's operands (if any).
1742 The for_eh_or_skip parameter controls whether register numbers are
1743 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1744 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1745 info). This should be suppressed for the cases that have not been converted
1746 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1748 static void
1749 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1751 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1752 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1754 switch (loc->dw_loc_opc)
1756 #ifdef DWARF2_DEBUGGING_INFO
1757 case DW_OP_const2u:
1758 case DW_OP_const2s:
1759 dw2_asm_output_data (2, val1->v.val_int, NULL);
1760 break;
1761 case DW_OP_const4u:
1762 if (loc->dtprel)
1764 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1765 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1766 val1->v.val_addr);
1767 fputc ('\n', asm_out_file);
1768 break;
1770 /* FALLTHRU */
1771 case DW_OP_const4s:
1772 dw2_asm_output_data (4, val1->v.val_int, NULL);
1773 break;
1774 case DW_OP_const8u:
1775 if (loc->dtprel)
1777 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1778 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1779 val1->v.val_addr);
1780 fputc ('\n', asm_out_file);
1781 break;
1783 /* FALLTHRU */
1784 case DW_OP_const8s:
1785 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1786 dw2_asm_output_data (8, val1->v.val_int, NULL);
1787 break;
1788 case DW_OP_skip:
1789 case DW_OP_bra:
1791 int offset;
1793 gcc_assert (val1->val_class == dw_val_class_loc);
1794 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1796 dw2_asm_output_data (2, offset, NULL);
1798 break;
1799 case DW_OP_implicit_value:
1800 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1801 switch (val2->val_class)
1803 case dw_val_class_const:
1804 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1805 break;
1806 case dw_val_class_vec:
1808 unsigned int elt_size = val2->v.val_vec.elt_size;
1809 unsigned int len = val2->v.val_vec.length;
1810 unsigned int i;
1811 unsigned char *p;
1813 if (elt_size > sizeof (HOST_WIDE_INT))
1815 elt_size /= 2;
1816 len *= 2;
1818 for (i = 0, p = val2->v.val_vec.array;
1819 i < len;
1820 i++, p += elt_size)
1821 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1822 "fp or vector constant word %u", i);
1824 break;
1825 case dw_val_class_const_double:
1827 unsigned HOST_WIDE_INT first, second;
1829 if (WORDS_BIG_ENDIAN)
1831 first = val2->v.val_double.high;
1832 second = val2->v.val_double.low;
1834 else
1836 first = val2->v.val_double.low;
1837 second = val2->v.val_double.high;
1839 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1840 first, NULL);
1841 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1842 second, NULL);
1844 break;
1845 case dw_val_class_wide_int:
1847 int i;
1848 int len = get_full_len (*val2->v.val_wide);
1849 if (WORDS_BIG_ENDIAN)
1850 for (i = len - 1; i >= 0; --i)
1851 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1852 val2->v.val_wide->elt (i), NULL);
1853 else
1854 for (i = 0; i < len; ++i)
1855 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1856 val2->v.val_wide->elt (i), NULL);
1858 break;
1859 case dw_val_class_addr:
1860 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1861 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1862 break;
1863 default:
1864 gcc_unreachable ();
1866 break;
1867 #else
1868 case DW_OP_const2u:
1869 case DW_OP_const2s:
1870 case DW_OP_const4u:
1871 case DW_OP_const4s:
1872 case DW_OP_const8u:
1873 case DW_OP_const8s:
1874 case DW_OP_skip:
1875 case DW_OP_bra:
1876 case DW_OP_implicit_value:
1877 /* We currently don't make any attempt to make sure these are
1878 aligned properly like we do for the main unwind info, so
1879 don't support emitting things larger than a byte if we're
1880 only doing unwinding. */
1881 gcc_unreachable ();
1882 #endif
1883 case DW_OP_const1u:
1884 case DW_OP_const1s:
1885 dw2_asm_output_data (1, val1->v.val_int, NULL);
1886 break;
1887 case DW_OP_constu:
1888 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1889 break;
1890 case DW_OP_consts:
1891 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1892 break;
1893 case DW_OP_pick:
1894 dw2_asm_output_data (1, val1->v.val_int, NULL);
1895 break;
1896 case DW_OP_plus_uconst:
1897 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1898 break;
1899 case DW_OP_breg0:
1900 case DW_OP_breg1:
1901 case DW_OP_breg2:
1902 case DW_OP_breg3:
1903 case DW_OP_breg4:
1904 case DW_OP_breg5:
1905 case DW_OP_breg6:
1906 case DW_OP_breg7:
1907 case DW_OP_breg8:
1908 case DW_OP_breg9:
1909 case DW_OP_breg10:
1910 case DW_OP_breg11:
1911 case DW_OP_breg12:
1912 case DW_OP_breg13:
1913 case DW_OP_breg14:
1914 case DW_OP_breg15:
1915 case DW_OP_breg16:
1916 case DW_OP_breg17:
1917 case DW_OP_breg18:
1918 case DW_OP_breg19:
1919 case DW_OP_breg20:
1920 case DW_OP_breg21:
1921 case DW_OP_breg22:
1922 case DW_OP_breg23:
1923 case DW_OP_breg24:
1924 case DW_OP_breg25:
1925 case DW_OP_breg26:
1926 case DW_OP_breg27:
1927 case DW_OP_breg28:
1928 case DW_OP_breg29:
1929 case DW_OP_breg30:
1930 case DW_OP_breg31:
1931 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1932 break;
1933 case DW_OP_regx:
1935 unsigned r = val1->v.val_unsigned;
1936 if (for_eh_or_skip >= 0)
1937 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1938 gcc_assert (size_of_uleb128 (r)
1939 == size_of_uleb128 (val1->v.val_unsigned));
1940 dw2_asm_output_data_uleb128 (r, NULL);
1942 break;
1943 case DW_OP_fbreg:
1944 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1945 break;
1946 case DW_OP_bregx:
1948 unsigned r = val1->v.val_unsigned;
1949 if (for_eh_or_skip >= 0)
1950 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1951 gcc_assert (size_of_uleb128 (r)
1952 == size_of_uleb128 (val1->v.val_unsigned));
1953 dw2_asm_output_data_uleb128 (r, NULL);
1954 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1956 break;
1957 case DW_OP_piece:
1958 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1959 break;
1960 case DW_OP_bit_piece:
1961 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1962 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1963 break;
1964 case DW_OP_deref_size:
1965 case DW_OP_xderef_size:
1966 dw2_asm_output_data (1, val1->v.val_int, NULL);
1967 break;
1969 case DW_OP_addr:
1970 if (loc->dtprel)
1972 if (targetm.asm_out.output_dwarf_dtprel)
1974 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1975 DWARF2_ADDR_SIZE,
1976 val1->v.val_addr);
1977 fputc ('\n', asm_out_file);
1979 else
1980 gcc_unreachable ();
1982 else
1984 #ifdef DWARF2_DEBUGGING_INFO
1985 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1986 #else
1987 gcc_unreachable ();
1988 #endif
1990 break;
1992 case DW_OP_GNU_addr_index:
1993 case DW_OP_GNU_const_index:
1994 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1995 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1996 "(index into .debug_addr)");
1997 break;
1999 case DW_OP_GNU_implicit_pointer:
2001 char label[MAX_ARTIFICIAL_LABEL_BYTES
2002 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2003 gcc_assert (val1->val_class == dw_val_class_die_ref);
2004 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2005 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2006 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2008 break;
2010 case DW_OP_GNU_entry_value:
2011 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2012 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2013 break;
2015 case DW_OP_GNU_const_type:
2017 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2018 gcc_assert (o);
2019 dw2_asm_output_data_uleb128 (o, NULL);
2020 switch (val2->val_class)
2022 case dw_val_class_const:
2023 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2024 dw2_asm_output_data (1, l, NULL);
2025 dw2_asm_output_data (l, val2->v.val_int, NULL);
2026 break;
2027 case dw_val_class_vec:
2029 unsigned int elt_size = val2->v.val_vec.elt_size;
2030 unsigned int len = val2->v.val_vec.length;
2031 unsigned int i;
2032 unsigned char *p;
2034 l = len * elt_size;
2035 dw2_asm_output_data (1, l, NULL);
2036 if (elt_size > sizeof (HOST_WIDE_INT))
2038 elt_size /= 2;
2039 len *= 2;
2041 for (i = 0, p = val2->v.val_vec.array;
2042 i < len;
2043 i++, p += elt_size)
2044 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2045 "fp or vector constant word %u", i);
2047 break;
2048 case dw_val_class_const_double:
2050 unsigned HOST_WIDE_INT first, second;
2051 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2053 dw2_asm_output_data (1, 2 * l, NULL);
2054 if (WORDS_BIG_ENDIAN)
2056 first = val2->v.val_double.high;
2057 second = val2->v.val_double.low;
2059 else
2061 first = val2->v.val_double.low;
2062 second = val2->v.val_double.high;
2064 dw2_asm_output_data (l, first, NULL);
2065 dw2_asm_output_data (l, second, NULL);
2067 break;
2068 case dw_val_class_wide_int:
2070 int i;
2071 int len = get_full_len (*val2->v.val_wide);
2072 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2074 dw2_asm_output_data (1, len * l, NULL);
2075 if (WORDS_BIG_ENDIAN)
2076 for (i = len - 1; i >= 0; --i)
2077 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2078 else
2079 for (i = 0; i < len; ++i)
2080 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2082 break;
2083 default:
2084 gcc_unreachable ();
2087 break;
2088 case DW_OP_GNU_regval_type:
2090 unsigned r = val1->v.val_unsigned;
2091 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2092 gcc_assert (o);
2093 if (for_eh_or_skip >= 0)
2095 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2096 gcc_assert (size_of_uleb128 (r)
2097 == size_of_uleb128 (val1->v.val_unsigned));
2099 dw2_asm_output_data_uleb128 (r, NULL);
2100 dw2_asm_output_data_uleb128 (o, NULL);
2102 break;
2103 case DW_OP_GNU_deref_type:
2105 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2106 gcc_assert (o);
2107 dw2_asm_output_data (1, val1->v.val_int, NULL);
2108 dw2_asm_output_data_uleb128 (o, NULL);
2110 break;
2111 case DW_OP_GNU_convert:
2112 case DW_OP_GNU_reinterpret:
2113 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2114 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2115 else
2117 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2118 gcc_assert (o);
2119 dw2_asm_output_data_uleb128 (o, NULL);
2121 break;
2123 case DW_OP_GNU_parameter_ref:
2125 unsigned long o;
2126 gcc_assert (val1->val_class == dw_val_class_die_ref);
2127 o = get_ref_die_offset (val1->v.val_die_ref.die);
2128 dw2_asm_output_data (4, o, NULL);
2130 break;
2132 default:
2133 /* Other codes have no operands. */
2134 break;
2138 /* Output a sequence of location operations.
2139 The for_eh_or_skip parameter controls whether register numbers are
2140 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2141 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2142 info). This should be suppressed for the cases that have not been converted
2143 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2145 void
2146 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2148 for (; loc != NULL; loc = loc->dw_loc_next)
2150 enum dwarf_location_atom opc = loc->dw_loc_opc;
2151 /* Output the opcode. */
2152 if (for_eh_or_skip >= 0
2153 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2155 unsigned r = (opc - DW_OP_breg0);
2156 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2157 gcc_assert (r <= 31);
2158 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2160 else if (for_eh_or_skip >= 0
2161 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2163 unsigned r = (opc - DW_OP_reg0);
2164 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2165 gcc_assert (r <= 31);
2166 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2169 dw2_asm_output_data (1, opc,
2170 "%s", dwarf_stack_op_name (opc));
2172 /* Output the operand(s) (if any). */
2173 output_loc_operands (loc, for_eh_or_skip);
2177 /* Output location description stack opcode's operands (if any).
2178 The output is single bytes on a line, suitable for .cfi_escape. */
2180 static void
2181 output_loc_operands_raw (dw_loc_descr_ref loc)
2183 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2184 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2186 switch (loc->dw_loc_opc)
2188 case DW_OP_addr:
2189 case DW_OP_GNU_addr_index:
2190 case DW_OP_GNU_const_index:
2191 case DW_OP_implicit_value:
2192 /* We cannot output addresses in .cfi_escape, only bytes. */
2193 gcc_unreachable ();
2195 case DW_OP_const1u:
2196 case DW_OP_const1s:
2197 case DW_OP_pick:
2198 case DW_OP_deref_size:
2199 case DW_OP_xderef_size:
2200 fputc (',', asm_out_file);
2201 dw2_asm_output_data_raw (1, val1->v.val_int);
2202 break;
2204 case DW_OP_const2u:
2205 case DW_OP_const2s:
2206 fputc (',', asm_out_file);
2207 dw2_asm_output_data_raw (2, val1->v.val_int);
2208 break;
2210 case DW_OP_const4u:
2211 case DW_OP_const4s:
2212 fputc (',', asm_out_file);
2213 dw2_asm_output_data_raw (4, val1->v.val_int);
2214 break;
2216 case DW_OP_const8u:
2217 case DW_OP_const8s:
2218 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (8, val1->v.val_int);
2221 break;
2223 case DW_OP_skip:
2224 case DW_OP_bra:
2226 int offset;
2228 gcc_assert (val1->val_class == dw_val_class_loc);
2229 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2231 fputc (',', asm_out_file);
2232 dw2_asm_output_data_raw (2, offset);
2234 break;
2236 case DW_OP_regx:
2238 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2239 gcc_assert (size_of_uleb128 (r)
2240 == size_of_uleb128 (val1->v.val_unsigned));
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_uleb128_raw (r);
2244 break;
2246 case DW_OP_constu:
2247 case DW_OP_plus_uconst:
2248 case DW_OP_piece:
2249 fputc (',', asm_out_file);
2250 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2251 break;
2253 case DW_OP_bit_piece:
2254 fputc (',', asm_out_file);
2255 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2256 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2257 break;
2259 case DW_OP_consts:
2260 case DW_OP_breg0:
2261 case DW_OP_breg1:
2262 case DW_OP_breg2:
2263 case DW_OP_breg3:
2264 case DW_OP_breg4:
2265 case DW_OP_breg5:
2266 case DW_OP_breg6:
2267 case DW_OP_breg7:
2268 case DW_OP_breg8:
2269 case DW_OP_breg9:
2270 case DW_OP_breg10:
2271 case DW_OP_breg11:
2272 case DW_OP_breg12:
2273 case DW_OP_breg13:
2274 case DW_OP_breg14:
2275 case DW_OP_breg15:
2276 case DW_OP_breg16:
2277 case DW_OP_breg17:
2278 case DW_OP_breg18:
2279 case DW_OP_breg19:
2280 case DW_OP_breg20:
2281 case DW_OP_breg21:
2282 case DW_OP_breg22:
2283 case DW_OP_breg23:
2284 case DW_OP_breg24:
2285 case DW_OP_breg25:
2286 case DW_OP_breg26:
2287 case DW_OP_breg27:
2288 case DW_OP_breg28:
2289 case DW_OP_breg29:
2290 case DW_OP_breg30:
2291 case DW_OP_breg31:
2292 case DW_OP_fbreg:
2293 fputc (',', asm_out_file);
2294 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2295 break;
2297 case DW_OP_bregx:
2299 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2300 gcc_assert (size_of_uleb128 (r)
2301 == size_of_uleb128 (val1->v.val_unsigned));
2302 fputc (',', asm_out_file);
2303 dw2_asm_output_data_uleb128_raw (r);
2304 fputc (',', asm_out_file);
2305 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2307 break;
2309 case DW_OP_GNU_implicit_pointer:
2310 case DW_OP_GNU_entry_value:
2311 case DW_OP_GNU_const_type:
2312 case DW_OP_GNU_regval_type:
2313 case DW_OP_GNU_deref_type:
2314 case DW_OP_GNU_convert:
2315 case DW_OP_GNU_reinterpret:
2316 case DW_OP_GNU_parameter_ref:
2317 gcc_unreachable ();
2318 break;
2320 default:
2321 /* Other codes have no operands. */
2322 break;
2326 void
2327 output_loc_sequence_raw (dw_loc_descr_ref loc)
2329 while (1)
2331 enum dwarf_location_atom opc = loc->dw_loc_opc;
2332 /* Output the opcode. */
2333 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2335 unsigned r = (opc - DW_OP_breg0);
2336 r = DWARF2_FRAME_REG_OUT (r, 1);
2337 gcc_assert (r <= 31);
2338 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2340 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2342 unsigned r = (opc - DW_OP_reg0);
2343 r = DWARF2_FRAME_REG_OUT (r, 1);
2344 gcc_assert (r <= 31);
2345 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2347 /* Output the opcode. */
2348 fprintf (asm_out_file, "%#x", opc);
2349 output_loc_operands_raw (loc);
2351 if (!loc->dw_loc_next)
2352 break;
2353 loc = loc->dw_loc_next;
2355 fputc (',', asm_out_file);
2359 /* This function builds a dwarf location descriptor sequence from a
2360 dw_cfa_location, adding the given OFFSET to the result of the
2361 expression. */
2363 struct dw_loc_descr_node *
2364 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2366 struct dw_loc_descr_node *head, *tmp;
2368 offset += cfa->offset;
2370 if (cfa->indirect)
2372 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2373 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2374 head->dw_loc_oprnd1.val_entry = NULL;
2375 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2376 add_loc_descr (&head, tmp);
2377 if (offset != 0)
2379 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2380 add_loc_descr (&head, tmp);
2383 else
2384 head = new_reg_loc_descr (cfa->reg, offset);
2386 return head;
2389 /* This function builds a dwarf location descriptor sequence for
2390 the address at OFFSET from the CFA when stack is aligned to
2391 ALIGNMENT byte. */
2393 struct dw_loc_descr_node *
2394 build_cfa_aligned_loc (dw_cfa_location *cfa,
2395 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2397 struct dw_loc_descr_node *head;
2398 unsigned int dwarf_fp
2399 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2401 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2402 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2404 head = new_reg_loc_descr (dwarf_fp, 0);
2405 add_loc_descr (&head, int_loc_descriptor (alignment));
2406 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2407 loc_descr_plus_const (&head, offset);
2409 else
2410 head = new_reg_loc_descr (dwarf_fp, offset);
2411 return head;
2414 /* And now, the support for symbolic debugging information. */
2416 /* .debug_str support. */
2417 static int output_indirect_string (void **, void *);
2419 static void dwarf2out_init (const char *);
2420 static void dwarf2out_finish (const char *);
2421 static void dwarf2out_assembly_start (void);
2422 static void dwarf2out_define (unsigned int, const char *);
2423 static void dwarf2out_undef (unsigned int, const char *);
2424 static void dwarf2out_start_source_file (unsigned, const char *);
2425 static void dwarf2out_end_source_file (unsigned);
2426 static void dwarf2out_function_decl (tree);
2427 static void dwarf2out_begin_block (unsigned, unsigned);
2428 static void dwarf2out_end_block (unsigned, unsigned);
2429 static bool dwarf2out_ignore_block (const_tree);
2430 static void dwarf2out_global_decl (tree);
2431 static void dwarf2out_type_decl (tree, int);
2432 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2433 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2434 dw_die_ref);
2435 static void dwarf2out_abstract_function (tree);
2436 static void dwarf2out_var_location (rtx);
2437 static void dwarf2out_begin_function (tree);
2438 static void dwarf2out_end_function (unsigned int);
2439 static void dwarf2out_set_name (tree, tree);
2441 /* The debug hooks structure. */
2443 const struct gcc_debug_hooks dwarf2_debug_hooks =
2445 dwarf2out_init,
2446 dwarf2out_finish,
2447 dwarf2out_assembly_start,
2448 dwarf2out_define,
2449 dwarf2out_undef,
2450 dwarf2out_start_source_file,
2451 dwarf2out_end_source_file,
2452 dwarf2out_begin_block,
2453 dwarf2out_end_block,
2454 dwarf2out_ignore_block,
2455 dwarf2out_source_line,
2456 dwarf2out_begin_prologue,
2457 #if VMS_DEBUGGING_INFO
2458 dwarf2out_vms_end_prologue,
2459 dwarf2out_vms_begin_epilogue,
2460 #else
2461 debug_nothing_int_charstar,
2462 debug_nothing_int_charstar,
2463 #endif
2464 dwarf2out_end_epilogue,
2465 dwarf2out_begin_function,
2466 dwarf2out_end_function, /* end_function */
2467 dwarf2out_function_decl, /* function_decl */
2468 dwarf2out_global_decl,
2469 dwarf2out_type_decl, /* type_decl */
2470 dwarf2out_imported_module_or_decl,
2471 debug_nothing_tree, /* deferred_inline_function */
2472 /* The DWARF 2 backend tries to reduce debugging bloat by not
2473 emitting the abstract description of inline functions until
2474 something tries to reference them. */
2475 dwarf2out_abstract_function, /* outlining_inline_function */
2476 debug_nothing_rtx, /* label */
2477 debug_nothing_int, /* handle_pch */
2478 dwarf2out_var_location,
2479 dwarf2out_switch_text_section,
2480 dwarf2out_set_name,
2481 1, /* start_end_main_source_file */
2482 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2485 /* NOTE: In the comments in this file, many references are made to
2486 "Debugging Information Entries". This term is abbreviated as `DIE'
2487 throughout the remainder of this file. */
2489 /* An internal representation of the DWARF output is built, and then
2490 walked to generate the DWARF debugging info. The walk of the internal
2491 representation is done after the entire program has been compiled.
2492 The types below are used to describe the internal representation. */
2494 /* Whether to put type DIEs into their own section .debug_types instead
2495 of making them part of the .debug_info section. Only supported for
2496 Dwarf V4 or higher and the user didn't disable them through
2497 -fno-debug-types-section. It is more efficient to put them in a
2498 separate comdat sections since the linker will then be able to
2499 remove duplicates. But not all tools support .debug_types sections
2500 yet. */
2502 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2504 /* Various DIE's use offsets relative to the beginning of the
2505 .debug_info section to refer to each other. */
2507 typedef long int dw_offset;
2509 /* Define typedefs here to avoid circular dependencies. */
2511 typedef struct dw_attr_struct *dw_attr_ref;
2512 typedef struct dw_line_info_struct *dw_line_info_ref;
2513 typedef struct pubname_struct *pubname_ref;
2514 typedef struct dw_ranges_struct *dw_ranges_ref;
2515 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2516 typedef struct comdat_type_struct *comdat_type_node_ref;
2518 /* The entries in the line_info table more-or-less mirror the opcodes
2519 that are used in the real dwarf line table. Arrays of these entries
2520 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2521 supported. */
2523 enum dw_line_info_opcode {
2524 /* Emit DW_LNE_set_address; the operand is the label index. */
2525 LI_set_address,
2527 /* Emit a row to the matrix with the given line. This may be done
2528 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2529 special opcodes. */
2530 LI_set_line,
2532 /* Emit a DW_LNS_set_file. */
2533 LI_set_file,
2535 /* Emit a DW_LNS_set_column. */
2536 LI_set_column,
2538 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2539 LI_negate_stmt,
2541 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2542 LI_set_prologue_end,
2543 LI_set_epilogue_begin,
2545 /* Emit a DW_LNE_set_discriminator. */
2546 LI_set_discriminator
2549 typedef struct GTY(()) dw_line_info_struct {
2550 enum dw_line_info_opcode opcode;
2551 unsigned int val;
2552 } dw_line_info_entry;
2555 typedef struct GTY(()) dw_line_info_table_struct {
2556 /* The label that marks the end of this section. */
2557 const char *end_label;
2559 /* The values for the last row of the matrix, as collected in the table.
2560 These are used to minimize the changes to the next row. */
2561 unsigned int file_num;
2562 unsigned int line_num;
2563 unsigned int column_num;
2564 int discrim_num;
2565 bool is_stmt;
2566 bool in_use;
2568 vec<dw_line_info_entry, va_gc> *entries;
2569 } dw_line_info_table;
2571 typedef dw_line_info_table *dw_line_info_table_p;
2574 /* Each DIE attribute has a field specifying the attribute kind,
2575 a link to the next attribute in the chain, and an attribute value.
2576 Attributes are typically linked below the DIE they modify. */
2578 typedef struct GTY(()) dw_attr_struct {
2579 enum dwarf_attribute dw_attr;
2580 dw_val_node dw_attr_val;
2582 dw_attr_node;
2585 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2586 The children of each node form a circular list linked by
2587 die_sib. die_child points to the node *before* the "first" child node. */
2589 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2590 union die_symbol_or_type_node
2592 const char * GTY ((tag ("0"))) die_symbol;
2593 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2595 GTY ((desc ("%0.comdat_type_p"))) die_id;
2596 vec<dw_attr_node, va_gc> *die_attr;
2597 dw_die_ref die_parent;
2598 dw_die_ref die_child;
2599 dw_die_ref die_sib;
2600 dw_die_ref die_definition; /* ref from a specification to its definition */
2601 dw_offset die_offset;
2602 unsigned long die_abbrev;
2603 int die_mark;
2604 unsigned int decl_id;
2605 enum dwarf_tag die_tag;
2606 /* Die is used and must not be pruned as unused. */
2607 BOOL_BITFIELD die_perennial_p : 1;
2608 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2609 /* Lots of spare bits. */
2611 die_node;
2613 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2614 #define FOR_EACH_CHILD(die, c, expr) do { \
2615 c = die->die_child; \
2616 if (c) do { \
2617 c = c->die_sib; \
2618 expr; \
2619 } while (c != die->die_child); \
2620 } while (0)
2622 /* The pubname structure */
2624 typedef struct GTY(()) pubname_struct {
2625 dw_die_ref die;
2626 const char *name;
2628 pubname_entry;
2631 struct GTY(()) dw_ranges_struct {
2632 /* If this is positive, it's a block number, otherwise it's a
2633 bitwise-negated index into dw_ranges_by_label. */
2634 int num;
2637 /* A structure to hold a macinfo entry. */
2639 typedef struct GTY(()) macinfo_struct {
2640 unsigned char code;
2641 unsigned HOST_WIDE_INT lineno;
2642 const char *info;
2644 macinfo_entry;
2647 struct GTY(()) dw_ranges_by_label_struct {
2648 const char *begin;
2649 const char *end;
2652 /* The comdat type node structure. */
2653 typedef struct GTY(()) comdat_type_struct
2655 dw_die_ref root_die;
2656 dw_die_ref type_die;
2657 dw_die_ref skeleton_die;
2658 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2659 struct comdat_type_struct *next;
2661 comdat_type_node;
2663 /* The limbo die list structure. */
2664 typedef struct GTY(()) limbo_die_struct {
2665 dw_die_ref die;
2666 tree created_for;
2667 struct limbo_die_struct *next;
2669 limbo_die_node;
2671 typedef struct skeleton_chain_struct
2673 dw_die_ref old_die;
2674 dw_die_ref new_die;
2675 struct skeleton_chain_struct *parent;
2677 skeleton_chain_node;
2679 /* Define a macro which returns nonzero for a TYPE_DECL which was
2680 implicitly generated for a type.
2682 Note that, unlike the C front-end (which generates a NULL named
2683 TYPE_DECL node for each complete tagged type, each array type,
2684 and each function type node created) the C++ front-end generates
2685 a _named_ TYPE_DECL node for each tagged type node created.
2686 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2687 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2688 front-end, but for each type, tagged or not. */
2690 #define TYPE_DECL_IS_STUB(decl) \
2691 (DECL_NAME (decl) == NULL_TREE \
2692 || (DECL_ARTIFICIAL (decl) \
2693 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2694 /* This is necessary for stub decls that \
2695 appear in nested inline functions. */ \
2696 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2697 && (decl_ultimate_origin (decl) \
2698 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2700 /* Information concerning the compilation unit's programming
2701 language, and compiler version. */
2703 /* Fixed size portion of the DWARF compilation unit header. */
2704 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2705 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2707 /* Fixed size portion of the DWARF comdat type unit header. */
2708 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2709 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2710 + DWARF_OFFSET_SIZE)
2712 /* Fixed size portion of public names info. */
2713 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2715 /* Fixed size portion of the address range info. */
2716 #define DWARF_ARANGES_HEADER_SIZE \
2717 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2718 DWARF2_ADDR_SIZE * 2) \
2719 - DWARF_INITIAL_LENGTH_SIZE)
2721 /* Size of padding portion in the address range info. It must be
2722 aligned to twice the pointer size. */
2723 #define DWARF_ARANGES_PAD_SIZE \
2724 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2725 DWARF2_ADDR_SIZE * 2) \
2726 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2728 /* Use assembler line directives if available. */
2729 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2730 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2731 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2732 #else
2733 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2734 #endif
2735 #endif
2737 /* Minimum line offset in a special line info. opcode.
2738 This value was chosen to give a reasonable range of values. */
2739 #define DWARF_LINE_BASE -10
2741 /* First special line opcode - leave room for the standard opcodes. */
2742 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2744 /* Range of line offsets in a special line info. opcode. */
2745 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2747 /* Flag that indicates the initial value of the is_stmt_start flag.
2748 In the present implementation, we do not mark any lines as
2749 the beginning of a source statement, because that information
2750 is not made available by the GCC front-end. */
2751 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2753 /* Maximum number of operations per instruction bundle. */
2754 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2755 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2756 #endif
2758 /* This location is used by calc_die_sizes() to keep track
2759 the offset of each DIE within the .debug_info section. */
2760 static unsigned long next_die_offset;
2762 /* Record the root of the DIE's built for the current compilation unit. */
2763 static GTY(()) dw_die_ref single_comp_unit_die;
2765 /* A list of type DIEs that have been separated into comdat sections. */
2766 static GTY(()) comdat_type_node *comdat_type_list;
2768 /* A list of DIEs with a NULL parent waiting to be relocated. */
2769 static GTY(()) limbo_die_node *limbo_die_list;
2771 /* A list of DIEs for which we may have to generate
2772 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2773 static GTY(()) limbo_die_node *deferred_asm_name;
2775 /* Filenames referenced by this compilation unit. */
2776 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2778 /* A hash table of references to DIE's that describe declarations.
2779 The key is a DECL_UID() which is a unique number identifying each decl. */
2780 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2782 /* A hash table of references to DIE's that describe COMMON blocks.
2783 The key is DECL_UID() ^ die_parent. */
2784 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2786 typedef struct GTY(()) die_arg_entry_struct {
2787 dw_die_ref die;
2788 tree arg;
2789 } die_arg_entry;
2792 /* Node of the variable location list. */
2793 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2794 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2795 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2796 in mode of the EXPR_LIST node and first EXPR_LIST operand
2797 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2798 location or NULL for padding. For larger bitsizes,
2799 mode is 0 and first operand is a CONCAT with bitsize
2800 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2801 NULL as second operand. */
2802 rtx GTY (()) loc;
2803 const char * GTY (()) label;
2804 struct var_loc_node * GTY (()) next;
2807 /* Variable location list. */
2808 struct GTY (()) var_loc_list_def {
2809 struct var_loc_node * GTY (()) first;
2811 /* Pointer to the last but one or last element of the
2812 chained list. If the list is empty, both first and
2813 last are NULL, if the list contains just one node
2814 or the last node certainly is not redundant, it points
2815 to the last node, otherwise points to the last but one.
2816 Do not mark it for GC because it is marked through the chain. */
2817 struct var_loc_node * GTY ((skip ("%h"))) last;
2819 /* Pointer to the last element before section switch,
2820 if NULL, either sections weren't switched or first
2821 is after section switch. */
2822 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2824 /* DECL_UID of the variable decl. */
2825 unsigned int decl_id;
2827 typedef struct var_loc_list_def var_loc_list;
2829 /* Call argument location list. */
2830 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2831 rtx GTY (()) call_arg_loc_note;
2832 const char * GTY (()) label;
2833 tree GTY (()) block;
2834 bool tail_call_p;
2835 rtx GTY (()) symbol_ref;
2836 struct call_arg_loc_node * GTY (()) next;
2840 /* Table of decl location linked lists. */
2841 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2843 /* Head and tail of call_arg_loc chain. */
2844 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2845 static struct call_arg_loc_node *call_arg_loc_last;
2847 /* Number of call sites in the current function. */
2848 static int call_site_count = -1;
2849 /* Number of tail call sites in the current function. */
2850 static int tail_call_site_count = -1;
2852 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2853 DIEs. */
2854 static vec<dw_die_ref> block_map;
2856 /* A cached location list. */
2857 struct GTY (()) cached_dw_loc_list_def {
2858 /* The DECL_UID of the decl that this entry describes. */
2859 unsigned int decl_id;
2861 /* The cached location list. */
2862 dw_loc_list_ref loc_list;
2864 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2866 /* Table of cached location lists. */
2867 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2869 /* A pointer to the base of a list of references to DIE's that
2870 are uniquely identified by their tag, presence/absence of
2871 children DIE's, and list of attribute/value pairs. */
2872 static GTY((length ("abbrev_die_table_allocated")))
2873 dw_die_ref *abbrev_die_table;
2875 /* Number of elements currently allocated for abbrev_die_table. */
2876 static GTY(()) unsigned abbrev_die_table_allocated;
2878 /* Number of elements in type_die_table currently in use. */
2879 static GTY(()) unsigned abbrev_die_table_in_use;
2881 /* Size (in elements) of increments by which we may expand the
2882 abbrev_die_table. */
2883 #define ABBREV_DIE_TABLE_INCREMENT 256
2885 /* A global counter for generating labels for line number data. */
2886 static unsigned int line_info_label_num;
2888 /* The current table to which we should emit line number information
2889 for the current function. This will be set up at the beginning of
2890 assembly for the function. */
2891 static dw_line_info_table *cur_line_info_table;
2893 /* The two default tables of line number info. */
2894 static GTY(()) dw_line_info_table *text_section_line_info;
2895 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2897 /* The set of all non-default tables of line number info. */
2898 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2900 /* A flag to tell pubnames/types export if there is an info section to
2901 refer to. */
2902 static bool info_section_emitted;
2904 /* A pointer to the base of a table that contains a list of publicly
2905 accessible names. */
2906 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2908 /* A pointer to the base of a table that contains a list of publicly
2909 accessible types. */
2910 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2912 /* A pointer to the base of a table that contains a list of macro
2913 defines/undefines (and file start/end markers). */
2914 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2916 /* True if .debug_macinfo or .debug_macros section is going to be
2917 emitted. */
2918 #define have_macinfo \
2919 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2920 && !macinfo_table->is_empty ())
2922 /* Array of dies for which we should generate .debug_ranges info. */
2923 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2925 /* Number of elements currently allocated for ranges_table. */
2926 static GTY(()) unsigned ranges_table_allocated;
2928 /* Number of elements in ranges_table currently in use. */
2929 static GTY(()) unsigned ranges_table_in_use;
2931 /* Array of pairs of labels referenced in ranges_table. */
2932 static GTY ((length ("ranges_by_label_allocated")))
2933 dw_ranges_by_label_ref ranges_by_label;
2935 /* Number of elements currently allocated for ranges_by_label. */
2936 static GTY(()) unsigned ranges_by_label_allocated;
2938 /* Number of elements in ranges_by_label currently in use. */
2939 static GTY(()) unsigned ranges_by_label_in_use;
2941 /* Size (in elements) of increments by which we may expand the
2942 ranges_table. */
2943 #define RANGES_TABLE_INCREMENT 64
2945 /* Whether we have location lists that need outputting */
2946 static GTY(()) bool have_location_lists;
2948 /* Unique label counter. */
2949 static GTY(()) unsigned int loclabel_num;
2951 /* Unique label counter for point-of-call tables. */
2952 static GTY(()) unsigned int poc_label_num;
2954 /* Record whether the function being analyzed contains inlined functions. */
2955 static int current_function_has_inlines;
2957 /* The last file entry emitted by maybe_emit_file(). */
2958 static GTY(()) struct dwarf_file_data * last_emitted_file;
2960 /* Number of internal labels generated by gen_internal_sym(). */
2961 static GTY(()) int label_num;
2963 /* Cached result of previous call to lookup_filename. */
2964 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2966 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2968 /* Instances of generic types for which we need to generate debug
2969 info that describe their generic parameters and arguments. That
2970 generation needs to happen once all types are properly laid out so
2971 we do it at the end of compilation. */
2972 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2974 /* Offset from the "steady-state frame pointer" to the frame base,
2975 within the current function. */
2976 static HOST_WIDE_INT frame_pointer_fb_offset;
2977 static bool frame_pointer_fb_offset_valid;
2979 static vec<dw_die_ref> base_types;
2981 /* Forward declarations for functions defined in this file. */
2983 static int is_pseudo_reg (const_rtx);
2984 static tree type_main_variant (tree);
2985 static int is_tagged_type (const_tree);
2986 static const char *dwarf_tag_name (unsigned);
2987 static const char *dwarf_attr_name (unsigned);
2988 static const char *dwarf_form_name (unsigned);
2989 static tree decl_ultimate_origin (const_tree);
2990 static tree decl_class_context (tree);
2991 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2992 static inline enum dw_val_class AT_class (dw_attr_ref);
2993 static inline unsigned int AT_index (dw_attr_ref);
2994 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2995 static inline unsigned AT_flag (dw_attr_ref);
2996 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2997 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2998 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2999 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3000 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3001 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3002 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3003 unsigned int, unsigned char *);
3004 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3005 static hashval_t debug_str_do_hash (const void *);
3006 static int debug_str_eq (const void *, const void *);
3007 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3008 static inline const char *AT_string (dw_attr_ref);
3009 static enum dwarf_form AT_string_form (dw_attr_ref);
3010 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3011 static void add_AT_specification (dw_die_ref, dw_die_ref);
3012 static inline dw_die_ref AT_ref (dw_attr_ref);
3013 static inline int AT_ref_external (dw_attr_ref);
3014 static inline void set_AT_ref_external (dw_attr_ref, int);
3015 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3016 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3017 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3018 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3019 dw_loc_list_ref);
3020 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3021 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3022 static void remove_addr_table_entry (addr_table_entry *);
3023 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3024 static inline rtx AT_addr (dw_attr_ref);
3025 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3026 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3027 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3028 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3029 unsigned HOST_WIDE_INT);
3030 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3031 unsigned long, bool);
3032 static inline const char *AT_lbl (dw_attr_ref);
3033 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3034 static const char *get_AT_low_pc (dw_die_ref);
3035 static const char *get_AT_hi_pc (dw_die_ref);
3036 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3037 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3038 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3039 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3040 static bool is_cxx (void);
3041 static bool is_fortran (void);
3042 static bool is_ada (void);
3043 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3044 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3045 static void add_child_die (dw_die_ref, dw_die_ref);
3046 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3047 static dw_die_ref lookup_type_die (tree);
3048 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3049 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3050 static void equate_type_number_to_die (tree, dw_die_ref);
3051 static hashval_t decl_die_table_hash (const void *);
3052 static int decl_die_table_eq (const void *, const void *);
3053 static dw_die_ref lookup_decl_die (tree);
3054 static hashval_t common_block_die_table_hash (const void *);
3055 static int common_block_die_table_eq (const void *, const void *);
3056 static hashval_t decl_loc_table_hash (const void *);
3057 static int decl_loc_table_eq (const void *, const void *);
3058 static var_loc_list *lookup_decl_loc (const_tree);
3059 static void equate_decl_number_to_die (tree, dw_die_ref);
3060 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3061 static void print_spaces (FILE *);
3062 static void print_die (dw_die_ref, FILE *);
3063 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3064 static dw_die_ref pop_compile_unit (dw_die_ref);
3065 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3066 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3067 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3068 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3069 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3070 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3071 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3072 struct md5_ctx *, int *);
3073 struct checksum_attributes;
3074 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3075 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3076 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3077 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3078 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3079 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3080 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3081 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3082 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3083 static void compute_section_prefix (dw_die_ref);
3084 static int is_type_die (dw_die_ref);
3085 static int is_comdat_die (dw_die_ref);
3086 static int is_symbol_die (dw_die_ref);
3087 static inline bool is_template_instantiation (dw_die_ref);
3088 static void assign_symbol_names (dw_die_ref);
3089 static void break_out_includes (dw_die_ref);
3090 static int is_declaration_die (dw_die_ref);
3091 static int should_move_die_to_comdat (dw_die_ref);
3092 static dw_die_ref clone_as_declaration (dw_die_ref);
3093 static dw_die_ref clone_die (dw_die_ref);
3094 static dw_die_ref clone_tree (dw_die_ref);
3095 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3096 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3097 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3098 static dw_die_ref generate_skeleton (dw_die_ref);
3099 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3100 dw_die_ref,
3101 dw_die_ref);
3102 static void break_out_comdat_types (dw_die_ref);
3103 static void copy_decls_for_unworthy_types (dw_die_ref);
3105 static void add_sibling_attributes (dw_die_ref);
3106 static void output_location_lists (dw_die_ref);
3107 static int constant_size (unsigned HOST_WIDE_INT);
3108 static unsigned long size_of_die (dw_die_ref);
3109 static void calc_die_sizes (dw_die_ref);
3110 static void calc_base_type_die_sizes (void);
3111 static void mark_dies (dw_die_ref);
3112 static void unmark_dies (dw_die_ref);
3113 static void unmark_all_dies (dw_die_ref);
3114 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3115 static unsigned long size_of_aranges (void);
3116 static enum dwarf_form value_format (dw_attr_ref);
3117 static void output_value_format (dw_attr_ref);
3118 static void output_abbrev_section (void);
3119 static void output_die_abbrevs (unsigned long, dw_die_ref);
3120 static void output_die_symbol (dw_die_ref);
3121 static void output_die (dw_die_ref);
3122 static void output_compilation_unit_header (void);
3123 static void output_comp_unit (dw_die_ref, int);
3124 static void output_comdat_type_unit (comdat_type_node *);
3125 static const char *dwarf2_name (tree, int);
3126 static void add_pubname (tree, dw_die_ref);
3127 static void add_enumerator_pubname (const char *, dw_die_ref);
3128 static void add_pubname_string (const char *, dw_die_ref);
3129 static void add_pubtype (tree, dw_die_ref);
3130 static void output_pubnames (vec<pubname_entry, va_gc> *);
3131 static void output_aranges (unsigned long);
3132 static unsigned int add_ranges_num (int);
3133 static unsigned int add_ranges (const_tree);
3134 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3135 bool *, bool);
3136 static void output_ranges (void);
3137 static dw_line_info_table *new_line_info_table (void);
3138 static void output_line_info (bool);
3139 static void output_file_names (void);
3140 static dw_die_ref base_type_die (tree);
3141 static int is_base_type (tree);
3142 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3143 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3144 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3145 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3146 static int type_is_enum (const_tree);
3147 static unsigned int dbx_reg_number (const_rtx);
3148 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3149 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3150 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3151 enum var_init_status);
3152 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3153 enum var_init_status);
3154 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3155 enum var_init_status);
3156 static int is_based_loc (const_rtx);
3157 static int resolve_one_addr (rtx *, void *);
3158 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3159 enum var_init_status);
3160 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3161 enum var_init_status);
3162 static dw_loc_list_ref loc_list_from_tree (tree, int);
3163 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3164 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3165 static tree field_type (const_tree);
3166 static unsigned int simple_type_align_in_bits (const_tree);
3167 static unsigned int simple_decl_align_in_bits (const_tree);
3168 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3169 static HOST_WIDE_INT field_byte_offset (const_tree);
3170 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3171 dw_loc_list_ref);
3172 static void add_data_member_location_attribute (dw_die_ref, tree);
3173 static bool add_const_value_attribute (dw_die_ref, rtx);
3174 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3175 static void insert_wide_int (const wide_int &, unsigned char *, int);
3176 static void insert_float (const_rtx, unsigned char *);
3177 static rtx rtl_for_decl_location (tree);
3178 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3179 enum dwarf_attribute);
3180 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3181 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3182 static void add_name_attribute (dw_die_ref, const char *);
3183 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3184 static void add_comp_dir_attribute (dw_die_ref);
3185 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3186 static void add_subscript_info (dw_die_ref, tree, bool);
3187 static void add_byte_size_attribute (dw_die_ref, tree);
3188 static void add_bit_offset_attribute (dw_die_ref, tree);
3189 static void add_bit_size_attribute (dw_die_ref, tree);
3190 static void add_prototyped_attribute (dw_die_ref, tree);
3191 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3192 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3193 static void add_src_coords_attributes (dw_die_ref, tree);
3194 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3195 static void push_decl_scope (tree);
3196 static void pop_decl_scope (void);
3197 static dw_die_ref scope_die_for (tree, dw_die_ref);
3198 static inline int local_scope_p (dw_die_ref);
3199 static inline int class_scope_p (dw_die_ref);
3200 static inline int class_or_namespace_scope_p (dw_die_ref);
3201 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3202 static void add_calling_convention_attribute (dw_die_ref, tree);
3203 static const char *type_tag (const_tree);
3204 static tree member_declared_type (const_tree);
3205 #if 0
3206 static const char *decl_start_label (tree);
3207 #endif
3208 static void gen_array_type_die (tree, dw_die_ref);
3209 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3210 #if 0
3211 static void gen_entry_point_die (tree, dw_die_ref);
3212 #endif
3213 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3214 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3215 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3216 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3217 static void gen_formal_types_die (tree, dw_die_ref);
3218 static void gen_subprogram_die (tree, dw_die_ref);
3219 static void gen_variable_die (tree, tree, dw_die_ref);
3220 static void gen_const_die (tree, dw_die_ref);
3221 static void gen_label_die (tree, dw_die_ref);
3222 static void gen_lexical_block_die (tree, dw_die_ref, int);
3223 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3224 static void gen_field_die (tree, dw_die_ref);
3225 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3226 static dw_die_ref gen_compile_unit_die (const char *);
3227 static void gen_inheritance_die (tree, tree, dw_die_ref);
3228 static void gen_member_die (tree, dw_die_ref);
3229 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3230 enum debug_info_usage);
3231 static void gen_subroutine_type_die (tree, dw_die_ref);
3232 static void gen_typedef_die (tree, dw_die_ref);
3233 static void gen_type_die (tree, dw_die_ref);
3234 static void gen_block_die (tree, dw_die_ref, int);
3235 static void decls_for_scope (tree, dw_die_ref, int);
3236 static inline int is_redundant_typedef (const_tree);
3237 static bool is_naming_typedef_decl (const_tree);
3238 static inline dw_die_ref get_context_die (tree);
3239 static void gen_namespace_die (tree, dw_die_ref);
3240 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3241 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3242 static dw_die_ref force_decl_die (tree);
3243 static dw_die_ref force_type_die (tree);
3244 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3245 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3246 static struct dwarf_file_data * lookup_filename (const char *);
3247 static void retry_incomplete_types (void);
3248 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3249 static void gen_generic_params_dies (tree);
3250 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3251 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3252 static void splice_child_die (dw_die_ref, dw_die_ref);
3253 static int file_info_cmp (const void *, const void *);
3254 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3255 const char *, const char *);
3256 static void output_loc_list (dw_loc_list_ref);
3257 static char *gen_internal_sym (const char *);
3258 static bool want_pubnames (void);
3260 static void prune_unmark_dies (dw_die_ref);
3261 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3262 static void prune_unused_types_mark (dw_die_ref, int);
3263 static void prune_unused_types_walk (dw_die_ref);
3264 static void prune_unused_types_walk_attribs (dw_die_ref);
3265 static void prune_unused_types_prune (dw_die_ref);
3266 static void prune_unused_types (void);
3267 static int maybe_emit_file (struct dwarf_file_data *fd);
3268 static inline const char *AT_vms_delta1 (dw_attr_ref);
3269 static inline const char *AT_vms_delta2 (dw_attr_ref);
3270 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3271 const char *, const char *);
3272 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3273 static void gen_remaining_tmpl_value_param_die_attribute (void);
3274 static bool generic_type_p (tree);
3275 static void schedule_generic_params_dies_gen (tree t);
3276 static void gen_scheduled_generic_parms_dies (void);
3278 static const char *comp_dir_string (void);
3280 static hashval_t hash_loc_operands (dw_loc_descr_ref, hashval_t);
3282 /* enum for tracking thread-local variables whose address is really an offset
3283 relative to the TLS pointer, which will need link-time relocation, but will
3284 not need relocation by the DWARF consumer. */
3286 enum dtprel_bool
3288 dtprel_false = 0,
3289 dtprel_true = 1
3292 /* Return the operator to use for an address of a variable. For dtprel_true, we
3293 use DW_OP_const*. For regular variables, which need both link-time
3294 relocation and consumer-level relocation (e.g., to account for shared objects
3295 loaded at a random address), we use DW_OP_addr*. */
3297 static inline enum dwarf_location_atom
3298 dw_addr_op (enum dtprel_bool dtprel)
3300 if (dtprel == dtprel_true)
3301 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3302 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3303 else
3304 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3307 /* Return a pointer to a newly allocated address location description. If
3308 dwarf_split_debug_info is true, then record the address with the appropriate
3309 relocation. */
3310 static inline dw_loc_descr_ref
3311 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3313 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3315 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3316 ref->dw_loc_oprnd1.v.val_addr = addr;
3317 ref->dtprel = dtprel;
3318 if (dwarf_split_debug_info)
3319 ref->dw_loc_oprnd1.val_entry
3320 = add_addr_table_entry (addr,
3321 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3322 else
3323 ref->dw_loc_oprnd1.val_entry = NULL;
3325 return ref;
3328 /* Section names used to hold DWARF debugging information. */
3330 #ifndef DEBUG_INFO_SECTION
3331 #define DEBUG_INFO_SECTION ".debug_info"
3332 #endif
3333 #ifndef DEBUG_DWO_INFO_SECTION
3334 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3335 #endif
3336 #ifndef DEBUG_ABBREV_SECTION
3337 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3338 #endif
3339 #ifndef DEBUG_DWO_ABBREV_SECTION
3340 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3341 #endif
3342 #ifndef DEBUG_ARANGES_SECTION
3343 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3344 #endif
3345 #ifndef DEBUG_ADDR_SECTION
3346 #define DEBUG_ADDR_SECTION ".debug_addr"
3347 #endif
3348 #ifndef DEBUG_NORM_MACINFO_SECTION
3349 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3350 #endif
3351 #ifndef DEBUG_DWO_MACINFO_SECTION
3352 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3353 #endif
3354 #ifndef DEBUG_MACINFO_SECTION
3355 #define DEBUG_MACINFO_SECTION \
3356 (!dwarf_split_debug_info \
3357 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3358 #endif
3359 #ifndef DEBUG_NORM_MACRO_SECTION
3360 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3361 #endif
3362 #ifndef DEBUG_DWO_MACRO_SECTION
3363 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3364 #endif
3365 #ifndef DEBUG_MACRO_SECTION
3366 #define DEBUG_MACRO_SECTION \
3367 (!dwarf_split_debug_info \
3368 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3369 #endif
3370 #ifndef DEBUG_LINE_SECTION
3371 #define DEBUG_LINE_SECTION ".debug_line"
3372 #endif
3373 #ifndef DEBUG_DWO_LINE_SECTION
3374 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3375 #endif
3376 #ifndef DEBUG_LOC_SECTION
3377 #define DEBUG_LOC_SECTION ".debug_loc"
3378 #endif
3379 #ifndef DEBUG_DWO_LOC_SECTION
3380 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3381 #endif
3382 #ifndef DEBUG_PUBNAMES_SECTION
3383 #define DEBUG_PUBNAMES_SECTION \
3384 ((debug_generate_pub_sections == 2) \
3385 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3386 #endif
3387 #ifndef DEBUG_PUBTYPES_SECTION
3388 #define DEBUG_PUBTYPES_SECTION \
3389 ((debug_generate_pub_sections == 2) \
3390 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3391 #endif
3392 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3393 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3394 #ifndef DEBUG_STR_OFFSETS_SECTION
3395 #define DEBUG_STR_OFFSETS_SECTION \
3396 (!dwarf_split_debug_info \
3397 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3398 #endif
3399 #ifndef DEBUG_STR_DWO_SECTION
3400 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3401 #endif
3402 #ifndef DEBUG_STR_SECTION
3403 #define DEBUG_STR_SECTION ".debug_str"
3404 #endif
3405 #ifndef DEBUG_RANGES_SECTION
3406 #define DEBUG_RANGES_SECTION ".debug_ranges"
3407 #endif
3409 /* Standard ELF section names for compiled code and data. */
3410 #ifndef TEXT_SECTION_NAME
3411 #define TEXT_SECTION_NAME ".text"
3412 #endif
3414 /* Section flags for .debug_macinfo/.debug_macro section. */
3415 #define DEBUG_MACRO_SECTION_FLAGS \
3416 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3418 /* Section flags for .debug_str section. */
3419 #define DEBUG_STR_SECTION_FLAGS \
3420 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3421 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3422 : SECTION_DEBUG)
3424 /* Section flags for .debug_str.dwo section. */
3425 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3427 /* Labels we insert at beginning sections we can reference instead of
3428 the section names themselves. */
3430 #ifndef TEXT_SECTION_LABEL
3431 #define TEXT_SECTION_LABEL "Ltext"
3432 #endif
3433 #ifndef COLD_TEXT_SECTION_LABEL
3434 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3435 #endif
3436 #ifndef DEBUG_LINE_SECTION_LABEL
3437 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3438 #endif
3439 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3440 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3441 #endif
3442 #ifndef DEBUG_INFO_SECTION_LABEL
3443 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3444 #endif
3445 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3446 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3447 #endif
3448 #ifndef DEBUG_ABBREV_SECTION_LABEL
3449 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3450 #endif
3451 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3452 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3453 #endif
3454 #ifndef DEBUG_ADDR_SECTION_LABEL
3455 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3456 #endif
3457 #ifndef DEBUG_LOC_SECTION_LABEL
3458 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3459 #endif
3460 #ifndef DEBUG_RANGES_SECTION_LABEL
3461 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3462 #endif
3463 #ifndef DEBUG_MACINFO_SECTION_LABEL
3464 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3465 #endif
3466 #ifndef DEBUG_MACRO_SECTION_LABEL
3467 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3468 #endif
3469 #define SKELETON_COMP_DIE_ABBREV 1
3470 #define SKELETON_TYPE_DIE_ABBREV 2
3472 /* Definitions of defaults for formats and names of various special
3473 (artificial) labels which may be generated within this file (when the -g
3474 options is used and DWARF2_DEBUGGING_INFO is in effect.
3475 If necessary, these may be overridden from within the tm.h file, but
3476 typically, overriding these defaults is unnecessary. */
3478 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3479 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3480 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3481 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3482 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3483 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3484 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3485 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3486 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3493 #ifndef TEXT_END_LABEL
3494 #define TEXT_END_LABEL "Letext"
3495 #endif
3496 #ifndef COLD_END_LABEL
3497 #define COLD_END_LABEL "Letext_cold"
3498 #endif
3499 #ifndef BLOCK_BEGIN_LABEL
3500 #define BLOCK_BEGIN_LABEL "LBB"
3501 #endif
3502 #ifndef BLOCK_END_LABEL
3503 #define BLOCK_END_LABEL "LBE"
3504 #endif
3505 #ifndef LINE_CODE_LABEL
3506 #define LINE_CODE_LABEL "LM"
3507 #endif
3510 /* Return the root of the DIE's built for the current compilation unit. */
3511 static dw_die_ref
3512 comp_unit_die (void)
3514 if (!single_comp_unit_die)
3515 single_comp_unit_die = gen_compile_unit_die (NULL);
3516 return single_comp_unit_die;
3519 /* We allow a language front-end to designate a function that is to be
3520 called to "demangle" any name before it is put into a DIE. */
3522 static const char *(*demangle_name_func) (const char *);
3524 void
3525 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3527 demangle_name_func = func;
3530 /* Test if rtl node points to a pseudo register. */
3532 static inline int
3533 is_pseudo_reg (const_rtx rtl)
3535 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3536 || (GET_CODE (rtl) == SUBREG
3537 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3540 /* Return a reference to a type, with its const and volatile qualifiers
3541 removed. */
3543 static inline tree
3544 type_main_variant (tree type)
3546 type = TYPE_MAIN_VARIANT (type);
3548 /* ??? There really should be only one main variant among any group of
3549 variants of a given type (and all of the MAIN_VARIANT values for all
3550 members of the group should point to that one type) but sometimes the C
3551 front-end messes this up for array types, so we work around that bug
3552 here. */
3553 if (TREE_CODE (type) == ARRAY_TYPE)
3554 while (type != TYPE_MAIN_VARIANT (type))
3555 type = TYPE_MAIN_VARIANT (type);
3557 return type;
3560 /* Return nonzero if the given type node represents a tagged type. */
3562 static inline int
3563 is_tagged_type (const_tree type)
3565 enum tree_code code = TREE_CODE (type);
3567 return (code == RECORD_TYPE || code == UNION_TYPE
3568 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3571 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3573 static void
3574 get_ref_die_offset_label (char *label, dw_die_ref ref)
3576 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3579 /* Return die_offset of a DIE reference to a base type. */
3581 static unsigned long int
3582 get_base_type_offset (dw_die_ref ref)
3584 if (ref->die_offset)
3585 return ref->die_offset;
3586 if (comp_unit_die ()->die_abbrev)
3588 calc_base_type_die_sizes ();
3589 gcc_assert (ref->die_offset);
3591 return ref->die_offset;
3594 /* Return die_offset of a DIE reference other than base type. */
3596 static unsigned long int
3597 get_ref_die_offset (dw_die_ref ref)
3599 gcc_assert (ref->die_offset);
3600 return ref->die_offset;
3603 /* Convert a DIE tag into its string name. */
3605 static const char *
3606 dwarf_tag_name (unsigned int tag)
3608 const char *name = get_DW_TAG_name (tag);
3610 if (name != NULL)
3611 return name;
3613 return "DW_TAG_<unknown>";
3616 /* Convert a DWARF attribute code into its string name. */
3618 static const char *
3619 dwarf_attr_name (unsigned int attr)
3621 const char *name;
3623 switch (attr)
3625 #if VMS_DEBUGGING_INFO
3626 case DW_AT_HP_prologue:
3627 return "DW_AT_HP_prologue";
3628 #else
3629 case DW_AT_MIPS_loop_unroll_factor:
3630 return "DW_AT_MIPS_loop_unroll_factor";
3631 #endif
3633 #if VMS_DEBUGGING_INFO
3634 case DW_AT_HP_epilogue:
3635 return "DW_AT_HP_epilogue";
3636 #else
3637 case DW_AT_MIPS_stride:
3638 return "DW_AT_MIPS_stride";
3639 #endif
3642 name = get_DW_AT_name (attr);
3644 if (name != NULL)
3645 return name;
3647 return "DW_AT_<unknown>";
3650 /* Convert a DWARF value form code into its string name. */
3652 static const char *
3653 dwarf_form_name (unsigned int form)
3655 const char *name = get_DW_FORM_name (form);
3657 if (name != NULL)
3658 return name;
3660 return "DW_FORM_<unknown>";
3663 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3664 instance of an inlined instance of a decl which is local to an inline
3665 function, so we have to trace all of the way back through the origin chain
3666 to find out what sort of node actually served as the original seed for the
3667 given block. */
3669 static tree
3670 decl_ultimate_origin (const_tree decl)
3672 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3673 return NULL_TREE;
3675 /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3676 nodes in the function to point to themselves; ignore that if
3677 we're trying to output the abstract instance of this function. */
3678 if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3679 return NULL_TREE;
3681 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3682 most distant ancestor, this should never happen. */
3683 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3685 return DECL_ABSTRACT_ORIGIN (decl);
3688 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3689 of a virtual function may refer to a base class, so we check the 'this'
3690 parameter. */
3692 static tree
3693 decl_class_context (tree decl)
3695 tree context = NULL_TREE;
3697 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3698 context = DECL_CONTEXT (decl);
3699 else
3700 context = TYPE_MAIN_VARIANT
3701 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3703 if (context && !TYPE_P (context))
3704 context = NULL_TREE;
3706 return context;
3709 /* Add an attribute/value pair to a DIE. */
3711 static inline void
3712 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3714 /* Maybe this should be an assert? */
3715 if (die == NULL)
3716 return;
3718 vec_safe_reserve (die->die_attr, 1);
3719 vec_safe_push (die->die_attr, *attr);
3722 static inline enum dw_val_class
3723 AT_class (dw_attr_ref a)
3725 return a->dw_attr_val.val_class;
3728 /* Return the index for any attribute that will be referenced with a
3729 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3730 are stored in dw_attr_val.v.val_str for reference counting
3731 pruning. */
3733 static inline unsigned int
3734 AT_index (dw_attr_ref a)
3736 if (AT_class (a) == dw_val_class_str)
3737 return a->dw_attr_val.v.val_str->index;
3738 else if (a->dw_attr_val.val_entry != NULL)
3739 return a->dw_attr_val.val_entry->index;
3740 return NOT_INDEXED;
3743 /* Add a flag value attribute to a DIE. */
3745 static inline void
3746 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3748 dw_attr_node attr;
3750 attr.dw_attr = attr_kind;
3751 attr.dw_attr_val.val_class = dw_val_class_flag;
3752 attr.dw_attr_val.val_entry = NULL;
3753 attr.dw_attr_val.v.val_flag = flag;
3754 add_dwarf_attr (die, &attr);
3757 static inline unsigned
3758 AT_flag (dw_attr_ref a)
3760 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3761 return a->dw_attr_val.v.val_flag;
3764 /* Add a signed integer attribute value to a DIE. */
3766 static inline void
3767 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3769 dw_attr_node attr;
3771 attr.dw_attr = attr_kind;
3772 attr.dw_attr_val.val_class = dw_val_class_const;
3773 attr.dw_attr_val.val_entry = NULL;
3774 attr.dw_attr_val.v.val_int = int_val;
3775 add_dwarf_attr (die, &attr);
3778 static inline HOST_WIDE_INT
3779 AT_int (dw_attr_ref a)
3781 gcc_assert (a && AT_class (a) == dw_val_class_const);
3782 return a->dw_attr_val.v.val_int;
3785 /* Add an unsigned integer attribute value to a DIE. */
3787 static inline void
3788 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3789 unsigned HOST_WIDE_INT unsigned_val)
3791 dw_attr_node attr;
3793 attr.dw_attr = attr_kind;
3794 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3795 attr.dw_attr_val.val_entry = NULL;
3796 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3797 add_dwarf_attr (die, &attr);
3800 static inline unsigned HOST_WIDE_INT
3801 AT_unsigned (dw_attr_ref a)
3803 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3804 return a->dw_attr_val.v.val_unsigned;
3807 /* Add an unsigned wide integer attribute value to a DIE. */
3809 static inline void
3810 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3811 const wide_int& w)
3813 dw_attr_node attr;
3815 attr.dw_attr = attr_kind;
3816 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3817 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3818 *attr.dw_attr_val.v.val_wide = w;
3819 add_dwarf_attr (die, &attr);
3822 /* Add an unsigned double integer attribute value to a DIE. */
3824 static inline void
3825 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3826 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3828 dw_attr_node attr;
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const_double;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_double.high = high;
3834 attr.dw_attr_val.v.val_double.low = low;
3835 add_dwarf_attr (die, &attr);
3838 /* Add a floating point attribute value to a DIE and return it. */
3840 static inline void
3841 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3842 unsigned int length, unsigned int elt_size, unsigned char *array)
3844 dw_attr_node attr;
3846 attr.dw_attr = attr_kind;
3847 attr.dw_attr_val.val_class = dw_val_class_vec;
3848 attr.dw_attr_val.val_entry = NULL;
3849 attr.dw_attr_val.v.val_vec.length = length;
3850 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3851 attr.dw_attr_val.v.val_vec.array = array;
3852 add_dwarf_attr (die, &attr);
3855 /* Add an 8-byte data attribute value to a DIE. */
3857 static inline void
3858 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3859 unsigned char data8[8])
3861 dw_attr_node attr;
3863 attr.dw_attr = attr_kind;
3864 attr.dw_attr_val.val_class = dw_val_class_data8;
3865 attr.dw_attr_val.val_entry = NULL;
3866 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3867 add_dwarf_attr (die, &attr);
3870 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3871 dwarf_split_debug_info, address attributes in dies destined for the
3872 final executable have force_direct set to avoid using indexed
3873 references. */
3875 static inline void
3876 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3877 bool force_direct)
3879 dw_attr_node attr;
3880 char * lbl_id;
3882 lbl_id = xstrdup (lbl_low);
3883 attr.dw_attr = DW_AT_low_pc;
3884 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3885 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3886 if (dwarf_split_debug_info && !force_direct)
3887 attr.dw_attr_val.val_entry
3888 = add_addr_table_entry (lbl_id, ate_kind_label);
3889 else
3890 attr.dw_attr_val.val_entry = NULL;
3891 add_dwarf_attr (die, &attr);
3893 attr.dw_attr = DW_AT_high_pc;
3894 if (dwarf_version < 4)
3895 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3896 else
3897 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3898 lbl_id = xstrdup (lbl_high);
3899 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3900 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3901 && dwarf_split_debug_info && !force_direct)
3902 attr.dw_attr_val.val_entry
3903 = add_addr_table_entry (lbl_id, ate_kind_label);
3904 else
3905 attr.dw_attr_val.val_entry = NULL;
3906 add_dwarf_attr (die, &attr);
3909 /* Hash and equality functions for debug_str_hash. */
3911 static hashval_t
3912 debug_str_do_hash (const void *x)
3914 return htab_hash_string (((const struct indirect_string_node *)x)->str);
3917 static int
3918 debug_str_eq (const void *x1, const void *x2)
3920 return strcmp ((((const struct indirect_string_node *)x1)->str),
3921 (const char *)x2) == 0;
3924 /* Add STR to the given string hash table. */
3926 static struct indirect_string_node *
3927 find_AT_string_in_table (const char *str, htab_t table)
3929 struct indirect_string_node *node;
3930 void **slot;
3932 slot = htab_find_slot_with_hash (table, str,
3933 htab_hash_string (str), INSERT);
3934 if (*slot == NULL)
3936 node = ggc_cleared_alloc<indirect_string_node> ();
3937 node->str = ggc_strdup (str);
3938 *slot = node;
3940 else
3941 node = (struct indirect_string_node *) *slot;
3943 node->refcount++;
3944 return node;
3947 /* Add STR to the indirect string hash table. */
3949 static struct indirect_string_node *
3950 find_AT_string (const char *str)
3952 if (! debug_str_hash)
3953 debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3954 debug_str_eq, NULL);
3956 return find_AT_string_in_table (str, debug_str_hash);
3959 /* Add a string attribute value to a DIE. */
3961 static inline void
3962 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3964 dw_attr_node attr;
3965 struct indirect_string_node *node;
3967 node = find_AT_string (str);
3969 attr.dw_attr = attr_kind;
3970 attr.dw_attr_val.val_class = dw_val_class_str;
3971 attr.dw_attr_val.val_entry = NULL;
3972 attr.dw_attr_val.v.val_str = node;
3973 add_dwarf_attr (die, &attr);
3976 static inline const char *
3977 AT_string (dw_attr_ref a)
3979 gcc_assert (a && AT_class (a) == dw_val_class_str);
3980 return a->dw_attr_val.v.val_str->str;
3983 /* Call this function directly to bypass AT_string_form's logic to put
3984 the string inline in the die. */
3986 static void
3987 set_indirect_string (struct indirect_string_node *node)
3989 char label[32];
3990 /* Already indirect is a no op. */
3991 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3993 gcc_assert (node->label);
3994 return;
3996 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3997 ++dw2_string_counter;
3998 node->label = xstrdup (label);
4000 if (!dwarf_split_debug_info)
4002 node->form = DW_FORM_strp;
4003 node->index = NOT_INDEXED;
4005 else
4007 node->form = DW_FORM_GNU_str_index;
4008 node->index = NO_INDEX_ASSIGNED;
4012 /* Find out whether a string should be output inline in DIE
4013 or out-of-line in .debug_str section. */
4015 static enum dwarf_form
4016 find_string_form (struct indirect_string_node *node)
4018 unsigned int len;
4020 if (node->form)
4021 return node->form;
4023 len = strlen (node->str) + 1;
4025 /* If the string is shorter or equal to the size of the reference, it is
4026 always better to put it inline. */
4027 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4028 return node->form = DW_FORM_string;
4030 /* If we cannot expect the linker to merge strings in .debug_str
4031 section, only put it into .debug_str if it is worth even in this
4032 single module. */
4033 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4034 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4035 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4036 return node->form = DW_FORM_string;
4038 set_indirect_string (node);
4040 return node->form;
4043 /* Find out whether the string referenced from the attribute should be
4044 output inline in DIE or out-of-line in .debug_str section. */
4046 static enum dwarf_form
4047 AT_string_form (dw_attr_ref a)
4049 gcc_assert (a && AT_class (a) == dw_val_class_str);
4050 return find_string_form (a->dw_attr_val.v.val_str);
4053 /* Add a DIE reference attribute value to a DIE. */
4055 static inline void
4056 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4058 dw_attr_node attr;
4060 #ifdef ENABLE_CHECKING
4061 gcc_assert (targ_die != NULL);
4062 #else
4063 /* With LTO we can end up trying to reference something we didn't create
4064 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4065 if (targ_die == NULL)
4066 return;
4067 #endif
4069 attr.dw_attr = attr_kind;
4070 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4071 attr.dw_attr_val.val_entry = NULL;
4072 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4073 attr.dw_attr_val.v.val_die_ref.external = 0;
4074 add_dwarf_attr (die, &attr);
4077 /* Change DIE reference REF to point to NEW_DIE instead. */
4079 static inline void
4080 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4082 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4083 ref->dw_attr_val.v.val_die_ref.die = new_die;
4084 ref->dw_attr_val.v.val_die_ref.external = 0;
4087 /* Add an AT_specification attribute to a DIE, and also make the back
4088 pointer from the specification to the definition. */
4090 static inline void
4091 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4093 add_AT_die_ref (die, DW_AT_specification, targ_die);
4094 gcc_assert (!targ_die->die_definition);
4095 targ_die->die_definition = die;
4098 static inline dw_die_ref
4099 AT_ref (dw_attr_ref a)
4101 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4102 return a->dw_attr_val.v.val_die_ref.die;
4105 static inline int
4106 AT_ref_external (dw_attr_ref a)
4108 if (a && AT_class (a) == dw_val_class_die_ref)
4109 return a->dw_attr_val.v.val_die_ref.external;
4111 return 0;
4114 static inline void
4115 set_AT_ref_external (dw_attr_ref a, int i)
4117 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4118 a->dw_attr_val.v.val_die_ref.external = i;
4121 /* Add an FDE reference attribute value to a DIE. */
4123 static inline void
4124 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4126 dw_attr_node attr;
4128 attr.dw_attr = attr_kind;
4129 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4130 attr.dw_attr_val.val_entry = NULL;
4131 attr.dw_attr_val.v.val_fde_index = targ_fde;
4132 add_dwarf_attr (die, &attr);
4135 /* Add a location description attribute value to a DIE. */
4137 static inline void
4138 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4140 dw_attr_node attr;
4142 attr.dw_attr = attr_kind;
4143 attr.dw_attr_val.val_class = dw_val_class_loc;
4144 attr.dw_attr_val.val_entry = NULL;
4145 attr.dw_attr_val.v.val_loc = loc;
4146 add_dwarf_attr (die, &attr);
4149 static inline dw_loc_descr_ref
4150 AT_loc (dw_attr_ref a)
4152 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4153 return a->dw_attr_val.v.val_loc;
4156 static inline void
4157 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4159 dw_attr_node attr;
4161 attr.dw_attr = attr_kind;
4162 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4163 attr.dw_attr_val.val_entry = NULL;
4164 attr.dw_attr_val.v.val_loc_list = loc_list;
4165 add_dwarf_attr (die, &attr);
4166 have_location_lists = true;
4169 static inline dw_loc_list_ref
4170 AT_loc_list (dw_attr_ref a)
4172 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4173 return a->dw_attr_val.v.val_loc_list;
4176 static inline dw_loc_list_ref *
4177 AT_loc_list_ptr (dw_attr_ref a)
4179 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4180 return &a->dw_attr_val.v.val_loc_list;
4183 /* Table of entries into the .debug_addr section. */
4185 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4187 /* Hash an address_table_entry. */
4189 static hashval_t
4190 addr_table_entry_do_hash (const void *x)
4192 const addr_table_entry *a = (const addr_table_entry *) x;
4193 switch (a->kind)
4195 case ate_kind_rtx:
4196 return iterative_hash_rtx (a->addr.rtl, 0);
4197 case ate_kind_rtx_dtprel:
4198 return iterative_hash_rtx (a->addr.rtl, 1);
4199 case ate_kind_label:
4200 return htab_hash_string (a->addr.label);
4201 default:
4202 gcc_unreachable ();
4206 /* Determine equality for two address_table_entries. */
4208 static int
4209 addr_table_entry_eq (const void *x1, const void *x2)
4211 const addr_table_entry *a1 = (const addr_table_entry *) x1;
4212 const addr_table_entry *a2 = (const addr_table_entry *) x2;
4214 if (a1->kind != a2->kind)
4215 return 0;
4216 switch (a1->kind)
4218 case ate_kind_rtx:
4219 case ate_kind_rtx_dtprel:
4220 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4221 case ate_kind_label:
4222 return strcmp (a1->addr.label, a2->addr.label) == 0;
4223 default:
4224 gcc_unreachable ();
4228 /* Initialize an addr_table_entry. */
4230 void
4231 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4233 e->kind = kind;
4234 switch (kind)
4236 case ate_kind_rtx:
4237 case ate_kind_rtx_dtprel:
4238 e->addr.rtl = (rtx) addr;
4239 break;
4240 case ate_kind_label:
4241 e->addr.label = (char *) addr;
4242 break;
4244 e->refcount = 0;
4245 e->index = NO_INDEX_ASSIGNED;
4248 /* Add attr to the address table entry to the table. Defer setting an
4249 index until output time. */
4251 static addr_table_entry *
4252 add_addr_table_entry (void *addr, enum ate_kind kind)
4254 addr_table_entry *node;
4255 addr_table_entry finder;
4256 void **slot;
4258 gcc_assert (dwarf_split_debug_info);
4259 if (! addr_index_table)
4260 addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4261 addr_table_entry_eq, NULL);
4262 init_addr_table_entry (&finder, kind, addr);
4263 slot = htab_find_slot (addr_index_table, &finder, INSERT);
4265 if (*slot == HTAB_EMPTY_ENTRY)
4267 node = ggc_cleared_alloc<addr_table_entry> ();
4268 init_addr_table_entry (node, kind, addr);
4269 *slot = node;
4271 else
4272 node = (addr_table_entry *) *slot;
4274 node->refcount++;
4275 return node;
4278 /* Remove an entry from the addr table by decrementing its refcount.
4279 Strictly, decrementing the refcount would be enough, but the
4280 assertion that the entry is actually in the table has found
4281 bugs. */
4283 static void
4284 remove_addr_table_entry (addr_table_entry *entry)
4286 addr_table_entry *node;
4288 gcc_assert (dwarf_split_debug_info && addr_index_table);
4289 node = (addr_table_entry *) htab_find (addr_index_table, entry);
4290 /* After an index is assigned, the table is frozen. */
4291 gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4292 node->refcount--;
4295 /* Given a location list, remove all addresses it refers to from the
4296 address_table. */
4298 static void
4299 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4301 for (; descr; descr = descr->dw_loc_next)
4302 if (descr->dw_loc_oprnd1.val_entry != NULL)
4304 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4305 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4309 /* A helper function for dwarf2out_finish called through
4310 htab_traverse. Assign an addr_table_entry its index. All entries
4311 must be collected into the table when this function is called,
4312 because the indexing code relies on htab_traverse to traverse nodes
4313 in the same order for each run. */
4315 static int
4316 index_addr_table_entry (void **h, void *v)
4318 addr_table_entry *node = (addr_table_entry *) *h;
4319 unsigned int *index = (unsigned int *) v;
4321 /* Don't index unreferenced nodes. */
4322 if (node->refcount == 0)
4323 return 1;
4325 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4326 node->index = *index;
4327 *index += 1;
4329 return 1;
4332 /* Add an address constant attribute value to a DIE. When using
4333 dwarf_split_debug_info, address attributes in dies destined for the
4334 final executable should be direct references--setting the parameter
4335 force_direct ensures this behavior. */
4337 static inline void
4338 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4339 bool force_direct)
4341 dw_attr_node attr;
4343 attr.dw_attr = attr_kind;
4344 attr.dw_attr_val.val_class = dw_val_class_addr;
4345 attr.dw_attr_val.v.val_addr = addr;
4346 if (dwarf_split_debug_info && !force_direct)
4347 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4348 else
4349 attr.dw_attr_val.val_entry = NULL;
4350 add_dwarf_attr (die, &attr);
4353 /* Get the RTX from to an address DIE attribute. */
4355 static inline rtx
4356 AT_addr (dw_attr_ref a)
4358 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4359 return a->dw_attr_val.v.val_addr;
4362 /* Add a file attribute value to a DIE. */
4364 static inline void
4365 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4366 struct dwarf_file_data *fd)
4368 dw_attr_node attr;
4370 attr.dw_attr = attr_kind;
4371 attr.dw_attr_val.val_class = dw_val_class_file;
4372 attr.dw_attr_val.val_entry = NULL;
4373 attr.dw_attr_val.v.val_file = fd;
4374 add_dwarf_attr (die, &attr);
4377 /* Get the dwarf_file_data from a file DIE attribute. */
4379 static inline struct dwarf_file_data *
4380 AT_file (dw_attr_ref a)
4382 gcc_assert (a && AT_class (a) == dw_val_class_file);
4383 return a->dw_attr_val.v.val_file;
4386 /* Add a vms delta attribute value to a DIE. */
4388 static inline void
4389 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4390 const char *lbl1, const char *lbl2)
4392 dw_attr_node attr;
4394 attr.dw_attr = attr_kind;
4395 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4396 attr.dw_attr_val.val_entry = NULL;
4397 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4398 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4399 add_dwarf_attr (die, &attr);
4402 /* Add a label identifier attribute value to a DIE. */
4404 static inline void
4405 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4406 const char *lbl_id)
4408 dw_attr_node attr;
4410 attr.dw_attr = attr_kind;
4411 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4412 attr.dw_attr_val.val_entry = NULL;
4413 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4414 if (dwarf_split_debug_info)
4415 attr.dw_attr_val.val_entry
4416 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4417 ate_kind_label);
4418 add_dwarf_attr (die, &attr);
4421 /* Add a section offset attribute value to a DIE, an offset into the
4422 debug_line section. */
4424 static inline void
4425 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4426 const char *label)
4428 dw_attr_node attr;
4430 attr.dw_attr = attr_kind;
4431 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4432 attr.dw_attr_val.val_entry = NULL;
4433 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4434 add_dwarf_attr (die, &attr);
4437 /* Add a section offset attribute value to a DIE, an offset into the
4438 debug_macinfo section. */
4440 static inline void
4441 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4442 const char *label)
4444 dw_attr_node attr;
4446 attr.dw_attr = attr_kind;
4447 attr.dw_attr_val.val_class = dw_val_class_macptr;
4448 attr.dw_attr_val.val_entry = NULL;
4449 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4450 add_dwarf_attr (die, &attr);
4453 /* Add an offset attribute value to a DIE. */
4455 static inline void
4456 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4457 unsigned HOST_WIDE_INT offset)
4459 dw_attr_node attr;
4461 attr.dw_attr = attr_kind;
4462 attr.dw_attr_val.val_class = dw_val_class_offset;
4463 attr.dw_attr_val.val_entry = NULL;
4464 attr.dw_attr_val.v.val_offset = offset;
4465 add_dwarf_attr (die, &attr);
4468 /* Add a range_list attribute value to a DIE. When using
4469 dwarf_split_debug_info, address attributes in dies destined for the
4470 final executable should be direct references--setting the parameter
4471 force_direct ensures this behavior. */
4473 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4474 #define RELOCATED_OFFSET (NULL)
4476 static void
4477 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4478 long unsigned int offset, bool force_direct)
4480 dw_attr_node attr;
4482 attr.dw_attr = attr_kind;
4483 attr.dw_attr_val.val_class = dw_val_class_range_list;
4484 /* For the range_list attribute, use val_entry to store whether the
4485 offset should follow split-debug-info or normal semantics. This
4486 value is read in output_range_list_offset. */
4487 if (dwarf_split_debug_info && !force_direct)
4488 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4489 else
4490 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4491 attr.dw_attr_val.v.val_offset = offset;
4492 add_dwarf_attr (die, &attr);
4495 /* Return the start label of a delta attribute. */
4497 static inline const char *
4498 AT_vms_delta1 (dw_attr_ref a)
4500 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4501 return a->dw_attr_val.v.val_vms_delta.lbl1;
4504 /* Return the end label of a delta attribute. */
4506 static inline const char *
4507 AT_vms_delta2 (dw_attr_ref a)
4509 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4510 return a->dw_attr_val.v.val_vms_delta.lbl2;
4513 static inline const char *
4514 AT_lbl (dw_attr_ref a)
4516 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4517 || AT_class (a) == dw_val_class_lineptr
4518 || AT_class (a) == dw_val_class_macptr
4519 || AT_class (a) == dw_val_class_high_pc));
4520 return a->dw_attr_val.v.val_lbl_id;
4523 /* Get the attribute of type attr_kind. */
4525 static dw_attr_ref
4526 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4528 dw_attr_ref a;
4529 unsigned ix;
4530 dw_die_ref spec = NULL;
4532 if (! die)
4533 return NULL;
4535 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4536 if (a->dw_attr == attr_kind)
4537 return a;
4538 else if (a->dw_attr == DW_AT_specification
4539 || a->dw_attr == DW_AT_abstract_origin)
4540 spec = AT_ref (a);
4542 if (spec)
4543 return get_AT (spec, attr_kind);
4545 return NULL;
4548 /* Returns the parent of the declaration of DIE. */
4550 static dw_die_ref
4551 get_die_parent (dw_die_ref die)
4553 dw_die_ref t;
4555 if (!die)
4556 return NULL;
4558 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4559 || (t = get_AT_ref (die, DW_AT_specification)))
4560 die = t;
4562 return die->die_parent;
4565 /* Return the "low pc" attribute value, typically associated with a subprogram
4566 DIE. Return null if the "low pc" attribute is either not present, or if it
4567 cannot be represented as an assembler label identifier. */
4569 static inline const char *
4570 get_AT_low_pc (dw_die_ref die)
4572 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4574 return a ? AT_lbl (a) : NULL;
4577 /* Return the "high pc" attribute value, typically associated with a subprogram
4578 DIE. Return null if the "high pc" attribute is either not present, or if it
4579 cannot be represented as an assembler label identifier. */
4581 static inline const char *
4582 get_AT_hi_pc (dw_die_ref die)
4584 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4586 return a ? AT_lbl (a) : NULL;
4589 /* Return the value of the string attribute designated by ATTR_KIND, or
4590 NULL if it is not present. */
4592 static inline const char *
4593 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4595 dw_attr_ref a = get_AT (die, attr_kind);
4597 return a ? AT_string (a) : NULL;
4600 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4601 if it is not present. */
4603 static inline int
4604 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4606 dw_attr_ref a = get_AT (die, attr_kind);
4608 return a ? AT_flag (a) : 0;
4611 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4612 if it is not present. */
4614 static inline unsigned
4615 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4617 dw_attr_ref a = get_AT (die, attr_kind);
4619 return a ? AT_unsigned (a) : 0;
4622 static inline dw_die_ref
4623 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4625 dw_attr_ref a = get_AT (die, attr_kind);
4627 return a ? AT_ref (a) : NULL;
4630 static inline struct dwarf_file_data *
4631 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4633 dw_attr_ref a = get_AT (die, attr_kind);
4635 return a ? AT_file (a) : NULL;
4638 /* Return TRUE if the language is C++. */
4640 static inline bool
4641 is_cxx (void)
4643 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4645 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4648 /* Return TRUE if the language is Java. */
4650 static inline bool
4651 is_java (void)
4653 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4655 return lang == DW_LANG_Java;
4658 /* Return TRUE if the language is Fortran. */
4660 static inline bool
4661 is_fortran (void)
4663 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4665 return (lang == DW_LANG_Fortran77
4666 || lang == DW_LANG_Fortran90
4667 || lang == DW_LANG_Fortran95);
4670 /* Return TRUE if the language is Ada. */
4672 static inline bool
4673 is_ada (void)
4675 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4677 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4680 /* Remove the specified attribute if present. */
4682 static void
4683 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4685 dw_attr_ref a;
4686 unsigned ix;
4688 if (! die)
4689 return;
4691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4692 if (a->dw_attr == attr_kind)
4694 if (AT_class (a) == dw_val_class_str)
4695 if (a->dw_attr_val.v.val_str->refcount)
4696 a->dw_attr_val.v.val_str->refcount--;
4698 /* vec::ordered_remove should help reduce the number of abbrevs
4699 that are needed. */
4700 die->die_attr->ordered_remove (ix);
4701 return;
4705 /* Remove CHILD from its parent. PREV must have the property that
4706 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4708 static void
4709 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4711 gcc_assert (child->die_parent == prev->die_parent);
4712 gcc_assert (prev->die_sib == child);
4713 if (prev == child)
4715 gcc_assert (child->die_parent->die_child == child);
4716 prev = NULL;
4718 else
4719 prev->die_sib = child->die_sib;
4720 if (child->die_parent->die_child == child)
4721 child->die_parent->die_child = prev;
4724 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4725 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4727 static void
4728 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4730 dw_die_ref parent = old_child->die_parent;
4732 gcc_assert (parent == prev->die_parent);
4733 gcc_assert (prev->die_sib == old_child);
4735 new_child->die_parent = parent;
4736 if (prev == old_child)
4738 gcc_assert (parent->die_child == old_child);
4739 new_child->die_sib = new_child;
4741 else
4743 prev->die_sib = new_child;
4744 new_child->die_sib = old_child->die_sib;
4746 if (old_child->die_parent->die_child == old_child)
4747 old_child->die_parent->die_child = new_child;
4750 /* Move all children from OLD_PARENT to NEW_PARENT. */
4752 static void
4753 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4755 dw_die_ref c;
4756 new_parent->die_child = old_parent->die_child;
4757 old_parent->die_child = NULL;
4758 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4761 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4762 matches TAG. */
4764 static void
4765 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4767 dw_die_ref c;
4769 c = die->die_child;
4770 if (c) do {
4771 dw_die_ref prev = c;
4772 c = c->die_sib;
4773 while (c->die_tag == tag)
4775 remove_child_with_prev (c, prev);
4776 /* Might have removed every child. */
4777 if (c == c->die_sib)
4778 return;
4779 c = c->die_sib;
4781 } while (c != die->die_child);
4784 /* Add a CHILD_DIE as the last child of DIE. */
4786 static void
4787 add_child_die (dw_die_ref die, dw_die_ref child_die)
4789 /* FIXME this should probably be an assert. */
4790 if (! die || ! child_die)
4791 return;
4792 gcc_assert (die != child_die);
4794 child_die->die_parent = die;
4795 if (die->die_child)
4797 child_die->die_sib = die->die_child->die_sib;
4798 die->die_child->die_sib = child_die;
4800 else
4801 child_die->die_sib = child_die;
4802 die->die_child = child_die;
4805 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4806 is the specification, to the end of PARENT's list of children.
4807 This is done by removing and re-adding it. */
4809 static void
4810 splice_child_die (dw_die_ref parent, dw_die_ref child)
4812 dw_die_ref p;
4814 /* We want the declaration DIE from inside the class, not the
4815 specification DIE at toplevel. */
4816 if (child->die_parent != parent)
4818 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4820 if (tmp)
4821 child = tmp;
4824 gcc_assert (child->die_parent == parent
4825 || (child->die_parent
4826 == get_AT_ref (parent, DW_AT_specification)));
4828 for (p = child->die_parent->die_child; ; p = p->die_sib)
4829 if (p->die_sib == child)
4831 remove_child_with_prev (child, p);
4832 break;
4835 add_child_die (parent, child);
4838 /* Return a pointer to a newly created DIE node. */
4840 static inline dw_die_ref
4841 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4843 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4845 die->die_tag = tag_value;
4847 if (parent_die != NULL)
4848 add_child_die (parent_die, die);
4849 else
4851 limbo_die_node *limbo_node;
4853 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4854 limbo_node->die = die;
4855 limbo_node->created_for = t;
4856 limbo_node->next = limbo_die_list;
4857 limbo_die_list = limbo_node;
4860 return die;
4863 /* Return the DIE associated with the given type specifier. */
4865 static inline dw_die_ref
4866 lookup_type_die (tree type)
4868 return TYPE_SYMTAB_DIE (type);
4871 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4872 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4873 anonymous type instead the one of the naming typedef. */
4875 static inline dw_die_ref
4876 strip_naming_typedef (tree type, dw_die_ref type_die)
4878 if (type
4879 && TREE_CODE (type) == RECORD_TYPE
4880 && type_die
4881 && type_die->die_tag == DW_TAG_typedef
4882 && is_naming_typedef_decl (TYPE_NAME (type)))
4883 type_die = get_AT_ref (type_die, DW_AT_type);
4884 return type_die;
4887 /* Like lookup_type_die, but if type is an anonymous type named by a
4888 typedef[1], return the DIE of the anonymous type instead the one of
4889 the naming typedef. This is because in gen_typedef_die, we did
4890 equate the anonymous struct named by the typedef with the DIE of
4891 the naming typedef. So by default, lookup_type_die on an anonymous
4892 struct yields the DIE of the naming typedef.
4894 [1]: Read the comment of is_naming_typedef_decl to learn about what
4895 a naming typedef is. */
4897 static inline dw_die_ref
4898 lookup_type_die_strip_naming_typedef (tree type)
4900 dw_die_ref die = lookup_type_die (type);
4901 return strip_naming_typedef (type, die);
4904 /* Equate a DIE to a given type specifier. */
4906 static inline void
4907 equate_type_number_to_die (tree type, dw_die_ref type_die)
4909 TYPE_SYMTAB_DIE (type) = type_die;
4912 /* Returns a hash value for X (which really is a die_struct). */
4914 static hashval_t
4915 decl_die_table_hash (const void *x)
4917 return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4920 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4922 static int
4923 decl_die_table_eq (const void *x, const void *y)
4925 return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4928 /* Return the DIE associated with a given declaration. */
4930 static inline dw_die_ref
4931 lookup_decl_die (tree decl)
4933 return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4936 /* Returns a hash value for X (which really is a var_loc_list). */
4938 static hashval_t
4939 decl_loc_table_hash (const void *x)
4941 return (hashval_t) ((const var_loc_list *) x)->decl_id;
4944 /* Return nonzero if decl_id of var_loc_list X is the same as
4945 UID of decl *Y. */
4947 static int
4948 decl_loc_table_eq (const void *x, const void *y)
4950 return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4953 /* Return the var_loc list associated with a given declaration. */
4955 static inline var_loc_list *
4956 lookup_decl_loc (const_tree decl)
4958 if (!decl_loc_table)
4959 return NULL;
4960 return (var_loc_list *)
4961 htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4964 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
4966 static hashval_t
4967 cached_dw_loc_list_table_hash (const void *x)
4969 return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4972 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4973 UID of decl *Y. */
4975 static int
4976 cached_dw_loc_list_table_eq (const void *x, const void *y)
4978 return (((const cached_dw_loc_list *) x)->decl_id
4979 == DECL_UID ((const_tree) y));
4982 /* Equate a DIE to a particular declaration. */
4984 static void
4985 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4987 unsigned int decl_id = DECL_UID (decl);
4988 void **slot;
4990 slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4991 *slot = decl_die;
4992 decl_die->decl_id = decl_id;
4995 /* Return how many bits covers PIECE EXPR_LIST. */
4997 static int
4998 decl_piece_bitsize (rtx piece)
5000 int ret = (int) GET_MODE (piece);
5001 if (ret)
5002 return ret;
5003 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5004 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5005 return INTVAL (XEXP (XEXP (piece, 0), 0));
5008 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5010 static rtx *
5011 decl_piece_varloc_ptr (rtx piece)
5013 if ((int) GET_MODE (piece))
5014 return &XEXP (piece, 0);
5015 else
5016 return &XEXP (XEXP (piece, 0), 1);
5019 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5020 Next is the chain of following piece nodes. */
5022 static rtx
5023 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5025 if (bitsize <= (int) MAX_MACHINE_MODE)
5026 return alloc_EXPR_LIST (bitsize, loc_note, next);
5027 else
5028 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5029 GEN_INT (bitsize),
5030 loc_note), next);
5033 /* Return rtx that should be stored into loc field for
5034 LOC_NOTE and BITPOS/BITSIZE. */
5036 static rtx
5037 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5038 HOST_WIDE_INT bitsize)
5040 if (bitsize != -1)
5042 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5043 if (bitpos != 0)
5044 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5046 return loc_note;
5049 /* This function either modifies location piece list *DEST in
5050 place (if SRC and INNER is NULL), or copies location piece list
5051 *SRC to *DEST while modifying it. Location BITPOS is modified
5052 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5053 not copied and if needed some padding around it is added.
5054 When modifying in place, DEST should point to EXPR_LIST where
5055 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5056 to the start of the whole list and INNER points to the EXPR_LIST
5057 where earlier pieces cover PIECE_BITPOS bits. */
5059 static void
5060 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5061 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5062 HOST_WIDE_INT bitsize, rtx loc_note)
5064 int diff;
5065 bool copy = inner != NULL;
5067 if (copy)
5069 /* First copy all nodes preceding the current bitpos. */
5070 while (src != inner)
5072 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5073 decl_piece_bitsize (*src), NULL_RTX);
5074 dest = &XEXP (*dest, 1);
5075 src = &XEXP (*src, 1);
5078 /* Add padding if needed. */
5079 if (bitpos != piece_bitpos)
5081 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5082 copy ? NULL_RTX : *dest);
5083 dest = &XEXP (*dest, 1);
5085 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5087 gcc_assert (!copy);
5088 /* A piece with correct bitpos and bitsize already exist,
5089 just update the location for it and return. */
5090 *decl_piece_varloc_ptr (*dest) = loc_note;
5091 return;
5093 /* Add the piece that changed. */
5094 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5095 dest = &XEXP (*dest, 1);
5096 /* Skip over pieces that overlap it. */
5097 diff = bitpos - piece_bitpos + bitsize;
5098 if (!copy)
5099 src = dest;
5100 while (diff > 0 && *src)
5102 rtx piece = *src;
5103 diff -= decl_piece_bitsize (piece);
5104 if (copy)
5105 src = &XEXP (piece, 1);
5106 else
5108 *src = XEXP (piece, 1);
5109 free_EXPR_LIST_node (piece);
5112 /* Add padding if needed. */
5113 if (diff < 0 && *src)
5115 if (!copy)
5116 dest = src;
5117 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5118 dest = &XEXP (*dest, 1);
5120 if (!copy)
5121 return;
5122 /* Finally copy all nodes following it. */
5123 while (*src)
5125 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5126 decl_piece_bitsize (*src), NULL_RTX);
5127 dest = &XEXP (*dest, 1);
5128 src = &XEXP (*src, 1);
5132 /* Add a variable location node to the linked list for DECL. */
5134 static struct var_loc_node *
5135 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5137 unsigned int decl_id;
5138 var_loc_list *temp;
5139 void **slot;
5140 struct var_loc_node *loc = NULL;
5141 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5143 if (TREE_CODE (decl) == VAR_DECL
5144 && DECL_HAS_DEBUG_EXPR_P (decl))
5146 tree realdecl = DECL_DEBUG_EXPR (decl);
5147 if (handled_component_p (realdecl)
5148 || (TREE_CODE (realdecl) == MEM_REF
5149 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5151 HOST_WIDE_INT maxsize;
5152 tree innerdecl;
5153 innerdecl
5154 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5155 if (!DECL_P (innerdecl)
5156 || DECL_IGNORED_P (innerdecl)
5157 || TREE_STATIC (innerdecl)
5158 || bitsize <= 0
5159 || bitpos + bitsize > 256
5160 || bitsize != maxsize)
5161 return NULL;
5162 decl = innerdecl;
5166 decl_id = DECL_UID (decl);
5167 slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5168 if (*slot == NULL)
5170 temp = ggc_cleared_alloc<var_loc_list> ();
5171 temp->decl_id = decl_id;
5172 *slot = temp;
5174 else
5175 temp = (var_loc_list *) *slot;
5177 /* For PARM_DECLs try to keep around the original incoming value,
5178 even if that means we'll emit a zero-range .debug_loc entry. */
5179 if (temp->last
5180 && temp->first == temp->last
5181 && TREE_CODE (decl) == PARM_DECL
5182 && NOTE_P (temp->first->loc)
5183 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5184 && DECL_INCOMING_RTL (decl)
5185 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5186 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5187 == GET_CODE (DECL_INCOMING_RTL (decl))
5188 && prev_real_insn (temp->first->loc) == NULL_RTX
5189 && (bitsize != -1
5190 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5191 NOTE_VAR_LOCATION_LOC (loc_note))
5192 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5193 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5195 loc = ggc_cleared_alloc<var_loc_node> ();
5196 temp->first->next = loc;
5197 temp->last = loc;
5198 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5200 else if (temp->last)
5202 struct var_loc_node *last = temp->last, *unused = NULL;
5203 rtx *piece_loc = NULL, last_loc_note;
5204 int piece_bitpos = 0;
5205 if (last->next)
5207 last = last->next;
5208 gcc_assert (last->next == NULL);
5210 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5212 piece_loc = &last->loc;
5215 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5216 if (piece_bitpos + cur_bitsize > bitpos)
5217 break;
5218 piece_bitpos += cur_bitsize;
5219 piece_loc = &XEXP (*piece_loc, 1);
5221 while (*piece_loc);
5223 /* TEMP->LAST here is either pointer to the last but one or
5224 last element in the chained list, LAST is pointer to the
5225 last element. */
5226 if (label && strcmp (last->label, label) == 0)
5228 /* For SRA optimized variables if there weren't any real
5229 insns since last note, just modify the last node. */
5230 if (piece_loc != NULL)
5232 adjust_piece_list (piece_loc, NULL, NULL,
5233 bitpos, piece_bitpos, bitsize, loc_note);
5234 return NULL;
5236 /* If the last note doesn't cover any instructions, remove it. */
5237 if (temp->last != last)
5239 temp->last->next = NULL;
5240 unused = last;
5241 last = temp->last;
5242 gcc_assert (strcmp (last->label, label) != 0);
5244 else
5246 gcc_assert (temp->first == temp->last
5247 || (temp->first->next == temp->last
5248 && TREE_CODE (decl) == PARM_DECL));
5249 memset (temp->last, '\0', sizeof (*temp->last));
5250 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5251 return temp->last;
5254 if (bitsize == -1 && NOTE_P (last->loc))
5255 last_loc_note = last->loc;
5256 else if (piece_loc != NULL
5257 && *piece_loc != NULL_RTX
5258 && piece_bitpos == bitpos
5259 && decl_piece_bitsize (*piece_loc) == bitsize)
5260 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5261 else
5262 last_loc_note = NULL_RTX;
5263 /* If the current location is the same as the end of the list,
5264 and either both or neither of the locations is uninitialized,
5265 we have nothing to do. */
5266 if (last_loc_note == NULL_RTX
5267 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5268 NOTE_VAR_LOCATION_LOC (loc_note)))
5269 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5270 != NOTE_VAR_LOCATION_STATUS (loc_note))
5271 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5272 == VAR_INIT_STATUS_UNINITIALIZED)
5273 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5274 == VAR_INIT_STATUS_UNINITIALIZED))))
5276 /* Add LOC to the end of list and update LAST. If the last
5277 element of the list has been removed above, reuse its
5278 memory for the new node, otherwise allocate a new one. */
5279 if (unused)
5281 loc = unused;
5282 memset (loc, '\0', sizeof (*loc));
5284 else
5285 loc = ggc_cleared_alloc<var_loc_node> ();
5286 if (bitsize == -1 || piece_loc == NULL)
5287 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5288 else
5289 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5290 bitpos, piece_bitpos, bitsize, loc_note);
5291 last->next = loc;
5292 /* Ensure TEMP->LAST will point either to the new last but one
5293 element of the chain, or to the last element in it. */
5294 if (last != temp->last)
5295 temp->last = last;
5297 else if (unused)
5298 ggc_free (unused);
5300 else
5302 loc = ggc_cleared_alloc<var_loc_node> ();
5303 temp->first = loc;
5304 temp->last = loc;
5305 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5307 return loc;
5310 /* Keep track of the number of spaces used to indent the
5311 output of the debugging routines that print the structure of
5312 the DIE internal representation. */
5313 static int print_indent;
5315 /* Indent the line the number of spaces given by print_indent. */
5317 static inline void
5318 print_spaces (FILE *outfile)
5320 fprintf (outfile, "%*s", print_indent, "");
5323 /* Print a type signature in hex. */
5325 static inline void
5326 print_signature (FILE *outfile, char *sig)
5328 int i;
5330 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5331 fprintf (outfile, "%02x", sig[i] & 0xff);
5334 /* Print the information associated with a given DIE, and its children.
5335 This routine is a debugging aid only. */
5337 static void
5338 print_die (dw_die_ref die, FILE *outfile)
5340 dw_attr_ref a;
5341 dw_die_ref c;
5342 unsigned ix;
5344 print_spaces (outfile);
5345 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5346 die->die_offset, dwarf_tag_name (die->die_tag),
5347 (void*) die);
5348 print_spaces (outfile);
5349 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5350 fprintf (outfile, " offset: %ld", die->die_offset);
5351 fprintf (outfile, " mark: %d\n", die->die_mark);
5353 if (die->comdat_type_p)
5355 print_spaces (outfile);
5356 fprintf (outfile, " signature: ");
5357 print_signature (outfile, die->die_id.die_type_node->signature);
5358 fprintf (outfile, "\n");
5361 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5363 print_spaces (outfile);
5364 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5366 switch (AT_class (a))
5368 case dw_val_class_addr:
5369 fprintf (outfile, "address");
5370 break;
5371 case dw_val_class_offset:
5372 fprintf (outfile, "offset");
5373 break;
5374 case dw_val_class_loc:
5375 fprintf (outfile, "location descriptor");
5376 break;
5377 case dw_val_class_loc_list:
5378 fprintf (outfile, "location list -> label:%s",
5379 AT_loc_list (a)->ll_symbol);
5380 break;
5381 case dw_val_class_range_list:
5382 fprintf (outfile, "range list");
5383 break;
5384 case dw_val_class_const:
5385 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5386 break;
5387 case dw_val_class_unsigned_const:
5388 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5389 break;
5390 case dw_val_class_const_double:
5391 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5392 HOST_WIDE_INT_PRINT_UNSIGNED")",
5393 a->dw_attr_val.v.val_double.high,
5394 a->dw_attr_val.v.val_double.low);
5395 break;
5396 case dw_val_class_wide_int:
5398 int i = a->dw_attr_val.v.val_wide->get_len ();
5399 fprintf (outfile, "constant (");
5400 gcc_assert (i > 0);
5401 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5402 fprintf (outfile, "0x");
5403 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5404 a->dw_attr_val.v.val_wide->elt (--i));
5405 while (--i >= 0)
5406 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5407 a->dw_attr_val.v.val_wide->elt (i));
5408 fprintf (outfile, ")");
5409 break;
5411 case dw_val_class_vec:
5412 fprintf (outfile, "floating-point or vector constant");
5413 break;
5414 case dw_val_class_flag:
5415 fprintf (outfile, "%u", AT_flag (a));
5416 break;
5417 case dw_val_class_die_ref:
5418 if (AT_ref (a) != NULL)
5420 if (AT_ref (a)->comdat_type_p)
5422 fprintf (outfile, "die -> signature: ");
5423 print_signature (outfile,
5424 AT_ref (a)->die_id.die_type_node->signature);
5426 else if (AT_ref (a)->die_id.die_symbol)
5427 fprintf (outfile, "die -> label: %s",
5428 AT_ref (a)->die_id.die_symbol);
5429 else
5430 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5431 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5433 else
5434 fprintf (outfile, "die -> <null>");
5435 break;
5436 case dw_val_class_vms_delta:
5437 fprintf (outfile, "delta: @slotcount(%s-%s)",
5438 AT_vms_delta2 (a), AT_vms_delta1 (a));
5439 break;
5440 case dw_val_class_lbl_id:
5441 case dw_val_class_lineptr:
5442 case dw_val_class_macptr:
5443 case dw_val_class_high_pc:
5444 fprintf (outfile, "label: %s", AT_lbl (a));
5445 break;
5446 case dw_val_class_str:
5447 if (AT_string (a) != NULL)
5448 fprintf (outfile, "\"%s\"", AT_string (a));
5449 else
5450 fprintf (outfile, "<null>");
5451 break;
5452 case dw_val_class_file:
5453 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5454 AT_file (a)->emitted_number);
5455 break;
5456 case dw_val_class_data8:
5458 int i;
5460 for (i = 0; i < 8; i++)
5461 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5462 break;
5464 default:
5465 break;
5468 fprintf (outfile, "\n");
5471 if (die->die_child != NULL)
5473 print_indent += 4;
5474 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5475 print_indent -= 4;
5477 if (print_indent == 0)
5478 fprintf (outfile, "\n");
5481 /* Print the information collected for a given DIE. */
5483 DEBUG_FUNCTION void
5484 debug_dwarf_die (dw_die_ref die)
5486 print_die (die, stderr);
5489 DEBUG_FUNCTION void
5490 debug (die_struct &ref)
5492 print_die (&ref, stderr);
5495 DEBUG_FUNCTION void
5496 debug (die_struct *ptr)
5498 if (ptr)
5499 debug (*ptr);
5500 else
5501 fprintf (stderr, "<nil>\n");
5505 /* Print all DWARF information collected for the compilation unit.
5506 This routine is a debugging aid only. */
5508 DEBUG_FUNCTION void
5509 debug_dwarf (void)
5511 print_indent = 0;
5512 print_die (comp_unit_die (), stderr);
5515 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5516 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5517 DIE that marks the start of the DIEs for this include file. */
5519 static dw_die_ref
5520 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5522 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5523 dw_die_ref new_unit = gen_compile_unit_die (filename);
5525 new_unit->die_sib = old_unit;
5526 return new_unit;
5529 /* Close an include-file CU and reopen the enclosing one. */
5531 static dw_die_ref
5532 pop_compile_unit (dw_die_ref old_unit)
5534 dw_die_ref new_unit = old_unit->die_sib;
5536 old_unit->die_sib = NULL;
5537 return new_unit;
5540 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5541 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5542 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5544 /* Calculate the checksum of a location expression. */
5546 static inline void
5547 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5549 int tem;
5550 hashval_t hash = 0;
5552 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5553 CHECKSUM (tem);
5554 hash = hash_loc_operands (loc, hash);
5555 CHECKSUM (hash);
5558 /* Calculate the checksum of an attribute. */
5560 static void
5561 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5563 dw_loc_descr_ref loc;
5564 rtx r;
5566 CHECKSUM (at->dw_attr);
5568 /* We don't care that this was compiled with a different compiler
5569 snapshot; if the output is the same, that's what matters. */
5570 if (at->dw_attr == DW_AT_producer)
5571 return;
5573 switch (AT_class (at))
5575 case dw_val_class_const:
5576 CHECKSUM (at->dw_attr_val.v.val_int);
5577 break;
5578 case dw_val_class_unsigned_const:
5579 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5580 break;
5581 case dw_val_class_const_double:
5582 CHECKSUM (at->dw_attr_val.v.val_double);
5583 break;
5584 case dw_val_class_wide_int:
5585 CHECKSUM (*at->dw_attr_val.v.val_wide);
5586 break;
5587 case dw_val_class_vec:
5588 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5589 (at->dw_attr_val.v.val_vec.length
5590 * at->dw_attr_val.v.val_vec.elt_size));
5591 break;
5592 case dw_val_class_flag:
5593 CHECKSUM (at->dw_attr_val.v.val_flag);
5594 break;
5595 case dw_val_class_str:
5596 CHECKSUM_STRING (AT_string (at));
5597 break;
5599 case dw_val_class_addr:
5600 r = AT_addr (at);
5601 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5602 CHECKSUM_STRING (XSTR (r, 0));
5603 break;
5605 case dw_val_class_offset:
5606 CHECKSUM (at->dw_attr_val.v.val_offset);
5607 break;
5609 case dw_val_class_loc:
5610 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5611 loc_checksum (loc, ctx);
5612 break;
5614 case dw_val_class_die_ref:
5615 die_checksum (AT_ref (at), ctx, mark);
5616 break;
5618 case dw_val_class_fde_ref:
5619 case dw_val_class_vms_delta:
5620 case dw_val_class_lbl_id:
5621 case dw_val_class_lineptr:
5622 case dw_val_class_macptr:
5623 case dw_val_class_high_pc:
5624 break;
5626 case dw_val_class_file:
5627 CHECKSUM_STRING (AT_file (at)->filename);
5628 break;
5630 case dw_val_class_data8:
5631 CHECKSUM (at->dw_attr_val.v.val_data8);
5632 break;
5634 default:
5635 break;
5639 /* Calculate the checksum of a DIE. */
5641 static void
5642 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5644 dw_die_ref c;
5645 dw_attr_ref a;
5646 unsigned ix;
5648 /* To avoid infinite recursion. */
5649 if (die->die_mark)
5651 CHECKSUM (die->die_mark);
5652 return;
5654 die->die_mark = ++(*mark);
5656 CHECKSUM (die->die_tag);
5658 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5659 attr_checksum (a, ctx, mark);
5661 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5664 #undef CHECKSUM
5665 #undef CHECKSUM_BLOCK
5666 #undef CHECKSUM_STRING
5668 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5669 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5670 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5671 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5672 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5673 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5674 #define CHECKSUM_ATTR(FOO) \
5675 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5677 /* Calculate the checksum of a number in signed LEB128 format. */
5679 static void
5680 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5682 unsigned char byte;
5683 bool more;
5685 while (1)
5687 byte = (value & 0x7f);
5688 value >>= 7;
5689 more = !((value == 0 && (byte & 0x40) == 0)
5690 || (value == -1 && (byte & 0x40) != 0));
5691 if (more)
5692 byte |= 0x80;
5693 CHECKSUM (byte);
5694 if (!more)
5695 break;
5699 /* Calculate the checksum of a number in unsigned LEB128 format. */
5701 static void
5702 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5704 while (1)
5706 unsigned char byte = (value & 0x7f);
5707 value >>= 7;
5708 if (value != 0)
5709 /* More bytes to follow. */
5710 byte |= 0x80;
5711 CHECKSUM (byte);
5712 if (value == 0)
5713 break;
5717 /* Checksum the context of the DIE. This adds the names of any
5718 surrounding namespaces or structures to the checksum. */
5720 static void
5721 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5723 const char *name;
5724 dw_die_ref spec;
5725 int tag = die->die_tag;
5727 if (tag != DW_TAG_namespace
5728 && tag != DW_TAG_structure_type
5729 && tag != DW_TAG_class_type)
5730 return;
5732 name = get_AT_string (die, DW_AT_name);
5734 spec = get_AT_ref (die, DW_AT_specification);
5735 if (spec != NULL)
5736 die = spec;
5738 if (die->die_parent != NULL)
5739 checksum_die_context (die->die_parent, ctx);
5741 CHECKSUM_ULEB128 ('C');
5742 CHECKSUM_ULEB128 (tag);
5743 if (name != NULL)
5744 CHECKSUM_STRING (name);
5747 /* Calculate the checksum of a location expression. */
5749 static inline void
5750 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5752 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5753 were emitted as a DW_FORM_sdata instead of a location expression. */
5754 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5756 CHECKSUM_ULEB128 (DW_FORM_sdata);
5757 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5758 return;
5761 /* Otherwise, just checksum the raw location expression. */
5762 while (loc != NULL)
5764 hashval_t hash = 0;
5766 CHECKSUM_ULEB128 (loc->dtprel);
5767 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5768 hash = hash_loc_operands (loc, hash);
5769 CHECKSUM (hash);
5770 loc = loc->dw_loc_next;
5774 /* Calculate the checksum of an attribute. */
5776 static void
5777 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5778 struct md5_ctx *ctx, int *mark)
5780 dw_loc_descr_ref loc;
5781 rtx r;
5783 if (AT_class (at) == dw_val_class_die_ref)
5785 dw_die_ref target_die = AT_ref (at);
5787 /* For pointer and reference types, we checksum only the (qualified)
5788 name of the target type (if there is a name). For friend entries,
5789 we checksum only the (qualified) name of the target type or function.
5790 This allows the checksum to remain the same whether the target type
5791 is complete or not. */
5792 if ((at->dw_attr == DW_AT_type
5793 && (tag == DW_TAG_pointer_type
5794 || tag == DW_TAG_reference_type
5795 || tag == DW_TAG_rvalue_reference_type
5796 || tag == DW_TAG_ptr_to_member_type))
5797 || (at->dw_attr == DW_AT_friend
5798 && tag == DW_TAG_friend))
5800 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5802 if (name_attr != NULL)
5804 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5806 if (decl == NULL)
5807 decl = target_die;
5808 CHECKSUM_ULEB128 ('N');
5809 CHECKSUM_ULEB128 (at->dw_attr);
5810 if (decl->die_parent != NULL)
5811 checksum_die_context (decl->die_parent, ctx);
5812 CHECKSUM_ULEB128 ('E');
5813 CHECKSUM_STRING (AT_string (name_attr));
5814 return;
5818 /* For all other references to another DIE, we check to see if the
5819 target DIE has already been visited. If it has, we emit a
5820 backward reference; if not, we descend recursively. */
5821 if (target_die->die_mark > 0)
5823 CHECKSUM_ULEB128 ('R');
5824 CHECKSUM_ULEB128 (at->dw_attr);
5825 CHECKSUM_ULEB128 (target_die->die_mark);
5827 else
5829 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5831 if (decl == NULL)
5832 decl = target_die;
5833 target_die->die_mark = ++(*mark);
5834 CHECKSUM_ULEB128 ('T');
5835 CHECKSUM_ULEB128 (at->dw_attr);
5836 if (decl->die_parent != NULL)
5837 checksum_die_context (decl->die_parent, ctx);
5838 die_checksum_ordered (target_die, ctx, mark);
5840 return;
5843 CHECKSUM_ULEB128 ('A');
5844 CHECKSUM_ULEB128 (at->dw_attr);
5846 switch (AT_class (at))
5848 case dw_val_class_const:
5849 CHECKSUM_ULEB128 (DW_FORM_sdata);
5850 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5851 break;
5853 case dw_val_class_unsigned_const:
5854 CHECKSUM_ULEB128 (DW_FORM_sdata);
5855 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5856 break;
5858 case dw_val_class_const_double:
5859 CHECKSUM_ULEB128 (DW_FORM_block);
5860 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5861 CHECKSUM (at->dw_attr_val.v.val_double);
5862 break;
5864 case dw_val_class_wide_int:
5865 CHECKSUM_ULEB128 (DW_FORM_block);
5866 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5867 CHECKSUM (*at->dw_attr_val.v.val_wide);
5868 break;
5870 case dw_val_class_vec:
5871 CHECKSUM_ULEB128 (DW_FORM_block);
5872 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5873 * at->dw_attr_val.v.val_vec.elt_size);
5874 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5875 (at->dw_attr_val.v.val_vec.length
5876 * at->dw_attr_val.v.val_vec.elt_size));
5877 break;
5879 case dw_val_class_flag:
5880 CHECKSUM_ULEB128 (DW_FORM_flag);
5881 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5882 break;
5884 case dw_val_class_str:
5885 CHECKSUM_ULEB128 (DW_FORM_string);
5886 CHECKSUM_STRING (AT_string (at));
5887 break;
5889 case dw_val_class_addr:
5890 r = AT_addr (at);
5891 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5892 CHECKSUM_ULEB128 (DW_FORM_string);
5893 CHECKSUM_STRING (XSTR (r, 0));
5894 break;
5896 case dw_val_class_offset:
5897 CHECKSUM_ULEB128 (DW_FORM_sdata);
5898 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5899 break;
5901 case dw_val_class_loc:
5902 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5903 loc_checksum_ordered (loc, ctx);
5904 break;
5906 case dw_val_class_fde_ref:
5907 case dw_val_class_lbl_id:
5908 case dw_val_class_lineptr:
5909 case dw_val_class_macptr:
5910 case dw_val_class_high_pc:
5911 break;
5913 case dw_val_class_file:
5914 CHECKSUM_ULEB128 (DW_FORM_string);
5915 CHECKSUM_STRING (AT_file (at)->filename);
5916 break;
5918 case dw_val_class_data8:
5919 CHECKSUM (at->dw_attr_val.v.val_data8);
5920 break;
5922 default:
5923 break;
5927 struct checksum_attributes
5929 dw_attr_ref at_name;
5930 dw_attr_ref at_type;
5931 dw_attr_ref at_friend;
5932 dw_attr_ref at_accessibility;
5933 dw_attr_ref at_address_class;
5934 dw_attr_ref at_allocated;
5935 dw_attr_ref at_artificial;
5936 dw_attr_ref at_associated;
5937 dw_attr_ref at_binary_scale;
5938 dw_attr_ref at_bit_offset;
5939 dw_attr_ref at_bit_size;
5940 dw_attr_ref at_bit_stride;
5941 dw_attr_ref at_byte_size;
5942 dw_attr_ref at_byte_stride;
5943 dw_attr_ref at_const_value;
5944 dw_attr_ref at_containing_type;
5945 dw_attr_ref at_count;
5946 dw_attr_ref at_data_location;
5947 dw_attr_ref at_data_member_location;
5948 dw_attr_ref at_decimal_scale;
5949 dw_attr_ref at_decimal_sign;
5950 dw_attr_ref at_default_value;
5951 dw_attr_ref at_digit_count;
5952 dw_attr_ref at_discr;
5953 dw_attr_ref at_discr_list;
5954 dw_attr_ref at_discr_value;
5955 dw_attr_ref at_encoding;
5956 dw_attr_ref at_endianity;
5957 dw_attr_ref at_explicit;
5958 dw_attr_ref at_is_optional;
5959 dw_attr_ref at_location;
5960 dw_attr_ref at_lower_bound;
5961 dw_attr_ref at_mutable;
5962 dw_attr_ref at_ordering;
5963 dw_attr_ref at_picture_string;
5964 dw_attr_ref at_prototyped;
5965 dw_attr_ref at_small;
5966 dw_attr_ref at_segment;
5967 dw_attr_ref at_string_length;
5968 dw_attr_ref at_threads_scaled;
5969 dw_attr_ref at_upper_bound;
5970 dw_attr_ref at_use_location;
5971 dw_attr_ref at_use_UTF8;
5972 dw_attr_ref at_variable_parameter;
5973 dw_attr_ref at_virtuality;
5974 dw_attr_ref at_visibility;
5975 dw_attr_ref at_vtable_elem_location;
5978 /* Collect the attributes that we will want to use for the checksum. */
5980 static void
5981 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5983 dw_attr_ref a;
5984 unsigned ix;
5986 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5988 switch (a->dw_attr)
5990 case DW_AT_name:
5991 attrs->at_name = a;
5992 break;
5993 case DW_AT_type:
5994 attrs->at_type = a;
5995 break;
5996 case DW_AT_friend:
5997 attrs->at_friend = a;
5998 break;
5999 case DW_AT_accessibility:
6000 attrs->at_accessibility = a;
6001 break;
6002 case DW_AT_address_class:
6003 attrs->at_address_class = a;
6004 break;
6005 case DW_AT_allocated:
6006 attrs->at_allocated = a;
6007 break;
6008 case DW_AT_artificial:
6009 attrs->at_artificial = a;
6010 break;
6011 case DW_AT_associated:
6012 attrs->at_associated = a;
6013 break;
6014 case DW_AT_binary_scale:
6015 attrs->at_binary_scale = a;
6016 break;
6017 case DW_AT_bit_offset:
6018 attrs->at_bit_offset = a;
6019 break;
6020 case DW_AT_bit_size:
6021 attrs->at_bit_size = a;
6022 break;
6023 case DW_AT_bit_stride:
6024 attrs->at_bit_stride = a;
6025 break;
6026 case DW_AT_byte_size:
6027 attrs->at_byte_size = a;
6028 break;
6029 case DW_AT_byte_stride:
6030 attrs->at_byte_stride = a;
6031 break;
6032 case DW_AT_const_value:
6033 attrs->at_const_value = a;
6034 break;
6035 case DW_AT_containing_type:
6036 attrs->at_containing_type = a;
6037 break;
6038 case DW_AT_count:
6039 attrs->at_count = a;
6040 break;
6041 case DW_AT_data_location:
6042 attrs->at_data_location = a;
6043 break;
6044 case DW_AT_data_member_location:
6045 attrs->at_data_member_location = a;
6046 break;
6047 case DW_AT_decimal_scale:
6048 attrs->at_decimal_scale = a;
6049 break;
6050 case DW_AT_decimal_sign:
6051 attrs->at_decimal_sign = a;
6052 break;
6053 case DW_AT_default_value:
6054 attrs->at_default_value = a;
6055 break;
6056 case DW_AT_digit_count:
6057 attrs->at_digit_count = a;
6058 break;
6059 case DW_AT_discr:
6060 attrs->at_discr = a;
6061 break;
6062 case DW_AT_discr_list:
6063 attrs->at_discr_list = a;
6064 break;
6065 case DW_AT_discr_value:
6066 attrs->at_discr_value = a;
6067 break;
6068 case DW_AT_encoding:
6069 attrs->at_encoding = a;
6070 break;
6071 case DW_AT_endianity:
6072 attrs->at_endianity = a;
6073 break;
6074 case DW_AT_explicit:
6075 attrs->at_explicit = a;
6076 break;
6077 case DW_AT_is_optional:
6078 attrs->at_is_optional = a;
6079 break;
6080 case DW_AT_location:
6081 attrs->at_location = a;
6082 break;
6083 case DW_AT_lower_bound:
6084 attrs->at_lower_bound = a;
6085 break;
6086 case DW_AT_mutable:
6087 attrs->at_mutable = a;
6088 break;
6089 case DW_AT_ordering:
6090 attrs->at_ordering = a;
6091 break;
6092 case DW_AT_picture_string:
6093 attrs->at_picture_string = a;
6094 break;
6095 case DW_AT_prototyped:
6096 attrs->at_prototyped = a;
6097 break;
6098 case DW_AT_small:
6099 attrs->at_small = a;
6100 break;
6101 case DW_AT_segment:
6102 attrs->at_segment = a;
6103 break;
6104 case DW_AT_string_length:
6105 attrs->at_string_length = a;
6106 break;
6107 case DW_AT_threads_scaled:
6108 attrs->at_threads_scaled = a;
6109 break;
6110 case DW_AT_upper_bound:
6111 attrs->at_upper_bound = a;
6112 break;
6113 case DW_AT_use_location:
6114 attrs->at_use_location = a;
6115 break;
6116 case DW_AT_use_UTF8:
6117 attrs->at_use_UTF8 = a;
6118 break;
6119 case DW_AT_variable_parameter:
6120 attrs->at_variable_parameter = a;
6121 break;
6122 case DW_AT_virtuality:
6123 attrs->at_virtuality = a;
6124 break;
6125 case DW_AT_visibility:
6126 attrs->at_visibility = a;
6127 break;
6128 case DW_AT_vtable_elem_location:
6129 attrs->at_vtable_elem_location = a;
6130 break;
6131 default:
6132 break;
6137 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6139 static void
6140 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6142 dw_die_ref c;
6143 dw_die_ref decl;
6144 struct checksum_attributes attrs;
6146 CHECKSUM_ULEB128 ('D');
6147 CHECKSUM_ULEB128 (die->die_tag);
6149 memset (&attrs, 0, sizeof (attrs));
6151 decl = get_AT_ref (die, DW_AT_specification);
6152 if (decl != NULL)
6153 collect_checksum_attributes (&attrs, decl);
6154 collect_checksum_attributes (&attrs, die);
6156 CHECKSUM_ATTR (attrs.at_name);
6157 CHECKSUM_ATTR (attrs.at_accessibility);
6158 CHECKSUM_ATTR (attrs.at_address_class);
6159 CHECKSUM_ATTR (attrs.at_allocated);
6160 CHECKSUM_ATTR (attrs.at_artificial);
6161 CHECKSUM_ATTR (attrs.at_associated);
6162 CHECKSUM_ATTR (attrs.at_binary_scale);
6163 CHECKSUM_ATTR (attrs.at_bit_offset);
6164 CHECKSUM_ATTR (attrs.at_bit_size);
6165 CHECKSUM_ATTR (attrs.at_bit_stride);
6166 CHECKSUM_ATTR (attrs.at_byte_size);
6167 CHECKSUM_ATTR (attrs.at_byte_stride);
6168 CHECKSUM_ATTR (attrs.at_const_value);
6169 CHECKSUM_ATTR (attrs.at_containing_type);
6170 CHECKSUM_ATTR (attrs.at_count);
6171 CHECKSUM_ATTR (attrs.at_data_location);
6172 CHECKSUM_ATTR (attrs.at_data_member_location);
6173 CHECKSUM_ATTR (attrs.at_decimal_scale);
6174 CHECKSUM_ATTR (attrs.at_decimal_sign);
6175 CHECKSUM_ATTR (attrs.at_default_value);
6176 CHECKSUM_ATTR (attrs.at_digit_count);
6177 CHECKSUM_ATTR (attrs.at_discr);
6178 CHECKSUM_ATTR (attrs.at_discr_list);
6179 CHECKSUM_ATTR (attrs.at_discr_value);
6180 CHECKSUM_ATTR (attrs.at_encoding);
6181 CHECKSUM_ATTR (attrs.at_endianity);
6182 CHECKSUM_ATTR (attrs.at_explicit);
6183 CHECKSUM_ATTR (attrs.at_is_optional);
6184 CHECKSUM_ATTR (attrs.at_location);
6185 CHECKSUM_ATTR (attrs.at_lower_bound);
6186 CHECKSUM_ATTR (attrs.at_mutable);
6187 CHECKSUM_ATTR (attrs.at_ordering);
6188 CHECKSUM_ATTR (attrs.at_picture_string);
6189 CHECKSUM_ATTR (attrs.at_prototyped);
6190 CHECKSUM_ATTR (attrs.at_small);
6191 CHECKSUM_ATTR (attrs.at_segment);
6192 CHECKSUM_ATTR (attrs.at_string_length);
6193 CHECKSUM_ATTR (attrs.at_threads_scaled);
6194 CHECKSUM_ATTR (attrs.at_upper_bound);
6195 CHECKSUM_ATTR (attrs.at_use_location);
6196 CHECKSUM_ATTR (attrs.at_use_UTF8);
6197 CHECKSUM_ATTR (attrs.at_variable_parameter);
6198 CHECKSUM_ATTR (attrs.at_virtuality);
6199 CHECKSUM_ATTR (attrs.at_visibility);
6200 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6201 CHECKSUM_ATTR (attrs.at_type);
6202 CHECKSUM_ATTR (attrs.at_friend);
6204 /* Checksum the child DIEs. */
6205 c = die->die_child;
6206 if (c) do {
6207 dw_attr_ref name_attr;
6209 c = c->die_sib;
6210 name_attr = get_AT (c, DW_AT_name);
6211 if (is_template_instantiation (c))
6213 /* Ignore instantiations of member type and function templates. */
6215 else if (name_attr != NULL
6216 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6218 /* Use a shallow checksum for named nested types and member
6219 functions. */
6220 CHECKSUM_ULEB128 ('S');
6221 CHECKSUM_ULEB128 (c->die_tag);
6222 CHECKSUM_STRING (AT_string (name_attr));
6224 else
6226 /* Use a deep checksum for other children. */
6227 /* Mark this DIE so it gets processed when unmarking. */
6228 if (c->die_mark == 0)
6229 c->die_mark = -1;
6230 die_checksum_ordered (c, ctx, mark);
6232 } while (c != die->die_child);
6234 CHECKSUM_ULEB128 (0);
6237 /* Add a type name and tag to a hash. */
6238 static void
6239 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6241 CHECKSUM_ULEB128 (tag);
6242 CHECKSUM_STRING (name);
6245 #undef CHECKSUM
6246 #undef CHECKSUM_STRING
6247 #undef CHECKSUM_ATTR
6248 #undef CHECKSUM_LEB128
6249 #undef CHECKSUM_ULEB128
6251 /* Generate the type signature for DIE. This is computed by generating an
6252 MD5 checksum over the DIE's tag, its relevant attributes, and its
6253 children. Attributes that are references to other DIEs are processed
6254 by recursion, using the MARK field to prevent infinite recursion.
6255 If the DIE is nested inside a namespace or another type, we also
6256 need to include that context in the signature. The lower 64 bits
6257 of the resulting MD5 checksum comprise the signature. */
6259 static void
6260 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6262 int mark;
6263 const char *name;
6264 unsigned char checksum[16];
6265 struct md5_ctx ctx;
6266 dw_die_ref decl;
6267 dw_die_ref parent;
6269 name = get_AT_string (die, DW_AT_name);
6270 decl = get_AT_ref (die, DW_AT_specification);
6271 parent = get_die_parent (die);
6273 /* First, compute a signature for just the type name (and its surrounding
6274 context, if any. This is stored in the type unit DIE for link-time
6275 ODR (one-definition rule) checking. */
6277 if (is_cxx () && name != NULL)
6279 md5_init_ctx (&ctx);
6281 /* Checksum the names of surrounding namespaces and structures. */
6282 if (parent != NULL)
6283 checksum_die_context (parent, &ctx);
6285 /* Checksum the current DIE. */
6286 die_odr_checksum (die->die_tag, name, &ctx);
6287 md5_finish_ctx (&ctx, checksum);
6289 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6292 /* Next, compute the complete type signature. */
6294 md5_init_ctx (&ctx);
6295 mark = 1;
6296 die->die_mark = mark;
6298 /* Checksum the names of surrounding namespaces and structures. */
6299 if (parent != NULL)
6300 checksum_die_context (parent, &ctx);
6302 /* Checksum the DIE and its children. */
6303 die_checksum_ordered (die, &ctx, &mark);
6304 unmark_all_dies (die);
6305 md5_finish_ctx (&ctx, checksum);
6307 /* Store the signature in the type node and link the type DIE and the
6308 type node together. */
6309 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6310 DWARF_TYPE_SIGNATURE_SIZE);
6311 die->comdat_type_p = true;
6312 die->die_id.die_type_node = type_node;
6313 type_node->type_die = die;
6315 /* If the DIE is a specification, link its declaration to the type node
6316 as well. */
6317 if (decl != NULL)
6319 decl->comdat_type_p = true;
6320 decl->die_id.die_type_node = type_node;
6324 /* Do the location expressions look same? */
6325 static inline int
6326 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6328 return loc1->dw_loc_opc == loc2->dw_loc_opc
6329 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6330 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6333 /* Do the values look the same? */
6334 static int
6335 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6337 dw_loc_descr_ref loc1, loc2;
6338 rtx r1, r2;
6340 if (v1->val_class != v2->val_class)
6341 return 0;
6343 switch (v1->val_class)
6345 case dw_val_class_const:
6346 return v1->v.val_int == v2->v.val_int;
6347 case dw_val_class_unsigned_const:
6348 return v1->v.val_unsigned == v2->v.val_unsigned;
6349 case dw_val_class_const_double:
6350 return v1->v.val_double.high == v2->v.val_double.high
6351 && v1->v.val_double.low == v2->v.val_double.low;
6352 case dw_val_class_wide_int:
6353 return *v1->v.val_wide == *v2->v.val_wide;
6354 case dw_val_class_vec:
6355 if (v1->v.val_vec.length != v2->v.val_vec.length
6356 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6357 return 0;
6358 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6359 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6360 return 0;
6361 return 1;
6362 case dw_val_class_flag:
6363 return v1->v.val_flag == v2->v.val_flag;
6364 case dw_val_class_str:
6365 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6367 case dw_val_class_addr:
6368 r1 = v1->v.val_addr;
6369 r2 = v2->v.val_addr;
6370 if (GET_CODE (r1) != GET_CODE (r2))
6371 return 0;
6372 return !rtx_equal_p (r1, r2);
6374 case dw_val_class_offset:
6375 return v1->v.val_offset == v2->v.val_offset;
6377 case dw_val_class_loc:
6378 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6379 loc1 && loc2;
6380 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6381 if (!same_loc_p (loc1, loc2, mark))
6382 return 0;
6383 return !loc1 && !loc2;
6385 case dw_val_class_die_ref:
6386 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6388 case dw_val_class_fde_ref:
6389 case dw_val_class_vms_delta:
6390 case dw_val_class_lbl_id:
6391 case dw_val_class_lineptr:
6392 case dw_val_class_macptr:
6393 case dw_val_class_high_pc:
6394 return 1;
6396 case dw_val_class_file:
6397 return v1->v.val_file == v2->v.val_file;
6399 case dw_val_class_data8:
6400 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6402 default:
6403 return 1;
6407 /* Do the attributes look the same? */
6409 static int
6410 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6412 if (at1->dw_attr != at2->dw_attr)
6413 return 0;
6415 /* We don't care that this was compiled with a different compiler
6416 snapshot; if the output is the same, that's what matters. */
6417 if (at1->dw_attr == DW_AT_producer)
6418 return 1;
6420 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6423 /* Do the dies look the same? */
6425 static int
6426 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6428 dw_die_ref c1, c2;
6429 dw_attr_ref a1;
6430 unsigned ix;
6432 /* To avoid infinite recursion. */
6433 if (die1->die_mark)
6434 return die1->die_mark == die2->die_mark;
6435 die1->die_mark = die2->die_mark = ++(*mark);
6437 if (die1->die_tag != die2->die_tag)
6438 return 0;
6440 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6441 return 0;
6443 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6444 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6445 return 0;
6447 c1 = die1->die_child;
6448 c2 = die2->die_child;
6449 if (! c1)
6451 if (c2)
6452 return 0;
6454 else
6455 for (;;)
6457 if (!same_die_p (c1, c2, mark))
6458 return 0;
6459 c1 = c1->die_sib;
6460 c2 = c2->die_sib;
6461 if (c1 == die1->die_child)
6463 if (c2 == die2->die_child)
6464 break;
6465 else
6466 return 0;
6470 return 1;
6473 /* Do the dies look the same? Wrapper around same_die_p. */
6475 static int
6476 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6478 int mark = 0;
6479 int ret = same_die_p (die1, die2, &mark);
6481 unmark_all_dies (die1);
6482 unmark_all_dies (die2);
6484 return ret;
6487 /* The prefix to attach to symbols on DIEs in the current comdat debug
6488 info section. */
6489 static const char *comdat_symbol_id;
6491 /* The index of the current symbol within the current comdat CU. */
6492 static unsigned int comdat_symbol_number;
6494 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6495 children, and set comdat_symbol_id accordingly. */
6497 static void
6498 compute_section_prefix (dw_die_ref unit_die)
6500 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6501 const char *base = die_name ? lbasename (die_name) : "anonymous";
6502 char *name = XALLOCAVEC (char, strlen (base) + 64);
6503 char *p;
6504 int i, mark;
6505 unsigned char checksum[16];
6506 struct md5_ctx ctx;
6508 /* Compute the checksum of the DIE, then append part of it as hex digits to
6509 the name filename of the unit. */
6511 md5_init_ctx (&ctx);
6512 mark = 0;
6513 die_checksum (unit_die, &ctx, &mark);
6514 unmark_all_dies (unit_die);
6515 md5_finish_ctx (&ctx, checksum);
6517 sprintf (name, "%s.", base);
6518 clean_symbol_name (name);
6520 p = name + strlen (name);
6521 for (i = 0; i < 4; i++)
6523 sprintf (p, "%.2x", checksum[i]);
6524 p += 2;
6527 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6528 comdat_symbol_number = 0;
6531 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6533 static int
6534 is_type_die (dw_die_ref die)
6536 switch (die->die_tag)
6538 case DW_TAG_array_type:
6539 case DW_TAG_class_type:
6540 case DW_TAG_interface_type:
6541 case DW_TAG_enumeration_type:
6542 case DW_TAG_pointer_type:
6543 case DW_TAG_reference_type:
6544 case DW_TAG_rvalue_reference_type:
6545 case DW_TAG_string_type:
6546 case DW_TAG_structure_type:
6547 case DW_TAG_subroutine_type:
6548 case DW_TAG_union_type:
6549 case DW_TAG_ptr_to_member_type:
6550 case DW_TAG_set_type:
6551 case DW_TAG_subrange_type:
6552 case DW_TAG_base_type:
6553 case DW_TAG_const_type:
6554 case DW_TAG_file_type:
6555 case DW_TAG_packed_type:
6556 case DW_TAG_volatile_type:
6557 case DW_TAG_typedef:
6558 return 1;
6559 default:
6560 return 0;
6564 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6565 Basically, we want to choose the bits that are likely to be shared between
6566 compilations (types) and leave out the bits that are specific to individual
6567 compilations (functions). */
6569 static int
6570 is_comdat_die (dw_die_ref c)
6572 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6573 we do for stabs. The advantage is a greater likelihood of sharing between
6574 objects that don't include headers in the same order (and therefore would
6575 put the base types in a different comdat). jason 8/28/00 */
6577 if (c->die_tag == DW_TAG_base_type)
6578 return 0;
6580 if (c->die_tag == DW_TAG_pointer_type
6581 || c->die_tag == DW_TAG_reference_type
6582 || c->die_tag == DW_TAG_rvalue_reference_type
6583 || c->die_tag == DW_TAG_const_type
6584 || c->die_tag == DW_TAG_volatile_type)
6586 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6588 return t ? is_comdat_die (t) : 0;
6591 return is_type_die (c);
6594 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6595 compilation unit. */
6597 static int
6598 is_symbol_die (dw_die_ref c)
6600 return (is_type_die (c)
6601 || is_declaration_die (c)
6602 || c->die_tag == DW_TAG_namespace
6603 || c->die_tag == DW_TAG_module);
6606 /* Returns true iff C is a compile-unit DIE. */
6608 static inline bool
6609 is_cu_die (dw_die_ref c)
6611 return c && c->die_tag == DW_TAG_compile_unit;
6614 /* Returns true iff C is a unit DIE of some sort. */
6616 static inline bool
6617 is_unit_die (dw_die_ref c)
6619 return c && (c->die_tag == DW_TAG_compile_unit
6620 || c->die_tag == DW_TAG_partial_unit
6621 || c->die_tag == DW_TAG_type_unit);
6624 /* Returns true iff C is a namespace DIE. */
6626 static inline bool
6627 is_namespace_die (dw_die_ref c)
6629 return c && c->die_tag == DW_TAG_namespace;
6632 /* Returns true iff C is a class or structure DIE. */
6634 static inline bool
6635 is_class_die (dw_die_ref c)
6637 return c && (c->die_tag == DW_TAG_class_type
6638 || c->die_tag == DW_TAG_structure_type);
6641 /* Return non-zero if this DIE is a template parameter. */
6643 static inline bool
6644 is_template_parameter (dw_die_ref die)
6646 switch (die->die_tag)
6648 case DW_TAG_template_type_param:
6649 case DW_TAG_template_value_param:
6650 case DW_TAG_GNU_template_template_param:
6651 case DW_TAG_GNU_template_parameter_pack:
6652 return true;
6653 default:
6654 return false;
6658 /* Return non-zero if this DIE represents a template instantiation. */
6660 static inline bool
6661 is_template_instantiation (dw_die_ref die)
6663 dw_die_ref c;
6665 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6666 return false;
6667 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6668 return false;
6671 static char *
6672 gen_internal_sym (const char *prefix)
6674 char buf[256];
6676 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6677 return xstrdup (buf);
6680 /* Assign symbols to all worthy DIEs under DIE. */
6682 static void
6683 assign_symbol_names (dw_die_ref die)
6685 dw_die_ref c;
6687 if (is_symbol_die (die) && !die->comdat_type_p)
6689 if (comdat_symbol_id)
6691 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6693 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6694 comdat_symbol_id, comdat_symbol_number++);
6695 die->die_id.die_symbol = xstrdup (p);
6697 else
6698 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6701 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6704 struct cu_hash_table_entry
6706 dw_die_ref cu;
6707 unsigned min_comdat_num, max_comdat_num;
6708 struct cu_hash_table_entry *next;
6711 /* Helpers to manipulate hash table of CUs. */
6713 struct cu_hash_table_entry_hasher
6715 typedef cu_hash_table_entry value_type;
6716 typedef die_struct compare_type;
6717 static inline hashval_t hash (const value_type *);
6718 static inline bool equal (const value_type *, const compare_type *);
6719 static inline void remove (value_type *);
6722 inline hashval_t
6723 cu_hash_table_entry_hasher::hash (const value_type *entry)
6725 return htab_hash_string (entry->cu->die_id.die_symbol);
6728 inline bool
6729 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6730 const compare_type *entry2)
6732 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6735 inline void
6736 cu_hash_table_entry_hasher::remove (value_type *entry)
6738 struct cu_hash_table_entry *next;
6740 while (entry)
6742 next = entry->next;
6743 free (entry);
6744 entry = next;
6748 typedef hash_table <cu_hash_table_entry_hasher> cu_hash_type;
6750 /* Check whether we have already seen this CU and set up SYM_NUM
6751 accordingly. */
6752 static int
6753 check_duplicate_cu (dw_die_ref cu, cu_hash_type htable, unsigned int *sym_num)
6755 struct cu_hash_table_entry dummy;
6756 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6758 dummy.max_comdat_num = 0;
6760 slot = htable.find_slot_with_hash (cu,
6761 htab_hash_string (cu->die_id.die_symbol),
6762 INSERT);
6763 entry = *slot;
6765 for (; entry; last = entry, entry = entry->next)
6767 if (same_die_p_wrap (cu, entry->cu))
6768 break;
6771 if (entry)
6773 *sym_num = entry->min_comdat_num;
6774 return 1;
6777 entry = XCNEW (struct cu_hash_table_entry);
6778 entry->cu = cu;
6779 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6780 entry->next = *slot;
6781 *slot = entry;
6783 return 0;
6786 /* Record SYM_NUM to record of CU in HTABLE. */
6787 static void
6788 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type htable,
6789 unsigned int sym_num)
6791 struct cu_hash_table_entry **slot, *entry;
6793 slot = htable.find_slot_with_hash (cu,
6794 htab_hash_string (cu->die_id.die_symbol),
6795 NO_INSERT);
6796 entry = *slot;
6798 entry->max_comdat_num = sym_num;
6801 /* Traverse the DIE (which is always comp_unit_die), and set up
6802 additional compilation units for each of the include files we see
6803 bracketed by BINCL/EINCL. */
6805 static void
6806 break_out_includes (dw_die_ref die)
6808 dw_die_ref c;
6809 dw_die_ref unit = NULL;
6810 limbo_die_node *node, **pnode;
6811 cu_hash_type cu_hash_table;
6813 c = die->die_child;
6814 if (c) do {
6815 dw_die_ref prev = c;
6816 c = c->die_sib;
6817 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6818 || (unit && is_comdat_die (c)))
6820 dw_die_ref next = c->die_sib;
6822 /* This DIE is for a secondary CU; remove it from the main one. */
6823 remove_child_with_prev (c, prev);
6825 if (c->die_tag == DW_TAG_GNU_BINCL)
6826 unit = push_new_compile_unit (unit, c);
6827 else if (c->die_tag == DW_TAG_GNU_EINCL)
6828 unit = pop_compile_unit (unit);
6829 else
6830 add_child_die (unit, c);
6831 c = next;
6832 if (c == die->die_child)
6833 break;
6835 } while (c != die->die_child);
6837 #if 0
6838 /* We can only use this in debugging, since the frontend doesn't check
6839 to make sure that we leave every include file we enter. */
6840 gcc_assert (!unit);
6841 #endif
6843 assign_symbol_names (die);
6844 cu_hash_table.create (10);
6845 for (node = limbo_die_list, pnode = &limbo_die_list;
6846 node;
6847 node = node->next)
6849 int is_dupl;
6851 compute_section_prefix (node->die);
6852 is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6853 &comdat_symbol_number);
6854 assign_symbol_names (node->die);
6855 if (is_dupl)
6856 *pnode = node->next;
6857 else
6859 pnode = &node->next;
6860 record_comdat_symbol_number (node->die, cu_hash_table,
6861 comdat_symbol_number);
6864 cu_hash_table.dispose ();
6867 /* Return non-zero if this DIE is a declaration. */
6869 static int
6870 is_declaration_die (dw_die_ref die)
6872 dw_attr_ref a;
6873 unsigned ix;
6875 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6876 if (a->dw_attr == DW_AT_declaration)
6877 return 1;
6879 return 0;
6882 /* Return non-zero if this DIE is nested inside a subprogram. */
6884 static int
6885 is_nested_in_subprogram (dw_die_ref die)
6887 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6889 if (decl == NULL)
6890 decl = die;
6891 return local_scope_p (decl);
6894 /* Return non-zero if this DIE contains a defining declaration of a
6895 subprogram. */
6897 static int
6898 contains_subprogram_definition (dw_die_ref die)
6900 dw_die_ref c;
6902 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6903 return 1;
6904 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6905 return 0;
6908 /* Return non-zero if this is a type DIE that should be moved to a
6909 COMDAT .debug_types section. */
6911 static int
6912 should_move_die_to_comdat (dw_die_ref die)
6914 switch (die->die_tag)
6916 case DW_TAG_class_type:
6917 case DW_TAG_structure_type:
6918 case DW_TAG_enumeration_type:
6919 case DW_TAG_union_type:
6920 /* Don't move declarations, inlined instances, types nested in a
6921 subprogram, or types that contain subprogram definitions. */
6922 if (is_declaration_die (die)
6923 || get_AT (die, DW_AT_abstract_origin)
6924 || is_nested_in_subprogram (die)
6925 || contains_subprogram_definition (die))
6926 return 0;
6927 return 1;
6928 case DW_TAG_array_type:
6929 case DW_TAG_interface_type:
6930 case DW_TAG_pointer_type:
6931 case DW_TAG_reference_type:
6932 case DW_TAG_rvalue_reference_type:
6933 case DW_TAG_string_type:
6934 case DW_TAG_subroutine_type:
6935 case DW_TAG_ptr_to_member_type:
6936 case DW_TAG_set_type:
6937 case DW_TAG_subrange_type:
6938 case DW_TAG_base_type:
6939 case DW_TAG_const_type:
6940 case DW_TAG_file_type:
6941 case DW_TAG_packed_type:
6942 case DW_TAG_volatile_type:
6943 case DW_TAG_typedef:
6944 default:
6945 return 0;
6949 /* Make a clone of DIE. */
6951 static dw_die_ref
6952 clone_die (dw_die_ref die)
6954 dw_die_ref clone;
6955 dw_attr_ref a;
6956 unsigned ix;
6958 clone = ggc_cleared_alloc<die_node> ();
6959 clone->die_tag = die->die_tag;
6961 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6962 add_dwarf_attr (clone, a);
6964 return clone;
6967 /* Make a clone of the tree rooted at DIE. */
6969 static dw_die_ref
6970 clone_tree (dw_die_ref die)
6972 dw_die_ref c;
6973 dw_die_ref clone = clone_die (die);
6975 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
6977 return clone;
6980 /* Make a clone of DIE as a declaration. */
6982 static dw_die_ref
6983 clone_as_declaration (dw_die_ref die)
6985 dw_die_ref clone;
6986 dw_die_ref decl;
6987 dw_attr_ref a;
6988 unsigned ix;
6990 /* If the DIE is already a declaration, just clone it. */
6991 if (is_declaration_die (die))
6992 return clone_die (die);
6994 /* If the DIE is a specification, just clone its declaration DIE. */
6995 decl = get_AT_ref (die, DW_AT_specification);
6996 if (decl != NULL)
6998 clone = clone_die (decl);
6999 if (die->comdat_type_p)
7000 add_AT_die_ref (clone, DW_AT_signature, die);
7001 return clone;
7004 clone = ggc_cleared_alloc<die_node> ();
7005 clone->die_tag = die->die_tag;
7007 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7009 /* We don't want to copy over all attributes.
7010 For example we don't want DW_AT_byte_size because otherwise we will no
7011 longer have a declaration and GDB will treat it as a definition. */
7013 switch (a->dw_attr)
7015 case DW_AT_abstract_origin:
7016 case DW_AT_artificial:
7017 case DW_AT_containing_type:
7018 case DW_AT_external:
7019 case DW_AT_name:
7020 case DW_AT_type:
7021 case DW_AT_virtuality:
7022 case DW_AT_linkage_name:
7023 case DW_AT_MIPS_linkage_name:
7024 add_dwarf_attr (clone, a);
7025 break;
7026 case DW_AT_byte_size:
7027 default:
7028 break;
7032 if (die->comdat_type_p)
7033 add_AT_die_ref (clone, DW_AT_signature, die);
7035 add_AT_flag (clone, DW_AT_declaration, 1);
7036 return clone;
7040 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7042 struct decl_table_entry
7044 dw_die_ref orig;
7045 dw_die_ref copy;
7048 /* Helpers to manipulate hash table of copied declarations. */
7050 /* Hashtable helpers. */
7052 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7054 typedef decl_table_entry value_type;
7055 typedef die_struct compare_type;
7056 static inline hashval_t hash (const value_type *);
7057 static inline bool equal (const value_type *, const compare_type *);
7060 inline hashval_t
7061 decl_table_entry_hasher::hash (const value_type *entry)
7063 return htab_hash_pointer (entry->orig);
7066 inline bool
7067 decl_table_entry_hasher::equal (const value_type *entry1,
7068 const compare_type *entry2)
7070 return entry1->orig == entry2;
7073 typedef hash_table <decl_table_entry_hasher> decl_hash_type;
7075 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7076 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7077 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7078 to check if the ancestor has already been copied into UNIT. */
7080 static dw_die_ref
7081 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7083 dw_die_ref parent = die->die_parent;
7084 dw_die_ref new_parent = unit;
7085 dw_die_ref copy;
7086 decl_table_entry **slot = NULL;
7087 struct decl_table_entry *entry = NULL;
7089 if (decl_table.is_created ())
7091 /* Check if the entry has already been copied to UNIT. */
7092 slot = decl_table.find_slot_with_hash (die, htab_hash_pointer (die),
7093 INSERT);
7094 if (*slot != HTAB_EMPTY_ENTRY)
7096 entry = *slot;
7097 return entry->copy;
7100 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7101 entry = XCNEW (struct decl_table_entry);
7102 entry->orig = die;
7103 entry->copy = NULL;
7104 *slot = entry;
7107 if (parent != NULL)
7109 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7110 if (spec != NULL)
7111 parent = spec;
7112 if (!is_unit_die (parent))
7113 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7116 copy = clone_as_declaration (die);
7117 add_child_die (new_parent, copy);
7119 if (decl_table.is_created ())
7121 /* Record the pointer to the copy. */
7122 entry->copy = copy;
7125 return copy;
7127 /* Copy the declaration context to the new type unit DIE. This includes
7128 any surrounding namespace or type declarations. If the DIE has an
7129 AT_specification attribute, it also includes attributes and children
7130 attached to the specification, and returns a pointer to the original
7131 parent of the declaration DIE. Returns NULL otherwise. */
7133 static dw_die_ref
7134 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7136 dw_die_ref decl;
7137 dw_die_ref new_decl;
7138 dw_die_ref orig_parent = NULL;
7140 decl = get_AT_ref (die, DW_AT_specification);
7141 if (decl == NULL)
7142 decl = die;
7143 else
7145 unsigned ix;
7146 dw_die_ref c;
7147 dw_attr_ref a;
7149 /* The original DIE will be changed to a declaration, and must
7150 be moved to be a child of the original declaration DIE. */
7151 orig_parent = decl->die_parent;
7153 /* Copy the type node pointer from the new DIE to the original
7154 declaration DIE so we can forward references later. */
7155 decl->comdat_type_p = true;
7156 decl->die_id.die_type_node = die->die_id.die_type_node;
7158 remove_AT (die, DW_AT_specification);
7160 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7162 if (a->dw_attr != DW_AT_name
7163 && a->dw_attr != DW_AT_declaration
7164 && a->dw_attr != DW_AT_external)
7165 add_dwarf_attr (die, a);
7168 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7171 if (decl->die_parent != NULL
7172 && !is_unit_die (decl->die_parent))
7174 new_decl = copy_ancestor_tree (unit, decl, decl_hash_type ());
7175 if (new_decl != NULL)
7177 remove_AT (new_decl, DW_AT_signature);
7178 add_AT_specification (die, new_decl);
7182 return orig_parent;
7185 /* Generate the skeleton ancestor tree for the given NODE, then clone
7186 the DIE and add the clone into the tree. */
7188 static void
7189 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7191 if (node->new_die != NULL)
7192 return;
7194 node->new_die = clone_as_declaration (node->old_die);
7196 if (node->parent != NULL)
7198 generate_skeleton_ancestor_tree (node->parent);
7199 add_child_die (node->parent->new_die, node->new_die);
7203 /* Generate a skeleton tree of DIEs containing any declarations that are
7204 found in the original tree. We traverse the tree looking for declaration
7205 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7207 static void
7208 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7210 skeleton_chain_node node;
7211 dw_die_ref c;
7212 dw_die_ref first;
7213 dw_die_ref prev = NULL;
7214 dw_die_ref next = NULL;
7216 node.parent = parent;
7218 first = c = parent->old_die->die_child;
7219 if (c)
7220 next = c->die_sib;
7221 if (c) do {
7222 if (prev == NULL || prev->die_sib == c)
7223 prev = c;
7224 c = next;
7225 next = (c == first ? NULL : c->die_sib);
7226 node.old_die = c;
7227 node.new_die = NULL;
7228 if (is_declaration_die (c))
7230 if (is_template_instantiation (c))
7232 /* Instantiated templates do not need to be cloned into the
7233 type unit. Just move the DIE and its children back to
7234 the skeleton tree (in the main CU). */
7235 remove_child_with_prev (c, prev);
7236 add_child_die (parent->new_die, c);
7237 c = prev;
7239 else
7241 /* Clone the existing DIE, move the original to the skeleton
7242 tree (which is in the main CU), and put the clone, with
7243 all the original's children, where the original came from
7244 (which is about to be moved to the type unit). */
7245 dw_die_ref clone = clone_die (c);
7246 move_all_children (c, clone);
7248 /* If the original has a DW_AT_object_pointer attribute,
7249 it would now point to a child DIE just moved to the
7250 cloned tree, so we need to remove that attribute from
7251 the original. */
7252 remove_AT (c, DW_AT_object_pointer);
7254 replace_child (c, clone, prev);
7255 generate_skeleton_ancestor_tree (parent);
7256 add_child_die (parent->new_die, c);
7257 node.new_die = c;
7258 c = clone;
7261 generate_skeleton_bottom_up (&node);
7262 } while (next != NULL);
7265 /* Wrapper function for generate_skeleton_bottom_up. */
7267 static dw_die_ref
7268 generate_skeleton (dw_die_ref die)
7270 skeleton_chain_node node;
7272 node.old_die = die;
7273 node.new_die = NULL;
7274 node.parent = NULL;
7276 /* If this type definition is nested inside another type,
7277 and is not an instantiation of a template, always leave
7278 at least a declaration in its place. */
7279 if (die->die_parent != NULL
7280 && is_type_die (die->die_parent)
7281 && !is_template_instantiation (die))
7282 node.new_die = clone_as_declaration (die);
7284 generate_skeleton_bottom_up (&node);
7285 return node.new_die;
7288 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7289 declaration. The original DIE is moved to a new compile unit so that
7290 existing references to it follow it to the new location. If any of the
7291 original DIE's descendants is a declaration, we need to replace the
7292 original DIE with a skeleton tree and move the declarations back into the
7293 skeleton tree. */
7295 static dw_die_ref
7296 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7297 dw_die_ref prev)
7299 dw_die_ref skeleton, orig_parent;
7301 /* Copy the declaration context to the type unit DIE. If the returned
7302 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7303 that DIE. */
7304 orig_parent = copy_declaration_context (unit, child);
7306 skeleton = generate_skeleton (child);
7307 if (skeleton == NULL)
7308 remove_child_with_prev (child, prev);
7309 else
7311 skeleton->comdat_type_p = true;
7312 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7314 /* If the original DIE was a specification, we need to put
7315 the skeleton under the parent DIE of the declaration.
7316 This leaves the original declaration in the tree, but
7317 it will be pruned later since there are no longer any
7318 references to it. */
7319 if (orig_parent != NULL)
7321 remove_child_with_prev (child, prev);
7322 add_child_die (orig_parent, skeleton);
7324 else
7325 replace_child (child, skeleton, prev);
7328 return skeleton;
7331 /* Traverse the DIE and set up additional .debug_types sections for each
7332 type worthy of being placed in a COMDAT section. */
7334 static void
7335 break_out_comdat_types (dw_die_ref die)
7337 dw_die_ref c;
7338 dw_die_ref first;
7339 dw_die_ref prev = NULL;
7340 dw_die_ref next = NULL;
7341 dw_die_ref unit = NULL;
7343 first = c = die->die_child;
7344 if (c)
7345 next = c->die_sib;
7346 if (c) do {
7347 if (prev == NULL || prev->die_sib == c)
7348 prev = c;
7349 c = next;
7350 next = (c == first ? NULL : c->die_sib);
7351 if (should_move_die_to_comdat (c))
7353 dw_die_ref replacement;
7354 comdat_type_node_ref type_node;
7356 /* Break out nested types into their own type units. */
7357 break_out_comdat_types (c);
7359 /* Create a new type unit DIE as the root for the new tree, and
7360 add it to the list of comdat types. */
7361 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7362 add_AT_unsigned (unit, DW_AT_language,
7363 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7364 type_node = ggc_cleared_alloc<comdat_type_node> ();
7365 type_node->root_die = unit;
7366 type_node->next = comdat_type_list;
7367 comdat_type_list = type_node;
7369 /* Generate the type signature. */
7370 generate_type_signature (c, type_node);
7372 /* Copy the declaration context, attributes, and children of the
7373 declaration into the new type unit DIE, then remove this DIE
7374 from the main CU (or replace it with a skeleton if necessary). */
7375 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7376 type_node->skeleton_die = replacement;
7378 /* Add the DIE to the new compunit. */
7379 add_child_die (unit, c);
7381 if (replacement != NULL)
7382 c = replacement;
7384 else if (c->die_tag == DW_TAG_namespace
7385 || c->die_tag == DW_TAG_class_type
7386 || c->die_tag == DW_TAG_structure_type
7387 || c->die_tag == DW_TAG_union_type)
7389 /* Look for nested types that can be broken out. */
7390 break_out_comdat_types (c);
7392 } while (next != NULL);
7395 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7396 Enter all the cloned children into the hash table decl_table. */
7398 static dw_die_ref
7399 clone_tree_partial (dw_die_ref die, decl_hash_type decl_table)
7401 dw_die_ref c;
7402 dw_die_ref clone;
7403 struct decl_table_entry *entry;
7404 decl_table_entry **slot;
7406 if (die->die_tag == DW_TAG_subprogram)
7407 clone = clone_as_declaration (die);
7408 else
7409 clone = clone_die (die);
7411 slot = decl_table.find_slot_with_hash (die,
7412 htab_hash_pointer (die), INSERT);
7414 /* Assert that DIE isn't in the hash table yet. If it would be there
7415 before, the ancestors would be necessarily there as well, therefore
7416 clone_tree_partial wouldn't be called. */
7417 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7419 entry = XCNEW (struct decl_table_entry);
7420 entry->orig = die;
7421 entry->copy = clone;
7422 *slot = entry;
7424 if (die->die_tag != DW_TAG_subprogram)
7425 FOR_EACH_CHILD (die, c,
7426 add_child_die (clone, clone_tree_partial (c, decl_table)));
7428 return clone;
7431 /* Walk the DIE and its children, looking for references to incomplete
7432 or trivial types that are unmarked (i.e., that are not in the current
7433 type_unit). */
7435 static void
7436 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type decl_table)
7438 dw_die_ref c;
7439 dw_attr_ref a;
7440 unsigned ix;
7442 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7444 if (AT_class (a) == dw_val_class_die_ref)
7446 dw_die_ref targ = AT_ref (a);
7447 decl_table_entry **slot;
7448 struct decl_table_entry *entry;
7450 if (targ->die_mark != 0 || targ->comdat_type_p)
7451 continue;
7453 slot = decl_table.find_slot_with_hash (targ, htab_hash_pointer (targ),
7454 INSERT);
7456 if (*slot != HTAB_EMPTY_ENTRY)
7458 /* TARG has already been copied, so we just need to
7459 modify the reference to point to the copy. */
7460 entry = *slot;
7461 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7463 else
7465 dw_die_ref parent = unit;
7466 dw_die_ref copy = clone_die (targ);
7468 /* Record in DECL_TABLE that TARG has been copied.
7469 Need to do this now, before the recursive call,
7470 because DECL_TABLE may be expanded and SLOT
7471 would no longer be a valid pointer. */
7472 entry = XCNEW (struct decl_table_entry);
7473 entry->orig = targ;
7474 entry->copy = copy;
7475 *slot = entry;
7477 /* If TARG is not a declaration DIE, we need to copy its
7478 children. */
7479 if (!is_declaration_die (targ))
7481 FOR_EACH_CHILD (
7482 targ, c,
7483 add_child_die (copy,
7484 clone_tree_partial (c, decl_table)));
7487 /* Make sure the cloned tree is marked as part of the
7488 type unit. */
7489 mark_dies (copy);
7491 /* If TARG has surrounding context, copy its ancestor tree
7492 into the new type unit. */
7493 if (targ->die_parent != NULL
7494 && !is_unit_die (targ->die_parent))
7495 parent = copy_ancestor_tree (unit, targ->die_parent,
7496 decl_table);
7498 add_child_die (parent, copy);
7499 a->dw_attr_val.v.val_die_ref.die = copy;
7501 /* Make sure the newly-copied DIE is walked. If it was
7502 installed in a previously-added context, it won't
7503 get visited otherwise. */
7504 if (parent != unit)
7506 /* Find the highest point of the newly-added tree,
7507 mark each node along the way, and walk from there. */
7508 parent->die_mark = 1;
7509 while (parent->die_parent
7510 && parent->die_parent->die_mark == 0)
7512 parent = parent->die_parent;
7513 parent->die_mark = 1;
7515 copy_decls_walk (unit, parent, decl_table);
7521 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7524 /* Copy declarations for "unworthy" types into the new comdat section.
7525 Incomplete types, modified types, and certain other types aren't broken
7526 out into comdat sections of their own, so they don't have a signature,
7527 and we need to copy the declaration into the same section so that we
7528 don't have an external reference. */
7530 static void
7531 copy_decls_for_unworthy_types (dw_die_ref unit)
7533 decl_hash_type decl_table;
7535 mark_dies (unit);
7536 decl_table.create (10);
7537 copy_decls_walk (unit, unit, decl_table);
7538 decl_table.dispose ();
7539 unmark_dies (unit);
7542 /* Traverse the DIE and add a sibling attribute if it may have the
7543 effect of speeding up access to siblings. To save some space,
7544 avoid generating sibling attributes for DIE's without children. */
7546 static void
7547 add_sibling_attributes (dw_die_ref die)
7549 dw_die_ref c;
7551 if (! die->die_child)
7552 return;
7554 if (die->die_parent && die != die->die_parent->die_child)
7555 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7557 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7560 /* Output all location lists for the DIE and its children. */
7562 static void
7563 output_location_lists (dw_die_ref die)
7565 dw_die_ref c;
7566 dw_attr_ref a;
7567 unsigned ix;
7569 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7570 if (AT_class (a) == dw_val_class_loc_list)
7571 output_loc_list (AT_loc_list (a));
7573 FOR_EACH_CHILD (die, c, output_location_lists (c));
7576 /* We want to limit the number of external references, because they are
7577 larger than local references: a relocation takes multiple words, and
7578 even a sig8 reference is always eight bytes, whereas a local reference
7579 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7580 So if we encounter multiple external references to the same type DIE, we
7581 make a local typedef stub for it and redirect all references there.
7583 This is the element of the hash table for keeping track of these
7584 references. */
7586 struct external_ref
7588 dw_die_ref type;
7589 dw_die_ref stub;
7590 unsigned n_refs;
7593 /* Hashtable helpers. */
7595 struct external_ref_hasher : typed_free_remove <external_ref>
7597 typedef external_ref value_type;
7598 typedef external_ref compare_type;
7599 static inline hashval_t hash (const value_type *);
7600 static inline bool equal (const value_type *, const compare_type *);
7603 inline hashval_t
7604 external_ref_hasher::hash (const value_type *r)
7606 dw_die_ref die = r->type;
7607 hashval_t h = 0;
7609 /* We can't use the address of the DIE for hashing, because
7610 that will make the order of the stub DIEs non-deterministic. */
7611 if (! die->comdat_type_p)
7612 /* We have a symbol; use it to compute a hash. */
7613 h = htab_hash_string (die->die_id.die_symbol);
7614 else
7616 /* We have a type signature; use a subset of the bits as the hash.
7617 The 8-byte signature is at least as large as hashval_t. */
7618 comdat_type_node_ref type_node = die->die_id.die_type_node;
7619 memcpy (&h, type_node->signature, sizeof (h));
7621 return h;
7624 inline bool
7625 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7627 return r1->type == r2->type;
7630 typedef hash_table <external_ref_hasher> external_ref_hash_type;
7632 /* Return a pointer to the external_ref for references to DIE. */
7634 static struct external_ref *
7635 lookup_external_ref (external_ref_hash_type map, dw_die_ref die)
7637 struct external_ref ref, *ref_p;
7638 external_ref **slot;
7640 ref.type = die;
7641 slot = map.find_slot (&ref, INSERT);
7642 if (*slot != HTAB_EMPTY_ENTRY)
7643 return *slot;
7645 ref_p = XCNEW (struct external_ref);
7646 ref_p->type = die;
7647 *slot = ref_p;
7648 return ref_p;
7651 /* Subroutine of optimize_external_refs, below.
7653 If we see a type skeleton, record it as our stub. If we see external
7654 references, remember how many we've seen. */
7656 static void
7657 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type map)
7659 dw_die_ref c;
7660 dw_attr_ref a;
7661 unsigned ix;
7662 struct external_ref *ref_p;
7664 if (is_type_die (die)
7665 && (c = get_AT_ref (die, DW_AT_signature)))
7667 /* This is a local skeleton; use it for local references. */
7668 ref_p = lookup_external_ref (map, c);
7669 ref_p->stub = die;
7672 /* Scan the DIE references, and remember any that refer to DIEs from
7673 other CUs (i.e. those which are not marked). */
7674 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7675 if (AT_class (a) == dw_val_class_die_ref
7676 && (c = AT_ref (a))->die_mark == 0
7677 && is_type_die (c))
7679 ref_p = lookup_external_ref (map, c);
7680 ref_p->n_refs++;
7683 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7686 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7687 points to an external_ref, DATA is the CU we're processing. If we don't
7688 already have a local stub, and we have multiple refs, build a stub. */
7691 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7693 struct external_ref *ref_p = *slot;
7695 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7697 /* We have multiple references to this type, so build a small stub.
7698 Both of these forms are a bit dodgy from the perspective of the
7699 DWARF standard, since technically they should have names. */
7700 dw_die_ref cu = data;
7701 dw_die_ref type = ref_p->type;
7702 dw_die_ref stub = NULL;
7704 if (type->comdat_type_p)
7706 /* If we refer to this type via sig8, use AT_signature. */
7707 stub = new_die (type->die_tag, cu, NULL_TREE);
7708 add_AT_die_ref (stub, DW_AT_signature, type);
7710 else
7712 /* Otherwise, use a typedef with no name. */
7713 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7714 add_AT_die_ref (stub, DW_AT_type, type);
7717 stub->die_mark++;
7718 ref_p->stub = stub;
7720 return 1;
7723 /* DIE is a unit; look through all the DIE references to see if there are
7724 any external references to types, and if so, create local stubs for
7725 them which will be applied in build_abbrev_table. This is useful because
7726 references to local DIEs are smaller. */
7728 static external_ref_hash_type
7729 optimize_external_refs (dw_die_ref die)
7731 external_ref_hash_type map;
7732 map.create (10);
7733 optimize_external_refs_1 (die, map);
7734 map.traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7735 return map;
7738 /* The format of each DIE (and its attribute value pairs) is encoded in an
7739 abbreviation table. This routine builds the abbreviation table and assigns
7740 a unique abbreviation id for each abbreviation entry. The children of each
7741 die are visited recursively. */
7743 static void
7744 build_abbrev_table (dw_die_ref die, external_ref_hash_type extern_map)
7746 unsigned long abbrev_id;
7747 unsigned int n_alloc;
7748 dw_die_ref c;
7749 dw_attr_ref a;
7750 unsigned ix;
7752 /* Scan the DIE references, and replace any that refer to
7753 DIEs from other CUs (i.e. those which are not marked) with
7754 the local stubs we built in optimize_external_refs. */
7755 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7756 if (AT_class (a) == dw_val_class_die_ref
7757 && (c = AT_ref (a))->die_mark == 0)
7759 struct external_ref *ref_p;
7760 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7762 ref_p = lookup_external_ref (extern_map, c);
7763 if (ref_p->stub && ref_p->stub != die)
7764 change_AT_die_ref (a, ref_p->stub);
7765 else
7766 /* We aren't changing this reference, so mark it external. */
7767 set_AT_ref_external (a, 1);
7770 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7772 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7773 dw_attr_ref die_a, abbrev_a;
7774 unsigned ix;
7775 bool ok = true;
7777 if (abbrev->die_tag != die->die_tag)
7778 continue;
7779 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7780 continue;
7782 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7783 continue;
7785 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7787 abbrev_a = &(*abbrev->die_attr)[ix];
7788 if ((abbrev_a->dw_attr != die_a->dw_attr)
7789 || (value_format (abbrev_a) != value_format (die_a)))
7791 ok = false;
7792 break;
7795 if (ok)
7796 break;
7799 if (abbrev_id >= abbrev_die_table_in_use)
7801 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7803 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7804 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7805 n_alloc);
7807 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7808 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7809 abbrev_die_table_allocated = n_alloc;
7812 ++abbrev_die_table_in_use;
7813 abbrev_die_table[abbrev_id] = die;
7816 die->die_abbrev = abbrev_id;
7817 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7820 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7822 static int
7823 constant_size (unsigned HOST_WIDE_INT value)
7825 int log;
7827 if (value == 0)
7828 log = 0;
7829 else
7830 log = floor_log2 (value);
7832 log = log / 8;
7833 log = 1 << (floor_log2 (log) + 1);
7835 return log;
7838 /* Return the size of a DIE as it is represented in the
7839 .debug_info section. */
7841 static unsigned long
7842 size_of_die (dw_die_ref die)
7844 unsigned long size = 0;
7845 dw_attr_ref a;
7846 unsigned ix;
7847 enum dwarf_form form;
7849 size += size_of_uleb128 (die->die_abbrev);
7850 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7852 switch (AT_class (a))
7854 case dw_val_class_addr:
7855 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7857 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7858 size += size_of_uleb128 (AT_index (a));
7860 else
7861 size += DWARF2_ADDR_SIZE;
7862 break;
7863 case dw_val_class_offset:
7864 size += DWARF_OFFSET_SIZE;
7865 break;
7866 case dw_val_class_loc:
7868 unsigned long lsize = size_of_locs (AT_loc (a));
7870 /* Block length. */
7871 if (dwarf_version >= 4)
7872 size += size_of_uleb128 (lsize);
7873 else
7874 size += constant_size (lsize);
7875 size += lsize;
7877 break;
7878 case dw_val_class_loc_list:
7879 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7881 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7882 size += size_of_uleb128 (AT_index (a));
7884 else
7885 size += DWARF_OFFSET_SIZE;
7886 break;
7887 case dw_val_class_range_list:
7888 size += DWARF_OFFSET_SIZE;
7889 break;
7890 case dw_val_class_const:
7891 size += size_of_sleb128 (AT_int (a));
7892 break;
7893 case dw_val_class_unsigned_const:
7895 int csize = constant_size (AT_unsigned (a));
7896 if (dwarf_version == 3
7897 && a->dw_attr == DW_AT_data_member_location
7898 && csize >= 4)
7899 size += size_of_uleb128 (AT_unsigned (a));
7900 else
7901 size += csize;
7903 break;
7904 case dw_val_class_const_double:
7905 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7906 if (HOST_BITS_PER_WIDE_INT >= 64)
7907 size++; /* block */
7908 break;
7909 case dw_val_class_wide_int:
7910 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7911 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7912 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7913 > 64)
7914 size++; /* block */
7915 break;
7916 case dw_val_class_vec:
7917 size += constant_size (a->dw_attr_val.v.val_vec.length
7918 * a->dw_attr_val.v.val_vec.elt_size)
7919 + a->dw_attr_val.v.val_vec.length
7920 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7921 break;
7922 case dw_val_class_flag:
7923 if (dwarf_version >= 4)
7924 /* Currently all add_AT_flag calls pass in 1 as last argument,
7925 so DW_FORM_flag_present can be used. If that ever changes,
7926 we'll need to use DW_FORM_flag and have some optimization
7927 in build_abbrev_table that will change those to
7928 DW_FORM_flag_present if it is set to 1 in all DIEs using
7929 the same abbrev entry. */
7930 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7931 else
7932 size += 1;
7933 break;
7934 case dw_val_class_die_ref:
7935 if (AT_ref_external (a))
7937 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7938 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7939 is sized by target address length, whereas in DWARF3
7940 it's always sized as an offset. */
7941 if (use_debug_types)
7942 size += DWARF_TYPE_SIGNATURE_SIZE;
7943 else if (dwarf_version == 2)
7944 size += DWARF2_ADDR_SIZE;
7945 else
7946 size += DWARF_OFFSET_SIZE;
7948 else
7949 size += DWARF_OFFSET_SIZE;
7950 break;
7951 case dw_val_class_fde_ref:
7952 size += DWARF_OFFSET_SIZE;
7953 break;
7954 case dw_val_class_lbl_id:
7955 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7957 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7958 size += size_of_uleb128 (AT_index (a));
7960 else
7961 size += DWARF2_ADDR_SIZE;
7962 break;
7963 case dw_val_class_lineptr:
7964 case dw_val_class_macptr:
7965 size += DWARF_OFFSET_SIZE;
7966 break;
7967 case dw_val_class_str:
7968 form = AT_string_form (a);
7969 if (form == DW_FORM_strp)
7970 size += DWARF_OFFSET_SIZE;
7971 else if (form == DW_FORM_GNU_str_index)
7972 size += size_of_uleb128 (AT_index (a));
7973 else
7974 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7975 break;
7976 case dw_val_class_file:
7977 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7978 break;
7979 case dw_val_class_data8:
7980 size += 8;
7981 break;
7982 case dw_val_class_vms_delta:
7983 size += DWARF_OFFSET_SIZE;
7984 break;
7985 case dw_val_class_high_pc:
7986 size += DWARF2_ADDR_SIZE;
7987 break;
7988 default:
7989 gcc_unreachable ();
7993 return size;
7996 /* Size the debugging information associated with a given DIE. Visits the
7997 DIE's children recursively. Updates the global variable next_die_offset, on
7998 each time through. Uses the current value of next_die_offset to update the
7999 die_offset field in each DIE. */
8001 static void
8002 calc_die_sizes (dw_die_ref die)
8004 dw_die_ref c;
8006 gcc_assert (die->die_offset == 0
8007 || (unsigned long int) die->die_offset == next_die_offset);
8008 die->die_offset = next_die_offset;
8009 next_die_offset += size_of_die (die);
8011 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8013 if (die->die_child != NULL)
8014 /* Count the null byte used to terminate sibling lists. */
8015 next_die_offset += 1;
8018 /* Size just the base type children at the start of the CU.
8019 This is needed because build_abbrev needs to size locs
8020 and sizing of type based stack ops needs to know die_offset
8021 values for the base types. */
8023 static void
8024 calc_base_type_die_sizes (void)
8026 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8027 unsigned int i;
8028 dw_die_ref base_type;
8029 #if ENABLE_ASSERT_CHECKING
8030 dw_die_ref prev = comp_unit_die ()->die_child;
8031 #endif
8033 die_offset += size_of_die (comp_unit_die ());
8034 for (i = 0; base_types.iterate (i, &base_type); i++)
8036 #if ENABLE_ASSERT_CHECKING
8037 gcc_assert (base_type->die_offset == 0
8038 && prev->die_sib == base_type
8039 && base_type->die_child == NULL
8040 && base_type->die_abbrev);
8041 prev = base_type;
8042 #endif
8043 base_type->die_offset = die_offset;
8044 die_offset += size_of_die (base_type);
8048 /* Set the marks for a die and its children. We do this so
8049 that we know whether or not a reference needs to use FORM_ref_addr; only
8050 DIEs in the same CU will be marked. We used to clear out the offset
8051 and use that as the flag, but ran into ordering problems. */
8053 static void
8054 mark_dies (dw_die_ref die)
8056 dw_die_ref c;
8058 gcc_assert (!die->die_mark);
8060 die->die_mark = 1;
8061 FOR_EACH_CHILD (die, c, mark_dies (c));
8064 /* Clear the marks for a die and its children. */
8066 static void
8067 unmark_dies (dw_die_ref die)
8069 dw_die_ref c;
8071 if (! use_debug_types)
8072 gcc_assert (die->die_mark);
8074 die->die_mark = 0;
8075 FOR_EACH_CHILD (die, c, unmark_dies (c));
8078 /* Clear the marks for a die, its children and referred dies. */
8080 static void
8081 unmark_all_dies (dw_die_ref die)
8083 dw_die_ref c;
8084 dw_attr_ref a;
8085 unsigned ix;
8087 if (!die->die_mark)
8088 return;
8089 die->die_mark = 0;
8091 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8093 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8094 if (AT_class (a) == dw_val_class_die_ref)
8095 unmark_all_dies (AT_ref (a));
8098 /* Calculate if the entry should appear in the final output file. It may be
8099 from a pruned a type. */
8101 static bool
8102 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8104 /* By limiting gnu pubnames to definitions only, gold can generate a
8105 gdb index without entries for declarations, which don't include
8106 enough information to be useful. */
8107 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8108 return false;
8110 if (table == pubname_table)
8112 /* Enumerator names are part of the pubname table, but the
8113 parent DW_TAG_enumeration_type die may have been pruned.
8114 Don't output them if that is the case. */
8115 if (p->die->die_tag == DW_TAG_enumerator &&
8116 (p->die->die_parent == NULL
8117 || !p->die->die_parent->die_perennial_p))
8118 return false;
8120 /* Everything else in the pubname table is included. */
8121 return true;
8124 /* The pubtypes table shouldn't include types that have been
8125 pruned. */
8126 return (p->die->die_offset != 0
8127 || !flag_eliminate_unused_debug_types);
8130 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8131 generated for the compilation unit. */
8133 static unsigned long
8134 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8136 unsigned long size;
8137 unsigned i;
8138 pubname_ref p;
8139 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8141 size = DWARF_PUBNAMES_HEADER_SIZE;
8142 FOR_EACH_VEC_ELT (*names, i, p)
8143 if (include_pubname_in_output (names, p))
8144 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8146 size += DWARF_OFFSET_SIZE;
8147 return size;
8150 /* Return the size of the information in the .debug_aranges section. */
8152 static unsigned long
8153 size_of_aranges (void)
8155 unsigned long size;
8157 size = DWARF_ARANGES_HEADER_SIZE;
8159 /* Count the address/length pair for this compilation unit. */
8160 if (text_section_used)
8161 size += 2 * DWARF2_ADDR_SIZE;
8162 if (cold_text_section_used)
8163 size += 2 * DWARF2_ADDR_SIZE;
8164 if (have_multiple_function_sections)
8166 unsigned fde_idx;
8167 dw_fde_ref fde;
8169 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8171 if (DECL_IGNORED_P (fde->decl))
8172 continue;
8173 if (!fde->in_std_section)
8174 size += 2 * DWARF2_ADDR_SIZE;
8175 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8176 size += 2 * DWARF2_ADDR_SIZE;
8180 /* Count the two zero words used to terminated the address range table. */
8181 size += 2 * DWARF2_ADDR_SIZE;
8182 return size;
8185 /* Select the encoding of an attribute value. */
8187 static enum dwarf_form
8188 value_format (dw_attr_ref a)
8190 switch (AT_class (a))
8192 case dw_val_class_addr:
8193 /* Only very few attributes allow DW_FORM_addr. */
8194 switch (a->dw_attr)
8196 case DW_AT_low_pc:
8197 case DW_AT_high_pc:
8198 case DW_AT_entry_pc:
8199 case DW_AT_trampoline:
8200 return (AT_index (a) == NOT_INDEXED
8201 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8202 default:
8203 break;
8205 switch (DWARF2_ADDR_SIZE)
8207 case 1:
8208 return DW_FORM_data1;
8209 case 2:
8210 return DW_FORM_data2;
8211 case 4:
8212 return DW_FORM_data4;
8213 case 8:
8214 return DW_FORM_data8;
8215 default:
8216 gcc_unreachable ();
8218 case dw_val_class_range_list:
8219 case dw_val_class_loc_list:
8220 if (dwarf_version >= 4)
8221 return DW_FORM_sec_offset;
8222 /* FALLTHRU */
8223 case dw_val_class_vms_delta:
8224 case dw_val_class_offset:
8225 switch (DWARF_OFFSET_SIZE)
8227 case 4:
8228 return DW_FORM_data4;
8229 case 8:
8230 return DW_FORM_data8;
8231 default:
8232 gcc_unreachable ();
8234 case dw_val_class_loc:
8235 if (dwarf_version >= 4)
8236 return DW_FORM_exprloc;
8237 switch (constant_size (size_of_locs (AT_loc (a))))
8239 case 1:
8240 return DW_FORM_block1;
8241 case 2:
8242 return DW_FORM_block2;
8243 case 4:
8244 return DW_FORM_block4;
8245 default:
8246 gcc_unreachable ();
8248 case dw_val_class_const:
8249 return DW_FORM_sdata;
8250 case dw_val_class_unsigned_const:
8251 switch (constant_size (AT_unsigned (a)))
8253 case 1:
8254 return DW_FORM_data1;
8255 case 2:
8256 return DW_FORM_data2;
8257 case 4:
8258 /* In DWARF3 DW_AT_data_member_location with
8259 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8260 constant, so we need to use DW_FORM_udata if we need
8261 a large constant. */
8262 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8263 return DW_FORM_udata;
8264 return DW_FORM_data4;
8265 case 8:
8266 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8267 return DW_FORM_udata;
8268 return DW_FORM_data8;
8269 default:
8270 gcc_unreachable ();
8272 case dw_val_class_const_double:
8273 switch (HOST_BITS_PER_WIDE_INT)
8275 case 8:
8276 return DW_FORM_data2;
8277 case 16:
8278 return DW_FORM_data4;
8279 case 32:
8280 return DW_FORM_data8;
8281 case 64:
8282 default:
8283 return DW_FORM_block1;
8285 case dw_val_class_wide_int:
8286 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8288 case 8:
8289 return DW_FORM_data1;
8290 case 16:
8291 return DW_FORM_data2;
8292 case 32:
8293 return DW_FORM_data4;
8294 case 64:
8295 return DW_FORM_data8;
8296 default:
8297 return DW_FORM_block1;
8299 case dw_val_class_vec:
8300 switch (constant_size (a->dw_attr_val.v.val_vec.length
8301 * a->dw_attr_val.v.val_vec.elt_size))
8303 case 1:
8304 return DW_FORM_block1;
8305 case 2:
8306 return DW_FORM_block2;
8307 case 4:
8308 return DW_FORM_block4;
8309 default:
8310 gcc_unreachable ();
8312 case dw_val_class_flag:
8313 if (dwarf_version >= 4)
8315 /* Currently all add_AT_flag calls pass in 1 as last argument,
8316 so DW_FORM_flag_present can be used. If that ever changes,
8317 we'll need to use DW_FORM_flag and have some optimization
8318 in build_abbrev_table that will change those to
8319 DW_FORM_flag_present if it is set to 1 in all DIEs using
8320 the same abbrev entry. */
8321 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8322 return DW_FORM_flag_present;
8324 return DW_FORM_flag;
8325 case dw_val_class_die_ref:
8326 if (AT_ref_external (a))
8327 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8328 else
8329 return DW_FORM_ref;
8330 case dw_val_class_fde_ref:
8331 return DW_FORM_data;
8332 case dw_val_class_lbl_id:
8333 return (AT_index (a) == NOT_INDEXED
8334 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8335 case dw_val_class_lineptr:
8336 case dw_val_class_macptr:
8337 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8338 case dw_val_class_str:
8339 return AT_string_form (a);
8340 case dw_val_class_file:
8341 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8343 case 1:
8344 return DW_FORM_data1;
8345 case 2:
8346 return DW_FORM_data2;
8347 case 4:
8348 return DW_FORM_data4;
8349 default:
8350 gcc_unreachable ();
8353 case dw_val_class_data8:
8354 return DW_FORM_data8;
8356 case dw_val_class_high_pc:
8357 switch (DWARF2_ADDR_SIZE)
8359 case 1:
8360 return DW_FORM_data1;
8361 case 2:
8362 return DW_FORM_data2;
8363 case 4:
8364 return DW_FORM_data4;
8365 case 8:
8366 return DW_FORM_data8;
8367 default:
8368 gcc_unreachable ();
8371 default:
8372 gcc_unreachable ();
8376 /* Output the encoding of an attribute value. */
8378 static void
8379 output_value_format (dw_attr_ref a)
8381 enum dwarf_form form = value_format (a);
8383 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8386 /* Given a die and id, produce the appropriate abbreviations. */
8388 static void
8389 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8391 unsigned ix;
8392 dw_attr_ref a_attr;
8394 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8395 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8396 dwarf_tag_name (abbrev->die_tag));
8398 if (abbrev->die_child != NULL)
8399 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8400 else
8401 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8403 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8405 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8406 dwarf_attr_name (a_attr->dw_attr));
8407 output_value_format (a_attr);
8410 dw2_asm_output_data (1, 0, NULL);
8411 dw2_asm_output_data (1, 0, NULL);
8415 /* Output the .debug_abbrev section which defines the DIE abbreviation
8416 table. */
8418 static void
8419 output_abbrev_section (void)
8421 unsigned long abbrev_id;
8423 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8424 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8426 /* Terminate the table. */
8427 dw2_asm_output_data (1, 0, NULL);
8430 /* Output a symbol we can use to refer to this DIE from another CU. */
8432 static inline void
8433 output_die_symbol (dw_die_ref die)
8435 const char *sym = die->die_id.die_symbol;
8437 gcc_assert (!die->comdat_type_p);
8439 if (sym == 0)
8440 return;
8442 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8443 /* We make these global, not weak; if the target doesn't support
8444 .linkonce, it doesn't support combining the sections, so debugging
8445 will break. */
8446 targetm.asm_out.globalize_label (asm_out_file, sym);
8448 ASM_OUTPUT_LABEL (asm_out_file, sym);
8451 /* Return a new location list, given the begin and end range, and the
8452 expression. */
8454 static inline dw_loc_list_ref
8455 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8456 const char *section)
8458 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8460 retlist->begin = begin;
8461 retlist->begin_entry = NULL;
8462 retlist->end = end;
8463 retlist->expr = expr;
8464 retlist->section = section;
8466 return retlist;
8469 /* Generate a new internal symbol for this location list node, if it
8470 hasn't got one yet. */
8472 static inline void
8473 gen_llsym (dw_loc_list_ref list)
8475 gcc_assert (!list->ll_symbol);
8476 list->ll_symbol = gen_internal_sym ("LLST");
8479 /* Output the location list given to us. */
8481 static void
8482 output_loc_list (dw_loc_list_ref list_head)
8484 dw_loc_list_ref curr = list_head;
8486 if (list_head->emitted)
8487 return;
8488 list_head->emitted = true;
8490 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8492 /* Walk the location list, and output each range + expression. */
8493 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8495 unsigned long size;
8496 /* Don't output an entry that starts and ends at the same address. */
8497 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8498 continue;
8499 size = size_of_locs (curr->expr);
8500 /* If the expression is too large, drop it on the floor. We could
8501 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8502 in the expression, but >= 64KB expressions for a single value
8503 in a single range are unlikely very useful. */
8504 if (size > 0xffff)
8505 continue;
8506 if (dwarf_split_debug_info)
8508 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8509 "Location list start/length entry (%s)",
8510 list_head->ll_symbol);
8511 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8512 "Location list range start index (%s)",
8513 curr->begin);
8514 /* The length field is 4 bytes. If we ever need to support
8515 an 8-byte length, we can add a new DW_LLE code or fall back
8516 to DW_LLE_GNU_start_end_entry. */
8517 dw2_asm_output_delta (4, curr->end, curr->begin,
8518 "Location list range length (%s)",
8519 list_head->ll_symbol);
8521 else if (!have_multiple_function_sections)
8523 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8524 "Location list begin address (%s)",
8525 list_head->ll_symbol);
8526 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8527 "Location list end address (%s)",
8528 list_head->ll_symbol);
8530 else
8532 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8533 "Location list begin address (%s)",
8534 list_head->ll_symbol);
8535 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8536 "Location list end address (%s)",
8537 list_head->ll_symbol);
8540 /* Output the block length for this list of location operations. */
8541 gcc_assert (size <= 0xffff);
8542 dw2_asm_output_data (2, size, "%s", "Location expression size");
8544 output_loc_sequence (curr->expr, -1);
8547 if (dwarf_split_debug_info)
8548 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8549 "Location list terminator (%s)",
8550 list_head->ll_symbol);
8551 else
8553 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8554 "Location list terminator begin (%s)",
8555 list_head->ll_symbol);
8556 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8557 "Location list terminator end (%s)",
8558 list_head->ll_symbol);
8562 /* Output a range_list offset into the debug_range section. Emit a
8563 relocated reference if val_entry is NULL, otherwise, emit an
8564 indirect reference. */
8566 static void
8567 output_range_list_offset (dw_attr_ref a)
8569 const char *name = dwarf_attr_name (a->dw_attr);
8571 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8573 char *p = strchr (ranges_section_label, '\0');
8574 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8575 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8576 debug_ranges_section, "%s", name);
8577 *p = '\0';
8579 else
8580 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8581 "%s (offset from %s)", name, ranges_section_label);
8584 /* Output the offset into the debug_loc section. */
8586 static void
8587 output_loc_list_offset (dw_attr_ref a)
8589 char *sym = AT_loc_list (a)->ll_symbol;
8591 gcc_assert (sym);
8592 if (dwarf_split_debug_info)
8593 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8594 "%s", dwarf_attr_name (a->dw_attr));
8595 else
8596 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8597 "%s", dwarf_attr_name (a->dw_attr));
8600 /* Output an attribute's index or value appropriately. */
8602 static void
8603 output_attr_index_or_value (dw_attr_ref a)
8605 const char *name = dwarf_attr_name (a->dw_attr);
8607 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8609 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8610 return;
8612 switch (AT_class (a))
8614 case dw_val_class_addr:
8615 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8616 break;
8617 case dw_val_class_high_pc:
8618 case dw_val_class_lbl_id:
8619 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8620 break;
8621 case dw_val_class_loc_list:
8622 output_loc_list_offset (a);
8623 break;
8624 default:
8625 gcc_unreachable ();
8629 /* Output a type signature. */
8631 static inline void
8632 output_signature (const char *sig, const char *name)
8634 int i;
8636 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8637 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8640 /* Output the DIE and its attributes. Called recursively to generate
8641 the definitions of each child DIE. */
8643 static void
8644 output_die (dw_die_ref die)
8646 dw_attr_ref a;
8647 dw_die_ref c;
8648 unsigned long size;
8649 unsigned ix;
8651 /* If someone in another CU might refer to us, set up a symbol for
8652 them to point to. */
8653 if (! die->comdat_type_p && die->die_id.die_symbol)
8654 output_die_symbol (die);
8656 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8657 (unsigned long)die->die_offset,
8658 dwarf_tag_name (die->die_tag));
8660 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8662 const char *name = dwarf_attr_name (a->dw_attr);
8664 switch (AT_class (a))
8666 case dw_val_class_addr:
8667 output_attr_index_or_value (a);
8668 break;
8670 case dw_val_class_offset:
8671 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8672 "%s", name);
8673 break;
8675 case dw_val_class_range_list:
8676 output_range_list_offset (a);
8677 break;
8679 case dw_val_class_loc:
8680 size = size_of_locs (AT_loc (a));
8682 /* Output the block length for this list of location operations. */
8683 if (dwarf_version >= 4)
8684 dw2_asm_output_data_uleb128 (size, "%s", name);
8685 else
8686 dw2_asm_output_data (constant_size (size), size, "%s", name);
8688 output_loc_sequence (AT_loc (a), -1);
8689 break;
8691 case dw_val_class_const:
8692 /* ??? It would be slightly more efficient to use a scheme like is
8693 used for unsigned constants below, but gdb 4.x does not sign
8694 extend. Gdb 5.x does sign extend. */
8695 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8696 break;
8698 case dw_val_class_unsigned_const:
8700 int csize = constant_size (AT_unsigned (a));
8701 if (dwarf_version == 3
8702 && a->dw_attr == DW_AT_data_member_location
8703 && csize >= 4)
8704 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8705 else
8706 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8708 break;
8710 case dw_val_class_const_double:
8712 unsigned HOST_WIDE_INT first, second;
8714 if (HOST_BITS_PER_WIDE_INT >= 64)
8715 dw2_asm_output_data (1,
8716 HOST_BITS_PER_DOUBLE_INT
8717 / HOST_BITS_PER_CHAR,
8718 NULL);
8720 if (WORDS_BIG_ENDIAN)
8722 first = a->dw_attr_val.v.val_double.high;
8723 second = a->dw_attr_val.v.val_double.low;
8725 else
8727 first = a->dw_attr_val.v.val_double.low;
8728 second = a->dw_attr_val.v.val_double.high;
8731 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8732 first, "%s", name);
8733 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8734 second, NULL);
8736 break;
8738 case dw_val_class_wide_int:
8740 int i;
8741 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8742 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8743 if (len * HOST_BITS_PER_WIDE_INT > 64)
8744 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8745 NULL);
8747 if (WORDS_BIG_ENDIAN)
8748 for (i = len - 1; i >= 0; --i)
8750 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8751 name);
8752 name = NULL;
8754 else
8755 for (i = 0; i < len; ++i)
8757 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8758 name);
8759 name = NULL;
8762 break;
8764 case dw_val_class_vec:
8766 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8767 unsigned int len = a->dw_attr_val.v.val_vec.length;
8768 unsigned int i;
8769 unsigned char *p;
8771 dw2_asm_output_data (constant_size (len * elt_size),
8772 len * elt_size, "%s", name);
8773 if (elt_size > sizeof (HOST_WIDE_INT))
8775 elt_size /= 2;
8776 len *= 2;
8778 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8779 i < len;
8780 i++, p += elt_size)
8781 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8782 "fp or vector constant word %u", i);
8783 break;
8786 case dw_val_class_flag:
8787 if (dwarf_version >= 4)
8789 /* Currently all add_AT_flag calls pass in 1 as last argument,
8790 so DW_FORM_flag_present can be used. If that ever changes,
8791 we'll need to use DW_FORM_flag and have some optimization
8792 in build_abbrev_table that will change those to
8793 DW_FORM_flag_present if it is set to 1 in all DIEs using
8794 the same abbrev entry. */
8795 gcc_assert (AT_flag (a) == 1);
8796 if (flag_debug_asm)
8797 fprintf (asm_out_file, "\t\t\t%s %s\n",
8798 ASM_COMMENT_START, name);
8799 break;
8801 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8802 break;
8804 case dw_val_class_loc_list:
8805 output_attr_index_or_value (a);
8806 break;
8808 case dw_val_class_die_ref:
8809 if (AT_ref_external (a))
8811 if (AT_ref (a)->comdat_type_p)
8813 comdat_type_node_ref type_node =
8814 AT_ref (a)->die_id.die_type_node;
8816 gcc_assert (type_node);
8817 output_signature (type_node->signature, name);
8819 else
8821 const char *sym = AT_ref (a)->die_id.die_symbol;
8822 int size;
8824 gcc_assert (sym);
8825 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8826 length, whereas in DWARF3 it's always sized as an
8827 offset. */
8828 if (dwarf_version == 2)
8829 size = DWARF2_ADDR_SIZE;
8830 else
8831 size = DWARF_OFFSET_SIZE;
8832 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8833 name);
8836 else
8838 gcc_assert (AT_ref (a)->die_offset);
8839 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8840 "%s", name);
8842 break;
8844 case dw_val_class_fde_ref:
8846 char l1[20];
8848 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8849 a->dw_attr_val.v.val_fde_index * 2);
8850 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8851 "%s", name);
8853 break;
8855 case dw_val_class_vms_delta:
8856 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8857 AT_vms_delta2 (a), AT_vms_delta1 (a),
8858 "%s", name);
8859 break;
8861 case dw_val_class_lbl_id:
8862 output_attr_index_or_value (a);
8863 break;
8865 case dw_val_class_lineptr:
8866 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8867 debug_line_section, "%s", name);
8868 break;
8870 case dw_val_class_macptr:
8871 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8872 debug_macinfo_section, "%s", name);
8873 break;
8875 case dw_val_class_str:
8876 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8877 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8878 a->dw_attr_val.v.val_str->label,
8879 debug_str_section,
8880 "%s: \"%s\"", name, AT_string (a));
8881 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8882 dw2_asm_output_data_uleb128 (AT_index (a),
8883 "%s: \"%s\"", name, AT_string (a));
8884 else
8885 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8886 break;
8888 case dw_val_class_file:
8890 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8892 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8893 a->dw_attr_val.v.val_file->filename);
8894 break;
8897 case dw_val_class_data8:
8899 int i;
8901 for (i = 0; i < 8; i++)
8902 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8903 i == 0 ? "%s" : NULL, name);
8904 break;
8907 case dw_val_class_high_pc:
8908 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8909 get_AT_low_pc (die), "DW_AT_high_pc");
8910 break;
8912 default:
8913 gcc_unreachable ();
8917 FOR_EACH_CHILD (die, c, output_die (c));
8919 /* Add null byte to terminate sibling list. */
8920 if (die->die_child != NULL)
8921 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8922 (unsigned long) die->die_offset);
8925 /* Output the compilation unit that appears at the beginning of the
8926 .debug_info section, and precedes the DIE descriptions. */
8928 static void
8929 output_compilation_unit_header (void)
8931 int ver = dwarf_version;
8933 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8934 dw2_asm_output_data (4, 0xffffffff,
8935 "Initial length escape value indicating 64-bit DWARF extension");
8936 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8937 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8938 "Length of Compilation Unit Info");
8939 dw2_asm_output_data (2, ver, "DWARF version number");
8940 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8941 debug_abbrev_section,
8942 "Offset Into Abbrev. Section");
8943 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8946 /* Output the compilation unit DIE and its children. */
8948 static void
8949 output_comp_unit (dw_die_ref die, int output_if_empty)
8951 const char *secname, *oldsym;
8952 char *tmp;
8953 external_ref_hash_type extern_map;
8955 /* Unless we are outputting main CU, we may throw away empty ones. */
8956 if (!output_if_empty && die->die_child == NULL)
8957 return;
8959 /* Even if there are no children of this DIE, we must output the information
8960 about the compilation unit. Otherwise, on an empty translation unit, we
8961 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8962 will then complain when examining the file. First mark all the DIEs in
8963 this CU so we know which get local refs. */
8964 mark_dies (die);
8966 extern_map = optimize_external_refs (die);
8968 build_abbrev_table (die, extern_map);
8970 extern_map.dispose ();
8972 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
8973 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8974 calc_die_sizes (die);
8976 oldsym = die->die_id.die_symbol;
8977 if (oldsym)
8979 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8981 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8982 secname = tmp;
8983 die->die_id.die_symbol = NULL;
8984 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8986 else
8988 switch_to_section (debug_info_section);
8989 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8990 info_section_emitted = true;
8993 /* Output debugging information. */
8994 output_compilation_unit_header ();
8995 output_die (die);
8997 /* Leave the marks on the main CU, so we can check them in
8998 output_pubnames. */
8999 if (oldsym)
9001 unmark_dies (die);
9002 die->die_id.die_symbol = oldsym;
9006 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9007 and .debug_pubtypes. This is configured per-target, but can be
9008 overridden by the -gpubnames or -gno-pubnames options. */
9010 static inline bool
9011 want_pubnames (void)
9013 if (debug_info_level <= DINFO_LEVEL_TERSE)
9014 return false;
9015 if (debug_generate_pub_sections != -1)
9016 return debug_generate_pub_sections;
9017 return targetm.want_debug_pub_sections;
9020 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9022 static void
9023 add_AT_pubnames (dw_die_ref die)
9025 if (want_pubnames ())
9026 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9029 /* Add a string attribute value to a skeleton DIE. */
9031 static inline void
9032 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9033 const char *str)
9035 dw_attr_node attr;
9036 struct indirect_string_node *node;
9038 if (! skeleton_debug_str_hash)
9039 skeleton_debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
9040 debug_str_eq, NULL);
9042 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9043 find_string_form (node);
9044 if (node->form == DW_FORM_GNU_str_index)
9045 node->form = DW_FORM_strp;
9047 attr.dw_attr = attr_kind;
9048 attr.dw_attr_val.val_class = dw_val_class_str;
9049 attr.dw_attr_val.val_entry = NULL;
9050 attr.dw_attr_val.v.val_str = node;
9051 add_dwarf_attr (die, &attr);
9054 /* Helper function to generate top-level dies for skeleton debug_info and
9055 debug_types. */
9057 static void
9058 add_top_level_skeleton_die_attrs (dw_die_ref die)
9060 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9061 const char *comp_dir = comp_dir_string ();
9063 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9064 if (comp_dir != NULL)
9065 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9066 add_AT_pubnames (die);
9067 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9070 /* Return the single type-unit die for skeleton type units. */
9072 static dw_die_ref
9073 get_skeleton_type_unit (void)
9075 /* For dwarf_split_debug_sections with use_type info, all type units in the
9076 skeleton sections have identical dies (but different headers). This
9077 single die will be output many times. */
9079 static dw_die_ref skeleton_type_unit = NULL;
9081 if (skeleton_type_unit == NULL)
9083 skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
9084 add_top_level_skeleton_die_attrs (skeleton_type_unit);
9085 skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
9087 return skeleton_type_unit;
9090 /* Output skeleton debug sections that point to the dwo file. */
9092 static void
9093 output_skeleton_debug_sections (dw_die_ref comp_unit)
9095 /* These attributes will be found in the full debug_info section. */
9096 remove_AT (comp_unit, DW_AT_producer);
9097 remove_AT (comp_unit, DW_AT_language);
9099 switch_to_section (debug_skeleton_info_section);
9100 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9102 /* Produce the skeleton compilation-unit header. This one differs enough from
9103 a normal CU header that it's better not to call output_compilation_unit
9104 header. */
9105 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9106 dw2_asm_output_data (4, 0xffffffff,
9107 "Initial length escape value indicating 64-bit DWARF extension");
9109 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9110 DWARF_COMPILE_UNIT_HEADER_SIZE
9111 - DWARF_INITIAL_LENGTH_SIZE
9112 + size_of_die (comp_unit),
9113 "Length of Compilation Unit Info");
9114 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9115 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9116 debug_abbrev_section,
9117 "Offset Into Abbrev. Section");
9118 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9120 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9121 output_die (comp_unit);
9123 /* Build the skeleton debug_abbrev section. */
9124 switch_to_section (debug_skeleton_abbrev_section);
9125 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9127 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9128 if (use_debug_types)
9129 output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
9131 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9134 /* Output a comdat type unit DIE and its children. */
9136 static void
9137 output_comdat_type_unit (comdat_type_node *node)
9139 const char *secname;
9140 char *tmp;
9141 int i;
9142 #if defined (OBJECT_FORMAT_ELF)
9143 tree comdat_key;
9144 #endif
9145 external_ref_hash_type extern_map;
9147 /* First mark all the DIEs in this CU so we know which get local refs. */
9148 mark_dies (node->root_die);
9150 extern_map = optimize_external_refs (node->root_die);
9152 build_abbrev_table (node->root_die, extern_map);
9154 extern_map.dispose ();
9156 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9157 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9158 calc_die_sizes (node->root_die);
9160 #if defined (OBJECT_FORMAT_ELF)
9161 if (!dwarf_split_debug_info)
9162 secname = ".debug_types";
9163 else
9164 secname = ".debug_types.dwo";
9166 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9167 sprintf (tmp, "wt.");
9168 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9169 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9170 comdat_key = get_identifier (tmp);
9171 targetm.asm_out.named_section (secname,
9172 SECTION_DEBUG | SECTION_LINKONCE,
9173 comdat_key);
9174 #else
9175 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9176 sprintf (tmp, ".gnu.linkonce.wt.");
9177 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9178 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9179 secname = tmp;
9180 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9181 #endif
9183 /* Output debugging information. */
9184 output_compilation_unit_header ();
9185 output_signature (node->signature, "Type Signature");
9186 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9187 "Offset to Type DIE");
9188 output_die (node->root_die);
9190 unmark_dies (node->root_die);
9192 #if defined (OBJECT_FORMAT_ELF)
9193 if (dwarf_split_debug_info)
9195 /* Produce the skeleton type-unit header. */
9196 const char *secname = ".debug_types";
9198 targetm.asm_out.named_section (secname,
9199 SECTION_DEBUG | SECTION_LINKONCE,
9200 comdat_key);
9201 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9202 dw2_asm_output_data (4, 0xffffffff,
9203 "Initial length escape value indicating 64-bit DWARF extension");
9205 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9206 DWARF_COMPILE_UNIT_HEADER_SIZE
9207 - DWARF_INITIAL_LENGTH_SIZE
9208 + size_of_die (get_skeleton_type_unit ())
9209 + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
9210 "Length of Type Unit Info");
9211 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9212 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9213 debug_skeleton_abbrev_section_label,
9214 debug_abbrev_section,
9215 "Offset Into Abbrev. Section");
9216 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9217 output_signature (node->signature, "Type Signature");
9218 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
9220 output_die (get_skeleton_type_unit ());
9222 #endif
9225 /* Return the DWARF2/3 pubname associated with a decl. */
9227 static const char *
9228 dwarf2_name (tree decl, int scope)
9230 if (DECL_NAMELESS (decl))
9231 return NULL;
9232 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9235 /* Add a new entry to .debug_pubnames if appropriate. */
9237 static void
9238 add_pubname_string (const char *str, dw_die_ref die)
9240 pubname_entry e;
9242 e.die = die;
9243 e.name = xstrdup (str);
9244 vec_safe_push (pubname_table, e);
9247 static void
9248 add_pubname (tree decl, dw_die_ref die)
9250 if (!want_pubnames ())
9251 return;
9253 /* Don't add items to the table when we expect that the consumer will have
9254 just read the enclosing die. For example, if the consumer is looking at a
9255 class_member, it will either be inside the class already, or will have just
9256 looked up the class to find the member. Either way, searching the class is
9257 faster than searching the index. */
9258 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9259 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9261 const char *name = dwarf2_name (decl, 1);
9263 if (name)
9264 add_pubname_string (name, die);
9268 /* Add an enumerator to the pubnames section. */
9270 static void
9271 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9273 pubname_entry e;
9275 gcc_assert (scope_name);
9276 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9277 e.die = die;
9278 vec_safe_push (pubname_table, e);
9281 /* Add a new entry to .debug_pubtypes if appropriate. */
9283 static void
9284 add_pubtype (tree decl, dw_die_ref die)
9286 pubname_entry e;
9288 if (!want_pubnames ())
9289 return;
9291 if ((TREE_PUBLIC (decl)
9292 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9293 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9295 tree scope = NULL;
9296 const char *scope_name = "";
9297 const char *sep = is_cxx () ? "::" : ".";
9298 const char *name;
9300 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9301 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9303 scope_name = lang_hooks.dwarf_name (scope, 1);
9304 if (scope_name != NULL && scope_name[0] != '\0')
9305 scope_name = concat (scope_name, sep, NULL);
9306 else
9307 scope_name = "";
9310 if (TYPE_P (decl))
9311 name = type_tag (decl);
9312 else
9313 name = lang_hooks.dwarf_name (decl, 1);
9315 /* If we don't have a name for the type, there's no point in adding
9316 it to the table. */
9317 if (name != NULL && name[0] != '\0')
9319 e.die = die;
9320 e.name = concat (scope_name, name, NULL);
9321 vec_safe_push (pubtype_table, e);
9324 /* Although it might be more consistent to add the pubinfo for the
9325 enumerators as their dies are created, they should only be added if the
9326 enum type meets the criteria above. So rather than re-check the parent
9327 enum type whenever an enumerator die is created, just output them all
9328 here. This isn't protected by the name conditional because anonymous
9329 enums don't have names. */
9330 if (die->die_tag == DW_TAG_enumeration_type)
9332 dw_die_ref c;
9334 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9339 /* Output a single entry in the pubnames table. */
9341 static void
9342 output_pubname (dw_offset die_offset, pubname_entry *entry)
9344 dw_die_ref die = entry->die;
9345 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9347 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9349 if (debug_generate_pub_sections == 2)
9351 /* This logic follows gdb's method for determining the value of the flag
9352 byte. */
9353 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9354 switch (die->die_tag)
9356 case DW_TAG_typedef:
9357 case DW_TAG_base_type:
9358 case DW_TAG_subrange_type:
9359 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9360 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9361 break;
9362 case DW_TAG_enumerator:
9363 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9364 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9365 if (!is_cxx () && !is_java ())
9366 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9367 break;
9368 case DW_TAG_subprogram:
9369 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9370 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9371 if (!is_ada ())
9372 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9373 break;
9374 case DW_TAG_constant:
9375 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9376 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9377 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9378 break;
9379 case DW_TAG_variable:
9380 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9381 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9382 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9383 break;
9384 case DW_TAG_namespace:
9385 case DW_TAG_imported_declaration:
9386 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9387 break;
9388 case DW_TAG_class_type:
9389 case DW_TAG_interface_type:
9390 case DW_TAG_structure_type:
9391 case DW_TAG_union_type:
9392 case DW_TAG_enumeration_type:
9393 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9394 if (!is_cxx () && !is_java ())
9395 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9396 break;
9397 default:
9398 /* An unusual tag. Leave the flag-byte empty. */
9399 break;
9401 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9402 "GDB-index flags");
9405 dw2_asm_output_nstring (entry->name, -1, "external name");
9409 /* Output the public names table used to speed up access to externally
9410 visible names; or the public types table used to find type definitions. */
9412 static void
9413 output_pubnames (vec<pubname_entry, va_gc> *names)
9415 unsigned i;
9416 unsigned long pubnames_length = size_of_pubnames (names);
9417 pubname_ref pub;
9419 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9420 dw2_asm_output_data (4, 0xffffffff,
9421 "Initial length escape value indicating 64-bit DWARF extension");
9422 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9424 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9425 dw2_asm_output_data (2, 2, "DWARF Version");
9427 if (dwarf_split_debug_info)
9428 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9429 debug_skeleton_info_section,
9430 "Offset of Compilation Unit Info");
9431 else
9432 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9433 debug_info_section,
9434 "Offset of Compilation Unit Info");
9435 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9436 "Compilation Unit Length");
9438 FOR_EACH_VEC_ELT (*names, i, pub)
9440 if (include_pubname_in_output (names, pub))
9442 dw_offset die_offset = pub->die->die_offset;
9444 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9445 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9446 gcc_assert (pub->die->die_mark);
9448 /* If we're putting types in their own .debug_types sections,
9449 the .debug_pubtypes table will still point to the compile
9450 unit (not the type unit), so we want to use the offset of
9451 the skeleton DIE (if there is one). */
9452 if (pub->die->comdat_type_p && names == pubtype_table)
9454 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9456 if (type_node != NULL)
9457 die_offset = (type_node->skeleton_die != NULL
9458 ? type_node->skeleton_die->die_offset
9459 : comp_unit_die ()->die_offset);
9462 output_pubname (die_offset, pub);
9466 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9469 /* Output public names and types tables if necessary. */
9471 static void
9472 output_pubtables (void)
9474 if (!want_pubnames () || !info_section_emitted)
9475 return;
9477 switch_to_section (debug_pubnames_section);
9478 output_pubnames (pubname_table);
9479 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9480 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9481 simply won't look for the section. */
9482 switch_to_section (debug_pubtypes_section);
9483 output_pubnames (pubtype_table);
9487 /* Output the information that goes into the .debug_aranges table.
9488 Namely, define the beginning and ending address range of the
9489 text section generated for this compilation unit. */
9491 static void
9492 output_aranges (unsigned long aranges_length)
9494 unsigned i;
9496 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9497 dw2_asm_output_data (4, 0xffffffff,
9498 "Initial length escape value indicating 64-bit DWARF extension");
9499 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9500 "Length of Address Ranges Info");
9501 /* Version number for aranges is still 2, even in DWARF3. */
9502 dw2_asm_output_data (2, 2, "DWARF Version");
9503 if (dwarf_split_debug_info)
9504 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9505 debug_skeleton_info_section,
9506 "Offset of Compilation Unit Info");
9507 else
9508 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9509 debug_info_section,
9510 "Offset of Compilation Unit Info");
9511 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9512 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9514 /* We need to align to twice the pointer size here. */
9515 if (DWARF_ARANGES_PAD_SIZE)
9517 /* Pad using a 2 byte words so that padding is correct for any
9518 pointer size. */
9519 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9520 2 * DWARF2_ADDR_SIZE);
9521 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9522 dw2_asm_output_data (2, 0, NULL);
9525 /* It is necessary not to output these entries if the sections were
9526 not used; if the sections were not used, the length will be 0 and
9527 the address may end up as 0 if the section is discarded by ld
9528 --gc-sections, leaving an invalid (0, 0) entry that can be
9529 confused with the terminator. */
9530 if (text_section_used)
9532 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9533 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9534 text_section_label, "Length");
9536 if (cold_text_section_used)
9538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9539 "Address");
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9541 cold_text_section_label, "Length");
9544 if (have_multiple_function_sections)
9546 unsigned fde_idx;
9547 dw_fde_ref fde;
9549 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9551 if (DECL_IGNORED_P (fde->decl))
9552 continue;
9553 if (!fde->in_std_section)
9555 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9556 "Address");
9557 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9558 fde->dw_fde_begin, "Length");
9560 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9562 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9563 "Address");
9564 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9565 fde->dw_fde_second_begin, "Length");
9570 /* Output the terminator words. */
9571 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9572 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9575 /* Add a new entry to .debug_ranges. Return the offset at which it
9576 was placed. */
9578 static unsigned int
9579 add_ranges_num (int num)
9581 unsigned int in_use = ranges_table_in_use;
9583 if (in_use == ranges_table_allocated)
9585 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9586 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9587 ranges_table_allocated);
9588 memset (ranges_table + ranges_table_in_use, 0,
9589 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9592 ranges_table[in_use].num = num;
9593 ranges_table_in_use = in_use + 1;
9595 return in_use * 2 * DWARF2_ADDR_SIZE;
9598 /* Add a new entry to .debug_ranges corresponding to a block, or a
9599 range terminator if BLOCK is NULL. */
9601 static unsigned int
9602 add_ranges (const_tree block)
9604 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9607 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9608 When using dwarf_split_debug_info, address attributes in dies destined
9609 for the final executable should be direct references--setting the
9610 parameter force_direct ensures this behavior. */
9612 static void
9613 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9614 bool *added, bool force_direct)
9616 unsigned int in_use = ranges_by_label_in_use;
9617 unsigned int offset;
9619 if (in_use == ranges_by_label_allocated)
9621 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9622 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9623 ranges_by_label,
9624 ranges_by_label_allocated);
9625 memset (ranges_by_label + ranges_by_label_in_use, 0,
9626 RANGES_TABLE_INCREMENT
9627 * sizeof (struct dw_ranges_by_label_struct));
9630 ranges_by_label[in_use].begin = begin;
9631 ranges_by_label[in_use].end = end;
9632 ranges_by_label_in_use = in_use + 1;
9634 offset = add_ranges_num (-(int)in_use - 1);
9635 if (!*added)
9637 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9638 *added = true;
9642 static void
9643 output_ranges (void)
9645 unsigned i;
9646 static const char *const start_fmt = "Offset %#x";
9647 const char *fmt = start_fmt;
9649 for (i = 0; i < ranges_table_in_use; i++)
9651 int block_num = ranges_table[i].num;
9653 if (block_num > 0)
9655 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9656 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9658 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9659 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9661 /* If all code is in the text section, then the compilation
9662 unit base address defaults to DW_AT_low_pc, which is the
9663 base of the text section. */
9664 if (!have_multiple_function_sections)
9666 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9667 text_section_label,
9668 fmt, i * 2 * DWARF2_ADDR_SIZE);
9669 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9670 text_section_label, NULL);
9673 /* Otherwise, the compilation unit base address is zero,
9674 which allows us to use absolute addresses, and not worry
9675 about whether the target supports cross-section
9676 arithmetic. */
9677 else
9679 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9680 fmt, i * 2 * DWARF2_ADDR_SIZE);
9681 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9684 fmt = NULL;
9687 /* Negative block_num stands for an index into ranges_by_label. */
9688 else if (block_num < 0)
9690 int lab_idx = - block_num - 1;
9692 if (!have_multiple_function_sections)
9694 gcc_unreachable ();
9695 #if 0
9696 /* If we ever use add_ranges_by_labels () for a single
9697 function section, all we have to do is to take out
9698 the #if 0 above. */
9699 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9700 ranges_by_label[lab_idx].begin,
9701 text_section_label,
9702 fmt, i * 2 * DWARF2_ADDR_SIZE);
9703 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9704 ranges_by_label[lab_idx].end,
9705 text_section_label, NULL);
9706 #endif
9708 else
9710 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9711 ranges_by_label[lab_idx].begin,
9712 fmt, i * 2 * DWARF2_ADDR_SIZE);
9713 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9714 ranges_by_label[lab_idx].end,
9715 NULL);
9718 else
9720 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9721 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9722 fmt = start_fmt;
9727 /* Data structure containing information about input files. */
9728 struct file_info
9730 const char *path; /* Complete file name. */
9731 const char *fname; /* File name part. */
9732 int length; /* Length of entire string. */
9733 struct dwarf_file_data * file_idx; /* Index in input file table. */
9734 int dir_idx; /* Index in directory table. */
9737 /* Data structure containing information about directories with source
9738 files. */
9739 struct dir_info
9741 const char *path; /* Path including directory name. */
9742 int length; /* Path length. */
9743 int prefix; /* Index of directory entry which is a prefix. */
9744 int count; /* Number of files in this directory. */
9745 int dir_idx; /* Index of directory used as base. */
9748 /* Callback function for file_info comparison. We sort by looking at
9749 the directories in the path. */
9751 static int
9752 file_info_cmp (const void *p1, const void *p2)
9754 const struct file_info *const s1 = (const struct file_info *) p1;
9755 const struct file_info *const s2 = (const struct file_info *) p2;
9756 const unsigned char *cp1;
9757 const unsigned char *cp2;
9759 /* Take care of file names without directories. We need to make sure that
9760 we return consistent values to qsort since some will get confused if
9761 we return the same value when identical operands are passed in opposite
9762 orders. So if neither has a directory, return 0 and otherwise return
9763 1 or -1 depending on which one has the directory. */
9764 if ((s1->path == s1->fname || s2->path == s2->fname))
9765 return (s2->path == s2->fname) - (s1->path == s1->fname);
9767 cp1 = (const unsigned char *) s1->path;
9768 cp2 = (const unsigned char *) s2->path;
9770 while (1)
9772 ++cp1;
9773 ++cp2;
9774 /* Reached the end of the first path? If so, handle like above. */
9775 if ((cp1 == (const unsigned char *) s1->fname)
9776 || (cp2 == (const unsigned char *) s2->fname))
9777 return ((cp2 == (const unsigned char *) s2->fname)
9778 - (cp1 == (const unsigned char *) s1->fname));
9780 /* Character of current path component the same? */
9781 else if (*cp1 != *cp2)
9782 return *cp1 - *cp2;
9786 struct file_name_acquire_data
9788 struct file_info *files;
9789 int used_files;
9790 int max_files;
9793 /* Traversal function for the hash table. */
9795 static int
9796 file_name_acquire (void ** slot, void *data)
9798 struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9799 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9800 struct file_info *fi;
9801 const char *f;
9803 gcc_assert (fnad->max_files >= d->emitted_number);
9805 if (! d->emitted_number)
9806 return 1;
9808 gcc_assert (fnad->max_files != fnad->used_files);
9810 fi = fnad->files + fnad->used_files++;
9812 /* Skip all leading "./". */
9813 f = d->filename;
9814 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9815 f += 2;
9817 /* Create a new array entry. */
9818 fi->path = f;
9819 fi->length = strlen (f);
9820 fi->file_idx = d;
9822 /* Search for the file name part. */
9823 f = strrchr (f, DIR_SEPARATOR);
9824 #if defined (DIR_SEPARATOR_2)
9826 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9828 if (g != NULL)
9830 if (f == NULL || f < g)
9831 f = g;
9834 #endif
9836 fi->fname = f == NULL ? fi->path : f + 1;
9837 return 1;
9840 /* Output the directory table and the file name table. We try to minimize
9841 the total amount of memory needed. A heuristic is used to avoid large
9842 slowdowns with many input files. */
9844 static void
9845 output_file_names (void)
9847 struct file_name_acquire_data fnad;
9848 int numfiles;
9849 struct file_info *files;
9850 struct dir_info *dirs;
9851 int *saved;
9852 int *savehere;
9853 int *backmap;
9854 int ndirs;
9855 int idx_offset;
9856 int i;
9858 if (!last_emitted_file)
9860 dw2_asm_output_data (1, 0, "End directory table");
9861 dw2_asm_output_data (1, 0, "End file name table");
9862 return;
9865 numfiles = last_emitted_file->emitted_number;
9867 /* Allocate the various arrays we need. */
9868 files = XALLOCAVEC (struct file_info, numfiles);
9869 dirs = XALLOCAVEC (struct dir_info, numfiles);
9871 fnad.files = files;
9872 fnad.used_files = 0;
9873 fnad.max_files = numfiles;
9874 htab_traverse (file_table, file_name_acquire, &fnad);
9875 gcc_assert (fnad.used_files == fnad.max_files);
9877 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9879 /* Find all the different directories used. */
9880 dirs[0].path = files[0].path;
9881 dirs[0].length = files[0].fname - files[0].path;
9882 dirs[0].prefix = -1;
9883 dirs[0].count = 1;
9884 dirs[0].dir_idx = 0;
9885 files[0].dir_idx = 0;
9886 ndirs = 1;
9888 for (i = 1; i < numfiles; i++)
9889 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9890 && memcmp (dirs[ndirs - 1].path, files[i].path,
9891 dirs[ndirs - 1].length) == 0)
9893 /* Same directory as last entry. */
9894 files[i].dir_idx = ndirs - 1;
9895 ++dirs[ndirs - 1].count;
9897 else
9899 int j;
9901 /* This is a new directory. */
9902 dirs[ndirs].path = files[i].path;
9903 dirs[ndirs].length = files[i].fname - files[i].path;
9904 dirs[ndirs].count = 1;
9905 dirs[ndirs].dir_idx = ndirs;
9906 files[i].dir_idx = ndirs;
9908 /* Search for a prefix. */
9909 dirs[ndirs].prefix = -1;
9910 for (j = 0; j < ndirs; j++)
9911 if (dirs[j].length < dirs[ndirs].length
9912 && dirs[j].length > 1
9913 && (dirs[ndirs].prefix == -1
9914 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9915 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9916 dirs[ndirs].prefix = j;
9918 ++ndirs;
9921 /* Now to the actual work. We have to find a subset of the directories which
9922 allow expressing the file name using references to the directory table
9923 with the least amount of characters. We do not do an exhaustive search
9924 where we would have to check out every combination of every single
9925 possible prefix. Instead we use a heuristic which provides nearly optimal
9926 results in most cases and never is much off. */
9927 saved = XALLOCAVEC (int, ndirs);
9928 savehere = XALLOCAVEC (int, ndirs);
9930 memset (saved, '\0', ndirs * sizeof (saved[0]));
9931 for (i = 0; i < ndirs; i++)
9933 int j;
9934 int total;
9936 /* We can always save some space for the current directory. But this
9937 does not mean it will be enough to justify adding the directory. */
9938 savehere[i] = dirs[i].length;
9939 total = (savehere[i] - saved[i]) * dirs[i].count;
9941 for (j = i + 1; j < ndirs; j++)
9943 savehere[j] = 0;
9944 if (saved[j] < dirs[i].length)
9946 /* Determine whether the dirs[i] path is a prefix of the
9947 dirs[j] path. */
9948 int k;
9950 k = dirs[j].prefix;
9951 while (k != -1 && k != (int) i)
9952 k = dirs[k].prefix;
9954 if (k == (int) i)
9956 /* Yes it is. We can possibly save some memory by
9957 writing the filenames in dirs[j] relative to
9958 dirs[i]. */
9959 savehere[j] = dirs[i].length;
9960 total += (savehere[j] - saved[j]) * dirs[j].count;
9965 /* Check whether we can save enough to justify adding the dirs[i]
9966 directory. */
9967 if (total > dirs[i].length + 1)
9969 /* It's worthwhile adding. */
9970 for (j = i; j < ndirs; j++)
9971 if (savehere[j] > 0)
9973 /* Remember how much we saved for this directory so far. */
9974 saved[j] = savehere[j];
9976 /* Remember the prefix directory. */
9977 dirs[j].dir_idx = i;
9982 /* Emit the directory name table. */
9983 idx_offset = dirs[0].length > 0 ? 1 : 0;
9984 for (i = 1 - idx_offset; i < ndirs; i++)
9985 dw2_asm_output_nstring (dirs[i].path,
9986 dirs[i].length
9987 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9988 "Directory Entry: %#x", i + idx_offset);
9990 dw2_asm_output_data (1, 0, "End directory table");
9992 /* We have to emit them in the order of emitted_number since that's
9993 used in the debug info generation. To do this efficiently we
9994 generate a back-mapping of the indices first. */
9995 backmap = XALLOCAVEC (int, numfiles);
9996 for (i = 0; i < numfiles; i++)
9997 backmap[files[i].file_idx->emitted_number - 1] = i;
9999 /* Now write all the file names. */
10000 for (i = 0; i < numfiles; i++)
10002 int file_idx = backmap[i];
10003 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10005 #ifdef VMS_DEBUGGING_INFO
10006 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10008 /* Setting these fields can lead to debugger miscomparisons,
10009 but VMS Debug requires them to be set correctly. */
10011 int ver;
10012 long long cdt;
10013 long siz;
10014 int maxfilelen = strlen (files[file_idx].path)
10015 + dirs[dir_idx].length
10016 + MAX_VMS_VERSION_LEN + 1;
10017 char *filebuf = XALLOCAVEC (char, maxfilelen);
10019 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10020 snprintf (filebuf, maxfilelen, "%s;%d",
10021 files[file_idx].path + dirs[dir_idx].length, ver);
10023 dw2_asm_output_nstring
10024 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10026 /* Include directory index. */
10027 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10029 /* Modification time. */
10030 dw2_asm_output_data_uleb128
10031 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10032 ? cdt : 0,
10033 NULL);
10035 /* File length in bytes. */
10036 dw2_asm_output_data_uleb128
10037 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10038 ? siz : 0,
10039 NULL);
10040 #else
10041 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10042 "File Entry: %#x", (unsigned) i + 1);
10044 /* Include directory index. */
10045 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10047 /* Modification time. */
10048 dw2_asm_output_data_uleb128 (0, NULL);
10050 /* File length in bytes. */
10051 dw2_asm_output_data_uleb128 (0, NULL);
10052 #endif /* VMS_DEBUGGING_INFO */
10055 dw2_asm_output_data (1, 0, "End file name table");
10059 /* Output one line number table into the .debug_line section. */
10061 static void
10062 output_one_line_info_table (dw_line_info_table *table)
10064 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10065 unsigned int current_line = 1;
10066 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10067 dw_line_info_entry *ent;
10068 size_t i;
10070 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10072 switch (ent->opcode)
10074 case LI_set_address:
10075 /* ??? Unfortunately, we have little choice here currently, and
10076 must always use the most general form. GCC does not know the
10077 address delta itself, so we can't use DW_LNS_advance_pc. Many
10078 ports do have length attributes which will give an upper bound
10079 on the address range. We could perhaps use length attributes
10080 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10081 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10083 /* This can handle any delta. This takes
10084 4+DWARF2_ADDR_SIZE bytes. */
10085 dw2_asm_output_data (1, 0, "set address %s", line_label);
10086 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10087 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10088 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10089 break;
10091 case LI_set_line:
10092 if (ent->val == current_line)
10094 /* We still need to start a new row, so output a copy insn. */
10095 dw2_asm_output_data (1, DW_LNS_copy,
10096 "copy line %u", current_line);
10098 else
10100 int line_offset = ent->val - current_line;
10101 int line_delta = line_offset - DWARF_LINE_BASE;
10103 current_line = ent->val;
10104 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10106 /* This can handle deltas from -10 to 234, using the current
10107 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10108 This takes 1 byte. */
10109 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10110 "line %u", current_line);
10112 else
10114 /* This can handle any delta. This takes at least 4 bytes,
10115 depending on the value being encoded. */
10116 dw2_asm_output_data (1, DW_LNS_advance_line,
10117 "advance to line %u", current_line);
10118 dw2_asm_output_data_sleb128 (line_offset, NULL);
10119 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10122 break;
10124 case LI_set_file:
10125 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10126 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10127 break;
10129 case LI_set_column:
10130 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10131 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10132 break;
10134 case LI_negate_stmt:
10135 current_is_stmt = !current_is_stmt;
10136 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10137 "is_stmt %d", current_is_stmt);
10138 break;
10140 case LI_set_prologue_end:
10141 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10142 "set prologue end");
10143 break;
10145 case LI_set_epilogue_begin:
10146 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10147 "set epilogue begin");
10148 break;
10150 case LI_set_discriminator:
10151 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10152 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10153 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10154 dw2_asm_output_data_uleb128 (ent->val, NULL);
10155 break;
10159 /* Emit debug info for the address of the end of the table. */
10160 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10161 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10162 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10163 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10165 dw2_asm_output_data (1, 0, "end sequence");
10166 dw2_asm_output_data_uleb128 (1, NULL);
10167 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10170 /* Output the source line number correspondence information. This
10171 information goes into the .debug_line section. */
10173 static void
10174 output_line_info (bool prologue_only)
10176 char l1[20], l2[20], p1[20], p2[20];
10177 int ver = dwarf_version;
10178 bool saw_one = false;
10179 int opc;
10181 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10182 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10183 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10184 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10186 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10187 dw2_asm_output_data (4, 0xffffffff,
10188 "Initial length escape value indicating 64-bit DWARF extension");
10189 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10190 "Length of Source Line Info");
10191 ASM_OUTPUT_LABEL (asm_out_file, l1);
10193 dw2_asm_output_data (2, ver, "DWARF Version");
10194 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10195 ASM_OUTPUT_LABEL (asm_out_file, p1);
10197 /* Define the architecture-dependent minimum instruction length (in bytes).
10198 In this implementation of DWARF, this field is used for information
10199 purposes only. Since GCC generates assembly language, we have no
10200 a priori knowledge of how many instruction bytes are generated for each
10201 source line, and therefore can use only the DW_LNE_set_address and
10202 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10203 this as '1', which is "correct enough" for all architectures,
10204 and don't let the target override. */
10205 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10207 if (ver >= 4)
10208 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10209 "Maximum Operations Per Instruction");
10210 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10211 "Default is_stmt_start flag");
10212 dw2_asm_output_data (1, DWARF_LINE_BASE,
10213 "Line Base Value (Special Opcodes)");
10214 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10215 "Line Range Value (Special Opcodes)");
10216 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10217 "Special Opcode Base");
10219 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10221 int n_op_args;
10222 switch (opc)
10224 case DW_LNS_advance_pc:
10225 case DW_LNS_advance_line:
10226 case DW_LNS_set_file:
10227 case DW_LNS_set_column:
10228 case DW_LNS_fixed_advance_pc:
10229 case DW_LNS_set_isa:
10230 n_op_args = 1;
10231 break;
10232 default:
10233 n_op_args = 0;
10234 break;
10237 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10238 opc, n_op_args);
10241 /* Write out the information about the files we use. */
10242 output_file_names ();
10243 ASM_OUTPUT_LABEL (asm_out_file, p2);
10244 if (prologue_only)
10246 /* Output the marker for the end of the line number info. */
10247 ASM_OUTPUT_LABEL (asm_out_file, l2);
10248 return;
10251 if (separate_line_info)
10253 dw_line_info_table *table;
10254 size_t i;
10256 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10257 if (table->in_use)
10259 output_one_line_info_table (table);
10260 saw_one = true;
10263 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10265 output_one_line_info_table (cold_text_section_line_info);
10266 saw_one = true;
10269 /* ??? Some Darwin linkers crash on a .debug_line section with no
10270 sequences. Further, merely a DW_LNE_end_sequence entry is not
10271 sufficient -- the address column must also be initialized.
10272 Make sure to output at least one set_address/end_sequence pair,
10273 choosing .text since that section is always present. */
10274 if (text_section_line_info->in_use || !saw_one)
10275 output_one_line_info_table (text_section_line_info);
10277 /* Output the marker for the end of the line number info. */
10278 ASM_OUTPUT_LABEL (asm_out_file, l2);
10281 /* Given a pointer to a tree node for some base type, return a pointer to
10282 a DIE that describes the given type.
10284 This routine must only be called for GCC type nodes that correspond to
10285 Dwarf base (fundamental) types. */
10287 static dw_die_ref
10288 base_type_die (tree type)
10290 dw_die_ref base_type_result;
10291 enum dwarf_type encoding;
10293 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10294 return 0;
10296 /* If this is a subtype that should not be emitted as a subrange type,
10297 use the base type. See subrange_type_for_debug_p. */
10298 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10299 type = TREE_TYPE (type);
10301 switch (TREE_CODE (type))
10303 case INTEGER_TYPE:
10304 if ((dwarf_version >= 4 || !dwarf_strict)
10305 && TYPE_NAME (type)
10306 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10307 && DECL_IS_BUILTIN (TYPE_NAME (type))
10308 && DECL_NAME (TYPE_NAME (type)))
10310 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10311 if (strcmp (name, "char16_t") == 0
10312 || strcmp (name, "char32_t") == 0)
10314 encoding = DW_ATE_UTF;
10315 break;
10318 if (TYPE_STRING_FLAG (type))
10320 if (TYPE_UNSIGNED (type))
10321 encoding = DW_ATE_unsigned_char;
10322 else
10323 encoding = DW_ATE_signed_char;
10325 else if (TYPE_UNSIGNED (type))
10326 encoding = DW_ATE_unsigned;
10327 else
10328 encoding = DW_ATE_signed;
10329 break;
10331 case REAL_TYPE:
10332 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10334 if (dwarf_version >= 3 || !dwarf_strict)
10335 encoding = DW_ATE_decimal_float;
10336 else
10337 encoding = DW_ATE_lo_user;
10339 else
10340 encoding = DW_ATE_float;
10341 break;
10343 case FIXED_POINT_TYPE:
10344 if (!(dwarf_version >= 3 || !dwarf_strict))
10345 encoding = DW_ATE_lo_user;
10346 else if (TYPE_UNSIGNED (type))
10347 encoding = DW_ATE_unsigned_fixed;
10348 else
10349 encoding = DW_ATE_signed_fixed;
10350 break;
10352 /* Dwarf2 doesn't know anything about complex ints, so use
10353 a user defined type for it. */
10354 case COMPLEX_TYPE:
10355 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10356 encoding = DW_ATE_complex_float;
10357 else
10358 encoding = DW_ATE_lo_user;
10359 break;
10361 case BOOLEAN_TYPE:
10362 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10363 encoding = DW_ATE_boolean;
10364 break;
10366 default:
10367 /* No other TREE_CODEs are Dwarf fundamental types. */
10368 gcc_unreachable ();
10371 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10373 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10374 int_size_in_bytes (type));
10375 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10376 add_pubtype (type, base_type_result);
10378 return base_type_result;
10381 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10382 named 'auto' in its type: return true for it, false otherwise. */
10384 static inline bool
10385 is_cxx_auto (tree type)
10387 if (is_cxx ())
10389 tree name = TYPE_IDENTIFIER (type);
10390 if (name == get_identifier ("auto")
10391 || name == get_identifier ("decltype(auto)"))
10392 return true;
10394 return false;
10397 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10398 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10400 static inline int
10401 is_base_type (tree type)
10403 switch (TREE_CODE (type))
10405 case ERROR_MARK:
10406 case VOID_TYPE:
10407 case INTEGER_TYPE:
10408 case REAL_TYPE:
10409 case FIXED_POINT_TYPE:
10410 case COMPLEX_TYPE:
10411 case BOOLEAN_TYPE:
10412 return 1;
10414 case ARRAY_TYPE:
10415 case RECORD_TYPE:
10416 case UNION_TYPE:
10417 case QUAL_UNION_TYPE:
10418 case ENUMERAL_TYPE:
10419 case FUNCTION_TYPE:
10420 case METHOD_TYPE:
10421 case POINTER_TYPE:
10422 case REFERENCE_TYPE:
10423 case NULLPTR_TYPE:
10424 case OFFSET_TYPE:
10425 case LANG_TYPE:
10426 case VECTOR_TYPE:
10427 return 0;
10429 default:
10430 if (is_cxx_auto (type))
10431 return 0;
10432 gcc_unreachable ();
10435 return 0;
10438 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10439 node, return the size in bits for the type if it is a constant, or else
10440 return the alignment for the type if the type's size is not constant, or
10441 else return BITS_PER_WORD if the type actually turns out to be an
10442 ERROR_MARK node. */
10444 static inline unsigned HOST_WIDE_INT
10445 simple_type_size_in_bits (const_tree type)
10447 if (TREE_CODE (type) == ERROR_MARK)
10448 return BITS_PER_WORD;
10449 else if (TYPE_SIZE (type) == NULL_TREE)
10450 return 0;
10451 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10452 return tree_to_uhwi (TYPE_SIZE (type));
10453 else
10454 return TYPE_ALIGN (type);
10457 /* Similarly, but return an offset_int instead of UHWI. */
10459 static inline offset_int
10460 offset_int_type_size_in_bits (const_tree type)
10462 if (TREE_CODE (type) == ERROR_MARK)
10463 return BITS_PER_WORD;
10464 else if (TYPE_SIZE (type) == NULL_TREE)
10465 return 0;
10466 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10467 return wi::to_offset (TYPE_SIZE (type));
10468 else
10469 return TYPE_ALIGN (type);
10472 /* Given a pointer to a tree node for a subrange type, return a pointer
10473 to a DIE that describes the given type. */
10475 static dw_die_ref
10476 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10478 dw_die_ref subrange_die;
10479 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10481 if (context_die == NULL)
10482 context_die = comp_unit_die ();
10484 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10486 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10488 /* The size of the subrange type and its base type do not match,
10489 so we need to generate a size attribute for the subrange type. */
10490 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10493 if (low)
10494 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10495 if (high)
10496 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10498 return subrange_die;
10501 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10502 entry that chains various modifiers in front of the given type. */
10504 static dw_die_ref
10505 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10506 dw_die_ref context_die)
10508 enum tree_code code = TREE_CODE (type);
10509 dw_die_ref mod_type_die;
10510 dw_die_ref sub_die = NULL;
10511 tree item_type = NULL;
10512 tree qualified_type;
10513 tree name, low, high;
10514 dw_die_ref mod_scope;
10516 if (code == ERROR_MARK)
10517 return NULL;
10519 /* See if we already have the appropriately qualified variant of
10520 this type. */
10521 qualified_type
10522 = get_qualified_type (type,
10523 ((is_const_type ? TYPE_QUAL_CONST : 0)
10524 | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10526 if (qualified_type == sizetype
10527 && TYPE_NAME (qualified_type)
10528 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10530 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10532 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10533 && TYPE_PRECISION (t)
10534 == TYPE_PRECISION (qualified_type)
10535 && TYPE_UNSIGNED (t)
10536 == TYPE_UNSIGNED (qualified_type));
10537 qualified_type = t;
10540 /* If we do, then we can just use its DIE, if it exists. */
10541 if (qualified_type)
10543 mod_type_die = lookup_type_die (qualified_type);
10544 if (mod_type_die)
10545 return mod_type_die;
10548 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10550 /* Handle C typedef types. */
10551 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10552 && !DECL_ARTIFICIAL (name))
10554 tree dtype = TREE_TYPE (name);
10556 if (qualified_type == dtype)
10558 /* For a named type, use the typedef. */
10559 gen_type_die (qualified_type, context_die);
10560 return lookup_type_die (qualified_type);
10562 else if (is_const_type < TYPE_READONLY (dtype)
10563 || is_volatile_type < TYPE_VOLATILE (dtype)
10564 || (is_const_type <= TYPE_READONLY (dtype)
10565 && is_volatile_type <= TYPE_VOLATILE (dtype)
10566 && DECL_ORIGINAL_TYPE (name) != type))
10567 /* cv-unqualified version of named type. Just use the unnamed
10568 type to which it refers. */
10569 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10570 is_const_type, is_volatile_type,
10571 context_die);
10572 /* Else cv-qualified version of named type; fall through. */
10575 mod_scope = scope_die_for (type, context_die);
10577 if (is_const_type
10578 /* If both is_const_type and is_volatile_type, prefer the path
10579 which leads to a qualified type. */
10580 && (!is_volatile_type
10581 || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10582 || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10584 mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10585 sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10587 else if (is_volatile_type)
10589 mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10590 sub_die = modified_type_die (type, is_const_type, 0, context_die);
10592 else if (code == POINTER_TYPE)
10594 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10595 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10596 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10597 item_type = TREE_TYPE (type);
10598 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10599 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10600 TYPE_ADDR_SPACE (item_type));
10602 else if (code == REFERENCE_TYPE)
10604 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10605 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10606 type);
10607 else
10608 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10609 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10610 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10611 item_type = TREE_TYPE (type);
10612 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10613 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10614 TYPE_ADDR_SPACE (item_type));
10616 else if (code == INTEGER_TYPE
10617 && TREE_TYPE (type) != NULL_TREE
10618 && subrange_type_for_debug_p (type, &low, &high))
10620 mod_type_die = subrange_type_die (type, low, high, context_die);
10621 item_type = TREE_TYPE (type);
10623 else if (is_base_type (type))
10624 mod_type_die = base_type_die (type);
10625 else
10627 gen_type_die (type, context_die);
10629 /* We have to get the type_main_variant here (and pass that to the
10630 `lookup_type_die' routine) because the ..._TYPE node we have
10631 might simply be a *copy* of some original type node (where the
10632 copy was created to help us keep track of typedef names) and
10633 that copy might have a different TYPE_UID from the original
10634 ..._TYPE node. */
10635 if (TREE_CODE (type) != VECTOR_TYPE)
10636 return lookup_type_die (type_main_variant (type));
10637 else
10638 /* Vectors have the debugging information in the type,
10639 not the main variant. */
10640 return lookup_type_die (type);
10643 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10644 don't output a DW_TAG_typedef, since there isn't one in the
10645 user's program; just attach a DW_AT_name to the type.
10646 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10647 if the base type already has the same name. */
10648 if (name
10649 && ((TREE_CODE (name) != TYPE_DECL
10650 && (qualified_type == TYPE_MAIN_VARIANT (type)
10651 || (!is_const_type && !is_volatile_type)))
10652 || (TREE_CODE (name) == TYPE_DECL
10653 && TREE_TYPE (name) == qualified_type
10654 && DECL_NAME (name))))
10656 if (TREE_CODE (name) == TYPE_DECL)
10657 /* Could just call add_name_and_src_coords_attributes here,
10658 but since this is a builtin type it doesn't have any
10659 useful source coordinates anyway. */
10660 name = DECL_NAME (name);
10661 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10663 /* This probably indicates a bug. */
10664 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10666 name = TYPE_IDENTIFIER (type);
10667 add_name_attribute (mod_type_die,
10668 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10671 if (qualified_type)
10672 equate_type_number_to_die (qualified_type, mod_type_die);
10674 if (item_type)
10675 /* We must do this after the equate_type_number_to_die call, in case
10676 this is a recursive type. This ensures that the modified_type_die
10677 recursion will terminate even if the type is recursive. Recursive
10678 types are possible in Ada. */
10679 sub_die = modified_type_die (item_type,
10680 TYPE_READONLY (item_type),
10681 TYPE_VOLATILE (item_type),
10682 context_die);
10684 if (sub_die != NULL)
10685 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10687 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10688 if (TYPE_ARTIFICIAL (type))
10689 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10691 return mod_type_die;
10694 /* Generate DIEs for the generic parameters of T.
10695 T must be either a generic type or a generic function.
10696 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10698 static void
10699 gen_generic_params_dies (tree t)
10701 tree parms, args;
10702 int parms_num, i;
10703 dw_die_ref die = NULL;
10704 int non_default;
10706 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10707 return;
10709 if (TYPE_P (t))
10710 die = lookup_type_die (t);
10711 else if (DECL_P (t))
10712 die = lookup_decl_die (t);
10714 gcc_assert (die);
10716 parms = lang_hooks.get_innermost_generic_parms (t);
10717 if (!parms)
10718 /* T has no generic parameter. It means T is neither a generic type
10719 or function. End of story. */
10720 return;
10722 parms_num = TREE_VEC_LENGTH (parms);
10723 args = lang_hooks.get_innermost_generic_args (t);
10724 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10725 non_default = int_cst_value (TREE_CHAIN (args));
10726 else
10727 non_default = TREE_VEC_LENGTH (args);
10728 for (i = 0; i < parms_num; i++)
10730 tree parm, arg, arg_pack_elems;
10731 dw_die_ref parm_die;
10733 parm = TREE_VEC_ELT (parms, i);
10734 arg = TREE_VEC_ELT (args, i);
10735 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10736 gcc_assert (parm && TREE_VALUE (parm) && arg);
10738 if (parm && TREE_VALUE (parm) && arg)
10740 /* If PARM represents a template parameter pack,
10741 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10742 by DW_TAG_template_*_parameter DIEs for the argument
10743 pack elements of ARG. Note that ARG would then be
10744 an argument pack. */
10745 if (arg_pack_elems)
10746 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10747 arg_pack_elems,
10748 die);
10749 else
10750 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10751 true /* emit name */, die);
10752 if (i >= non_default)
10753 add_AT_flag (parm_die, DW_AT_default_value, 1);
10758 /* Create and return a DIE for PARM which should be
10759 the representation of a generic type parameter.
10760 For instance, in the C++ front end, PARM would be a template parameter.
10761 ARG is the argument to PARM.
10762 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10763 name of the PARM.
10764 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10765 as a child node. */
10767 static dw_die_ref
10768 generic_parameter_die (tree parm, tree arg,
10769 bool emit_name_p,
10770 dw_die_ref parent_die)
10772 dw_die_ref tmpl_die = NULL;
10773 const char *name = NULL;
10775 if (!parm || !DECL_NAME (parm) || !arg)
10776 return NULL;
10778 /* We support non-type generic parameters and arguments,
10779 type generic parameters and arguments, as well as
10780 generic generic parameters (a.k.a. template template parameters in C++)
10781 and arguments. */
10782 if (TREE_CODE (parm) == PARM_DECL)
10783 /* PARM is a nontype generic parameter */
10784 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10785 else if (TREE_CODE (parm) == TYPE_DECL)
10786 /* PARM is a type generic parameter. */
10787 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10788 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10789 /* PARM is a generic generic parameter.
10790 Its DIE is a GNU extension. It shall have a
10791 DW_AT_name attribute to represent the name of the template template
10792 parameter, and a DW_AT_GNU_template_name attribute to represent the
10793 name of the template template argument. */
10794 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10795 parent_die, parm);
10796 else
10797 gcc_unreachable ();
10799 if (tmpl_die)
10801 tree tmpl_type;
10803 /* If PARM is a generic parameter pack, it means we are
10804 emitting debug info for a template argument pack element.
10805 In other terms, ARG is a template argument pack element.
10806 In that case, we don't emit any DW_AT_name attribute for
10807 the die. */
10808 if (emit_name_p)
10810 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10811 gcc_assert (name);
10812 add_AT_string (tmpl_die, DW_AT_name, name);
10815 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10817 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10818 TMPL_DIE should have a child DW_AT_type attribute that is set
10819 to the type of the argument to PARM, which is ARG.
10820 If PARM is a type generic parameter, TMPL_DIE should have a
10821 child DW_AT_type that is set to ARG. */
10822 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10823 add_type_attribute (tmpl_die, tmpl_type, 0,
10824 TREE_THIS_VOLATILE (tmpl_type),
10825 parent_die);
10827 else
10829 /* So TMPL_DIE is a DIE representing a
10830 a generic generic template parameter, a.k.a template template
10831 parameter in C++ and arg is a template. */
10833 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10834 to the name of the argument. */
10835 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10836 if (name)
10837 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10840 if (TREE_CODE (parm) == PARM_DECL)
10841 /* So PARM is a non-type generic parameter.
10842 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10843 attribute of TMPL_DIE which value represents the value
10844 of ARG.
10845 We must be careful here:
10846 The value of ARG might reference some function decls.
10847 We might currently be emitting debug info for a generic
10848 type and types are emitted before function decls, we don't
10849 know if the function decls referenced by ARG will actually be
10850 emitted after cgraph computations.
10851 So must defer the generation of the DW_AT_const_value to
10852 after cgraph is ready. */
10853 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10856 return tmpl_die;
10859 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10860 PARM_PACK must be a template parameter pack. The returned DIE
10861 will be child DIE of PARENT_DIE. */
10863 static dw_die_ref
10864 template_parameter_pack_die (tree parm_pack,
10865 tree parm_pack_args,
10866 dw_die_ref parent_die)
10868 dw_die_ref die;
10869 int j;
10871 gcc_assert (parent_die && parm_pack);
10873 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10874 add_name_and_src_coords_attributes (die, parm_pack);
10875 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10876 generic_parameter_die (parm_pack,
10877 TREE_VEC_ELT (parm_pack_args, j),
10878 false /* Don't emit DW_AT_name */,
10879 die);
10880 return die;
10883 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10884 an enumerated type. */
10886 static inline int
10887 type_is_enum (const_tree type)
10889 return TREE_CODE (type) == ENUMERAL_TYPE;
10892 /* Return the DBX register number described by a given RTL node. */
10894 static unsigned int
10895 dbx_reg_number (const_rtx rtl)
10897 unsigned regno = REGNO (rtl);
10899 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10901 #ifdef LEAF_REG_REMAP
10902 if (crtl->uses_only_leaf_regs)
10904 int leaf_reg = LEAF_REG_REMAP (regno);
10905 if (leaf_reg != -1)
10906 regno = (unsigned) leaf_reg;
10908 #endif
10910 regno = DBX_REGISTER_NUMBER (regno);
10911 gcc_assert (regno != INVALID_REGNUM);
10912 return regno;
10915 /* Optionally add a DW_OP_piece term to a location description expression.
10916 DW_OP_piece is only added if the location description expression already
10917 doesn't end with DW_OP_piece. */
10919 static void
10920 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10922 dw_loc_descr_ref loc;
10924 if (*list_head != NULL)
10926 /* Find the end of the chain. */
10927 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10930 if (loc->dw_loc_opc != DW_OP_piece)
10931 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10935 /* Return a location descriptor that designates a machine register or
10936 zero if there is none. */
10938 static dw_loc_descr_ref
10939 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10941 rtx regs;
10943 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10944 return 0;
10946 /* We only use "frame base" when we're sure we're talking about the
10947 post-prologue local stack frame. We do this by *not* running
10948 register elimination until this point, and recognizing the special
10949 argument pointer and soft frame pointer rtx's.
10950 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10951 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10952 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10954 dw_loc_descr_ref result = NULL;
10956 if (dwarf_version >= 4 || !dwarf_strict)
10958 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10959 initialized);
10960 if (result)
10961 add_loc_descr (&result,
10962 new_loc_descr (DW_OP_stack_value, 0, 0));
10964 return result;
10967 regs = targetm.dwarf_register_span (rtl);
10969 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10970 return multiple_reg_loc_descriptor (rtl, regs, initialized);
10971 else
10973 unsigned int dbx_regnum = dbx_reg_number (rtl);
10974 if (dbx_regnum == IGNORED_DWARF_REGNUM)
10975 return 0;
10976 return one_reg_loc_descriptor (dbx_regnum, initialized);
10980 /* Return a location descriptor that designates a machine register for
10981 a given hard register number. */
10983 static dw_loc_descr_ref
10984 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10986 dw_loc_descr_ref reg_loc_descr;
10988 if (regno <= 31)
10989 reg_loc_descr
10990 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10991 else
10992 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10994 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10995 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10997 return reg_loc_descr;
11000 /* Given an RTL of a register, return a location descriptor that
11001 designates a value that spans more than one register. */
11003 static dw_loc_descr_ref
11004 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11005 enum var_init_status initialized)
11007 int size, i;
11008 dw_loc_descr_ref loc_result = NULL;
11010 /* Simple, contiguous registers. */
11011 if (regs == NULL_RTX)
11013 unsigned reg = REGNO (rtl);
11014 int nregs;
11016 #ifdef LEAF_REG_REMAP
11017 if (crtl->uses_only_leaf_regs)
11019 int leaf_reg = LEAF_REG_REMAP (reg);
11020 if (leaf_reg != -1)
11021 reg = (unsigned) leaf_reg;
11023 #endif
11025 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11026 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11028 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11030 loc_result = NULL;
11031 while (nregs--)
11033 dw_loc_descr_ref t;
11035 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11036 VAR_INIT_STATUS_INITIALIZED);
11037 add_loc_descr (&loc_result, t);
11038 add_loc_descr_op_piece (&loc_result, size);
11039 ++reg;
11041 return loc_result;
11044 /* Now onto stupid register sets in non contiguous locations. */
11046 gcc_assert (GET_CODE (regs) == PARALLEL);
11048 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11049 loc_result = NULL;
11051 for (i = 0; i < XVECLEN (regs, 0); ++i)
11053 dw_loc_descr_ref t;
11055 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11056 VAR_INIT_STATUS_INITIALIZED);
11057 add_loc_descr (&loc_result, t);
11058 add_loc_descr_op_piece (&loc_result, size);
11061 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11062 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11063 return loc_result;
11066 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11068 /* Return a location descriptor that designates a constant i,
11069 as a compound operation from constant (i >> shift), constant shift
11070 and DW_OP_shl. */
11072 static dw_loc_descr_ref
11073 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11075 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11076 add_loc_descr (&ret, int_loc_descriptor (shift));
11077 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11078 return ret;
11081 /* Return a location descriptor that designates a constant. */
11083 static dw_loc_descr_ref
11084 int_loc_descriptor (HOST_WIDE_INT i)
11086 enum dwarf_location_atom op;
11088 /* Pick the smallest representation of a constant, rather than just
11089 defaulting to the LEB encoding. */
11090 if (i >= 0)
11092 int clz = clz_hwi (i);
11093 int ctz = ctz_hwi (i);
11094 if (i <= 31)
11095 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11096 else if (i <= 0xff)
11097 op = DW_OP_const1u;
11098 else if (i <= 0xffff)
11099 op = DW_OP_const2u;
11100 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11101 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11102 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11103 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11104 while DW_OP_const4u is 5 bytes. */
11105 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11106 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11107 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11108 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11109 while DW_OP_const4u is 5 bytes. */
11110 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11111 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11112 op = DW_OP_const4u;
11113 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11114 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11115 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11116 while DW_OP_constu of constant >= 0x100000000 takes at least
11117 6 bytes. */
11118 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11119 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11120 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11121 >= HOST_BITS_PER_WIDE_INT)
11122 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11123 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11124 while DW_OP_constu takes in this case at least 6 bytes. */
11125 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11126 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11127 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11128 && size_of_uleb128 (i) > 6)
11129 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11130 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11131 else
11132 op = DW_OP_constu;
11134 else
11136 if (i >= -0x80)
11137 op = DW_OP_const1s;
11138 else if (i >= -0x8000)
11139 op = DW_OP_const2s;
11140 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11142 if (size_of_int_loc_descriptor (i) < 5)
11144 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11145 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11146 return ret;
11148 op = DW_OP_const4s;
11150 else
11152 if (size_of_int_loc_descriptor (i)
11153 < (unsigned long) 1 + size_of_sleb128 (i))
11155 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11156 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11157 return ret;
11159 op = DW_OP_consts;
11163 return new_loc_descr (op, i, 0);
11166 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11167 without actually allocating it. */
11169 static unsigned long
11170 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11172 return size_of_int_loc_descriptor (i >> shift)
11173 + size_of_int_loc_descriptor (shift)
11174 + 1;
11177 /* Return size_of_locs (int_loc_descriptor (i)) without
11178 actually allocating it. */
11180 static unsigned long
11181 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11183 unsigned long s;
11185 if (i >= 0)
11187 int clz, ctz;
11188 if (i <= 31)
11189 return 1;
11190 else if (i <= 0xff)
11191 return 2;
11192 else if (i <= 0xffff)
11193 return 3;
11194 clz = clz_hwi (i);
11195 ctz = ctz_hwi (i);
11196 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11197 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11198 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11199 - clz - 5);
11200 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11201 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11202 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11203 - clz - 8);
11204 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11205 return 5;
11206 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11207 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11208 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11209 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11210 - clz - 8);
11211 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11212 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11213 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11214 - clz - 16);
11215 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11216 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11217 && s > 6)
11218 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11219 - clz - 32);
11220 else
11221 return 1 + s;
11223 else
11225 if (i >= -0x80)
11226 return 2;
11227 else if (i >= -0x8000)
11228 return 3;
11229 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11231 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11233 s = size_of_int_loc_descriptor (-i) + 1;
11234 if (s < 5)
11235 return s;
11237 return 5;
11239 else
11241 unsigned long r = 1 + size_of_sleb128 (i);
11242 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11244 s = size_of_int_loc_descriptor (-i) + 1;
11245 if (s < r)
11246 return s;
11248 return r;
11253 /* Return loc description representing "address" of integer value.
11254 This can appear only as toplevel expression. */
11256 static dw_loc_descr_ref
11257 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11259 int litsize;
11260 dw_loc_descr_ref loc_result = NULL;
11262 if (!(dwarf_version >= 4 || !dwarf_strict))
11263 return NULL;
11265 litsize = size_of_int_loc_descriptor (i);
11266 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11267 is more compact. For DW_OP_stack_value we need:
11268 litsize + 1 (DW_OP_stack_value)
11269 and for DW_OP_implicit_value:
11270 1 (DW_OP_implicit_value) + 1 (length) + size. */
11271 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11273 loc_result = int_loc_descriptor (i);
11274 add_loc_descr (&loc_result,
11275 new_loc_descr (DW_OP_stack_value, 0, 0));
11276 return loc_result;
11279 loc_result = new_loc_descr (DW_OP_implicit_value,
11280 size, 0);
11281 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11282 loc_result->dw_loc_oprnd2.v.val_int = i;
11283 return loc_result;
11286 /* Return a location descriptor that designates a base+offset location. */
11288 static dw_loc_descr_ref
11289 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11290 enum var_init_status initialized)
11292 unsigned int regno;
11293 dw_loc_descr_ref result;
11294 dw_fde_ref fde = cfun->fde;
11296 /* We only use "frame base" when we're sure we're talking about the
11297 post-prologue local stack frame. We do this by *not* running
11298 register elimination until this point, and recognizing the special
11299 argument pointer and soft frame pointer rtx's. */
11300 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11302 rtx elim = (ira_use_lra_p
11303 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11304 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11306 if (elim != reg)
11308 if (GET_CODE (elim) == PLUS)
11310 offset += INTVAL (XEXP (elim, 1));
11311 elim = XEXP (elim, 0);
11313 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11314 && (elim == hard_frame_pointer_rtx
11315 || elim == stack_pointer_rtx))
11316 || elim == (frame_pointer_needed
11317 ? hard_frame_pointer_rtx
11318 : stack_pointer_rtx));
11320 /* If drap register is used to align stack, use frame
11321 pointer + offset to access stack variables. If stack
11322 is aligned without drap, use stack pointer + offset to
11323 access stack variables. */
11324 if (crtl->stack_realign_tried
11325 && reg == frame_pointer_rtx)
11327 int base_reg
11328 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11329 ? HARD_FRAME_POINTER_REGNUM
11330 : REGNO (elim));
11331 return new_reg_loc_descr (base_reg, offset);
11334 gcc_assert (frame_pointer_fb_offset_valid);
11335 offset += frame_pointer_fb_offset;
11336 return new_loc_descr (DW_OP_fbreg, offset, 0);
11340 regno = REGNO (reg);
11341 #ifdef LEAF_REG_REMAP
11342 if (crtl->uses_only_leaf_regs)
11344 int leaf_reg = LEAF_REG_REMAP (regno);
11345 if (leaf_reg != -1)
11346 regno = (unsigned) leaf_reg;
11348 #endif
11349 regno = DWARF_FRAME_REGNUM (regno);
11351 if (!optimize && fde
11352 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11354 /* Use cfa+offset to represent the location of arguments passed
11355 on the stack when drap is used to align stack.
11356 Only do this when not optimizing, for optimized code var-tracking
11357 is supposed to track where the arguments live and the register
11358 used as vdrap or drap in some spot might be used for something
11359 else in other part of the routine. */
11360 return new_loc_descr (DW_OP_fbreg, offset, 0);
11363 if (regno <= 31)
11364 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11365 offset, 0);
11366 else
11367 result = new_loc_descr (DW_OP_bregx, regno, offset);
11369 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11370 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11372 return result;
11375 /* Return true if this RTL expression describes a base+offset calculation. */
11377 static inline int
11378 is_based_loc (const_rtx rtl)
11380 return (GET_CODE (rtl) == PLUS
11381 && ((REG_P (XEXP (rtl, 0))
11382 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11383 && CONST_INT_P (XEXP (rtl, 1)))));
11386 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11387 failed. */
11389 static dw_loc_descr_ref
11390 tls_mem_loc_descriptor (rtx mem)
11392 tree base;
11393 dw_loc_descr_ref loc_result;
11395 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11396 return NULL;
11398 base = get_base_address (MEM_EXPR (mem));
11399 if (base == NULL
11400 || TREE_CODE (base) != VAR_DECL
11401 || !DECL_THREAD_LOCAL_P (base))
11402 return NULL;
11404 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11405 if (loc_result == NULL)
11406 return NULL;
11408 if (MEM_OFFSET (mem))
11409 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11411 return loc_result;
11414 /* Output debug info about reason why we failed to expand expression as dwarf
11415 expression. */
11417 static void
11418 expansion_failed (tree expr, rtx rtl, char const *reason)
11420 if (dump_file && (dump_flags & TDF_DETAILS))
11422 fprintf (dump_file, "Failed to expand as dwarf: ");
11423 if (expr)
11424 print_generic_expr (dump_file, expr, dump_flags);
11425 if (rtl)
11427 fprintf (dump_file, "\n");
11428 print_rtl (dump_file, rtl);
11430 fprintf (dump_file, "\nReason: %s\n", reason);
11434 /* Helper function for const_ok_for_output, called either directly
11435 or via for_each_rtx. */
11437 static int
11438 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11440 rtx rtl = *rtlp;
11442 if (GET_CODE (rtl) == UNSPEC)
11444 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11445 we can't express it in the debug info. */
11446 #ifdef ENABLE_CHECKING
11447 /* Don't complain about TLS UNSPECs, those are just too hard to
11448 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11449 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11450 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11451 if (XVECLEN (rtl, 0) == 0
11452 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11453 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11454 inform (current_function_decl
11455 ? DECL_SOURCE_LOCATION (current_function_decl)
11456 : UNKNOWN_LOCATION,
11457 #if NUM_UNSPEC_VALUES > 0
11458 "non-delegitimized UNSPEC %s (%d) found in variable location",
11459 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11460 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11461 XINT (rtl, 1));
11462 #else
11463 "non-delegitimized UNSPEC %d found in variable location",
11464 XINT (rtl, 1));
11465 #endif
11466 #endif
11467 expansion_failed (NULL_TREE, rtl,
11468 "UNSPEC hasn't been delegitimized.\n");
11469 return 1;
11472 if (targetm.const_not_ok_for_debug_p (rtl))
11474 expansion_failed (NULL_TREE, rtl,
11475 "Expression rejected for debug by the backend.\n");
11476 return 1;
11479 /* FIXME: Refer to PR60655. It is possible for simplification
11480 of rtl expressions in var tracking to produce such expressions.
11481 We should really identify / validate expressions
11482 enclosed in CONST that can be handled by assemblers on various
11483 targets and only handle legitimate cases here. */
11484 if (GET_CODE (rtl) != SYMBOL_REF)
11486 if (GET_CODE (rtl) == NOT)
11487 return 1;
11489 return 0;
11492 if (CONSTANT_POOL_ADDRESS_P (rtl))
11494 bool marked;
11495 get_pool_constant_mark (rtl, &marked);
11496 /* If all references to this pool constant were optimized away,
11497 it was not output and thus we can't represent it. */
11498 if (!marked)
11500 expansion_failed (NULL_TREE, rtl,
11501 "Constant was removed from constant pool.\n");
11502 return 1;
11506 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11507 return 1;
11509 /* Avoid references to external symbols in debug info, on several targets
11510 the linker might even refuse to link when linking a shared library,
11511 and in many other cases the relocations for .debug_info/.debug_loc are
11512 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11513 to be defined within the same shared library or executable are fine. */
11514 if (SYMBOL_REF_EXTERNAL_P (rtl))
11516 tree decl = SYMBOL_REF_DECL (rtl);
11518 if (decl == NULL || !targetm.binds_local_p (decl))
11520 expansion_failed (NULL_TREE, rtl,
11521 "Symbol not defined in current TU.\n");
11522 return 1;
11526 return 0;
11529 /* Return true if constant RTL can be emitted in DW_OP_addr or
11530 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11531 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11533 static bool
11534 const_ok_for_output (rtx rtl)
11536 if (GET_CODE (rtl) == SYMBOL_REF)
11537 return const_ok_for_output_1 (&rtl, NULL) == 0;
11539 if (GET_CODE (rtl) == CONST)
11540 return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11542 return true;
11545 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11546 if possible, NULL otherwise. */
11548 static dw_die_ref
11549 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11551 dw_die_ref type_die;
11552 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11554 if (type == NULL)
11555 return NULL;
11556 switch (TREE_CODE (type))
11558 case INTEGER_TYPE:
11559 case REAL_TYPE:
11560 break;
11561 default:
11562 return NULL;
11564 type_die = lookup_type_die (type);
11565 if (!type_die)
11566 type_die = modified_type_die (type, false, false, comp_unit_die ());
11567 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11568 return NULL;
11569 return type_die;
11572 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11573 type matching MODE, or, if MODE is narrower than or as wide as
11574 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11575 possible. */
11577 static dw_loc_descr_ref
11578 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11580 enum machine_mode outer_mode = mode;
11581 dw_die_ref type_die;
11582 dw_loc_descr_ref cvt;
11584 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11586 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11587 return op;
11589 type_die = base_type_for_mode (outer_mode, 1);
11590 if (type_die == NULL)
11591 return NULL;
11592 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11593 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11594 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11595 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11596 add_loc_descr (&op, cvt);
11597 return op;
11600 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11602 static dw_loc_descr_ref
11603 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11604 dw_loc_descr_ref op1)
11606 dw_loc_descr_ref ret = op0;
11607 add_loc_descr (&ret, op1);
11608 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11609 if (STORE_FLAG_VALUE != 1)
11611 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11612 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11614 return ret;
11617 /* Return location descriptor for signed comparison OP RTL. */
11619 static dw_loc_descr_ref
11620 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11621 enum machine_mode mem_mode)
11623 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11624 dw_loc_descr_ref op0, op1;
11625 int shift;
11627 if (op_mode == VOIDmode)
11628 op_mode = GET_MODE (XEXP (rtl, 1));
11629 if (op_mode == VOIDmode)
11630 return NULL;
11632 if (dwarf_strict
11633 && (GET_MODE_CLASS (op_mode) != MODE_INT
11634 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11635 return NULL;
11637 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11638 VAR_INIT_STATUS_INITIALIZED);
11639 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11640 VAR_INIT_STATUS_INITIALIZED);
11642 if (op0 == NULL || op1 == NULL)
11643 return NULL;
11645 if (GET_MODE_CLASS (op_mode) != MODE_INT
11646 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11647 return compare_loc_descriptor (op, op0, op1);
11649 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11651 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11652 dw_loc_descr_ref cvt;
11654 if (type_die == NULL)
11655 return NULL;
11656 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11657 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11658 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11659 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11660 add_loc_descr (&op0, cvt);
11661 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11662 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11663 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11664 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11665 add_loc_descr (&op1, cvt);
11666 return compare_loc_descriptor (op, op0, op1);
11669 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11670 /* For eq/ne, if the operands are known to be zero-extended,
11671 there is no need to do the fancy shifting up. */
11672 if (op == DW_OP_eq || op == DW_OP_ne)
11674 dw_loc_descr_ref last0, last1;
11675 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11677 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11679 /* deref_size zero extends, and for constants we can check
11680 whether they are zero extended or not. */
11681 if (((last0->dw_loc_opc == DW_OP_deref_size
11682 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11683 || (CONST_INT_P (XEXP (rtl, 0))
11684 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11685 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11686 && ((last1->dw_loc_opc == DW_OP_deref_size
11687 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11688 || (CONST_INT_P (XEXP (rtl, 1))
11689 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11690 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11691 return compare_loc_descriptor (op, op0, op1);
11693 /* EQ/NE comparison against constant in narrower type than
11694 DWARF2_ADDR_SIZE can be performed either as
11695 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11696 DW_OP_{eq,ne}
11698 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11699 DW_OP_{eq,ne}. Pick whatever is shorter. */
11700 if (CONST_INT_P (XEXP (rtl, 1))
11701 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11702 && (size_of_int_loc_descriptor (shift) + 1
11703 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11704 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11705 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11706 & GET_MODE_MASK (op_mode))))
11708 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11709 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11710 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11711 & GET_MODE_MASK (op_mode));
11712 return compare_loc_descriptor (op, op0, op1);
11715 add_loc_descr (&op0, int_loc_descriptor (shift));
11716 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11717 if (CONST_INT_P (XEXP (rtl, 1)))
11718 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11719 else
11721 add_loc_descr (&op1, int_loc_descriptor (shift));
11722 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11724 return compare_loc_descriptor (op, op0, op1);
11727 /* Return location descriptor for unsigned comparison OP RTL. */
11729 static dw_loc_descr_ref
11730 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11731 enum machine_mode mem_mode)
11733 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11734 dw_loc_descr_ref op0, op1;
11736 if (op_mode == VOIDmode)
11737 op_mode = GET_MODE (XEXP (rtl, 1));
11738 if (op_mode == VOIDmode)
11739 return NULL;
11740 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11741 return NULL;
11743 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11744 return NULL;
11746 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11747 VAR_INIT_STATUS_INITIALIZED);
11748 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11749 VAR_INIT_STATUS_INITIALIZED);
11751 if (op0 == NULL || op1 == NULL)
11752 return NULL;
11754 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11756 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11757 dw_loc_descr_ref last0, last1;
11758 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11760 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11762 if (CONST_INT_P (XEXP (rtl, 0)))
11763 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11764 /* deref_size zero extends, so no need to mask it again. */
11765 else if (last0->dw_loc_opc != DW_OP_deref_size
11766 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11768 add_loc_descr (&op0, int_loc_descriptor (mask));
11769 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11771 if (CONST_INT_P (XEXP (rtl, 1)))
11772 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11773 /* deref_size zero extends, so no need to mask it again. */
11774 else if (last1->dw_loc_opc != DW_OP_deref_size
11775 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11777 add_loc_descr (&op1, int_loc_descriptor (mask));
11778 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11781 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11783 HOST_WIDE_INT bias = 1;
11784 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11785 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11786 if (CONST_INT_P (XEXP (rtl, 1)))
11787 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11788 + INTVAL (XEXP (rtl, 1)));
11789 else
11790 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11791 bias, 0));
11793 return compare_loc_descriptor (op, op0, op1);
11796 /* Return location descriptor for {U,S}{MIN,MAX}. */
11798 static dw_loc_descr_ref
11799 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11800 enum machine_mode mem_mode)
11802 enum dwarf_location_atom op;
11803 dw_loc_descr_ref op0, op1, ret;
11804 dw_loc_descr_ref bra_node, drop_node;
11806 if (dwarf_strict
11807 && (GET_MODE_CLASS (mode) != MODE_INT
11808 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11809 return NULL;
11811 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11812 VAR_INIT_STATUS_INITIALIZED);
11813 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11814 VAR_INIT_STATUS_INITIALIZED);
11816 if (op0 == NULL || op1 == NULL)
11817 return NULL;
11819 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11820 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11821 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11822 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11824 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11826 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11827 add_loc_descr (&op0, int_loc_descriptor (mask));
11828 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11829 add_loc_descr (&op1, int_loc_descriptor (mask));
11830 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11832 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11834 HOST_WIDE_INT bias = 1;
11835 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11836 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11837 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11840 else if (GET_MODE_CLASS (mode) == MODE_INT
11841 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11843 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11844 add_loc_descr (&op0, int_loc_descriptor (shift));
11845 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11846 add_loc_descr (&op1, int_loc_descriptor (shift));
11847 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11849 else if (GET_MODE_CLASS (mode) == MODE_INT
11850 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11852 dw_die_ref type_die = base_type_for_mode (mode, 0);
11853 dw_loc_descr_ref cvt;
11854 if (type_die == NULL)
11855 return NULL;
11856 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11857 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11858 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11859 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11860 add_loc_descr (&op0, cvt);
11861 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11862 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11863 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11864 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11865 add_loc_descr (&op1, cvt);
11868 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11869 op = DW_OP_lt;
11870 else
11871 op = DW_OP_gt;
11872 ret = op0;
11873 add_loc_descr (&ret, op1);
11874 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11875 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11876 add_loc_descr (&ret, bra_node);
11877 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11878 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11879 add_loc_descr (&ret, drop_node);
11880 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11881 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11882 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11883 && GET_MODE_CLASS (mode) == MODE_INT
11884 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11885 ret = convert_descriptor_to_mode (mode, ret);
11886 return ret;
11889 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11890 but after converting arguments to type_die, afterwards
11891 convert back to unsigned. */
11893 static dw_loc_descr_ref
11894 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11895 enum machine_mode mode, enum machine_mode mem_mode)
11897 dw_loc_descr_ref cvt, op0, op1;
11899 if (type_die == NULL)
11900 return NULL;
11901 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11902 VAR_INIT_STATUS_INITIALIZED);
11903 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11904 VAR_INIT_STATUS_INITIALIZED);
11905 if (op0 == NULL || op1 == NULL)
11906 return NULL;
11907 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11908 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11909 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11910 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11911 add_loc_descr (&op0, cvt);
11912 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11913 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11914 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11915 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11916 add_loc_descr (&op1, cvt);
11917 add_loc_descr (&op0, op1);
11918 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11919 return convert_descriptor_to_mode (mode, op0);
11922 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11923 const0 is DW_OP_lit0 or corresponding typed constant,
11924 const1 is DW_OP_lit1 or corresponding typed constant
11925 and constMSB is constant with just the MSB bit set
11926 for the mode):
11927 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11928 L1: const0 DW_OP_swap
11929 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11930 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11931 L3: DW_OP_drop
11932 L4: DW_OP_nop
11934 CTZ is similar:
11935 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11936 L1: const0 DW_OP_swap
11937 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11938 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11939 L3: DW_OP_drop
11940 L4: DW_OP_nop
11942 FFS is similar:
11943 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11944 L1: const1 DW_OP_swap
11945 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11946 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11947 L3: DW_OP_drop
11948 L4: DW_OP_nop */
11950 static dw_loc_descr_ref
11951 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11952 enum machine_mode mem_mode)
11954 dw_loc_descr_ref op0, ret, tmp;
11955 HOST_WIDE_INT valv;
11956 dw_loc_descr_ref l1jump, l1label;
11957 dw_loc_descr_ref l2jump, l2label;
11958 dw_loc_descr_ref l3jump, l3label;
11959 dw_loc_descr_ref l4jump, l4label;
11960 rtx msb;
11962 if (GET_MODE_CLASS (mode) != MODE_INT
11963 || GET_MODE (XEXP (rtl, 0)) != mode)
11964 return NULL;
11966 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11967 VAR_INIT_STATUS_INITIALIZED);
11968 if (op0 == NULL)
11969 return NULL;
11970 ret = op0;
11971 if (GET_CODE (rtl) == CLZ)
11973 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11974 valv = GET_MODE_BITSIZE (mode);
11976 else if (GET_CODE (rtl) == FFS)
11977 valv = 0;
11978 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11979 valv = GET_MODE_BITSIZE (mode);
11980 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11981 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11982 add_loc_descr (&ret, l1jump);
11983 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11984 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11985 VAR_INIT_STATUS_INITIALIZED);
11986 if (tmp == NULL)
11987 return NULL;
11988 add_loc_descr (&ret, tmp);
11989 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11990 add_loc_descr (&ret, l4jump);
11991 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11992 ? const1_rtx : const0_rtx,
11993 mode, mem_mode,
11994 VAR_INIT_STATUS_INITIALIZED);
11995 if (l1label == NULL)
11996 return NULL;
11997 add_loc_descr (&ret, l1label);
11998 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11999 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12000 add_loc_descr (&ret, l2label);
12001 if (GET_CODE (rtl) != CLZ)
12002 msb = const1_rtx;
12003 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12004 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12005 << (GET_MODE_BITSIZE (mode) - 1));
12006 else
12007 msb = immed_wide_int_const
12008 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12009 GET_MODE_PRECISION (mode)), mode);
12010 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12011 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12012 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12013 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12014 else
12015 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12016 VAR_INIT_STATUS_INITIALIZED);
12017 if (tmp == NULL)
12018 return NULL;
12019 add_loc_descr (&ret, tmp);
12020 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12021 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12022 add_loc_descr (&ret, l3jump);
12023 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12024 VAR_INIT_STATUS_INITIALIZED);
12025 if (tmp == NULL)
12026 return NULL;
12027 add_loc_descr (&ret, tmp);
12028 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12029 ? DW_OP_shl : DW_OP_shr, 0, 0));
12030 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12031 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12032 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12033 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12034 add_loc_descr (&ret, l2jump);
12035 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12036 add_loc_descr (&ret, l3label);
12037 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12038 add_loc_descr (&ret, l4label);
12039 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12040 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12041 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12042 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12043 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12044 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12045 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12046 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12047 return ret;
12050 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12051 const1 is DW_OP_lit1 or corresponding typed constant):
12052 const0 DW_OP_swap
12053 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12054 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12055 L2: DW_OP_drop
12057 PARITY is similar:
12058 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12059 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12060 L2: DW_OP_drop */
12062 static dw_loc_descr_ref
12063 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12064 enum machine_mode mem_mode)
12066 dw_loc_descr_ref op0, ret, tmp;
12067 dw_loc_descr_ref l1jump, l1label;
12068 dw_loc_descr_ref l2jump, l2label;
12070 if (GET_MODE_CLASS (mode) != MODE_INT
12071 || GET_MODE (XEXP (rtl, 0)) != mode)
12072 return NULL;
12074 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12075 VAR_INIT_STATUS_INITIALIZED);
12076 if (op0 == NULL)
12077 return NULL;
12078 ret = op0;
12079 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12080 VAR_INIT_STATUS_INITIALIZED);
12081 if (tmp == NULL)
12082 return NULL;
12083 add_loc_descr (&ret, tmp);
12084 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12085 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12086 add_loc_descr (&ret, l1label);
12087 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12088 add_loc_descr (&ret, l2jump);
12089 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12090 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12091 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12092 VAR_INIT_STATUS_INITIALIZED);
12093 if (tmp == NULL)
12094 return NULL;
12095 add_loc_descr (&ret, tmp);
12096 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12097 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12098 ? DW_OP_plus : DW_OP_xor, 0, 0));
12099 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12100 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12101 VAR_INIT_STATUS_INITIALIZED);
12102 add_loc_descr (&ret, tmp);
12103 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12104 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12105 add_loc_descr (&ret, l1jump);
12106 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12107 add_loc_descr (&ret, l2label);
12108 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12109 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12110 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12111 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12112 return ret;
12115 /* BSWAP (constS is initial shift count, either 56 or 24):
12116 constS const0
12117 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12118 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12119 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12120 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12121 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12123 static dw_loc_descr_ref
12124 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12125 enum machine_mode mem_mode)
12127 dw_loc_descr_ref op0, ret, tmp;
12128 dw_loc_descr_ref l1jump, l1label;
12129 dw_loc_descr_ref l2jump, l2label;
12131 if (GET_MODE_CLASS (mode) != MODE_INT
12132 || BITS_PER_UNIT != 8
12133 || (GET_MODE_BITSIZE (mode) != 32
12134 && GET_MODE_BITSIZE (mode) != 64))
12135 return NULL;
12137 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12138 VAR_INIT_STATUS_INITIALIZED);
12139 if (op0 == NULL)
12140 return NULL;
12142 ret = op0;
12143 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12144 mode, mem_mode,
12145 VAR_INIT_STATUS_INITIALIZED);
12146 if (tmp == NULL)
12147 return NULL;
12148 add_loc_descr (&ret, tmp);
12149 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12150 VAR_INIT_STATUS_INITIALIZED);
12151 if (tmp == NULL)
12152 return NULL;
12153 add_loc_descr (&ret, tmp);
12154 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12155 add_loc_descr (&ret, l1label);
12156 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12157 mode, mem_mode,
12158 VAR_INIT_STATUS_INITIALIZED);
12159 add_loc_descr (&ret, tmp);
12160 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12161 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12162 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12163 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12164 VAR_INIT_STATUS_INITIALIZED);
12165 if (tmp == NULL)
12166 return NULL;
12167 add_loc_descr (&ret, tmp);
12168 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12169 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12170 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12171 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12172 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12173 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12174 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12175 VAR_INIT_STATUS_INITIALIZED);
12176 add_loc_descr (&ret, tmp);
12177 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12178 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12179 add_loc_descr (&ret, l2jump);
12180 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12181 VAR_INIT_STATUS_INITIALIZED);
12182 add_loc_descr (&ret, tmp);
12183 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12184 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12185 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12186 add_loc_descr (&ret, l1jump);
12187 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12188 add_loc_descr (&ret, l2label);
12189 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12190 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12191 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12192 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12193 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12194 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12195 return ret;
12198 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12199 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12200 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12201 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12203 ROTATERT is similar:
12204 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12205 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12206 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12208 static dw_loc_descr_ref
12209 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12210 enum machine_mode mem_mode)
12212 rtx rtlop1 = XEXP (rtl, 1);
12213 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12214 int i;
12216 if (GET_MODE_CLASS (mode) != MODE_INT)
12217 return NULL;
12219 if (GET_MODE (rtlop1) != VOIDmode
12220 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12221 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12222 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12223 VAR_INIT_STATUS_INITIALIZED);
12224 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12225 VAR_INIT_STATUS_INITIALIZED);
12226 if (op0 == NULL || op1 == NULL)
12227 return NULL;
12228 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12229 for (i = 0; i < 2; i++)
12231 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12232 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12233 mode, mem_mode,
12234 VAR_INIT_STATUS_INITIALIZED);
12235 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12236 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12237 ? DW_OP_const4u
12238 : HOST_BITS_PER_WIDE_INT == 64
12239 ? DW_OP_const8u : DW_OP_constu,
12240 GET_MODE_MASK (mode), 0);
12241 else
12242 mask[i] = NULL;
12243 if (mask[i] == NULL)
12244 return NULL;
12245 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12247 ret = op0;
12248 add_loc_descr (&ret, op1);
12249 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12250 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12251 if (GET_CODE (rtl) == ROTATERT)
12253 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12254 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12255 GET_MODE_BITSIZE (mode), 0));
12257 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12258 if (mask[0] != NULL)
12259 add_loc_descr (&ret, mask[0]);
12260 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12261 if (mask[1] != NULL)
12263 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12264 add_loc_descr (&ret, mask[1]);
12265 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12267 if (GET_CODE (rtl) == ROTATE)
12269 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12270 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12271 GET_MODE_BITSIZE (mode), 0));
12273 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12274 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12275 return ret;
12278 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12279 for DEBUG_PARAMETER_REF RTL. */
12281 static dw_loc_descr_ref
12282 parameter_ref_descriptor (rtx rtl)
12284 dw_loc_descr_ref ret;
12285 dw_die_ref ref;
12287 if (dwarf_strict)
12288 return NULL;
12289 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12290 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12291 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12292 if (ref)
12294 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12295 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12296 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12298 else
12300 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12301 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12303 return ret;
12306 /* The following routine converts the RTL for a variable or parameter
12307 (resident in memory) into an equivalent Dwarf representation of a
12308 mechanism for getting the address of that same variable onto the top of a
12309 hypothetical "address evaluation" stack.
12311 When creating memory location descriptors, we are effectively transforming
12312 the RTL for a memory-resident object into its Dwarf postfix expression
12313 equivalent. This routine recursively descends an RTL tree, turning
12314 it into Dwarf postfix code as it goes.
12316 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12318 MEM_MODE is the mode of the memory reference, needed to handle some
12319 autoincrement addressing modes.
12321 Return 0 if we can't represent the location. */
12323 dw_loc_descr_ref
12324 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12325 enum machine_mode mem_mode,
12326 enum var_init_status initialized)
12328 dw_loc_descr_ref mem_loc_result = NULL;
12329 enum dwarf_location_atom op;
12330 dw_loc_descr_ref op0, op1;
12331 rtx inner = NULL_RTX;
12333 if (mode == VOIDmode)
12334 mode = GET_MODE (rtl);
12336 /* Note that for a dynamically sized array, the location we will generate a
12337 description of here will be the lowest numbered location which is
12338 actually within the array. That's *not* necessarily the same as the
12339 zeroth element of the array. */
12341 rtl = targetm.delegitimize_address (rtl);
12343 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12344 return NULL;
12346 switch (GET_CODE (rtl))
12348 case POST_INC:
12349 case POST_DEC:
12350 case POST_MODIFY:
12351 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12353 case SUBREG:
12354 /* The case of a subreg may arise when we have a local (register)
12355 variable or a formal (register) parameter which doesn't quite fill
12356 up an entire register. For now, just assume that it is
12357 legitimate to make the Dwarf info refer to the whole register which
12358 contains the given subreg. */
12359 if (!subreg_lowpart_p (rtl))
12360 break;
12361 inner = SUBREG_REG (rtl);
12362 case TRUNCATE:
12363 if (inner == NULL_RTX)
12364 inner = XEXP (rtl, 0);
12365 if (GET_MODE_CLASS (mode) == MODE_INT
12366 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12367 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12368 #ifdef POINTERS_EXTEND_UNSIGNED
12369 || (mode == Pmode && mem_mode != VOIDmode)
12370 #endif
12372 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12374 mem_loc_result = mem_loc_descriptor (inner,
12375 GET_MODE (inner),
12376 mem_mode, initialized);
12377 break;
12379 if (dwarf_strict)
12380 break;
12381 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12382 break;
12383 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12384 && (GET_MODE_CLASS (mode) != MODE_INT
12385 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12386 break;
12387 else
12389 dw_die_ref type_die;
12390 dw_loc_descr_ref cvt;
12392 mem_loc_result = mem_loc_descriptor (inner,
12393 GET_MODE (inner),
12394 mem_mode, initialized);
12395 if (mem_loc_result == NULL)
12396 break;
12397 type_die = base_type_for_mode (mode,
12398 GET_MODE_CLASS (mode) == MODE_INT);
12399 if (type_die == NULL)
12401 mem_loc_result = NULL;
12402 break;
12404 if (GET_MODE_SIZE (mode)
12405 != GET_MODE_SIZE (GET_MODE (inner)))
12406 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12407 else
12408 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12409 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12410 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12411 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12412 add_loc_descr (&mem_loc_result, cvt);
12414 break;
12416 case REG:
12417 if (GET_MODE_CLASS (mode) != MODE_INT
12418 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12419 && rtl != arg_pointer_rtx
12420 && rtl != frame_pointer_rtx
12421 #ifdef POINTERS_EXTEND_UNSIGNED
12422 && (mode != Pmode || mem_mode == VOIDmode)
12423 #endif
12426 dw_die_ref type_die;
12427 unsigned int dbx_regnum;
12429 if (dwarf_strict)
12430 break;
12431 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12432 break;
12433 type_die = base_type_for_mode (mode,
12434 GET_MODE_CLASS (mode) == MODE_INT);
12435 if (type_die == NULL)
12436 break;
12438 dbx_regnum = dbx_reg_number (rtl);
12439 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12440 break;
12441 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12442 dbx_regnum, 0);
12443 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12444 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12445 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12446 break;
12448 /* Whenever a register number forms a part of the description of the
12449 method for calculating the (dynamic) address of a memory resident
12450 object, DWARF rules require the register number be referred to as
12451 a "base register". This distinction is not based in any way upon
12452 what category of register the hardware believes the given register
12453 belongs to. This is strictly DWARF terminology we're dealing with
12454 here. Note that in cases where the location of a memory-resident
12455 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12456 OP_CONST (0)) the actual DWARF location descriptor that we generate
12457 may just be OP_BASEREG (basereg). This may look deceptively like
12458 the object in question was allocated to a register (rather than in
12459 memory) so DWARF consumers need to be aware of the subtle
12460 distinction between OP_REG and OP_BASEREG. */
12461 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12462 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12463 else if (stack_realign_drap
12464 && crtl->drap_reg
12465 && crtl->args.internal_arg_pointer == rtl
12466 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12468 /* If RTL is internal_arg_pointer, which has been optimized
12469 out, use DRAP instead. */
12470 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12471 VAR_INIT_STATUS_INITIALIZED);
12473 break;
12475 case SIGN_EXTEND:
12476 case ZERO_EXTEND:
12477 if (GET_MODE_CLASS (mode) != MODE_INT)
12478 break;
12479 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12480 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12481 if (op0 == 0)
12482 break;
12483 else if (GET_CODE (rtl) == ZERO_EXTEND
12484 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12485 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12486 < HOST_BITS_PER_WIDE_INT
12487 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12488 to expand zero extend as two shifts instead of
12489 masking. */
12490 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12492 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12493 mem_loc_result = op0;
12494 add_loc_descr (&mem_loc_result,
12495 int_loc_descriptor (GET_MODE_MASK (imode)));
12496 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12498 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12500 int shift = DWARF2_ADDR_SIZE
12501 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12502 shift *= BITS_PER_UNIT;
12503 if (GET_CODE (rtl) == SIGN_EXTEND)
12504 op = DW_OP_shra;
12505 else
12506 op = DW_OP_shr;
12507 mem_loc_result = op0;
12508 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12509 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12510 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12511 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12513 else if (!dwarf_strict)
12515 dw_die_ref type_die1, type_die2;
12516 dw_loc_descr_ref cvt;
12518 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12519 GET_CODE (rtl) == ZERO_EXTEND);
12520 if (type_die1 == NULL)
12521 break;
12522 type_die2 = base_type_for_mode (mode, 1);
12523 if (type_die2 == NULL)
12524 break;
12525 mem_loc_result = op0;
12526 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12527 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12528 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12529 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12530 add_loc_descr (&mem_loc_result, cvt);
12531 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12532 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12533 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12534 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12535 add_loc_descr (&mem_loc_result, cvt);
12537 break;
12539 case MEM:
12541 rtx new_rtl = avoid_constant_pool_reference (rtl);
12542 if (new_rtl != rtl)
12544 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12545 initialized);
12546 if (mem_loc_result != NULL)
12547 return mem_loc_result;
12550 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12551 get_address_mode (rtl), mode,
12552 VAR_INIT_STATUS_INITIALIZED);
12553 if (mem_loc_result == NULL)
12554 mem_loc_result = tls_mem_loc_descriptor (rtl);
12555 if (mem_loc_result != NULL)
12557 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12558 || GET_MODE_CLASS (mode) != MODE_INT)
12560 dw_die_ref type_die;
12561 dw_loc_descr_ref deref;
12563 if (dwarf_strict)
12564 return NULL;
12565 type_die
12566 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12567 if (type_die == NULL)
12568 return NULL;
12569 deref = new_loc_descr (DW_OP_GNU_deref_type,
12570 GET_MODE_SIZE (mode), 0);
12571 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12572 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12573 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12574 add_loc_descr (&mem_loc_result, deref);
12576 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12577 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12578 else
12579 add_loc_descr (&mem_loc_result,
12580 new_loc_descr (DW_OP_deref_size,
12581 GET_MODE_SIZE (mode), 0));
12583 break;
12585 case LO_SUM:
12586 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12588 case LABEL_REF:
12589 /* Some ports can transform a symbol ref into a label ref, because
12590 the symbol ref is too far away and has to be dumped into a constant
12591 pool. */
12592 case CONST:
12593 case SYMBOL_REF:
12594 if (GET_MODE_CLASS (mode) != MODE_INT
12595 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12596 #ifdef POINTERS_EXTEND_UNSIGNED
12597 && (mode != Pmode || mem_mode == VOIDmode)
12598 #endif
12600 break;
12601 if (GET_CODE (rtl) == SYMBOL_REF
12602 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12604 dw_loc_descr_ref temp;
12606 /* If this is not defined, we have no way to emit the data. */
12607 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12608 break;
12610 temp = new_addr_loc_descr (rtl, dtprel_true);
12612 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12613 add_loc_descr (&mem_loc_result, temp);
12615 break;
12618 if (!const_ok_for_output (rtl))
12619 break;
12621 symref:
12622 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12623 vec_safe_push (used_rtx_array, rtl);
12624 break;
12626 case CONCAT:
12627 case CONCATN:
12628 case VAR_LOCATION:
12629 case DEBUG_IMPLICIT_PTR:
12630 expansion_failed (NULL_TREE, rtl,
12631 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12632 return 0;
12634 case ENTRY_VALUE:
12635 if (dwarf_strict)
12636 return NULL;
12637 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12639 if (GET_MODE_CLASS (mode) != MODE_INT
12640 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12641 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12642 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12643 else
12645 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12646 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12647 return NULL;
12648 op0 = one_reg_loc_descriptor (dbx_regnum,
12649 VAR_INIT_STATUS_INITIALIZED);
12652 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12653 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12655 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12656 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12657 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12658 return NULL;
12660 else
12661 gcc_unreachable ();
12662 if (op0 == NULL)
12663 return NULL;
12664 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12665 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12666 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12667 break;
12669 case DEBUG_PARAMETER_REF:
12670 mem_loc_result = parameter_ref_descriptor (rtl);
12671 break;
12673 case PRE_MODIFY:
12674 /* Extract the PLUS expression nested inside and fall into
12675 PLUS code below. */
12676 rtl = XEXP (rtl, 1);
12677 goto plus;
12679 case PRE_INC:
12680 case PRE_DEC:
12681 /* Turn these into a PLUS expression and fall into the PLUS code
12682 below. */
12683 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12684 gen_int_mode (GET_CODE (rtl) == PRE_INC
12685 ? GET_MODE_UNIT_SIZE (mem_mode)
12686 : -GET_MODE_UNIT_SIZE (mem_mode),
12687 mode));
12689 /* ... fall through ... */
12691 case PLUS:
12692 plus:
12693 if (is_based_loc (rtl)
12694 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12695 || XEXP (rtl, 0) == arg_pointer_rtx
12696 || XEXP (rtl, 0) == frame_pointer_rtx)
12697 && GET_MODE_CLASS (mode) == MODE_INT)
12698 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12699 INTVAL (XEXP (rtl, 1)),
12700 VAR_INIT_STATUS_INITIALIZED);
12701 else
12703 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12704 VAR_INIT_STATUS_INITIALIZED);
12705 if (mem_loc_result == 0)
12706 break;
12708 if (CONST_INT_P (XEXP (rtl, 1))
12709 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12710 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12711 else
12713 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12714 VAR_INIT_STATUS_INITIALIZED);
12715 if (op1 == 0)
12716 break;
12717 add_loc_descr (&mem_loc_result, op1);
12718 add_loc_descr (&mem_loc_result,
12719 new_loc_descr (DW_OP_plus, 0, 0));
12722 break;
12724 /* If a pseudo-reg is optimized away, it is possible for it to
12725 be replaced with a MEM containing a multiply or shift. */
12726 case MINUS:
12727 op = DW_OP_minus;
12728 goto do_binop;
12730 case MULT:
12731 op = DW_OP_mul;
12732 goto do_binop;
12734 case DIV:
12735 if (!dwarf_strict
12736 && GET_MODE_CLASS (mode) == MODE_INT
12737 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12739 mem_loc_result = typed_binop (DW_OP_div, rtl,
12740 base_type_for_mode (mode, 0),
12741 mode, mem_mode);
12742 break;
12744 op = DW_OP_div;
12745 goto do_binop;
12747 case UMOD:
12748 op = DW_OP_mod;
12749 goto do_binop;
12751 case ASHIFT:
12752 op = DW_OP_shl;
12753 goto do_shift;
12755 case ASHIFTRT:
12756 op = DW_OP_shra;
12757 goto do_shift;
12759 case LSHIFTRT:
12760 op = DW_OP_shr;
12761 goto do_shift;
12763 do_shift:
12764 if (GET_MODE_CLASS (mode) != MODE_INT)
12765 break;
12766 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12767 VAR_INIT_STATUS_INITIALIZED);
12769 rtx rtlop1 = XEXP (rtl, 1);
12770 if (GET_MODE (rtlop1) != VOIDmode
12771 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12772 < GET_MODE_BITSIZE (mode))
12773 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12774 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12775 VAR_INIT_STATUS_INITIALIZED);
12778 if (op0 == 0 || op1 == 0)
12779 break;
12781 mem_loc_result = op0;
12782 add_loc_descr (&mem_loc_result, op1);
12783 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12784 break;
12786 case AND:
12787 op = DW_OP_and;
12788 goto do_binop;
12790 case IOR:
12791 op = DW_OP_or;
12792 goto do_binop;
12794 case XOR:
12795 op = DW_OP_xor;
12796 goto do_binop;
12798 do_binop:
12799 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12800 VAR_INIT_STATUS_INITIALIZED);
12801 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12802 VAR_INIT_STATUS_INITIALIZED);
12804 if (op0 == 0 || op1 == 0)
12805 break;
12807 mem_loc_result = op0;
12808 add_loc_descr (&mem_loc_result, op1);
12809 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12810 break;
12812 case MOD:
12813 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12815 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12816 base_type_for_mode (mode, 0),
12817 mode, mem_mode);
12818 break;
12821 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12822 VAR_INIT_STATUS_INITIALIZED);
12823 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12824 VAR_INIT_STATUS_INITIALIZED);
12826 if (op0 == 0 || op1 == 0)
12827 break;
12829 mem_loc_result = op0;
12830 add_loc_descr (&mem_loc_result, op1);
12831 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12832 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12833 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12834 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12835 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12836 break;
12838 case UDIV:
12839 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12841 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12843 op = DW_OP_div;
12844 goto do_binop;
12846 mem_loc_result = typed_binop (DW_OP_div, rtl,
12847 base_type_for_mode (mode, 1),
12848 mode, mem_mode);
12850 break;
12852 case NOT:
12853 op = DW_OP_not;
12854 goto do_unop;
12856 case ABS:
12857 op = DW_OP_abs;
12858 goto do_unop;
12860 case NEG:
12861 op = DW_OP_neg;
12862 goto do_unop;
12864 do_unop:
12865 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12866 VAR_INIT_STATUS_INITIALIZED);
12868 if (op0 == 0)
12869 break;
12871 mem_loc_result = op0;
12872 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12873 break;
12875 case CONST_INT:
12876 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12877 #ifdef POINTERS_EXTEND_UNSIGNED
12878 || (mode == Pmode
12879 && mem_mode != VOIDmode
12880 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12881 #endif
12884 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12885 break;
12887 if (!dwarf_strict
12888 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12889 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12891 dw_die_ref type_die = base_type_for_mode (mode, 1);
12892 enum machine_mode amode;
12893 if (type_die == NULL)
12894 return NULL;
12895 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12896 MODE_INT, 0);
12897 if (INTVAL (rtl) >= 0
12898 && amode != BLKmode
12899 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12900 /* const DW_OP_GNU_convert <XXX> vs.
12901 DW_OP_GNU_const_type <XXX, 1, const>. */
12902 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12903 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12905 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12906 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12907 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12908 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12909 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12910 add_loc_descr (&mem_loc_result, op0);
12911 return mem_loc_result;
12913 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12914 INTVAL (rtl));
12915 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12916 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12917 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12918 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12919 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12920 else
12922 mem_loc_result->dw_loc_oprnd2.val_class
12923 = dw_val_class_const_double;
12924 mem_loc_result->dw_loc_oprnd2.v.val_double
12925 = double_int::from_shwi (INTVAL (rtl));
12928 break;
12930 case CONST_DOUBLE:
12931 if (!dwarf_strict)
12933 dw_die_ref type_die;
12935 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12936 CONST_DOUBLE rtx could represent either a large integer
12937 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12938 the value is always a floating point constant.
12940 When it is an integer, a CONST_DOUBLE is used whenever
12941 the constant requires 2 HWIs to be adequately represented.
12942 We output CONST_DOUBLEs as blocks. */
12943 if (mode == VOIDmode
12944 || (GET_MODE (rtl) == VOIDmode
12945 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12946 break;
12947 type_die = base_type_for_mode (mode,
12948 GET_MODE_CLASS (mode) == MODE_INT);
12949 if (type_die == NULL)
12950 return NULL;
12951 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12952 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12953 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12954 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12955 #if TARGET_SUPPORTS_WIDE_INT == 0
12956 if (!SCALAR_FLOAT_MODE_P (mode))
12958 mem_loc_result->dw_loc_oprnd2.val_class
12959 = dw_val_class_const_double;
12960 mem_loc_result->dw_loc_oprnd2.v.val_double
12961 = rtx_to_double_int (rtl);
12963 else
12964 #endif
12966 unsigned int length = GET_MODE_SIZE (mode);
12967 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
12969 insert_float (rtl, array);
12970 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12971 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12972 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12973 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12976 break;
12978 case CONST_WIDE_INT:
12979 if (!dwarf_strict)
12981 dw_die_ref type_die;
12983 type_die = base_type_for_mode (mode,
12984 GET_MODE_CLASS (mode) == MODE_INT);
12985 if (type_die == NULL)
12986 return NULL;
12987 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12988 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12989 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12990 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12991 mem_loc_result->dw_loc_oprnd2.val_class
12992 = dw_val_class_wide_int;
12993 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
12994 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
12996 break;
12998 case EQ:
12999 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13000 break;
13002 case GE:
13003 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13004 break;
13006 case GT:
13007 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13008 break;
13010 case LE:
13011 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13012 break;
13014 case LT:
13015 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13016 break;
13018 case NE:
13019 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13020 break;
13022 case GEU:
13023 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13024 break;
13026 case GTU:
13027 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13028 break;
13030 case LEU:
13031 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13032 break;
13034 case LTU:
13035 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13036 break;
13038 case UMIN:
13039 case UMAX:
13040 if (GET_MODE_CLASS (mode) != MODE_INT)
13041 break;
13042 /* FALLTHRU */
13043 case SMIN:
13044 case SMAX:
13045 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13046 break;
13048 case ZERO_EXTRACT:
13049 case SIGN_EXTRACT:
13050 if (CONST_INT_P (XEXP (rtl, 1))
13051 && CONST_INT_P (XEXP (rtl, 2))
13052 && ((unsigned) INTVAL (XEXP (rtl, 1))
13053 + (unsigned) INTVAL (XEXP (rtl, 2))
13054 <= GET_MODE_BITSIZE (mode))
13055 && GET_MODE_CLASS (mode) == MODE_INT
13056 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13057 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13059 int shift, size;
13060 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13061 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13062 if (op0 == 0)
13063 break;
13064 if (GET_CODE (rtl) == SIGN_EXTRACT)
13065 op = DW_OP_shra;
13066 else
13067 op = DW_OP_shr;
13068 mem_loc_result = op0;
13069 size = INTVAL (XEXP (rtl, 1));
13070 shift = INTVAL (XEXP (rtl, 2));
13071 if (BITS_BIG_ENDIAN)
13072 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13073 - shift - size;
13074 if (shift + size != (int) DWARF2_ADDR_SIZE)
13076 add_loc_descr (&mem_loc_result,
13077 int_loc_descriptor (DWARF2_ADDR_SIZE
13078 - shift - size));
13079 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13081 if (size != (int) DWARF2_ADDR_SIZE)
13083 add_loc_descr (&mem_loc_result,
13084 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13085 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13088 break;
13090 case IF_THEN_ELSE:
13092 dw_loc_descr_ref op2, bra_node, drop_node;
13093 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13094 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13095 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13096 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13097 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13098 VAR_INIT_STATUS_INITIALIZED);
13099 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13100 VAR_INIT_STATUS_INITIALIZED);
13101 if (op0 == NULL || op1 == NULL || op2 == NULL)
13102 break;
13104 mem_loc_result = op1;
13105 add_loc_descr (&mem_loc_result, op2);
13106 add_loc_descr (&mem_loc_result, op0);
13107 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13108 add_loc_descr (&mem_loc_result, bra_node);
13109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13110 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13111 add_loc_descr (&mem_loc_result, drop_node);
13112 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13113 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13115 break;
13117 case FLOAT_EXTEND:
13118 case FLOAT_TRUNCATE:
13119 case FLOAT:
13120 case UNSIGNED_FLOAT:
13121 case FIX:
13122 case UNSIGNED_FIX:
13123 if (!dwarf_strict)
13125 dw_die_ref type_die;
13126 dw_loc_descr_ref cvt;
13128 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13129 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13130 if (op0 == NULL)
13131 break;
13132 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13133 && (GET_CODE (rtl) == FLOAT
13134 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13135 <= DWARF2_ADDR_SIZE))
13137 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13138 GET_CODE (rtl) == UNSIGNED_FLOAT);
13139 if (type_die == NULL)
13140 break;
13141 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13142 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13143 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13144 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13145 add_loc_descr (&op0, cvt);
13147 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13148 if (type_die == NULL)
13149 break;
13150 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13151 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13152 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13153 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13154 add_loc_descr (&op0, cvt);
13155 if (GET_MODE_CLASS (mode) == MODE_INT
13156 && (GET_CODE (rtl) == FIX
13157 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13159 op0 = convert_descriptor_to_mode (mode, op0);
13160 if (op0 == NULL)
13161 break;
13163 mem_loc_result = op0;
13165 break;
13167 case CLZ:
13168 case CTZ:
13169 case FFS:
13170 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13171 break;
13173 case POPCOUNT:
13174 case PARITY:
13175 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13176 break;
13178 case BSWAP:
13179 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13180 break;
13182 case ROTATE:
13183 case ROTATERT:
13184 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13185 break;
13187 case COMPARE:
13188 /* In theory, we could implement the above. */
13189 /* DWARF cannot represent the unsigned compare operations
13190 natively. */
13191 case SS_MULT:
13192 case US_MULT:
13193 case SS_DIV:
13194 case US_DIV:
13195 case SS_PLUS:
13196 case US_PLUS:
13197 case SS_MINUS:
13198 case US_MINUS:
13199 case SS_NEG:
13200 case US_NEG:
13201 case SS_ABS:
13202 case SS_ASHIFT:
13203 case US_ASHIFT:
13204 case SS_TRUNCATE:
13205 case US_TRUNCATE:
13206 case UNORDERED:
13207 case ORDERED:
13208 case UNEQ:
13209 case UNGE:
13210 case UNGT:
13211 case UNLE:
13212 case UNLT:
13213 case LTGT:
13214 case FRACT_CONVERT:
13215 case UNSIGNED_FRACT_CONVERT:
13216 case SAT_FRACT:
13217 case UNSIGNED_SAT_FRACT:
13218 case SQRT:
13219 case ASM_OPERANDS:
13220 case VEC_MERGE:
13221 case VEC_SELECT:
13222 case VEC_CONCAT:
13223 case VEC_DUPLICATE:
13224 case UNSPEC:
13225 case HIGH:
13226 case FMA:
13227 case STRICT_LOW_PART:
13228 case CONST_VECTOR:
13229 case CONST_FIXED:
13230 case CLRSB:
13231 case CLOBBER:
13232 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13233 can't express it in the debug info. This can happen e.g. with some
13234 TLS UNSPECs. */
13235 break;
13237 case CONST_STRING:
13238 resolve_one_addr (&rtl, NULL);
13239 goto symref;
13241 default:
13242 #ifdef ENABLE_CHECKING
13243 print_rtl (stderr, rtl);
13244 gcc_unreachable ();
13245 #else
13246 break;
13247 #endif
13250 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13251 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13253 return mem_loc_result;
13256 /* Return a descriptor that describes the concatenation of two locations.
13257 This is typically a complex variable. */
13259 static dw_loc_descr_ref
13260 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13262 dw_loc_descr_ref cc_loc_result = NULL;
13263 dw_loc_descr_ref x0_ref
13264 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13265 dw_loc_descr_ref x1_ref
13266 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13268 if (x0_ref == 0 || x1_ref == 0)
13269 return 0;
13271 cc_loc_result = x0_ref;
13272 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13274 add_loc_descr (&cc_loc_result, x1_ref);
13275 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13277 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13278 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13280 return cc_loc_result;
13283 /* Return a descriptor that describes the concatenation of N
13284 locations. */
13286 static dw_loc_descr_ref
13287 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13289 unsigned int i;
13290 dw_loc_descr_ref cc_loc_result = NULL;
13291 unsigned int n = XVECLEN (concatn, 0);
13293 for (i = 0; i < n; ++i)
13295 dw_loc_descr_ref ref;
13296 rtx x = XVECEXP (concatn, 0, i);
13298 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13299 if (ref == NULL)
13300 return NULL;
13302 add_loc_descr (&cc_loc_result, ref);
13303 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13306 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13307 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13309 return cc_loc_result;
13312 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13313 for DEBUG_IMPLICIT_PTR RTL. */
13315 static dw_loc_descr_ref
13316 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13318 dw_loc_descr_ref ret;
13319 dw_die_ref ref;
13321 if (dwarf_strict)
13322 return NULL;
13323 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13324 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13325 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13326 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13327 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13328 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13329 if (ref)
13331 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13332 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13333 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13335 else
13337 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13338 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13340 return ret;
13343 /* Output a proper Dwarf location descriptor for a variable or parameter
13344 which is either allocated in a register or in a memory location. For a
13345 register, we just generate an OP_REG and the register number. For a
13346 memory location we provide a Dwarf postfix expression describing how to
13347 generate the (dynamic) address of the object onto the address stack.
13349 MODE is mode of the decl if this loc_descriptor is going to be used in
13350 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13351 allowed, VOIDmode otherwise.
13353 If we don't know how to describe it, return 0. */
13355 static dw_loc_descr_ref
13356 loc_descriptor (rtx rtl, enum machine_mode mode,
13357 enum var_init_status initialized)
13359 dw_loc_descr_ref loc_result = NULL;
13361 switch (GET_CODE (rtl))
13363 case SUBREG:
13364 /* The case of a subreg may arise when we have a local (register)
13365 variable or a formal (register) parameter which doesn't quite fill
13366 up an entire register. For now, just assume that it is
13367 legitimate to make the Dwarf info refer to the whole register which
13368 contains the given subreg. */
13369 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13370 loc_result = loc_descriptor (SUBREG_REG (rtl),
13371 GET_MODE (SUBREG_REG (rtl)), initialized);
13372 else
13373 goto do_default;
13374 break;
13376 case REG:
13377 loc_result = reg_loc_descriptor (rtl, initialized);
13378 break;
13380 case MEM:
13381 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13382 GET_MODE (rtl), initialized);
13383 if (loc_result == NULL)
13384 loc_result = tls_mem_loc_descriptor (rtl);
13385 if (loc_result == NULL)
13387 rtx new_rtl = avoid_constant_pool_reference (rtl);
13388 if (new_rtl != rtl)
13389 loc_result = loc_descriptor (new_rtl, mode, initialized);
13391 break;
13393 case CONCAT:
13394 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13395 initialized);
13396 break;
13398 case CONCATN:
13399 loc_result = concatn_loc_descriptor (rtl, initialized);
13400 break;
13402 case VAR_LOCATION:
13403 /* Single part. */
13404 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13406 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13407 if (GET_CODE (loc) == EXPR_LIST)
13408 loc = XEXP (loc, 0);
13409 loc_result = loc_descriptor (loc, mode, initialized);
13410 break;
13413 rtl = XEXP (rtl, 1);
13414 /* FALLTHRU */
13416 case PARALLEL:
13418 rtvec par_elems = XVEC (rtl, 0);
13419 int num_elem = GET_NUM_ELEM (par_elems);
13420 enum machine_mode mode;
13421 int i;
13423 /* Create the first one, so we have something to add to. */
13424 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13425 VOIDmode, initialized);
13426 if (loc_result == NULL)
13427 return NULL;
13428 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13429 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13430 for (i = 1; i < num_elem; i++)
13432 dw_loc_descr_ref temp;
13434 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13435 VOIDmode, initialized);
13436 if (temp == NULL)
13437 return NULL;
13438 add_loc_descr (&loc_result, temp);
13439 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13440 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13443 break;
13445 case CONST_INT:
13446 if (mode != VOIDmode && mode != BLKmode)
13447 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13448 INTVAL (rtl));
13449 break;
13451 case CONST_DOUBLE:
13452 if (mode == VOIDmode)
13453 mode = GET_MODE (rtl);
13455 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13457 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13459 /* Note that a CONST_DOUBLE rtx could represent either an integer
13460 or a floating-point constant. A CONST_DOUBLE is used whenever
13461 the constant requires more than one word in order to be
13462 adequately represented. We output CONST_DOUBLEs as blocks. */
13463 loc_result = new_loc_descr (DW_OP_implicit_value,
13464 GET_MODE_SIZE (mode), 0);
13465 #if TARGET_SUPPORTS_WIDE_INT == 0
13466 if (!SCALAR_FLOAT_MODE_P (mode))
13468 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13469 loc_result->dw_loc_oprnd2.v.val_double
13470 = rtx_to_double_int (rtl);
13472 else
13473 #endif
13475 unsigned int length = GET_MODE_SIZE (mode);
13476 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13478 insert_float (rtl, array);
13479 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13480 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13481 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13482 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13485 break;
13487 case CONST_WIDE_INT:
13488 if (mode == VOIDmode)
13489 mode = GET_MODE (rtl);
13491 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13493 loc_result = new_loc_descr (DW_OP_implicit_value,
13494 GET_MODE_SIZE (mode), 0);
13495 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13496 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13497 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13499 break;
13501 case CONST_VECTOR:
13502 if (mode == VOIDmode)
13503 mode = GET_MODE (rtl);
13505 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13507 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13508 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13509 unsigned char *array
13510 = ggc_vec_alloc<unsigned char> (length * elt_size);
13511 unsigned int i;
13512 unsigned char *p;
13513 enum machine_mode imode = GET_MODE_INNER (mode);
13515 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13516 switch (GET_MODE_CLASS (mode))
13518 case MODE_VECTOR_INT:
13519 for (i = 0, p = array; i < length; i++, p += elt_size)
13521 rtx elt = CONST_VECTOR_ELT (rtl, i);
13522 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13524 break;
13526 case MODE_VECTOR_FLOAT:
13527 for (i = 0, p = array; i < length; i++, p += elt_size)
13529 rtx elt = CONST_VECTOR_ELT (rtl, i);
13530 insert_float (elt, p);
13532 break;
13534 default:
13535 gcc_unreachable ();
13538 loc_result = new_loc_descr (DW_OP_implicit_value,
13539 length * elt_size, 0);
13540 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13541 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13542 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13543 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13545 break;
13547 case CONST:
13548 if (mode == VOIDmode
13549 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13550 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13551 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13553 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13554 break;
13556 /* FALLTHROUGH */
13557 case SYMBOL_REF:
13558 if (!const_ok_for_output (rtl))
13559 break;
13560 case LABEL_REF:
13561 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13562 && (dwarf_version >= 4 || !dwarf_strict))
13564 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13565 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13566 vec_safe_push (used_rtx_array, rtl);
13568 break;
13570 case DEBUG_IMPLICIT_PTR:
13571 loc_result = implicit_ptr_descriptor (rtl, 0);
13572 break;
13574 case PLUS:
13575 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13576 && CONST_INT_P (XEXP (rtl, 1)))
13578 loc_result
13579 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13580 break;
13582 /* FALLTHRU */
13583 do_default:
13584 default:
13585 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13586 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13587 && dwarf_version >= 4)
13588 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13590 /* Value expression. */
13591 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13592 if (loc_result)
13593 add_loc_descr (&loc_result,
13594 new_loc_descr (DW_OP_stack_value, 0, 0));
13596 break;
13599 return loc_result;
13602 /* We need to figure out what section we should use as the base for the
13603 address ranges where a given location is valid.
13604 1. If this particular DECL has a section associated with it, use that.
13605 2. If this function has a section associated with it, use that.
13606 3. Otherwise, use the text section.
13607 XXX: If you split a variable across multiple sections, we won't notice. */
13609 static const char *
13610 secname_for_decl (const_tree decl)
13612 const char *secname;
13614 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13616 tree sectree = DECL_SECTION_NAME (decl);
13617 secname = TREE_STRING_POINTER (sectree);
13619 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13621 tree sectree = DECL_SECTION_NAME (current_function_decl);
13622 secname = TREE_STRING_POINTER (sectree);
13624 else if (cfun && in_cold_section_p)
13625 secname = crtl->subsections.cold_section_label;
13626 else
13627 secname = text_section_label;
13629 return secname;
13632 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13634 static bool
13635 decl_by_reference_p (tree decl)
13637 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13638 || TREE_CODE (decl) == VAR_DECL)
13639 && DECL_BY_REFERENCE (decl));
13642 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13643 for VARLOC. */
13645 static dw_loc_descr_ref
13646 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13647 enum var_init_status initialized)
13649 int have_address = 0;
13650 dw_loc_descr_ref descr;
13651 enum machine_mode mode;
13653 if (want_address != 2)
13655 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13656 /* Single part. */
13657 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13659 varloc = PAT_VAR_LOCATION_LOC (varloc);
13660 if (GET_CODE (varloc) == EXPR_LIST)
13661 varloc = XEXP (varloc, 0);
13662 mode = GET_MODE (varloc);
13663 if (MEM_P (varloc))
13665 rtx addr = XEXP (varloc, 0);
13666 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13667 mode, initialized);
13668 if (descr)
13669 have_address = 1;
13670 else
13672 rtx x = avoid_constant_pool_reference (varloc);
13673 if (x != varloc)
13674 descr = mem_loc_descriptor (x, mode, VOIDmode,
13675 initialized);
13678 else
13679 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13681 else
13682 return 0;
13684 else
13686 if (GET_CODE (varloc) == VAR_LOCATION)
13687 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13688 else
13689 mode = DECL_MODE (loc);
13690 descr = loc_descriptor (varloc, mode, initialized);
13691 have_address = 1;
13694 if (!descr)
13695 return 0;
13697 if (want_address == 2 && !have_address
13698 && (dwarf_version >= 4 || !dwarf_strict))
13700 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13702 expansion_failed (loc, NULL_RTX,
13703 "DWARF address size mismatch");
13704 return 0;
13706 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13707 have_address = 1;
13709 /* Show if we can't fill the request for an address. */
13710 if (want_address && !have_address)
13712 expansion_failed (loc, NULL_RTX,
13713 "Want address and only have value");
13714 return 0;
13717 /* If we've got an address and don't want one, dereference. */
13718 if (!want_address && have_address)
13720 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13721 enum dwarf_location_atom op;
13723 if (size > DWARF2_ADDR_SIZE || size == -1)
13725 expansion_failed (loc, NULL_RTX,
13726 "DWARF address size mismatch");
13727 return 0;
13729 else if (size == DWARF2_ADDR_SIZE)
13730 op = DW_OP_deref;
13731 else
13732 op = DW_OP_deref_size;
13734 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13737 return descr;
13740 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13741 if it is not possible. */
13743 static dw_loc_descr_ref
13744 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13746 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13747 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13748 else if (dwarf_version >= 3 || !dwarf_strict)
13749 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13750 else
13751 return NULL;
13754 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13755 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13757 static dw_loc_descr_ref
13758 dw_sra_loc_expr (tree decl, rtx loc)
13760 rtx p;
13761 unsigned int padsize = 0;
13762 dw_loc_descr_ref descr, *descr_tail;
13763 unsigned HOST_WIDE_INT decl_size;
13764 rtx varloc;
13765 enum var_init_status initialized;
13767 if (DECL_SIZE (decl) == NULL
13768 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13769 return NULL;
13771 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13772 descr = NULL;
13773 descr_tail = &descr;
13775 for (p = loc; p; p = XEXP (p, 1))
13777 unsigned int bitsize = decl_piece_bitsize (p);
13778 rtx loc_note = *decl_piece_varloc_ptr (p);
13779 dw_loc_descr_ref cur_descr;
13780 dw_loc_descr_ref *tail, last = NULL;
13781 unsigned int opsize = 0;
13783 if (loc_note == NULL_RTX
13784 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13786 padsize += bitsize;
13787 continue;
13789 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13790 varloc = NOTE_VAR_LOCATION (loc_note);
13791 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13792 if (cur_descr == NULL)
13794 padsize += bitsize;
13795 continue;
13798 /* Check that cur_descr either doesn't use
13799 DW_OP_*piece operations, or their sum is equal
13800 to bitsize. Otherwise we can't embed it. */
13801 for (tail = &cur_descr; *tail != NULL;
13802 tail = &(*tail)->dw_loc_next)
13803 if ((*tail)->dw_loc_opc == DW_OP_piece)
13805 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13806 * BITS_PER_UNIT;
13807 last = *tail;
13809 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13811 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13812 last = *tail;
13815 if (last != NULL && opsize != bitsize)
13817 padsize += bitsize;
13818 /* Discard the current piece of the descriptor and release any
13819 addr_table entries it uses. */
13820 remove_loc_list_addr_table_entries (cur_descr);
13821 continue;
13824 /* If there is a hole, add DW_OP_*piece after empty DWARF
13825 expression, which means that those bits are optimized out. */
13826 if (padsize)
13828 if (padsize > decl_size)
13830 remove_loc_list_addr_table_entries (cur_descr);
13831 goto discard_descr;
13833 decl_size -= padsize;
13834 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13835 if (*descr_tail == NULL)
13837 remove_loc_list_addr_table_entries (cur_descr);
13838 goto discard_descr;
13840 descr_tail = &(*descr_tail)->dw_loc_next;
13841 padsize = 0;
13843 *descr_tail = cur_descr;
13844 descr_tail = tail;
13845 if (bitsize > decl_size)
13846 goto discard_descr;
13847 decl_size -= bitsize;
13848 if (last == NULL)
13850 HOST_WIDE_INT offset = 0;
13851 if (GET_CODE (varloc) == VAR_LOCATION
13852 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13854 varloc = PAT_VAR_LOCATION_LOC (varloc);
13855 if (GET_CODE (varloc) == EXPR_LIST)
13856 varloc = XEXP (varloc, 0);
13860 if (GET_CODE (varloc) == CONST
13861 || GET_CODE (varloc) == SIGN_EXTEND
13862 || GET_CODE (varloc) == ZERO_EXTEND)
13863 varloc = XEXP (varloc, 0);
13864 else if (GET_CODE (varloc) == SUBREG)
13865 varloc = SUBREG_REG (varloc);
13866 else
13867 break;
13869 while (1);
13870 /* DW_OP_bit_size offset should be zero for register
13871 or implicit location descriptions and empty location
13872 descriptions, but for memory addresses needs big endian
13873 adjustment. */
13874 if (MEM_P (varloc))
13876 unsigned HOST_WIDE_INT memsize
13877 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13878 if (memsize != bitsize)
13880 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13881 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13882 goto discard_descr;
13883 if (memsize < bitsize)
13884 goto discard_descr;
13885 if (BITS_BIG_ENDIAN)
13886 offset = memsize - bitsize;
13890 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13891 if (*descr_tail == NULL)
13892 goto discard_descr;
13893 descr_tail = &(*descr_tail)->dw_loc_next;
13897 /* If there were any non-empty expressions, add padding till the end of
13898 the decl. */
13899 if (descr != NULL && decl_size != 0)
13901 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13902 if (*descr_tail == NULL)
13903 goto discard_descr;
13905 return descr;
13907 discard_descr:
13908 /* Discard the descriptor and release any addr_table entries it uses. */
13909 remove_loc_list_addr_table_entries (descr);
13910 return NULL;
13913 /* Return the dwarf representation of the location list LOC_LIST of
13914 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13915 function. */
13917 static dw_loc_list_ref
13918 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13920 const char *endname, *secname;
13921 rtx varloc;
13922 enum var_init_status initialized;
13923 struct var_loc_node *node;
13924 dw_loc_descr_ref descr;
13925 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13926 dw_loc_list_ref list = NULL;
13927 dw_loc_list_ref *listp = &list;
13929 /* Now that we know what section we are using for a base,
13930 actually construct the list of locations.
13931 The first location information is what is passed to the
13932 function that creates the location list, and the remaining
13933 locations just get added on to that list.
13934 Note that we only know the start address for a location
13935 (IE location changes), so to build the range, we use
13936 the range [current location start, next location start].
13937 This means we have to special case the last node, and generate
13938 a range of [last location start, end of function label]. */
13940 secname = secname_for_decl (decl);
13942 for (node = loc_list->first; node; node = node->next)
13943 if (GET_CODE (node->loc) == EXPR_LIST
13944 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13946 if (GET_CODE (node->loc) == EXPR_LIST)
13948 /* This requires DW_OP_{,bit_}piece, which is not usable
13949 inside DWARF expressions. */
13950 if (want_address != 2)
13951 continue;
13952 descr = dw_sra_loc_expr (decl, node->loc);
13953 if (descr == NULL)
13954 continue;
13956 else
13958 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13959 varloc = NOTE_VAR_LOCATION (node->loc);
13960 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13962 if (descr)
13964 bool range_across_switch = false;
13965 /* If section switch happens in between node->label
13966 and node->next->label (or end of function) and
13967 we can't emit it as a single entry list,
13968 emit two ranges, first one ending at the end
13969 of first partition and second one starting at the
13970 beginning of second partition. */
13971 if (node == loc_list->last_before_switch
13972 && (node != loc_list->first || loc_list->first->next)
13973 && current_function_decl)
13975 endname = cfun->fde->dw_fde_end;
13976 range_across_switch = true;
13978 /* The variable has a location between NODE->LABEL and
13979 NODE->NEXT->LABEL. */
13980 else if (node->next)
13981 endname = node->next->label;
13982 /* If the variable has a location at the last label
13983 it keeps its location until the end of function. */
13984 else if (!current_function_decl)
13985 endname = text_end_label;
13986 else
13988 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13989 current_function_funcdef_no);
13990 endname = ggc_strdup (label_id);
13993 *listp = new_loc_list (descr, node->label, endname, secname);
13994 if (TREE_CODE (decl) == PARM_DECL
13995 && node == loc_list->first
13996 && NOTE_P (node->loc)
13997 && strcmp (node->label, endname) == 0)
13998 (*listp)->force = true;
13999 listp = &(*listp)->dw_loc_next;
14001 if (range_across_switch)
14003 if (GET_CODE (node->loc) == EXPR_LIST)
14004 descr = dw_sra_loc_expr (decl, node->loc);
14005 else
14007 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14008 varloc = NOTE_VAR_LOCATION (node->loc);
14009 descr = dw_loc_list_1 (decl, varloc, want_address,
14010 initialized);
14012 gcc_assert (descr);
14013 /* The variable has a location between NODE->LABEL and
14014 NODE->NEXT->LABEL. */
14015 if (node->next)
14016 endname = node->next->label;
14017 else
14018 endname = cfun->fde->dw_fde_second_end;
14019 *listp = new_loc_list (descr,
14020 cfun->fde->dw_fde_second_begin,
14021 endname, secname);
14022 listp = &(*listp)->dw_loc_next;
14027 /* Try to avoid the overhead of a location list emitting a location
14028 expression instead, but only if we didn't have more than one
14029 location entry in the first place. If some entries were not
14030 representable, we don't want to pretend a single entry that was
14031 applies to the entire scope in which the variable is
14032 available. */
14033 if (list && loc_list->first->next)
14034 gen_llsym (list);
14036 return list;
14039 /* Return if the loc_list has only single element and thus can be represented
14040 as location description. */
14042 static bool
14043 single_element_loc_list_p (dw_loc_list_ref list)
14045 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14046 return !list->ll_symbol;
14049 /* To each location in list LIST add loc descr REF. */
14051 static void
14052 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14054 dw_loc_descr_ref copy;
14055 add_loc_descr (&list->expr, ref);
14056 list = list->dw_loc_next;
14057 while (list)
14059 copy = ggc_alloc<dw_loc_descr_node> ();
14060 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14061 add_loc_descr (&list->expr, copy);
14062 while (copy->dw_loc_next)
14064 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14065 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14066 copy->dw_loc_next = new_copy;
14067 copy = new_copy;
14069 list = list->dw_loc_next;
14073 /* Given two lists RET and LIST
14074 produce location list that is result of adding expression in LIST
14075 to expression in RET on each position in program.
14076 Might be destructive on both RET and LIST.
14078 TODO: We handle only simple cases of RET or LIST having at most one
14079 element. General case would inolve sorting the lists in program order
14080 and merging them that will need some additional work.
14081 Adding that will improve quality of debug info especially for SRA-ed
14082 structures. */
14084 static void
14085 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14087 if (!list)
14088 return;
14089 if (!*ret)
14091 *ret = list;
14092 return;
14094 if (!list->dw_loc_next)
14096 add_loc_descr_to_each (*ret, list->expr);
14097 return;
14099 if (!(*ret)->dw_loc_next)
14101 add_loc_descr_to_each (list, (*ret)->expr);
14102 *ret = list;
14103 return;
14105 expansion_failed (NULL_TREE, NULL_RTX,
14106 "Don't know how to merge two non-trivial"
14107 " location lists.\n");
14108 *ret = NULL;
14109 return;
14112 /* LOC is constant expression. Try a luck, look it up in constant
14113 pool and return its loc_descr of its address. */
14115 static dw_loc_descr_ref
14116 cst_pool_loc_descr (tree loc)
14118 /* Get an RTL for this, if something has been emitted. */
14119 rtx rtl = lookup_constant_def (loc);
14121 if (!rtl || !MEM_P (rtl))
14123 gcc_assert (!rtl);
14124 return 0;
14126 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14128 /* TODO: We might get more coverage if we was actually delaying expansion
14129 of all expressions till end of compilation when constant pools are fully
14130 populated. */
14131 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14133 expansion_failed (loc, NULL_RTX,
14134 "CST value in contant pool but not marked.");
14135 return 0;
14137 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14138 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14141 /* Return dw_loc_list representing address of addr_expr LOC
14142 by looking for inner INDIRECT_REF expression and turning
14143 it into simple arithmetics. */
14145 static dw_loc_list_ref
14146 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14148 tree obj, offset;
14149 HOST_WIDE_INT bitsize, bitpos, bytepos;
14150 enum machine_mode mode;
14151 int unsignedp, volatilep = 0;
14152 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14154 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14155 &bitsize, &bitpos, &offset, &mode,
14156 &unsignedp, &volatilep, false);
14157 STRIP_NOPS (obj);
14158 if (bitpos % BITS_PER_UNIT)
14160 expansion_failed (loc, NULL_RTX, "bitfield access");
14161 return 0;
14163 if (!INDIRECT_REF_P (obj))
14165 expansion_failed (obj,
14166 NULL_RTX, "no indirect ref in inner refrence");
14167 return 0;
14169 if (!offset && !bitpos)
14170 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14171 else if (toplev
14172 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14173 && (dwarf_version >= 4 || !dwarf_strict))
14175 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14176 if (!list_ret)
14177 return 0;
14178 if (offset)
14180 /* Variable offset. */
14181 list_ret1 = loc_list_from_tree (offset, 0);
14182 if (list_ret1 == 0)
14183 return 0;
14184 add_loc_list (&list_ret, list_ret1);
14185 if (!list_ret)
14186 return 0;
14187 add_loc_descr_to_each (list_ret,
14188 new_loc_descr (DW_OP_plus, 0, 0));
14190 bytepos = bitpos / BITS_PER_UNIT;
14191 if (bytepos > 0)
14192 add_loc_descr_to_each (list_ret,
14193 new_loc_descr (DW_OP_plus_uconst,
14194 bytepos, 0));
14195 else if (bytepos < 0)
14196 loc_list_plus_const (list_ret, bytepos);
14197 add_loc_descr_to_each (list_ret,
14198 new_loc_descr (DW_OP_stack_value, 0, 0));
14200 return list_ret;
14204 /* Generate Dwarf location list representing LOC.
14205 If WANT_ADDRESS is false, expression computing LOC will be computed
14206 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14207 if WANT_ADDRESS is 2, expression computing address useable in location
14208 will be returned (i.e. DW_OP_reg can be used
14209 to refer to register values). */
14211 static dw_loc_list_ref
14212 loc_list_from_tree (tree loc, int want_address)
14214 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14215 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14216 int have_address = 0;
14217 enum dwarf_location_atom op;
14219 /* ??? Most of the time we do not take proper care for sign/zero
14220 extending the values properly. Hopefully this won't be a real
14221 problem... */
14223 switch (TREE_CODE (loc))
14225 case ERROR_MARK:
14226 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14227 return 0;
14229 case PLACEHOLDER_EXPR:
14230 /* This case involves extracting fields from an object to determine the
14231 position of other fields. We don't try to encode this here. The
14232 only user of this is Ada, which encodes the needed information using
14233 the names of types. */
14234 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14235 return 0;
14237 case CALL_EXPR:
14238 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14239 /* There are no opcodes for these operations. */
14240 return 0;
14242 case PREINCREMENT_EXPR:
14243 case PREDECREMENT_EXPR:
14244 case POSTINCREMENT_EXPR:
14245 case POSTDECREMENT_EXPR:
14246 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14247 /* There are no opcodes for these operations. */
14248 return 0;
14250 case ADDR_EXPR:
14251 /* If we already want an address, see if there is INDIRECT_REF inside
14252 e.g. for &this->field. */
14253 if (want_address)
14255 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14256 (loc, want_address == 2);
14257 if (list_ret)
14258 have_address = 1;
14259 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14260 && (ret = cst_pool_loc_descr (loc)))
14261 have_address = 1;
14263 /* Otherwise, process the argument and look for the address. */
14264 if (!list_ret && !ret)
14265 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14266 else
14268 if (want_address)
14269 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14270 return NULL;
14272 break;
14274 case VAR_DECL:
14275 if (DECL_THREAD_LOCAL_P (loc))
14277 rtx rtl;
14278 enum dwarf_location_atom tls_op;
14279 enum dtprel_bool dtprel = dtprel_false;
14281 if (targetm.have_tls)
14283 /* If this is not defined, we have no way to emit the
14284 data. */
14285 if (!targetm.asm_out.output_dwarf_dtprel)
14286 return 0;
14288 /* The way DW_OP_GNU_push_tls_address is specified, we
14289 can only look up addresses of objects in the current
14290 module. We used DW_OP_addr as first op, but that's
14291 wrong, because DW_OP_addr is relocated by the debug
14292 info consumer, while DW_OP_GNU_push_tls_address
14293 operand shouldn't be. */
14294 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14295 return 0;
14296 dtprel = dtprel_true;
14297 tls_op = DW_OP_GNU_push_tls_address;
14299 else
14301 if (!targetm.emutls.debug_form_tls_address
14302 || !(dwarf_version >= 3 || !dwarf_strict))
14303 return 0;
14304 /* We stuffed the control variable into the DECL_VALUE_EXPR
14305 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14306 no longer appear in gimple code. We used the control
14307 variable in specific so that we could pick it up here. */
14308 loc = DECL_VALUE_EXPR (loc);
14309 tls_op = DW_OP_form_tls_address;
14312 rtl = rtl_for_decl_location (loc);
14313 if (rtl == NULL_RTX)
14314 return 0;
14316 if (!MEM_P (rtl))
14317 return 0;
14318 rtl = XEXP (rtl, 0);
14319 if (! CONSTANT_P (rtl))
14320 return 0;
14322 ret = new_addr_loc_descr (rtl, dtprel);
14323 ret1 = new_loc_descr (tls_op, 0, 0);
14324 add_loc_descr (&ret, ret1);
14326 have_address = 1;
14327 break;
14329 /* FALLTHRU */
14331 case PARM_DECL:
14332 case RESULT_DECL:
14333 if (DECL_HAS_VALUE_EXPR_P (loc))
14334 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14335 want_address);
14336 /* FALLTHRU */
14338 case FUNCTION_DECL:
14340 rtx rtl;
14341 var_loc_list *loc_list = lookup_decl_loc (loc);
14343 if (loc_list && loc_list->first)
14345 list_ret = dw_loc_list (loc_list, loc, want_address);
14346 have_address = want_address != 0;
14347 break;
14349 rtl = rtl_for_decl_location (loc);
14350 if (rtl == NULL_RTX)
14352 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14353 return 0;
14355 else if (CONST_INT_P (rtl))
14357 HOST_WIDE_INT val = INTVAL (rtl);
14358 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14359 val &= GET_MODE_MASK (DECL_MODE (loc));
14360 ret = int_loc_descriptor (val);
14362 else if (GET_CODE (rtl) == CONST_STRING)
14364 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14365 return 0;
14367 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14368 ret = new_addr_loc_descr (rtl, dtprel_false);
14369 else
14371 enum machine_mode mode, mem_mode;
14373 /* Certain constructs can only be represented at top-level. */
14374 if (want_address == 2)
14376 ret = loc_descriptor (rtl, VOIDmode,
14377 VAR_INIT_STATUS_INITIALIZED);
14378 have_address = 1;
14380 else
14382 mode = GET_MODE (rtl);
14383 mem_mode = VOIDmode;
14384 if (MEM_P (rtl))
14386 mem_mode = mode;
14387 mode = get_address_mode (rtl);
14388 rtl = XEXP (rtl, 0);
14389 have_address = 1;
14391 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14392 VAR_INIT_STATUS_INITIALIZED);
14394 if (!ret)
14395 expansion_failed (loc, rtl,
14396 "failed to produce loc descriptor for rtl");
14399 break;
14401 case MEM_REF:
14402 /* ??? FIXME. */
14403 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14404 return 0;
14405 /* Fallthru. */
14406 case INDIRECT_REF:
14407 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14408 have_address = 1;
14409 break;
14411 case COMPOUND_EXPR:
14412 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14414 CASE_CONVERT:
14415 case VIEW_CONVERT_EXPR:
14416 case SAVE_EXPR:
14417 case MODIFY_EXPR:
14418 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14420 case COMPONENT_REF:
14421 case BIT_FIELD_REF:
14422 case ARRAY_REF:
14423 case ARRAY_RANGE_REF:
14424 case REALPART_EXPR:
14425 case IMAGPART_EXPR:
14427 tree obj, offset;
14428 HOST_WIDE_INT bitsize, bitpos, bytepos;
14429 enum machine_mode mode;
14430 int unsignedp, volatilep = 0;
14432 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14433 &unsignedp, &volatilep, false);
14435 gcc_assert (obj != loc);
14437 list_ret = loc_list_from_tree (obj,
14438 want_address == 2
14439 && !bitpos && !offset ? 2 : 1);
14440 /* TODO: We can extract value of the small expression via shifting even
14441 for nonzero bitpos. */
14442 if (list_ret == 0)
14443 return 0;
14444 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14446 expansion_failed (loc, NULL_RTX,
14447 "bitfield access");
14448 return 0;
14451 if (offset != NULL_TREE)
14453 /* Variable offset. */
14454 list_ret1 = loc_list_from_tree (offset, 0);
14455 if (list_ret1 == 0)
14456 return 0;
14457 add_loc_list (&list_ret, list_ret1);
14458 if (!list_ret)
14459 return 0;
14460 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14463 bytepos = bitpos / BITS_PER_UNIT;
14464 if (bytepos > 0)
14465 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14466 else if (bytepos < 0)
14467 loc_list_plus_const (list_ret, bytepos);
14469 have_address = 1;
14470 break;
14473 case INTEGER_CST:
14474 if ((want_address || !tree_fits_shwi_p (loc))
14475 && (ret = cst_pool_loc_descr (loc)))
14476 have_address = 1;
14477 else if (want_address == 2
14478 && tree_fits_shwi_p (loc)
14479 && (ret = address_of_int_loc_descriptor
14480 (int_size_in_bytes (TREE_TYPE (loc)),
14481 tree_to_shwi (loc))))
14482 have_address = 1;
14483 else if (tree_fits_shwi_p (loc))
14484 ret = int_loc_descriptor (tree_to_shwi (loc));
14485 else
14487 expansion_failed (loc, NULL_RTX,
14488 "Integer operand is not host integer");
14489 return 0;
14491 break;
14493 case CONSTRUCTOR:
14494 case REAL_CST:
14495 case STRING_CST:
14496 case COMPLEX_CST:
14497 if ((ret = cst_pool_loc_descr (loc)))
14498 have_address = 1;
14499 else
14500 /* We can construct small constants here using int_loc_descriptor. */
14501 expansion_failed (loc, NULL_RTX,
14502 "constructor or constant not in constant pool");
14503 break;
14505 case TRUTH_AND_EXPR:
14506 case TRUTH_ANDIF_EXPR:
14507 case BIT_AND_EXPR:
14508 op = DW_OP_and;
14509 goto do_binop;
14511 case TRUTH_XOR_EXPR:
14512 case BIT_XOR_EXPR:
14513 op = DW_OP_xor;
14514 goto do_binop;
14516 case TRUTH_OR_EXPR:
14517 case TRUTH_ORIF_EXPR:
14518 case BIT_IOR_EXPR:
14519 op = DW_OP_or;
14520 goto do_binop;
14522 case FLOOR_DIV_EXPR:
14523 case CEIL_DIV_EXPR:
14524 case ROUND_DIV_EXPR:
14525 case TRUNC_DIV_EXPR:
14526 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14527 return 0;
14528 op = DW_OP_div;
14529 goto do_binop;
14531 case MINUS_EXPR:
14532 op = DW_OP_minus;
14533 goto do_binop;
14535 case FLOOR_MOD_EXPR:
14536 case CEIL_MOD_EXPR:
14537 case ROUND_MOD_EXPR:
14538 case TRUNC_MOD_EXPR:
14539 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14541 op = DW_OP_mod;
14542 goto do_binop;
14544 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14545 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14546 if (list_ret == 0 || list_ret1 == 0)
14547 return 0;
14549 add_loc_list (&list_ret, list_ret1);
14550 if (list_ret == 0)
14551 return 0;
14552 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14553 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14554 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14555 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14556 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14557 break;
14559 case MULT_EXPR:
14560 op = DW_OP_mul;
14561 goto do_binop;
14563 case LSHIFT_EXPR:
14564 op = DW_OP_shl;
14565 goto do_binop;
14567 case RSHIFT_EXPR:
14568 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14569 goto do_binop;
14571 case POINTER_PLUS_EXPR:
14572 case PLUS_EXPR:
14573 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14575 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14576 if (list_ret == 0)
14577 return 0;
14579 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14580 break;
14583 op = DW_OP_plus;
14584 goto do_binop;
14586 case LE_EXPR:
14587 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14588 return 0;
14590 op = DW_OP_le;
14591 goto do_binop;
14593 case GE_EXPR:
14594 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14595 return 0;
14597 op = DW_OP_ge;
14598 goto do_binop;
14600 case LT_EXPR:
14601 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14602 return 0;
14604 op = DW_OP_lt;
14605 goto do_binop;
14607 case GT_EXPR:
14608 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14609 return 0;
14611 op = DW_OP_gt;
14612 goto do_binop;
14614 case EQ_EXPR:
14615 op = DW_OP_eq;
14616 goto do_binop;
14618 case NE_EXPR:
14619 op = DW_OP_ne;
14620 goto do_binop;
14622 do_binop:
14623 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14624 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14625 if (list_ret == 0 || list_ret1 == 0)
14626 return 0;
14628 add_loc_list (&list_ret, list_ret1);
14629 if (list_ret == 0)
14630 return 0;
14631 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14632 break;
14634 case TRUTH_NOT_EXPR:
14635 case BIT_NOT_EXPR:
14636 op = DW_OP_not;
14637 goto do_unop;
14639 case ABS_EXPR:
14640 op = DW_OP_abs;
14641 goto do_unop;
14643 case NEGATE_EXPR:
14644 op = DW_OP_neg;
14645 goto do_unop;
14647 do_unop:
14648 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14649 if (list_ret == 0)
14650 return 0;
14652 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14653 break;
14655 case MIN_EXPR:
14656 case MAX_EXPR:
14658 const enum tree_code code =
14659 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14661 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14662 build2 (code, integer_type_node,
14663 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14664 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14667 /* ... fall through ... */
14669 case COND_EXPR:
14671 dw_loc_descr_ref lhs
14672 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14673 dw_loc_list_ref rhs
14674 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14675 dw_loc_descr_ref bra_node, jump_node, tmp;
14677 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14678 if (list_ret == 0 || lhs == 0 || rhs == 0)
14679 return 0;
14681 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14682 add_loc_descr_to_each (list_ret, bra_node);
14684 add_loc_list (&list_ret, rhs);
14685 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14686 add_loc_descr_to_each (list_ret, jump_node);
14688 add_loc_descr_to_each (list_ret, lhs);
14689 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14690 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14692 /* ??? Need a node to point the skip at. Use a nop. */
14693 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14694 add_loc_descr_to_each (list_ret, tmp);
14695 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14696 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14698 break;
14700 case FIX_TRUNC_EXPR:
14701 return 0;
14703 default:
14704 /* Leave front-end specific codes as simply unknown. This comes
14705 up, for instance, with the C STMT_EXPR. */
14706 if ((unsigned int) TREE_CODE (loc)
14707 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14709 expansion_failed (loc, NULL_RTX,
14710 "language specific tree node");
14711 return 0;
14714 #ifdef ENABLE_CHECKING
14715 /* Otherwise this is a generic code; we should just lists all of
14716 these explicitly. We forgot one. */
14717 gcc_unreachable ();
14718 #else
14719 /* In a release build, we want to degrade gracefully: better to
14720 generate incomplete debugging information than to crash. */
14721 return NULL;
14722 #endif
14725 if (!ret && !list_ret)
14726 return 0;
14728 if (want_address == 2 && !have_address
14729 && (dwarf_version >= 4 || !dwarf_strict))
14731 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14733 expansion_failed (loc, NULL_RTX,
14734 "DWARF address size mismatch");
14735 return 0;
14737 if (ret)
14738 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14739 else
14740 add_loc_descr_to_each (list_ret,
14741 new_loc_descr (DW_OP_stack_value, 0, 0));
14742 have_address = 1;
14744 /* Show if we can't fill the request for an address. */
14745 if (want_address && !have_address)
14747 expansion_failed (loc, NULL_RTX,
14748 "Want address and only have value");
14749 return 0;
14752 gcc_assert (!ret || !list_ret);
14754 /* If we've got an address and don't want one, dereference. */
14755 if (!want_address && have_address)
14757 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14759 if (size > DWARF2_ADDR_SIZE || size == -1)
14761 expansion_failed (loc, NULL_RTX,
14762 "DWARF address size mismatch");
14763 return 0;
14765 else if (size == DWARF2_ADDR_SIZE)
14766 op = DW_OP_deref;
14767 else
14768 op = DW_OP_deref_size;
14770 if (ret)
14771 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14772 else
14773 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14775 if (ret)
14776 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14778 return list_ret;
14781 /* Same as above but return only single location expression. */
14782 static dw_loc_descr_ref
14783 loc_descriptor_from_tree (tree loc, int want_address)
14785 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14786 if (!ret)
14787 return NULL;
14788 if (ret->dw_loc_next)
14790 expansion_failed (loc, NULL_RTX,
14791 "Location list where only loc descriptor needed");
14792 return NULL;
14794 return ret->expr;
14797 /* Given a value, round it up to the lowest multiple of `boundary'
14798 which is not less than the value itself. */
14800 static inline HOST_WIDE_INT
14801 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14803 return (((value + boundary - 1) / boundary) * boundary);
14806 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14807 pointer to the declared type for the relevant field variable, or return
14808 `integer_type_node' if the given node turns out to be an
14809 ERROR_MARK node. */
14811 static inline tree
14812 field_type (const_tree decl)
14814 tree type;
14816 if (TREE_CODE (decl) == ERROR_MARK)
14817 return integer_type_node;
14819 type = DECL_BIT_FIELD_TYPE (decl);
14820 if (type == NULL_TREE)
14821 type = TREE_TYPE (decl);
14823 return type;
14826 /* Given a pointer to a tree node, return the alignment in bits for
14827 it, or else return BITS_PER_WORD if the node actually turns out to
14828 be an ERROR_MARK node. */
14830 static inline unsigned
14831 simple_type_align_in_bits (const_tree type)
14833 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14836 static inline unsigned
14837 simple_decl_align_in_bits (const_tree decl)
14839 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14842 /* Return the result of rounding T up to ALIGN. */
14844 static inline offset_int
14845 round_up_to_align (const offset_int &t, unsigned int align)
14847 return wi::udiv_trunc (t + align - 1, align) * align;
14850 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14851 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14852 or return 0 if we are unable to determine what that offset is, either
14853 because the argument turns out to be a pointer to an ERROR_MARK node, or
14854 because the offset is actually variable. (We can't handle the latter case
14855 just yet). */
14857 static HOST_WIDE_INT
14858 field_byte_offset (const_tree decl)
14860 offset_int object_offset_in_bits;
14861 offset_int object_offset_in_bytes;
14862 offset_int bitpos_int;
14864 if (TREE_CODE (decl) == ERROR_MARK)
14865 return 0;
14867 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14869 /* We cannot yet cope with fields whose positions are variable, so
14870 for now, when we see such things, we simply return 0. Someday, we may
14871 be able to handle such cases, but it will be damn difficult. */
14872 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14873 return 0;
14875 bitpos_int = wi::to_offset (bit_position (decl));
14877 #ifdef PCC_BITFIELD_TYPE_MATTERS
14878 if (PCC_BITFIELD_TYPE_MATTERS)
14880 tree type;
14881 tree field_size_tree;
14882 offset_int deepest_bitpos;
14883 offset_int field_size_in_bits;
14884 unsigned int type_align_in_bits;
14885 unsigned int decl_align_in_bits;
14886 offset_int type_size_in_bits;
14888 type = field_type (decl);
14889 type_size_in_bits = offset_int_type_size_in_bits (type);
14890 type_align_in_bits = simple_type_align_in_bits (type);
14892 field_size_tree = DECL_SIZE (decl);
14894 /* The size could be unspecified if there was an error, or for
14895 a flexible array member. */
14896 if (!field_size_tree)
14897 field_size_tree = bitsize_zero_node;
14899 /* If the size of the field is not constant, use the type size. */
14900 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14901 field_size_in_bits = wi::to_offset (field_size_tree);
14902 else
14903 field_size_in_bits = type_size_in_bits;
14905 decl_align_in_bits = simple_decl_align_in_bits (decl);
14907 /* The GCC front-end doesn't make any attempt to keep track of the
14908 starting bit offset (relative to the start of the containing
14909 structure type) of the hypothetical "containing object" for a
14910 bit-field. Thus, when computing the byte offset value for the
14911 start of the "containing object" of a bit-field, we must deduce
14912 this information on our own. This can be rather tricky to do in
14913 some cases. For example, handling the following structure type
14914 definition when compiling for an i386/i486 target (which only
14915 aligns long long's to 32-bit boundaries) can be very tricky:
14917 struct S { int field1; long long field2:31; };
14919 Fortunately, there is a simple rule-of-thumb which can be used
14920 in such cases. When compiling for an i386/i486, GCC will
14921 allocate 8 bytes for the structure shown above. It decides to
14922 do this based upon one simple rule for bit-field allocation.
14923 GCC allocates each "containing object" for each bit-field at
14924 the first (i.e. lowest addressed) legitimate alignment boundary
14925 (based upon the required minimum alignment for the declared
14926 type of the field) which it can possibly use, subject to the
14927 condition that there is still enough available space remaining
14928 in the containing object (when allocated at the selected point)
14929 to fully accommodate all of the bits of the bit-field itself.
14931 This simple rule makes it obvious why GCC allocates 8 bytes for
14932 each object of the structure type shown above. When looking
14933 for a place to allocate the "containing object" for `field2',
14934 the compiler simply tries to allocate a 64-bit "containing
14935 object" at each successive 32-bit boundary (starting at zero)
14936 until it finds a place to allocate that 64- bit field such that
14937 at least 31 contiguous (and previously unallocated) bits remain
14938 within that selected 64 bit field. (As it turns out, for the
14939 example above, the compiler finds it is OK to allocate the
14940 "containing object" 64-bit field at bit-offset zero within the
14941 structure type.)
14943 Here we attempt to work backwards from the limited set of facts
14944 we're given, and we try to deduce from those facts, where GCC
14945 must have believed that the containing object started (within
14946 the structure type). The value we deduce is then used (by the
14947 callers of this routine) to generate DW_AT_location and
14948 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14949 the case of DW_AT_location, regular fields as well). */
14951 /* Figure out the bit-distance from the start of the structure to
14952 the "deepest" bit of the bit-field. */
14953 deepest_bitpos = bitpos_int + field_size_in_bits;
14955 /* This is the tricky part. Use some fancy footwork to deduce
14956 where the lowest addressed bit of the containing object must
14957 be. */
14958 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14960 /* Round up to type_align by default. This works best for
14961 bitfields. */
14962 object_offset_in_bits
14963 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14965 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
14967 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14969 /* Round up to decl_align instead. */
14970 object_offset_in_bits
14971 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14974 else
14975 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14976 object_offset_in_bits = bitpos_int;
14978 object_offset_in_bytes
14979 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
14980 return object_offset_in_bytes.to_shwi ();
14983 /* The following routines define various Dwarf attributes and any data
14984 associated with them. */
14986 /* Add a location description attribute value to a DIE.
14988 This emits location attributes suitable for whole variables and
14989 whole parameters. Note that the location attributes for struct fields are
14990 generated by the routine `data_member_location_attribute' below. */
14992 static inline void
14993 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14994 dw_loc_list_ref descr)
14996 if (descr == 0)
14997 return;
14998 if (single_element_loc_list_p (descr))
14999 add_AT_loc (die, attr_kind, descr->expr);
15000 else
15001 add_AT_loc_list (die, attr_kind, descr);
15004 /* Add DW_AT_accessibility attribute to DIE if needed. */
15006 static void
15007 add_accessibility_attribute (dw_die_ref die, tree decl)
15009 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15010 children, otherwise the default is DW_ACCESS_public. In DWARF2
15011 the default has always been DW_ACCESS_public. */
15012 if (TREE_PROTECTED (decl))
15013 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15014 else if (TREE_PRIVATE (decl))
15016 if (dwarf_version == 2
15017 || die->die_parent == NULL
15018 || die->die_parent->die_tag != DW_TAG_class_type)
15019 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15021 else if (dwarf_version > 2
15022 && die->die_parent
15023 && die->die_parent->die_tag == DW_TAG_class_type)
15024 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15027 /* Attach the specialized form of location attribute used for data members of
15028 struct and union types. In the special case of a FIELD_DECL node which
15029 represents a bit-field, the "offset" part of this special location
15030 descriptor must indicate the distance in bytes from the lowest-addressed
15031 byte of the containing struct or union type to the lowest-addressed byte of
15032 the "containing object" for the bit-field. (See the `field_byte_offset'
15033 function above).
15035 For any given bit-field, the "containing object" is a hypothetical object
15036 (of some integral or enum type) within which the given bit-field lives. The
15037 type of this hypothetical "containing object" is always the same as the
15038 declared type of the individual bit-field itself (for GCC anyway... the
15039 DWARF spec doesn't actually mandate this). Note that it is the size (in
15040 bytes) of the hypothetical "containing object" which will be given in the
15041 DW_AT_byte_size attribute for this bit-field. (See the
15042 `byte_size_attribute' function below.) It is also used when calculating the
15043 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15044 function below.) */
15046 static void
15047 add_data_member_location_attribute (dw_die_ref die, tree decl)
15049 HOST_WIDE_INT offset;
15050 dw_loc_descr_ref loc_descr = 0;
15052 if (TREE_CODE (decl) == TREE_BINFO)
15054 /* We're working on the TAG_inheritance for a base class. */
15055 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15057 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15058 aren't at a fixed offset from all (sub)objects of the same
15059 type. We need to extract the appropriate offset from our
15060 vtable. The following dwarf expression means
15062 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15064 This is specific to the V3 ABI, of course. */
15066 dw_loc_descr_ref tmp;
15068 /* Make a copy of the object address. */
15069 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15070 add_loc_descr (&loc_descr, tmp);
15072 /* Extract the vtable address. */
15073 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15074 add_loc_descr (&loc_descr, tmp);
15076 /* Calculate the address of the offset. */
15077 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15078 gcc_assert (offset < 0);
15080 tmp = int_loc_descriptor (-offset);
15081 add_loc_descr (&loc_descr, tmp);
15082 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15083 add_loc_descr (&loc_descr, tmp);
15085 /* Extract the offset. */
15086 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15087 add_loc_descr (&loc_descr, tmp);
15089 /* Add it to the object address. */
15090 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15091 add_loc_descr (&loc_descr, tmp);
15093 else
15094 offset = tree_to_shwi (BINFO_OFFSET (decl));
15096 else
15097 offset = field_byte_offset (decl);
15099 if (! loc_descr)
15101 if (dwarf_version > 2)
15103 /* Don't need to output a location expression, just the constant. */
15104 if (offset < 0)
15105 add_AT_int (die, DW_AT_data_member_location, offset);
15106 else
15107 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15108 return;
15110 else
15112 enum dwarf_location_atom op;
15114 /* The DWARF2 standard says that we should assume that the structure
15115 address is already on the stack, so we can specify a structure
15116 field address by using DW_OP_plus_uconst. */
15117 op = DW_OP_plus_uconst;
15118 loc_descr = new_loc_descr (op, offset, 0);
15122 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15125 /* Writes integer values to dw_vec_const array. */
15127 static void
15128 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15130 while (size != 0)
15132 *dest++ = val & 0xff;
15133 val >>= 8;
15134 --size;
15138 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15140 static HOST_WIDE_INT
15141 extract_int (const unsigned char *src, unsigned int size)
15143 HOST_WIDE_INT val = 0;
15145 src += size;
15146 while (size != 0)
15148 val <<= 8;
15149 val |= *--src & 0xff;
15150 --size;
15152 return val;
15155 /* Writes wide_int values to dw_vec_const array. */
15157 static void
15158 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15160 int i;
15162 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15164 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15165 return;
15168 /* We'd have to extend this code to support odd sizes. */
15169 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15171 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15173 if (WORDS_BIG_ENDIAN)
15174 for (i = n - 1; i >= 0; i--)
15176 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15177 dest += sizeof (HOST_WIDE_INT);
15179 else
15180 for (i = 0; i < n; i++)
15182 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15183 dest += sizeof (HOST_WIDE_INT);
15187 /* Writes floating point values to dw_vec_const array. */
15189 static void
15190 insert_float (const_rtx rtl, unsigned char *array)
15192 REAL_VALUE_TYPE rv;
15193 long val[4];
15194 int i;
15196 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15197 real_to_target (val, &rv, GET_MODE (rtl));
15199 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15200 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15202 insert_int (val[i], 4, array);
15203 array += 4;
15207 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15208 does not have a "location" either in memory or in a register. These
15209 things can arise in GNU C when a constant is passed as an actual parameter
15210 to an inlined function. They can also arise in C++ where declared
15211 constants do not necessarily get memory "homes". */
15213 static bool
15214 add_const_value_attribute (dw_die_ref die, rtx rtl)
15216 switch (GET_CODE (rtl))
15218 case CONST_INT:
15220 HOST_WIDE_INT val = INTVAL (rtl);
15222 if (val < 0)
15223 add_AT_int (die, DW_AT_const_value, val);
15224 else
15225 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15227 return true;
15229 case CONST_WIDE_INT:
15230 add_AT_wide (die, DW_AT_const_value,
15231 std::make_pair (rtl, GET_MODE (rtl)));
15232 return true;
15234 case CONST_DOUBLE:
15235 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15236 floating-point constant. A CONST_DOUBLE is used whenever the
15237 constant requires more than one word in order to be adequately
15238 represented. */
15240 enum machine_mode mode = GET_MODE (rtl);
15242 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15243 add_AT_double (die, DW_AT_const_value,
15244 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15245 else
15247 unsigned int length = GET_MODE_SIZE (mode);
15248 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15250 insert_float (rtl, array);
15251 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15254 return true;
15256 case CONST_VECTOR:
15258 enum machine_mode mode = GET_MODE (rtl);
15259 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15260 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15261 unsigned char *array
15262 = ggc_vec_alloc<unsigned char> (length * elt_size);
15263 unsigned int i;
15264 unsigned char *p;
15265 enum machine_mode imode = GET_MODE_INNER (mode);
15267 switch (GET_MODE_CLASS (mode))
15269 case MODE_VECTOR_INT:
15270 for (i = 0, p = array; i < length; i++, p += elt_size)
15272 rtx elt = CONST_VECTOR_ELT (rtl, i);
15273 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15275 break;
15277 case MODE_VECTOR_FLOAT:
15278 for (i = 0, p = array; i < length; i++, p += elt_size)
15280 rtx elt = CONST_VECTOR_ELT (rtl, i);
15281 insert_float (elt, p);
15283 break;
15285 default:
15286 gcc_unreachable ();
15289 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15291 return true;
15293 case CONST_STRING:
15294 if (dwarf_version >= 4 || !dwarf_strict)
15296 dw_loc_descr_ref loc_result;
15297 resolve_one_addr (&rtl, NULL);
15298 rtl_addr:
15299 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15300 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15301 add_AT_loc (die, DW_AT_location, loc_result);
15302 vec_safe_push (used_rtx_array, rtl);
15303 return true;
15305 return false;
15307 case CONST:
15308 if (CONSTANT_P (XEXP (rtl, 0)))
15309 return add_const_value_attribute (die, XEXP (rtl, 0));
15310 /* FALLTHROUGH */
15311 case SYMBOL_REF:
15312 if (!const_ok_for_output (rtl))
15313 return false;
15314 case LABEL_REF:
15315 if (dwarf_version >= 4 || !dwarf_strict)
15316 goto rtl_addr;
15317 return false;
15319 case PLUS:
15320 /* In cases where an inlined instance of an inline function is passed
15321 the address of an `auto' variable (which is local to the caller) we
15322 can get a situation where the DECL_RTL of the artificial local
15323 variable (for the inlining) which acts as a stand-in for the
15324 corresponding formal parameter (of the inline function) will look
15325 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15326 exactly a compile-time constant expression, but it isn't the address
15327 of the (artificial) local variable either. Rather, it represents the
15328 *value* which the artificial local variable always has during its
15329 lifetime. We currently have no way to represent such quasi-constant
15330 values in Dwarf, so for now we just punt and generate nothing. */
15331 return false;
15333 case HIGH:
15334 case CONST_FIXED:
15335 return false;
15337 case MEM:
15338 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15339 && MEM_READONLY_P (rtl)
15340 && GET_MODE (rtl) == BLKmode)
15342 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15343 return true;
15345 return false;
15347 default:
15348 /* No other kinds of rtx should be possible here. */
15349 gcc_unreachable ();
15351 return false;
15354 /* Determine whether the evaluation of EXPR references any variables
15355 or functions which aren't otherwise used (and therefore may not be
15356 output). */
15357 static tree
15358 reference_to_unused (tree * tp, int * walk_subtrees,
15359 void * data ATTRIBUTE_UNUSED)
15361 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15362 *walk_subtrees = 0;
15364 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15365 && ! TREE_ASM_WRITTEN (*tp))
15366 return *tp;
15367 /* ??? The C++ FE emits debug information for using decls, so
15368 putting gcc_unreachable here falls over. See PR31899. For now
15369 be conservative. */
15370 else if (!cgraph_global_info_ready
15371 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15372 return *tp;
15373 else if (TREE_CODE (*tp) == VAR_DECL)
15375 varpool_node *node = varpool_get_node (*tp);
15376 if (!node || !node->definition)
15377 return *tp;
15379 else if (TREE_CODE (*tp) == FUNCTION_DECL
15380 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15382 /* The call graph machinery must have finished analyzing,
15383 optimizing and gimplifying the CU by now.
15384 So if *TP has no call graph node associated
15385 to it, it means *TP will not be emitted. */
15386 if (!cgraph_get_node (*tp))
15387 return *tp;
15389 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15390 return *tp;
15392 return NULL_TREE;
15395 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15396 for use in a later add_const_value_attribute call. */
15398 static rtx
15399 rtl_for_decl_init (tree init, tree type)
15401 rtx rtl = NULL_RTX;
15403 STRIP_NOPS (init);
15405 /* If a variable is initialized with a string constant without embedded
15406 zeros, build CONST_STRING. */
15407 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15409 tree enttype = TREE_TYPE (type);
15410 tree domain = TYPE_DOMAIN (type);
15411 enum machine_mode mode = TYPE_MODE (enttype);
15413 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15414 && domain
15415 && integer_zerop (TYPE_MIN_VALUE (domain))
15416 && compare_tree_int (TYPE_MAX_VALUE (domain),
15417 TREE_STRING_LENGTH (init) - 1) == 0
15418 && ((size_t) TREE_STRING_LENGTH (init)
15419 == strlen (TREE_STRING_POINTER (init)) + 1))
15421 rtl = gen_rtx_CONST_STRING (VOIDmode,
15422 ggc_strdup (TREE_STRING_POINTER (init)));
15423 rtl = gen_rtx_MEM (BLKmode, rtl);
15424 MEM_READONLY_P (rtl) = 1;
15427 /* Other aggregates, and complex values, could be represented using
15428 CONCAT: FIXME! */
15429 else if (AGGREGATE_TYPE_P (type)
15430 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15431 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15432 || TREE_CODE (type) == COMPLEX_TYPE)
15434 /* Vectors only work if their mode is supported by the target.
15435 FIXME: generic vectors ought to work too. */
15436 else if (TREE_CODE (type) == VECTOR_TYPE
15437 && !VECTOR_MODE_P (TYPE_MODE (type)))
15439 /* If the initializer is something that we know will expand into an
15440 immediate RTL constant, expand it now. We must be careful not to
15441 reference variables which won't be output. */
15442 else if (initializer_constant_valid_p (init, type)
15443 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15445 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15446 possible. */
15447 if (TREE_CODE (type) == VECTOR_TYPE)
15448 switch (TREE_CODE (init))
15450 case VECTOR_CST:
15451 break;
15452 case CONSTRUCTOR:
15453 if (TREE_CONSTANT (init))
15455 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15456 bool constant_p = true;
15457 tree value;
15458 unsigned HOST_WIDE_INT ix;
15460 /* Even when ctor is constant, it might contain non-*_CST
15461 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15462 belong into VECTOR_CST nodes. */
15463 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15464 if (!CONSTANT_CLASS_P (value))
15466 constant_p = false;
15467 break;
15470 if (constant_p)
15472 init = build_vector_from_ctor (type, elts);
15473 break;
15476 /* FALLTHRU */
15478 default:
15479 return NULL;
15482 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15484 /* If expand_expr returns a MEM, it wasn't immediate. */
15485 gcc_assert (!rtl || !MEM_P (rtl));
15488 return rtl;
15491 /* Generate RTL for the variable DECL to represent its location. */
15493 static rtx
15494 rtl_for_decl_location (tree decl)
15496 rtx rtl;
15498 /* Here we have to decide where we are going to say the parameter "lives"
15499 (as far as the debugger is concerned). We only have a couple of
15500 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15502 DECL_RTL normally indicates where the parameter lives during most of the
15503 activation of the function. If optimization is enabled however, this
15504 could be either NULL or else a pseudo-reg. Both of those cases indicate
15505 that the parameter doesn't really live anywhere (as far as the code
15506 generation parts of GCC are concerned) during most of the function's
15507 activation. That will happen (for example) if the parameter is never
15508 referenced within the function.
15510 We could just generate a location descriptor here for all non-NULL
15511 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15512 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15513 where DECL_RTL is NULL or is a pseudo-reg.
15515 Note however that we can only get away with using DECL_INCOMING_RTL as
15516 a backup substitute for DECL_RTL in certain limited cases. In cases
15517 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15518 we can be sure that the parameter was passed using the same type as it is
15519 declared to have within the function, and that its DECL_INCOMING_RTL
15520 points us to a place where a value of that type is passed.
15522 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15523 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15524 because in these cases DECL_INCOMING_RTL points us to a value of some
15525 type which is *different* from the type of the parameter itself. Thus,
15526 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15527 such cases, the debugger would end up (for example) trying to fetch a
15528 `float' from a place which actually contains the first part of a
15529 `double'. That would lead to really incorrect and confusing
15530 output at debug-time.
15532 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15533 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15534 are a couple of exceptions however. On little-endian machines we can
15535 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15536 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15537 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15538 when (on a little-endian machine) a non-prototyped function has a
15539 parameter declared to be of type `short' or `char'. In such cases,
15540 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15541 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15542 passed `int' value. If the debugger then uses that address to fetch
15543 a `short' or a `char' (on a little-endian machine) the result will be
15544 the correct data, so we allow for such exceptional cases below.
15546 Note that our goal here is to describe the place where the given formal
15547 parameter lives during most of the function's activation (i.e. between the
15548 end of the prologue and the start of the epilogue). We'll do that as best
15549 as we can. Note however that if the given formal parameter is modified
15550 sometime during the execution of the function, then a stack backtrace (at
15551 debug-time) will show the function as having been called with the *new*
15552 value rather than the value which was originally passed in. This happens
15553 rarely enough that it is not a major problem, but it *is* a problem, and
15554 I'd like to fix it.
15556 A future version of dwarf2out.c may generate two additional attributes for
15557 any given DW_TAG_formal_parameter DIE which will describe the "passed
15558 type" and the "passed location" for the given formal parameter in addition
15559 to the attributes we now generate to indicate the "declared type" and the
15560 "active location" for each parameter. This additional set of attributes
15561 could be used by debuggers for stack backtraces. Separately, note that
15562 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15563 This happens (for example) for inlined-instances of inline function formal
15564 parameters which are never referenced. This really shouldn't be
15565 happening. All PARM_DECL nodes should get valid non-NULL
15566 DECL_INCOMING_RTL values. FIXME. */
15568 /* Use DECL_RTL as the "location" unless we find something better. */
15569 rtl = DECL_RTL_IF_SET (decl);
15571 /* When generating abstract instances, ignore everything except
15572 constants, symbols living in memory, and symbols living in
15573 fixed registers. */
15574 if (! reload_completed)
15576 if (rtl
15577 && (CONSTANT_P (rtl)
15578 || (MEM_P (rtl)
15579 && CONSTANT_P (XEXP (rtl, 0)))
15580 || (REG_P (rtl)
15581 && TREE_CODE (decl) == VAR_DECL
15582 && TREE_STATIC (decl))))
15584 rtl = targetm.delegitimize_address (rtl);
15585 return rtl;
15587 rtl = NULL_RTX;
15589 else if (TREE_CODE (decl) == PARM_DECL)
15591 if (rtl == NULL_RTX
15592 || is_pseudo_reg (rtl)
15593 || (MEM_P (rtl)
15594 && is_pseudo_reg (XEXP (rtl, 0))
15595 && DECL_INCOMING_RTL (decl)
15596 && MEM_P (DECL_INCOMING_RTL (decl))
15597 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15599 tree declared_type = TREE_TYPE (decl);
15600 tree passed_type = DECL_ARG_TYPE (decl);
15601 enum machine_mode dmode = TYPE_MODE (declared_type);
15602 enum machine_mode pmode = TYPE_MODE (passed_type);
15604 /* This decl represents a formal parameter which was optimized out.
15605 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15606 all cases where (rtl == NULL_RTX) just below. */
15607 if (dmode == pmode)
15608 rtl = DECL_INCOMING_RTL (decl);
15609 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15610 && SCALAR_INT_MODE_P (dmode)
15611 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15612 && DECL_INCOMING_RTL (decl))
15614 rtx inc = DECL_INCOMING_RTL (decl);
15615 if (REG_P (inc))
15616 rtl = inc;
15617 else if (MEM_P (inc))
15619 if (BYTES_BIG_ENDIAN)
15620 rtl = adjust_address_nv (inc, dmode,
15621 GET_MODE_SIZE (pmode)
15622 - GET_MODE_SIZE (dmode));
15623 else
15624 rtl = inc;
15629 /* If the parm was passed in registers, but lives on the stack, then
15630 make a big endian correction if the mode of the type of the
15631 parameter is not the same as the mode of the rtl. */
15632 /* ??? This is the same series of checks that are made in dbxout.c before
15633 we reach the big endian correction code there. It isn't clear if all
15634 of these checks are necessary here, but keeping them all is the safe
15635 thing to do. */
15636 else if (MEM_P (rtl)
15637 && XEXP (rtl, 0) != const0_rtx
15638 && ! CONSTANT_P (XEXP (rtl, 0))
15639 /* Not passed in memory. */
15640 && !MEM_P (DECL_INCOMING_RTL (decl))
15641 /* Not passed by invisible reference. */
15642 && (!REG_P (XEXP (rtl, 0))
15643 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15644 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15645 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15646 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15647 #endif
15649 /* Big endian correction check. */
15650 && BYTES_BIG_ENDIAN
15651 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15652 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15653 < UNITS_PER_WORD))
15655 enum machine_mode addr_mode = get_address_mode (rtl);
15656 int offset = (UNITS_PER_WORD
15657 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15659 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15660 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15663 else if (TREE_CODE (decl) == VAR_DECL
15664 && rtl
15665 && MEM_P (rtl)
15666 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15667 && BYTES_BIG_ENDIAN)
15669 enum machine_mode addr_mode = get_address_mode (rtl);
15670 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15671 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15673 /* If a variable is declared "register" yet is smaller than
15674 a register, then if we store the variable to memory, it
15675 looks like we're storing a register-sized value, when in
15676 fact we are not. We need to adjust the offset of the
15677 storage location to reflect the actual value's bytes,
15678 else gdb will not be able to display it. */
15679 if (rsize > dsize)
15680 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15681 plus_constant (addr_mode, XEXP (rtl, 0),
15682 rsize - dsize));
15685 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15686 and will have been substituted directly into all expressions that use it.
15687 C does not have such a concept, but C++ and other languages do. */
15688 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15689 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15691 if (rtl)
15692 rtl = targetm.delegitimize_address (rtl);
15694 /* If we don't look past the constant pool, we risk emitting a
15695 reference to a constant pool entry that isn't referenced from
15696 code, and thus is not emitted. */
15697 if (rtl)
15698 rtl = avoid_constant_pool_reference (rtl);
15700 /* Try harder to get a rtl. If this symbol ends up not being emitted
15701 in the current CU, resolve_addr will remove the expression referencing
15702 it. */
15703 if (rtl == NULL_RTX
15704 && TREE_CODE (decl) == VAR_DECL
15705 && !DECL_EXTERNAL (decl)
15706 && TREE_STATIC (decl)
15707 && DECL_NAME (decl)
15708 && !DECL_HARD_REGISTER (decl)
15709 && DECL_MODE (decl) != VOIDmode)
15711 rtl = make_decl_rtl_for_debug (decl);
15712 if (!MEM_P (rtl)
15713 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15714 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15715 rtl = NULL_RTX;
15718 return rtl;
15721 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15722 returned. If so, the decl for the COMMON block is returned, and the
15723 value is the offset into the common block for the symbol. */
15725 static tree
15726 fortran_common (tree decl, HOST_WIDE_INT *value)
15728 tree val_expr, cvar;
15729 enum machine_mode mode;
15730 HOST_WIDE_INT bitsize, bitpos;
15731 tree offset;
15732 int unsignedp, volatilep = 0;
15734 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15735 it does not have a value (the offset into the common area), or if it
15736 is thread local (as opposed to global) then it isn't common, and shouldn't
15737 be handled as such. */
15738 if (TREE_CODE (decl) != VAR_DECL
15739 || !TREE_STATIC (decl)
15740 || !DECL_HAS_VALUE_EXPR_P (decl)
15741 || !is_fortran ())
15742 return NULL_TREE;
15744 val_expr = DECL_VALUE_EXPR (decl);
15745 if (TREE_CODE (val_expr) != COMPONENT_REF)
15746 return NULL_TREE;
15748 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15749 &mode, &unsignedp, &volatilep, true);
15751 if (cvar == NULL_TREE
15752 || TREE_CODE (cvar) != VAR_DECL
15753 || DECL_ARTIFICIAL (cvar)
15754 || !TREE_PUBLIC (cvar))
15755 return NULL_TREE;
15757 *value = 0;
15758 if (offset != NULL)
15760 if (!tree_fits_shwi_p (offset))
15761 return NULL_TREE;
15762 *value = tree_to_shwi (offset);
15764 if (bitpos != 0)
15765 *value += bitpos / BITS_PER_UNIT;
15767 return cvar;
15770 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15771 data attribute for a variable or a parameter. We generate the
15772 DW_AT_const_value attribute only in those cases where the given variable
15773 or parameter does not have a true "location" either in memory or in a
15774 register. This can happen (for example) when a constant is passed as an
15775 actual argument in a call to an inline function. (It's possible that
15776 these things can crop up in other ways also.) Note that one type of
15777 constant value which can be passed into an inlined function is a constant
15778 pointer. This can happen for example if an actual argument in an inlined
15779 function call evaluates to a compile-time constant address.
15781 CACHE_P is true if it is worth caching the location list for DECL,
15782 so that future calls can reuse it rather than regenerate it from scratch.
15783 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15784 since we will need to refer to them each time the function is inlined. */
15786 static bool
15787 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15788 enum dwarf_attribute attr)
15790 rtx rtl;
15791 dw_loc_list_ref list;
15792 var_loc_list *loc_list;
15793 cached_dw_loc_list *cache;
15794 void **slot;
15796 if (TREE_CODE (decl) == ERROR_MARK)
15797 return false;
15799 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15800 || TREE_CODE (decl) == RESULT_DECL);
15802 /* Try to get some constant RTL for this decl, and use that as the value of
15803 the location. */
15805 rtl = rtl_for_decl_location (decl);
15806 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15807 && add_const_value_attribute (die, rtl))
15808 return true;
15810 /* See if we have single element location list that is equivalent to
15811 a constant value. That way we are better to use add_const_value_attribute
15812 rather than expanding constant value equivalent. */
15813 loc_list = lookup_decl_loc (decl);
15814 if (loc_list
15815 && loc_list->first
15816 && loc_list->first->next == NULL
15817 && NOTE_P (loc_list->first->loc)
15818 && NOTE_VAR_LOCATION (loc_list->first->loc)
15819 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15821 struct var_loc_node *node;
15823 node = loc_list->first;
15824 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15825 if (GET_CODE (rtl) == EXPR_LIST)
15826 rtl = XEXP (rtl, 0);
15827 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15828 && add_const_value_attribute (die, rtl))
15829 return true;
15831 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15832 list several times. See if we've already cached the contents. */
15833 list = NULL;
15834 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15835 cache_p = false;
15836 if (cache_p)
15838 cache = (cached_dw_loc_list *)
15839 htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15840 if (cache)
15841 list = cache->loc_list;
15843 if (list == NULL)
15845 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15846 /* It is usually worth caching this result if the decl is from
15847 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15848 if (cache_p && list && list->dw_loc_next)
15850 slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15851 DECL_UID (decl), INSERT);
15852 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15853 cache->decl_id = DECL_UID (decl);
15854 cache->loc_list = list;
15855 *slot = cache;
15858 if (list)
15860 add_AT_location_description (die, attr, list);
15861 return true;
15863 /* None of that worked, so it must not really have a location;
15864 try adding a constant value attribute from the DECL_INITIAL. */
15865 return tree_add_const_value_attribute_for_decl (die, decl);
15868 /* Add VARIABLE and DIE into deferred locations list. */
15870 static void
15871 defer_location (tree variable, dw_die_ref die)
15873 deferred_locations entry;
15874 entry.variable = variable;
15875 entry.die = die;
15876 vec_safe_push (deferred_locations_list, entry);
15879 /* Helper function for tree_add_const_value_attribute. Natively encode
15880 initializer INIT into an array. Return true if successful. */
15882 static bool
15883 native_encode_initializer (tree init, unsigned char *array, int size)
15885 tree type;
15887 if (init == NULL_TREE)
15888 return false;
15890 STRIP_NOPS (init);
15891 switch (TREE_CODE (init))
15893 case STRING_CST:
15894 type = TREE_TYPE (init);
15895 if (TREE_CODE (type) == ARRAY_TYPE)
15897 tree enttype = TREE_TYPE (type);
15898 enum machine_mode mode = TYPE_MODE (enttype);
15900 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15901 return false;
15902 if (int_size_in_bytes (type) != size)
15903 return false;
15904 if (size > TREE_STRING_LENGTH (init))
15906 memcpy (array, TREE_STRING_POINTER (init),
15907 TREE_STRING_LENGTH (init));
15908 memset (array + TREE_STRING_LENGTH (init),
15909 '\0', size - TREE_STRING_LENGTH (init));
15911 else
15912 memcpy (array, TREE_STRING_POINTER (init), size);
15913 return true;
15915 return false;
15916 case CONSTRUCTOR:
15917 type = TREE_TYPE (init);
15918 if (int_size_in_bytes (type) != size)
15919 return false;
15920 if (TREE_CODE (type) == ARRAY_TYPE)
15922 HOST_WIDE_INT min_index;
15923 unsigned HOST_WIDE_INT cnt;
15924 int curpos = 0, fieldsize;
15925 constructor_elt *ce;
15927 if (TYPE_DOMAIN (type) == NULL_TREE
15928 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15929 return false;
15931 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15932 if (fieldsize <= 0)
15933 return false;
15935 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15936 memset (array, '\0', size);
15937 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15939 tree val = ce->value;
15940 tree index = ce->index;
15941 int pos = curpos;
15942 if (index && TREE_CODE (index) == RANGE_EXPR)
15943 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15944 * fieldsize;
15945 else if (index)
15946 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15948 if (val)
15950 STRIP_NOPS (val);
15951 if (!native_encode_initializer (val, array + pos, fieldsize))
15952 return false;
15954 curpos = pos + fieldsize;
15955 if (index && TREE_CODE (index) == RANGE_EXPR)
15957 int count = tree_to_shwi (TREE_OPERAND (index, 1))
15958 - tree_to_shwi (TREE_OPERAND (index, 0));
15959 while (count-- > 0)
15961 if (val)
15962 memcpy (array + curpos, array + pos, fieldsize);
15963 curpos += fieldsize;
15966 gcc_assert (curpos <= size);
15968 return true;
15970 else if (TREE_CODE (type) == RECORD_TYPE
15971 || TREE_CODE (type) == UNION_TYPE)
15973 tree field = NULL_TREE;
15974 unsigned HOST_WIDE_INT cnt;
15975 constructor_elt *ce;
15977 if (int_size_in_bytes (type) != size)
15978 return false;
15980 if (TREE_CODE (type) == RECORD_TYPE)
15981 field = TYPE_FIELDS (type);
15983 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15985 tree val = ce->value;
15986 int pos, fieldsize;
15988 if (ce->index != 0)
15989 field = ce->index;
15991 if (val)
15992 STRIP_NOPS (val);
15994 if (field == NULL_TREE || DECL_BIT_FIELD (field))
15995 return false;
15997 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15998 && TYPE_DOMAIN (TREE_TYPE (field))
15999 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16000 return false;
16001 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16002 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16003 return false;
16004 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16005 pos = int_byte_position (field);
16006 gcc_assert (pos + fieldsize <= size);
16007 if (val
16008 && !native_encode_initializer (val, array + pos, fieldsize))
16009 return false;
16011 return true;
16013 return false;
16014 case VIEW_CONVERT_EXPR:
16015 case NON_LVALUE_EXPR:
16016 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16017 default:
16018 return native_encode_expr (init, array, size) == size;
16022 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16023 attribute is the const value T. */
16025 static bool
16026 tree_add_const_value_attribute (dw_die_ref die, tree t)
16028 tree init;
16029 tree type = TREE_TYPE (t);
16030 rtx rtl;
16032 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16033 return false;
16035 init = t;
16036 gcc_assert (!DECL_P (init));
16038 rtl = rtl_for_decl_init (init, type);
16039 if (rtl)
16040 return add_const_value_attribute (die, rtl);
16041 /* If the host and target are sane, try harder. */
16042 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16043 && initializer_constant_valid_p (init, type))
16045 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16046 if (size > 0 && (int) size == size)
16048 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16050 if (native_encode_initializer (init, array, size))
16052 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16053 return true;
16055 ggc_free (array);
16058 return false;
16061 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16062 attribute is the const value of T, where T is an integral constant
16063 variable with static storage duration
16064 (so it can't be a PARM_DECL or a RESULT_DECL). */
16066 static bool
16067 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16070 if (!decl
16071 || (TREE_CODE (decl) != VAR_DECL
16072 && TREE_CODE (decl) != CONST_DECL)
16073 || (TREE_CODE (decl) == VAR_DECL
16074 && !TREE_STATIC (decl)))
16075 return false;
16077 if (TREE_READONLY (decl)
16078 && ! TREE_THIS_VOLATILE (decl)
16079 && DECL_INITIAL (decl))
16080 /* OK */;
16081 else
16082 return false;
16084 /* Don't add DW_AT_const_value if abstract origin already has one. */
16085 if (get_AT (var_die, DW_AT_const_value))
16086 return false;
16088 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16091 /* Convert the CFI instructions for the current function into a
16092 location list. This is used for DW_AT_frame_base when we targeting
16093 a dwarf2 consumer that does not support the dwarf3
16094 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16095 expressions. */
16097 static dw_loc_list_ref
16098 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16100 int ix;
16101 dw_fde_ref fde;
16102 dw_loc_list_ref list, *list_tail;
16103 dw_cfi_ref cfi;
16104 dw_cfa_location last_cfa, next_cfa;
16105 const char *start_label, *last_label, *section;
16106 dw_cfa_location remember;
16108 fde = cfun->fde;
16109 gcc_assert (fde != NULL);
16111 section = secname_for_decl (current_function_decl);
16112 list_tail = &list;
16113 list = NULL;
16115 memset (&next_cfa, 0, sizeof (next_cfa));
16116 next_cfa.reg = INVALID_REGNUM;
16117 remember = next_cfa;
16119 start_label = fde->dw_fde_begin;
16121 /* ??? Bald assumption that the CIE opcode list does not contain
16122 advance opcodes. */
16123 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16124 lookup_cfa_1 (cfi, &next_cfa, &remember);
16126 last_cfa = next_cfa;
16127 last_label = start_label;
16129 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16131 /* If the first partition contained no CFI adjustments, the
16132 CIE opcodes apply to the whole first partition. */
16133 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16134 fde->dw_fde_begin, fde->dw_fde_end, section);
16135 list_tail =&(*list_tail)->dw_loc_next;
16136 start_label = last_label = fde->dw_fde_second_begin;
16139 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16141 switch (cfi->dw_cfi_opc)
16143 case DW_CFA_set_loc:
16144 case DW_CFA_advance_loc1:
16145 case DW_CFA_advance_loc2:
16146 case DW_CFA_advance_loc4:
16147 if (!cfa_equal_p (&last_cfa, &next_cfa))
16149 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16150 start_label, last_label, section);
16152 list_tail = &(*list_tail)->dw_loc_next;
16153 last_cfa = next_cfa;
16154 start_label = last_label;
16156 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16157 break;
16159 case DW_CFA_advance_loc:
16160 /* The encoding is complex enough that we should never emit this. */
16161 gcc_unreachable ();
16163 default:
16164 lookup_cfa_1 (cfi, &next_cfa, &remember);
16165 break;
16167 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16169 if (!cfa_equal_p (&last_cfa, &next_cfa))
16171 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16172 start_label, last_label, section);
16174 list_tail = &(*list_tail)->dw_loc_next;
16175 last_cfa = next_cfa;
16176 start_label = last_label;
16178 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16179 start_label, fde->dw_fde_end, section);
16180 list_tail = &(*list_tail)->dw_loc_next;
16181 start_label = last_label = fde->dw_fde_second_begin;
16185 if (!cfa_equal_p (&last_cfa, &next_cfa))
16187 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16188 start_label, last_label, section);
16189 list_tail = &(*list_tail)->dw_loc_next;
16190 start_label = last_label;
16193 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16194 start_label,
16195 fde->dw_fde_second_begin
16196 ? fde->dw_fde_second_end : fde->dw_fde_end,
16197 section);
16199 if (list && list->dw_loc_next)
16200 gen_llsym (list);
16202 return list;
16205 /* Compute a displacement from the "steady-state frame pointer" to the
16206 frame base (often the same as the CFA), and store it in
16207 frame_pointer_fb_offset. OFFSET is added to the displacement
16208 before the latter is negated. */
16210 static void
16211 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16213 rtx reg, elim;
16215 #ifdef FRAME_POINTER_CFA_OFFSET
16216 reg = frame_pointer_rtx;
16217 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16218 #else
16219 reg = arg_pointer_rtx;
16220 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16221 #endif
16223 elim = (ira_use_lra_p
16224 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16225 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16226 if (GET_CODE (elim) == PLUS)
16228 offset += INTVAL (XEXP (elim, 1));
16229 elim = XEXP (elim, 0);
16232 frame_pointer_fb_offset = -offset;
16234 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16235 in which to eliminate. This is because it's stack pointer isn't
16236 directly accessible as a register within the ISA. To work around
16237 this, assume that while we cannot provide a proper value for
16238 frame_pointer_fb_offset, we won't need one either. */
16239 frame_pointer_fb_offset_valid
16240 = ((SUPPORTS_STACK_ALIGNMENT
16241 && (elim == hard_frame_pointer_rtx
16242 || elim == stack_pointer_rtx))
16243 || elim == (frame_pointer_needed
16244 ? hard_frame_pointer_rtx
16245 : stack_pointer_rtx));
16248 /* Generate a DW_AT_name attribute given some string value to be included as
16249 the value of the attribute. */
16251 static void
16252 add_name_attribute (dw_die_ref die, const char *name_string)
16254 if (name_string != NULL && *name_string != 0)
16256 if (demangle_name_func)
16257 name_string = (*demangle_name_func) (name_string);
16259 add_AT_string (die, DW_AT_name, name_string);
16263 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16264 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16265 of TYPE accordingly.
16267 ??? This is a temporary measure until after we're able to generate
16268 regular DWARF for the complex Ada type system. */
16270 static void
16271 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16272 dw_die_ref context_die)
16274 tree dtype;
16275 dw_die_ref dtype_die;
16277 if (!lang_hooks.types.descriptive_type)
16278 return;
16280 dtype = lang_hooks.types.descriptive_type (type);
16281 if (!dtype)
16282 return;
16284 dtype_die = lookup_type_die (dtype);
16285 if (!dtype_die)
16287 gen_type_die (dtype, context_die);
16288 dtype_die = lookup_type_die (dtype);
16289 gcc_assert (dtype_die);
16292 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16295 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16297 static const char *
16298 comp_dir_string (void)
16300 const char *wd;
16301 char *wd1;
16302 static const char *cached_wd = NULL;
16304 if (cached_wd != NULL)
16305 return cached_wd;
16307 wd = get_src_pwd ();
16308 if (wd == NULL)
16309 return NULL;
16311 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16313 int wdlen;
16315 wdlen = strlen (wd);
16316 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16317 strcpy (wd1, wd);
16318 wd1 [wdlen] = DIR_SEPARATOR;
16319 wd1 [wdlen + 1] = 0;
16320 wd = wd1;
16323 cached_wd = remap_debug_filename (wd);
16324 return cached_wd;
16327 /* Generate a DW_AT_comp_dir attribute for DIE. */
16329 static void
16330 add_comp_dir_attribute (dw_die_ref die)
16332 const char * wd = comp_dir_string ();
16333 if (wd != NULL)
16334 add_AT_string (die, DW_AT_comp_dir, wd);
16337 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16338 default. */
16340 static int
16341 lower_bound_default (void)
16343 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16345 case DW_LANG_C:
16346 case DW_LANG_C89:
16347 case DW_LANG_C99:
16348 case DW_LANG_C_plus_plus:
16349 case DW_LANG_ObjC:
16350 case DW_LANG_ObjC_plus_plus:
16351 case DW_LANG_Java:
16352 return 0;
16353 case DW_LANG_Fortran77:
16354 case DW_LANG_Fortran90:
16355 case DW_LANG_Fortran95:
16356 return 1;
16357 case DW_LANG_UPC:
16358 case DW_LANG_D:
16359 case DW_LANG_Python:
16360 return dwarf_version >= 4 ? 0 : -1;
16361 case DW_LANG_Ada95:
16362 case DW_LANG_Ada83:
16363 case DW_LANG_Cobol74:
16364 case DW_LANG_Cobol85:
16365 case DW_LANG_Pascal83:
16366 case DW_LANG_Modula2:
16367 case DW_LANG_PLI:
16368 return dwarf_version >= 4 ? 1 : -1;
16369 default:
16370 return -1;
16374 /* Given a tree node describing an array bound (either lower or upper) output
16375 a representation for that bound. */
16377 static void
16378 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16380 switch (TREE_CODE (bound))
16382 case ERROR_MARK:
16383 return;
16385 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16386 case INTEGER_CST:
16388 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16389 int dflt;
16391 /* Use the default if possible. */
16392 if (bound_attr == DW_AT_lower_bound
16393 && tree_fits_shwi_p (bound)
16394 && (dflt = lower_bound_default ()) != -1
16395 && tree_to_shwi (bound) == dflt)
16398 /* If HOST_WIDE_INT is big enough then represent the bound as
16399 a constant value. We need to choose a form based on
16400 whether the type is signed or unsigned. We cannot just
16401 call add_AT_unsigned if the value itself is positive
16402 (add_AT_unsigned might add the unsigned value encoded as
16403 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16404 bounds type and then sign extend any unsigned values found
16405 for signed types. This is needed only for
16406 DW_AT_{lower,upper}_bound, since for most other attributes,
16407 consumers will treat DW_FORM_data[1248] as unsigned values,
16408 regardless of the underlying type. */
16409 else if (prec <= HOST_BITS_PER_WIDE_INT
16410 || tree_fits_uhwi_p (bound))
16412 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16413 add_AT_unsigned (subrange_die, bound_attr,
16414 TREE_INT_CST_LOW (bound));
16415 else
16416 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16418 else
16419 /* Otherwise represent the bound as an unsigned value with
16420 the precision of its type. The precision and signedness
16421 of the type will be necessary to re-interpret it
16422 unambiguously. */
16423 add_AT_wide (subrange_die, bound_attr, bound);
16425 break;
16427 CASE_CONVERT:
16428 case VIEW_CONVERT_EXPR:
16429 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16430 break;
16432 case SAVE_EXPR:
16433 break;
16435 case VAR_DECL:
16436 case PARM_DECL:
16437 case RESULT_DECL:
16439 dw_die_ref decl_die = lookup_decl_die (bound);
16441 /* ??? Can this happen, or should the variable have been bound
16442 first? Probably it can, since I imagine that we try to create
16443 the types of parameters in the order in which they exist in
16444 the list, and won't have created a forward reference to a
16445 later parameter. */
16446 if (decl_die != NULL)
16448 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16449 break;
16452 /* FALLTHRU */
16454 default:
16456 /* Otherwise try to create a stack operation procedure to
16457 evaluate the value of the array bound. */
16459 dw_die_ref ctx, decl_die;
16460 dw_loc_list_ref list;
16462 list = loc_list_from_tree (bound, 2);
16463 if (list == NULL || single_element_loc_list_p (list))
16465 /* If DW_AT_*bound is not a reference nor constant, it is
16466 a DWARF expression rather than location description.
16467 For that loc_list_from_tree (bound, 0) is needed.
16468 If that fails to give a single element list,
16469 fall back to outputting this as a reference anyway. */
16470 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16471 if (list2 && single_element_loc_list_p (list2))
16473 add_AT_loc (subrange_die, bound_attr, list2->expr);
16474 break;
16477 if (list == NULL)
16478 break;
16480 if (current_function_decl == 0)
16481 ctx = comp_unit_die ();
16482 else
16483 ctx = lookup_decl_die (current_function_decl);
16485 decl_die = new_die (DW_TAG_variable, ctx, bound);
16486 add_AT_flag (decl_die, DW_AT_artificial, 1);
16487 add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
16488 add_AT_location_description (decl_die, DW_AT_location, list);
16489 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16490 break;
16495 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16496 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16497 Note that the block of subscript information for an array type also
16498 includes information about the element type of the given array type. */
16500 static void
16501 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16503 unsigned dimension_number;
16504 tree lower, upper;
16505 dw_die_ref subrange_die;
16507 for (dimension_number = 0;
16508 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16509 type = TREE_TYPE (type), dimension_number++)
16511 tree domain = TYPE_DOMAIN (type);
16513 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16514 break;
16516 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16517 and (in GNU C only) variable bounds. Handle all three forms
16518 here. */
16519 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16520 if (domain)
16522 /* We have an array type with specified bounds. */
16523 lower = TYPE_MIN_VALUE (domain);
16524 upper = TYPE_MAX_VALUE (domain);
16526 /* Define the index type. */
16527 if (TREE_TYPE (domain))
16529 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16530 TREE_TYPE field. We can't emit debug info for this
16531 because it is an unnamed integral type. */
16532 if (TREE_CODE (domain) == INTEGER_TYPE
16533 && TYPE_NAME (domain) == NULL_TREE
16534 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16535 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16537 else
16538 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16539 type_die);
16542 /* ??? If upper is NULL, the array has unspecified length,
16543 but it does have a lower bound. This happens with Fortran
16544 dimension arr(N:*)
16545 Since the debugger is definitely going to need to know N
16546 to produce useful results, go ahead and output the lower
16547 bound solo, and hope the debugger can cope. */
16549 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16550 if (upper)
16551 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16554 /* Otherwise we have an array type with an unspecified length. The
16555 DWARF-2 spec does not say how to handle this; let's just leave out the
16556 bounds. */
16560 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16562 static void
16563 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16565 dw_die_ref decl_die;
16566 HOST_WIDE_INT size;
16568 switch (TREE_CODE (tree_node))
16570 case ERROR_MARK:
16571 size = 0;
16572 break;
16573 case ENUMERAL_TYPE:
16574 case RECORD_TYPE:
16575 case UNION_TYPE:
16576 case QUAL_UNION_TYPE:
16577 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16578 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16580 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16581 return;
16583 size = int_size_in_bytes (tree_node);
16584 break;
16585 case FIELD_DECL:
16586 /* For a data member of a struct or union, the DW_AT_byte_size is
16587 generally given as the number of bytes normally allocated for an
16588 object of the *declared* type of the member itself. This is true
16589 even for bit-fields. */
16590 size = int_size_in_bytes (field_type (tree_node));
16591 break;
16592 default:
16593 gcc_unreachable ();
16596 /* Note that `size' might be -1 when we get to this point. If it is, that
16597 indicates that the byte size of the entity in question is variable. We
16598 have no good way of expressing this fact in Dwarf at the present time,
16599 when location description was not used by the caller code instead. */
16600 if (size >= 0)
16601 add_AT_unsigned (die, DW_AT_byte_size, size);
16604 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16605 which specifies the distance in bits from the highest order bit of the
16606 "containing object" for the bit-field to the highest order bit of the
16607 bit-field itself.
16609 For any given bit-field, the "containing object" is a hypothetical object
16610 (of some integral or enum type) within which the given bit-field lives. The
16611 type of this hypothetical "containing object" is always the same as the
16612 declared type of the individual bit-field itself. The determination of the
16613 exact location of the "containing object" for a bit-field is rather
16614 complicated. It's handled by the `field_byte_offset' function (above).
16616 Note that it is the size (in bytes) of the hypothetical "containing object"
16617 which will be given in the DW_AT_byte_size attribute for this bit-field.
16618 (See `byte_size_attribute' above). */
16620 static inline void
16621 add_bit_offset_attribute (dw_die_ref die, tree decl)
16623 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16624 tree type = DECL_BIT_FIELD_TYPE (decl);
16625 HOST_WIDE_INT bitpos_int;
16626 HOST_WIDE_INT highest_order_object_bit_offset;
16627 HOST_WIDE_INT highest_order_field_bit_offset;
16628 HOST_WIDE_INT bit_offset;
16630 /* Must be a field and a bit field. */
16631 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16633 /* We can't yet handle bit-fields whose offsets are variable, so if we
16634 encounter such things, just return without generating any attribute
16635 whatsoever. Likewise for variable or too large size. */
16636 if (! tree_fits_shwi_p (bit_position (decl))
16637 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16638 return;
16640 bitpos_int = int_bit_position (decl);
16642 /* Note that the bit offset is always the distance (in bits) from the
16643 highest-order bit of the "containing object" to the highest-order bit of
16644 the bit-field itself. Since the "high-order end" of any object or field
16645 is different on big-endian and little-endian machines, the computation
16646 below must take account of these differences. */
16647 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16648 highest_order_field_bit_offset = bitpos_int;
16650 if (! BYTES_BIG_ENDIAN)
16652 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16653 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16656 bit_offset
16657 = (! BYTES_BIG_ENDIAN
16658 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16659 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16661 if (bit_offset < 0)
16662 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16663 else
16664 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16667 /* For a FIELD_DECL node which represents a bit field, output an attribute
16668 which specifies the length in bits of the given field. */
16670 static inline void
16671 add_bit_size_attribute (dw_die_ref die, tree decl)
16673 /* Must be a field and a bit field. */
16674 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16675 && DECL_BIT_FIELD_TYPE (decl));
16677 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16678 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16681 /* If the compiled language is ANSI C, then add a 'prototyped'
16682 attribute, if arg types are given for the parameters of a function. */
16684 static inline void
16685 add_prototyped_attribute (dw_die_ref die, tree func_type)
16687 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16688 && prototype_p (func_type))
16689 add_AT_flag (die, DW_AT_prototyped, 1);
16692 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16693 by looking in either the type declaration or object declaration
16694 equate table. */
16696 static inline dw_die_ref
16697 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16699 dw_die_ref origin_die = NULL;
16701 if (TREE_CODE (origin) != FUNCTION_DECL)
16703 /* We may have gotten separated from the block for the inlined
16704 function, if we're in an exception handler or some such; make
16705 sure that the abstract function has been written out.
16707 Doing this for nested functions is wrong, however; functions are
16708 distinct units, and our context might not even be inline. */
16709 tree fn = origin;
16711 if (TYPE_P (fn))
16712 fn = TYPE_STUB_DECL (fn);
16714 fn = decl_function_context (fn);
16715 if (fn)
16716 dwarf2out_abstract_function (fn);
16719 if (DECL_P (origin))
16720 origin_die = lookup_decl_die (origin);
16721 else if (TYPE_P (origin))
16722 origin_die = lookup_type_die (origin);
16724 /* XXX: Functions that are never lowered don't always have correct block
16725 trees (in the case of java, they simply have no block tree, in some other
16726 languages). For these functions, there is nothing we can really do to
16727 output correct debug info for inlined functions in all cases. Rather
16728 than die, we'll just produce deficient debug info now, in that we will
16729 have variables without a proper abstract origin. In the future, when all
16730 functions are lowered, we should re-add a gcc_assert (origin_die)
16731 here. */
16733 if (origin_die)
16734 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16735 return origin_die;
16738 /* We do not currently support the pure_virtual attribute. */
16740 static inline void
16741 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16743 if (DECL_VINDEX (func_decl))
16745 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16747 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16748 add_AT_loc (die, DW_AT_vtable_elem_location,
16749 new_loc_descr (DW_OP_constu,
16750 tree_to_shwi (DECL_VINDEX (func_decl)),
16751 0));
16753 /* GNU extension: Record what type this method came from originally. */
16754 if (debug_info_level > DINFO_LEVEL_TERSE
16755 && DECL_CONTEXT (func_decl))
16756 add_AT_die_ref (die, DW_AT_containing_type,
16757 lookup_type_die (DECL_CONTEXT (func_decl)));
16761 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16762 given decl. This used to be a vendor extension until after DWARF 4
16763 standardized it. */
16765 static void
16766 add_linkage_attr (dw_die_ref die, tree decl)
16768 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16770 /* Mimic what assemble_name_raw does with a leading '*'. */
16771 if (name[0] == '*')
16772 name = &name[1];
16774 if (dwarf_version >= 4)
16775 add_AT_string (die, DW_AT_linkage_name, name);
16776 else
16777 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16780 /* Add source coordinate attributes for the given decl. */
16782 static void
16783 add_src_coords_attributes (dw_die_ref die, tree decl)
16785 expanded_location s;
16787 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16788 return;
16789 s = expand_location (DECL_SOURCE_LOCATION (decl));
16790 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16791 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16794 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16796 static void
16797 add_linkage_name (dw_die_ref die, tree decl)
16799 if (debug_info_level > DINFO_LEVEL_TERSE
16800 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16801 && TREE_PUBLIC (decl)
16802 && !DECL_ABSTRACT (decl)
16803 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16804 && die->die_tag != DW_TAG_member)
16806 /* Defer until we have an assembler name set. */
16807 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16809 limbo_die_node *asm_name;
16811 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16812 asm_name->die = die;
16813 asm_name->created_for = decl;
16814 asm_name->next = deferred_asm_name;
16815 deferred_asm_name = asm_name;
16817 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16818 add_linkage_attr (die, decl);
16822 /* Add a DW_AT_name attribute and source coordinate attribute for the
16823 given decl, but only if it actually has a name. */
16825 static void
16826 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16828 tree decl_name;
16830 decl_name = DECL_NAME (decl);
16831 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16833 const char *name = dwarf2_name (decl, 0);
16834 if (name)
16835 add_name_attribute (die, name);
16836 if (! DECL_ARTIFICIAL (decl))
16837 add_src_coords_attributes (die, decl);
16839 add_linkage_name (die, decl);
16842 #ifdef VMS_DEBUGGING_INFO
16843 /* Get the function's name, as described by its RTL. This may be different
16844 from the DECL_NAME name used in the source file. */
16845 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16847 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16848 XEXP (DECL_RTL (decl), 0), false);
16849 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16851 #endif /* VMS_DEBUGGING_INFO */
16854 #ifdef VMS_DEBUGGING_INFO
16855 /* Output the debug main pointer die for VMS */
16857 void
16858 dwarf2out_vms_debug_main_pointer (void)
16860 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16861 dw_die_ref die;
16863 /* Allocate the VMS debug main subprogram die. */
16864 die = ggc_cleared_alloc<die_node> ();
16865 die->die_tag = DW_TAG_subprogram;
16866 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16867 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16868 current_function_funcdef_no);
16869 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16871 /* Make it the first child of comp_unit_die (). */
16872 die->die_parent = comp_unit_die ();
16873 if (comp_unit_die ()->die_child)
16875 die->die_sib = comp_unit_die ()->die_child->die_sib;
16876 comp_unit_die ()->die_child->die_sib = die;
16878 else
16880 die->die_sib = die;
16881 comp_unit_die ()->die_child = die;
16884 #endif /* VMS_DEBUGGING_INFO */
16886 /* Push a new declaration scope. */
16888 static void
16889 push_decl_scope (tree scope)
16891 vec_safe_push (decl_scope_table, scope);
16894 /* Pop a declaration scope. */
16896 static inline void
16897 pop_decl_scope (void)
16899 decl_scope_table->pop ();
16902 /* walk_tree helper function for uses_local_type, below. */
16904 static tree
16905 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16907 if (!TYPE_P (*tp))
16908 *walk_subtrees = 0;
16909 else
16911 tree name = TYPE_NAME (*tp);
16912 if (name && DECL_P (name) && decl_function_context (name))
16913 return *tp;
16915 return NULL_TREE;
16918 /* If TYPE involves a function-local type (including a local typedef to a
16919 non-local type), returns that type; otherwise returns NULL_TREE. */
16921 static tree
16922 uses_local_type (tree type)
16924 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16925 return used;
16928 /* Return the DIE for the scope that immediately contains this type.
16929 Non-named types that do not involve a function-local type get global
16930 scope. Named types nested in namespaces or other types get their
16931 containing scope. All other types (i.e. function-local named types) get
16932 the current active scope. */
16934 static dw_die_ref
16935 scope_die_for (tree t, dw_die_ref context_die)
16937 dw_die_ref scope_die = NULL;
16938 tree containing_scope;
16940 /* Non-types always go in the current scope. */
16941 gcc_assert (TYPE_P (t));
16943 /* Use the scope of the typedef, rather than the scope of the type
16944 it refers to. */
16945 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16946 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16947 else
16948 containing_scope = TYPE_CONTEXT (t);
16950 /* Use the containing namespace if there is one. */
16951 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16953 if (context_die == lookup_decl_die (containing_scope))
16954 /* OK */;
16955 else if (debug_info_level > DINFO_LEVEL_TERSE)
16956 context_die = get_context_die (containing_scope);
16957 else
16958 containing_scope = NULL_TREE;
16961 /* Ignore function type "scopes" from the C frontend. They mean that
16962 a tagged type is local to a parmlist of a function declarator, but
16963 that isn't useful to DWARF. */
16964 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16965 containing_scope = NULL_TREE;
16967 if (SCOPE_FILE_SCOPE_P (containing_scope))
16969 /* If T uses a local type keep it local as well, to avoid references
16970 to function-local DIEs from outside the function. */
16971 if (current_function_decl && uses_local_type (t))
16972 scope_die = context_die;
16973 else
16974 scope_die = comp_unit_die ();
16976 else if (TYPE_P (containing_scope))
16978 /* For types, we can just look up the appropriate DIE. */
16979 if (debug_info_level > DINFO_LEVEL_TERSE)
16980 scope_die = get_context_die (containing_scope);
16981 else
16983 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16984 if (scope_die == NULL)
16985 scope_die = comp_unit_die ();
16988 else
16989 scope_die = context_die;
16991 return scope_die;
16994 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
16996 static inline int
16997 local_scope_p (dw_die_ref context_die)
16999 for (; context_die; context_die = context_die->die_parent)
17000 if (context_die->die_tag == DW_TAG_inlined_subroutine
17001 || context_die->die_tag == DW_TAG_subprogram)
17002 return 1;
17004 return 0;
17007 /* Returns nonzero if CONTEXT_DIE is a class. */
17009 static inline int
17010 class_scope_p (dw_die_ref context_die)
17012 return (context_die
17013 && (context_die->die_tag == DW_TAG_structure_type
17014 || context_die->die_tag == DW_TAG_class_type
17015 || context_die->die_tag == DW_TAG_interface_type
17016 || context_die->die_tag == DW_TAG_union_type));
17019 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17020 whether or not to treat a DIE in this context as a declaration. */
17022 static inline int
17023 class_or_namespace_scope_p (dw_die_ref context_die)
17025 return (class_scope_p (context_die)
17026 || (context_die && context_die->die_tag == DW_TAG_namespace));
17029 /* Many forms of DIEs require a "type description" attribute. This
17030 routine locates the proper "type descriptor" die for the type given
17031 by 'type', and adds a DW_AT_type attribute below the given die. */
17033 static void
17034 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17035 int decl_volatile, dw_die_ref context_die)
17037 enum tree_code code = TREE_CODE (type);
17038 dw_die_ref type_die = NULL;
17040 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17041 or fixed-point type, use the inner type. This is because we have no
17042 support for unnamed types in base_type_die. This can happen if this is
17043 an Ada subrange type. Correct solution is emit a subrange type die. */
17044 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17045 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17046 type = TREE_TYPE (type), code = TREE_CODE (type);
17048 if (code == ERROR_MARK
17049 /* Handle a special case. For functions whose return type is void, we
17050 generate *no* type attribute. (Note that no object may have type
17051 `void', so this only applies to function return types). */
17052 || code == VOID_TYPE)
17053 return;
17055 type_die = modified_type_die (type,
17056 decl_const || TYPE_READONLY (type),
17057 decl_volatile || TYPE_VOLATILE (type),
17058 context_die);
17060 if (type_die != NULL)
17061 add_AT_die_ref (object_die, DW_AT_type, type_die);
17064 /* Given an object die, add the calling convention attribute for the
17065 function call type. */
17066 static void
17067 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17069 enum dwarf_calling_convention value = DW_CC_normal;
17071 value = ((enum dwarf_calling_convention)
17072 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17074 if (is_fortran ()
17075 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17077 /* DWARF 2 doesn't provide a way to identify a program's source-level
17078 entry point. DW_AT_calling_convention attributes are only meant
17079 to describe functions' calling conventions. However, lacking a
17080 better way to signal the Fortran main program, we used this for
17081 a long time, following existing custom. Now, DWARF 4 has
17082 DW_AT_main_subprogram, which we add below, but some tools still
17083 rely on the old way, which we thus keep. */
17084 value = DW_CC_program;
17086 if (dwarf_version >= 4 || !dwarf_strict)
17087 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17090 /* Only add the attribute if the backend requests it, and
17091 is not DW_CC_normal. */
17092 if (value && (value != DW_CC_normal))
17093 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17096 /* Given a tree pointer to a struct, class, union, or enum type node, return
17097 a pointer to the (string) tag name for the given type, or zero if the type
17098 was declared without a tag. */
17100 static const char *
17101 type_tag (const_tree type)
17103 const char *name = 0;
17105 if (TYPE_NAME (type) != 0)
17107 tree t = 0;
17109 /* Find the IDENTIFIER_NODE for the type name. */
17110 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17111 && !TYPE_NAMELESS (type))
17112 t = TYPE_NAME (type);
17114 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17115 a TYPE_DECL node, regardless of whether or not a `typedef' was
17116 involved. */
17117 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17118 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17120 /* We want to be extra verbose. Don't call dwarf_name if
17121 DECL_NAME isn't set. The default hook for decl_printable_name
17122 doesn't like that, and in this context it's correct to return
17123 0, instead of "<anonymous>" or the like. */
17124 if (DECL_NAME (TYPE_NAME (type))
17125 && !DECL_NAMELESS (TYPE_NAME (type)))
17126 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17129 /* Now get the name as a string, or invent one. */
17130 if (!name && t != 0)
17131 name = IDENTIFIER_POINTER (t);
17134 return (name == 0 || *name == '\0') ? 0 : name;
17137 /* Return the type associated with a data member, make a special check
17138 for bit field types. */
17140 static inline tree
17141 member_declared_type (const_tree member)
17143 return (DECL_BIT_FIELD_TYPE (member)
17144 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17147 /* Get the decl's label, as described by its RTL. This may be different
17148 from the DECL_NAME name used in the source file. */
17150 #if 0
17151 static const char *
17152 decl_start_label (tree decl)
17154 rtx x;
17155 const char *fnname;
17157 x = DECL_RTL (decl);
17158 gcc_assert (MEM_P (x));
17160 x = XEXP (x, 0);
17161 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17163 fnname = XSTR (x, 0);
17164 return fnname;
17166 #endif
17168 /* These routines generate the internal representation of the DIE's for
17169 the compilation unit. Debugging information is collected by walking
17170 the declaration trees passed in from dwarf2out_decl(). */
17172 static void
17173 gen_array_type_die (tree type, dw_die_ref context_die)
17175 dw_die_ref scope_die = scope_die_for (type, context_die);
17176 dw_die_ref array_die;
17178 /* GNU compilers represent multidimensional array types as sequences of one
17179 dimensional array types whose element types are themselves array types.
17180 We sometimes squish that down to a single array_type DIE with multiple
17181 subscripts in the Dwarf debugging info. The draft Dwarf specification
17182 say that we are allowed to do this kind of compression in C, because
17183 there is no difference between an array of arrays and a multidimensional
17184 array. We don't do this for Ada to remain as close as possible to the
17185 actual representation, which is especially important against the language
17186 flexibilty wrt arrays of variable size. */
17188 bool collapse_nested_arrays = !is_ada ();
17189 tree element_type;
17191 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17192 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17193 if (TYPE_STRING_FLAG (type)
17194 && TREE_CODE (type) == ARRAY_TYPE
17195 && is_fortran ()
17196 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17198 HOST_WIDE_INT size;
17200 array_die = new_die (DW_TAG_string_type, scope_die, type);
17201 add_name_attribute (array_die, type_tag (type));
17202 equate_type_number_to_die (type, array_die);
17203 size = int_size_in_bytes (type);
17204 if (size >= 0)
17205 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17206 else if (TYPE_DOMAIN (type) != NULL_TREE
17207 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17208 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17210 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17211 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17213 size = int_size_in_bytes (TREE_TYPE (szdecl));
17214 if (loc && size > 0)
17216 add_AT_location_description (array_die, DW_AT_string_length, loc);
17217 if (size != DWARF2_ADDR_SIZE)
17218 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17221 return;
17224 array_die = new_die (DW_TAG_array_type, scope_die, type);
17225 add_name_attribute (array_die, type_tag (type));
17226 equate_type_number_to_die (type, array_die);
17228 if (TREE_CODE (type) == VECTOR_TYPE)
17229 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17231 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17232 if (is_fortran ()
17233 && TREE_CODE (type) == ARRAY_TYPE
17234 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17235 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17236 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17238 #if 0
17239 /* We default the array ordering. SDB will probably do
17240 the right things even if DW_AT_ordering is not present. It's not even
17241 an issue until we start to get into multidimensional arrays anyway. If
17242 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17243 then we'll have to put the DW_AT_ordering attribute back in. (But if
17244 and when we find out that we need to put these in, we will only do so
17245 for multidimensional arrays. */
17246 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17247 #endif
17249 if (TREE_CODE (type) == VECTOR_TYPE)
17251 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17252 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17253 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17254 add_bound_info (subrange_die, DW_AT_upper_bound,
17255 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17257 else
17258 add_subscript_info (array_die, type, collapse_nested_arrays);
17260 /* Add representation of the type of the elements of this array type and
17261 emit the corresponding DIE if we haven't done it already. */
17262 element_type = TREE_TYPE (type);
17263 if (collapse_nested_arrays)
17264 while (TREE_CODE (element_type) == ARRAY_TYPE)
17266 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17267 break;
17268 element_type = TREE_TYPE (element_type);
17271 add_type_attribute (array_die, element_type, 0, 0, context_die);
17273 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17274 if (TYPE_ARTIFICIAL (type))
17275 add_AT_flag (array_die, DW_AT_artificial, 1);
17277 if (get_AT (array_die, DW_AT_name))
17278 add_pubtype (type, array_die);
17281 static dw_loc_descr_ref
17282 descr_info_loc (tree val, tree base_decl)
17284 HOST_WIDE_INT size;
17285 dw_loc_descr_ref loc, loc2;
17286 enum dwarf_location_atom op;
17288 if (val == base_decl)
17289 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17291 switch (TREE_CODE (val))
17293 CASE_CONVERT:
17294 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17295 case VAR_DECL:
17296 return loc_descriptor_from_tree (val, 0);
17297 case INTEGER_CST:
17298 if (tree_fits_shwi_p (val))
17299 return int_loc_descriptor (tree_to_shwi (val));
17300 break;
17301 case INDIRECT_REF:
17302 size = int_size_in_bytes (TREE_TYPE (val));
17303 if (size < 0)
17304 break;
17305 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17306 if (!loc)
17307 break;
17308 if (size == DWARF2_ADDR_SIZE)
17309 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17310 else
17311 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17312 return loc;
17313 case POINTER_PLUS_EXPR:
17314 case PLUS_EXPR:
17315 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17316 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17318 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17319 if (!loc)
17320 break;
17321 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17323 else
17325 op = DW_OP_plus;
17326 do_binop:
17327 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17328 if (!loc)
17329 break;
17330 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17331 if (!loc2)
17332 break;
17333 add_loc_descr (&loc, loc2);
17334 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17336 return loc;
17337 case MINUS_EXPR:
17338 op = DW_OP_minus;
17339 goto do_binop;
17340 case MULT_EXPR:
17341 op = DW_OP_mul;
17342 goto do_binop;
17343 case EQ_EXPR:
17344 op = DW_OP_eq;
17345 goto do_binop;
17346 case NE_EXPR:
17347 op = DW_OP_ne;
17348 goto do_binop;
17349 default:
17350 break;
17352 return NULL;
17355 static void
17356 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17357 tree val, tree base_decl)
17359 dw_loc_descr_ref loc;
17361 if (tree_fits_shwi_p (val))
17363 add_AT_unsigned (die, attr, tree_to_shwi (val));
17364 return;
17367 loc = descr_info_loc (val, base_decl);
17368 if (!loc)
17369 return;
17371 add_AT_loc (die, attr, loc);
17374 /* This routine generates DIE for array with hidden descriptor, details
17375 are filled into *info by a langhook. */
17377 static void
17378 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17379 dw_die_ref context_die)
17381 dw_die_ref scope_die = scope_die_for (type, context_die);
17382 dw_die_ref array_die;
17383 int dim;
17385 array_die = new_die (DW_TAG_array_type, scope_die, type);
17386 add_name_attribute (array_die, type_tag (type));
17387 equate_type_number_to_die (type, array_die);
17389 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17390 if (is_fortran ()
17391 && info->ndimensions >= 2)
17392 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17394 if (info->data_location)
17395 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17396 info->base_decl);
17397 if (info->associated)
17398 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17399 info->base_decl);
17400 if (info->allocated)
17401 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17402 info->base_decl);
17404 for (dim = 0; dim < info->ndimensions; dim++)
17406 dw_die_ref subrange_die
17407 = new_die (DW_TAG_subrange_type, array_die, NULL);
17409 if (info->dimen[dim].lower_bound)
17411 /* If it is the default value, omit it. */
17412 int dflt;
17414 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17415 && (dflt = lower_bound_default ()) != -1
17416 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17418 else
17419 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17420 info->dimen[dim].lower_bound,
17421 info->base_decl);
17423 if (info->dimen[dim].upper_bound)
17424 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17425 info->dimen[dim].upper_bound,
17426 info->base_decl);
17427 if (info->dimen[dim].stride)
17428 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17429 info->dimen[dim].stride,
17430 info->base_decl);
17433 gen_type_die (info->element_type, context_die);
17434 add_type_attribute (array_die, info->element_type, 0, 0, context_die);
17436 if (get_AT (array_die, DW_AT_name))
17437 add_pubtype (type, array_die);
17440 #if 0
17441 static void
17442 gen_entry_point_die (tree decl, dw_die_ref context_die)
17444 tree origin = decl_ultimate_origin (decl);
17445 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17447 if (origin != NULL)
17448 add_abstract_origin_attribute (decl_die, origin);
17449 else
17451 add_name_and_src_coords_attributes (decl_die, decl);
17452 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17453 0, 0, context_die);
17456 if (DECL_ABSTRACT (decl))
17457 equate_decl_number_to_die (decl, decl_die);
17458 else
17459 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17461 #endif
17463 /* Walk through the list of incomplete types again, trying once more to
17464 emit full debugging info for them. */
17466 static void
17467 retry_incomplete_types (void)
17469 int i;
17471 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17472 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17473 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17476 /* Determine what tag to use for a record type. */
17478 static enum dwarf_tag
17479 record_type_tag (tree type)
17481 if (! lang_hooks.types.classify_record)
17482 return DW_TAG_structure_type;
17484 switch (lang_hooks.types.classify_record (type))
17486 case RECORD_IS_STRUCT:
17487 return DW_TAG_structure_type;
17489 case RECORD_IS_CLASS:
17490 return DW_TAG_class_type;
17492 case RECORD_IS_INTERFACE:
17493 if (dwarf_version >= 3 || !dwarf_strict)
17494 return DW_TAG_interface_type;
17495 return DW_TAG_structure_type;
17497 default:
17498 gcc_unreachable ();
17502 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17503 include all of the information about the enumeration values also. Each
17504 enumerated type name/value is listed as a child of the enumerated type
17505 DIE. */
17507 static dw_die_ref
17508 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17510 dw_die_ref type_die = lookup_type_die (type);
17512 if (type_die == NULL)
17514 type_die = new_die (DW_TAG_enumeration_type,
17515 scope_die_for (type, context_die), type);
17516 equate_type_number_to_die (type, type_die);
17517 add_name_attribute (type_die, type_tag (type));
17518 if (dwarf_version >= 4 || !dwarf_strict)
17520 if (ENUM_IS_SCOPED (type))
17521 add_AT_flag (type_die, DW_AT_enum_class, 1);
17522 if (ENUM_IS_OPAQUE (type))
17523 add_AT_flag (type_die, DW_AT_declaration, 1);
17526 else if (! TYPE_SIZE (type))
17527 return type_die;
17528 else
17529 remove_AT (type_die, DW_AT_declaration);
17531 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17532 given enum type is incomplete, do not generate the DW_AT_byte_size
17533 attribute or the DW_AT_element_list attribute. */
17534 if (TYPE_SIZE (type))
17536 tree link;
17538 TREE_ASM_WRITTEN (type) = 1;
17539 add_byte_size_attribute (type_die, type);
17540 if (dwarf_version >= 3 || !dwarf_strict)
17542 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17543 add_type_attribute (type_die, underlying, 0, 0, context_die);
17545 if (TYPE_STUB_DECL (type) != NULL_TREE)
17547 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17548 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17551 /* If the first reference to this type was as the return type of an
17552 inline function, then it may not have a parent. Fix this now. */
17553 if (type_die->die_parent == NULL)
17554 add_child_die (scope_die_for (type, context_die), type_die);
17556 for (link = TYPE_VALUES (type);
17557 link != NULL; link = TREE_CHAIN (link))
17559 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17560 tree value = TREE_VALUE (link);
17562 add_name_attribute (enum_die,
17563 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17565 if (TREE_CODE (value) == CONST_DECL)
17566 value = DECL_INITIAL (value);
17568 if (simple_type_size_in_bits (TREE_TYPE (value))
17569 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17571 /* For constant forms created by add_AT_unsigned DWARF
17572 consumers (GDB, elfutils, etc.) always zero extend
17573 the value. Only when the actual value is negative
17574 do we need to use add_AT_int to generate a constant
17575 form that can represent negative values. */
17576 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17577 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17578 add_AT_unsigned (enum_die, DW_AT_const_value,
17579 (unsigned HOST_WIDE_INT) val);
17580 else
17581 add_AT_int (enum_die, DW_AT_const_value, val);
17583 else
17584 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17585 that here. TODO: This should be re-worked to use correct
17586 signed/unsigned double tags for all cases. */
17587 add_AT_wide (enum_die, DW_AT_const_value, value);
17590 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17591 if (TYPE_ARTIFICIAL (type))
17592 add_AT_flag (type_die, DW_AT_artificial, 1);
17594 else
17595 add_AT_flag (type_die, DW_AT_declaration, 1);
17597 add_pubtype (type, type_die);
17599 return type_die;
17602 /* Generate a DIE to represent either a real live formal parameter decl or to
17603 represent just the type of some formal parameter position in some function
17604 type.
17606 Note that this routine is a bit unusual because its argument may be a
17607 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17608 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17609 node. If it's the former then this function is being called to output a
17610 DIE to represent a formal parameter object (or some inlining thereof). If
17611 it's the latter, then this function is only being called to output a
17612 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17613 argument type of some subprogram type.
17614 If EMIT_NAME_P is true, name and source coordinate attributes
17615 are emitted. */
17617 static dw_die_ref
17618 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17619 dw_die_ref context_die)
17621 tree node_or_origin = node ? node : origin;
17622 tree ultimate_origin;
17623 dw_die_ref parm_die
17624 = new_die (DW_TAG_formal_parameter, context_die, node);
17626 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17628 case tcc_declaration:
17629 ultimate_origin = decl_ultimate_origin (node_or_origin);
17630 if (node || ultimate_origin)
17631 origin = ultimate_origin;
17632 if (origin != NULL)
17633 add_abstract_origin_attribute (parm_die, origin);
17634 else if (emit_name_p)
17635 add_name_and_src_coords_attributes (parm_die, node);
17636 if (origin == NULL
17637 || (! DECL_ABSTRACT (node_or_origin)
17638 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17639 decl_function_context
17640 (node_or_origin))))
17642 tree type = TREE_TYPE (node_or_origin);
17643 if (decl_by_reference_p (node_or_origin))
17644 add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17645 context_die);
17646 else
17647 add_type_attribute (parm_die, type,
17648 TREE_READONLY (node_or_origin),
17649 TREE_THIS_VOLATILE (node_or_origin),
17650 context_die);
17652 if (origin == NULL && DECL_ARTIFICIAL (node))
17653 add_AT_flag (parm_die, DW_AT_artificial, 1);
17655 if (node && node != origin)
17656 equate_decl_number_to_die (node, parm_die);
17657 if (! DECL_ABSTRACT (node_or_origin))
17658 add_location_or_const_value_attribute (parm_die, node_or_origin,
17659 node == NULL, DW_AT_location);
17661 break;
17663 case tcc_type:
17664 /* We were called with some kind of a ..._TYPE node. */
17665 add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17666 break;
17668 default:
17669 gcc_unreachable ();
17672 return parm_die;
17675 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17676 children DW_TAG_formal_parameter DIEs representing the arguments of the
17677 parameter pack.
17679 PARM_PACK must be a function parameter pack.
17680 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17681 must point to the subsequent arguments of the function PACK_ARG belongs to.
17682 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17683 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17684 following the last one for which a DIE was generated. */
17686 static dw_die_ref
17687 gen_formal_parameter_pack_die (tree parm_pack,
17688 tree pack_arg,
17689 dw_die_ref subr_die,
17690 tree *next_arg)
17692 tree arg;
17693 dw_die_ref parm_pack_die;
17695 gcc_assert (parm_pack
17696 && lang_hooks.function_parameter_pack_p (parm_pack)
17697 && subr_die);
17699 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17700 add_src_coords_attributes (parm_pack_die, parm_pack);
17702 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17704 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17705 parm_pack))
17706 break;
17707 gen_formal_parameter_die (arg, NULL,
17708 false /* Don't emit name attribute. */,
17709 parm_pack_die);
17711 if (next_arg)
17712 *next_arg = arg;
17713 return parm_pack_die;
17716 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17717 at the end of an (ANSI prototyped) formal parameters list. */
17719 static void
17720 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17722 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17725 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17726 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17727 parameters as specified in some function type specification (except for
17728 those which appear as part of a function *definition*). */
17730 static void
17731 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17733 tree link;
17734 tree formal_type = NULL;
17735 tree first_parm_type;
17736 tree arg;
17738 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17740 arg = DECL_ARGUMENTS (function_or_method_type);
17741 function_or_method_type = TREE_TYPE (function_or_method_type);
17743 else
17744 arg = NULL_TREE;
17746 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17748 /* Make our first pass over the list of formal parameter types and output a
17749 DW_TAG_formal_parameter DIE for each one. */
17750 for (link = first_parm_type; link; )
17752 dw_die_ref parm_die;
17754 formal_type = TREE_VALUE (link);
17755 if (formal_type == void_type_node)
17756 break;
17758 /* Output a (nameless) DIE to represent the formal parameter itself. */
17759 parm_die = gen_formal_parameter_die (formal_type, NULL,
17760 true /* Emit name attribute. */,
17761 context_die);
17762 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17763 && link == first_parm_type)
17765 add_AT_flag (parm_die, DW_AT_artificial, 1);
17766 if (dwarf_version >= 3 || !dwarf_strict)
17767 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17769 else if (arg && DECL_ARTIFICIAL (arg))
17770 add_AT_flag (parm_die, DW_AT_artificial, 1);
17772 link = TREE_CHAIN (link);
17773 if (arg)
17774 arg = DECL_CHAIN (arg);
17777 /* If this function type has an ellipsis, add a
17778 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17779 if (formal_type != void_type_node)
17780 gen_unspecified_parameters_die (function_or_method_type, context_die);
17782 /* Make our second (and final) pass over the list of formal parameter types
17783 and output DIEs to represent those types (as necessary). */
17784 for (link = TYPE_ARG_TYPES (function_or_method_type);
17785 link && TREE_VALUE (link);
17786 link = TREE_CHAIN (link))
17787 gen_type_die (TREE_VALUE (link), context_die);
17790 /* We want to generate the DIE for TYPE so that we can generate the
17791 die for MEMBER, which has been defined; we will need to refer back
17792 to the member declaration nested within TYPE. If we're trying to
17793 generate minimal debug info for TYPE, processing TYPE won't do the
17794 trick; we need to attach the member declaration by hand. */
17796 static void
17797 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17799 gen_type_die (type, context_die);
17801 /* If we're trying to avoid duplicate debug info, we may not have
17802 emitted the member decl for this function. Emit it now. */
17803 if (TYPE_STUB_DECL (type)
17804 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17805 && ! lookup_decl_die (member))
17807 dw_die_ref type_die;
17808 gcc_assert (!decl_ultimate_origin (member));
17810 push_decl_scope (type);
17811 type_die = lookup_type_die_strip_naming_typedef (type);
17812 if (TREE_CODE (member) == FUNCTION_DECL)
17813 gen_subprogram_die (member, type_die);
17814 else if (TREE_CODE (member) == FIELD_DECL)
17816 /* Ignore the nameless fields that are used to skip bits but handle
17817 C++ anonymous unions and structs. */
17818 if (DECL_NAME (member) != NULL_TREE
17819 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17820 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17822 gen_type_die (member_declared_type (member), type_die);
17823 gen_field_die (member, type_die);
17826 else
17827 gen_variable_die (member, NULL_TREE, type_die);
17829 pop_decl_scope ();
17833 /* Forward declare these functions, because they are mutually recursive
17834 with their set_block_* pairing functions. */
17835 static void set_decl_origin_self (tree);
17836 static void set_decl_abstract_flags (tree, int);
17838 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17839 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17840 that it points to the node itself, thus indicating that the node is its
17841 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17842 the given node is NULL, recursively descend the decl/block tree which
17843 it is the root of, and for each other ..._DECL or BLOCK node contained
17844 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17845 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17846 values to point to themselves. */
17848 static void
17849 set_block_origin_self (tree stmt)
17851 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17853 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17856 tree local_decl;
17858 for (local_decl = BLOCK_VARS (stmt);
17859 local_decl != NULL_TREE;
17860 local_decl = DECL_CHAIN (local_decl))
17861 if (! DECL_EXTERNAL (local_decl))
17862 set_decl_origin_self (local_decl); /* Potential recursion. */
17866 tree subblock;
17868 for (subblock = BLOCK_SUBBLOCKS (stmt);
17869 subblock != NULL_TREE;
17870 subblock = BLOCK_CHAIN (subblock))
17871 set_block_origin_self (subblock); /* Recurse. */
17876 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17877 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17878 node to so that it points to the node itself, thus indicating that the
17879 node represents its own (abstract) origin. Additionally, if the
17880 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17881 the decl/block tree of which the given node is the root of, and for
17882 each other ..._DECL or BLOCK node contained therein whose
17883 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17884 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17885 point to themselves. */
17887 static void
17888 set_decl_origin_self (tree decl)
17890 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17892 DECL_ABSTRACT_ORIGIN (decl) = decl;
17893 if (TREE_CODE (decl) == FUNCTION_DECL)
17895 tree arg;
17897 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17898 DECL_ABSTRACT_ORIGIN (arg) = arg;
17899 if (DECL_INITIAL (decl) != NULL_TREE
17900 && DECL_INITIAL (decl) != error_mark_node)
17901 set_block_origin_self (DECL_INITIAL (decl));
17906 /* Given a pointer to some BLOCK node, and a boolean value to set the
17907 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17908 the given block, and for all local decls and all local sub-blocks
17909 (recursively) which are contained therein. */
17911 static void
17912 set_block_abstract_flags (tree stmt, int setting)
17914 tree local_decl;
17915 tree subblock;
17916 unsigned int i;
17918 BLOCK_ABSTRACT (stmt) = setting;
17920 for (local_decl = BLOCK_VARS (stmt);
17921 local_decl != NULL_TREE;
17922 local_decl = DECL_CHAIN (local_decl))
17923 if (! DECL_EXTERNAL (local_decl))
17924 set_decl_abstract_flags (local_decl, setting);
17926 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17928 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17929 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17930 || TREE_CODE (local_decl) == PARM_DECL)
17931 set_decl_abstract_flags (local_decl, setting);
17934 for (subblock = BLOCK_SUBBLOCKS (stmt);
17935 subblock != NULL_TREE;
17936 subblock = BLOCK_CHAIN (subblock))
17937 set_block_abstract_flags (subblock, setting);
17940 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17941 "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17942 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17943 set the abstract flags for all of the parameters, local vars, local
17944 blocks and sub-blocks (recursively) to the same setting. */
17946 static void
17947 set_decl_abstract_flags (tree decl, int setting)
17949 DECL_ABSTRACT (decl) = setting;
17950 if (TREE_CODE (decl) == FUNCTION_DECL)
17952 tree arg;
17954 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17955 DECL_ABSTRACT (arg) = setting;
17956 if (DECL_INITIAL (decl) != NULL_TREE
17957 && DECL_INITIAL (decl) != error_mark_node)
17958 set_block_abstract_flags (DECL_INITIAL (decl), setting);
17962 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17963 may later generate inlined and/or out-of-line instances of. */
17965 static void
17966 dwarf2out_abstract_function (tree decl)
17968 dw_die_ref old_die;
17969 tree save_fn;
17970 tree context;
17971 int was_abstract;
17972 htab_t old_decl_loc_table;
17973 htab_t old_cached_dw_loc_list_table;
17974 int old_call_site_count, old_tail_call_site_count;
17975 struct call_arg_loc_node *old_call_arg_locations;
17977 /* Make sure we have the actual abstract inline, not a clone. */
17978 decl = DECL_ORIGIN (decl);
17980 old_die = lookup_decl_die (decl);
17981 if (old_die && get_AT (old_die, DW_AT_inline))
17982 /* We've already generated the abstract instance. */
17983 return;
17985 /* We can be called while recursively when seeing block defining inlined subroutine
17986 DIE. Be sure to not clobber the outer location table nor use it or we would
17987 get locations in abstract instantces. */
17988 old_decl_loc_table = decl_loc_table;
17989 decl_loc_table = NULL;
17990 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17991 cached_dw_loc_list_table = NULL;
17992 old_call_arg_locations = call_arg_locations;
17993 call_arg_locations = NULL;
17994 old_call_site_count = call_site_count;
17995 call_site_count = -1;
17996 old_tail_call_site_count = tail_call_site_count;
17997 tail_call_site_count = -1;
17999 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18000 we don't get confused by DECL_ABSTRACT. */
18001 if (debug_info_level > DINFO_LEVEL_TERSE)
18003 context = decl_class_context (decl);
18004 if (context)
18005 gen_type_die_for_member
18006 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18009 /* Pretend we've just finished compiling this function. */
18010 save_fn = current_function_decl;
18011 current_function_decl = decl;
18013 was_abstract = DECL_ABSTRACT (decl);
18014 set_decl_abstract_flags (decl, 1);
18015 dwarf2out_decl (decl);
18016 if (! was_abstract)
18017 set_decl_abstract_flags (decl, 0);
18019 current_function_decl = save_fn;
18020 decl_loc_table = old_decl_loc_table;
18021 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18022 call_arg_locations = old_call_arg_locations;
18023 call_site_count = old_call_site_count;
18024 tail_call_site_count = old_tail_call_site_count;
18027 /* Helper function of premark_used_types() which gets called through
18028 htab_traverse.
18030 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18031 marked as unused by prune_unused_types. */
18033 static int
18034 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18036 tree type;
18037 dw_die_ref die;
18039 type = (tree) *slot;
18040 die = lookup_type_die (type);
18041 if (die != NULL)
18042 die->die_perennial_p = 1;
18043 return 1;
18046 /* Helper function of premark_types_used_by_global_vars which gets called
18047 through htab_traverse.
18049 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18050 marked as unused by prune_unused_types. The DIE of the type is marked
18051 only if the global variable using the type will actually be emitted. */
18053 static int
18054 premark_types_used_by_global_vars_helper (void **slot,
18055 void *data ATTRIBUTE_UNUSED)
18057 struct types_used_by_vars_entry *entry;
18058 dw_die_ref die;
18060 entry = (struct types_used_by_vars_entry *) *slot;
18061 gcc_assert (entry->type != NULL
18062 && entry->var_decl != NULL);
18063 die = lookup_type_die (entry->type);
18064 if (die)
18066 /* Ask cgraph if the global variable really is to be emitted.
18067 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18068 varpool_node *node = varpool_get_node (entry->var_decl);
18069 if (node && node->definition)
18071 die->die_perennial_p = 1;
18072 /* Keep the parent DIEs as well. */
18073 while ((die = die->die_parent) && die->die_perennial_p == 0)
18074 die->die_perennial_p = 1;
18077 return 1;
18080 /* Mark all members of used_types_hash as perennial. */
18082 static void
18083 premark_used_types (struct function *fun)
18085 if (fun && fun->used_types_hash)
18086 htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
18089 /* Mark all members of types_used_by_vars_entry as perennial. */
18091 static void
18092 premark_types_used_by_global_vars (void)
18094 if (types_used_by_vars_hash)
18095 htab_traverse (types_used_by_vars_hash,
18096 premark_types_used_by_global_vars_helper, NULL);
18099 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18100 for CA_LOC call arg loc node. */
18102 static dw_die_ref
18103 gen_call_site_die (tree decl, dw_die_ref subr_die,
18104 struct call_arg_loc_node *ca_loc)
18106 dw_die_ref stmt_die = NULL, die;
18107 tree block = ca_loc->block;
18109 while (block
18110 && block != DECL_INITIAL (decl)
18111 && TREE_CODE (block) == BLOCK)
18113 if (block_map.length () > BLOCK_NUMBER (block))
18114 stmt_die = block_map[BLOCK_NUMBER (block)];
18115 if (stmt_die)
18116 break;
18117 block = BLOCK_SUPERCONTEXT (block);
18119 if (stmt_die == NULL)
18120 stmt_die = subr_die;
18121 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18122 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18123 if (ca_loc->tail_call_p)
18124 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18125 if (ca_loc->symbol_ref)
18127 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18128 if (tdie)
18129 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18130 else
18131 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18133 return die;
18136 /* Generate a DIE to represent a declared function (either file-scope or
18137 block-local). */
18139 static void
18140 gen_subprogram_die (tree decl, dw_die_ref context_die)
18142 tree origin = decl_ultimate_origin (decl);
18143 dw_die_ref subr_die;
18144 tree outer_scope;
18145 dw_die_ref old_die = lookup_decl_die (decl);
18146 int declaration = (current_function_decl != decl
18147 || class_or_namespace_scope_p (context_die));
18149 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18151 /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18152 started to generate the abstract instance of an inline, decided to output
18153 its containing class, and proceeded to emit the declaration of the inline
18154 from the member list for the class. If so, DECLARATION takes priority;
18155 we'll get back to the abstract instance when done with the class. */
18157 /* The class-scope declaration DIE must be the primary DIE. */
18158 if (origin && declaration && class_or_namespace_scope_p (context_die))
18160 origin = NULL;
18161 gcc_assert (!old_die);
18164 /* Now that the C++ front end lazily declares artificial member fns, we
18165 might need to retrofit the declaration into its class. */
18166 if (!declaration && !origin && !old_die
18167 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18168 && !class_or_namespace_scope_p (context_die)
18169 && debug_info_level > DINFO_LEVEL_TERSE)
18170 old_die = force_decl_die (decl);
18172 if (origin != NULL)
18174 gcc_assert (!declaration || local_scope_p (context_die));
18176 /* Fixup die_parent for the abstract instance of a nested
18177 inline function. */
18178 if (old_die && old_die->die_parent == NULL)
18179 add_child_die (context_die, old_die);
18181 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18182 add_abstract_origin_attribute (subr_die, origin);
18183 /* This is where the actual code for a cloned function is.
18184 Let's emit linkage name attribute for it. This helps
18185 debuggers to e.g, set breakpoints into
18186 constructors/destructors when the user asks "break
18187 K::K". */
18188 add_linkage_name (subr_die, decl);
18190 else if (old_die)
18192 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18193 struct dwarf_file_data * file_index = lookup_filename (s.file);
18195 if (!get_AT_flag (old_die, DW_AT_declaration)
18196 /* We can have a normal definition following an inline one in the
18197 case of redefinition of GNU C extern inlines.
18198 It seems reasonable to use AT_specification in this case. */
18199 && !get_AT (old_die, DW_AT_inline))
18201 /* Detect and ignore this case, where we are trying to output
18202 something we have already output. */
18203 return;
18206 /* If the definition comes from the same place as the declaration,
18207 maybe use the old DIE. We always want the DIE for this function
18208 that has the *_pc attributes to be under comp_unit_die so the
18209 debugger can find it. We also need to do this for abstract
18210 instances of inlines, since the spec requires the out-of-line copy
18211 to have the same parent. For local class methods, this doesn't
18212 apply; we just use the old DIE. */
18213 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18214 && (DECL_ARTIFICIAL (decl)
18215 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18216 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18217 == (unsigned) s.line))))
18219 subr_die = old_die;
18221 /* Clear out the declaration attribute and the formal parameters.
18222 Do not remove all children, because it is possible that this
18223 declaration die was forced using force_decl_die(). In such
18224 cases die that forced declaration die (e.g. TAG_imported_module)
18225 is one of the children that we do not want to remove. */
18226 remove_AT (subr_die, DW_AT_declaration);
18227 remove_AT (subr_die, DW_AT_object_pointer);
18228 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18230 else
18232 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18233 add_AT_specification (subr_die, old_die);
18234 add_pubname (decl, subr_die);
18235 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18236 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18237 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18238 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18240 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18241 emit the real type on the definition die. */
18242 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18244 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18245 if (die == auto_die || die == decltype_auto_die)
18246 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18247 0, 0, context_die);
18251 else
18253 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18255 if (TREE_PUBLIC (decl))
18256 add_AT_flag (subr_die, DW_AT_external, 1);
18258 add_name_and_src_coords_attributes (subr_die, decl);
18259 add_pubname (decl, subr_die);
18260 if (debug_info_level > DINFO_LEVEL_TERSE)
18262 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18263 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18264 0, 0, context_die);
18267 add_pure_or_virtual_attribute (subr_die, decl);
18268 if (DECL_ARTIFICIAL (decl))
18269 add_AT_flag (subr_die, DW_AT_artificial, 1);
18271 add_accessibility_attribute (subr_die, decl);
18274 if (declaration)
18276 if (!old_die || !get_AT (old_die, DW_AT_inline))
18278 add_AT_flag (subr_die, DW_AT_declaration, 1);
18280 /* If this is an explicit function declaration then generate
18281 a DW_AT_explicit attribute. */
18282 if (lang_hooks.decls.function_decl_explicit_p (decl)
18283 && (dwarf_version >= 3 || !dwarf_strict))
18284 add_AT_flag (subr_die, DW_AT_explicit, 1);
18286 /* The first time we see a member function, it is in the context of
18287 the class to which it belongs. We make sure of this by emitting
18288 the class first. The next time is the definition, which is
18289 handled above. The two may come from the same source text.
18291 Note that force_decl_die() forces function declaration die. It is
18292 later reused to represent definition. */
18293 equate_decl_number_to_die (decl, subr_die);
18296 else if (DECL_ABSTRACT (decl))
18298 if (DECL_DECLARED_INLINE_P (decl))
18300 if (cgraph_function_possibly_inlined_p (decl))
18301 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18302 else
18303 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18305 else
18307 if (cgraph_function_possibly_inlined_p (decl))
18308 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18309 else
18310 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18313 if (DECL_DECLARED_INLINE_P (decl)
18314 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18315 add_AT_flag (subr_die, DW_AT_artificial, 1);
18317 equate_decl_number_to_die (decl, subr_die);
18319 else if (!DECL_EXTERNAL (decl))
18321 HOST_WIDE_INT cfa_fb_offset;
18322 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18324 if (!old_die || !get_AT (old_die, DW_AT_inline))
18325 equate_decl_number_to_die (decl, subr_die);
18327 gcc_checking_assert (fun);
18328 if (!flag_reorder_blocks_and_partition)
18330 dw_fde_ref fde = fun->fde;
18331 if (fde->dw_fde_begin)
18333 /* We have already generated the labels. */
18334 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18335 fde->dw_fde_end, false);
18337 else
18339 /* Create start/end labels and add the range. */
18340 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18341 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18342 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18343 current_function_funcdef_no);
18344 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18345 current_function_funcdef_no);
18346 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18347 false);
18350 #if VMS_DEBUGGING_INFO
18351 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18352 Section 2.3 Prologue and Epilogue Attributes:
18353 When a breakpoint is set on entry to a function, it is generally
18354 desirable for execution to be suspended, not on the very first
18355 instruction of the function, but rather at a point after the
18356 function's frame has been set up, after any language defined local
18357 declaration processing has been completed, and before execution of
18358 the first statement of the function begins. Debuggers generally
18359 cannot properly determine where this point is. Similarly for a
18360 breakpoint set on exit from a function. The prologue and epilogue
18361 attributes allow a compiler to communicate the location(s) to use. */
18364 if (fde->dw_fde_vms_end_prologue)
18365 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18366 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18368 if (fde->dw_fde_vms_begin_epilogue)
18369 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18370 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18372 #endif
18375 else
18377 /* Generate pubnames entries for the split function code ranges. */
18378 dw_fde_ref fde = fun->fde;
18380 if (fde->dw_fde_second_begin)
18382 if (dwarf_version >= 3 || !dwarf_strict)
18384 /* We should use ranges for non-contiguous code section
18385 addresses. Use the actual code range for the initial
18386 section, since the HOT/COLD labels might precede an
18387 alignment offset. */
18388 bool range_list_added = false;
18389 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18390 fde->dw_fde_end, &range_list_added,
18391 false);
18392 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18393 fde->dw_fde_second_end,
18394 &range_list_added, false);
18395 if (range_list_added)
18396 add_ranges (NULL);
18398 else
18400 /* There is no real support in DW2 for this .. so we make
18401 a work-around. First, emit the pub name for the segment
18402 containing the function label. Then make and emit a
18403 simplified subprogram DIE for the second segment with the
18404 name pre-fixed by __hot/cold_sect_of_. We use the same
18405 linkage name for the second die so that gdb will find both
18406 sections when given "b foo". */
18407 const char *name = NULL;
18408 tree decl_name = DECL_NAME (decl);
18409 dw_die_ref seg_die;
18411 /* Do the 'primary' section. */
18412 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18413 fde->dw_fde_end, false);
18415 /* Build a minimal DIE for the secondary section. */
18416 seg_die = new_die (DW_TAG_subprogram,
18417 subr_die->die_parent, decl);
18419 if (TREE_PUBLIC (decl))
18420 add_AT_flag (seg_die, DW_AT_external, 1);
18422 if (decl_name != NULL
18423 && IDENTIFIER_POINTER (decl_name) != NULL)
18425 name = dwarf2_name (decl, 1);
18426 if (! DECL_ARTIFICIAL (decl))
18427 add_src_coords_attributes (seg_die, decl);
18429 add_linkage_name (seg_die, decl);
18431 gcc_assert (name != NULL);
18432 add_pure_or_virtual_attribute (seg_die, decl);
18433 if (DECL_ARTIFICIAL (decl))
18434 add_AT_flag (seg_die, DW_AT_artificial, 1);
18436 name = concat ("__second_sect_of_", name, NULL);
18437 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18438 fde->dw_fde_second_end, false);
18439 add_name_attribute (seg_die, name);
18440 if (want_pubnames ())
18441 add_pubname_string (name, seg_die);
18444 else
18445 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18446 false);
18449 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18451 /* We define the "frame base" as the function's CFA. This is more
18452 convenient for several reasons: (1) It's stable across the prologue
18453 and epilogue, which makes it better than just a frame pointer,
18454 (2) With dwarf3, there exists a one-byte encoding that allows us
18455 to reference the .debug_frame data by proxy, but failing that,
18456 (3) We can at least reuse the code inspection and interpretation
18457 code that determines the CFA position at various points in the
18458 function. */
18459 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18461 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18462 add_AT_loc (subr_die, DW_AT_frame_base, op);
18464 else
18466 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18467 if (list->dw_loc_next)
18468 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18469 else
18470 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18473 /* Compute a displacement from the "steady-state frame pointer" to
18474 the CFA. The former is what all stack slots and argument slots
18475 will reference in the rtl; the latter is what we've told the
18476 debugger about. We'll need to adjust all frame_base references
18477 by this displacement. */
18478 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18480 if (fun->static_chain_decl)
18481 add_AT_location_description (subr_die, DW_AT_static_link,
18482 loc_list_from_tree (fun->static_chain_decl, 2));
18485 /* Generate child dies for template paramaters. */
18486 if (debug_info_level > DINFO_LEVEL_TERSE)
18487 gen_generic_params_dies (decl);
18489 /* Now output descriptions of the arguments for this function. This gets
18490 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18491 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18492 `...' at the end of the formal parameter list. In order to find out if
18493 there was a trailing ellipsis or not, we must instead look at the type
18494 associated with the FUNCTION_DECL. This will be a node of type
18495 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18496 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18497 an ellipsis at the end. */
18499 /* In the case where we are describing a mere function declaration, all we
18500 need to do here (and all we *can* do here) is to describe the *types* of
18501 its formal parameters. */
18502 if (debug_info_level <= DINFO_LEVEL_TERSE)
18504 else if (declaration)
18505 gen_formal_types_die (decl, subr_die);
18506 else
18508 /* Generate DIEs to represent all known formal parameters. */
18509 tree parm = DECL_ARGUMENTS (decl);
18510 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18511 tree generic_decl_parm = generic_decl
18512 ? DECL_ARGUMENTS (generic_decl)
18513 : NULL;
18515 /* Now we want to walk the list of parameters of the function and
18516 emit their relevant DIEs.
18518 We consider the case of DECL being an instance of a generic function
18519 as well as it being a normal function.
18521 If DECL is an instance of a generic function we walk the
18522 parameters of the generic function declaration _and_ the parameters of
18523 DECL itself. This is useful because we want to emit specific DIEs for
18524 function parameter packs and those are declared as part of the
18525 generic function declaration. In that particular case,
18526 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18527 That DIE has children DIEs representing the set of arguments
18528 of the pack. Note that the set of pack arguments can be empty.
18529 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18530 children DIE.
18532 Otherwise, we just consider the parameters of DECL. */
18533 while (generic_decl_parm || parm)
18535 if (generic_decl_parm
18536 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18537 gen_formal_parameter_pack_die (generic_decl_parm,
18538 parm, subr_die,
18539 &parm);
18540 else if (parm)
18542 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18544 if (parm == DECL_ARGUMENTS (decl)
18545 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18546 && parm_die
18547 && (dwarf_version >= 3 || !dwarf_strict))
18548 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18550 parm = DECL_CHAIN (parm);
18553 if (generic_decl_parm)
18554 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18557 /* Decide whether we need an unspecified_parameters DIE at the end.
18558 There are 2 more cases to do this for: 1) the ansi ... declaration -
18559 this is detectable when the end of the arg list is not a
18560 void_type_node 2) an unprototyped function declaration (not a
18561 definition). This just means that we have no info about the
18562 parameters at all. */
18563 if (prototype_p (TREE_TYPE (decl)))
18565 /* This is the prototyped case, check for.... */
18566 if (stdarg_p (TREE_TYPE (decl)))
18567 gen_unspecified_parameters_die (decl, subr_die);
18569 else if (DECL_INITIAL (decl) == NULL_TREE)
18570 gen_unspecified_parameters_die (decl, subr_die);
18573 /* Output Dwarf info for all of the stuff within the body of the function
18574 (if it has one - it may be just a declaration). */
18575 outer_scope = DECL_INITIAL (decl);
18577 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18578 a function. This BLOCK actually represents the outermost binding contour
18579 for the function, i.e. the contour in which the function's formal
18580 parameters and labels get declared. Curiously, it appears that the front
18581 end doesn't actually put the PARM_DECL nodes for the current function onto
18582 the BLOCK_VARS list for this outer scope, but are strung off of the
18583 DECL_ARGUMENTS list for the function instead.
18585 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18586 the LABEL_DECL nodes for the function however, and we output DWARF info
18587 for those in decls_for_scope. Just within the `outer_scope' there will be
18588 a BLOCK node representing the function's outermost pair of curly braces,
18589 and any blocks used for the base and member initializers of a C++
18590 constructor function. */
18591 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18593 int call_site_note_count = 0;
18594 int tail_call_site_note_count = 0;
18596 /* Emit a DW_TAG_variable DIE for a named return value. */
18597 if (DECL_NAME (DECL_RESULT (decl)))
18598 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18600 current_function_has_inlines = 0;
18601 decls_for_scope (outer_scope, subr_die, 0);
18603 if (call_arg_locations && !dwarf_strict)
18605 struct call_arg_loc_node *ca_loc;
18606 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18608 dw_die_ref die = NULL;
18609 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18610 rtx arg, next_arg;
18612 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18613 arg; arg = next_arg)
18615 dw_loc_descr_ref reg, val;
18616 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18617 dw_die_ref cdie, tdie = NULL;
18619 next_arg = XEXP (arg, 1);
18620 if (REG_P (XEXP (XEXP (arg, 0), 0))
18621 && next_arg
18622 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18623 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18624 && REGNO (XEXP (XEXP (arg, 0), 0))
18625 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18626 next_arg = XEXP (next_arg, 1);
18627 if (mode == VOIDmode)
18629 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18630 if (mode == VOIDmode)
18631 mode = GET_MODE (XEXP (arg, 0));
18633 if (mode == VOIDmode || mode == BLKmode)
18634 continue;
18635 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18637 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18638 tloc = XEXP (XEXP (arg, 0), 1);
18639 continue;
18641 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18642 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18644 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18645 tlocc = XEXP (XEXP (arg, 0), 1);
18646 continue;
18648 reg = NULL;
18649 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18650 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18651 VAR_INIT_STATUS_INITIALIZED);
18652 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18654 rtx mem = XEXP (XEXP (arg, 0), 0);
18655 reg = mem_loc_descriptor (XEXP (mem, 0),
18656 get_address_mode (mem),
18657 GET_MODE (mem),
18658 VAR_INIT_STATUS_INITIALIZED);
18660 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18661 == DEBUG_PARAMETER_REF)
18663 tree tdecl
18664 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18665 tdie = lookup_decl_die (tdecl);
18666 if (tdie == NULL)
18667 continue;
18669 else
18670 continue;
18671 if (reg == NULL
18672 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18673 != DEBUG_PARAMETER_REF)
18674 continue;
18675 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18676 VOIDmode,
18677 VAR_INIT_STATUS_INITIALIZED);
18678 if (val == NULL)
18679 continue;
18680 if (die == NULL)
18681 die = gen_call_site_die (decl, subr_die, ca_loc);
18682 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18683 NULL_TREE);
18684 if (reg != NULL)
18685 add_AT_loc (cdie, DW_AT_location, reg);
18686 else if (tdie != NULL)
18687 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18688 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18689 if (next_arg != XEXP (arg, 1))
18691 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18692 if (mode == VOIDmode)
18693 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18694 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18695 0), 1),
18696 mode, VOIDmode,
18697 VAR_INIT_STATUS_INITIALIZED);
18698 if (val != NULL)
18699 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18702 if (die == NULL
18703 && (ca_loc->symbol_ref || tloc))
18704 die = gen_call_site_die (decl, subr_die, ca_loc);
18705 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18707 dw_loc_descr_ref tval = NULL;
18709 if (tloc != NULL_RTX)
18710 tval = mem_loc_descriptor (tloc,
18711 GET_MODE (tloc) == VOIDmode
18712 ? Pmode : GET_MODE (tloc),
18713 VOIDmode,
18714 VAR_INIT_STATUS_INITIALIZED);
18715 if (tval)
18716 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18717 else if (tlocc != NULL_RTX)
18719 tval = mem_loc_descriptor (tlocc,
18720 GET_MODE (tlocc) == VOIDmode
18721 ? Pmode : GET_MODE (tlocc),
18722 VOIDmode,
18723 VAR_INIT_STATUS_INITIALIZED);
18724 if (tval)
18725 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18726 tval);
18729 if (die != NULL)
18731 call_site_note_count++;
18732 if (ca_loc->tail_call_p)
18733 tail_call_site_note_count++;
18737 call_arg_locations = NULL;
18738 call_arg_loc_last = NULL;
18739 if (tail_call_site_count >= 0
18740 && tail_call_site_count == tail_call_site_note_count
18741 && !dwarf_strict)
18743 if (call_site_count >= 0
18744 && call_site_count == call_site_note_count)
18745 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18746 else
18747 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18749 call_site_count = -1;
18750 tail_call_site_count = -1;
18753 if (subr_die != old_die)
18754 /* Add the calling convention attribute if requested. */
18755 add_calling_convention_attribute (subr_die, decl);
18758 /* Returns a hash value for X (which really is a die_struct). */
18760 static hashval_t
18761 common_block_die_table_hash (const void *x)
18763 const_dw_die_ref d = (const_dw_die_ref) x;
18764 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18767 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18768 as decl_id and die_parent of die_struct Y. */
18770 static int
18771 common_block_die_table_eq (const void *x, const void *y)
18773 const_dw_die_ref d = (const_dw_die_ref) x;
18774 const_dw_die_ref e = (const_dw_die_ref) y;
18775 return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18778 /* Generate a DIE to represent a declared data object.
18779 Either DECL or ORIGIN must be non-null. */
18781 static void
18782 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18784 HOST_WIDE_INT off = 0;
18785 tree com_decl;
18786 tree decl_or_origin = decl ? decl : origin;
18787 tree ultimate_origin;
18788 dw_die_ref var_die;
18789 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18790 dw_die_ref origin_die;
18791 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18792 || class_or_namespace_scope_p (context_die));
18793 bool specialization_p = false;
18795 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18796 if (decl || ultimate_origin)
18797 origin = ultimate_origin;
18798 com_decl = fortran_common (decl_or_origin, &off);
18800 /* Symbol in common gets emitted as a child of the common block, in the form
18801 of a data member. */
18802 if (com_decl)
18804 dw_die_ref com_die;
18805 dw_loc_list_ref loc;
18806 die_node com_die_arg;
18808 var_die = lookup_decl_die (decl_or_origin);
18809 if (var_die)
18811 if (get_AT (var_die, DW_AT_location) == NULL)
18813 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18814 if (loc)
18816 if (off)
18818 /* Optimize the common case. */
18819 if (single_element_loc_list_p (loc)
18820 && loc->expr->dw_loc_opc == DW_OP_addr
18821 && loc->expr->dw_loc_next == NULL
18822 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18823 == SYMBOL_REF)
18825 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18826 loc->expr->dw_loc_oprnd1.v.val_addr
18827 = plus_constant (GET_MODE (x), x , off);
18829 else
18830 loc_list_plus_const (loc, off);
18832 add_AT_location_description (var_die, DW_AT_location, loc);
18833 remove_AT (var_die, DW_AT_declaration);
18836 return;
18839 if (common_block_die_table == NULL)
18840 common_block_die_table
18841 = htab_create_ggc (10, common_block_die_table_hash,
18842 common_block_die_table_eq, NULL);
18844 com_die_arg.decl_id = DECL_UID (com_decl);
18845 com_die_arg.die_parent = context_die;
18846 com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18847 loc = loc_list_from_tree (com_decl, 2);
18848 if (com_die == NULL)
18850 const char *cnam
18851 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18852 void **slot;
18854 com_die = new_die (DW_TAG_common_block, context_die, decl);
18855 add_name_and_src_coords_attributes (com_die, com_decl);
18856 if (loc)
18858 add_AT_location_description (com_die, DW_AT_location, loc);
18859 /* Avoid sharing the same loc descriptor between
18860 DW_TAG_common_block and DW_TAG_variable. */
18861 loc = loc_list_from_tree (com_decl, 2);
18863 else if (DECL_EXTERNAL (decl))
18864 add_AT_flag (com_die, DW_AT_declaration, 1);
18865 if (want_pubnames ())
18866 add_pubname_string (cnam, com_die); /* ??? needed? */
18867 com_die->decl_id = DECL_UID (com_decl);
18868 slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18869 *slot = (void *) com_die;
18871 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18873 add_AT_location_description (com_die, DW_AT_location, loc);
18874 loc = loc_list_from_tree (com_decl, 2);
18875 remove_AT (com_die, DW_AT_declaration);
18877 var_die = new_die (DW_TAG_variable, com_die, decl);
18878 add_name_and_src_coords_attributes (var_die, decl);
18879 add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18880 TREE_THIS_VOLATILE (decl), context_die);
18881 add_AT_flag (var_die, DW_AT_external, 1);
18882 if (loc)
18884 if (off)
18886 /* Optimize the common case. */
18887 if (single_element_loc_list_p (loc)
18888 && loc->expr->dw_loc_opc == DW_OP_addr
18889 && loc->expr->dw_loc_next == NULL
18890 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18892 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18893 loc->expr->dw_loc_oprnd1.v.val_addr
18894 = plus_constant (GET_MODE (x), x, off);
18896 else
18897 loc_list_plus_const (loc, off);
18899 add_AT_location_description (var_die, DW_AT_location, loc);
18901 else if (DECL_EXTERNAL (decl))
18902 add_AT_flag (var_die, DW_AT_declaration, 1);
18903 equate_decl_number_to_die (decl, var_die);
18904 return;
18907 /* If the compiler emitted a definition for the DECL declaration
18908 and if we already emitted a DIE for it, don't emit a second
18909 DIE for it again. Allow re-declarations of DECLs that are
18910 inside functions, though. */
18911 if (old_die && declaration && !local_scope_p (context_die))
18912 return;
18914 /* For static data members, the declaration in the class is supposed
18915 to have DW_TAG_member tag; the specification should still be
18916 DW_TAG_variable referencing the DW_TAG_member DIE. */
18917 if (declaration && class_scope_p (context_die))
18918 var_die = new_die (DW_TAG_member, context_die, decl);
18919 else
18920 var_die = new_die (DW_TAG_variable, context_die, decl);
18922 origin_die = NULL;
18923 if (origin != NULL)
18924 origin_die = add_abstract_origin_attribute (var_die, origin);
18926 /* Loop unrolling can create multiple blocks that refer to the same
18927 static variable, so we must test for the DW_AT_declaration flag.
18929 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18930 copy decls and set the DECL_ABSTRACT flag on them instead of
18931 sharing them.
18933 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18935 ??? The declare_in_namespace support causes us to get two DIEs for one
18936 variable, both of which are declarations. We want to avoid considering
18937 one to be a specification, so we must test that this DIE is not a
18938 declaration. */
18939 else if (old_die && TREE_STATIC (decl) && ! declaration
18940 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18942 /* This is a definition of a C++ class level static. */
18943 add_AT_specification (var_die, old_die);
18944 specialization_p = true;
18945 if (DECL_NAME (decl))
18947 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18948 struct dwarf_file_data * file_index = lookup_filename (s.file);
18950 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18951 add_AT_file (var_die, DW_AT_decl_file, file_index);
18953 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18954 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18956 if (old_die->die_tag == DW_TAG_member)
18957 add_linkage_name (var_die, decl);
18960 else
18961 add_name_and_src_coords_attributes (var_die, decl);
18963 if ((origin == NULL && !specialization_p)
18964 || (origin != NULL
18965 && !DECL_ABSTRACT (decl_or_origin)
18966 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18967 decl_function_context
18968 (decl_or_origin))))
18970 tree type = TREE_TYPE (decl_or_origin);
18972 if (decl_by_reference_p (decl_or_origin))
18973 add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18974 else
18975 add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18976 TREE_THIS_VOLATILE (decl_or_origin), context_die);
18979 if (origin == NULL && !specialization_p)
18981 if (TREE_PUBLIC (decl))
18982 add_AT_flag (var_die, DW_AT_external, 1);
18984 if (DECL_ARTIFICIAL (decl))
18985 add_AT_flag (var_die, DW_AT_artificial, 1);
18987 add_accessibility_attribute (var_die, decl);
18990 if (declaration)
18991 add_AT_flag (var_die, DW_AT_declaration, 1);
18993 if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18994 equate_decl_number_to_die (decl, var_die);
18996 if (! declaration
18997 && (! DECL_ABSTRACT (decl_or_origin)
18998 /* Local static vars are shared between all clones/inlines,
18999 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19000 already set. */
19001 || (TREE_CODE (decl_or_origin) == VAR_DECL
19002 && TREE_STATIC (decl_or_origin)
19003 && DECL_RTL_SET_P (decl_or_origin)))
19004 /* When abstract origin already has DW_AT_location attribute, no need
19005 to add it again. */
19006 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19008 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19009 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19010 defer_location (decl_or_origin, var_die);
19011 else
19012 add_location_or_const_value_attribute (var_die, decl_or_origin,
19013 decl == NULL, DW_AT_location);
19014 add_pubname (decl_or_origin, var_die);
19016 else
19017 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19020 /* Generate a DIE to represent a named constant. */
19022 static void
19023 gen_const_die (tree decl, dw_die_ref context_die)
19025 dw_die_ref const_die;
19026 tree type = TREE_TYPE (decl);
19028 const_die = new_die (DW_TAG_constant, context_die, decl);
19029 add_name_and_src_coords_attributes (const_die, decl);
19030 add_type_attribute (const_die, type, 1, 0, context_die);
19031 if (TREE_PUBLIC (decl))
19032 add_AT_flag (const_die, DW_AT_external, 1);
19033 if (DECL_ARTIFICIAL (decl))
19034 add_AT_flag (const_die, DW_AT_artificial, 1);
19035 tree_add_const_value_attribute_for_decl (const_die, decl);
19038 /* Generate a DIE to represent a label identifier. */
19040 static void
19041 gen_label_die (tree decl, dw_die_ref context_die)
19043 tree origin = decl_ultimate_origin (decl);
19044 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19045 rtx insn;
19046 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19048 if (origin != NULL)
19049 add_abstract_origin_attribute (lbl_die, origin);
19050 else
19051 add_name_and_src_coords_attributes (lbl_die, decl);
19053 if (DECL_ABSTRACT (decl))
19054 equate_decl_number_to_die (decl, lbl_die);
19055 else
19057 insn = DECL_RTL_IF_SET (decl);
19059 /* Deleted labels are programmer specified labels which have been
19060 eliminated because of various optimizations. We still emit them
19061 here so that it is possible to put breakpoints on them. */
19062 if (insn
19063 && (LABEL_P (insn)
19064 || ((NOTE_P (insn)
19065 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19067 /* When optimization is enabled (via -O) some parts of the compiler
19068 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19069 represent source-level labels which were explicitly declared by
19070 the user. This really shouldn't be happening though, so catch
19071 it if it ever does happen. */
19072 gcc_assert (!INSN_DELETED_P (insn));
19074 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19075 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19077 else if (insn
19078 && NOTE_P (insn)
19079 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19080 && CODE_LABEL_NUMBER (insn) != -1)
19082 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19083 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19088 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19089 attributes to the DIE for a block STMT, to describe where the inlined
19090 function was called from. This is similar to add_src_coords_attributes. */
19092 static inline void
19093 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19095 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19097 if (dwarf_version >= 3 || !dwarf_strict)
19099 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19100 add_AT_unsigned (die, DW_AT_call_line, s.line);
19105 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19106 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19108 static inline void
19109 add_high_low_attributes (tree stmt, dw_die_ref die)
19111 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19113 if (BLOCK_FRAGMENT_CHAIN (stmt)
19114 && (dwarf_version >= 3 || !dwarf_strict))
19116 tree chain, superblock = NULL_TREE;
19117 dw_die_ref pdie;
19118 dw_attr_ref attr = NULL;
19120 if (inlined_function_outer_scope_p (stmt))
19122 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19123 BLOCK_NUMBER (stmt));
19124 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19127 /* Optimize duplicate .debug_ranges lists or even tails of
19128 lists. If this BLOCK has same ranges as its supercontext,
19129 lookup DW_AT_ranges attribute in the supercontext (and
19130 recursively so), verify that the ranges_table contains the
19131 right values and use it instead of adding a new .debug_range. */
19132 for (chain = stmt, pdie = die;
19133 BLOCK_SAME_RANGE (chain);
19134 chain = BLOCK_SUPERCONTEXT (chain))
19136 dw_attr_ref new_attr;
19138 pdie = pdie->die_parent;
19139 if (pdie == NULL)
19140 break;
19141 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19142 break;
19143 new_attr = get_AT (pdie, DW_AT_ranges);
19144 if (new_attr == NULL
19145 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19146 break;
19147 attr = new_attr;
19148 superblock = BLOCK_SUPERCONTEXT (chain);
19150 if (attr != NULL
19151 && (ranges_table[attr->dw_attr_val.v.val_offset
19152 / 2 / DWARF2_ADDR_SIZE].num
19153 == BLOCK_NUMBER (superblock))
19154 && BLOCK_FRAGMENT_CHAIN (superblock))
19156 unsigned long off = attr->dw_attr_val.v.val_offset
19157 / 2 / DWARF2_ADDR_SIZE;
19158 unsigned long supercnt = 0, thiscnt = 0;
19159 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19160 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19162 ++supercnt;
19163 gcc_checking_assert (ranges_table[off + supercnt].num
19164 == BLOCK_NUMBER (chain));
19166 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19167 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19168 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19169 ++thiscnt;
19170 gcc_assert (supercnt >= thiscnt);
19171 add_AT_range_list (die, DW_AT_ranges,
19172 ((off + supercnt - thiscnt)
19173 * 2 * DWARF2_ADDR_SIZE),
19174 false);
19175 return;
19178 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19180 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19183 add_ranges (chain);
19184 chain = BLOCK_FRAGMENT_CHAIN (chain);
19186 while (chain);
19187 add_ranges (NULL);
19189 else
19191 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19192 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19193 BLOCK_NUMBER (stmt));
19194 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19195 BLOCK_NUMBER (stmt));
19196 add_AT_low_high_pc (die, label, label_high, false);
19200 /* Generate a DIE for a lexical block. */
19202 static void
19203 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19205 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19207 if (call_arg_locations)
19209 if (block_map.length () <= BLOCK_NUMBER (stmt))
19210 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19211 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19214 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19215 add_high_low_attributes (stmt, stmt_die);
19217 decls_for_scope (stmt, stmt_die, depth);
19220 /* Generate a DIE for an inlined subprogram. */
19222 static void
19223 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19225 tree decl;
19227 /* The instance of function that is effectively being inlined shall not
19228 be abstract. */
19229 gcc_assert (! BLOCK_ABSTRACT (stmt));
19231 decl = block_ultimate_origin (stmt);
19233 /* Emit info for the abstract instance first, if we haven't yet. We
19234 must emit this even if the block is abstract, otherwise when we
19235 emit the block below (or elsewhere), we may end up trying to emit
19236 a die whose origin die hasn't been emitted, and crashing. */
19237 dwarf2out_abstract_function (decl);
19239 if (! BLOCK_ABSTRACT (stmt))
19241 dw_die_ref subr_die
19242 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19244 if (call_arg_locations)
19246 if (block_map.length () <= BLOCK_NUMBER (stmt))
19247 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19248 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19250 add_abstract_origin_attribute (subr_die, decl);
19251 if (TREE_ASM_WRITTEN (stmt))
19252 add_high_low_attributes (stmt, subr_die);
19253 add_call_src_coords_attributes (stmt, subr_die);
19255 decls_for_scope (stmt, subr_die, depth);
19256 current_function_has_inlines = 1;
19260 /* Generate a DIE for a field in a record, or structure. */
19262 static void
19263 gen_field_die (tree decl, dw_die_ref context_die)
19265 dw_die_ref decl_die;
19267 if (TREE_TYPE (decl) == error_mark_node)
19268 return;
19270 decl_die = new_die (DW_TAG_member, context_die, decl);
19271 add_name_and_src_coords_attributes (decl_die, decl);
19272 add_type_attribute (decl_die, member_declared_type (decl),
19273 TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19274 context_die);
19276 if (DECL_BIT_FIELD_TYPE (decl))
19278 add_byte_size_attribute (decl_die, decl);
19279 add_bit_size_attribute (decl_die, decl);
19280 add_bit_offset_attribute (decl_die, decl);
19283 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19284 add_data_member_location_attribute (decl_die, decl);
19286 if (DECL_ARTIFICIAL (decl))
19287 add_AT_flag (decl_die, DW_AT_artificial, 1);
19289 add_accessibility_attribute (decl_die, decl);
19291 /* Equate decl number to die, so that we can look up this decl later on. */
19292 equate_decl_number_to_die (decl, decl_die);
19295 #if 0
19296 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19297 Use modified_type_die instead.
19298 We keep this code here just in case these types of DIEs may be needed to
19299 represent certain things in other languages (e.g. Pascal) someday. */
19301 static void
19302 gen_pointer_type_die (tree type, dw_die_ref context_die)
19304 dw_die_ref ptr_die
19305 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19307 equate_type_number_to_die (type, ptr_die);
19308 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19309 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19312 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19313 Use modified_type_die instead.
19314 We keep this code here just in case these types of DIEs may be needed to
19315 represent certain things in other languages (e.g. Pascal) someday. */
19317 static void
19318 gen_reference_type_die (tree type, dw_die_ref context_die)
19320 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19322 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19323 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19324 else
19325 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19327 equate_type_number_to_die (type, ref_die);
19328 add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19329 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19331 #endif
19333 /* Generate a DIE for a pointer to a member type. */
19335 static void
19336 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19338 dw_die_ref ptr_die
19339 = new_die (DW_TAG_ptr_to_member_type,
19340 scope_die_for (type, context_die), type);
19342 equate_type_number_to_die (type, ptr_die);
19343 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19344 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19345 add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19348 typedef const char *dchar_p; /* For DEF_VEC_P. */
19350 static char *producer_string;
19352 /* Return a heap allocated producer string including command line options
19353 if -grecord-gcc-switches. */
19355 static char *
19356 gen_producer_string (void)
19358 size_t j;
19359 auto_vec<dchar_p> switches;
19360 const char *language_string = lang_hooks.name;
19361 char *producer, *tail;
19362 const char *p;
19363 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19364 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19366 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19367 switch (save_decoded_options[j].opt_index)
19369 case OPT_o:
19370 case OPT_d:
19371 case OPT_dumpbase:
19372 case OPT_dumpdir:
19373 case OPT_auxbase:
19374 case OPT_auxbase_strip:
19375 case OPT_quiet:
19376 case OPT_version:
19377 case OPT_v:
19378 case OPT_w:
19379 case OPT_L:
19380 case OPT_D:
19381 case OPT_I:
19382 case OPT_U:
19383 case OPT_SPECIAL_unknown:
19384 case OPT_SPECIAL_ignore:
19385 case OPT_SPECIAL_program_name:
19386 case OPT_SPECIAL_input_file:
19387 case OPT_grecord_gcc_switches:
19388 case OPT_gno_record_gcc_switches:
19389 case OPT__output_pch_:
19390 case OPT_fdiagnostics_show_location_:
19391 case OPT_fdiagnostics_show_option:
19392 case OPT_fdiagnostics_show_caret:
19393 case OPT_fdiagnostics_color_:
19394 case OPT_fverbose_asm:
19395 case OPT____:
19396 case OPT__sysroot_:
19397 case OPT_nostdinc:
19398 case OPT_nostdinc__:
19399 /* Ignore these. */
19400 continue;
19401 default:
19402 if (cl_options[save_decoded_options[j].opt_index].flags
19403 & CL_NO_DWARF_RECORD)
19404 continue;
19405 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19406 == '-');
19407 switch (save_decoded_options[j].canonical_option[0][1])
19409 case 'M':
19410 case 'i':
19411 case 'W':
19412 continue;
19413 case 'f':
19414 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19415 "dump", 4) == 0)
19416 continue;
19417 break;
19418 default:
19419 break;
19421 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19422 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19423 break;
19426 producer = XNEWVEC (char, plen + 1 + len + 1);
19427 tail = producer;
19428 sprintf (tail, "%s %s", language_string, version_string);
19429 tail += plen;
19431 FOR_EACH_VEC_ELT (switches, j, p)
19433 len = strlen (p);
19434 *tail = ' ';
19435 memcpy (tail + 1, p, len);
19436 tail += len + 1;
19439 *tail = '\0';
19440 return producer;
19443 /* Generate the DIE for the compilation unit. */
19445 static dw_die_ref
19446 gen_compile_unit_die (const char *filename)
19448 dw_die_ref die;
19449 const char *language_string = lang_hooks.name;
19450 int language;
19452 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19454 if (filename)
19456 add_name_attribute (die, filename);
19457 /* Don't add cwd for <built-in>. */
19458 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19459 add_comp_dir_attribute (die);
19462 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19464 /* If our producer is LTO try to figure out a common language to use
19465 from the global list of translation units. */
19466 if (strcmp (language_string, "GNU GIMPLE") == 0)
19468 unsigned i;
19469 tree t;
19470 const char *common_lang = NULL;
19472 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19474 if (!TRANSLATION_UNIT_LANGUAGE (t))
19475 continue;
19476 if (!common_lang)
19477 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19478 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19480 else if (strncmp (common_lang, "GNU C", 5) == 0
19481 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19482 /* Mixing C and C++ is ok, use C++ in that case. */
19483 common_lang = "GNU C++";
19484 else
19486 /* Fall back to C. */
19487 common_lang = NULL;
19488 break;
19492 if (common_lang)
19493 language_string = common_lang;
19496 language = DW_LANG_C89;
19497 if (strcmp (language_string, "GNU C++") == 0)
19498 language = DW_LANG_C_plus_plus;
19499 else if (strcmp (language_string, "GNU F77") == 0)
19500 language = DW_LANG_Fortran77;
19501 else if (strcmp (language_string, "GNU Pascal") == 0)
19502 language = DW_LANG_Pascal83;
19503 else if (dwarf_version >= 3 || !dwarf_strict)
19505 if (strcmp (language_string, "GNU Ada") == 0)
19506 language = DW_LANG_Ada95;
19507 else if (strcmp (language_string, "GNU Fortran") == 0)
19508 language = DW_LANG_Fortran95;
19509 else if (strcmp (language_string, "GNU Java") == 0)
19510 language = DW_LANG_Java;
19511 else if (strcmp (language_string, "GNU Objective-C") == 0)
19512 language = DW_LANG_ObjC;
19513 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19514 language = DW_LANG_ObjC_plus_plus;
19515 else if (dwarf_version >= 5 || !dwarf_strict)
19517 if (strcmp (language_string, "GNU Go") == 0)
19518 language = DW_LANG_Go;
19521 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19522 else if (strcmp (language_string, "GNU Fortran") == 0)
19523 language = DW_LANG_Fortran90;
19525 add_AT_unsigned (die, DW_AT_language, language);
19527 switch (language)
19529 case DW_LANG_Fortran77:
19530 case DW_LANG_Fortran90:
19531 case DW_LANG_Fortran95:
19532 /* Fortran has case insensitive identifiers and the front-end
19533 lowercases everything. */
19534 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19535 break;
19536 default:
19537 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19538 break;
19540 return die;
19543 /* Generate the DIE for a base class. */
19545 static void
19546 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19548 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19550 add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19551 add_data_member_location_attribute (die, binfo);
19553 if (BINFO_VIRTUAL_P (binfo))
19554 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19556 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19557 children, otherwise the default is DW_ACCESS_public. In DWARF2
19558 the default has always been DW_ACCESS_private. */
19559 if (access == access_public_node)
19561 if (dwarf_version == 2
19562 || context_die->die_tag == DW_TAG_class_type)
19563 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19565 else if (access == access_protected_node)
19566 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19567 else if (dwarf_version > 2
19568 && context_die->die_tag != DW_TAG_class_type)
19569 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19572 /* Generate a DIE for a class member. */
19574 static void
19575 gen_member_die (tree type, dw_die_ref context_die)
19577 tree member;
19578 tree binfo = TYPE_BINFO (type);
19579 dw_die_ref child;
19581 /* If this is not an incomplete type, output descriptions of each of its
19582 members. Note that as we output the DIEs necessary to represent the
19583 members of this record or union type, we will also be trying to output
19584 DIEs to represent the *types* of those members. However the `type'
19585 function (above) will specifically avoid generating type DIEs for member
19586 types *within* the list of member DIEs for this (containing) type except
19587 for those types (of members) which are explicitly marked as also being
19588 members of this (containing) type themselves. The g++ front- end can
19589 force any given type to be treated as a member of some other (containing)
19590 type by setting the TYPE_CONTEXT of the given (member) type to point to
19591 the TREE node representing the appropriate (containing) type. */
19593 /* First output info about the base classes. */
19594 if (binfo)
19596 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19597 int i;
19598 tree base;
19600 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19601 gen_inheritance_die (base,
19602 (accesses ? (*accesses)[i] : access_public_node),
19603 context_die);
19606 /* Now output info about the data members and type members. */
19607 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19609 /* If we thought we were generating minimal debug info for TYPE
19610 and then changed our minds, some of the member declarations
19611 may have already been defined. Don't define them again, but
19612 do put them in the right order. */
19614 child = lookup_decl_die (member);
19615 if (child)
19616 splice_child_die (context_die, child);
19617 else
19618 gen_decl_die (member, NULL, context_die);
19621 /* Now output info about the function members (if any). */
19622 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19624 /* Don't include clones in the member list. */
19625 if (DECL_ABSTRACT_ORIGIN (member))
19626 continue;
19628 child = lookup_decl_die (member);
19629 if (child)
19630 splice_child_die (context_die, child);
19631 else
19632 gen_decl_die (member, NULL, context_die);
19636 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19637 is set, we pretend that the type was never defined, so we only get the
19638 member DIEs needed by later specification DIEs. */
19640 static void
19641 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19642 enum debug_info_usage usage)
19644 dw_die_ref type_die = lookup_type_die (type);
19645 dw_die_ref scope_die = 0;
19646 int nested = 0;
19647 int complete = (TYPE_SIZE (type)
19648 && (! TYPE_STUB_DECL (type)
19649 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19650 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19651 complete = complete && should_emit_struct_debug (type, usage);
19653 if (type_die && ! complete)
19654 return;
19656 if (TYPE_CONTEXT (type) != NULL_TREE
19657 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19658 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19659 nested = 1;
19661 scope_die = scope_die_for (type, context_die);
19663 /* Generate child dies for template paramaters. */
19664 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19665 schedule_generic_params_dies_gen (type);
19667 if (! type_die || (nested && is_cu_die (scope_die)))
19668 /* First occurrence of type or toplevel definition of nested class. */
19670 dw_die_ref old_die = type_die;
19672 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19673 ? record_type_tag (type) : DW_TAG_union_type,
19674 scope_die, type);
19675 equate_type_number_to_die (type, type_die);
19676 if (old_die)
19677 add_AT_specification (type_die, old_die);
19678 else
19679 add_name_attribute (type_die, type_tag (type));
19681 else
19682 remove_AT (type_die, DW_AT_declaration);
19684 /* If this type has been completed, then give it a byte_size attribute and
19685 then give a list of members. */
19686 if (complete && !ns_decl)
19688 /* Prevent infinite recursion in cases where the type of some member of
19689 this type is expressed in terms of this type itself. */
19690 TREE_ASM_WRITTEN (type) = 1;
19691 add_byte_size_attribute (type_die, type);
19692 if (TYPE_STUB_DECL (type) != NULL_TREE)
19694 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19695 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19698 /* If the first reference to this type was as the return type of an
19699 inline function, then it may not have a parent. Fix this now. */
19700 if (type_die->die_parent == NULL)
19701 add_child_die (scope_die, type_die);
19703 push_decl_scope (type);
19704 gen_member_die (type, type_die);
19705 pop_decl_scope ();
19707 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19708 if (TYPE_ARTIFICIAL (type))
19709 add_AT_flag (type_die, DW_AT_artificial, 1);
19711 /* GNU extension: Record what type our vtable lives in. */
19712 if (TYPE_VFIELD (type))
19714 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19716 gen_type_die (vtype, context_die);
19717 add_AT_die_ref (type_die, DW_AT_containing_type,
19718 lookup_type_die (vtype));
19721 else
19723 add_AT_flag (type_die, DW_AT_declaration, 1);
19725 /* We don't need to do this for function-local types. */
19726 if (TYPE_STUB_DECL (type)
19727 && ! decl_function_context (TYPE_STUB_DECL (type)))
19728 vec_safe_push (incomplete_types, type);
19731 if (get_AT (type_die, DW_AT_name))
19732 add_pubtype (type, type_die);
19735 /* Generate a DIE for a subroutine _type_. */
19737 static void
19738 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19740 tree return_type = TREE_TYPE (type);
19741 dw_die_ref subr_die
19742 = new_die (DW_TAG_subroutine_type,
19743 scope_die_for (type, context_die), type);
19745 equate_type_number_to_die (type, subr_die);
19746 add_prototyped_attribute (subr_die, type);
19747 add_type_attribute (subr_die, return_type, 0, 0, context_die);
19748 gen_formal_types_die (type, subr_die);
19750 if (get_AT (subr_die, DW_AT_name))
19751 add_pubtype (type, subr_die);
19754 /* Generate a DIE for a type definition. */
19756 static void
19757 gen_typedef_die (tree decl, dw_die_ref context_die)
19759 dw_die_ref type_die;
19760 tree origin;
19762 if (TREE_ASM_WRITTEN (decl))
19763 return;
19765 TREE_ASM_WRITTEN (decl) = 1;
19766 type_die = new_die (DW_TAG_typedef, context_die, decl);
19767 origin = decl_ultimate_origin (decl);
19768 if (origin != NULL)
19769 add_abstract_origin_attribute (type_die, origin);
19770 else
19772 tree type;
19774 add_name_and_src_coords_attributes (type_die, decl);
19775 if (DECL_ORIGINAL_TYPE (decl))
19777 type = DECL_ORIGINAL_TYPE (decl);
19779 gcc_assert (type != TREE_TYPE (decl));
19780 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19782 else
19784 type = TREE_TYPE (decl);
19786 if (is_naming_typedef_decl (TYPE_NAME (type)))
19788 /* Here, we are in the case of decl being a typedef naming
19789 an anonymous type, e.g:
19790 typedef struct {...} foo;
19791 In that case TREE_TYPE (decl) is not a typedef variant
19792 type and TYPE_NAME of the anonymous type is set to the
19793 TYPE_DECL of the typedef. This construct is emitted by
19794 the C++ FE.
19796 TYPE is the anonymous struct named by the typedef
19797 DECL. As we need the DW_AT_type attribute of the
19798 DW_TAG_typedef to point to the DIE of TYPE, let's
19799 generate that DIE right away. add_type_attribute
19800 called below will then pick (via lookup_type_die) that
19801 anonymous struct DIE. */
19802 if (!TREE_ASM_WRITTEN (type))
19803 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19805 /* This is a GNU Extension. We are adding a
19806 DW_AT_linkage_name attribute to the DIE of the
19807 anonymous struct TYPE. The value of that attribute
19808 is the name of the typedef decl naming the anonymous
19809 struct. This greatly eases the work of consumers of
19810 this debug info. */
19811 add_linkage_attr (lookup_type_die (type), decl);
19815 add_type_attribute (type_die, type, TREE_READONLY (decl),
19816 TREE_THIS_VOLATILE (decl), context_die);
19818 if (is_naming_typedef_decl (decl))
19819 /* We want that all subsequent calls to lookup_type_die with
19820 TYPE in argument yield the DW_TAG_typedef we have just
19821 created. */
19822 equate_type_number_to_die (type, type_die);
19824 add_accessibility_attribute (type_die, decl);
19827 if (DECL_ABSTRACT (decl))
19828 equate_decl_number_to_die (decl, type_die);
19830 if (get_AT (type_die, DW_AT_name))
19831 add_pubtype (decl, type_die);
19834 /* Generate a DIE for a struct, class, enum or union type. */
19836 static void
19837 gen_tagged_type_die (tree type,
19838 dw_die_ref context_die,
19839 enum debug_info_usage usage)
19841 int need_pop;
19843 if (type == NULL_TREE
19844 || !is_tagged_type (type))
19845 return;
19847 /* If this is a nested type whose containing class hasn't been written
19848 out yet, writing it out will cover this one, too. This does not apply
19849 to instantiations of member class templates; they need to be added to
19850 the containing class as they are generated. FIXME: This hurts the
19851 idea of combining type decls from multiple TUs, since we can't predict
19852 what set of template instantiations we'll get. */
19853 if (TYPE_CONTEXT (type)
19854 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19855 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19857 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19859 if (TREE_ASM_WRITTEN (type))
19860 return;
19862 /* If that failed, attach ourselves to the stub. */
19863 push_decl_scope (TYPE_CONTEXT (type));
19864 context_die = lookup_type_die (TYPE_CONTEXT (type));
19865 need_pop = 1;
19867 else if (TYPE_CONTEXT (type) != NULL_TREE
19868 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19870 /* If this type is local to a function that hasn't been written
19871 out yet, use a NULL context for now; it will be fixed up in
19872 decls_for_scope. */
19873 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19874 /* A declaration DIE doesn't count; nested types need to go in the
19875 specification. */
19876 if (context_die && is_declaration_die (context_die))
19877 context_die = NULL;
19878 need_pop = 0;
19880 else
19882 context_die = declare_in_namespace (type, context_die);
19883 need_pop = 0;
19886 if (TREE_CODE (type) == ENUMERAL_TYPE)
19888 /* This might have been written out by the call to
19889 declare_in_namespace. */
19890 if (!TREE_ASM_WRITTEN (type))
19891 gen_enumeration_type_die (type, context_die);
19893 else
19894 gen_struct_or_union_type_die (type, context_die, usage);
19896 if (need_pop)
19897 pop_decl_scope ();
19899 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19900 it up if it is ever completed. gen_*_type_die will set it for us
19901 when appropriate. */
19904 /* Generate a type description DIE. */
19906 static void
19907 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19908 enum debug_info_usage usage)
19910 struct array_descr_info info;
19912 if (type == NULL_TREE || type == error_mark_node)
19913 return;
19915 if (TYPE_NAME (type) != NULL_TREE
19916 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19917 && is_redundant_typedef (TYPE_NAME (type))
19918 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19919 /* The DECL of this type is a typedef we don't want to emit debug
19920 info for but we want debug info for its underlying typedef.
19921 This can happen for e.g, the injected-class-name of a C++
19922 type. */
19923 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19925 /* If TYPE is a typedef type variant, let's generate debug info
19926 for the parent typedef which TYPE is a type of. */
19927 if (typedef_variant_p (type))
19929 if (TREE_ASM_WRITTEN (type))
19930 return;
19932 /* Prevent broken recursion; we can't hand off to the same type. */
19933 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19935 /* Give typedefs the right scope. */
19936 context_die = scope_die_for (type, context_die);
19938 TREE_ASM_WRITTEN (type) = 1;
19940 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19941 return;
19944 /* If type is an anonymous tagged type named by a typedef, let's
19945 generate debug info for the typedef. */
19946 if (is_naming_typedef_decl (TYPE_NAME (type)))
19948 /* Use the DIE of the containing namespace as the parent DIE of
19949 the type description DIE we want to generate. */
19950 if (DECL_CONTEXT (TYPE_NAME (type))
19951 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19952 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19954 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19955 return;
19958 /* If this is an array type with hidden descriptor, handle it first. */
19959 if (!TREE_ASM_WRITTEN (type)
19960 && lang_hooks.types.get_array_descr_info
19961 && lang_hooks.types.get_array_descr_info (type, &info)
19962 && (dwarf_version >= 3 || !dwarf_strict))
19964 gen_descr_array_type_die (type, &info, context_die);
19965 TREE_ASM_WRITTEN (type) = 1;
19966 return;
19969 /* We are going to output a DIE to represent the unqualified version
19970 of this type (i.e. without any const or volatile qualifiers) so
19971 get the main variant (i.e. the unqualified version) of this type
19972 now. (Vectors are special because the debugging info is in the
19973 cloned type itself). */
19974 if (TREE_CODE (type) != VECTOR_TYPE)
19975 type = type_main_variant (type);
19977 if (TREE_ASM_WRITTEN (type))
19978 return;
19980 switch (TREE_CODE (type))
19982 case ERROR_MARK:
19983 break;
19985 case POINTER_TYPE:
19986 case REFERENCE_TYPE:
19987 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
19988 ensures that the gen_type_die recursion will terminate even if the
19989 type is recursive. Recursive types are possible in Ada. */
19990 /* ??? We could perhaps do this for all types before the switch
19991 statement. */
19992 TREE_ASM_WRITTEN (type) = 1;
19994 /* For these types, all that is required is that we output a DIE (or a
19995 set of DIEs) to represent the "basis" type. */
19996 gen_type_die_with_usage (TREE_TYPE (type), context_die,
19997 DINFO_USAGE_IND_USE);
19998 break;
20000 case OFFSET_TYPE:
20001 /* This code is used for C++ pointer-to-data-member types.
20002 Output a description of the relevant class type. */
20003 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20004 DINFO_USAGE_IND_USE);
20006 /* Output a description of the type of the object pointed to. */
20007 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20008 DINFO_USAGE_IND_USE);
20010 /* Now output a DIE to represent this pointer-to-data-member type
20011 itself. */
20012 gen_ptr_to_mbr_type_die (type, context_die);
20013 break;
20015 case FUNCTION_TYPE:
20016 /* Force out return type (in case it wasn't forced out already). */
20017 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20018 DINFO_USAGE_DIR_USE);
20019 gen_subroutine_type_die (type, context_die);
20020 break;
20022 case METHOD_TYPE:
20023 /* Force out return type (in case it wasn't forced out already). */
20024 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20025 DINFO_USAGE_DIR_USE);
20026 gen_subroutine_type_die (type, context_die);
20027 break;
20029 case ARRAY_TYPE:
20030 gen_array_type_die (type, context_die);
20031 break;
20033 case VECTOR_TYPE:
20034 gen_array_type_die (type, context_die);
20035 break;
20037 case ENUMERAL_TYPE:
20038 case RECORD_TYPE:
20039 case UNION_TYPE:
20040 case QUAL_UNION_TYPE:
20041 gen_tagged_type_die (type, context_die, usage);
20042 return;
20044 case VOID_TYPE:
20045 case INTEGER_TYPE:
20046 case REAL_TYPE:
20047 case FIXED_POINT_TYPE:
20048 case COMPLEX_TYPE:
20049 case BOOLEAN_TYPE:
20050 /* No DIEs needed for fundamental types. */
20051 break;
20053 case NULLPTR_TYPE:
20054 case LANG_TYPE:
20055 /* Just use DW_TAG_unspecified_type. */
20057 dw_die_ref type_die = lookup_type_die (type);
20058 if (type_die == NULL)
20060 tree name = TYPE_IDENTIFIER (type);
20061 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20062 type);
20063 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20064 equate_type_number_to_die (type, type_die);
20067 break;
20069 default:
20070 if (is_cxx_auto (type))
20072 tree name = TYPE_IDENTIFIER (type);
20073 dw_die_ref *die = (name == get_identifier ("auto")
20074 ? &auto_die : &decltype_auto_die);
20075 if (!*die)
20077 *die = new_die (DW_TAG_unspecified_type,
20078 comp_unit_die (), NULL_TREE);
20079 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20081 equate_type_number_to_die (type, *die);
20082 break;
20084 gcc_unreachable ();
20087 TREE_ASM_WRITTEN (type) = 1;
20090 static void
20091 gen_type_die (tree type, dw_die_ref context_die)
20093 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20096 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20097 things which are local to the given block. */
20099 static void
20100 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20102 int must_output_die = 0;
20103 bool inlined_func;
20105 /* Ignore blocks that are NULL. */
20106 if (stmt == NULL_TREE)
20107 return;
20109 inlined_func = inlined_function_outer_scope_p (stmt);
20111 /* If the block is one fragment of a non-contiguous block, do not
20112 process the variables, since they will have been done by the
20113 origin block. Do process subblocks. */
20114 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20116 tree sub;
20118 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20119 gen_block_die (sub, context_die, depth + 1);
20121 return;
20124 /* Determine if we need to output any Dwarf DIEs at all to represent this
20125 block. */
20126 if (inlined_func)
20127 /* The outer scopes for inlinings *must* always be represented. We
20128 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20129 must_output_die = 1;
20130 else
20132 /* Determine if this block directly contains any "significant"
20133 local declarations which we will need to output DIEs for. */
20134 if (debug_info_level > DINFO_LEVEL_TERSE)
20135 /* We are not in terse mode so *any* local declaration counts
20136 as being a "significant" one. */
20137 must_output_die = ((BLOCK_VARS (stmt) != NULL
20138 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20139 && (TREE_USED (stmt)
20140 || TREE_ASM_WRITTEN (stmt)
20141 || BLOCK_ABSTRACT (stmt)));
20142 else if ((TREE_USED (stmt)
20143 || TREE_ASM_WRITTEN (stmt)
20144 || BLOCK_ABSTRACT (stmt))
20145 && !dwarf2out_ignore_block (stmt))
20146 must_output_die = 1;
20149 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20150 DIE for any block which contains no significant local declarations at
20151 all. Rather, in such cases we just call `decls_for_scope' so that any
20152 needed Dwarf info for any sub-blocks will get properly generated. Note
20153 that in terse mode, our definition of what constitutes a "significant"
20154 local declaration gets restricted to include only inlined function
20155 instances and local (nested) function definitions. */
20156 if (must_output_die)
20158 if (inlined_func)
20160 /* If STMT block is abstract, that means we have been called
20161 indirectly from dwarf2out_abstract_function.
20162 That function rightfully marks the descendent blocks (of
20163 the abstract function it is dealing with) as being abstract,
20164 precisely to prevent us from emitting any
20165 DW_TAG_inlined_subroutine DIE as a descendent
20166 of an abstract function instance. So in that case, we should
20167 not call gen_inlined_subroutine_die.
20169 Later though, when cgraph asks dwarf2out to emit info
20170 for the concrete instance of the function decl into which
20171 the concrete instance of STMT got inlined, the later will lead
20172 to the generation of a DW_TAG_inlined_subroutine DIE. */
20173 if (! BLOCK_ABSTRACT (stmt))
20174 gen_inlined_subroutine_die (stmt, context_die, depth);
20176 else
20177 gen_lexical_block_die (stmt, context_die, depth);
20179 else
20180 decls_for_scope (stmt, context_die, depth);
20183 /* Process variable DECL (or variable with origin ORIGIN) within
20184 block STMT and add it to CONTEXT_DIE. */
20185 static void
20186 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20188 dw_die_ref die;
20189 tree decl_or_origin = decl ? decl : origin;
20191 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20192 die = lookup_decl_die (decl_or_origin);
20193 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20194 && TYPE_DECL_IS_STUB (decl_or_origin))
20195 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20196 else
20197 die = NULL;
20199 if (die != NULL && die->die_parent == NULL)
20200 add_child_die (context_die, die);
20201 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20202 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20203 stmt, context_die);
20204 else
20205 gen_decl_die (decl, origin, context_die);
20208 /* Generate all of the decls declared within a given scope and (recursively)
20209 all of its sub-blocks. */
20211 static void
20212 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20214 tree decl;
20215 unsigned int i;
20216 tree subblocks;
20218 /* Ignore NULL blocks. */
20219 if (stmt == NULL_TREE)
20220 return;
20222 /* Output the DIEs to represent all of the data objects and typedefs
20223 declared directly within this block but not within any nested
20224 sub-blocks. Also, nested function and tag DIEs have been
20225 generated with a parent of NULL; fix that up now. We don't
20226 have to do this if we're at -g1. */
20227 if (debug_info_level > DINFO_LEVEL_TERSE)
20229 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20230 process_scope_var (stmt, decl, NULL_TREE, context_die);
20231 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20232 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20233 context_die);
20236 /* Even if we're at -g1, we need to process the subblocks in order to get
20237 inlined call information. */
20239 /* Output the DIEs to represent all sub-blocks (and the items declared
20240 therein) of this block. */
20241 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20242 subblocks != NULL;
20243 subblocks = BLOCK_CHAIN (subblocks))
20244 gen_block_die (subblocks, context_die, depth + 1);
20247 /* Is this a typedef we can avoid emitting? */
20249 static inline int
20250 is_redundant_typedef (const_tree decl)
20252 if (TYPE_DECL_IS_STUB (decl))
20253 return 1;
20255 if (DECL_ARTIFICIAL (decl)
20256 && DECL_CONTEXT (decl)
20257 && is_tagged_type (DECL_CONTEXT (decl))
20258 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20259 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20260 /* Also ignore the artificial member typedef for the class name. */
20261 return 1;
20263 return 0;
20266 /* Return TRUE if TYPE is a typedef that names a type for linkage
20267 purposes. This kind of typedefs is produced by the C++ FE for
20268 constructs like:
20270 typedef struct {...} foo;
20272 In that case, there is no typedef variant type produced for foo.
20273 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20274 struct type. */
20276 static bool
20277 is_naming_typedef_decl (const_tree decl)
20279 if (decl == NULL_TREE
20280 || TREE_CODE (decl) != TYPE_DECL
20281 || !is_tagged_type (TREE_TYPE (decl))
20282 || DECL_IS_BUILTIN (decl)
20283 || is_redundant_typedef (decl)
20284 /* It looks like Ada produces TYPE_DECLs that are very similar
20285 to C++ naming typedefs but that have different
20286 semantics. Let's be specific to c++ for now. */
20287 || !is_cxx ())
20288 return FALSE;
20290 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20291 && TYPE_NAME (TREE_TYPE (decl)) == decl
20292 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20293 != TYPE_NAME (TREE_TYPE (decl))));
20296 /* Returns the DIE for a context. */
20298 static inline dw_die_ref
20299 get_context_die (tree context)
20301 if (context)
20303 /* Find die that represents this context. */
20304 if (TYPE_P (context))
20306 context = TYPE_MAIN_VARIANT (context);
20307 return strip_naming_typedef (context, force_type_die (context));
20309 else
20310 return force_decl_die (context);
20312 return comp_unit_die ();
20315 /* Returns the DIE for decl. A DIE will always be returned. */
20317 static dw_die_ref
20318 force_decl_die (tree decl)
20320 dw_die_ref decl_die;
20321 unsigned saved_external_flag;
20322 tree save_fn = NULL_TREE;
20323 decl_die = lookup_decl_die (decl);
20324 if (!decl_die)
20326 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20328 decl_die = lookup_decl_die (decl);
20329 if (decl_die)
20330 return decl_die;
20332 switch (TREE_CODE (decl))
20334 case FUNCTION_DECL:
20335 /* Clear current_function_decl, so that gen_subprogram_die thinks
20336 that this is a declaration. At this point, we just want to force
20337 declaration die. */
20338 save_fn = current_function_decl;
20339 current_function_decl = NULL_TREE;
20340 gen_subprogram_die (decl, context_die);
20341 current_function_decl = save_fn;
20342 break;
20344 case VAR_DECL:
20345 /* Set external flag to force declaration die. Restore it after
20346 gen_decl_die() call. */
20347 saved_external_flag = DECL_EXTERNAL (decl);
20348 DECL_EXTERNAL (decl) = 1;
20349 gen_decl_die (decl, NULL, context_die);
20350 DECL_EXTERNAL (decl) = saved_external_flag;
20351 break;
20353 case NAMESPACE_DECL:
20354 if (dwarf_version >= 3 || !dwarf_strict)
20355 dwarf2out_decl (decl);
20356 else
20357 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20358 decl_die = comp_unit_die ();
20359 break;
20361 case TRANSLATION_UNIT_DECL:
20362 decl_die = comp_unit_die ();
20363 break;
20365 default:
20366 gcc_unreachable ();
20369 /* We should be able to find the DIE now. */
20370 if (!decl_die)
20371 decl_die = lookup_decl_die (decl);
20372 gcc_assert (decl_die);
20375 return decl_die;
20378 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20379 always returned. */
20381 static dw_die_ref
20382 force_type_die (tree type)
20384 dw_die_ref type_die;
20386 type_die = lookup_type_die (type);
20387 if (!type_die)
20389 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20391 type_die = modified_type_die (type, TYPE_READONLY (type),
20392 TYPE_VOLATILE (type), context_die);
20393 gcc_assert (type_die);
20395 return type_die;
20398 /* Force out any required namespaces to be able to output DECL,
20399 and return the new context_die for it, if it's changed. */
20401 static dw_die_ref
20402 setup_namespace_context (tree thing, dw_die_ref context_die)
20404 tree context = (DECL_P (thing)
20405 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20406 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20407 /* Force out the namespace. */
20408 context_die = force_decl_die (context);
20410 return context_die;
20413 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20414 type) within its namespace, if appropriate.
20416 For compatibility with older debuggers, namespace DIEs only contain
20417 declarations; all definitions are emitted at CU scope. */
20419 static dw_die_ref
20420 declare_in_namespace (tree thing, dw_die_ref context_die)
20422 dw_die_ref ns_context;
20424 if (debug_info_level <= DINFO_LEVEL_TERSE)
20425 return context_die;
20427 /* If this decl is from an inlined function, then don't try to emit it in its
20428 namespace, as we will get confused. It would have already been emitted
20429 when the abstract instance of the inline function was emitted anyways. */
20430 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20431 return context_die;
20433 ns_context = setup_namespace_context (thing, context_die);
20435 if (ns_context != context_die)
20437 if (is_fortran ())
20438 return ns_context;
20439 if (DECL_P (thing))
20440 gen_decl_die (thing, NULL, ns_context);
20441 else
20442 gen_type_die (thing, ns_context);
20444 return context_die;
20447 /* Generate a DIE for a namespace or namespace alias. */
20449 static void
20450 gen_namespace_die (tree decl, dw_die_ref context_die)
20452 dw_die_ref namespace_die;
20454 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20455 they are an alias of. */
20456 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20458 /* Output a real namespace or module. */
20459 context_die = setup_namespace_context (decl, comp_unit_die ());
20460 namespace_die = new_die (is_fortran ()
20461 ? DW_TAG_module : DW_TAG_namespace,
20462 context_die, decl);
20463 /* For Fortran modules defined in different CU don't add src coords. */
20464 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20466 const char *name = dwarf2_name (decl, 0);
20467 if (name)
20468 add_name_attribute (namespace_die, name);
20470 else
20471 add_name_and_src_coords_attributes (namespace_die, decl);
20472 if (DECL_EXTERNAL (decl))
20473 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20474 equate_decl_number_to_die (decl, namespace_die);
20476 else
20478 /* Output a namespace alias. */
20480 /* Force out the namespace we are an alias of, if necessary. */
20481 dw_die_ref origin_die
20482 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20484 if (DECL_FILE_SCOPE_P (decl)
20485 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20486 context_die = setup_namespace_context (decl, comp_unit_die ());
20487 /* Now create the namespace alias DIE. */
20488 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20489 add_name_and_src_coords_attributes (namespace_die, decl);
20490 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20491 equate_decl_number_to_die (decl, namespace_die);
20493 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20494 if (want_pubnames ())
20495 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20498 /* Generate Dwarf debug information for a decl described by DECL.
20499 The return value is currently only meaningful for PARM_DECLs,
20500 for all other decls it returns NULL. */
20502 static dw_die_ref
20503 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20505 tree decl_or_origin = decl ? decl : origin;
20506 tree class_origin = NULL, ultimate_origin;
20508 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20509 return NULL;
20511 switch (TREE_CODE (decl_or_origin))
20513 case ERROR_MARK:
20514 break;
20516 case CONST_DECL:
20517 if (!is_fortran () && !is_ada ())
20519 /* The individual enumerators of an enum type get output when we output
20520 the Dwarf representation of the relevant enum type itself. */
20521 break;
20524 /* Emit its type. */
20525 gen_type_die (TREE_TYPE (decl), context_die);
20527 /* And its containing namespace. */
20528 context_die = declare_in_namespace (decl, context_die);
20530 gen_const_die (decl, context_die);
20531 break;
20533 case FUNCTION_DECL:
20534 /* Don't output any DIEs to represent mere function declarations,
20535 unless they are class members or explicit block externs. */
20536 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20537 && DECL_FILE_SCOPE_P (decl_or_origin)
20538 && (current_function_decl == NULL_TREE
20539 || DECL_ARTIFICIAL (decl_or_origin)))
20540 break;
20542 #if 0
20543 /* FIXME */
20544 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20545 on local redeclarations of global functions. That seems broken. */
20546 if (current_function_decl != decl)
20547 /* This is only a declaration. */;
20548 #endif
20550 /* If we're emitting a clone, emit info for the abstract instance. */
20551 if (origin || DECL_ORIGIN (decl) != decl)
20552 dwarf2out_abstract_function (origin
20553 ? DECL_ORIGIN (origin)
20554 : DECL_ABSTRACT_ORIGIN (decl));
20556 /* If we're emitting an out-of-line copy of an inline function,
20557 emit info for the abstract instance and set up to refer to it. */
20558 else if (cgraph_function_possibly_inlined_p (decl)
20559 && ! DECL_ABSTRACT (decl)
20560 && ! class_or_namespace_scope_p (context_die)
20561 /* dwarf2out_abstract_function won't emit a die if this is just
20562 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20563 that case, because that works only if we have a die. */
20564 && DECL_INITIAL (decl) != NULL_TREE)
20566 dwarf2out_abstract_function (decl);
20567 set_decl_origin_self (decl);
20570 /* Otherwise we're emitting the primary DIE for this decl. */
20571 else if (debug_info_level > DINFO_LEVEL_TERSE)
20573 /* Before we describe the FUNCTION_DECL itself, make sure that we
20574 have its containing type. */
20575 if (!origin)
20576 origin = decl_class_context (decl);
20577 if (origin != NULL_TREE)
20578 gen_type_die (origin, context_die);
20580 /* And its return type. */
20581 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20583 /* And its virtual context. */
20584 if (DECL_VINDEX (decl) != NULL_TREE)
20585 gen_type_die (DECL_CONTEXT (decl), context_die);
20587 /* Make sure we have a member DIE for decl. */
20588 if (origin != NULL_TREE)
20589 gen_type_die_for_member (origin, decl, context_die);
20591 /* And its containing namespace. */
20592 context_die = declare_in_namespace (decl, context_die);
20595 /* Now output a DIE to represent the function itself. */
20596 if (decl)
20597 gen_subprogram_die (decl, context_die);
20598 break;
20600 case TYPE_DECL:
20601 /* If we are in terse mode, don't generate any DIEs to represent any
20602 actual typedefs. */
20603 if (debug_info_level <= DINFO_LEVEL_TERSE)
20604 break;
20606 /* In the special case of a TYPE_DECL node representing the declaration
20607 of some type tag, if the given TYPE_DECL is marked as having been
20608 instantiated from some other (original) TYPE_DECL node (e.g. one which
20609 was generated within the original definition of an inline function) we
20610 used to generate a special (abbreviated) DW_TAG_structure_type,
20611 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20612 should be actually referencing those DIEs, as variable DIEs with that
20613 type would be emitted already in the abstract origin, so it was always
20614 removed during unused type prunning. Don't add anything in this
20615 case. */
20616 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20617 break;
20619 if (is_redundant_typedef (decl))
20620 gen_type_die (TREE_TYPE (decl), context_die);
20621 else
20622 /* Output a DIE to represent the typedef itself. */
20623 gen_typedef_die (decl, context_die);
20624 break;
20626 case LABEL_DECL:
20627 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20628 gen_label_die (decl, context_die);
20629 break;
20631 case VAR_DECL:
20632 case RESULT_DECL:
20633 /* If we are in terse mode, don't generate any DIEs to represent any
20634 variable declarations or definitions. */
20635 if (debug_info_level <= DINFO_LEVEL_TERSE)
20636 break;
20638 /* Output any DIEs that are needed to specify the type of this data
20639 object. */
20640 if (decl_by_reference_p (decl_or_origin))
20641 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20642 else
20643 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20645 /* And its containing type. */
20646 class_origin = decl_class_context (decl_or_origin);
20647 if (class_origin != NULL_TREE)
20648 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20650 /* And its containing namespace. */
20651 context_die = declare_in_namespace (decl_or_origin, context_die);
20653 /* Now output the DIE to represent the data object itself. This gets
20654 complicated because of the possibility that the VAR_DECL really
20655 represents an inlined instance of a formal parameter for an inline
20656 function. */
20657 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20658 if (ultimate_origin != NULL_TREE
20659 && TREE_CODE (ultimate_origin) == PARM_DECL)
20660 gen_formal_parameter_die (decl, origin,
20661 true /* Emit name attribute. */,
20662 context_die);
20663 else
20664 gen_variable_die (decl, origin, context_die);
20665 break;
20667 case FIELD_DECL:
20668 /* Ignore the nameless fields that are used to skip bits but handle C++
20669 anonymous unions and structs. */
20670 if (DECL_NAME (decl) != NULL_TREE
20671 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20672 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20674 gen_type_die (member_declared_type (decl), context_die);
20675 gen_field_die (decl, context_die);
20677 break;
20679 case PARM_DECL:
20680 if (DECL_BY_REFERENCE (decl_or_origin))
20681 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20682 else
20683 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20684 return gen_formal_parameter_die (decl, origin,
20685 true /* Emit name attribute. */,
20686 context_die);
20688 case NAMESPACE_DECL:
20689 case IMPORTED_DECL:
20690 if (dwarf_version >= 3 || !dwarf_strict)
20691 gen_namespace_die (decl, context_die);
20692 break;
20694 case NAMELIST_DECL:
20695 gen_namelist_decl (DECL_NAME (decl), context_die,
20696 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20697 break;
20699 default:
20700 /* Probably some frontend-internal decl. Assume we don't care. */
20701 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20702 break;
20705 return NULL;
20708 /* Output debug information for global decl DECL. Called from toplev.c after
20709 compilation proper has finished. */
20711 static void
20712 dwarf2out_global_decl (tree decl)
20714 /* Output DWARF2 information for file-scope tentative data object
20715 declarations, file-scope (extern) function declarations (which
20716 had no corresponding body) and file-scope tagged type declarations
20717 and definitions which have not yet been forced out. */
20718 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20719 dwarf2out_decl (decl);
20722 /* Output debug information for type decl DECL. Called from toplev.c
20723 and from language front ends (to record built-in types). */
20724 static void
20725 dwarf2out_type_decl (tree decl, int local)
20727 if (!local)
20728 dwarf2out_decl (decl);
20731 /* Output debug information for imported module or decl DECL.
20732 NAME is non-NULL name in the lexical block if the decl has been renamed.
20733 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20734 that DECL belongs to.
20735 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20736 static void
20737 dwarf2out_imported_module_or_decl_1 (tree decl,
20738 tree name,
20739 tree lexical_block,
20740 dw_die_ref lexical_block_die)
20742 expanded_location xloc;
20743 dw_die_ref imported_die = NULL;
20744 dw_die_ref at_import_die;
20746 if (TREE_CODE (decl) == IMPORTED_DECL)
20748 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20749 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20750 gcc_assert (decl);
20752 else
20753 xloc = expand_location (input_location);
20755 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20757 at_import_die = force_type_die (TREE_TYPE (decl));
20758 /* For namespace N { typedef void T; } using N::T; base_type_die
20759 returns NULL, but DW_TAG_imported_declaration requires
20760 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20761 if (!at_import_die)
20763 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20764 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20765 at_import_die = lookup_type_die (TREE_TYPE (decl));
20766 gcc_assert (at_import_die);
20769 else
20771 at_import_die = lookup_decl_die (decl);
20772 if (!at_import_die)
20774 /* If we're trying to avoid duplicate debug info, we may not have
20775 emitted the member decl for this field. Emit it now. */
20776 if (TREE_CODE (decl) == FIELD_DECL)
20778 tree type = DECL_CONTEXT (decl);
20780 if (TYPE_CONTEXT (type)
20781 && TYPE_P (TYPE_CONTEXT (type))
20782 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20783 DINFO_USAGE_DIR_USE))
20784 return;
20785 gen_type_die_for_member (type, decl,
20786 get_context_die (TYPE_CONTEXT (type)));
20788 if (TREE_CODE (decl) == NAMELIST_DECL)
20789 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20790 get_context_die (DECL_CONTEXT (decl)),
20791 NULL_TREE);
20792 else
20793 at_import_die = force_decl_die (decl);
20797 if (TREE_CODE (decl) == NAMESPACE_DECL)
20799 if (dwarf_version >= 3 || !dwarf_strict)
20800 imported_die = new_die (DW_TAG_imported_module,
20801 lexical_block_die,
20802 lexical_block);
20803 else
20804 return;
20806 else
20807 imported_die = new_die (DW_TAG_imported_declaration,
20808 lexical_block_die,
20809 lexical_block);
20811 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20812 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20813 if (name)
20814 add_AT_string (imported_die, DW_AT_name,
20815 IDENTIFIER_POINTER (name));
20816 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20819 /* Output debug information for imported module or decl DECL.
20820 NAME is non-NULL name in context if the decl has been renamed.
20821 CHILD is true if decl is one of the renamed decls as part of
20822 importing whole module. */
20824 static void
20825 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20826 bool child)
20828 /* dw_die_ref at_import_die; */
20829 dw_die_ref scope_die;
20831 if (debug_info_level <= DINFO_LEVEL_TERSE)
20832 return;
20834 gcc_assert (decl);
20836 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20837 We need decl DIE for reference and scope die. First, get DIE for the decl
20838 itself. */
20840 /* Get the scope die for decl context. Use comp_unit_die for global module
20841 or decl. If die is not found for non globals, force new die. */
20842 if (context
20843 && TYPE_P (context)
20844 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20845 return;
20847 if (!(dwarf_version >= 3 || !dwarf_strict))
20848 return;
20850 scope_die = get_context_die (context);
20852 if (child)
20854 gcc_assert (scope_die->die_child);
20855 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20856 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20857 scope_die = scope_die->die_child;
20860 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20861 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20865 /* Output debug information for namelists. */
20867 static dw_die_ref
20868 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20870 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20871 tree value;
20872 unsigned i;
20874 if (debug_info_level <= DINFO_LEVEL_TERSE)
20875 return NULL;
20877 gcc_assert (scope_die != NULL);
20878 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20879 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20881 /* If there are no item_decls, we have a nondefining namelist, e.g.
20882 with USE association; hence, set DW_AT_declaration. */
20883 if (item_decls == NULL_TREE)
20885 add_AT_flag (nml_die, DW_AT_declaration, 1);
20886 return nml_die;
20889 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20891 nml_item_ref_die = lookup_decl_die (value);
20892 if (!nml_item_ref_die)
20893 nml_item_ref_die = force_decl_die (value);
20895 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20896 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20898 return nml_die;
20902 /* Write the debugging output for DECL. */
20904 void
20905 dwarf2out_decl (tree decl)
20907 dw_die_ref context_die = comp_unit_die ();
20909 switch (TREE_CODE (decl))
20911 case ERROR_MARK:
20912 return;
20914 case FUNCTION_DECL:
20915 /* What we would really like to do here is to filter out all mere
20916 file-scope declarations of file-scope functions which are never
20917 referenced later within this translation unit (and keep all of ones
20918 that *are* referenced later on) but we aren't clairvoyant, so we have
20919 no idea which functions will be referenced in the future (i.e. later
20920 on within the current translation unit). So here we just ignore all
20921 file-scope function declarations which are not also definitions. If
20922 and when the debugger needs to know something about these functions,
20923 it will have to hunt around and find the DWARF information associated
20924 with the definition of the function.
20926 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20927 nodes represent definitions and which ones represent mere
20928 declarations. We have to check DECL_INITIAL instead. That's because
20929 the C front-end supports some weird semantics for "extern inline"
20930 function definitions. These can get inlined within the current
20931 translation unit (and thus, we need to generate Dwarf info for their
20932 abstract instances so that the Dwarf info for the concrete inlined
20933 instances can have something to refer to) but the compiler never
20934 generates any out-of-lines instances of such things (despite the fact
20935 that they *are* definitions).
20937 The important point is that the C front-end marks these "extern
20938 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20939 them anyway. Note that the C++ front-end also plays some similar games
20940 for inline function definitions appearing within include files which
20941 also contain `#pragma interface' pragmas.
20943 If we are called from dwarf2out_abstract_function output a DIE
20944 anyway. We can end up here this way with early inlining and LTO
20945 where the inlined function is output in a different LTRANS unit
20946 or not at all. */
20947 if (DECL_INITIAL (decl) == NULL_TREE
20948 && ! DECL_ABSTRACT (decl))
20949 return;
20951 /* If we're a nested function, initially use a parent of NULL; if we're
20952 a plain function, this will be fixed up in decls_for_scope. If
20953 we're a method, it will be ignored, since we already have a DIE. */
20954 if (decl_function_context (decl)
20955 /* But if we're in terse mode, we don't care about scope. */
20956 && debug_info_level > DINFO_LEVEL_TERSE)
20957 context_die = NULL;
20958 break;
20960 case VAR_DECL:
20961 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20962 declaration and if the declaration was never even referenced from
20963 within this entire compilation unit. We suppress these DIEs in
20964 order to save space in the .debug section (by eliminating entries
20965 which are probably useless). Note that we must not suppress
20966 block-local extern declarations (whether used or not) because that
20967 would screw-up the debugger's name lookup mechanism and cause it to
20968 miss things which really ought to be in scope at a given point. */
20969 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20970 return;
20972 /* For local statics lookup proper context die. */
20973 if (TREE_STATIC (decl)
20974 && DECL_CONTEXT (decl)
20975 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20976 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20978 /* If we are in terse mode, don't generate any DIEs to represent any
20979 variable declarations or definitions. */
20980 if (debug_info_level <= DINFO_LEVEL_TERSE)
20981 return;
20982 break;
20984 case CONST_DECL:
20985 if (debug_info_level <= DINFO_LEVEL_TERSE)
20986 return;
20987 if (!is_fortran () && !is_ada ())
20988 return;
20989 if (TREE_STATIC (decl) && decl_function_context (decl))
20990 context_die = lookup_decl_die (DECL_CONTEXT (decl));
20991 break;
20993 case NAMESPACE_DECL:
20994 case IMPORTED_DECL:
20995 if (debug_info_level <= DINFO_LEVEL_TERSE)
20996 return;
20997 if (lookup_decl_die (decl) != NULL)
20998 return;
20999 break;
21001 case TYPE_DECL:
21002 /* Don't emit stubs for types unless they are needed by other DIEs. */
21003 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21004 return;
21006 /* Don't bother trying to generate any DIEs to represent any of the
21007 normal built-in types for the language we are compiling. */
21008 if (DECL_IS_BUILTIN (decl))
21009 return;
21011 /* If we are in terse mode, don't generate any DIEs for types. */
21012 if (debug_info_level <= DINFO_LEVEL_TERSE)
21013 return;
21015 /* If we're a function-scope tag, initially use a parent of NULL;
21016 this will be fixed up in decls_for_scope. */
21017 if (decl_function_context (decl))
21018 context_die = NULL;
21020 break;
21022 case NAMELIST_DECL:
21023 break;
21025 default:
21026 return;
21029 gen_decl_die (decl, NULL, context_die);
21032 /* Write the debugging output for DECL. */
21034 static void
21035 dwarf2out_function_decl (tree decl)
21037 dwarf2out_decl (decl);
21038 call_arg_locations = NULL;
21039 call_arg_loc_last = NULL;
21040 call_site_count = -1;
21041 tail_call_site_count = -1;
21042 block_map.release ();
21043 htab_empty (decl_loc_table);
21044 htab_empty (cached_dw_loc_list_table);
21047 /* Output a marker (i.e. a label) for the beginning of the generated code for
21048 a lexical block. */
21050 static void
21051 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21052 unsigned int blocknum)
21054 switch_to_section (current_function_section ());
21055 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21058 /* Output a marker (i.e. a label) for the end of the generated code for a
21059 lexical block. */
21061 static void
21062 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21064 switch_to_section (current_function_section ());
21065 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21068 /* Returns nonzero if it is appropriate not to emit any debugging
21069 information for BLOCK, because it doesn't contain any instructions.
21071 Don't allow this for blocks with nested functions or local classes
21072 as we would end up with orphans, and in the presence of scheduling
21073 we may end up calling them anyway. */
21075 static bool
21076 dwarf2out_ignore_block (const_tree block)
21078 tree decl;
21079 unsigned int i;
21081 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21082 if (TREE_CODE (decl) == FUNCTION_DECL
21083 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21084 return 0;
21085 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21087 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21088 if (TREE_CODE (decl) == FUNCTION_DECL
21089 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21090 return 0;
21093 return 1;
21096 /* Hash table routines for file_hash. */
21098 static int
21099 file_table_eq (const void *p1_p, const void *p2_p)
21101 const struct dwarf_file_data *const p1 =
21102 (const struct dwarf_file_data *) p1_p;
21103 const char *const p2 = (const char *) p2_p;
21104 return filename_cmp (p1->filename, p2) == 0;
21107 static hashval_t
21108 file_table_hash (const void *p_p)
21110 const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21111 return htab_hash_string (p->filename);
21114 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21115 dwarf2out.c) and return its "index". The index of each (known) filename is
21116 just a unique number which is associated with only that one filename. We
21117 need such numbers for the sake of generating labels (in the .debug_sfnames
21118 section) and references to those files numbers (in the .debug_srcinfo
21119 and.debug_macinfo sections). If the filename given as an argument is not
21120 found in our current list, add it to the list and assign it the next
21121 available unique index number. In order to speed up searches, we remember
21122 the index of the filename was looked up last. This handles the majority of
21123 all searches. */
21125 static struct dwarf_file_data *
21126 lookup_filename (const char *file_name)
21128 void ** slot;
21129 struct dwarf_file_data * created;
21131 /* Check to see if the file name that was searched on the previous
21132 call matches this file name. If so, return the index. */
21133 if (file_table_last_lookup
21134 && (file_name == file_table_last_lookup->filename
21135 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21136 return file_table_last_lookup;
21138 /* Didn't match the previous lookup, search the table. */
21139 slot = htab_find_slot_with_hash (file_table, file_name,
21140 htab_hash_string (file_name), INSERT);
21141 if (*slot)
21142 return (struct dwarf_file_data *) *slot;
21144 created = ggc_alloc<dwarf_file_data> ();
21145 created->filename = file_name;
21146 created->emitted_number = 0;
21147 *slot = created;
21148 return created;
21151 /* If the assembler will construct the file table, then translate the compiler
21152 internal file table number into the assembler file table number, and emit
21153 a .file directive if we haven't already emitted one yet. The file table
21154 numbers are different because we prune debug info for unused variables and
21155 types, which may include filenames. */
21157 static int
21158 maybe_emit_file (struct dwarf_file_data * fd)
21160 if (! fd->emitted_number)
21162 if (last_emitted_file)
21163 fd->emitted_number = last_emitted_file->emitted_number + 1;
21164 else
21165 fd->emitted_number = 1;
21166 last_emitted_file = fd;
21168 if (DWARF2_ASM_LINE_DEBUG_INFO)
21170 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21171 output_quoted_string (asm_out_file,
21172 remap_debug_filename (fd->filename));
21173 fputc ('\n', asm_out_file);
21177 return fd->emitted_number;
21180 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21181 That generation should happen after function debug info has been
21182 generated. The value of the attribute is the constant value of ARG. */
21184 static void
21185 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21187 die_arg_entry entry;
21189 if (!die || !arg)
21190 return;
21192 if (!tmpl_value_parm_die_table)
21193 vec_alloc (tmpl_value_parm_die_table, 32);
21195 entry.die = die;
21196 entry.arg = arg;
21197 vec_safe_push (tmpl_value_parm_die_table, entry);
21200 /* Return TRUE if T is an instance of generic type, FALSE
21201 otherwise. */
21203 static bool
21204 generic_type_p (tree t)
21206 if (t == NULL_TREE || !TYPE_P (t))
21207 return false;
21208 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21211 /* Schedule the generation of the generic parameter dies for the
21212 instance of generic type T. The proper generation itself is later
21213 done by gen_scheduled_generic_parms_dies. */
21215 static void
21216 schedule_generic_params_dies_gen (tree t)
21218 if (!generic_type_p (t))
21219 return;
21221 if (!generic_type_instances)
21222 vec_alloc (generic_type_instances, 256);
21224 vec_safe_push (generic_type_instances, t);
21227 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21228 by append_entry_to_tmpl_value_parm_die_table. This function must
21229 be called after function DIEs have been generated. */
21231 static void
21232 gen_remaining_tmpl_value_param_die_attribute (void)
21234 if (tmpl_value_parm_die_table)
21236 unsigned i;
21237 die_arg_entry *e;
21239 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21240 tree_add_const_value_attribute (e->die, e->arg);
21244 /* Generate generic parameters DIEs for instances of generic types
21245 that have been previously scheduled by
21246 schedule_generic_params_dies_gen. This function must be called
21247 after all the types of the CU have been laid out. */
21249 static void
21250 gen_scheduled_generic_parms_dies (void)
21252 unsigned i;
21253 tree t;
21255 if (!generic_type_instances)
21256 return;
21258 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21259 if (COMPLETE_TYPE_P (t))
21260 gen_generic_params_dies (t);
21264 /* Replace DW_AT_name for the decl with name. */
21266 static void
21267 dwarf2out_set_name (tree decl, tree name)
21269 dw_die_ref die;
21270 dw_attr_ref attr;
21271 const char *dname;
21273 die = TYPE_SYMTAB_DIE (decl);
21274 if (!die)
21275 return;
21277 dname = dwarf2_name (name, 0);
21278 if (!dname)
21279 return;
21281 attr = get_AT (die, DW_AT_name);
21282 if (attr)
21284 struct indirect_string_node *node;
21286 node = find_AT_string (dname);
21287 /* replace the string. */
21288 attr->dw_attr_val.v.val_str = node;
21291 else
21292 add_name_attribute (die, dname);
21295 /* True if before or during processing of the first function being emitted. */
21296 static bool in_first_function_p = true;
21297 /* True if loc_note during dwarf2out_var_location call might still be
21298 before first real instruction at address equal to .Ltext0. */
21299 static bool maybe_at_text_label_p = true;
21300 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21301 static unsigned int first_loclabel_num_not_at_text_label;
21303 /* Called by the final INSN scan whenever we see a var location. We
21304 use it to drop labels in the right places, and throw the location in
21305 our lookup table. */
21307 static void
21308 dwarf2out_var_location (rtx loc_note)
21310 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21311 struct var_loc_node *newloc;
21312 rtx next_real, next_note;
21313 static const char *last_label;
21314 static const char *last_postcall_label;
21315 static bool last_in_cold_section_p;
21316 static rtx expected_next_loc_note;
21317 tree decl;
21318 bool var_loc_p;
21320 if (!NOTE_P (loc_note))
21322 if (CALL_P (loc_note))
21324 call_site_count++;
21325 if (SIBLING_CALL_P (loc_note))
21326 tail_call_site_count++;
21328 return;
21331 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21332 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21333 return;
21335 /* Optimize processing a large consecutive sequence of location
21336 notes so we don't spend too much time in next_real_insn. If the
21337 next insn is another location note, remember the next_real_insn
21338 calculation for next time. */
21339 next_real = cached_next_real_insn;
21340 if (next_real)
21342 if (expected_next_loc_note != loc_note)
21343 next_real = NULL_RTX;
21346 next_note = NEXT_INSN (loc_note);
21347 if (! next_note
21348 || INSN_DELETED_P (next_note)
21349 || ! NOTE_P (next_note)
21350 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21351 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21352 next_note = NULL_RTX;
21354 if (! next_real)
21355 next_real = next_real_insn (loc_note);
21357 if (next_note)
21359 expected_next_loc_note = next_note;
21360 cached_next_real_insn = next_real;
21362 else
21363 cached_next_real_insn = NULL_RTX;
21365 /* If there are no instructions which would be affected by this note,
21366 don't do anything. */
21367 if (var_loc_p
21368 && next_real == NULL_RTX
21369 && !NOTE_DURING_CALL_P (loc_note))
21370 return;
21372 if (next_real == NULL_RTX)
21373 next_real = get_last_insn ();
21375 /* If there were any real insns between note we processed last time
21376 and this note (or if it is the first note), clear
21377 last_{,postcall_}label so that they are not reused this time. */
21378 if (last_var_location_insn == NULL_RTX
21379 || last_var_location_insn != next_real
21380 || last_in_cold_section_p != in_cold_section_p)
21382 last_label = NULL;
21383 last_postcall_label = NULL;
21386 if (var_loc_p)
21388 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21389 newloc = add_var_loc_to_decl (decl, loc_note,
21390 NOTE_DURING_CALL_P (loc_note)
21391 ? last_postcall_label : last_label);
21392 if (newloc == NULL)
21393 return;
21395 else
21397 decl = NULL_TREE;
21398 newloc = NULL;
21401 /* If there were no real insns between note we processed last time
21402 and this note, use the label we emitted last time. Otherwise
21403 create a new label and emit it. */
21404 if (last_label == NULL)
21406 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21407 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21408 loclabel_num++;
21409 last_label = ggc_strdup (loclabel);
21410 /* See if loclabel might be equal to .Ltext0. If yes,
21411 bump first_loclabel_num_not_at_text_label. */
21412 if (!have_multiple_function_sections
21413 && in_first_function_p
21414 && maybe_at_text_label_p)
21416 static rtx last_start;
21417 rtx insn;
21418 for (insn = loc_note; insn; insn = previous_insn (insn))
21419 if (insn == last_start)
21420 break;
21421 else if (!NONDEBUG_INSN_P (insn))
21422 continue;
21423 else
21425 rtx body = PATTERN (insn);
21426 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21427 continue;
21428 /* Inline asm could occupy zero bytes. */
21429 else if (GET_CODE (body) == ASM_INPUT
21430 || asm_noperands (body) >= 0)
21431 continue;
21432 #ifdef HAVE_attr_length
21433 else if (get_attr_min_length (insn) == 0)
21434 continue;
21435 #endif
21436 else
21438 /* Assume insn has non-zero length. */
21439 maybe_at_text_label_p = false;
21440 break;
21443 if (maybe_at_text_label_p)
21445 last_start = loc_note;
21446 first_loclabel_num_not_at_text_label = loclabel_num;
21451 if (!var_loc_p)
21453 struct call_arg_loc_node *ca_loc
21454 = ggc_cleared_alloc<call_arg_loc_node> ();
21455 rtx prev = prev_real_insn (loc_note), x;
21456 ca_loc->call_arg_loc_note = loc_note;
21457 ca_loc->next = NULL;
21458 ca_loc->label = last_label;
21459 gcc_assert (prev
21460 && (CALL_P (prev)
21461 || (NONJUMP_INSN_P (prev)
21462 && GET_CODE (PATTERN (prev)) == SEQUENCE
21463 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21464 if (!CALL_P (prev))
21465 prev = XVECEXP (PATTERN (prev), 0, 0);
21466 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21467 x = get_call_rtx_from (PATTERN (prev));
21468 if (x)
21470 x = XEXP (XEXP (x, 0), 0);
21471 if (GET_CODE (x) == SYMBOL_REF
21472 && SYMBOL_REF_DECL (x)
21473 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21474 ca_loc->symbol_ref = x;
21476 ca_loc->block = insn_scope (prev);
21477 if (call_arg_locations)
21478 call_arg_loc_last->next = ca_loc;
21479 else
21480 call_arg_locations = ca_loc;
21481 call_arg_loc_last = ca_loc;
21483 else if (!NOTE_DURING_CALL_P (loc_note))
21484 newloc->label = last_label;
21485 else
21487 if (!last_postcall_label)
21489 sprintf (loclabel, "%s-1", last_label);
21490 last_postcall_label = ggc_strdup (loclabel);
21492 newloc->label = last_postcall_label;
21495 last_var_location_insn = next_real;
21496 last_in_cold_section_p = in_cold_section_p;
21499 /* Note in one location list that text section has changed. */
21501 static int
21502 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21504 var_loc_list *list = (var_loc_list *) *slot;
21505 if (list->first)
21506 list->last_before_switch
21507 = list->last->next ? list->last->next : list->last;
21508 return 1;
21511 /* Note in all location lists that text section has changed. */
21513 static void
21514 var_location_switch_text_section (void)
21516 if (decl_loc_table == NULL)
21517 return;
21519 htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
21522 /* Create a new line number table. */
21524 static dw_line_info_table *
21525 new_line_info_table (void)
21527 dw_line_info_table *table;
21529 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21530 table->file_num = 1;
21531 table->line_num = 1;
21532 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21534 return table;
21537 /* Lookup the "current" table into which we emit line info, so
21538 that we don't have to do it for every source line. */
21540 static void
21541 set_cur_line_info_table (section *sec)
21543 dw_line_info_table *table;
21545 if (sec == text_section)
21546 table = text_section_line_info;
21547 else if (sec == cold_text_section)
21549 table = cold_text_section_line_info;
21550 if (!table)
21552 cold_text_section_line_info = table = new_line_info_table ();
21553 table->end_label = cold_end_label;
21556 else
21558 const char *end_label;
21560 if (flag_reorder_blocks_and_partition)
21562 if (in_cold_section_p)
21563 end_label = crtl->subsections.cold_section_end_label;
21564 else
21565 end_label = crtl->subsections.hot_section_end_label;
21567 else
21569 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21570 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21571 current_function_funcdef_no);
21572 end_label = ggc_strdup (label);
21575 table = new_line_info_table ();
21576 table->end_label = end_label;
21578 vec_safe_push (separate_line_info, table);
21581 if (DWARF2_ASM_LINE_DEBUG_INFO)
21582 table->is_stmt = (cur_line_info_table
21583 ? cur_line_info_table->is_stmt
21584 : DWARF_LINE_DEFAULT_IS_STMT_START);
21585 cur_line_info_table = table;
21589 /* We need to reset the locations at the beginning of each
21590 function. We can't do this in the end_function hook, because the
21591 declarations that use the locations won't have been output when
21592 that hook is called. Also compute have_multiple_function_sections here. */
21594 static void
21595 dwarf2out_begin_function (tree fun)
21597 section *sec = function_section (fun);
21599 if (sec != text_section)
21600 have_multiple_function_sections = true;
21602 if (flag_reorder_blocks_and_partition && !cold_text_section)
21604 gcc_assert (current_function_decl == fun);
21605 cold_text_section = unlikely_text_section ();
21606 switch_to_section (cold_text_section);
21607 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21608 switch_to_section (sec);
21611 dwarf2out_note_section_used ();
21612 call_site_count = 0;
21613 tail_call_site_count = 0;
21615 set_cur_line_info_table (sec);
21618 /* Helper function of dwarf2out_end_function, called only after emitting
21619 the very first function into assembly. Check if some .debug_loc range
21620 might end with a .LVL* label that could be equal to .Ltext0.
21621 In that case we must force using absolute addresses in .debug_loc ranges,
21622 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21623 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21624 list terminator.
21625 Set have_multiple_function_sections to true in that case and
21626 terminate htab traversal. */
21628 static int
21629 find_empty_loc_ranges_at_text_label (void **slot, void *)
21631 var_loc_list *entry;
21632 struct var_loc_node *node;
21634 entry = (var_loc_list *) *slot;
21635 node = entry->first;
21636 if (node && node->next && node->next->label)
21638 unsigned int i;
21639 const char *label = node->next->label;
21640 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21642 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21644 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21645 if (strcmp (label, loclabel) == 0)
21647 have_multiple_function_sections = true;
21648 return 0;
21652 return 1;
21655 /* Hook called after emitting a function into assembly.
21656 This does something only for the very first function emitted. */
21658 static void
21659 dwarf2out_end_function (unsigned int)
21661 if (in_first_function_p
21662 && !have_multiple_function_sections
21663 && first_loclabel_num_not_at_text_label
21664 && decl_loc_table)
21665 htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21666 NULL);
21667 in_first_function_p = false;
21668 maybe_at_text_label_p = false;
21671 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21673 static void
21674 push_dw_line_info_entry (dw_line_info_table *table,
21675 enum dw_line_info_opcode opcode, unsigned int val)
21677 dw_line_info_entry e;
21678 e.opcode = opcode;
21679 e.val = val;
21680 vec_safe_push (table->entries, e);
21683 /* Output a label to mark the beginning of a source code line entry
21684 and record information relating to this source line, in
21685 'line_info_table' for later output of the .debug_line section. */
21686 /* ??? The discriminator parameter ought to be unsigned. */
21688 static void
21689 dwarf2out_source_line (unsigned int line, const char *filename,
21690 int discriminator, bool is_stmt)
21692 unsigned int file_num;
21693 dw_line_info_table *table;
21695 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21696 return;
21698 /* The discriminator column was added in dwarf4. Simplify the below
21699 by simply removing it if we're not supposed to output it. */
21700 if (dwarf_version < 4 && dwarf_strict)
21701 discriminator = 0;
21703 table = cur_line_info_table;
21704 file_num = maybe_emit_file (lookup_filename (filename));
21706 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21707 the debugger has used the second (possibly duplicate) line number
21708 at the beginning of the function to mark the end of the prologue.
21709 We could eliminate any other duplicates within the function. For
21710 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21711 that second line number entry. */
21712 /* Recall that this end-of-prologue indication is *not* the same thing
21713 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21714 to which the hook corresponds, follows the last insn that was
21715 emitted by gen_prologue. What we need is to precede the first insn
21716 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21717 insn that corresponds to something the user wrote. These may be
21718 very different locations once scheduling is enabled. */
21720 if (0 && file_num == table->file_num
21721 && line == table->line_num
21722 && discriminator == table->discrim_num
21723 && is_stmt == table->is_stmt)
21724 return;
21726 switch_to_section (current_function_section ());
21728 /* If requested, emit something human-readable. */
21729 if (flag_debug_asm)
21730 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21732 if (DWARF2_ASM_LINE_DEBUG_INFO)
21734 /* Emit the .loc directive understood by GNU as. */
21735 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21736 file_num, line, is_stmt, discriminator */
21737 fputs ("\t.loc ", asm_out_file);
21738 fprint_ul (asm_out_file, file_num);
21739 putc (' ', asm_out_file);
21740 fprint_ul (asm_out_file, line);
21741 putc (' ', asm_out_file);
21742 putc ('0', asm_out_file);
21744 if (is_stmt != table->is_stmt)
21746 fputs (" is_stmt ", asm_out_file);
21747 putc (is_stmt ? '1' : '0', asm_out_file);
21749 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21751 gcc_assert (discriminator > 0);
21752 fputs (" discriminator ", asm_out_file);
21753 fprint_ul (asm_out_file, (unsigned long) discriminator);
21755 putc ('\n', asm_out_file);
21757 else
21759 unsigned int label_num = ++line_info_label_num;
21761 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21763 push_dw_line_info_entry (table, LI_set_address, label_num);
21764 if (file_num != table->file_num)
21765 push_dw_line_info_entry (table, LI_set_file, file_num);
21766 if (discriminator != table->discrim_num)
21767 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21768 if (is_stmt != table->is_stmt)
21769 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21770 push_dw_line_info_entry (table, LI_set_line, line);
21773 table->file_num = file_num;
21774 table->line_num = line;
21775 table->discrim_num = discriminator;
21776 table->is_stmt = is_stmt;
21777 table->in_use = true;
21780 /* Record the beginning of a new source file. */
21782 static void
21783 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21785 if (flag_eliminate_dwarf2_dups)
21787 /* Record the beginning of the file for break_out_includes. */
21788 dw_die_ref bincl_die;
21790 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21791 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21794 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21796 macinfo_entry e;
21797 e.code = DW_MACINFO_start_file;
21798 e.lineno = lineno;
21799 e.info = ggc_strdup (filename);
21800 vec_safe_push (macinfo_table, e);
21804 /* Record the end of a source file. */
21806 static void
21807 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21809 if (flag_eliminate_dwarf2_dups)
21810 /* Record the end of the file for break_out_includes. */
21811 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21813 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21815 macinfo_entry e;
21816 e.code = DW_MACINFO_end_file;
21817 e.lineno = lineno;
21818 e.info = NULL;
21819 vec_safe_push (macinfo_table, e);
21823 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21824 the tail part of the directive line, i.e. the part which is past the
21825 initial whitespace, #, whitespace, directive-name, whitespace part. */
21827 static void
21828 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21829 const char *buffer ATTRIBUTE_UNUSED)
21831 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21833 macinfo_entry e;
21834 /* Insert a dummy first entry to be able to optimize the whole
21835 predefined macro block using DW_MACRO_GNU_transparent_include. */
21836 if (macinfo_table->is_empty () && lineno <= 1)
21838 e.code = 0;
21839 e.lineno = 0;
21840 e.info = NULL;
21841 vec_safe_push (macinfo_table, e);
21843 e.code = DW_MACINFO_define;
21844 e.lineno = lineno;
21845 e.info = ggc_strdup (buffer);
21846 vec_safe_push (macinfo_table, e);
21850 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21851 the tail part of the directive line, i.e. the part which is past the
21852 initial whitespace, #, whitespace, directive-name, whitespace part. */
21854 static void
21855 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21856 const char *buffer ATTRIBUTE_UNUSED)
21858 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21860 macinfo_entry e;
21861 /* Insert a dummy first entry to be able to optimize the whole
21862 predefined macro block using DW_MACRO_GNU_transparent_include. */
21863 if (macinfo_table->is_empty () && lineno <= 1)
21865 e.code = 0;
21866 e.lineno = 0;
21867 e.info = NULL;
21868 vec_safe_push (macinfo_table, e);
21870 e.code = DW_MACINFO_undef;
21871 e.lineno = lineno;
21872 e.info = ggc_strdup (buffer);
21873 vec_safe_push (macinfo_table, e);
21877 /* Helpers to manipulate hash table of CUs. */
21879 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21881 typedef macinfo_entry value_type;
21882 typedef macinfo_entry compare_type;
21883 static inline hashval_t hash (const value_type *);
21884 static inline bool equal (const value_type *, const compare_type *);
21887 inline hashval_t
21888 macinfo_entry_hasher::hash (const value_type *entry)
21890 return htab_hash_string (entry->info);
21893 inline bool
21894 macinfo_entry_hasher::equal (const value_type *entry1,
21895 const compare_type *entry2)
21897 return !strcmp (entry1->info, entry2->info);
21900 typedef hash_table <macinfo_entry_hasher> macinfo_hash_type;
21902 /* Output a single .debug_macinfo entry. */
21904 static void
21905 output_macinfo_op (macinfo_entry *ref)
21907 int file_num;
21908 size_t len;
21909 struct indirect_string_node *node;
21910 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21911 struct dwarf_file_data *fd;
21913 switch (ref->code)
21915 case DW_MACINFO_start_file:
21916 fd = lookup_filename (ref->info);
21917 file_num = maybe_emit_file (fd);
21918 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21919 dw2_asm_output_data_uleb128 (ref->lineno,
21920 "Included from line number %lu",
21921 (unsigned long) ref->lineno);
21922 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21923 break;
21924 case DW_MACINFO_end_file:
21925 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21926 break;
21927 case DW_MACINFO_define:
21928 case DW_MACINFO_undef:
21929 len = strlen (ref->info) + 1;
21930 if (!dwarf_strict
21931 && len > DWARF_OFFSET_SIZE
21932 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21933 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21935 ref->code = ref->code == DW_MACINFO_define
21936 ? DW_MACRO_GNU_define_indirect
21937 : DW_MACRO_GNU_undef_indirect;
21938 output_macinfo_op (ref);
21939 return;
21941 dw2_asm_output_data (1, ref->code,
21942 ref->code == DW_MACINFO_define
21943 ? "Define macro" : "Undefine macro");
21944 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21945 (unsigned long) ref->lineno);
21946 dw2_asm_output_nstring (ref->info, -1, "The macro");
21947 break;
21948 case DW_MACRO_GNU_define_indirect:
21949 case DW_MACRO_GNU_undef_indirect:
21950 node = find_AT_string (ref->info);
21951 gcc_assert (node
21952 && ((node->form == DW_FORM_strp)
21953 || (node->form == DW_FORM_GNU_str_index)));
21954 dw2_asm_output_data (1, ref->code,
21955 ref->code == DW_MACRO_GNU_define_indirect
21956 ? "Define macro indirect"
21957 : "Undefine macro indirect");
21958 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21959 (unsigned long) ref->lineno);
21960 if (node->form == DW_FORM_strp)
21961 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21962 debug_str_section, "The macro: \"%s\"",
21963 ref->info);
21964 else
21965 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21966 ref->info);
21967 break;
21968 case DW_MACRO_GNU_transparent_include:
21969 dw2_asm_output_data (1, ref->code, "Transparent include");
21970 ASM_GENERATE_INTERNAL_LABEL (label,
21971 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21972 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21973 break;
21974 default:
21975 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21976 ASM_COMMENT_START, (unsigned long) ref->code);
21977 break;
21981 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21982 other compilation unit .debug_macinfo sections. IDX is the first
21983 index of a define/undef, return the number of ops that should be
21984 emitted in a comdat .debug_macinfo section and emit
21985 a DW_MACRO_GNU_transparent_include entry referencing it.
21986 If the define/undef entry should be emitted normally, return 0. */
21988 static unsigned
21989 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21990 macinfo_hash_type *macinfo_htab)
21992 macinfo_entry *first, *second, *cur, *inc;
21993 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21994 unsigned char checksum[16];
21995 struct md5_ctx ctx;
21996 char *grp_name, *tail;
21997 const char *base;
21998 unsigned int i, count, encoded_filename_len, linebuf_len;
21999 macinfo_entry **slot;
22001 first = &(*macinfo_table)[idx];
22002 second = &(*macinfo_table)[idx + 1];
22004 /* Optimize only if there are at least two consecutive define/undef ops,
22005 and either all of them are before first DW_MACINFO_start_file
22006 with lineno {0,1} (i.e. predefined macro block), or all of them are
22007 in some included header file. */
22008 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22009 return 0;
22010 if (vec_safe_is_empty (files))
22012 if (first->lineno > 1 || second->lineno > 1)
22013 return 0;
22015 else if (first->lineno == 0)
22016 return 0;
22018 /* Find the last define/undef entry that can be grouped together
22019 with first and at the same time compute md5 checksum of their
22020 codes, linenumbers and strings. */
22021 md5_init_ctx (&ctx);
22022 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22023 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22024 break;
22025 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22026 break;
22027 else
22029 unsigned char code = cur->code;
22030 md5_process_bytes (&code, 1, &ctx);
22031 checksum_uleb128 (cur->lineno, &ctx);
22032 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22034 md5_finish_ctx (&ctx, checksum);
22035 count = i - idx;
22037 /* From the containing include filename (if any) pick up just
22038 usable characters from its basename. */
22039 if (vec_safe_is_empty (files))
22040 base = "";
22041 else
22042 base = lbasename (files->last ().info);
22043 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22044 if (ISIDNUM (base[i]) || base[i] == '.')
22045 encoded_filename_len++;
22046 /* Count . at the end. */
22047 if (encoded_filename_len)
22048 encoded_filename_len++;
22050 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22051 linebuf_len = strlen (linebuf);
22053 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22054 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22055 + 16 * 2 + 1);
22056 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22057 tail = grp_name + 4;
22058 if (encoded_filename_len)
22060 for (i = 0; base[i]; i++)
22061 if (ISIDNUM (base[i]) || base[i] == '.')
22062 *tail++ = base[i];
22063 *tail++ = '.';
22065 memcpy (tail, linebuf, linebuf_len);
22066 tail += linebuf_len;
22067 *tail++ = '.';
22068 for (i = 0; i < 16; i++)
22069 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22071 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22072 in the empty vector entry before the first define/undef. */
22073 inc = &(*macinfo_table)[idx - 1];
22074 inc->code = DW_MACRO_GNU_transparent_include;
22075 inc->lineno = 0;
22076 inc->info = ggc_strdup (grp_name);
22077 if (!macinfo_htab->is_created ())
22078 macinfo_htab->create (10);
22079 /* Avoid emitting duplicates. */
22080 slot = macinfo_htab->find_slot (inc, INSERT);
22081 if (*slot != NULL)
22083 inc->code = 0;
22084 inc->info = NULL;
22085 /* If such an entry has been used before, just emit
22086 a DW_MACRO_GNU_transparent_include op. */
22087 inc = *slot;
22088 output_macinfo_op (inc);
22089 /* And clear all macinfo_entry in the range to avoid emitting them
22090 in the second pass. */
22091 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22093 cur->code = 0;
22094 cur->info = NULL;
22097 else
22099 *slot = inc;
22100 inc->lineno = macinfo_htab->elements ();
22101 output_macinfo_op (inc);
22103 return count;
22106 /* Save any strings needed by the macinfo table in the debug str
22107 table. All strings must be collected into the table by the time
22108 index_string is called. */
22110 static void
22111 save_macinfo_strings (void)
22113 unsigned len;
22114 unsigned i;
22115 macinfo_entry *ref;
22117 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22119 switch (ref->code)
22121 /* Match the logic in output_macinfo_op to decide on
22122 indirect strings. */
22123 case DW_MACINFO_define:
22124 case DW_MACINFO_undef:
22125 len = strlen (ref->info) + 1;
22126 if (!dwarf_strict
22127 && len > DWARF_OFFSET_SIZE
22128 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22129 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22130 set_indirect_string (find_AT_string (ref->info));
22131 break;
22132 case DW_MACRO_GNU_define_indirect:
22133 case DW_MACRO_GNU_undef_indirect:
22134 set_indirect_string (find_AT_string (ref->info));
22135 break;
22136 default:
22137 break;
22142 /* Output macinfo section(s). */
22144 static void
22145 output_macinfo (void)
22147 unsigned i;
22148 unsigned long length = vec_safe_length (macinfo_table);
22149 macinfo_entry *ref;
22150 vec<macinfo_entry, va_gc> *files = NULL;
22151 macinfo_hash_type macinfo_htab;
22153 if (! length)
22154 return;
22156 /* output_macinfo* uses these interchangeably. */
22157 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22158 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22159 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22160 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22162 /* For .debug_macro emit the section header. */
22163 if (!dwarf_strict)
22165 dw2_asm_output_data (2, 4, "DWARF macro version number");
22166 if (DWARF_OFFSET_SIZE == 8)
22167 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22168 else
22169 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22170 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22171 (!dwarf_split_debug_info ? debug_line_section_label
22172 : debug_skeleton_line_section_label),
22173 debug_line_section, NULL);
22176 /* In the first loop, it emits the primary .debug_macinfo section
22177 and after each emitted op the macinfo_entry is cleared.
22178 If a longer range of define/undef ops can be optimized using
22179 DW_MACRO_GNU_transparent_include, the
22180 DW_MACRO_GNU_transparent_include op is emitted and kept in
22181 the vector before the first define/undef in the range and the
22182 whole range of define/undef ops is not emitted and kept. */
22183 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22185 switch (ref->code)
22187 case DW_MACINFO_start_file:
22188 vec_safe_push (files, *ref);
22189 break;
22190 case DW_MACINFO_end_file:
22191 if (!vec_safe_is_empty (files))
22192 files->pop ();
22193 break;
22194 case DW_MACINFO_define:
22195 case DW_MACINFO_undef:
22196 if (!dwarf_strict
22197 && HAVE_COMDAT_GROUP
22198 && vec_safe_length (files) != 1
22199 && i > 0
22200 && i + 1 < length
22201 && (*macinfo_table)[i - 1].code == 0)
22203 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22204 if (count)
22206 i += count - 1;
22207 continue;
22210 break;
22211 case 0:
22212 /* A dummy entry may be inserted at the beginning to be able
22213 to optimize the whole block of predefined macros. */
22214 if (i == 0)
22215 continue;
22216 default:
22217 break;
22219 output_macinfo_op (ref);
22220 ref->info = NULL;
22221 ref->code = 0;
22224 if (!macinfo_htab.is_created ())
22225 return;
22227 macinfo_htab.dispose ();
22229 /* If any DW_MACRO_GNU_transparent_include were used, on those
22230 DW_MACRO_GNU_transparent_include entries terminate the
22231 current chain and switch to a new comdat .debug_macinfo
22232 section and emit the define/undef entries within it. */
22233 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22234 switch (ref->code)
22236 case 0:
22237 continue;
22238 case DW_MACRO_GNU_transparent_include:
22240 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22241 tree comdat_key = get_identifier (ref->info);
22242 /* Terminate the previous .debug_macinfo section. */
22243 dw2_asm_output_data (1, 0, "End compilation unit");
22244 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22245 SECTION_DEBUG
22246 | SECTION_LINKONCE,
22247 comdat_key);
22248 ASM_GENERATE_INTERNAL_LABEL (label,
22249 DEBUG_MACRO_SECTION_LABEL,
22250 ref->lineno);
22251 ASM_OUTPUT_LABEL (asm_out_file, label);
22252 ref->code = 0;
22253 ref->info = NULL;
22254 dw2_asm_output_data (2, 4, "DWARF macro version number");
22255 if (DWARF_OFFSET_SIZE == 8)
22256 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22257 else
22258 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22260 break;
22261 case DW_MACINFO_define:
22262 case DW_MACINFO_undef:
22263 output_macinfo_op (ref);
22264 ref->code = 0;
22265 ref->info = NULL;
22266 break;
22267 default:
22268 gcc_unreachable ();
22272 /* Set up for Dwarf output at the start of compilation. */
22274 static void
22275 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22277 /* Allocate the file_table. */
22278 file_table = htab_create_ggc (50, file_table_hash,
22279 file_table_eq, NULL);
22281 /* Allocate the decl_die_table. */
22282 decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22283 decl_die_table_eq, NULL);
22285 /* Allocate the decl_loc_table. */
22286 decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22287 decl_loc_table_eq, NULL);
22289 /* Allocate the cached_dw_loc_list_table. */
22290 cached_dw_loc_list_table
22291 = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22292 cached_dw_loc_list_table_eq, NULL);
22294 /* Allocate the initial hunk of the decl_scope_table. */
22295 vec_alloc (decl_scope_table, 256);
22297 /* Allocate the initial hunk of the abbrev_die_table. */
22298 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22299 (ABBREV_DIE_TABLE_INCREMENT);
22300 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22301 /* Zero-th entry is allocated, but unused. */
22302 abbrev_die_table_in_use = 1;
22304 /* Allocate the pubtypes and pubnames vectors. */
22305 vec_alloc (pubname_table, 32);
22306 vec_alloc (pubtype_table, 32);
22308 vec_alloc (incomplete_types, 64);
22310 vec_alloc (used_rtx_array, 32);
22312 if (!dwarf_split_debug_info)
22314 debug_info_section = get_section (DEBUG_INFO_SECTION,
22315 SECTION_DEBUG, NULL);
22316 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22317 SECTION_DEBUG, NULL);
22318 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22319 SECTION_DEBUG, NULL);
22321 else
22323 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22324 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22325 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22326 SECTION_DEBUG | SECTION_EXCLUDE,
22327 NULL);
22328 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22329 SECTION_DEBUG, NULL);
22330 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22331 SECTION_DEBUG, NULL);
22332 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22333 SECTION_DEBUG, NULL);
22334 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22335 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22337 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22338 the main .o, but the skeleton_line goes into the split off dwo. */
22339 debug_skeleton_line_section
22340 = get_section (DEBUG_DWO_LINE_SECTION,
22341 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22342 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22343 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22344 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22345 SECTION_DEBUG | SECTION_EXCLUDE,
22346 NULL);
22347 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22348 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22349 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22350 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22351 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22352 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22354 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22355 SECTION_DEBUG, NULL);
22356 debug_macinfo_section = get_section (dwarf_strict
22357 ? DEBUG_MACINFO_SECTION
22358 : DEBUG_MACRO_SECTION,
22359 DEBUG_MACRO_SECTION_FLAGS, NULL);
22360 debug_line_section = get_section (DEBUG_LINE_SECTION,
22361 SECTION_DEBUG, NULL);
22362 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22363 SECTION_DEBUG, NULL);
22364 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22365 SECTION_DEBUG, NULL);
22366 debug_str_section = get_section (DEBUG_STR_SECTION,
22367 DEBUG_STR_SECTION_FLAGS, NULL);
22368 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22369 SECTION_DEBUG, NULL);
22370 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22371 SECTION_DEBUG, NULL);
22373 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22374 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22375 DEBUG_ABBREV_SECTION_LABEL, 0);
22376 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22377 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22378 COLD_TEXT_SECTION_LABEL, 0);
22379 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22381 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22382 DEBUG_INFO_SECTION_LABEL, 0);
22383 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22384 DEBUG_LINE_SECTION_LABEL, 0);
22385 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22386 DEBUG_RANGES_SECTION_LABEL, 0);
22387 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22388 DEBUG_ADDR_SECTION_LABEL, 0);
22389 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22390 dwarf_strict
22391 ? DEBUG_MACINFO_SECTION_LABEL
22392 : DEBUG_MACRO_SECTION_LABEL, 0);
22393 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22395 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22396 vec_alloc (macinfo_table, 64);
22398 switch_to_section (text_section);
22399 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22401 /* Make sure the line number table for .text always exists. */
22402 text_section_line_info = new_line_info_table ();
22403 text_section_line_info->end_label = text_end_label;
22406 /* Called before compile () starts outputtting functions, variables
22407 and toplevel asms into assembly. */
22409 static void
22410 dwarf2out_assembly_start (void)
22412 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22413 && dwarf2out_do_cfi_asm ()
22414 && (!(flag_unwind_tables || flag_exceptions)
22415 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22416 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22419 /* A helper function for dwarf2out_finish called through
22420 htab_traverse. Assign a string its index. All strings must be
22421 collected into the table by the time index_string is called,
22422 because the indexing code relies on htab_traverse to traverse nodes
22423 in the same order for each run. */
22425 static int
22426 index_string (void **h, void *v)
22428 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22429 unsigned int *index = (unsigned int *) v;
22431 find_string_form (node);
22432 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22434 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22435 node->index = *index;
22436 *index += 1;
22438 return 1;
22441 /* A helper function for output_indirect_strings called through
22442 htab_traverse. Output the offset to a string and update the
22443 current offset. */
22445 static int
22446 output_index_string_offset (void **h, void *v)
22448 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22449 unsigned int *offset = (unsigned int *) v;
22451 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22453 /* Assert that this node has been assigned an index. */
22454 gcc_assert (node->index != NO_INDEX_ASSIGNED
22455 && node->index != NOT_INDEXED);
22456 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22457 "indexed string 0x%x: %s", node->index, node->str);
22458 *offset += strlen (node->str) + 1;
22460 return 1;
22463 /* A helper function for dwarf2out_finish called through
22464 htab_traverse. Output the indexed string. */
22466 static int
22467 output_index_string (void **h, void *v)
22469 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22470 unsigned int *cur_idx = (unsigned int *) v;
22472 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22474 /* Assert that the strings are output in the same order as their
22475 indexes were assigned. */
22476 gcc_assert (*cur_idx == node->index);
22477 assemble_string (node->str, strlen (node->str) + 1);
22478 *cur_idx += 1;
22480 return 1;
22483 /* A helper function for dwarf2out_finish called through
22484 htab_traverse. Emit one queued .debug_str string. */
22486 static int
22487 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22489 struct indirect_string_node *node = (struct indirect_string_node *) *h;
22491 node->form = find_string_form (node);
22492 if (node->form == DW_FORM_strp && node->refcount > 0)
22494 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22495 assemble_string (node->str, strlen (node->str) + 1);
22498 return 1;
22501 /* Output the indexed string table. */
22503 static void
22504 output_indirect_strings (void)
22506 switch_to_section (debug_str_section);
22507 if (!dwarf_split_debug_info)
22508 htab_traverse (debug_str_hash, output_indirect_string, NULL);
22509 else
22511 unsigned int offset = 0;
22512 unsigned int cur_idx = 0;
22514 htab_traverse (skeleton_debug_str_hash, output_indirect_string, NULL);
22516 switch_to_section (debug_str_offsets_section);
22517 htab_traverse_noresize (debug_str_hash,
22518 output_index_string_offset,
22519 &offset);
22520 switch_to_section (debug_str_dwo_section);
22521 htab_traverse_noresize (debug_str_hash,
22522 output_index_string,
22523 &cur_idx);
22527 /* Callback for htab_traverse to assign an index to an entry in the
22528 table, and to write that entry to the .debug_addr section. */
22530 static int
22531 output_addr_table_entry (void **slot, void *data)
22533 addr_table_entry *entry = (addr_table_entry *) *slot;
22534 unsigned int *cur_index = (unsigned int *)data;
22536 if (entry->refcount == 0)
22538 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22539 || entry->index == NOT_INDEXED);
22540 return 1;
22543 gcc_assert (entry->index == *cur_index);
22544 (*cur_index)++;
22546 switch (entry->kind)
22548 case ate_kind_rtx:
22549 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22550 "0x%x", entry->index);
22551 break;
22552 case ate_kind_rtx_dtprel:
22553 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22554 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22555 DWARF2_ADDR_SIZE,
22556 entry->addr.rtl);
22557 fputc ('\n', asm_out_file);
22558 break;
22559 case ate_kind_label:
22560 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22561 "0x%x", entry->index);
22562 break;
22563 default:
22564 gcc_unreachable ();
22566 return 1;
22569 /* Produce the .debug_addr section. */
22571 static void
22572 output_addr_table (void)
22574 unsigned int index = 0;
22575 if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
22576 return;
22578 switch_to_section (debug_addr_section);
22579 htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
22582 #if ENABLE_ASSERT_CHECKING
22583 /* Verify that all marks are clear. */
22585 static void
22586 verify_marks_clear (dw_die_ref die)
22588 dw_die_ref c;
22590 gcc_assert (! die->die_mark);
22591 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22593 #endif /* ENABLE_ASSERT_CHECKING */
22595 /* Clear the marks for a die and its children.
22596 Be cool if the mark isn't set. */
22598 static void
22599 prune_unmark_dies (dw_die_ref die)
22601 dw_die_ref c;
22603 if (die->die_mark)
22604 die->die_mark = 0;
22605 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22608 /* Given DIE that we're marking as used, find any other dies
22609 it references as attributes and mark them as used. */
22611 static void
22612 prune_unused_types_walk_attribs (dw_die_ref die)
22614 dw_attr_ref a;
22615 unsigned ix;
22617 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22619 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22621 /* A reference to another DIE.
22622 Make sure that it will get emitted.
22623 If it was broken out into a comdat group, don't follow it. */
22624 if (! AT_ref (a)->comdat_type_p
22625 || a->dw_attr == DW_AT_specification)
22626 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22628 /* Set the string's refcount to 0 so that prune_unused_types_mark
22629 accounts properly for it. */
22630 if (AT_class (a) == dw_val_class_str)
22631 a->dw_attr_val.v.val_str->refcount = 0;
22635 /* Mark the generic parameters and arguments children DIEs of DIE. */
22637 static void
22638 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22640 dw_die_ref c;
22642 if (die == NULL || die->die_child == NULL)
22643 return;
22644 c = die->die_child;
22647 if (is_template_parameter (c))
22648 prune_unused_types_mark (c, 1);
22649 c = c->die_sib;
22650 } while (c && c != die->die_child);
22653 /* Mark DIE as being used. If DOKIDS is true, then walk down
22654 to DIE's children. */
22656 static void
22657 prune_unused_types_mark (dw_die_ref die, int dokids)
22659 dw_die_ref c;
22661 if (die->die_mark == 0)
22663 /* We haven't done this node yet. Mark it as used. */
22664 die->die_mark = 1;
22665 /* If this is the DIE of a generic type instantiation,
22666 mark the children DIEs that describe its generic parms and
22667 args. */
22668 prune_unused_types_mark_generic_parms_dies (die);
22670 /* We also have to mark its parents as used.
22671 (But we don't want to mark our parent's kids due to this,
22672 unless it is a class.) */
22673 if (die->die_parent)
22674 prune_unused_types_mark (die->die_parent,
22675 class_scope_p (die->die_parent));
22677 /* Mark any referenced nodes. */
22678 prune_unused_types_walk_attribs (die);
22680 /* If this node is a specification,
22681 also mark the definition, if it exists. */
22682 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22683 prune_unused_types_mark (die->die_definition, 1);
22686 if (dokids && die->die_mark != 2)
22688 /* We need to walk the children, but haven't done so yet.
22689 Remember that we've walked the kids. */
22690 die->die_mark = 2;
22692 /* If this is an array type, we need to make sure our
22693 kids get marked, even if they're types. If we're
22694 breaking out types into comdat sections, do this
22695 for all type definitions. */
22696 if (die->die_tag == DW_TAG_array_type
22697 || (use_debug_types
22698 && is_type_die (die) && ! is_declaration_die (die)))
22699 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22700 else
22701 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22705 /* For local classes, look if any static member functions were emitted
22706 and if so, mark them. */
22708 static void
22709 prune_unused_types_walk_local_classes (dw_die_ref die)
22711 dw_die_ref c;
22713 if (die->die_mark == 2)
22714 return;
22716 switch (die->die_tag)
22718 case DW_TAG_structure_type:
22719 case DW_TAG_union_type:
22720 case DW_TAG_class_type:
22721 break;
22723 case DW_TAG_subprogram:
22724 if (!get_AT_flag (die, DW_AT_declaration)
22725 || die->die_definition != NULL)
22726 prune_unused_types_mark (die, 1);
22727 return;
22729 default:
22730 return;
22733 /* Mark children. */
22734 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22737 /* Walk the tree DIE and mark types that we actually use. */
22739 static void
22740 prune_unused_types_walk (dw_die_ref die)
22742 dw_die_ref c;
22744 /* Don't do anything if this node is already marked and
22745 children have been marked as well. */
22746 if (die->die_mark == 2)
22747 return;
22749 switch (die->die_tag)
22751 case DW_TAG_structure_type:
22752 case DW_TAG_union_type:
22753 case DW_TAG_class_type:
22754 if (die->die_perennial_p)
22755 break;
22757 for (c = die->die_parent; c; c = c->die_parent)
22758 if (c->die_tag == DW_TAG_subprogram)
22759 break;
22761 /* Finding used static member functions inside of classes
22762 is needed just for local classes, because for other classes
22763 static member function DIEs with DW_AT_specification
22764 are emitted outside of the DW_TAG_*_type. If we ever change
22765 it, we'd need to call this even for non-local classes. */
22766 if (c)
22767 prune_unused_types_walk_local_classes (die);
22769 /* It's a type node --- don't mark it. */
22770 return;
22772 case DW_TAG_const_type:
22773 case DW_TAG_packed_type:
22774 case DW_TAG_pointer_type:
22775 case DW_TAG_reference_type:
22776 case DW_TAG_rvalue_reference_type:
22777 case DW_TAG_volatile_type:
22778 case DW_TAG_typedef:
22779 case DW_TAG_array_type:
22780 case DW_TAG_interface_type:
22781 case DW_TAG_friend:
22782 case DW_TAG_variant_part:
22783 case DW_TAG_enumeration_type:
22784 case DW_TAG_subroutine_type:
22785 case DW_TAG_string_type:
22786 case DW_TAG_set_type:
22787 case DW_TAG_subrange_type:
22788 case DW_TAG_ptr_to_member_type:
22789 case DW_TAG_file_type:
22790 if (die->die_perennial_p)
22791 break;
22793 /* It's a type node --- don't mark it. */
22794 return;
22796 default:
22797 /* Mark everything else. */
22798 break;
22801 if (die->die_mark == 0)
22803 die->die_mark = 1;
22805 /* Now, mark any dies referenced from here. */
22806 prune_unused_types_walk_attribs (die);
22809 die->die_mark = 2;
22811 /* Mark children. */
22812 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22815 /* Increment the string counts on strings referred to from DIE's
22816 attributes. */
22818 static void
22819 prune_unused_types_update_strings (dw_die_ref die)
22821 dw_attr_ref a;
22822 unsigned ix;
22824 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22825 if (AT_class (a) == dw_val_class_str)
22827 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22828 s->refcount++;
22829 /* Avoid unnecessarily putting strings that are used less than
22830 twice in the hash table. */
22831 if (s->refcount
22832 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22834 void ** slot;
22835 slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22836 htab_hash_string (s->str),
22837 INSERT);
22838 gcc_assert (*slot == NULL);
22839 *slot = s;
22844 /* Remove from the tree DIE any dies that aren't marked. */
22846 static void
22847 prune_unused_types_prune (dw_die_ref die)
22849 dw_die_ref c;
22851 gcc_assert (die->die_mark);
22852 prune_unused_types_update_strings (die);
22854 if (! die->die_child)
22855 return;
22857 c = die->die_child;
22858 do {
22859 dw_die_ref prev = c;
22860 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22861 if (c == die->die_child)
22863 /* No marked children between 'prev' and the end of the list. */
22864 if (prev == c)
22865 /* No marked children at all. */
22866 die->die_child = NULL;
22867 else
22869 prev->die_sib = c->die_sib;
22870 die->die_child = prev;
22872 return;
22875 if (c != prev->die_sib)
22876 prev->die_sib = c;
22877 prune_unused_types_prune (c);
22878 } while (c != die->die_child);
22881 /* Remove dies representing declarations that we never use. */
22883 static void
22884 prune_unused_types (void)
22886 unsigned int i;
22887 limbo_die_node *node;
22888 comdat_type_node *ctnode;
22889 pubname_ref pub;
22890 dw_die_ref base_type;
22892 #if ENABLE_ASSERT_CHECKING
22893 /* All the marks should already be clear. */
22894 verify_marks_clear (comp_unit_die ());
22895 for (node = limbo_die_list; node; node = node->next)
22896 verify_marks_clear (node->die);
22897 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22898 verify_marks_clear (ctnode->root_die);
22899 #endif /* ENABLE_ASSERT_CHECKING */
22901 /* Mark types that are used in global variables. */
22902 premark_types_used_by_global_vars ();
22904 /* Set the mark on nodes that are actually used. */
22905 prune_unused_types_walk (comp_unit_die ());
22906 for (node = limbo_die_list; node; node = node->next)
22907 prune_unused_types_walk (node->die);
22908 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22910 prune_unused_types_walk (ctnode->root_die);
22911 prune_unused_types_mark (ctnode->type_die, 1);
22914 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22915 are unusual in that they are pubnames that are the children of pubtypes.
22916 They should only be marked via their parent DW_TAG_enumeration_type die,
22917 not as roots in themselves. */
22918 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22919 if (pub->die->die_tag != DW_TAG_enumerator)
22920 prune_unused_types_mark (pub->die, 1);
22921 for (i = 0; base_types.iterate (i, &base_type); i++)
22922 prune_unused_types_mark (base_type, 1);
22924 if (debug_str_hash)
22925 htab_empty (debug_str_hash);
22926 if (skeleton_debug_str_hash)
22927 htab_empty (skeleton_debug_str_hash);
22928 prune_unused_types_prune (comp_unit_die ());
22929 for (node = limbo_die_list; node; node = node->next)
22930 prune_unused_types_prune (node->die);
22931 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22932 prune_unused_types_prune (ctnode->root_die);
22934 /* Leave the marks clear. */
22935 prune_unmark_dies (comp_unit_die ());
22936 for (node = limbo_die_list; node; node = node->next)
22937 prune_unmark_dies (node->die);
22938 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22939 prune_unmark_dies (ctnode->root_die);
22942 /* Set the parameter to true if there are any relative pathnames in
22943 the file table. */
22944 static int
22945 file_table_relative_p (void ** slot, void *param)
22947 bool *p = (bool *) param;
22948 struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22949 if (!IS_ABSOLUTE_PATH (d->filename))
22951 *p = true;
22952 return 0;
22954 return 1;
22957 /* Helpers to manipulate hash table of comdat type units. */
22959 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22961 typedef comdat_type_node value_type;
22962 typedef comdat_type_node compare_type;
22963 static inline hashval_t hash (const value_type *);
22964 static inline bool equal (const value_type *, const compare_type *);
22967 inline hashval_t
22968 comdat_type_hasher::hash (const value_type *type_node)
22970 hashval_t h;
22971 memcpy (&h, type_node->signature, sizeof (h));
22972 return h;
22975 inline bool
22976 comdat_type_hasher::equal (const value_type *type_node_1,
22977 const compare_type *type_node_2)
22979 return (! memcmp (type_node_1->signature, type_node_2->signature,
22980 DWARF_TYPE_SIGNATURE_SIZE));
22983 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22984 to the location it would have been added, should we know its
22985 DECL_ASSEMBLER_NAME when we added other attributes. This will
22986 probably improve compactness of debug info, removing equivalent
22987 abbrevs, and hide any differences caused by deferring the
22988 computation of the assembler name, triggered by e.g. PCH. */
22990 static inline void
22991 move_linkage_attr (dw_die_ref die)
22993 unsigned ix = vec_safe_length (die->die_attr);
22994 dw_attr_node linkage = (*die->die_attr)[ix - 1];
22996 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22997 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22999 while (--ix > 0)
23001 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23003 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23004 break;
23007 if (ix != vec_safe_length (die->die_attr) - 1)
23009 die->die_attr->pop ();
23010 die->die_attr->quick_insert (ix, linkage);
23014 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23015 referenced from typed stack ops and count how often they are used. */
23017 static void
23018 mark_base_types (dw_loc_descr_ref loc)
23020 dw_die_ref base_type = NULL;
23022 for (; loc; loc = loc->dw_loc_next)
23024 switch (loc->dw_loc_opc)
23026 case DW_OP_GNU_regval_type:
23027 case DW_OP_GNU_deref_type:
23028 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23029 break;
23030 case DW_OP_GNU_convert:
23031 case DW_OP_GNU_reinterpret:
23032 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23033 continue;
23034 /* FALLTHRU */
23035 case DW_OP_GNU_const_type:
23036 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23037 break;
23038 case DW_OP_GNU_entry_value:
23039 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23040 continue;
23041 default:
23042 continue;
23044 gcc_assert (base_type->die_parent == comp_unit_die ());
23045 if (base_type->die_mark)
23046 base_type->die_mark++;
23047 else
23049 base_types.safe_push (base_type);
23050 base_type->die_mark = 1;
23055 /* Comparison function for sorting marked base types. */
23057 static int
23058 base_type_cmp (const void *x, const void *y)
23060 dw_die_ref dx = *(const dw_die_ref *) x;
23061 dw_die_ref dy = *(const dw_die_ref *) y;
23062 unsigned int byte_size1, byte_size2;
23063 unsigned int encoding1, encoding2;
23064 if (dx->die_mark > dy->die_mark)
23065 return -1;
23066 if (dx->die_mark < dy->die_mark)
23067 return 1;
23068 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23069 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23070 if (byte_size1 < byte_size2)
23071 return 1;
23072 if (byte_size1 > byte_size2)
23073 return -1;
23074 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23075 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23076 if (encoding1 < encoding2)
23077 return 1;
23078 if (encoding1 > encoding2)
23079 return -1;
23080 return 0;
23083 /* Move base types marked by mark_base_types as early as possible
23084 in the CU, sorted by decreasing usage count both to make the
23085 uleb128 references as small as possible and to make sure they
23086 will have die_offset already computed by calc_die_sizes when
23087 sizes of typed stack loc ops is computed. */
23089 static void
23090 move_marked_base_types (void)
23092 unsigned int i;
23093 dw_die_ref base_type, die, c;
23095 if (base_types.is_empty ())
23096 return;
23098 /* Sort by decreasing usage count, they will be added again in that
23099 order later on. */
23100 base_types.qsort (base_type_cmp);
23101 die = comp_unit_die ();
23102 c = die->die_child;
23105 dw_die_ref prev = c;
23106 c = c->die_sib;
23107 while (c->die_mark)
23109 remove_child_with_prev (c, prev);
23110 /* As base types got marked, there must be at least
23111 one node other than DW_TAG_base_type. */
23112 gcc_assert (c != c->die_sib);
23113 c = c->die_sib;
23116 while (c != die->die_child);
23117 gcc_assert (die->die_child);
23118 c = die->die_child;
23119 for (i = 0; base_types.iterate (i, &base_type); i++)
23121 base_type->die_mark = 0;
23122 base_type->die_sib = c->die_sib;
23123 c->die_sib = base_type;
23124 c = base_type;
23128 /* Helper function for resolve_addr, attempt to resolve
23129 one CONST_STRING, return non-zero if not successful. Similarly verify that
23130 SYMBOL_REFs refer to variables emitted in the current CU. */
23132 static int
23133 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23135 rtx rtl = *addr;
23137 if (GET_CODE (rtl) == CONST_STRING)
23139 size_t len = strlen (XSTR (rtl, 0)) + 1;
23140 tree t = build_string (len, XSTR (rtl, 0));
23141 tree tlen = size_int (len - 1);
23142 TREE_TYPE (t)
23143 = build_array_type (char_type_node, build_index_type (tlen));
23144 rtl = lookup_constant_def (t);
23145 if (!rtl || !MEM_P (rtl))
23146 return 1;
23147 rtl = XEXP (rtl, 0);
23148 if (GET_CODE (rtl) == SYMBOL_REF
23149 && SYMBOL_REF_DECL (rtl)
23150 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23151 return 1;
23152 vec_safe_push (used_rtx_array, rtl);
23153 *addr = rtl;
23154 return 0;
23157 if (GET_CODE (rtl) == SYMBOL_REF
23158 && SYMBOL_REF_DECL (rtl))
23160 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23162 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23163 return 1;
23165 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23166 return 1;
23169 if (GET_CODE (rtl) == CONST
23170 && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23171 return 1;
23173 return 0;
23176 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23177 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23178 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23180 static rtx
23181 string_cst_pool_decl (tree t)
23183 rtx rtl = output_constant_def (t, 1);
23184 unsigned char *array;
23185 dw_loc_descr_ref l;
23186 tree decl;
23187 size_t len;
23188 dw_die_ref ref;
23190 if (!rtl || !MEM_P (rtl))
23191 return NULL_RTX;
23192 rtl = XEXP (rtl, 0);
23193 if (GET_CODE (rtl) != SYMBOL_REF
23194 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23195 return NULL_RTX;
23197 decl = SYMBOL_REF_DECL (rtl);
23198 if (!lookup_decl_die (decl))
23200 len = TREE_STRING_LENGTH (t);
23201 vec_safe_push (used_rtx_array, rtl);
23202 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23203 array = ggc_vec_alloc<unsigned char> (len);
23204 memcpy (array, TREE_STRING_POINTER (t), len);
23205 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23206 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23207 l->dw_loc_oprnd2.v.val_vec.length = len;
23208 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23209 l->dw_loc_oprnd2.v.val_vec.array = array;
23210 add_AT_loc (ref, DW_AT_location, l);
23211 equate_decl_number_to_die (decl, ref);
23213 return rtl;
23216 /* Helper function of resolve_addr_in_expr. LOC is
23217 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23218 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23219 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23220 with DW_OP_GNU_implicit_pointer if possible
23221 and return true, if unsuccessful, return false. */
23223 static bool
23224 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23226 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23227 HOST_WIDE_INT offset = 0;
23228 dw_die_ref ref = NULL;
23229 tree decl;
23231 if (GET_CODE (rtl) == CONST
23232 && GET_CODE (XEXP (rtl, 0)) == PLUS
23233 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23235 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23236 rtl = XEXP (XEXP (rtl, 0), 0);
23238 if (GET_CODE (rtl) == CONST_STRING)
23240 size_t len = strlen (XSTR (rtl, 0)) + 1;
23241 tree t = build_string (len, XSTR (rtl, 0));
23242 tree tlen = size_int (len - 1);
23244 TREE_TYPE (t)
23245 = build_array_type (char_type_node, build_index_type (tlen));
23246 rtl = string_cst_pool_decl (t);
23247 if (!rtl)
23248 return false;
23250 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23252 decl = SYMBOL_REF_DECL (rtl);
23253 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23255 ref = lookup_decl_die (decl);
23256 if (ref && (get_AT (ref, DW_AT_location)
23257 || get_AT (ref, DW_AT_const_value)))
23259 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23260 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23261 loc->dw_loc_oprnd1.val_entry = NULL;
23262 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23263 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23264 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23265 loc->dw_loc_oprnd2.v.val_int = offset;
23266 return true;
23270 return false;
23273 /* Helper function for resolve_addr, handle one location
23274 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23275 the location list couldn't be resolved. */
23277 static bool
23278 resolve_addr_in_expr (dw_loc_descr_ref loc)
23280 dw_loc_descr_ref keep = NULL;
23281 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23282 switch (loc->dw_loc_opc)
23284 case DW_OP_addr:
23285 if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23287 if ((prev == NULL
23288 || prev->dw_loc_opc == DW_OP_piece
23289 || prev->dw_loc_opc == DW_OP_bit_piece)
23290 && loc->dw_loc_next
23291 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23292 && !dwarf_strict
23293 && optimize_one_addr_into_implicit_ptr (loc))
23294 break;
23295 return false;
23297 break;
23298 case DW_OP_GNU_addr_index:
23299 case DW_OP_GNU_const_index:
23300 if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
23301 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23302 && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
23303 NULL))
23304 return false;
23305 break;
23306 case DW_OP_const4u:
23307 case DW_OP_const8u:
23308 if (loc->dtprel
23309 && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23310 return false;
23311 break;
23312 case DW_OP_plus_uconst:
23313 if (size_of_loc_descr (loc)
23314 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23316 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23318 dw_loc_descr_ref repl
23319 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23320 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23321 add_loc_descr (&repl, loc->dw_loc_next);
23322 *loc = *repl;
23324 break;
23325 case DW_OP_implicit_value:
23326 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23327 && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
23328 return false;
23329 break;
23330 case DW_OP_GNU_implicit_pointer:
23331 case DW_OP_GNU_parameter_ref:
23332 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23334 dw_die_ref ref
23335 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23336 if (ref == NULL)
23337 return false;
23338 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23339 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23340 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23342 break;
23343 case DW_OP_GNU_const_type:
23344 case DW_OP_GNU_regval_type:
23345 case DW_OP_GNU_deref_type:
23346 case DW_OP_GNU_convert:
23347 case DW_OP_GNU_reinterpret:
23348 while (loc->dw_loc_next
23349 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23351 dw_die_ref base1, base2;
23352 unsigned enc1, enc2, size1, size2;
23353 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23354 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23355 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23356 else if (loc->dw_loc_oprnd1.val_class
23357 == dw_val_class_unsigned_const)
23358 break;
23359 else
23360 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23361 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23362 == dw_val_class_unsigned_const)
23363 break;
23364 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23365 gcc_assert (base1->die_tag == DW_TAG_base_type
23366 && base2->die_tag == DW_TAG_base_type);
23367 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23368 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23369 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23370 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23371 if (size1 == size2
23372 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23373 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23374 && loc != keep)
23375 || enc1 == enc2))
23377 /* Optimize away next DW_OP_GNU_convert after
23378 adjusting LOC's base type die reference. */
23379 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23380 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23381 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23382 else
23383 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23384 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23385 continue;
23387 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23388 point typed stack entry. */
23389 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23390 keep = loc->dw_loc_next;
23391 break;
23393 break;
23394 default:
23395 break;
23397 return true;
23400 /* Helper function of resolve_addr. DIE had DW_AT_location of
23401 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23402 and DW_OP_addr couldn't be resolved. resolve_addr has already
23403 removed the DW_AT_location attribute. This function attempts to
23404 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23405 to it or DW_AT_const_value attribute, if possible. */
23407 static void
23408 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23410 if (TREE_CODE (decl) != VAR_DECL
23411 || lookup_decl_die (decl) != die
23412 || DECL_EXTERNAL (decl)
23413 || !TREE_STATIC (decl)
23414 || DECL_INITIAL (decl) == NULL_TREE
23415 || DECL_P (DECL_INITIAL (decl))
23416 || get_AT (die, DW_AT_const_value))
23417 return;
23419 tree init = DECL_INITIAL (decl);
23420 HOST_WIDE_INT offset = 0;
23421 /* For variables that have been optimized away and thus
23422 don't have a memory location, see if we can emit
23423 DW_AT_const_value instead. */
23424 if (tree_add_const_value_attribute (die, init))
23425 return;
23426 if (dwarf_strict)
23427 return;
23428 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23429 and ADDR_EXPR refers to a decl that has DW_AT_location or
23430 DW_AT_const_value (but isn't addressable, otherwise
23431 resolving the original DW_OP_addr wouldn't fail), see if
23432 we can add DW_OP_GNU_implicit_pointer. */
23433 STRIP_NOPS (init);
23434 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23435 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23437 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23438 init = TREE_OPERAND (init, 0);
23439 STRIP_NOPS (init);
23441 if (TREE_CODE (init) != ADDR_EXPR)
23442 return;
23443 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23444 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23445 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23446 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23447 && TREE_OPERAND (init, 0) != decl))
23449 dw_die_ref ref;
23450 dw_loc_descr_ref l;
23452 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23454 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23455 if (!rtl)
23456 return;
23457 decl = SYMBOL_REF_DECL (rtl);
23459 else
23460 decl = TREE_OPERAND (init, 0);
23461 ref = lookup_decl_die (decl);
23462 if (ref == NULL
23463 || (!get_AT (ref, DW_AT_location)
23464 && !get_AT (ref, DW_AT_const_value)))
23465 return;
23466 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23467 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23468 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23469 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23470 add_AT_loc (die, DW_AT_location, l);
23474 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23475 an address in .rodata section if the string literal is emitted there,
23476 or remove the containing location list or replace DW_AT_const_value
23477 with DW_AT_location and empty location expression, if it isn't found
23478 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23479 to something that has been emitted in the current CU. */
23481 static void
23482 resolve_addr (dw_die_ref die)
23484 dw_die_ref c;
23485 dw_attr_ref a;
23486 dw_loc_list_ref *curr, *start, loc;
23487 unsigned ix;
23489 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23490 switch (AT_class (a))
23492 case dw_val_class_loc_list:
23493 start = curr = AT_loc_list_ptr (a);
23494 loc = *curr;
23495 gcc_assert (loc);
23496 /* The same list can be referenced more than once. See if we have
23497 already recorded the result from a previous pass. */
23498 if (loc->replaced)
23499 *curr = loc->dw_loc_next;
23500 else if (!loc->resolved_addr)
23502 /* As things stand, we do not expect or allow one die to
23503 reference a suffix of another die's location list chain.
23504 References must be identical or completely separate.
23505 There is therefore no need to cache the result of this
23506 pass on any list other than the first; doing so
23507 would lead to unnecessary writes. */
23508 while (*curr)
23510 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23511 if (!resolve_addr_in_expr ((*curr)->expr))
23513 dw_loc_list_ref next = (*curr)->dw_loc_next;
23514 dw_loc_descr_ref l = (*curr)->expr;
23516 if (next && (*curr)->ll_symbol)
23518 gcc_assert (!next->ll_symbol);
23519 next->ll_symbol = (*curr)->ll_symbol;
23521 if (dwarf_split_debug_info)
23522 remove_loc_list_addr_table_entries (l);
23523 *curr = next;
23525 else
23527 mark_base_types ((*curr)->expr);
23528 curr = &(*curr)->dw_loc_next;
23531 if (loc == *start)
23532 loc->resolved_addr = 1;
23533 else
23535 loc->replaced = 1;
23536 loc->dw_loc_next = *start;
23539 if (!*start)
23541 remove_AT (die, a->dw_attr);
23542 ix--;
23544 break;
23545 case dw_val_class_loc:
23547 dw_loc_descr_ref l = AT_loc (a);
23548 /* For -gdwarf-2 don't attempt to optimize
23549 DW_AT_data_member_location containing
23550 DW_OP_plus_uconst - older consumers might
23551 rely on it being that op instead of a more complex,
23552 but shorter, location description. */
23553 if ((dwarf_version > 2
23554 || a->dw_attr != DW_AT_data_member_location
23555 || l == NULL
23556 || l->dw_loc_opc != DW_OP_plus_uconst
23557 || l->dw_loc_next != NULL)
23558 && !resolve_addr_in_expr (l))
23560 if (dwarf_split_debug_info)
23561 remove_loc_list_addr_table_entries (l);
23562 if (l != NULL
23563 && l->dw_loc_next == NULL
23564 && l->dw_loc_opc == DW_OP_addr
23565 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23566 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23567 && a->dw_attr == DW_AT_location)
23569 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23570 remove_AT (die, a->dw_attr);
23571 ix--;
23572 optimize_location_into_implicit_ptr (die, decl);
23573 break;
23575 remove_AT (die, a->dw_attr);
23576 ix--;
23578 else
23579 mark_base_types (l);
23581 break;
23582 case dw_val_class_addr:
23583 if (a->dw_attr == DW_AT_const_value
23584 && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23586 if (AT_index (a) != NOT_INDEXED)
23587 remove_addr_table_entry (a->dw_attr_val.val_entry);
23588 remove_AT (die, a->dw_attr);
23589 ix--;
23591 if (die->die_tag == DW_TAG_GNU_call_site
23592 && a->dw_attr == DW_AT_abstract_origin)
23594 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23595 dw_die_ref tdie = lookup_decl_die (tdecl);
23596 if (tdie == NULL
23597 && DECL_EXTERNAL (tdecl)
23598 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23600 force_decl_die (tdecl);
23601 tdie = lookup_decl_die (tdecl);
23603 if (tdie)
23605 a->dw_attr_val.val_class = dw_val_class_die_ref;
23606 a->dw_attr_val.v.val_die_ref.die = tdie;
23607 a->dw_attr_val.v.val_die_ref.external = 0;
23609 else
23611 if (AT_index (a) != NOT_INDEXED)
23612 remove_addr_table_entry (a->dw_attr_val.val_entry);
23613 remove_AT (die, a->dw_attr);
23614 ix--;
23617 break;
23618 default:
23619 break;
23622 FOR_EACH_CHILD (die, c, resolve_addr (c));
23625 /* Helper routines for optimize_location_lists.
23626 This pass tries to share identical local lists in .debug_loc
23627 section. */
23629 /* Iteratively hash operands of LOC opcode. */
23631 static hashval_t
23632 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23634 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23635 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23637 switch (loc->dw_loc_opc)
23639 case DW_OP_const4u:
23640 case DW_OP_const8u:
23641 if (loc->dtprel)
23642 goto hash_addr;
23643 /* FALLTHRU */
23644 case DW_OP_const1u:
23645 case DW_OP_const1s:
23646 case DW_OP_const2u:
23647 case DW_OP_const2s:
23648 case DW_OP_const4s:
23649 case DW_OP_const8s:
23650 case DW_OP_constu:
23651 case DW_OP_consts:
23652 case DW_OP_pick:
23653 case DW_OP_plus_uconst:
23654 case DW_OP_breg0:
23655 case DW_OP_breg1:
23656 case DW_OP_breg2:
23657 case DW_OP_breg3:
23658 case DW_OP_breg4:
23659 case DW_OP_breg5:
23660 case DW_OP_breg6:
23661 case DW_OP_breg7:
23662 case DW_OP_breg8:
23663 case DW_OP_breg9:
23664 case DW_OP_breg10:
23665 case DW_OP_breg11:
23666 case DW_OP_breg12:
23667 case DW_OP_breg13:
23668 case DW_OP_breg14:
23669 case DW_OP_breg15:
23670 case DW_OP_breg16:
23671 case DW_OP_breg17:
23672 case DW_OP_breg18:
23673 case DW_OP_breg19:
23674 case DW_OP_breg20:
23675 case DW_OP_breg21:
23676 case DW_OP_breg22:
23677 case DW_OP_breg23:
23678 case DW_OP_breg24:
23679 case DW_OP_breg25:
23680 case DW_OP_breg26:
23681 case DW_OP_breg27:
23682 case DW_OP_breg28:
23683 case DW_OP_breg29:
23684 case DW_OP_breg30:
23685 case DW_OP_breg31:
23686 case DW_OP_regx:
23687 case DW_OP_fbreg:
23688 case DW_OP_piece:
23689 case DW_OP_deref_size:
23690 case DW_OP_xderef_size:
23691 hash = iterative_hash_object (val1->v.val_int, hash);
23692 break;
23693 case DW_OP_skip:
23694 case DW_OP_bra:
23696 int offset;
23698 gcc_assert (val1->val_class == dw_val_class_loc);
23699 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23700 hash = iterative_hash_object (offset, hash);
23702 break;
23703 case DW_OP_implicit_value:
23704 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23705 switch (val2->val_class)
23707 case dw_val_class_const:
23708 hash = iterative_hash_object (val2->v.val_int, hash);
23709 break;
23710 case dw_val_class_vec:
23712 unsigned int elt_size = val2->v.val_vec.elt_size;
23713 unsigned int len = val2->v.val_vec.length;
23715 hash = iterative_hash_object (elt_size, hash);
23716 hash = iterative_hash_object (len, hash);
23717 hash = iterative_hash (val2->v.val_vec.array,
23718 len * elt_size, hash);
23720 break;
23721 case dw_val_class_const_double:
23722 hash = iterative_hash_object (val2->v.val_double.low, hash);
23723 hash = iterative_hash_object (val2->v.val_double.high, hash);
23724 break;
23725 case dw_val_class_wide_int:
23726 hash = iterative_hash_object (*val2->v.val_wide, hash);
23727 break;
23728 case dw_val_class_addr:
23729 hash = iterative_hash_rtx (val2->v.val_addr, hash);
23730 break;
23731 default:
23732 gcc_unreachable ();
23734 break;
23735 case DW_OP_bregx:
23736 case DW_OP_bit_piece:
23737 hash = iterative_hash_object (val1->v.val_int, hash);
23738 hash = iterative_hash_object (val2->v.val_int, hash);
23739 break;
23740 case DW_OP_addr:
23741 hash_addr:
23742 if (loc->dtprel)
23744 unsigned char dtprel = 0xd1;
23745 hash = iterative_hash_object (dtprel, hash);
23747 hash = iterative_hash_rtx (val1->v.val_addr, hash);
23748 break;
23749 case DW_OP_GNU_addr_index:
23750 case DW_OP_GNU_const_index:
23752 if (loc->dtprel)
23754 unsigned char dtprel = 0xd1;
23755 hash = iterative_hash_object (dtprel, hash);
23757 hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
23759 break;
23760 case DW_OP_GNU_implicit_pointer:
23761 hash = iterative_hash_object (val2->v.val_int, hash);
23762 break;
23763 case DW_OP_GNU_entry_value:
23764 hash = hash_loc_operands (val1->v.val_loc, hash);
23765 break;
23766 case DW_OP_GNU_regval_type:
23767 case DW_OP_GNU_deref_type:
23769 unsigned int byte_size
23770 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23771 unsigned int encoding
23772 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23773 hash = iterative_hash_object (val1->v.val_int, hash);
23774 hash = iterative_hash_object (byte_size, hash);
23775 hash = iterative_hash_object (encoding, hash);
23777 break;
23778 case DW_OP_GNU_convert:
23779 case DW_OP_GNU_reinterpret:
23780 if (val1->val_class == dw_val_class_unsigned_const)
23782 hash = iterative_hash_object (val1->v.val_unsigned, hash);
23783 break;
23785 /* FALLTHRU */
23786 case DW_OP_GNU_const_type:
23788 unsigned int byte_size
23789 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23790 unsigned int encoding
23791 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23792 hash = iterative_hash_object (byte_size, hash);
23793 hash = iterative_hash_object (encoding, hash);
23794 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23795 break;
23796 hash = iterative_hash_object (val2->val_class, hash);
23797 switch (val2->val_class)
23799 case dw_val_class_const:
23800 hash = iterative_hash_object (val2->v.val_int, hash);
23801 break;
23802 case dw_val_class_vec:
23804 unsigned int elt_size = val2->v.val_vec.elt_size;
23805 unsigned int len = val2->v.val_vec.length;
23807 hash = iterative_hash_object (elt_size, hash);
23808 hash = iterative_hash_object (len, hash);
23809 hash = iterative_hash (val2->v.val_vec.array,
23810 len * elt_size, hash);
23812 break;
23813 case dw_val_class_const_double:
23814 hash = iterative_hash_object (val2->v.val_double.low, hash);
23815 hash = iterative_hash_object (val2->v.val_double.high, hash);
23816 break;
23817 case dw_val_class_wide_int:
23818 hash = iterative_hash_object (*val2->v.val_wide, hash);
23819 break;
23820 default:
23821 gcc_unreachable ();
23824 break;
23826 default:
23827 /* Other codes have no operands. */
23828 break;
23830 return hash;
23833 /* Iteratively hash the whole DWARF location expression LOC. */
23835 static inline hashval_t
23836 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23838 dw_loc_descr_ref l;
23839 bool sizes_computed = false;
23840 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23841 size_of_locs (loc);
23843 for (l = loc; l != NULL; l = l->dw_loc_next)
23845 enum dwarf_location_atom opc = l->dw_loc_opc;
23846 hash = iterative_hash_object (opc, hash);
23847 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23849 size_of_locs (loc);
23850 sizes_computed = true;
23852 hash = hash_loc_operands (l, hash);
23854 return hash;
23857 /* Compute hash of the whole location list LIST_HEAD. */
23859 static inline void
23860 hash_loc_list (dw_loc_list_ref list_head)
23862 dw_loc_list_ref curr = list_head;
23863 hashval_t hash = 0;
23865 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23867 hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23868 hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23869 if (curr->section)
23870 hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23871 hash);
23872 hash = hash_locs (curr->expr, hash);
23874 list_head->hash = hash;
23877 /* Return true if X and Y opcodes have the same operands. */
23879 static inline bool
23880 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23882 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23883 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23884 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23885 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23887 switch (x->dw_loc_opc)
23889 case DW_OP_const4u:
23890 case DW_OP_const8u:
23891 if (x->dtprel)
23892 goto hash_addr;
23893 /* FALLTHRU */
23894 case DW_OP_const1u:
23895 case DW_OP_const1s:
23896 case DW_OP_const2u:
23897 case DW_OP_const2s:
23898 case DW_OP_const4s:
23899 case DW_OP_const8s:
23900 case DW_OP_constu:
23901 case DW_OP_consts:
23902 case DW_OP_pick:
23903 case DW_OP_plus_uconst:
23904 case DW_OP_breg0:
23905 case DW_OP_breg1:
23906 case DW_OP_breg2:
23907 case DW_OP_breg3:
23908 case DW_OP_breg4:
23909 case DW_OP_breg5:
23910 case DW_OP_breg6:
23911 case DW_OP_breg7:
23912 case DW_OP_breg8:
23913 case DW_OP_breg9:
23914 case DW_OP_breg10:
23915 case DW_OP_breg11:
23916 case DW_OP_breg12:
23917 case DW_OP_breg13:
23918 case DW_OP_breg14:
23919 case DW_OP_breg15:
23920 case DW_OP_breg16:
23921 case DW_OP_breg17:
23922 case DW_OP_breg18:
23923 case DW_OP_breg19:
23924 case DW_OP_breg20:
23925 case DW_OP_breg21:
23926 case DW_OP_breg22:
23927 case DW_OP_breg23:
23928 case DW_OP_breg24:
23929 case DW_OP_breg25:
23930 case DW_OP_breg26:
23931 case DW_OP_breg27:
23932 case DW_OP_breg28:
23933 case DW_OP_breg29:
23934 case DW_OP_breg30:
23935 case DW_OP_breg31:
23936 case DW_OP_regx:
23937 case DW_OP_fbreg:
23938 case DW_OP_piece:
23939 case DW_OP_deref_size:
23940 case DW_OP_xderef_size:
23941 return valx1->v.val_int == valy1->v.val_int;
23942 case DW_OP_skip:
23943 case DW_OP_bra:
23944 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23945 can cause irrelevant differences in dw_loc_addr. */
23946 gcc_assert (valx1->val_class == dw_val_class_loc
23947 && valy1->val_class == dw_val_class_loc
23948 && (dwarf_split_debug_info
23949 || x->dw_loc_addr == y->dw_loc_addr));
23950 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23951 case DW_OP_implicit_value:
23952 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23953 || valx2->val_class != valy2->val_class)
23954 return false;
23955 switch (valx2->val_class)
23957 case dw_val_class_const:
23958 return valx2->v.val_int == valy2->v.val_int;
23959 case dw_val_class_vec:
23960 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23961 && valx2->v.val_vec.length == valy2->v.val_vec.length
23962 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23963 valx2->v.val_vec.elt_size
23964 * valx2->v.val_vec.length) == 0;
23965 case dw_val_class_const_double:
23966 return valx2->v.val_double.low == valy2->v.val_double.low
23967 && valx2->v.val_double.high == valy2->v.val_double.high;
23968 case dw_val_class_wide_int:
23969 return *valx2->v.val_wide == *valy2->v.val_wide;
23970 case dw_val_class_addr:
23971 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23972 default:
23973 gcc_unreachable ();
23975 case DW_OP_bregx:
23976 case DW_OP_bit_piece:
23977 return valx1->v.val_int == valy1->v.val_int
23978 && valx2->v.val_int == valy2->v.val_int;
23979 case DW_OP_addr:
23980 hash_addr:
23981 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23982 case DW_OP_GNU_addr_index:
23983 case DW_OP_GNU_const_index:
23985 rtx ax1 = valx1->val_entry->addr.rtl;
23986 rtx ay1 = valy1->val_entry->addr.rtl;
23987 return rtx_equal_p (ax1, ay1);
23989 case DW_OP_GNU_implicit_pointer:
23990 return valx1->val_class == dw_val_class_die_ref
23991 && valx1->val_class == valy1->val_class
23992 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23993 && valx2->v.val_int == valy2->v.val_int;
23994 case DW_OP_GNU_entry_value:
23995 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23996 case DW_OP_GNU_const_type:
23997 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23998 || valx2->val_class != valy2->val_class)
23999 return false;
24000 switch (valx2->val_class)
24002 case dw_val_class_const:
24003 return valx2->v.val_int == valy2->v.val_int;
24004 case dw_val_class_vec:
24005 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24006 && valx2->v.val_vec.length == valy2->v.val_vec.length
24007 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24008 valx2->v.val_vec.elt_size
24009 * valx2->v.val_vec.length) == 0;
24010 case dw_val_class_const_double:
24011 return valx2->v.val_double.low == valy2->v.val_double.low
24012 && valx2->v.val_double.high == valy2->v.val_double.high;
24013 case dw_val_class_wide_int:
24014 return *valx2->v.val_wide == *valy2->v.val_wide;
24015 default:
24016 gcc_unreachable ();
24018 case DW_OP_GNU_regval_type:
24019 case DW_OP_GNU_deref_type:
24020 return valx1->v.val_int == valy1->v.val_int
24021 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24022 case DW_OP_GNU_convert:
24023 case DW_OP_GNU_reinterpret:
24024 if (valx1->val_class != valy1->val_class)
24025 return false;
24026 if (valx1->val_class == dw_val_class_unsigned_const)
24027 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24028 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24029 case DW_OP_GNU_parameter_ref:
24030 return valx1->val_class == dw_val_class_die_ref
24031 && valx1->val_class == valy1->val_class
24032 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24033 default:
24034 /* Other codes have no operands. */
24035 return true;
24039 /* Return true if DWARF location expressions X and Y are the same. */
24041 static inline bool
24042 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24044 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24045 if (x->dw_loc_opc != y->dw_loc_opc
24046 || x->dtprel != y->dtprel
24047 || !compare_loc_operands (x, y))
24048 break;
24049 return x == NULL && y == NULL;
24052 /* Hashtable helpers. */
24054 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24056 typedef dw_loc_list_struct value_type;
24057 typedef dw_loc_list_struct compare_type;
24058 static inline hashval_t hash (const value_type *);
24059 static inline bool equal (const value_type *, const compare_type *);
24062 /* Return precomputed hash of location list X. */
24064 inline hashval_t
24065 loc_list_hasher::hash (const value_type *x)
24067 return x->hash;
24070 /* Return true if location lists A and B are the same. */
24072 inline bool
24073 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24075 if (a == b)
24076 return 1;
24077 if (a->hash != b->hash)
24078 return 0;
24079 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24080 if (strcmp (a->begin, b->begin) != 0
24081 || strcmp (a->end, b->end) != 0
24082 || (a->section == NULL) != (b->section == NULL)
24083 || (a->section && strcmp (a->section, b->section) != 0)
24084 || !compare_locs (a->expr, b->expr))
24085 break;
24086 return a == NULL && b == NULL;
24089 typedef hash_table <loc_list_hasher> loc_list_hash_type;
24092 /* Recursively optimize location lists referenced from DIE
24093 children and share them whenever possible. */
24095 static void
24096 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type htab)
24098 dw_die_ref c;
24099 dw_attr_ref a;
24100 unsigned ix;
24101 dw_loc_list_struct **slot;
24103 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24104 if (AT_class (a) == dw_val_class_loc_list)
24106 dw_loc_list_ref list = AT_loc_list (a);
24107 /* TODO: perform some optimizations here, before hashing
24108 it and storing into the hash table. */
24109 hash_loc_list (list);
24110 slot = htab.find_slot_with_hash (list, list->hash, INSERT);
24111 if (*slot == NULL)
24112 *slot = list;
24113 else
24114 a->dw_attr_val.v.val_loc_list = *slot;
24117 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24121 /* Recursively assign each location list a unique index into the debug_addr
24122 section. */
24124 static void
24125 index_location_lists (dw_die_ref die)
24127 dw_die_ref c;
24128 dw_attr_ref a;
24129 unsigned ix;
24131 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24132 if (AT_class (a) == dw_val_class_loc_list)
24134 dw_loc_list_ref list = AT_loc_list (a);
24135 dw_loc_list_ref curr;
24136 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24138 /* Don't index an entry that has already been indexed
24139 or won't be output. */
24140 if (curr->begin_entry != NULL
24141 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24142 continue;
24144 curr->begin_entry
24145 = add_addr_table_entry (xstrdup (curr->begin),
24146 ate_kind_label);
24150 FOR_EACH_CHILD (die, c, index_location_lists (c));
24153 /* Optimize location lists referenced from DIE
24154 children and share them whenever possible. */
24156 static void
24157 optimize_location_lists (dw_die_ref die)
24159 loc_list_hash_type htab;
24160 htab.create (500);
24161 optimize_location_lists_1 (die, htab);
24162 htab.dispose ();
24165 /* Output stuff that dwarf requires at the end of every file,
24166 and generate the DWARF-2 debugging info. */
24168 static void
24169 dwarf2out_finish (const char *filename)
24171 limbo_die_node *node, *next_node;
24172 comdat_type_node *ctnode;
24173 hash_table <comdat_type_hasher> comdat_type_table;
24174 unsigned int i;
24175 dw_die_ref main_comp_unit_die;
24177 /* PCH might result in DW_AT_producer string being restored from the
24178 header compilation, so always fill it with empty string initially
24179 and overwrite only here. */
24180 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24181 producer_string = gen_producer_string ();
24182 producer->dw_attr_val.v.val_str->refcount--;
24183 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24185 gen_scheduled_generic_parms_dies ();
24186 gen_remaining_tmpl_value_param_die_attribute ();
24188 /* Add the name for the main input file now. We delayed this from
24189 dwarf2out_init to avoid complications with PCH. */
24190 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24191 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24192 add_comp_dir_attribute (comp_unit_die ());
24193 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24195 bool p = false;
24196 htab_traverse (file_table, file_table_relative_p, &p);
24197 if (p)
24198 add_comp_dir_attribute (comp_unit_die ());
24201 if (deferred_locations_list)
24202 for (i = 0; i < deferred_locations_list->length (); i++)
24204 add_location_or_const_value_attribute (
24205 (*deferred_locations_list)[i].die,
24206 (*deferred_locations_list)[i].variable,
24207 false,
24208 DW_AT_location);
24211 /* Traverse the limbo die list, and add parent/child links. The only
24212 dies without parents that should be here are concrete instances of
24213 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24214 For concrete instances, we can get the parent die from the abstract
24215 instance. */
24216 for (node = limbo_die_list; node; node = next_node)
24218 dw_die_ref die = node->die;
24219 next_node = node->next;
24221 if (die->die_parent == NULL)
24223 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24225 if (origin && origin->die_parent)
24226 add_child_die (origin->die_parent, die);
24227 else if (is_cu_die (die))
24229 else if (seen_error ())
24230 /* It's OK to be confused by errors in the input. */
24231 add_child_die (comp_unit_die (), die);
24232 else
24234 /* In certain situations, the lexical block containing a
24235 nested function can be optimized away, which results
24236 in the nested function die being orphaned. Likewise
24237 with the return type of that nested function. Force
24238 this to be a child of the containing function.
24240 It may happen that even the containing function got fully
24241 inlined and optimized out. In that case we are lost and
24242 assign the empty child. This should not be big issue as
24243 the function is likely unreachable too. */
24244 gcc_assert (node->created_for);
24246 if (DECL_P (node->created_for))
24247 origin = get_context_die (DECL_CONTEXT (node->created_for));
24248 else if (TYPE_P (node->created_for))
24249 origin = scope_die_for (node->created_for, comp_unit_die ());
24250 else
24251 origin = comp_unit_die ();
24253 add_child_die (origin, die);
24258 limbo_die_list = NULL;
24260 #if ENABLE_ASSERT_CHECKING
24262 dw_die_ref die = comp_unit_die (), c;
24263 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24265 #endif
24266 resolve_addr (comp_unit_die ());
24267 move_marked_base_types ();
24269 for (node = deferred_asm_name; node; node = node->next)
24271 tree decl = node->created_for;
24272 /* When generating LTO bytecode we can not generate new assembler
24273 names at this point and all important decls got theirs via
24274 free-lang-data. */
24275 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24276 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24278 add_linkage_attr (node->die, decl);
24279 move_linkage_attr (node->die);
24283 deferred_asm_name = NULL;
24285 /* Walk through the list of incomplete types again, trying once more to
24286 emit full debugging info for them. */
24287 retry_incomplete_types ();
24289 if (flag_eliminate_unused_debug_types)
24290 prune_unused_types ();
24292 /* Generate separate COMDAT sections for type DIEs. */
24293 if (use_debug_types)
24295 break_out_comdat_types (comp_unit_die ());
24297 /* Each new type_unit DIE was added to the limbo die list when created.
24298 Since these have all been added to comdat_type_list, clear the
24299 limbo die list. */
24300 limbo_die_list = NULL;
24302 /* For each new comdat type unit, copy declarations for incomplete
24303 types to make the new unit self-contained (i.e., no direct
24304 references to the main compile unit). */
24305 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24306 copy_decls_for_unworthy_types (ctnode->root_die);
24307 copy_decls_for_unworthy_types (comp_unit_die ());
24309 /* In the process of copying declarations from one unit to another,
24310 we may have left some declarations behind that are no longer
24311 referenced. Prune them. */
24312 prune_unused_types ();
24315 /* Generate separate CUs for each of the include files we've seen.
24316 They will go into limbo_die_list. */
24317 if (flag_eliminate_dwarf2_dups)
24318 break_out_includes (comp_unit_die ());
24320 /* Traverse the DIE's and add add sibling attributes to those DIE's
24321 that have children. */
24322 add_sibling_attributes (comp_unit_die ());
24323 for (node = limbo_die_list; node; node = node->next)
24324 add_sibling_attributes (node->die);
24325 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24326 add_sibling_attributes (ctnode->root_die);
24328 /* When splitting DWARF info, we put some attributes in the
24329 skeleton compile_unit DIE that remains in the .o, while
24330 most attributes go in the DWO compile_unit_die. */
24331 if (dwarf_split_debug_info)
24332 main_comp_unit_die = gen_compile_unit_die (NULL);
24333 else
24334 main_comp_unit_die = comp_unit_die ();
24336 /* Output a terminator label for the .text section. */
24337 switch_to_section (text_section);
24338 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24339 if (cold_text_section)
24341 switch_to_section (cold_text_section);
24342 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24345 /* We can only use the low/high_pc attributes if all of the code was
24346 in .text. */
24347 if (!have_multiple_function_sections
24348 || (dwarf_version < 3 && dwarf_strict))
24350 /* Don't add if the CU has no associated code. */
24351 if (text_section_used)
24352 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24353 text_end_label, true);
24355 else
24357 unsigned fde_idx;
24358 dw_fde_ref fde;
24359 bool range_list_added = false;
24361 if (text_section_used)
24362 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24363 text_end_label, &range_list_added, true);
24364 if (cold_text_section_used)
24365 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24366 cold_end_label, &range_list_added, true);
24368 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24370 if (DECL_IGNORED_P (fde->decl))
24371 continue;
24372 if (!fde->in_std_section)
24373 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24374 fde->dw_fde_end, &range_list_added,
24375 true);
24376 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24377 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24378 fde->dw_fde_second_end, &range_list_added,
24379 true);
24382 if (range_list_added)
24384 /* We need to give .debug_loc and .debug_ranges an appropriate
24385 "base address". Use zero so that these addresses become
24386 absolute. Historically, we've emitted the unexpected
24387 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24388 Emit both to give time for other tools to adapt. */
24389 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24390 if (! dwarf_strict && dwarf_version < 4)
24391 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24393 add_ranges (NULL);
24397 if (debug_info_level >= DINFO_LEVEL_TERSE)
24398 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24399 debug_line_section_label);
24401 if (have_macinfo)
24402 add_AT_macptr (comp_unit_die (),
24403 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24404 macinfo_section_label);
24406 if (dwarf_split_debug_info && addr_index_table != NULL)
24408 /* optimize_location_lists calculates the size of the lists,
24409 so index them first, and assign indices to the entries.
24410 Although optimize_location_lists will remove entries from
24411 the table, it only does so for duplicates, and therefore
24412 only reduces ref_counts to 1. */
24413 unsigned int index = 0;
24414 index_location_lists (comp_unit_die ());
24415 htab_traverse_noresize (addr_index_table,
24416 index_addr_table_entry, &index);
24418 if (have_location_lists)
24419 optimize_location_lists (comp_unit_die ());
24421 save_macinfo_strings ();
24423 if (dwarf_split_debug_info)
24425 unsigned int index = 0;
24427 /* Add attributes common to skeleton compile_units and
24428 type_units. Because these attributes include strings, it
24429 must be done before freezing the string table. Top-level
24430 skeleton die attrs are added when the skeleton type unit is
24431 created, so ensure it is created by this point. */
24432 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24433 (void) get_skeleton_type_unit ();
24434 htab_traverse_noresize (debug_str_hash, index_string, &index);
24437 /* Output all of the compilation units. We put the main one last so that
24438 the offsets are available to output_pubnames. */
24439 for (node = limbo_die_list; node; node = node->next)
24440 output_comp_unit (node->die, 0);
24442 comdat_type_table.create (100);
24443 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24445 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24447 /* Don't output duplicate types. */
24448 if (*slot != HTAB_EMPTY_ENTRY)
24449 continue;
24451 /* Add a pointer to the line table for the main compilation unit
24452 so that the debugger can make sense of DW_AT_decl_file
24453 attributes. */
24454 if (debug_info_level >= DINFO_LEVEL_TERSE)
24455 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24456 (!dwarf_split_debug_info
24457 ? debug_line_section_label
24458 : debug_skeleton_line_section_label));
24460 output_comdat_type_unit (ctnode);
24461 *slot = ctnode;
24463 comdat_type_table.dispose ();
24465 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24466 both the main_cu and all skeleton TUs. Making this call unconditional
24467 would end up either adding a second copy of the AT_pubnames attribute, or
24468 requiring a special case in add_top_level_skeleton_die_attrs. */
24469 if (!dwarf_split_debug_info)
24470 add_AT_pubnames (comp_unit_die ());
24472 if (dwarf_split_debug_info)
24474 int mark;
24475 unsigned char checksum[16];
24476 struct md5_ctx ctx;
24478 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24479 md5_init_ctx (&ctx);
24480 mark = 0;
24481 die_checksum (comp_unit_die (), &ctx, &mark);
24482 unmark_all_dies (comp_unit_die ());
24483 md5_finish_ctx (&ctx, checksum);
24485 /* Use the first 8 bytes of the checksum as the dwo_id,
24486 and add it to both comp-unit DIEs. */
24487 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24488 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24490 /* Add the base offset of the ranges table to the skeleton
24491 comp-unit DIE. */
24492 if (ranges_table_in_use)
24493 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24494 ranges_section_label);
24496 switch_to_section (debug_addr_section);
24497 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24498 output_addr_table ();
24501 /* Output the main compilation unit if non-empty or if .debug_macinfo
24502 or .debug_macro will be emitted. */
24503 output_comp_unit (comp_unit_die (), have_macinfo);
24505 if (dwarf_split_debug_info && info_section_emitted)
24506 output_skeleton_debug_sections (main_comp_unit_die);
24508 /* Output the abbreviation table. */
24509 if (abbrev_die_table_in_use != 1)
24511 switch_to_section (debug_abbrev_section);
24512 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24513 output_abbrev_section ();
24516 /* Output location list section if necessary. */
24517 if (have_location_lists)
24519 /* Output the location lists info. */
24520 switch_to_section (debug_loc_section);
24521 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24522 output_location_lists (comp_unit_die ());
24525 output_pubtables ();
24527 /* Output the address range information if a CU (.debug_info section)
24528 was emitted. We output an empty table even if we had no functions
24529 to put in it. This because the consumer has no way to tell the
24530 difference between an empty table that we omitted and failure to
24531 generate a table that would have contained data. */
24532 if (info_section_emitted)
24534 unsigned long aranges_length = size_of_aranges ();
24536 switch_to_section (debug_aranges_section);
24537 output_aranges (aranges_length);
24540 /* Output ranges section if necessary. */
24541 if (ranges_table_in_use)
24543 switch_to_section (debug_ranges_section);
24544 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24545 output_ranges ();
24548 /* Have to end the macro section. */
24549 if (have_macinfo)
24551 switch_to_section (debug_macinfo_section);
24552 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24553 output_macinfo ();
24554 dw2_asm_output_data (1, 0, "End compilation unit");
24557 /* Output the source line correspondence table. We must do this
24558 even if there is no line information. Otherwise, on an empty
24559 translation unit, we will generate a present, but empty,
24560 .debug_info section. IRIX 6.5 `nm' will then complain when
24561 examining the file. This is done late so that any filenames
24562 used by the debug_info section are marked as 'used'. */
24563 switch_to_section (debug_line_section);
24564 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24565 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24566 output_line_info (false);
24568 if (dwarf_split_debug_info && info_section_emitted)
24570 switch_to_section (debug_skeleton_line_section);
24571 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24572 output_line_info (true);
24575 /* If we emitted any indirect strings, output the string table too. */
24576 if (debug_str_hash || skeleton_debug_str_hash)
24577 output_indirect_strings ();
24580 #include "gt-dwarf2out.h"