Various gimple to gimple_call conversions in IPA
[official-gcc.git] / gcc / dwarf2out.c
blob00eba7896855993faf1274585e655659d36ec257
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 "rtlhash.h"
75 #include "insn-config.h"
76 #include "reload.h"
77 #include "function.h"
78 #include "output.h"
79 #include "expr.h"
80 #include "except.h"
81 #include "dwarf2.h"
82 #include "dwarf2out.h"
83 #include "dwarf2asm.h"
84 #include "toplev.h"
85 #include "md5.h"
86 #include "tm_p.h"
87 #include "diagnostic.h"
88 #include "tree-pretty-print.h"
89 #include "debug.h"
90 #include "target.h"
91 #include "common/common-target.h"
92 #include "langhooks.h"
93 #include "cgraph.h"
94 #include "input.h"
95 #include "ira.h"
96 #include "lra.h"
97 #include "dumpfile.h"
98 #include "opts.h"
99 #include "tree-dfa.h"
100 #include "gdb/gdb-index.h"
101 #include "rtl-iter.h"
103 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
104 static rtx_insn *last_var_location_insn;
105 static rtx_insn *cached_next_real_insn;
106 static void dwarf2out_decl (tree);
108 #ifdef VMS_DEBUGGING_INFO
109 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
111 /* Define this macro to be a nonzero value if the directory specifications
112 which are output in the debug info should end with a separator. */
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
114 /* Define this macro to evaluate to a nonzero value if GCC should refrain
115 from generating indirect strings in DWARF2 debug information, for instance
116 if your target is stuck with an old version of GDB that is unable to
117 process them properly or uses VMS Debug. */
118 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
119 #else
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
121 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
122 #endif
124 /* ??? Poison these here until it can be done generically. They've been
125 totally replaced in this file; make sure it stays that way. */
126 #undef DWARF2_UNWIND_INFO
127 #undef DWARF2_FRAME_INFO
128 #if (GCC_VERSION >= 3000)
129 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
130 #endif
132 /* The size of the target's pointer type. */
133 #ifndef PTR_SIZE
134 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
135 #endif
137 /* Array of RTXes referenced by the debugging information, which therefore
138 must be kept around forever. */
139 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
141 /* A pointer to the base of a list of incomplete types which might be
142 completed at some later time. incomplete_types_list needs to be a
143 vec<tree, va_gc> *because we want to tell the garbage collector about
144 it. */
145 static GTY(()) vec<tree, va_gc> *incomplete_types;
147 /* A pointer to the base of a table of references to declaration
148 scopes. This table is a display which tracks the nesting
149 of declaration scopes at the current scope and containing
150 scopes. This table is used to find the proper place to
151 define type declaration DIE's. */
152 static GTY(()) vec<tree, va_gc> *decl_scope_table;
154 /* Pointers to various DWARF2 sections. */
155 static GTY(()) section *debug_info_section;
156 static GTY(()) section *debug_skeleton_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_skeleton_abbrev_section;
159 static GTY(()) section *debug_aranges_section;
160 static GTY(()) section *debug_addr_section;
161 static GTY(()) section *debug_macinfo_section;
162 static GTY(()) section *debug_line_section;
163 static GTY(()) section *debug_skeleton_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_str_dwo_section;
169 static GTY(()) section *debug_str_offsets_section;
170 static GTY(()) section *debug_ranges_section;
171 static GTY(()) section *debug_frame_section;
173 /* Maximum size (in bytes) of an artificially generated label. */
174 #define MAX_ARTIFICIAL_LABEL_BYTES 30
176 /* According to the (draft) DWARF 3 specification, the initial length
177 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
178 bytes are 0xffffffff, followed by the length stored in the next 8
179 bytes.
181 However, the SGI/MIPS ABI uses an initial length which is equal to
182 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
184 #ifndef DWARF_INITIAL_LENGTH_SIZE
185 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
186 #endif
188 /* Round SIZE up to the nearest BOUNDARY. */
189 #define DWARF_ROUND(SIZE,BOUNDARY) \
190 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
192 /* CIE identifier. */
193 #if HOST_BITS_PER_WIDE_INT >= 64
194 #define DWARF_CIE_ID \
195 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
196 #else
197 #define DWARF_CIE_ID DW_CIE_ID
198 #endif
201 /* A vector for a table that contains frame description
202 information for each routine. */
203 #define NOT_INDEXED (-1U)
204 #define NO_INDEX_ASSIGNED (-2U)
206 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
208 struct GTY((for_user)) indirect_string_node {
209 const char *str;
210 unsigned int refcount;
211 enum dwarf_form form;
212 char *label;
213 unsigned int index;
216 struct indirect_string_hasher : ggc_hasher<indirect_string_node *>
218 typedef const char *compare_type;
220 static hashval_t hash (indirect_string_node *);
221 static bool equal (indirect_string_node *, const char *);
224 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
226 /* With split_debug_info, both the comp_dir and dwo_name go in the
227 main object file, rather than the dwo, similar to the force_direct
228 parameter elsewhere but with additional complications:
230 1) The string is needed in both the main object file and the dwo.
231 That is, the comp_dir and dwo_name will appear in both places.
233 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
234 DW_FORM_GNU_str_index.
236 3) GCC chooses the form to use late, depending on the size and
237 reference count.
239 Rather than forcing the all debug string handling functions and
240 callers to deal with these complications, simply use a separate,
241 special-cased string table for any attribute that should go in the
242 main object file. This limits the complexity to just the places
243 that need it. */
245 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
247 static GTY(()) int dw2_string_counter;
249 /* True if the compilation unit places functions in more than one section. */
250 static GTY(()) bool have_multiple_function_sections = false;
252 /* Whether the default text and cold text sections have been used at all. */
254 static GTY(()) bool text_section_used = false;
255 static GTY(()) bool cold_text_section_used = false;
257 /* The default cold text section. */
258 static GTY(()) section *cold_text_section;
260 /* The DIE for C++14 'auto' in a function return type. */
261 static GTY(()) dw_die_ref auto_die;
263 /* The DIE for C++14 'decltype(auto)' in a function return type. */
264 static GTY(()) dw_die_ref decltype_auto_die;
266 /* Forward declarations for functions defined in this file. */
268 static char *stripattributes (const char *);
269 static void output_call_frame_info (int);
270 static void dwarf2out_note_section_used (void);
272 /* Personality decl of current unit. Used only when assembler does not support
273 personality CFI. */
274 static GTY(()) rtx current_unit_personality;
276 /* Data and reference forms for relocatable data. */
277 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
278 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
280 #ifndef DEBUG_FRAME_SECTION
281 #define DEBUG_FRAME_SECTION ".debug_frame"
282 #endif
284 #ifndef FUNC_BEGIN_LABEL
285 #define FUNC_BEGIN_LABEL "LFB"
286 #endif
288 #ifndef FUNC_END_LABEL
289 #define FUNC_END_LABEL "LFE"
290 #endif
292 #ifndef PROLOGUE_END_LABEL
293 #define PROLOGUE_END_LABEL "LPE"
294 #endif
296 #ifndef EPILOGUE_BEGIN_LABEL
297 #define EPILOGUE_BEGIN_LABEL "LEB"
298 #endif
300 #ifndef FRAME_BEGIN_LABEL
301 #define FRAME_BEGIN_LABEL "Lframe"
302 #endif
303 #define CIE_AFTER_SIZE_LABEL "LSCIE"
304 #define CIE_END_LABEL "LECIE"
305 #define FDE_LABEL "LSFDE"
306 #define FDE_AFTER_SIZE_LABEL "LASFDE"
307 #define FDE_END_LABEL "LEFDE"
308 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
309 #define LINE_NUMBER_END_LABEL "LELT"
310 #define LN_PROLOG_AS_LABEL "LASLTP"
311 #define LN_PROLOG_END_LABEL "LELTP"
312 #define DIE_LABEL_PREFIX "DW"
314 /* Match the base name of a file to the base name of a compilation unit. */
316 static int
317 matches_main_base (const char *path)
319 /* Cache the last query. */
320 static const char *last_path = NULL;
321 static int last_match = 0;
322 if (path != last_path)
324 const char *base;
325 int length = base_of_path (path, &base);
326 last_path = path;
327 last_match = (length == main_input_baselength
328 && memcmp (base, main_input_basename, length) == 0);
330 return last_match;
333 #ifdef DEBUG_DEBUG_STRUCT
335 static int
336 dump_struct_debug (tree type, enum debug_info_usage usage,
337 enum debug_struct_file criterion, int generic,
338 int matches, int result)
340 /* Find the type name. */
341 tree type_decl = TYPE_STUB_DECL (type);
342 tree t = type_decl;
343 const char *name = 0;
344 if (TREE_CODE (t) == TYPE_DECL)
345 t = DECL_NAME (t);
346 if (t)
347 name = IDENTIFIER_POINTER (t);
349 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
350 criterion,
351 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
352 matches ? "bas" : "hdr",
353 generic ? "gen" : "ord",
354 usage == DINFO_USAGE_DFN ? ";" :
355 usage == DINFO_USAGE_DIR_USE ? "." : "*",
356 result,
357 (void*) type_decl, name);
358 return result;
360 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
361 dump_struct_debug (type, usage, criterion, generic, matches, result)
363 #else
365 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
366 (result)
368 #endif
370 /* Get the number of HOST_WIDE_INTs needed to represent the precision
371 of the number. */
373 static unsigned int
374 get_full_len (const wide_int &op)
376 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
377 / HOST_BITS_PER_WIDE_INT);
380 static bool
381 should_emit_struct_debug (tree type, enum debug_info_usage usage)
383 enum debug_struct_file criterion;
384 tree type_decl;
385 bool generic = lang_hooks.types.generic_p (type);
387 if (generic)
388 criterion = debug_struct_generic[usage];
389 else
390 criterion = debug_struct_ordinary[usage];
392 if (criterion == DINFO_STRUCT_FILE_NONE)
393 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
394 if (criterion == DINFO_STRUCT_FILE_ANY)
395 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
397 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
399 if (type_decl != NULL)
401 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
404 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
405 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
408 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
411 /* Return a pointer to a copy of the section string name S with all
412 attributes stripped off, and an asterisk prepended (for assemble_name). */
414 static inline char *
415 stripattributes (const char *s)
417 char *stripped = XNEWVEC (char, strlen (s) + 2);
418 char *p = stripped;
420 *p++ = '*';
422 while (*s && *s != ',')
423 *p++ = *s++;
425 *p = '\0';
426 return stripped;
429 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
430 switch to the data section instead, and write out a synthetic start label
431 for collect2 the first time around. */
433 static void
434 switch_to_eh_frame_section (bool back)
436 tree label;
438 #ifdef EH_FRAME_SECTION_NAME
439 if (eh_frame_section == 0)
441 int flags;
443 if (EH_TABLES_CAN_BE_READ_ONLY)
445 int fde_encoding;
446 int per_encoding;
447 int lsda_encoding;
449 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
450 /*global=*/0);
451 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
452 /*global=*/1);
453 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
454 /*global=*/0);
455 flags = ((! flag_pic
456 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
457 && (fde_encoding & 0x70) != DW_EH_PE_aligned
458 && (per_encoding & 0x70) != DW_EH_PE_absptr
459 && (per_encoding & 0x70) != DW_EH_PE_aligned
460 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
461 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
462 ? 0 : SECTION_WRITE);
464 else
465 flags = SECTION_WRITE;
466 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
468 #endif /* EH_FRAME_SECTION_NAME */
470 if (eh_frame_section)
471 switch_to_section (eh_frame_section);
472 else
474 /* We have no special eh_frame section. Put the information in
475 the data section and emit special labels to guide collect2. */
476 switch_to_section (data_section);
478 if (!back)
480 label = get_file_function_name ("F");
481 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
482 targetm.asm_out.globalize_label (asm_out_file,
483 IDENTIFIER_POINTER (label));
484 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
489 /* Switch [BACK] to the eh or debug frame table section, depending on
490 FOR_EH. */
492 static void
493 switch_to_frame_table_section (int for_eh, bool back)
495 if (for_eh)
496 switch_to_eh_frame_section (back);
497 else
499 if (!debug_frame_section)
500 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
501 SECTION_DEBUG, NULL);
502 switch_to_section (debug_frame_section);
506 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
508 enum dw_cfi_oprnd_type
509 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
511 switch (cfi)
513 case DW_CFA_nop:
514 case DW_CFA_GNU_window_save:
515 case DW_CFA_remember_state:
516 case DW_CFA_restore_state:
517 return dw_cfi_oprnd_unused;
519 case DW_CFA_set_loc:
520 case DW_CFA_advance_loc1:
521 case DW_CFA_advance_loc2:
522 case DW_CFA_advance_loc4:
523 case DW_CFA_MIPS_advance_loc8:
524 return dw_cfi_oprnd_addr;
526 case DW_CFA_offset:
527 case DW_CFA_offset_extended:
528 case DW_CFA_def_cfa:
529 case DW_CFA_offset_extended_sf:
530 case DW_CFA_def_cfa_sf:
531 case DW_CFA_restore:
532 case DW_CFA_restore_extended:
533 case DW_CFA_undefined:
534 case DW_CFA_same_value:
535 case DW_CFA_def_cfa_register:
536 case DW_CFA_register:
537 case DW_CFA_expression:
538 return dw_cfi_oprnd_reg_num;
540 case DW_CFA_def_cfa_offset:
541 case DW_CFA_GNU_args_size:
542 case DW_CFA_def_cfa_offset_sf:
543 return dw_cfi_oprnd_offset;
545 case DW_CFA_def_cfa_expression:
546 return dw_cfi_oprnd_loc;
548 default:
549 gcc_unreachable ();
553 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
555 enum dw_cfi_oprnd_type
556 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
558 switch (cfi)
560 case DW_CFA_def_cfa:
561 case DW_CFA_def_cfa_sf:
562 case DW_CFA_offset:
563 case DW_CFA_offset_extended_sf:
564 case DW_CFA_offset_extended:
565 return dw_cfi_oprnd_offset;
567 case DW_CFA_register:
568 return dw_cfi_oprnd_reg_num;
570 case DW_CFA_expression:
571 return dw_cfi_oprnd_loc;
573 default:
574 return dw_cfi_oprnd_unused;
578 /* Output one FDE. */
580 static void
581 output_fde (dw_fde_ref fde, bool for_eh, bool second,
582 char *section_start_label, int fde_encoding, char *augmentation,
583 bool any_lsda_needed, int lsda_encoding)
585 const char *begin, *end;
586 static unsigned int j;
587 char l1[20], l2[20];
589 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
590 /* empty */ 0);
591 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
592 for_eh + j);
593 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
594 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
595 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
596 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
597 " indicating 64-bit DWARF extension");
598 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
599 "FDE Length");
600 ASM_OUTPUT_LABEL (asm_out_file, l1);
602 if (for_eh)
603 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
604 else
605 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
606 debug_frame_section, "FDE CIE offset");
608 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
609 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
611 if (for_eh)
613 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
614 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
615 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
616 "FDE initial location");
617 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
618 end, begin, "FDE address range");
620 else
622 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
623 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
626 if (augmentation[0])
628 if (any_lsda_needed)
630 int size = size_of_encoded_value (lsda_encoding);
632 if (lsda_encoding == DW_EH_PE_aligned)
634 int offset = ( 4 /* Length */
635 + 4 /* CIE offset */
636 + 2 * size_of_encoded_value (fde_encoding)
637 + 1 /* Augmentation size */ );
638 int pad = -offset & (PTR_SIZE - 1);
640 size += pad;
641 gcc_assert (size_of_uleb128 (size) == 1);
644 dw2_asm_output_data_uleb128 (size, "Augmentation size");
646 if (fde->uses_eh_lsda)
648 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
649 fde->funcdef_number);
650 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
651 gen_rtx_SYMBOL_REF (Pmode, l1),
652 false,
653 "Language Specific Data Area");
655 else
657 if (lsda_encoding == DW_EH_PE_aligned)
658 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
659 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
660 "Language Specific Data Area (none)");
663 else
664 dw2_asm_output_data_uleb128 (0, "Augmentation size");
667 /* Loop through the Call Frame Instructions associated with this FDE. */
668 fde->dw_fde_current_label = begin;
670 size_t from, until, i;
672 from = 0;
673 until = vec_safe_length (fde->dw_fde_cfi);
675 if (fde->dw_fde_second_begin == NULL)
677 else if (!second)
678 until = fde->dw_fde_switch_cfi_index;
679 else
680 from = fde->dw_fde_switch_cfi_index;
682 for (i = from; i < until; i++)
683 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
686 /* If we are to emit a ref/link from function bodies to their frame tables,
687 do it now. This is typically performed to make sure that tables
688 associated with functions are dragged with them and not discarded in
689 garbage collecting links. We need to do this on a per function basis to
690 cope with -ffunction-sections. */
692 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
693 /* Switch to the function section, emit the ref to the tables, and
694 switch *back* into the table section. */
695 switch_to_section (function_section (fde->decl));
696 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
697 switch_to_frame_table_section (for_eh, true);
698 #endif
700 /* Pad the FDE out to an address sized boundary. */
701 ASM_OUTPUT_ALIGN (asm_out_file,
702 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
703 ASM_OUTPUT_LABEL (asm_out_file, l2);
705 j += 2;
708 /* Return true if frame description entry FDE is needed for EH. */
710 static bool
711 fde_needed_for_eh_p (dw_fde_ref fde)
713 if (flag_asynchronous_unwind_tables)
714 return true;
716 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
717 return true;
719 if (fde->uses_eh_lsda)
720 return true;
722 /* If exceptions are enabled, we have collected nothrow info. */
723 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
724 return false;
726 return true;
729 /* Output the call frame information used to record information
730 that relates to calculating the frame pointer, and records the
731 location of saved registers. */
733 static void
734 output_call_frame_info (int for_eh)
736 unsigned int i;
737 dw_fde_ref fde;
738 dw_cfi_ref cfi;
739 char l1[20], l2[20], section_start_label[20];
740 bool any_lsda_needed = false;
741 char augmentation[6];
742 int augmentation_size;
743 int fde_encoding = DW_EH_PE_absptr;
744 int per_encoding = DW_EH_PE_absptr;
745 int lsda_encoding = DW_EH_PE_absptr;
746 int return_reg;
747 rtx personality = NULL;
748 int dw_cie_version;
750 /* Don't emit a CIE if there won't be any FDEs. */
751 if (!fde_vec)
752 return;
754 /* Nothing to do if the assembler's doing it all. */
755 if (dwarf2out_do_cfi_asm ())
756 return;
758 /* If we don't have any functions we'll want to unwind out of, don't emit
759 any EH unwind information. If we make FDEs linkonce, we may have to
760 emit an empty label for an FDE that wouldn't otherwise be emitted. We
761 want to avoid having an FDE kept around when the function it refers to
762 is discarded. Example where this matters: a primary function template
763 in C++ requires EH information, an explicit specialization doesn't. */
764 if (for_eh)
766 bool any_eh_needed = false;
768 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
770 if (fde->uses_eh_lsda)
771 any_eh_needed = any_lsda_needed = true;
772 else if (fde_needed_for_eh_p (fde))
773 any_eh_needed = true;
774 else if (TARGET_USES_WEAK_UNWIND_INFO)
775 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
778 if (!any_eh_needed)
779 return;
782 /* We're going to be generating comments, so turn on app. */
783 if (flag_debug_asm)
784 app_enable ();
786 /* Switch to the proper frame section, first time. */
787 switch_to_frame_table_section (for_eh, false);
789 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
790 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
792 /* Output the CIE. */
793 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
794 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
795 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
796 dw2_asm_output_data (4, 0xffffffff,
797 "Initial length escape value indicating 64-bit DWARF extension");
798 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
799 "Length of Common Information Entry");
800 ASM_OUTPUT_LABEL (asm_out_file, l1);
802 /* Now that the CIE pointer is PC-relative for EH,
803 use 0 to identify the CIE. */
804 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
805 (for_eh ? 0 : DWARF_CIE_ID),
806 "CIE Identifier Tag");
808 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
809 use CIE version 1, unless that would produce incorrect results
810 due to overflowing the return register column. */
811 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
812 dw_cie_version = 1;
813 if (return_reg >= 256 || dwarf_version > 2)
814 dw_cie_version = 3;
815 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
817 augmentation[0] = 0;
818 augmentation_size = 0;
820 personality = current_unit_personality;
821 if (for_eh)
823 char *p;
825 /* Augmentation:
826 z Indicates that a uleb128 is present to size the
827 augmentation section.
828 L Indicates the encoding (and thus presence) of
829 an LSDA pointer in the FDE augmentation.
830 R Indicates a non-default pointer encoding for
831 FDE code pointers.
832 P Indicates the presence of an encoding + language
833 personality routine in the CIE augmentation. */
835 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
836 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
837 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
839 p = augmentation + 1;
840 if (personality)
842 *p++ = 'P';
843 augmentation_size += 1 + size_of_encoded_value (per_encoding);
844 assemble_external_libcall (personality);
846 if (any_lsda_needed)
848 *p++ = 'L';
849 augmentation_size += 1;
851 if (fde_encoding != DW_EH_PE_absptr)
853 *p++ = 'R';
854 augmentation_size += 1;
856 if (p > augmentation + 1)
858 augmentation[0] = 'z';
859 *p = '\0';
862 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
863 if (personality && per_encoding == DW_EH_PE_aligned)
865 int offset = ( 4 /* Length */
866 + 4 /* CIE Id */
867 + 1 /* CIE version */
868 + strlen (augmentation) + 1 /* Augmentation */
869 + size_of_uleb128 (1) /* Code alignment */
870 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
871 + 1 /* RA column */
872 + 1 /* Augmentation size */
873 + 1 /* Personality encoding */ );
874 int pad = -offset & (PTR_SIZE - 1);
876 augmentation_size += pad;
878 /* Augmentations should be small, so there's scarce need to
879 iterate for a solution. Die if we exceed one uleb128 byte. */
880 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
884 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
885 if (dw_cie_version >= 4)
887 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
888 dw2_asm_output_data (1, 0, "CIE Segment Size");
890 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
891 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
892 "CIE Data Alignment Factor");
894 if (dw_cie_version == 1)
895 dw2_asm_output_data (1, return_reg, "CIE RA Column");
896 else
897 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
899 if (augmentation[0])
901 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
902 if (personality)
904 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
905 eh_data_format_name (per_encoding));
906 dw2_asm_output_encoded_addr_rtx (per_encoding,
907 personality,
908 true, NULL);
911 if (any_lsda_needed)
912 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
913 eh_data_format_name (lsda_encoding));
915 if (fde_encoding != DW_EH_PE_absptr)
916 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
917 eh_data_format_name (fde_encoding));
920 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
921 output_cfi (cfi, NULL, for_eh);
923 /* Pad the CIE out to an address sized boundary. */
924 ASM_OUTPUT_ALIGN (asm_out_file,
925 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
926 ASM_OUTPUT_LABEL (asm_out_file, l2);
928 /* Loop through all of the FDE's. */
929 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
931 unsigned int k;
933 /* Don't emit EH unwind info for leaf functions that don't need it. */
934 if (for_eh && !fde_needed_for_eh_p (fde))
935 continue;
937 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
938 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
939 augmentation, any_lsda_needed, lsda_encoding);
942 if (for_eh && targetm.terminate_dw2_eh_frame_info)
943 dw2_asm_output_data (4, 0, "End of Table");
945 /* Turn off app to make assembly quicker. */
946 if (flag_debug_asm)
947 app_disable ();
950 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
952 static void
953 dwarf2out_do_cfi_startproc (bool second)
955 int enc;
956 rtx ref;
957 rtx personality = get_personality_function (current_function_decl);
959 fprintf (asm_out_file, "\t.cfi_startproc\n");
961 if (personality)
963 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
964 ref = personality;
966 /* ??? The GAS support isn't entirely consistent. We have to
967 handle indirect support ourselves, but PC-relative is done
968 in the assembler. Further, the assembler can't handle any
969 of the weirder relocation types. */
970 if (enc & DW_EH_PE_indirect)
971 ref = dw2_force_const_mem (ref, true);
973 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
974 output_addr_const (asm_out_file, ref);
975 fputc ('\n', asm_out_file);
978 if (crtl->uses_eh_lsda)
980 char lab[20];
982 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
983 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
984 current_function_funcdef_no);
985 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
986 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
988 if (enc & DW_EH_PE_indirect)
989 ref = dw2_force_const_mem (ref, true);
991 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
992 output_addr_const (asm_out_file, ref);
993 fputc ('\n', asm_out_file);
997 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
998 this allocation may be done before pass_final. */
1000 dw_fde_ref
1001 dwarf2out_alloc_current_fde (void)
1003 dw_fde_ref fde;
1005 fde = ggc_cleared_alloc<dw_fde_node> ();
1006 fde->decl = current_function_decl;
1007 fde->funcdef_number = current_function_funcdef_no;
1008 fde->fde_index = vec_safe_length (fde_vec);
1009 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1010 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1011 fde->nothrow = crtl->nothrow;
1012 fde->drap_reg = INVALID_REGNUM;
1013 fde->vdrap_reg = INVALID_REGNUM;
1015 /* Record the FDE associated with this function. */
1016 cfun->fde = fde;
1017 vec_safe_push (fde_vec, fde);
1019 return fde;
1022 /* Output a marker (i.e. a label) for the beginning of a function, before
1023 the prologue. */
1025 void
1026 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1027 const char *file ATTRIBUTE_UNUSED)
1029 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1030 char * dup_label;
1031 dw_fde_ref fde;
1032 section *fnsec;
1033 bool do_frame;
1035 current_function_func_begin_label = NULL;
1037 do_frame = dwarf2out_do_frame ();
1039 /* ??? current_function_func_begin_label is also used by except.c for
1040 call-site information. We must emit this label if it might be used. */
1041 if (!do_frame
1042 && (!flag_exceptions
1043 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1044 return;
1046 fnsec = function_section (current_function_decl);
1047 switch_to_section (fnsec);
1048 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1049 current_function_funcdef_no);
1050 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1051 current_function_funcdef_no);
1052 dup_label = xstrdup (label);
1053 current_function_func_begin_label = dup_label;
1055 /* We can elide the fde allocation if we're not emitting debug info. */
1056 if (!do_frame)
1057 return;
1059 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1060 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1061 would include pass_dwarf2_frame. If we've not created the FDE yet,
1062 do so now. */
1063 fde = cfun->fde;
1064 if (fde == NULL)
1065 fde = dwarf2out_alloc_current_fde ();
1067 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1068 fde->dw_fde_begin = dup_label;
1069 fde->dw_fde_current_label = dup_label;
1070 fde->in_std_section = (fnsec == text_section
1071 || (cold_text_section && fnsec == cold_text_section));
1073 /* We only want to output line number information for the genuine dwarf2
1074 prologue case, not the eh frame case. */
1075 #ifdef DWARF2_DEBUGGING_INFO
1076 if (file)
1077 dwarf2out_source_line (line, file, 0, true);
1078 #endif
1080 if (dwarf2out_do_cfi_asm ())
1081 dwarf2out_do_cfi_startproc (false);
1082 else
1084 rtx personality = get_personality_function (current_function_decl);
1085 if (!current_unit_personality)
1086 current_unit_personality = personality;
1088 /* We cannot keep a current personality per function as without CFI
1089 asm, at the point where we emit the CFI data, there is no current
1090 function anymore. */
1091 if (personality && current_unit_personality != personality)
1092 sorry ("multiple EH personalities are supported only with assemblers "
1093 "supporting .cfi_personality directive");
1097 /* Output a marker (i.e. a label) for the end of the generated code
1098 for a function prologue. This gets called *after* the prologue code has
1099 been generated. */
1101 void
1102 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1103 const char *file ATTRIBUTE_UNUSED)
1105 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1107 /* Output a label to mark the endpoint of the code generated for this
1108 function. */
1109 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1110 current_function_funcdef_no);
1111 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1112 current_function_funcdef_no);
1113 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1116 /* Output a marker (i.e. a label) for the beginning of the generated code
1117 for a function epilogue. This gets called *before* the prologue code has
1118 been generated. */
1120 void
1121 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1122 const char *file ATTRIBUTE_UNUSED)
1124 dw_fde_ref fde = cfun->fde;
1125 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1127 if (fde->dw_fde_vms_begin_epilogue)
1128 return;
1130 /* Output a label to mark the endpoint of the code generated for this
1131 function. */
1132 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1133 current_function_funcdef_no);
1134 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1135 current_function_funcdef_no);
1136 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1139 /* Output a marker (i.e. a label) for the absolute end of the generated code
1140 for a function definition. This gets called *after* the epilogue code has
1141 been generated. */
1143 void
1144 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1145 const char *file ATTRIBUTE_UNUSED)
1147 dw_fde_ref fde;
1148 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1150 last_var_location_insn = NULL;
1151 cached_next_real_insn = NULL;
1153 if (dwarf2out_do_cfi_asm ())
1154 fprintf (asm_out_file, "\t.cfi_endproc\n");
1156 /* Output a label to mark the endpoint of the code generated for this
1157 function. */
1158 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1159 current_function_funcdef_no);
1160 ASM_OUTPUT_LABEL (asm_out_file, label);
1161 fde = cfun->fde;
1162 gcc_assert (fde != NULL);
1163 if (fde->dw_fde_second_begin == NULL)
1164 fde->dw_fde_end = xstrdup (label);
1167 void
1168 dwarf2out_frame_finish (void)
1170 /* Output call frame information. */
1171 if (targetm.debug_unwind_info () == UI_DWARF2)
1172 output_call_frame_info (0);
1174 /* Output another copy for the unwinder. */
1175 if ((flag_unwind_tables || flag_exceptions)
1176 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1177 output_call_frame_info (1);
1180 /* Note that the current function section is being used for code. */
1182 static void
1183 dwarf2out_note_section_used (void)
1185 section *sec = current_function_section ();
1186 if (sec == text_section)
1187 text_section_used = true;
1188 else if (sec == cold_text_section)
1189 cold_text_section_used = true;
1192 static void var_location_switch_text_section (void);
1193 static void set_cur_line_info_table (section *);
1195 void
1196 dwarf2out_switch_text_section (void)
1198 section *sect;
1199 dw_fde_ref fde = cfun->fde;
1201 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1203 if (!in_cold_section_p)
1205 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1206 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1207 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1209 else
1211 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1212 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1213 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1215 have_multiple_function_sections = true;
1217 /* There is no need to mark used sections when not debugging. */
1218 if (cold_text_section != NULL)
1219 dwarf2out_note_section_used ();
1221 if (dwarf2out_do_cfi_asm ())
1222 fprintf (asm_out_file, "\t.cfi_endproc\n");
1224 /* Now do the real section switch. */
1225 sect = current_function_section ();
1226 switch_to_section (sect);
1228 fde->second_in_std_section
1229 = (sect == text_section
1230 || (cold_text_section && sect == cold_text_section));
1232 if (dwarf2out_do_cfi_asm ())
1233 dwarf2out_do_cfi_startproc (true);
1235 var_location_switch_text_section ();
1237 if (cold_text_section != NULL)
1238 set_cur_line_info_table (sect);
1241 /* And now, the subset of the debugging information support code necessary
1242 for emitting location expressions. */
1244 /* Data about a single source file. */
1245 struct GTY((for_user)) dwarf_file_data {
1246 const char * filename;
1247 int emitted_number;
1250 typedef struct GTY(()) deferred_locations_struct
1252 tree variable;
1253 dw_die_ref die;
1254 } deferred_locations;
1257 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1260 /* Describe an entry into the .debug_addr section. */
1262 enum ate_kind {
1263 ate_kind_rtx,
1264 ate_kind_rtx_dtprel,
1265 ate_kind_label
1268 typedef struct GTY((for_user)) addr_table_entry_struct {
1269 enum ate_kind kind;
1270 unsigned int refcount;
1271 unsigned int index;
1272 union addr_table_entry_struct_union
1274 rtx GTY ((tag ("0"))) rtl;
1275 char * GTY ((tag ("1"))) label;
1277 GTY ((desc ("%1.kind"))) addr;
1279 addr_table_entry;
1281 /* Location lists are ranges + location descriptions for that range,
1282 so you can track variables that are in different places over
1283 their entire life. */
1284 typedef struct GTY(()) dw_loc_list_struct {
1285 dw_loc_list_ref dw_loc_next;
1286 const char *begin; /* Label and addr_entry for start of range */
1287 addr_table_entry *begin_entry;
1288 const char *end; /* Label for end of range */
1289 char *ll_symbol; /* Label for beginning of location list.
1290 Only on head of list */
1291 const char *section; /* Section this loclist is relative to */
1292 dw_loc_descr_ref expr;
1293 hashval_t hash;
1294 /* True if all addresses in this and subsequent lists are known to be
1295 resolved. */
1296 bool resolved_addr;
1297 /* True if this list has been replaced by dw_loc_next. */
1298 bool replaced;
1299 bool emitted;
1300 /* True if the range should be emitted even if begin and end
1301 are the same. */
1302 bool force;
1303 } dw_loc_list_node;
1305 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1307 /* Convert a DWARF stack opcode into its string name. */
1309 static const char *
1310 dwarf_stack_op_name (unsigned int op)
1312 const char *name = get_DW_OP_name (op);
1314 if (name != NULL)
1315 return name;
1317 return "OP_<unknown>";
1320 /* Return a pointer to a newly allocated location description. Location
1321 descriptions are simple expression terms that can be strung
1322 together to form more complicated location (address) descriptions. */
1324 static inline dw_loc_descr_ref
1325 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1326 unsigned HOST_WIDE_INT oprnd2)
1328 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1330 descr->dw_loc_opc = op;
1331 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd1.val_entry = NULL;
1333 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1334 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1335 descr->dw_loc_oprnd2.val_entry = NULL;
1336 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1338 return descr;
1341 /* Return a pointer to a newly allocated location description for
1342 REG and OFFSET. */
1344 static inline dw_loc_descr_ref
1345 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1347 if (reg <= 31)
1348 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1349 offset, 0);
1350 else
1351 return new_loc_descr (DW_OP_bregx, reg, offset);
1354 /* Add a location description term to a location description expression. */
1356 static inline void
1357 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1359 dw_loc_descr_ref *d;
1361 /* Find the end of the chain. */
1362 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1365 *d = descr;
1368 /* Compare two location operands for exact equality. */
1370 static bool
1371 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1373 if (a->val_class != b->val_class)
1374 return false;
1375 switch (a->val_class)
1377 case dw_val_class_none:
1378 return true;
1379 case dw_val_class_addr:
1380 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1382 case dw_val_class_offset:
1383 case dw_val_class_unsigned_const:
1384 case dw_val_class_const:
1385 case dw_val_class_range_list:
1386 case dw_val_class_lineptr:
1387 case dw_val_class_macptr:
1388 /* These are all HOST_WIDE_INT, signed or unsigned. */
1389 return a->v.val_unsigned == b->v.val_unsigned;
1391 case dw_val_class_loc:
1392 return a->v.val_loc == b->v.val_loc;
1393 case dw_val_class_loc_list:
1394 return a->v.val_loc_list == b->v.val_loc_list;
1395 case dw_val_class_die_ref:
1396 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1397 case dw_val_class_fde_ref:
1398 return a->v.val_fde_index == b->v.val_fde_index;
1399 case dw_val_class_lbl_id:
1400 case dw_val_class_high_pc:
1401 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1402 case dw_val_class_str:
1403 return a->v.val_str == b->v.val_str;
1404 case dw_val_class_flag:
1405 return a->v.val_flag == b->v.val_flag;
1406 case dw_val_class_file:
1407 return a->v.val_file == b->v.val_file;
1408 case dw_val_class_decl_ref:
1409 return a->v.val_decl_ref == b->v.val_decl_ref;
1411 case dw_val_class_const_double:
1412 return (a->v.val_double.high == b->v.val_double.high
1413 && a->v.val_double.low == b->v.val_double.low);
1415 case dw_val_class_wide_int:
1416 return *a->v.val_wide == *b->v.val_wide;
1418 case dw_val_class_vec:
1420 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1421 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1423 return (a_len == b_len
1424 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1427 case dw_val_class_data8:
1428 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1430 case dw_val_class_vms_delta:
1431 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1432 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1434 gcc_unreachable ();
1437 /* Compare two location atoms for exact equality. */
1439 static bool
1440 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1442 if (a->dw_loc_opc != b->dw_loc_opc)
1443 return false;
1445 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1446 address size, but since we always allocate cleared storage it
1447 should be zero for other types of locations. */
1448 if (a->dtprel != b->dtprel)
1449 return false;
1451 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1452 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1455 /* Compare two complete location expressions for exact equality. */
1457 bool
1458 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1460 while (1)
1462 if (a == b)
1463 return true;
1464 if (a == NULL || b == NULL)
1465 return false;
1466 if (!loc_descr_equal_p_1 (a, b))
1467 return false;
1469 a = a->dw_loc_next;
1470 b = b->dw_loc_next;
1475 /* Add a constant OFFSET to a location expression. */
1477 static void
1478 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1480 dw_loc_descr_ref loc;
1481 HOST_WIDE_INT *p;
1483 gcc_assert (*list_head != NULL);
1485 if (!offset)
1486 return;
1488 /* Find the end of the chain. */
1489 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1492 p = NULL;
1493 if (loc->dw_loc_opc == DW_OP_fbreg
1494 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1495 p = &loc->dw_loc_oprnd1.v.val_int;
1496 else if (loc->dw_loc_opc == DW_OP_bregx)
1497 p = &loc->dw_loc_oprnd2.v.val_int;
1499 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1500 offset. Don't optimize if an signed integer overflow would happen. */
1501 if (p != NULL
1502 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1503 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1504 *p += offset;
1506 else if (offset > 0)
1507 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1509 else
1511 loc->dw_loc_next = int_loc_descriptor (-offset);
1512 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1516 /* Add a constant OFFSET to a location list. */
1518 static void
1519 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1521 dw_loc_list_ref d;
1522 for (d = list_head; d != NULL; d = d->dw_loc_next)
1523 loc_descr_plus_const (&d->expr, offset);
1526 #define DWARF_REF_SIZE \
1527 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1529 static unsigned long int get_base_type_offset (dw_die_ref);
1531 /* Return the size of a location descriptor. */
1533 static unsigned long
1534 size_of_loc_descr (dw_loc_descr_ref loc)
1536 unsigned long size = 1;
1538 switch (loc->dw_loc_opc)
1540 case DW_OP_addr:
1541 size += DWARF2_ADDR_SIZE;
1542 break;
1543 case DW_OP_GNU_addr_index:
1544 case DW_OP_GNU_const_index:
1545 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1546 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1547 break;
1548 case DW_OP_const1u:
1549 case DW_OP_const1s:
1550 size += 1;
1551 break;
1552 case DW_OP_const2u:
1553 case DW_OP_const2s:
1554 size += 2;
1555 break;
1556 case DW_OP_const4u:
1557 case DW_OP_const4s:
1558 size += 4;
1559 break;
1560 case DW_OP_const8u:
1561 case DW_OP_const8s:
1562 size += 8;
1563 break;
1564 case DW_OP_constu:
1565 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1566 break;
1567 case DW_OP_consts:
1568 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1569 break;
1570 case DW_OP_pick:
1571 size += 1;
1572 break;
1573 case DW_OP_plus_uconst:
1574 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1575 break;
1576 case DW_OP_skip:
1577 case DW_OP_bra:
1578 size += 2;
1579 break;
1580 case DW_OP_breg0:
1581 case DW_OP_breg1:
1582 case DW_OP_breg2:
1583 case DW_OP_breg3:
1584 case DW_OP_breg4:
1585 case DW_OP_breg5:
1586 case DW_OP_breg6:
1587 case DW_OP_breg7:
1588 case DW_OP_breg8:
1589 case DW_OP_breg9:
1590 case DW_OP_breg10:
1591 case DW_OP_breg11:
1592 case DW_OP_breg12:
1593 case DW_OP_breg13:
1594 case DW_OP_breg14:
1595 case DW_OP_breg15:
1596 case DW_OP_breg16:
1597 case DW_OP_breg17:
1598 case DW_OP_breg18:
1599 case DW_OP_breg19:
1600 case DW_OP_breg20:
1601 case DW_OP_breg21:
1602 case DW_OP_breg22:
1603 case DW_OP_breg23:
1604 case DW_OP_breg24:
1605 case DW_OP_breg25:
1606 case DW_OP_breg26:
1607 case DW_OP_breg27:
1608 case DW_OP_breg28:
1609 case DW_OP_breg29:
1610 case DW_OP_breg30:
1611 case DW_OP_breg31:
1612 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1613 break;
1614 case DW_OP_regx:
1615 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1616 break;
1617 case DW_OP_fbreg:
1618 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1619 break;
1620 case DW_OP_bregx:
1621 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1622 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1623 break;
1624 case DW_OP_piece:
1625 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1626 break;
1627 case DW_OP_bit_piece:
1628 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1629 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1630 break;
1631 case DW_OP_deref_size:
1632 case DW_OP_xderef_size:
1633 size += 1;
1634 break;
1635 case DW_OP_call2:
1636 size += 2;
1637 break;
1638 case DW_OP_call4:
1639 size += 4;
1640 break;
1641 case DW_OP_call_ref:
1642 size += DWARF_REF_SIZE;
1643 break;
1644 case DW_OP_implicit_value:
1645 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1646 + loc->dw_loc_oprnd1.v.val_unsigned;
1647 break;
1648 case DW_OP_GNU_implicit_pointer:
1649 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1650 break;
1651 case DW_OP_GNU_entry_value:
1653 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1654 size += size_of_uleb128 (op_size) + op_size;
1655 break;
1657 case DW_OP_GNU_const_type:
1659 unsigned long o
1660 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1661 size += size_of_uleb128 (o) + 1;
1662 switch (loc->dw_loc_oprnd2.val_class)
1664 case dw_val_class_vec:
1665 size += loc->dw_loc_oprnd2.v.val_vec.length
1666 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1667 break;
1668 case dw_val_class_const:
1669 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1670 break;
1671 case dw_val_class_const_double:
1672 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1673 break;
1674 case dw_val_class_wide_int:
1675 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1676 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1677 break;
1678 default:
1679 gcc_unreachable ();
1681 break;
1683 case DW_OP_GNU_regval_type:
1685 unsigned long o
1686 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1687 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1688 + size_of_uleb128 (o);
1690 break;
1691 case DW_OP_GNU_deref_type:
1693 unsigned long o
1694 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1695 size += 1 + size_of_uleb128 (o);
1697 break;
1698 case DW_OP_GNU_convert:
1699 case DW_OP_GNU_reinterpret:
1700 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1701 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1702 else
1704 unsigned long o
1705 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1706 size += size_of_uleb128 (o);
1708 break;
1709 case DW_OP_GNU_parameter_ref:
1710 size += 4;
1711 break;
1712 default:
1713 break;
1716 return size;
1719 /* Return the size of a series of location descriptors. */
1721 unsigned long
1722 size_of_locs (dw_loc_descr_ref loc)
1724 dw_loc_descr_ref l;
1725 unsigned long size;
1727 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1728 field, to avoid writing to a PCH file. */
1729 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1731 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1732 break;
1733 size += size_of_loc_descr (l);
1735 if (! l)
1736 return size;
1738 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1740 l->dw_loc_addr = size;
1741 size += size_of_loc_descr (l);
1744 return size;
1747 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1748 static void get_ref_die_offset_label (char *, dw_die_ref);
1749 static unsigned long int get_ref_die_offset (dw_die_ref);
1751 /* Output location description stack opcode's operands (if any).
1752 The for_eh_or_skip parameter controls whether register numbers are
1753 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1754 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1755 info). This should be suppressed for the cases that have not been converted
1756 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1758 static void
1759 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1761 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1762 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1764 switch (loc->dw_loc_opc)
1766 #ifdef DWARF2_DEBUGGING_INFO
1767 case DW_OP_const2u:
1768 case DW_OP_const2s:
1769 dw2_asm_output_data (2, val1->v.val_int, NULL);
1770 break;
1771 case DW_OP_const4u:
1772 if (loc->dtprel)
1774 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1775 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1776 val1->v.val_addr);
1777 fputc ('\n', asm_out_file);
1778 break;
1780 /* FALLTHRU */
1781 case DW_OP_const4s:
1782 dw2_asm_output_data (4, val1->v.val_int, NULL);
1783 break;
1784 case DW_OP_const8u:
1785 if (loc->dtprel)
1787 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1788 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1789 val1->v.val_addr);
1790 fputc ('\n', asm_out_file);
1791 break;
1793 /* FALLTHRU */
1794 case DW_OP_const8s:
1795 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1796 dw2_asm_output_data (8, val1->v.val_int, NULL);
1797 break;
1798 case DW_OP_skip:
1799 case DW_OP_bra:
1801 int offset;
1803 gcc_assert (val1->val_class == dw_val_class_loc);
1804 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1806 dw2_asm_output_data (2, offset, NULL);
1808 break;
1809 case DW_OP_implicit_value:
1810 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1811 switch (val2->val_class)
1813 case dw_val_class_const:
1814 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1815 break;
1816 case dw_val_class_vec:
1818 unsigned int elt_size = val2->v.val_vec.elt_size;
1819 unsigned int len = val2->v.val_vec.length;
1820 unsigned int i;
1821 unsigned char *p;
1823 if (elt_size > sizeof (HOST_WIDE_INT))
1825 elt_size /= 2;
1826 len *= 2;
1828 for (i = 0, p = val2->v.val_vec.array;
1829 i < len;
1830 i++, p += elt_size)
1831 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1832 "fp or vector constant word %u", i);
1834 break;
1835 case dw_val_class_const_double:
1837 unsigned HOST_WIDE_INT first, second;
1839 if (WORDS_BIG_ENDIAN)
1841 first = val2->v.val_double.high;
1842 second = val2->v.val_double.low;
1844 else
1846 first = val2->v.val_double.low;
1847 second = val2->v.val_double.high;
1849 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1850 first, NULL);
1851 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1852 second, NULL);
1854 break;
1855 case dw_val_class_wide_int:
1857 int i;
1858 int len = get_full_len (*val2->v.val_wide);
1859 if (WORDS_BIG_ENDIAN)
1860 for (i = len - 1; i >= 0; --i)
1861 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1862 val2->v.val_wide->elt (i), NULL);
1863 else
1864 for (i = 0; i < len; ++i)
1865 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1866 val2->v.val_wide->elt (i), NULL);
1868 break;
1869 case dw_val_class_addr:
1870 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1871 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1872 break;
1873 default:
1874 gcc_unreachable ();
1876 break;
1877 #else
1878 case DW_OP_const2u:
1879 case DW_OP_const2s:
1880 case DW_OP_const4u:
1881 case DW_OP_const4s:
1882 case DW_OP_const8u:
1883 case DW_OP_const8s:
1884 case DW_OP_skip:
1885 case DW_OP_bra:
1886 case DW_OP_implicit_value:
1887 /* We currently don't make any attempt to make sure these are
1888 aligned properly like we do for the main unwind info, so
1889 don't support emitting things larger than a byte if we're
1890 only doing unwinding. */
1891 gcc_unreachable ();
1892 #endif
1893 case DW_OP_const1u:
1894 case DW_OP_const1s:
1895 dw2_asm_output_data (1, val1->v.val_int, NULL);
1896 break;
1897 case DW_OP_constu:
1898 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1899 break;
1900 case DW_OP_consts:
1901 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1902 break;
1903 case DW_OP_pick:
1904 dw2_asm_output_data (1, val1->v.val_int, NULL);
1905 break;
1906 case DW_OP_plus_uconst:
1907 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1908 break;
1909 case DW_OP_breg0:
1910 case DW_OP_breg1:
1911 case DW_OP_breg2:
1912 case DW_OP_breg3:
1913 case DW_OP_breg4:
1914 case DW_OP_breg5:
1915 case DW_OP_breg6:
1916 case DW_OP_breg7:
1917 case DW_OP_breg8:
1918 case DW_OP_breg9:
1919 case DW_OP_breg10:
1920 case DW_OP_breg11:
1921 case DW_OP_breg12:
1922 case DW_OP_breg13:
1923 case DW_OP_breg14:
1924 case DW_OP_breg15:
1925 case DW_OP_breg16:
1926 case DW_OP_breg17:
1927 case DW_OP_breg18:
1928 case DW_OP_breg19:
1929 case DW_OP_breg20:
1930 case DW_OP_breg21:
1931 case DW_OP_breg22:
1932 case DW_OP_breg23:
1933 case DW_OP_breg24:
1934 case DW_OP_breg25:
1935 case DW_OP_breg26:
1936 case DW_OP_breg27:
1937 case DW_OP_breg28:
1938 case DW_OP_breg29:
1939 case DW_OP_breg30:
1940 case DW_OP_breg31:
1941 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1942 break;
1943 case DW_OP_regx:
1945 unsigned r = val1->v.val_unsigned;
1946 if (for_eh_or_skip >= 0)
1947 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1948 gcc_assert (size_of_uleb128 (r)
1949 == size_of_uleb128 (val1->v.val_unsigned));
1950 dw2_asm_output_data_uleb128 (r, NULL);
1952 break;
1953 case DW_OP_fbreg:
1954 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1955 break;
1956 case DW_OP_bregx:
1958 unsigned r = val1->v.val_unsigned;
1959 if (for_eh_or_skip >= 0)
1960 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1961 gcc_assert (size_of_uleb128 (r)
1962 == size_of_uleb128 (val1->v.val_unsigned));
1963 dw2_asm_output_data_uleb128 (r, NULL);
1964 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1966 break;
1967 case DW_OP_piece:
1968 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1969 break;
1970 case DW_OP_bit_piece:
1971 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1972 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1973 break;
1974 case DW_OP_deref_size:
1975 case DW_OP_xderef_size:
1976 dw2_asm_output_data (1, val1->v.val_int, NULL);
1977 break;
1979 case DW_OP_addr:
1980 if (loc->dtprel)
1982 if (targetm.asm_out.output_dwarf_dtprel)
1984 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1985 DWARF2_ADDR_SIZE,
1986 val1->v.val_addr);
1987 fputc ('\n', asm_out_file);
1989 else
1990 gcc_unreachable ();
1992 else
1994 #ifdef DWARF2_DEBUGGING_INFO
1995 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1996 #else
1997 gcc_unreachable ();
1998 #endif
2000 break;
2002 case DW_OP_GNU_addr_index:
2003 case DW_OP_GNU_const_index:
2004 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2005 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2006 "(index into .debug_addr)");
2007 break;
2009 case DW_OP_GNU_implicit_pointer:
2011 char label[MAX_ARTIFICIAL_LABEL_BYTES
2012 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2013 gcc_assert (val1->val_class == dw_val_class_die_ref);
2014 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2015 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2016 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2018 break;
2020 case DW_OP_GNU_entry_value:
2021 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2022 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2023 break;
2025 case DW_OP_GNU_const_type:
2027 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2028 gcc_assert (o);
2029 dw2_asm_output_data_uleb128 (o, NULL);
2030 switch (val2->val_class)
2032 case dw_val_class_const:
2033 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2034 dw2_asm_output_data (1, l, NULL);
2035 dw2_asm_output_data (l, val2->v.val_int, NULL);
2036 break;
2037 case dw_val_class_vec:
2039 unsigned int elt_size = val2->v.val_vec.elt_size;
2040 unsigned int len = val2->v.val_vec.length;
2041 unsigned int i;
2042 unsigned char *p;
2044 l = len * elt_size;
2045 dw2_asm_output_data (1, l, NULL);
2046 if (elt_size > sizeof (HOST_WIDE_INT))
2048 elt_size /= 2;
2049 len *= 2;
2051 for (i = 0, p = val2->v.val_vec.array;
2052 i < len;
2053 i++, p += elt_size)
2054 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2055 "fp or vector constant word %u", i);
2057 break;
2058 case dw_val_class_const_double:
2060 unsigned HOST_WIDE_INT first, second;
2061 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2063 dw2_asm_output_data (1, 2 * l, NULL);
2064 if (WORDS_BIG_ENDIAN)
2066 first = val2->v.val_double.high;
2067 second = val2->v.val_double.low;
2069 else
2071 first = val2->v.val_double.low;
2072 second = val2->v.val_double.high;
2074 dw2_asm_output_data (l, first, NULL);
2075 dw2_asm_output_data (l, second, NULL);
2077 break;
2078 case dw_val_class_wide_int:
2080 int i;
2081 int len = get_full_len (*val2->v.val_wide);
2082 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2084 dw2_asm_output_data (1, len * l, NULL);
2085 if (WORDS_BIG_ENDIAN)
2086 for (i = len - 1; i >= 0; --i)
2087 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2088 else
2089 for (i = 0; i < len; ++i)
2090 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2092 break;
2093 default:
2094 gcc_unreachable ();
2097 break;
2098 case DW_OP_GNU_regval_type:
2100 unsigned r = val1->v.val_unsigned;
2101 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2102 gcc_assert (o);
2103 if (for_eh_or_skip >= 0)
2105 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2106 gcc_assert (size_of_uleb128 (r)
2107 == size_of_uleb128 (val1->v.val_unsigned));
2109 dw2_asm_output_data_uleb128 (r, NULL);
2110 dw2_asm_output_data_uleb128 (o, NULL);
2112 break;
2113 case DW_OP_GNU_deref_type:
2115 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2116 gcc_assert (o);
2117 dw2_asm_output_data (1, val1->v.val_int, NULL);
2118 dw2_asm_output_data_uleb128 (o, NULL);
2120 break;
2121 case DW_OP_GNU_convert:
2122 case DW_OP_GNU_reinterpret:
2123 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2124 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2125 else
2127 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2128 gcc_assert (o);
2129 dw2_asm_output_data_uleb128 (o, NULL);
2131 break;
2133 case DW_OP_GNU_parameter_ref:
2135 unsigned long o;
2136 gcc_assert (val1->val_class == dw_val_class_die_ref);
2137 o = get_ref_die_offset (val1->v.val_die_ref.die);
2138 dw2_asm_output_data (4, o, NULL);
2140 break;
2142 default:
2143 /* Other codes have no operands. */
2144 break;
2148 /* Output a sequence of location operations.
2149 The for_eh_or_skip parameter controls whether register numbers are
2150 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2151 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2152 info). This should be suppressed for the cases that have not been converted
2153 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2155 void
2156 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2158 for (; loc != NULL; loc = loc->dw_loc_next)
2160 enum dwarf_location_atom opc = loc->dw_loc_opc;
2161 /* Output the opcode. */
2162 if (for_eh_or_skip >= 0
2163 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2165 unsigned r = (opc - DW_OP_breg0);
2166 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2167 gcc_assert (r <= 31);
2168 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2170 else if (for_eh_or_skip >= 0
2171 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2173 unsigned r = (opc - DW_OP_reg0);
2174 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2175 gcc_assert (r <= 31);
2176 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2179 dw2_asm_output_data (1, opc,
2180 "%s", dwarf_stack_op_name (opc));
2182 /* Output the operand(s) (if any). */
2183 output_loc_operands (loc, for_eh_or_skip);
2187 /* Output location description stack opcode's operands (if any).
2188 The output is single bytes on a line, suitable for .cfi_escape. */
2190 static void
2191 output_loc_operands_raw (dw_loc_descr_ref loc)
2193 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2194 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2196 switch (loc->dw_loc_opc)
2198 case DW_OP_addr:
2199 case DW_OP_GNU_addr_index:
2200 case DW_OP_GNU_const_index:
2201 case DW_OP_implicit_value:
2202 /* We cannot output addresses in .cfi_escape, only bytes. */
2203 gcc_unreachable ();
2205 case DW_OP_const1u:
2206 case DW_OP_const1s:
2207 case DW_OP_pick:
2208 case DW_OP_deref_size:
2209 case DW_OP_xderef_size:
2210 fputc (',', asm_out_file);
2211 dw2_asm_output_data_raw (1, val1->v.val_int);
2212 break;
2214 case DW_OP_const2u:
2215 case DW_OP_const2s:
2216 fputc (',', asm_out_file);
2217 dw2_asm_output_data_raw (2, val1->v.val_int);
2218 break;
2220 case DW_OP_const4u:
2221 case DW_OP_const4s:
2222 fputc (',', asm_out_file);
2223 dw2_asm_output_data_raw (4, val1->v.val_int);
2224 break;
2226 case DW_OP_const8u:
2227 case DW_OP_const8s:
2228 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2229 fputc (',', asm_out_file);
2230 dw2_asm_output_data_raw (8, val1->v.val_int);
2231 break;
2233 case DW_OP_skip:
2234 case DW_OP_bra:
2236 int offset;
2238 gcc_assert (val1->val_class == dw_val_class_loc);
2239 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2241 fputc (',', asm_out_file);
2242 dw2_asm_output_data_raw (2, offset);
2244 break;
2246 case DW_OP_regx:
2248 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2249 gcc_assert (size_of_uleb128 (r)
2250 == size_of_uleb128 (val1->v.val_unsigned));
2251 fputc (',', asm_out_file);
2252 dw2_asm_output_data_uleb128_raw (r);
2254 break;
2256 case DW_OP_constu:
2257 case DW_OP_plus_uconst:
2258 case DW_OP_piece:
2259 fputc (',', asm_out_file);
2260 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2261 break;
2263 case DW_OP_bit_piece:
2264 fputc (',', asm_out_file);
2265 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2266 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2267 break;
2269 case DW_OP_consts:
2270 case DW_OP_breg0:
2271 case DW_OP_breg1:
2272 case DW_OP_breg2:
2273 case DW_OP_breg3:
2274 case DW_OP_breg4:
2275 case DW_OP_breg5:
2276 case DW_OP_breg6:
2277 case DW_OP_breg7:
2278 case DW_OP_breg8:
2279 case DW_OP_breg9:
2280 case DW_OP_breg10:
2281 case DW_OP_breg11:
2282 case DW_OP_breg12:
2283 case DW_OP_breg13:
2284 case DW_OP_breg14:
2285 case DW_OP_breg15:
2286 case DW_OP_breg16:
2287 case DW_OP_breg17:
2288 case DW_OP_breg18:
2289 case DW_OP_breg19:
2290 case DW_OP_breg20:
2291 case DW_OP_breg21:
2292 case DW_OP_breg22:
2293 case DW_OP_breg23:
2294 case DW_OP_breg24:
2295 case DW_OP_breg25:
2296 case DW_OP_breg26:
2297 case DW_OP_breg27:
2298 case DW_OP_breg28:
2299 case DW_OP_breg29:
2300 case DW_OP_breg30:
2301 case DW_OP_breg31:
2302 case DW_OP_fbreg:
2303 fputc (',', asm_out_file);
2304 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2305 break;
2307 case DW_OP_bregx:
2309 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2310 gcc_assert (size_of_uleb128 (r)
2311 == size_of_uleb128 (val1->v.val_unsigned));
2312 fputc (',', asm_out_file);
2313 dw2_asm_output_data_uleb128_raw (r);
2314 fputc (',', asm_out_file);
2315 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2317 break;
2319 case DW_OP_GNU_implicit_pointer:
2320 case DW_OP_GNU_entry_value:
2321 case DW_OP_GNU_const_type:
2322 case DW_OP_GNU_regval_type:
2323 case DW_OP_GNU_deref_type:
2324 case DW_OP_GNU_convert:
2325 case DW_OP_GNU_reinterpret:
2326 case DW_OP_GNU_parameter_ref:
2327 gcc_unreachable ();
2328 break;
2330 default:
2331 /* Other codes have no operands. */
2332 break;
2336 void
2337 output_loc_sequence_raw (dw_loc_descr_ref loc)
2339 while (1)
2341 enum dwarf_location_atom opc = loc->dw_loc_opc;
2342 /* Output the opcode. */
2343 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2345 unsigned r = (opc - DW_OP_breg0);
2346 r = DWARF2_FRAME_REG_OUT (r, 1);
2347 gcc_assert (r <= 31);
2348 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2350 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2352 unsigned r = (opc - DW_OP_reg0);
2353 r = DWARF2_FRAME_REG_OUT (r, 1);
2354 gcc_assert (r <= 31);
2355 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2357 /* Output the opcode. */
2358 fprintf (asm_out_file, "%#x", opc);
2359 output_loc_operands_raw (loc);
2361 if (!loc->dw_loc_next)
2362 break;
2363 loc = loc->dw_loc_next;
2365 fputc (',', asm_out_file);
2369 /* This function builds a dwarf location descriptor sequence from a
2370 dw_cfa_location, adding the given OFFSET to the result of the
2371 expression. */
2373 struct dw_loc_descr_node *
2374 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2376 struct dw_loc_descr_node *head, *tmp;
2378 offset += cfa->offset;
2380 if (cfa->indirect)
2382 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2383 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2384 head->dw_loc_oprnd1.val_entry = NULL;
2385 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2386 add_loc_descr (&head, tmp);
2387 if (offset != 0)
2389 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2390 add_loc_descr (&head, tmp);
2393 else
2394 head = new_reg_loc_descr (cfa->reg, offset);
2396 return head;
2399 /* This function builds a dwarf location descriptor sequence for
2400 the address at OFFSET from the CFA when stack is aligned to
2401 ALIGNMENT byte. */
2403 struct dw_loc_descr_node *
2404 build_cfa_aligned_loc (dw_cfa_location *cfa,
2405 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2407 struct dw_loc_descr_node *head;
2408 unsigned int dwarf_fp
2409 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2411 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2412 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2414 head = new_reg_loc_descr (dwarf_fp, 0);
2415 add_loc_descr (&head, int_loc_descriptor (alignment));
2416 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2417 loc_descr_plus_const (&head, offset);
2419 else
2420 head = new_reg_loc_descr (dwarf_fp, offset);
2421 return head;
2424 /* And now, the support for symbolic debugging information. */
2426 /* .debug_str support. */
2428 static void dwarf2out_init (const char *);
2429 static void dwarf2out_finish (const char *);
2430 static void dwarf2out_assembly_start (void);
2431 static void dwarf2out_define (unsigned int, const char *);
2432 static void dwarf2out_undef (unsigned int, const char *);
2433 static void dwarf2out_start_source_file (unsigned, const char *);
2434 static void dwarf2out_end_source_file (unsigned);
2435 static void dwarf2out_function_decl (tree);
2436 static void dwarf2out_begin_block (unsigned, unsigned);
2437 static void dwarf2out_end_block (unsigned, unsigned);
2438 static bool dwarf2out_ignore_block (const_tree);
2439 static void dwarf2out_global_decl (tree);
2440 static void dwarf2out_type_decl (tree, int);
2441 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2442 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2443 dw_die_ref);
2444 static void dwarf2out_abstract_function (tree);
2445 static void dwarf2out_var_location (rtx_insn *);
2446 static void dwarf2out_begin_function (tree);
2447 static void dwarf2out_end_function (unsigned int);
2448 static void dwarf2out_set_name (tree, tree);
2450 /* The debug hooks structure. */
2452 const struct gcc_debug_hooks dwarf2_debug_hooks =
2454 dwarf2out_init,
2455 dwarf2out_finish,
2456 dwarf2out_assembly_start,
2457 dwarf2out_define,
2458 dwarf2out_undef,
2459 dwarf2out_start_source_file,
2460 dwarf2out_end_source_file,
2461 dwarf2out_begin_block,
2462 dwarf2out_end_block,
2463 dwarf2out_ignore_block,
2464 dwarf2out_source_line,
2465 dwarf2out_begin_prologue,
2466 #if VMS_DEBUGGING_INFO
2467 dwarf2out_vms_end_prologue,
2468 dwarf2out_vms_begin_epilogue,
2469 #else
2470 debug_nothing_int_charstar,
2471 debug_nothing_int_charstar,
2472 #endif
2473 dwarf2out_end_epilogue,
2474 dwarf2out_begin_function,
2475 dwarf2out_end_function, /* end_function */
2476 dwarf2out_function_decl, /* function_decl */
2477 dwarf2out_global_decl,
2478 dwarf2out_type_decl, /* type_decl */
2479 dwarf2out_imported_module_or_decl,
2480 debug_nothing_tree, /* deferred_inline_function */
2481 /* The DWARF 2 backend tries to reduce debugging bloat by not
2482 emitting the abstract description of inline functions until
2483 something tries to reference them. */
2484 dwarf2out_abstract_function, /* outlining_inline_function */
2485 debug_nothing_rtx_code_label, /* label */
2486 debug_nothing_int, /* handle_pch */
2487 dwarf2out_var_location,
2488 dwarf2out_switch_text_section,
2489 dwarf2out_set_name,
2490 1, /* start_end_main_source_file */
2491 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2494 /* NOTE: In the comments in this file, many references are made to
2495 "Debugging Information Entries". This term is abbreviated as `DIE'
2496 throughout the remainder of this file. */
2498 /* An internal representation of the DWARF output is built, and then
2499 walked to generate the DWARF debugging info. The walk of the internal
2500 representation is done after the entire program has been compiled.
2501 The types below are used to describe the internal representation. */
2503 /* Whether to put type DIEs into their own section .debug_types instead
2504 of making them part of the .debug_info section. Only supported for
2505 Dwarf V4 or higher and the user didn't disable them through
2506 -fno-debug-types-section. It is more efficient to put them in a
2507 separate comdat sections since the linker will then be able to
2508 remove duplicates. But not all tools support .debug_types sections
2509 yet. */
2511 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2513 /* Various DIE's use offsets relative to the beginning of the
2514 .debug_info section to refer to each other. */
2516 typedef long int dw_offset;
2518 /* Define typedefs here to avoid circular dependencies. */
2520 typedef struct dw_attr_struct *dw_attr_ref;
2521 typedef struct dw_line_info_struct *dw_line_info_ref;
2522 typedef struct pubname_struct *pubname_ref;
2523 typedef struct dw_ranges_struct *dw_ranges_ref;
2524 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2525 typedef struct comdat_type_struct *comdat_type_node_ref;
2527 /* The entries in the line_info table more-or-less mirror the opcodes
2528 that are used in the real dwarf line table. Arrays of these entries
2529 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2530 supported. */
2532 enum dw_line_info_opcode {
2533 /* Emit DW_LNE_set_address; the operand is the label index. */
2534 LI_set_address,
2536 /* Emit a row to the matrix with the given line. This may be done
2537 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2538 special opcodes. */
2539 LI_set_line,
2541 /* Emit a DW_LNS_set_file. */
2542 LI_set_file,
2544 /* Emit a DW_LNS_set_column. */
2545 LI_set_column,
2547 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2548 LI_negate_stmt,
2550 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2551 LI_set_prologue_end,
2552 LI_set_epilogue_begin,
2554 /* Emit a DW_LNE_set_discriminator. */
2555 LI_set_discriminator
2558 typedef struct GTY(()) dw_line_info_struct {
2559 enum dw_line_info_opcode opcode;
2560 unsigned int val;
2561 } dw_line_info_entry;
2564 typedef struct GTY(()) dw_line_info_table_struct {
2565 /* The label that marks the end of this section. */
2566 const char *end_label;
2568 /* The values for the last row of the matrix, as collected in the table.
2569 These are used to minimize the changes to the next row. */
2570 unsigned int file_num;
2571 unsigned int line_num;
2572 unsigned int column_num;
2573 int discrim_num;
2574 bool is_stmt;
2575 bool in_use;
2577 vec<dw_line_info_entry, va_gc> *entries;
2578 } dw_line_info_table;
2580 typedef dw_line_info_table *dw_line_info_table_p;
2583 /* Each DIE attribute has a field specifying the attribute kind,
2584 a link to the next attribute in the chain, and an attribute value.
2585 Attributes are typically linked below the DIE they modify. */
2587 typedef struct GTY(()) dw_attr_struct {
2588 enum dwarf_attribute dw_attr;
2589 dw_val_node dw_attr_val;
2591 dw_attr_node;
2594 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2595 The children of each node form a circular list linked by
2596 die_sib. die_child points to the node *before* the "first" child node. */
2598 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2599 union die_symbol_or_type_node
2601 const char * GTY ((tag ("0"))) die_symbol;
2602 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2604 GTY ((desc ("%0.comdat_type_p"))) die_id;
2605 vec<dw_attr_node, va_gc> *die_attr;
2606 dw_die_ref die_parent;
2607 dw_die_ref die_child;
2608 dw_die_ref die_sib;
2609 dw_die_ref die_definition; /* ref from a specification to its definition */
2610 dw_offset die_offset;
2611 unsigned long die_abbrev;
2612 int die_mark;
2613 unsigned int decl_id;
2614 enum dwarf_tag die_tag;
2615 /* Die is used and must not be pruned as unused. */
2616 BOOL_BITFIELD die_perennial_p : 1;
2617 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2618 /* Lots of spare bits. */
2620 die_node;
2622 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2623 #define FOR_EACH_CHILD(die, c, expr) do { \
2624 c = die->die_child; \
2625 if (c) do { \
2626 c = c->die_sib; \
2627 expr; \
2628 } while (c != die->die_child); \
2629 } while (0)
2631 /* The pubname structure */
2633 typedef struct GTY(()) pubname_struct {
2634 dw_die_ref die;
2635 const char *name;
2637 pubname_entry;
2640 struct GTY(()) dw_ranges_struct {
2641 /* If this is positive, it's a block number, otherwise it's a
2642 bitwise-negated index into dw_ranges_by_label. */
2643 int num;
2646 /* A structure to hold a macinfo entry. */
2648 typedef struct GTY(()) macinfo_struct {
2649 unsigned char code;
2650 unsigned HOST_WIDE_INT lineno;
2651 const char *info;
2653 macinfo_entry;
2656 struct GTY(()) dw_ranges_by_label_struct {
2657 const char *begin;
2658 const char *end;
2661 /* The comdat type node structure. */
2662 typedef struct GTY(()) comdat_type_struct
2664 dw_die_ref root_die;
2665 dw_die_ref type_die;
2666 dw_die_ref skeleton_die;
2667 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2668 struct comdat_type_struct *next;
2670 comdat_type_node;
2672 /* The limbo die list structure. */
2673 typedef struct GTY(()) limbo_die_struct {
2674 dw_die_ref die;
2675 tree created_for;
2676 struct limbo_die_struct *next;
2678 limbo_die_node;
2680 typedef struct skeleton_chain_struct
2682 dw_die_ref old_die;
2683 dw_die_ref new_die;
2684 struct skeleton_chain_struct *parent;
2686 skeleton_chain_node;
2688 /* Define a macro which returns nonzero for a TYPE_DECL which was
2689 implicitly generated for a type.
2691 Note that, unlike the C front-end (which generates a NULL named
2692 TYPE_DECL node for each complete tagged type, each array type,
2693 and each function type node created) the C++ front-end generates
2694 a _named_ TYPE_DECL node for each tagged type node created.
2695 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2696 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2697 front-end, but for each type, tagged or not. */
2699 #define TYPE_DECL_IS_STUB(decl) \
2700 (DECL_NAME (decl) == NULL_TREE \
2701 || (DECL_ARTIFICIAL (decl) \
2702 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2703 /* This is necessary for stub decls that \
2704 appear in nested inline functions. */ \
2705 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2706 && (decl_ultimate_origin (decl) \
2707 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2709 /* Information concerning the compilation unit's programming
2710 language, and compiler version. */
2712 /* Fixed size portion of the DWARF compilation unit header. */
2713 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2714 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2716 /* Fixed size portion of the DWARF comdat type unit header. */
2717 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2718 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2719 + DWARF_OFFSET_SIZE)
2721 /* Fixed size portion of public names info. */
2722 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2724 /* Fixed size portion of the address range info. */
2725 #define DWARF_ARANGES_HEADER_SIZE \
2726 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2727 DWARF2_ADDR_SIZE * 2) \
2728 - DWARF_INITIAL_LENGTH_SIZE)
2730 /* Size of padding portion in the address range info. It must be
2731 aligned to twice the pointer size. */
2732 #define DWARF_ARANGES_PAD_SIZE \
2733 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2734 DWARF2_ADDR_SIZE * 2) \
2735 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2737 /* Use assembler line directives if available. */
2738 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2739 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2740 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2741 #else
2742 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2743 #endif
2744 #endif
2746 /* Minimum line offset in a special line info. opcode.
2747 This value was chosen to give a reasonable range of values. */
2748 #define DWARF_LINE_BASE -10
2750 /* First special line opcode - leave room for the standard opcodes. */
2751 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2753 /* Range of line offsets in a special line info. opcode. */
2754 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2756 /* Flag that indicates the initial value of the is_stmt_start flag.
2757 In the present implementation, we do not mark any lines as
2758 the beginning of a source statement, because that information
2759 is not made available by the GCC front-end. */
2760 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2762 /* Maximum number of operations per instruction bundle. */
2763 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2764 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2765 #endif
2767 /* This location is used by calc_die_sizes() to keep track
2768 the offset of each DIE within the .debug_info section. */
2769 static unsigned long next_die_offset;
2771 /* Record the root of the DIE's built for the current compilation unit. */
2772 static GTY(()) dw_die_ref single_comp_unit_die;
2774 /* A list of type DIEs that have been separated into comdat sections. */
2775 static GTY(()) comdat_type_node *comdat_type_list;
2777 /* A list of DIEs with a NULL parent waiting to be relocated. */
2778 static GTY(()) limbo_die_node *limbo_die_list;
2780 /* A list of DIEs for which we may have to generate
2781 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2782 static GTY(()) limbo_die_node *deferred_asm_name;
2784 struct dwarf_file_hasher : ggc_hasher<dwarf_file_data *>
2786 typedef const char *compare_type;
2788 static hashval_t hash (dwarf_file_data *);
2789 static bool equal (dwarf_file_data *, const char *);
2792 /* Filenames referenced by this compilation unit. */
2793 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2795 struct decl_die_hasher : ggc_hasher<die_node *>
2797 typedef tree compare_type;
2799 static hashval_t hash (die_node *);
2800 static bool equal (die_node *, tree);
2802 /* A hash table of references to DIE's that describe declarations.
2803 The key is a DECL_UID() which is a unique number identifying each decl. */
2804 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2806 struct block_die_hasher : ggc_hasher<die_struct *>
2808 static hashval_t hash (die_struct *);
2809 static bool equal (die_struct *, die_struct *);
2812 /* A hash table of references to DIE's that describe COMMON blocks.
2813 The key is DECL_UID() ^ die_parent. */
2814 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2816 typedef struct GTY(()) die_arg_entry_struct {
2817 dw_die_ref die;
2818 tree arg;
2819 } die_arg_entry;
2822 /* Node of the variable location list. */
2823 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2824 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2825 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2826 in mode of the EXPR_LIST node and first EXPR_LIST operand
2827 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2828 location or NULL for padding. For larger bitsizes,
2829 mode is 0 and first operand is a CONCAT with bitsize
2830 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2831 NULL as second operand. */
2832 rtx GTY (()) loc;
2833 const char * GTY (()) label;
2834 struct var_loc_node * GTY (()) next;
2837 /* Variable location list. */
2838 struct GTY ((for_user)) var_loc_list_def {
2839 struct var_loc_node * GTY (()) first;
2841 /* Pointer to the last but one or last element of the
2842 chained list. If the list is empty, both first and
2843 last are NULL, if the list contains just one node
2844 or the last node certainly is not redundant, it points
2845 to the last node, otherwise points to the last but one.
2846 Do not mark it for GC because it is marked through the chain. */
2847 struct var_loc_node * GTY ((skip ("%h"))) last;
2849 /* Pointer to the last element before section switch,
2850 if NULL, either sections weren't switched or first
2851 is after section switch. */
2852 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2854 /* DECL_UID of the variable decl. */
2855 unsigned int decl_id;
2857 typedef struct var_loc_list_def var_loc_list;
2859 /* Call argument location list. */
2860 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2861 rtx GTY (()) call_arg_loc_note;
2862 const char * GTY (()) label;
2863 tree GTY (()) block;
2864 bool tail_call_p;
2865 rtx GTY (()) symbol_ref;
2866 struct call_arg_loc_node * GTY (()) next;
2870 struct decl_loc_hasher : ggc_hasher<var_loc_list *>
2872 typedef const_tree compare_type;
2874 static hashval_t hash (var_loc_list *);
2875 static bool equal (var_loc_list *, const_tree);
2878 /* Table of decl location linked lists. */
2879 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2881 /* Head and tail of call_arg_loc chain. */
2882 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2883 static struct call_arg_loc_node *call_arg_loc_last;
2885 /* Number of call sites in the current function. */
2886 static int call_site_count = -1;
2887 /* Number of tail call sites in the current function. */
2888 static int tail_call_site_count = -1;
2890 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2891 DIEs. */
2892 static vec<dw_die_ref> block_map;
2894 /* A cached location list. */
2895 struct GTY ((for_user)) cached_dw_loc_list_def {
2896 /* The DECL_UID of the decl that this entry describes. */
2897 unsigned int decl_id;
2899 /* The cached location list. */
2900 dw_loc_list_ref loc_list;
2902 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2904 struct dw_loc_list_hasher : ggc_hasher<cached_dw_loc_list *>
2907 typedef const_tree compare_type;
2909 static hashval_t hash (cached_dw_loc_list *);
2910 static bool equal (cached_dw_loc_list *, const_tree);
2913 /* Table of cached location lists. */
2914 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2916 /* A pointer to the base of a list of references to DIE's that
2917 are uniquely identified by their tag, presence/absence of
2918 children DIE's, and list of attribute/value pairs. */
2919 static GTY((length ("abbrev_die_table_allocated")))
2920 dw_die_ref *abbrev_die_table;
2922 /* Number of elements currently allocated for abbrev_die_table. */
2923 static GTY(()) unsigned abbrev_die_table_allocated;
2925 /* Number of elements in type_die_table currently in use. */
2926 static GTY(()) unsigned abbrev_die_table_in_use;
2928 /* Size (in elements) of increments by which we may expand the
2929 abbrev_die_table. */
2930 #define ABBREV_DIE_TABLE_INCREMENT 256
2932 /* A global counter for generating labels for line number data. */
2933 static unsigned int line_info_label_num;
2935 /* The current table to which we should emit line number information
2936 for the current function. This will be set up at the beginning of
2937 assembly for the function. */
2938 static dw_line_info_table *cur_line_info_table;
2940 /* The two default tables of line number info. */
2941 static GTY(()) dw_line_info_table *text_section_line_info;
2942 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2944 /* The set of all non-default tables of line number info. */
2945 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2947 /* A flag to tell pubnames/types export if there is an info section to
2948 refer to. */
2949 static bool info_section_emitted;
2951 /* A pointer to the base of a table that contains a list of publicly
2952 accessible names. */
2953 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2955 /* A pointer to the base of a table that contains a list of publicly
2956 accessible types. */
2957 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2959 /* A pointer to the base of a table that contains a list of macro
2960 defines/undefines (and file start/end markers). */
2961 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2963 /* True if .debug_macinfo or .debug_macros section is going to be
2964 emitted. */
2965 #define have_macinfo \
2966 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2967 && !macinfo_table->is_empty ())
2969 /* Array of dies for which we should generate .debug_ranges info. */
2970 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2972 /* Number of elements currently allocated for ranges_table. */
2973 static GTY(()) unsigned ranges_table_allocated;
2975 /* Number of elements in ranges_table currently in use. */
2976 static GTY(()) unsigned ranges_table_in_use;
2978 /* Array of pairs of labels referenced in ranges_table. */
2979 static GTY ((length ("ranges_by_label_allocated")))
2980 dw_ranges_by_label_ref ranges_by_label;
2982 /* Number of elements currently allocated for ranges_by_label. */
2983 static GTY(()) unsigned ranges_by_label_allocated;
2985 /* Number of elements in ranges_by_label currently in use. */
2986 static GTY(()) unsigned ranges_by_label_in_use;
2988 /* Size (in elements) of increments by which we may expand the
2989 ranges_table. */
2990 #define RANGES_TABLE_INCREMENT 64
2992 /* Whether we have location lists that need outputting */
2993 static GTY(()) bool have_location_lists;
2995 /* Unique label counter. */
2996 static GTY(()) unsigned int loclabel_num;
2998 /* Unique label counter for point-of-call tables. */
2999 static GTY(()) unsigned int poc_label_num;
3001 /* The last file entry emitted by maybe_emit_file(). */
3002 static GTY(()) struct dwarf_file_data * last_emitted_file;
3004 /* Number of internal labels generated by gen_internal_sym(). */
3005 static GTY(()) int label_num;
3007 /* Cached result of previous call to lookup_filename. */
3008 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3010 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3012 /* Instances of generic types for which we need to generate debug
3013 info that describe their generic parameters and arguments. That
3014 generation needs to happen once all types are properly laid out so
3015 we do it at the end of compilation. */
3016 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3018 /* Offset from the "steady-state frame pointer" to the frame base,
3019 within the current function. */
3020 static HOST_WIDE_INT frame_pointer_fb_offset;
3021 static bool frame_pointer_fb_offset_valid;
3023 static vec<dw_die_ref> base_types;
3025 /* Forward declarations for functions defined in this file. */
3027 static int is_pseudo_reg (const_rtx);
3028 static tree type_main_variant (tree);
3029 static int is_tagged_type (const_tree);
3030 static const char *dwarf_tag_name (unsigned);
3031 static const char *dwarf_attr_name (unsigned);
3032 static const char *dwarf_form_name (unsigned);
3033 static tree decl_ultimate_origin (const_tree);
3034 static tree decl_class_context (tree);
3035 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3036 static inline enum dw_val_class AT_class (dw_attr_ref);
3037 static inline unsigned int AT_index (dw_attr_ref);
3038 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3039 static inline unsigned AT_flag (dw_attr_ref);
3040 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3041 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3042 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3043 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3044 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3045 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3046 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3047 unsigned int, unsigned char *);
3048 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3049 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3050 static inline const char *AT_string (dw_attr_ref);
3051 static enum dwarf_form AT_string_form (dw_attr_ref);
3052 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3053 static void add_AT_specification (dw_die_ref, dw_die_ref);
3054 static inline dw_die_ref AT_ref (dw_attr_ref);
3055 static inline int AT_ref_external (dw_attr_ref);
3056 static inline void set_AT_ref_external (dw_attr_ref, int);
3057 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3058 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3059 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3060 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3061 dw_loc_list_ref);
3062 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3063 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3064 static void remove_addr_table_entry (addr_table_entry *);
3065 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3066 static inline rtx AT_addr (dw_attr_ref);
3067 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3068 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3069 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3070 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3071 unsigned HOST_WIDE_INT);
3072 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3073 unsigned long, bool);
3074 static inline const char *AT_lbl (dw_attr_ref);
3075 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3076 static const char *get_AT_low_pc (dw_die_ref);
3077 static const char *get_AT_hi_pc (dw_die_ref);
3078 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3079 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3080 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3081 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3082 static bool is_cxx (void);
3083 static bool is_fortran (void);
3084 static bool is_ada (void);
3085 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3086 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3087 static void add_child_die (dw_die_ref, dw_die_ref);
3088 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3089 static dw_die_ref lookup_type_die (tree);
3090 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3091 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3092 static void equate_type_number_to_die (tree, dw_die_ref);
3093 static dw_die_ref lookup_decl_die (tree);
3094 static var_loc_list *lookup_decl_loc (const_tree);
3095 static void equate_decl_number_to_die (tree, dw_die_ref);
3096 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3097 static void print_spaces (FILE *);
3098 static void print_die (dw_die_ref, FILE *);
3099 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3100 static dw_die_ref pop_compile_unit (dw_die_ref);
3101 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3102 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3103 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3104 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3105 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3106 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3107 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3108 struct md5_ctx *, int *);
3109 struct checksum_attributes;
3110 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3111 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3112 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3113 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3114 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3115 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3116 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3117 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3118 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3119 static void compute_section_prefix (dw_die_ref);
3120 static int is_type_die (dw_die_ref);
3121 static int is_comdat_die (dw_die_ref);
3122 static int is_symbol_die (dw_die_ref);
3123 static inline bool is_template_instantiation (dw_die_ref);
3124 static void assign_symbol_names (dw_die_ref);
3125 static void break_out_includes (dw_die_ref);
3126 static int is_declaration_die (dw_die_ref);
3127 static int should_move_die_to_comdat (dw_die_ref);
3128 static dw_die_ref clone_as_declaration (dw_die_ref);
3129 static dw_die_ref clone_die (dw_die_ref);
3130 static dw_die_ref clone_tree (dw_die_ref);
3131 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3132 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3133 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3134 static dw_die_ref generate_skeleton (dw_die_ref);
3135 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3136 dw_die_ref,
3137 dw_die_ref);
3138 static void break_out_comdat_types (dw_die_ref);
3139 static void copy_decls_for_unworthy_types (dw_die_ref);
3141 static void add_sibling_attributes (dw_die_ref);
3142 static void output_location_lists (dw_die_ref);
3143 static int constant_size (unsigned HOST_WIDE_INT);
3144 static unsigned long size_of_die (dw_die_ref);
3145 static void calc_die_sizes (dw_die_ref);
3146 static void calc_base_type_die_sizes (void);
3147 static void mark_dies (dw_die_ref);
3148 static void unmark_dies (dw_die_ref);
3149 static void unmark_all_dies (dw_die_ref);
3150 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3151 static unsigned long size_of_aranges (void);
3152 static enum dwarf_form value_format (dw_attr_ref);
3153 static void output_value_format (dw_attr_ref);
3154 static void output_abbrev_section (void);
3155 static void output_die_abbrevs (unsigned long, dw_die_ref);
3156 static void output_die_symbol (dw_die_ref);
3157 static void output_die (dw_die_ref);
3158 static void output_compilation_unit_header (void);
3159 static void output_comp_unit (dw_die_ref, int);
3160 static void output_comdat_type_unit (comdat_type_node *);
3161 static const char *dwarf2_name (tree, int);
3162 static void add_pubname (tree, dw_die_ref);
3163 static void add_enumerator_pubname (const char *, dw_die_ref);
3164 static void add_pubname_string (const char *, dw_die_ref);
3165 static void add_pubtype (tree, dw_die_ref);
3166 static void output_pubnames (vec<pubname_entry, va_gc> *);
3167 static void output_aranges (unsigned long);
3168 static unsigned int add_ranges_num (int);
3169 static unsigned int add_ranges (const_tree);
3170 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3171 bool *, bool);
3172 static void output_ranges (void);
3173 static dw_line_info_table *new_line_info_table (void);
3174 static void output_line_info (bool);
3175 static void output_file_names (void);
3176 static dw_die_ref base_type_die (tree);
3177 static int is_base_type (tree);
3178 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3179 static int decl_quals (const_tree);
3180 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3181 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3182 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3183 static int type_is_enum (const_tree);
3184 static unsigned int dbx_reg_number (const_rtx);
3185 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3186 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3187 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3188 enum var_init_status);
3189 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3190 enum var_init_status);
3191 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3192 enum var_init_status);
3193 static int is_based_loc (const_rtx);
3194 static bool resolve_one_addr (rtx *);
3195 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3196 enum var_init_status);
3197 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3198 enum var_init_status);
3199 static dw_loc_list_ref loc_list_from_tree (tree, int);
3200 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3201 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3202 static tree field_type (const_tree);
3203 static unsigned int simple_type_align_in_bits (const_tree);
3204 static unsigned int simple_decl_align_in_bits (const_tree);
3205 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3206 static HOST_WIDE_INT field_byte_offset (const_tree);
3207 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3208 dw_loc_list_ref);
3209 static void add_data_member_location_attribute (dw_die_ref, tree);
3210 static bool add_const_value_attribute (dw_die_ref, rtx);
3211 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3212 static void insert_wide_int (const wide_int &, unsigned char *, int);
3213 static void insert_float (const_rtx, unsigned char *);
3214 static rtx rtl_for_decl_location (tree);
3215 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3216 enum dwarf_attribute);
3217 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3218 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3219 static void add_name_attribute (dw_die_ref, const char *);
3220 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3221 static void add_comp_dir_attribute (dw_die_ref);
3222 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3223 static void add_subscript_info (dw_die_ref, tree, bool);
3224 static void add_byte_size_attribute (dw_die_ref, tree);
3225 static void add_bit_offset_attribute (dw_die_ref, tree);
3226 static void add_bit_size_attribute (dw_die_ref, tree);
3227 static void add_prototyped_attribute (dw_die_ref, tree);
3228 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3229 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3230 static void add_src_coords_attributes (dw_die_ref, tree);
3231 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3232 static void push_decl_scope (tree);
3233 static void pop_decl_scope (void);
3234 static dw_die_ref scope_die_for (tree, dw_die_ref);
3235 static inline int local_scope_p (dw_die_ref);
3236 static inline int class_scope_p (dw_die_ref);
3237 static inline int class_or_namespace_scope_p (dw_die_ref);
3238 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3239 static void add_calling_convention_attribute (dw_die_ref, tree);
3240 static const char *type_tag (const_tree);
3241 static tree member_declared_type (const_tree);
3242 #if 0
3243 static const char *decl_start_label (tree);
3244 #endif
3245 static void gen_array_type_die (tree, dw_die_ref);
3246 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3247 #if 0
3248 static void gen_entry_point_die (tree, dw_die_ref);
3249 #endif
3250 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3251 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3252 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3253 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3254 static void gen_formal_types_die (tree, dw_die_ref);
3255 static void gen_subprogram_die (tree, dw_die_ref);
3256 static void gen_variable_die (tree, tree, dw_die_ref);
3257 static void gen_const_die (tree, dw_die_ref);
3258 static void gen_label_die (tree, dw_die_ref);
3259 static void gen_lexical_block_die (tree, dw_die_ref, int);
3260 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3261 static void gen_field_die (tree, dw_die_ref);
3262 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3263 static dw_die_ref gen_compile_unit_die (const char *);
3264 static void gen_inheritance_die (tree, tree, dw_die_ref);
3265 static void gen_member_die (tree, dw_die_ref);
3266 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3267 enum debug_info_usage);
3268 static void gen_subroutine_type_die (tree, dw_die_ref);
3269 static void gen_typedef_die (tree, dw_die_ref);
3270 static void gen_type_die (tree, dw_die_ref);
3271 static void gen_block_die (tree, dw_die_ref, int);
3272 static void decls_for_scope (tree, dw_die_ref, int);
3273 static inline int is_redundant_typedef (const_tree);
3274 static bool is_naming_typedef_decl (const_tree);
3275 static inline dw_die_ref get_context_die (tree);
3276 static void gen_namespace_die (tree, dw_die_ref);
3277 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3278 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3279 static dw_die_ref force_decl_die (tree);
3280 static dw_die_ref force_type_die (tree);
3281 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3282 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3283 static struct dwarf_file_data * lookup_filename (const char *);
3284 static void retry_incomplete_types (void);
3285 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3286 static void gen_generic_params_dies (tree);
3287 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3288 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3289 static void splice_child_die (dw_die_ref, dw_die_ref);
3290 static int file_info_cmp (const void *, const void *);
3291 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3292 const char *, const char *);
3293 static void output_loc_list (dw_loc_list_ref);
3294 static char *gen_internal_sym (const char *);
3295 static bool want_pubnames (void);
3297 static void prune_unmark_dies (dw_die_ref);
3298 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3299 static void prune_unused_types_mark (dw_die_ref, int);
3300 static void prune_unused_types_walk (dw_die_ref);
3301 static void prune_unused_types_walk_attribs (dw_die_ref);
3302 static void prune_unused_types_prune (dw_die_ref);
3303 static void prune_unused_types (void);
3304 static int maybe_emit_file (struct dwarf_file_data *fd);
3305 static inline const char *AT_vms_delta1 (dw_attr_ref);
3306 static inline const char *AT_vms_delta2 (dw_attr_ref);
3307 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3308 const char *, const char *);
3309 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3310 static void gen_remaining_tmpl_value_param_die_attribute (void);
3311 static bool generic_type_p (tree);
3312 static void schedule_generic_params_dies_gen (tree t);
3313 static void gen_scheduled_generic_parms_dies (void);
3315 static const char *comp_dir_string (void);
3317 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3319 /* enum for tracking thread-local variables whose address is really an offset
3320 relative to the TLS pointer, which will need link-time relocation, but will
3321 not need relocation by the DWARF consumer. */
3323 enum dtprel_bool
3325 dtprel_false = 0,
3326 dtprel_true = 1
3329 /* Return the operator to use for an address of a variable. For dtprel_true, we
3330 use DW_OP_const*. For regular variables, which need both link-time
3331 relocation and consumer-level relocation (e.g., to account for shared objects
3332 loaded at a random address), we use DW_OP_addr*. */
3334 static inline enum dwarf_location_atom
3335 dw_addr_op (enum dtprel_bool dtprel)
3337 if (dtprel == dtprel_true)
3338 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3339 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3340 else
3341 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3344 /* Return a pointer to a newly allocated address location description. If
3345 dwarf_split_debug_info is true, then record the address with the appropriate
3346 relocation. */
3347 static inline dw_loc_descr_ref
3348 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3350 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3352 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3353 ref->dw_loc_oprnd1.v.val_addr = addr;
3354 ref->dtprel = dtprel;
3355 if (dwarf_split_debug_info)
3356 ref->dw_loc_oprnd1.val_entry
3357 = add_addr_table_entry (addr,
3358 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3359 else
3360 ref->dw_loc_oprnd1.val_entry = NULL;
3362 return ref;
3365 /* Section names used to hold DWARF debugging information. */
3367 #ifndef DEBUG_INFO_SECTION
3368 #define DEBUG_INFO_SECTION ".debug_info"
3369 #endif
3370 #ifndef DEBUG_DWO_INFO_SECTION
3371 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3372 #endif
3373 #ifndef DEBUG_ABBREV_SECTION
3374 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3375 #endif
3376 #ifndef DEBUG_DWO_ABBREV_SECTION
3377 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3378 #endif
3379 #ifndef DEBUG_ARANGES_SECTION
3380 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3381 #endif
3382 #ifndef DEBUG_ADDR_SECTION
3383 #define DEBUG_ADDR_SECTION ".debug_addr"
3384 #endif
3385 #ifndef DEBUG_NORM_MACINFO_SECTION
3386 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3387 #endif
3388 #ifndef DEBUG_DWO_MACINFO_SECTION
3389 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3390 #endif
3391 #ifndef DEBUG_MACINFO_SECTION
3392 #define DEBUG_MACINFO_SECTION \
3393 (!dwarf_split_debug_info \
3394 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3395 #endif
3396 #ifndef DEBUG_NORM_MACRO_SECTION
3397 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3398 #endif
3399 #ifndef DEBUG_DWO_MACRO_SECTION
3400 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3401 #endif
3402 #ifndef DEBUG_MACRO_SECTION
3403 #define DEBUG_MACRO_SECTION \
3404 (!dwarf_split_debug_info \
3405 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3406 #endif
3407 #ifndef DEBUG_LINE_SECTION
3408 #define DEBUG_LINE_SECTION ".debug_line"
3409 #endif
3410 #ifndef DEBUG_DWO_LINE_SECTION
3411 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3412 #endif
3413 #ifndef DEBUG_LOC_SECTION
3414 #define DEBUG_LOC_SECTION ".debug_loc"
3415 #endif
3416 #ifndef DEBUG_DWO_LOC_SECTION
3417 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3418 #endif
3419 #ifndef DEBUG_PUBNAMES_SECTION
3420 #define DEBUG_PUBNAMES_SECTION \
3421 ((debug_generate_pub_sections == 2) \
3422 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3423 #endif
3424 #ifndef DEBUG_PUBTYPES_SECTION
3425 #define DEBUG_PUBTYPES_SECTION \
3426 ((debug_generate_pub_sections == 2) \
3427 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3428 #endif
3429 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3430 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3431 #ifndef DEBUG_STR_OFFSETS_SECTION
3432 #define DEBUG_STR_OFFSETS_SECTION \
3433 (!dwarf_split_debug_info \
3434 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3435 #endif
3436 #ifndef DEBUG_STR_DWO_SECTION
3437 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3438 #endif
3439 #ifndef DEBUG_STR_SECTION
3440 #define DEBUG_STR_SECTION ".debug_str"
3441 #endif
3442 #ifndef DEBUG_RANGES_SECTION
3443 #define DEBUG_RANGES_SECTION ".debug_ranges"
3444 #endif
3446 /* Standard ELF section names for compiled code and data. */
3447 #ifndef TEXT_SECTION_NAME
3448 #define TEXT_SECTION_NAME ".text"
3449 #endif
3451 /* Section flags for .debug_macinfo/.debug_macro section. */
3452 #define DEBUG_MACRO_SECTION_FLAGS \
3453 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3455 /* Section flags for .debug_str section. */
3456 #define DEBUG_STR_SECTION_FLAGS \
3457 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3458 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3459 : SECTION_DEBUG)
3461 /* Section flags for .debug_str.dwo section. */
3462 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3464 /* Labels we insert at beginning sections we can reference instead of
3465 the section names themselves. */
3467 #ifndef TEXT_SECTION_LABEL
3468 #define TEXT_SECTION_LABEL "Ltext"
3469 #endif
3470 #ifndef COLD_TEXT_SECTION_LABEL
3471 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3472 #endif
3473 #ifndef DEBUG_LINE_SECTION_LABEL
3474 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3475 #endif
3476 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3477 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3478 #endif
3479 #ifndef DEBUG_INFO_SECTION_LABEL
3480 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3481 #endif
3482 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3483 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3484 #endif
3485 #ifndef DEBUG_ABBREV_SECTION_LABEL
3486 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3487 #endif
3488 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3489 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3490 #endif
3491 #ifndef DEBUG_ADDR_SECTION_LABEL
3492 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3493 #endif
3494 #ifndef DEBUG_LOC_SECTION_LABEL
3495 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3496 #endif
3497 #ifndef DEBUG_RANGES_SECTION_LABEL
3498 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3499 #endif
3500 #ifndef DEBUG_MACINFO_SECTION_LABEL
3501 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3502 #endif
3503 #ifndef DEBUG_MACRO_SECTION_LABEL
3504 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3505 #endif
3506 #define SKELETON_COMP_DIE_ABBREV 1
3507 #define SKELETON_TYPE_DIE_ABBREV 2
3509 /* Definitions of defaults for formats and names of various special
3510 (artificial) labels which may be generated within this file (when the -g
3511 options is used and DWARF2_DEBUGGING_INFO is in effect.
3512 If necessary, these may be overridden from within the tm.h file, but
3513 typically, overriding these defaults is unnecessary. */
3515 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3516 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3517 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3518 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3519 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3520 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3521 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3522 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3523 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3524 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3525 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3526 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3527 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3528 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3530 #ifndef TEXT_END_LABEL
3531 #define TEXT_END_LABEL "Letext"
3532 #endif
3533 #ifndef COLD_END_LABEL
3534 #define COLD_END_LABEL "Letext_cold"
3535 #endif
3536 #ifndef BLOCK_BEGIN_LABEL
3537 #define BLOCK_BEGIN_LABEL "LBB"
3538 #endif
3539 #ifndef BLOCK_END_LABEL
3540 #define BLOCK_END_LABEL "LBE"
3541 #endif
3542 #ifndef LINE_CODE_LABEL
3543 #define LINE_CODE_LABEL "LM"
3544 #endif
3547 /* Return the root of the DIE's built for the current compilation unit. */
3548 static dw_die_ref
3549 comp_unit_die (void)
3551 if (!single_comp_unit_die)
3552 single_comp_unit_die = gen_compile_unit_die (NULL);
3553 return single_comp_unit_die;
3556 /* We allow a language front-end to designate a function that is to be
3557 called to "demangle" any name before it is put into a DIE. */
3559 static const char *(*demangle_name_func) (const char *);
3561 void
3562 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3564 demangle_name_func = func;
3567 /* Test if rtl node points to a pseudo register. */
3569 static inline int
3570 is_pseudo_reg (const_rtx rtl)
3572 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3573 || (GET_CODE (rtl) == SUBREG
3574 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3577 /* Return a reference to a type, with its const and volatile qualifiers
3578 removed. */
3580 static inline tree
3581 type_main_variant (tree type)
3583 type = TYPE_MAIN_VARIANT (type);
3585 /* ??? There really should be only one main variant among any group of
3586 variants of a given type (and all of the MAIN_VARIANT values for all
3587 members of the group should point to that one type) but sometimes the C
3588 front-end messes this up for array types, so we work around that bug
3589 here. */
3590 if (TREE_CODE (type) == ARRAY_TYPE)
3591 while (type != TYPE_MAIN_VARIANT (type))
3592 type = TYPE_MAIN_VARIANT (type);
3594 return type;
3597 /* Return nonzero if the given type node represents a tagged type. */
3599 static inline int
3600 is_tagged_type (const_tree type)
3602 enum tree_code code = TREE_CODE (type);
3604 return (code == RECORD_TYPE || code == UNION_TYPE
3605 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3608 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3610 static void
3611 get_ref_die_offset_label (char *label, dw_die_ref ref)
3613 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3616 /* Return die_offset of a DIE reference to a base type. */
3618 static unsigned long int
3619 get_base_type_offset (dw_die_ref ref)
3621 if (ref->die_offset)
3622 return ref->die_offset;
3623 if (comp_unit_die ()->die_abbrev)
3625 calc_base_type_die_sizes ();
3626 gcc_assert (ref->die_offset);
3628 return ref->die_offset;
3631 /* Return die_offset of a DIE reference other than base type. */
3633 static unsigned long int
3634 get_ref_die_offset (dw_die_ref ref)
3636 gcc_assert (ref->die_offset);
3637 return ref->die_offset;
3640 /* Convert a DIE tag into its string name. */
3642 static const char *
3643 dwarf_tag_name (unsigned int tag)
3645 const char *name = get_DW_TAG_name (tag);
3647 if (name != NULL)
3648 return name;
3650 return "DW_TAG_<unknown>";
3653 /* Convert a DWARF attribute code into its string name. */
3655 static const char *
3656 dwarf_attr_name (unsigned int attr)
3658 const char *name;
3660 switch (attr)
3662 #if VMS_DEBUGGING_INFO
3663 case DW_AT_HP_prologue:
3664 return "DW_AT_HP_prologue";
3665 #else
3666 case DW_AT_MIPS_loop_unroll_factor:
3667 return "DW_AT_MIPS_loop_unroll_factor";
3668 #endif
3670 #if VMS_DEBUGGING_INFO
3671 case DW_AT_HP_epilogue:
3672 return "DW_AT_HP_epilogue";
3673 #else
3674 case DW_AT_MIPS_stride:
3675 return "DW_AT_MIPS_stride";
3676 #endif
3679 name = get_DW_AT_name (attr);
3681 if (name != NULL)
3682 return name;
3684 return "DW_AT_<unknown>";
3687 /* Convert a DWARF value form code into its string name. */
3689 static const char *
3690 dwarf_form_name (unsigned int form)
3692 const char *name = get_DW_FORM_name (form);
3694 if (name != NULL)
3695 return name;
3697 return "DW_FORM_<unknown>";
3700 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3701 instance of an inlined instance of a decl which is local to an inline
3702 function, so we have to trace all of the way back through the origin chain
3703 to find out what sort of node actually served as the original seed for the
3704 given block. */
3706 static tree
3707 decl_ultimate_origin (const_tree decl)
3709 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3710 return NULL_TREE;
3712 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3713 we're trying to output the abstract instance of this function. */
3714 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3715 return NULL_TREE;
3717 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3718 most distant ancestor, this should never happen. */
3719 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3721 return DECL_ABSTRACT_ORIGIN (decl);
3724 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3725 of a virtual function may refer to a base class, so we check the 'this'
3726 parameter. */
3728 static tree
3729 decl_class_context (tree decl)
3731 tree context = NULL_TREE;
3733 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3734 context = DECL_CONTEXT (decl);
3735 else
3736 context = TYPE_MAIN_VARIANT
3737 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3739 if (context && !TYPE_P (context))
3740 context = NULL_TREE;
3742 return context;
3745 /* Add an attribute/value pair to a DIE. */
3747 static inline void
3748 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3750 /* Maybe this should be an assert? */
3751 if (die == NULL)
3752 return;
3754 vec_safe_reserve (die->die_attr, 1);
3755 vec_safe_push (die->die_attr, *attr);
3758 static inline enum dw_val_class
3759 AT_class (dw_attr_ref a)
3761 return a->dw_attr_val.val_class;
3764 /* Return the index for any attribute that will be referenced with a
3765 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3766 are stored in dw_attr_val.v.val_str for reference counting
3767 pruning. */
3769 static inline unsigned int
3770 AT_index (dw_attr_ref a)
3772 if (AT_class (a) == dw_val_class_str)
3773 return a->dw_attr_val.v.val_str->index;
3774 else if (a->dw_attr_val.val_entry != NULL)
3775 return a->dw_attr_val.val_entry->index;
3776 return NOT_INDEXED;
3779 /* Add a flag value attribute to a DIE. */
3781 static inline void
3782 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3784 dw_attr_node attr;
3786 attr.dw_attr = attr_kind;
3787 attr.dw_attr_val.val_class = dw_val_class_flag;
3788 attr.dw_attr_val.val_entry = NULL;
3789 attr.dw_attr_val.v.val_flag = flag;
3790 add_dwarf_attr (die, &attr);
3793 static inline unsigned
3794 AT_flag (dw_attr_ref a)
3796 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3797 return a->dw_attr_val.v.val_flag;
3800 /* Add a signed integer attribute value to a DIE. */
3802 static inline void
3803 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3805 dw_attr_node attr;
3807 attr.dw_attr = attr_kind;
3808 attr.dw_attr_val.val_class = dw_val_class_const;
3809 attr.dw_attr_val.val_entry = NULL;
3810 attr.dw_attr_val.v.val_int = int_val;
3811 add_dwarf_attr (die, &attr);
3814 static inline HOST_WIDE_INT
3815 AT_int (dw_attr_ref a)
3817 gcc_assert (a && AT_class (a) == dw_val_class_const);
3818 return a->dw_attr_val.v.val_int;
3821 /* Add an unsigned integer attribute value to a DIE. */
3823 static inline void
3824 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3825 unsigned HOST_WIDE_INT unsigned_val)
3827 dw_attr_node attr;
3829 attr.dw_attr = attr_kind;
3830 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3831 attr.dw_attr_val.val_entry = NULL;
3832 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3833 add_dwarf_attr (die, &attr);
3836 static inline unsigned HOST_WIDE_INT
3837 AT_unsigned (dw_attr_ref a)
3839 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3840 return a->dw_attr_val.v.val_unsigned;
3843 /* Add an unsigned wide integer attribute value to a DIE. */
3845 static inline void
3846 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3847 const wide_int& w)
3849 dw_attr_node attr;
3851 attr.dw_attr = attr_kind;
3852 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3853 attr.dw_attr_val.v.val_wide = ggc_cleared_alloc<wide_int> ();
3854 *attr.dw_attr_val.v.val_wide = w;
3855 add_dwarf_attr (die, &attr);
3858 /* Add an unsigned double integer attribute value to a DIE. */
3860 static inline void
3861 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3862 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3864 dw_attr_node attr;
3866 attr.dw_attr = attr_kind;
3867 attr.dw_attr_val.val_class = dw_val_class_const_double;
3868 attr.dw_attr_val.val_entry = NULL;
3869 attr.dw_attr_val.v.val_double.high = high;
3870 attr.dw_attr_val.v.val_double.low = low;
3871 add_dwarf_attr (die, &attr);
3874 /* Add a floating point attribute value to a DIE and return it. */
3876 static inline void
3877 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3878 unsigned int length, unsigned int elt_size, unsigned char *array)
3880 dw_attr_node attr;
3882 attr.dw_attr = attr_kind;
3883 attr.dw_attr_val.val_class = dw_val_class_vec;
3884 attr.dw_attr_val.val_entry = NULL;
3885 attr.dw_attr_val.v.val_vec.length = length;
3886 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3887 attr.dw_attr_val.v.val_vec.array = array;
3888 add_dwarf_attr (die, &attr);
3891 /* Add an 8-byte data attribute value to a DIE. */
3893 static inline void
3894 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3895 unsigned char data8[8])
3897 dw_attr_node attr;
3899 attr.dw_attr = attr_kind;
3900 attr.dw_attr_val.val_class = dw_val_class_data8;
3901 attr.dw_attr_val.val_entry = NULL;
3902 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3903 add_dwarf_attr (die, &attr);
3906 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3907 dwarf_split_debug_info, address attributes in dies destined for the
3908 final executable have force_direct set to avoid using indexed
3909 references. */
3911 static inline void
3912 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3913 bool force_direct)
3915 dw_attr_node attr;
3916 char * lbl_id;
3918 lbl_id = xstrdup (lbl_low);
3919 attr.dw_attr = DW_AT_low_pc;
3920 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3921 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3922 if (dwarf_split_debug_info && !force_direct)
3923 attr.dw_attr_val.val_entry
3924 = add_addr_table_entry (lbl_id, ate_kind_label);
3925 else
3926 attr.dw_attr_val.val_entry = NULL;
3927 add_dwarf_attr (die, &attr);
3929 attr.dw_attr = DW_AT_high_pc;
3930 if (dwarf_version < 4)
3931 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3932 else
3933 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3934 lbl_id = xstrdup (lbl_high);
3935 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3936 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3937 && dwarf_split_debug_info && !force_direct)
3938 attr.dw_attr_val.val_entry
3939 = add_addr_table_entry (lbl_id, ate_kind_label);
3940 else
3941 attr.dw_attr_val.val_entry = NULL;
3942 add_dwarf_attr (die, &attr);
3945 /* Hash and equality functions for debug_str_hash. */
3947 hashval_t
3948 indirect_string_hasher::hash (indirect_string_node *x)
3950 return htab_hash_string (x->str);
3953 bool
3954 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3956 return strcmp (x1->str, x2) == 0;
3959 /* Add STR to the given string hash table. */
3961 static struct indirect_string_node *
3962 find_AT_string_in_table (const char *str,
3963 hash_table<indirect_string_hasher> *table)
3965 struct indirect_string_node *node;
3967 indirect_string_node **slot
3968 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3969 if (*slot == NULL)
3971 node = ggc_cleared_alloc<indirect_string_node> ();
3972 node->str = ggc_strdup (str);
3973 *slot = node;
3975 else
3976 node = *slot;
3978 node->refcount++;
3979 return node;
3982 /* Add STR to the indirect string hash table. */
3984 static struct indirect_string_node *
3985 find_AT_string (const char *str)
3987 if (! debug_str_hash)
3988 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
3990 return find_AT_string_in_table (str, debug_str_hash);
3993 /* Add a string attribute value to a DIE. */
3995 static inline void
3996 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3998 dw_attr_node attr;
3999 struct indirect_string_node *node;
4001 node = find_AT_string (str);
4003 attr.dw_attr = attr_kind;
4004 attr.dw_attr_val.val_class = dw_val_class_str;
4005 attr.dw_attr_val.val_entry = NULL;
4006 attr.dw_attr_val.v.val_str = node;
4007 add_dwarf_attr (die, &attr);
4010 static inline const char *
4011 AT_string (dw_attr_ref a)
4013 gcc_assert (a && AT_class (a) == dw_val_class_str);
4014 return a->dw_attr_val.v.val_str->str;
4017 /* Call this function directly to bypass AT_string_form's logic to put
4018 the string inline in the die. */
4020 static void
4021 set_indirect_string (struct indirect_string_node *node)
4023 char label[32];
4024 /* Already indirect is a no op. */
4025 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4027 gcc_assert (node->label);
4028 return;
4030 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4031 ++dw2_string_counter;
4032 node->label = xstrdup (label);
4034 if (!dwarf_split_debug_info)
4036 node->form = DW_FORM_strp;
4037 node->index = NOT_INDEXED;
4039 else
4041 node->form = DW_FORM_GNU_str_index;
4042 node->index = NO_INDEX_ASSIGNED;
4046 /* Find out whether a string should be output inline in DIE
4047 or out-of-line in .debug_str section. */
4049 static enum dwarf_form
4050 find_string_form (struct indirect_string_node *node)
4052 unsigned int len;
4054 if (node->form)
4055 return node->form;
4057 len = strlen (node->str) + 1;
4059 /* If the string is shorter or equal to the size of the reference, it is
4060 always better to put it inline. */
4061 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4062 return node->form = DW_FORM_string;
4064 /* If we cannot expect the linker to merge strings in .debug_str
4065 section, only put it into .debug_str if it is worth even in this
4066 single module. */
4067 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4068 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4069 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4070 return node->form = DW_FORM_string;
4072 set_indirect_string (node);
4074 return node->form;
4077 /* Find out whether the string referenced from the attribute should be
4078 output inline in DIE or out-of-line in .debug_str section. */
4080 static enum dwarf_form
4081 AT_string_form (dw_attr_ref a)
4083 gcc_assert (a && AT_class (a) == dw_val_class_str);
4084 return find_string_form (a->dw_attr_val.v.val_str);
4087 /* Add a DIE reference attribute value to a DIE. */
4089 static inline void
4090 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4092 dw_attr_node attr;
4094 #ifdef ENABLE_CHECKING
4095 gcc_assert (targ_die != NULL);
4096 #else
4097 /* With LTO we can end up trying to reference something we didn't create
4098 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4099 if (targ_die == NULL)
4100 return;
4101 #endif
4103 attr.dw_attr = attr_kind;
4104 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4105 attr.dw_attr_val.val_entry = NULL;
4106 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4107 attr.dw_attr_val.v.val_die_ref.external = 0;
4108 add_dwarf_attr (die, &attr);
4111 /* Change DIE reference REF to point to NEW_DIE instead. */
4113 static inline void
4114 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4116 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4117 ref->dw_attr_val.v.val_die_ref.die = new_die;
4118 ref->dw_attr_val.v.val_die_ref.external = 0;
4121 /* Add an AT_specification attribute to a DIE, and also make the back
4122 pointer from the specification to the definition. */
4124 static inline void
4125 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4127 add_AT_die_ref (die, DW_AT_specification, targ_die);
4128 gcc_assert (!targ_die->die_definition);
4129 targ_die->die_definition = die;
4132 static inline dw_die_ref
4133 AT_ref (dw_attr_ref a)
4135 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4136 return a->dw_attr_val.v.val_die_ref.die;
4139 static inline int
4140 AT_ref_external (dw_attr_ref a)
4142 if (a && AT_class (a) == dw_val_class_die_ref)
4143 return a->dw_attr_val.v.val_die_ref.external;
4145 return 0;
4148 static inline void
4149 set_AT_ref_external (dw_attr_ref a, int i)
4151 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4152 a->dw_attr_val.v.val_die_ref.external = i;
4155 /* Add an FDE reference attribute value to a DIE. */
4157 static inline void
4158 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4160 dw_attr_node attr;
4162 attr.dw_attr = attr_kind;
4163 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4164 attr.dw_attr_val.val_entry = NULL;
4165 attr.dw_attr_val.v.val_fde_index = targ_fde;
4166 add_dwarf_attr (die, &attr);
4169 /* Add a location description attribute value to a DIE. */
4171 static inline void
4172 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4174 dw_attr_node attr;
4176 attr.dw_attr = attr_kind;
4177 attr.dw_attr_val.val_class = dw_val_class_loc;
4178 attr.dw_attr_val.val_entry = NULL;
4179 attr.dw_attr_val.v.val_loc = loc;
4180 add_dwarf_attr (die, &attr);
4183 static inline dw_loc_descr_ref
4184 AT_loc (dw_attr_ref a)
4186 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4187 return a->dw_attr_val.v.val_loc;
4190 static inline void
4191 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4193 dw_attr_node attr;
4195 attr.dw_attr = attr_kind;
4196 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4197 attr.dw_attr_val.val_entry = NULL;
4198 attr.dw_attr_val.v.val_loc_list = loc_list;
4199 add_dwarf_attr (die, &attr);
4200 have_location_lists = true;
4203 static inline dw_loc_list_ref
4204 AT_loc_list (dw_attr_ref a)
4206 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4207 return a->dw_attr_val.v.val_loc_list;
4210 static inline dw_loc_list_ref *
4211 AT_loc_list_ptr (dw_attr_ref a)
4213 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4214 return &a->dw_attr_val.v.val_loc_list;
4217 struct addr_hasher : ggc_hasher<addr_table_entry *>
4219 static hashval_t hash (addr_table_entry *);
4220 static bool equal (addr_table_entry *, addr_table_entry *);
4223 /* Table of entries into the .debug_addr section. */
4225 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4227 /* Hash an address_table_entry. */
4229 hashval_t
4230 addr_hasher::hash (addr_table_entry *a)
4232 inchash::hash hstate;
4233 switch (a->kind)
4235 case ate_kind_rtx:
4236 hstate.add_int (0);
4237 break;
4238 case ate_kind_rtx_dtprel:
4239 hstate.add_int (1);
4240 break;
4241 case ate_kind_label:
4242 return htab_hash_string (a->addr.label);
4243 default:
4244 gcc_unreachable ();
4246 inchash::add_rtx (a->addr.rtl, hstate);
4247 return hstate.end ();
4250 /* Determine equality for two address_table_entries. */
4252 bool
4253 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4255 if (a1->kind != a2->kind)
4256 return 0;
4257 switch (a1->kind)
4259 case ate_kind_rtx:
4260 case ate_kind_rtx_dtprel:
4261 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4262 case ate_kind_label:
4263 return strcmp (a1->addr.label, a2->addr.label) == 0;
4264 default:
4265 gcc_unreachable ();
4269 /* Initialize an addr_table_entry. */
4271 void
4272 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4274 e->kind = kind;
4275 switch (kind)
4277 case ate_kind_rtx:
4278 case ate_kind_rtx_dtprel:
4279 e->addr.rtl = (rtx) addr;
4280 break;
4281 case ate_kind_label:
4282 e->addr.label = (char *) addr;
4283 break;
4285 e->refcount = 0;
4286 e->index = NO_INDEX_ASSIGNED;
4289 /* Add attr to the address table entry to the table. Defer setting an
4290 index until output time. */
4292 static addr_table_entry *
4293 add_addr_table_entry (void *addr, enum ate_kind kind)
4295 addr_table_entry *node;
4296 addr_table_entry finder;
4298 gcc_assert (dwarf_split_debug_info);
4299 if (! addr_index_table)
4300 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4301 init_addr_table_entry (&finder, kind, addr);
4302 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4304 if (*slot == HTAB_EMPTY_ENTRY)
4306 node = ggc_cleared_alloc<addr_table_entry> ();
4307 init_addr_table_entry (node, kind, addr);
4308 *slot = node;
4310 else
4311 node = *slot;
4313 node->refcount++;
4314 return node;
4317 /* Remove an entry from the addr table by decrementing its refcount.
4318 Strictly, decrementing the refcount would be enough, but the
4319 assertion that the entry is actually in the table has found
4320 bugs. */
4322 static void
4323 remove_addr_table_entry (addr_table_entry *entry)
4325 gcc_assert (dwarf_split_debug_info && addr_index_table);
4326 /* After an index is assigned, the table is frozen. */
4327 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4328 entry->refcount--;
4331 /* Given a location list, remove all addresses it refers to from the
4332 address_table. */
4334 static void
4335 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4337 for (; descr; descr = descr->dw_loc_next)
4338 if (descr->dw_loc_oprnd1.val_entry != NULL)
4340 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4341 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4345 /* A helper function for dwarf2out_finish called through
4346 htab_traverse. Assign an addr_table_entry its index. All entries
4347 must be collected into the table when this function is called,
4348 because the indexing code relies on htab_traverse to traverse nodes
4349 in the same order for each run. */
4352 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4354 addr_table_entry *node = *h;
4356 /* Don't index unreferenced nodes. */
4357 if (node->refcount == 0)
4358 return 1;
4360 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4361 node->index = *index;
4362 *index += 1;
4364 return 1;
4367 /* Add an address constant attribute value to a DIE. When using
4368 dwarf_split_debug_info, address attributes in dies destined for the
4369 final executable should be direct references--setting the parameter
4370 force_direct ensures this behavior. */
4372 static inline void
4373 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4374 bool force_direct)
4376 dw_attr_node attr;
4378 attr.dw_attr = attr_kind;
4379 attr.dw_attr_val.val_class = dw_val_class_addr;
4380 attr.dw_attr_val.v.val_addr = addr;
4381 if (dwarf_split_debug_info && !force_direct)
4382 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4383 else
4384 attr.dw_attr_val.val_entry = NULL;
4385 add_dwarf_attr (die, &attr);
4388 /* Get the RTX from to an address DIE attribute. */
4390 static inline rtx
4391 AT_addr (dw_attr_ref a)
4393 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4394 return a->dw_attr_val.v.val_addr;
4397 /* Add a file attribute value to a DIE. */
4399 static inline void
4400 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4401 struct dwarf_file_data *fd)
4403 dw_attr_node attr;
4405 attr.dw_attr = attr_kind;
4406 attr.dw_attr_val.val_class = dw_val_class_file;
4407 attr.dw_attr_val.val_entry = NULL;
4408 attr.dw_attr_val.v.val_file = fd;
4409 add_dwarf_attr (die, &attr);
4412 /* Get the dwarf_file_data from a file DIE attribute. */
4414 static inline struct dwarf_file_data *
4415 AT_file (dw_attr_ref a)
4417 gcc_assert (a && AT_class (a) == dw_val_class_file);
4418 return a->dw_attr_val.v.val_file;
4421 /* Add a vms delta attribute value to a DIE. */
4423 static inline void
4424 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4425 const char *lbl1, const char *lbl2)
4427 dw_attr_node attr;
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4433 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4434 add_dwarf_attr (die, &attr);
4437 /* Add a label identifier attribute value to a DIE. */
4439 static inline void
4440 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4441 const char *lbl_id)
4443 dw_attr_node attr;
4445 attr.dw_attr = attr_kind;
4446 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4447 attr.dw_attr_val.val_entry = NULL;
4448 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4449 if (dwarf_split_debug_info)
4450 attr.dw_attr_val.val_entry
4451 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4452 ate_kind_label);
4453 add_dwarf_attr (die, &attr);
4456 /* Add a section offset attribute value to a DIE, an offset into the
4457 debug_line section. */
4459 static inline void
4460 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4461 const char *label)
4463 dw_attr_node attr;
4465 attr.dw_attr = attr_kind;
4466 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4467 attr.dw_attr_val.val_entry = NULL;
4468 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4469 add_dwarf_attr (die, &attr);
4472 /* Add a section offset attribute value to a DIE, an offset into the
4473 debug_macinfo section. */
4475 static inline void
4476 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4477 const char *label)
4479 dw_attr_node attr;
4481 attr.dw_attr = attr_kind;
4482 attr.dw_attr_val.val_class = dw_val_class_macptr;
4483 attr.dw_attr_val.val_entry = NULL;
4484 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4485 add_dwarf_attr (die, &attr);
4488 /* Add an offset attribute value to a DIE. */
4490 static inline void
4491 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4492 unsigned HOST_WIDE_INT offset)
4494 dw_attr_node attr;
4496 attr.dw_attr = attr_kind;
4497 attr.dw_attr_val.val_class = dw_val_class_offset;
4498 attr.dw_attr_val.val_entry = NULL;
4499 attr.dw_attr_val.v.val_offset = offset;
4500 add_dwarf_attr (die, &attr);
4503 /* Add a range_list attribute value to a DIE. When using
4504 dwarf_split_debug_info, address attributes in dies destined for the
4505 final executable should be direct references--setting the parameter
4506 force_direct ensures this behavior. */
4508 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4509 #define RELOCATED_OFFSET (NULL)
4511 static void
4512 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4513 long unsigned int offset, bool force_direct)
4515 dw_attr_node attr;
4517 attr.dw_attr = attr_kind;
4518 attr.dw_attr_val.val_class = dw_val_class_range_list;
4519 /* For the range_list attribute, use val_entry to store whether the
4520 offset should follow split-debug-info or normal semantics. This
4521 value is read in output_range_list_offset. */
4522 if (dwarf_split_debug_info && !force_direct)
4523 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4524 else
4525 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4526 attr.dw_attr_val.v.val_offset = offset;
4527 add_dwarf_attr (die, &attr);
4530 /* Return the start label of a delta attribute. */
4532 static inline const char *
4533 AT_vms_delta1 (dw_attr_ref a)
4535 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4536 return a->dw_attr_val.v.val_vms_delta.lbl1;
4539 /* Return the end label of a delta attribute. */
4541 static inline const char *
4542 AT_vms_delta2 (dw_attr_ref a)
4544 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4545 return a->dw_attr_val.v.val_vms_delta.lbl2;
4548 static inline const char *
4549 AT_lbl (dw_attr_ref a)
4551 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4552 || AT_class (a) == dw_val_class_lineptr
4553 || AT_class (a) == dw_val_class_macptr
4554 || AT_class (a) == dw_val_class_high_pc));
4555 return a->dw_attr_val.v.val_lbl_id;
4558 /* Get the attribute of type attr_kind. */
4560 static dw_attr_ref
4561 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4563 dw_attr_ref a;
4564 unsigned ix;
4565 dw_die_ref spec = NULL;
4567 if (! die)
4568 return NULL;
4570 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4571 if (a->dw_attr == attr_kind)
4572 return a;
4573 else if (a->dw_attr == DW_AT_specification
4574 || a->dw_attr == DW_AT_abstract_origin)
4575 spec = AT_ref (a);
4577 if (spec)
4578 return get_AT (spec, attr_kind);
4580 return NULL;
4583 /* Returns the parent of the declaration of DIE. */
4585 static dw_die_ref
4586 get_die_parent (dw_die_ref die)
4588 dw_die_ref t;
4590 if (!die)
4591 return NULL;
4593 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4594 || (t = get_AT_ref (die, DW_AT_specification)))
4595 die = t;
4597 return die->die_parent;
4600 /* Return the "low pc" attribute value, typically associated with a subprogram
4601 DIE. Return null if the "low pc" attribute is either not present, or if it
4602 cannot be represented as an assembler label identifier. */
4604 static inline const char *
4605 get_AT_low_pc (dw_die_ref die)
4607 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4609 return a ? AT_lbl (a) : NULL;
4612 /* Return the "high pc" attribute value, typically associated with a subprogram
4613 DIE. Return null if the "high pc" attribute is either not present, or if it
4614 cannot be represented as an assembler label identifier. */
4616 static inline const char *
4617 get_AT_hi_pc (dw_die_ref die)
4619 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4621 return a ? AT_lbl (a) : NULL;
4624 /* Return the value of the string attribute designated by ATTR_KIND, or
4625 NULL if it is not present. */
4627 static inline const char *
4628 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4630 dw_attr_ref a = get_AT (die, attr_kind);
4632 return a ? AT_string (a) : NULL;
4635 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4636 if it is not present. */
4638 static inline int
4639 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4641 dw_attr_ref a = get_AT (die, attr_kind);
4643 return a ? AT_flag (a) : 0;
4646 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4647 if it is not present. */
4649 static inline unsigned
4650 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4652 dw_attr_ref a = get_AT (die, attr_kind);
4654 return a ? AT_unsigned (a) : 0;
4657 static inline dw_die_ref
4658 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4660 dw_attr_ref a = get_AT (die, attr_kind);
4662 return a ? AT_ref (a) : NULL;
4665 static inline struct dwarf_file_data *
4666 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4668 dw_attr_ref a = get_AT (die, attr_kind);
4670 return a ? AT_file (a) : NULL;
4673 /* Return TRUE if the language is C++. */
4675 static inline bool
4676 is_cxx (void)
4678 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4680 return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4683 /* Return TRUE if the language is Java. */
4685 static inline bool
4686 is_java (void)
4688 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4690 return lang == DW_LANG_Java;
4693 /* Return TRUE if the language is Fortran. */
4695 static inline bool
4696 is_fortran (void)
4698 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4700 return (lang == DW_LANG_Fortran77
4701 || lang == DW_LANG_Fortran90
4702 || lang == DW_LANG_Fortran95);
4705 /* Return TRUE if the language is Ada. */
4707 static inline bool
4708 is_ada (void)
4710 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4712 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4715 /* Remove the specified attribute if present. */
4717 static void
4718 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4720 dw_attr_ref a;
4721 unsigned ix;
4723 if (! die)
4724 return;
4726 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4727 if (a->dw_attr == attr_kind)
4729 if (AT_class (a) == dw_val_class_str)
4730 if (a->dw_attr_val.v.val_str->refcount)
4731 a->dw_attr_val.v.val_str->refcount--;
4733 /* vec::ordered_remove should help reduce the number of abbrevs
4734 that are needed. */
4735 die->die_attr->ordered_remove (ix);
4736 return;
4740 /* Remove CHILD from its parent. PREV must have the property that
4741 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4743 static void
4744 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4746 gcc_assert (child->die_parent == prev->die_parent);
4747 gcc_assert (prev->die_sib == child);
4748 if (prev == child)
4750 gcc_assert (child->die_parent->die_child == child);
4751 prev = NULL;
4753 else
4754 prev->die_sib = child->die_sib;
4755 if (child->die_parent->die_child == child)
4756 child->die_parent->die_child = prev;
4759 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4760 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4762 static void
4763 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4765 dw_die_ref parent = old_child->die_parent;
4767 gcc_assert (parent == prev->die_parent);
4768 gcc_assert (prev->die_sib == old_child);
4770 new_child->die_parent = parent;
4771 if (prev == old_child)
4773 gcc_assert (parent->die_child == old_child);
4774 new_child->die_sib = new_child;
4776 else
4778 prev->die_sib = new_child;
4779 new_child->die_sib = old_child->die_sib;
4781 if (old_child->die_parent->die_child == old_child)
4782 old_child->die_parent->die_child = new_child;
4785 /* Move all children from OLD_PARENT to NEW_PARENT. */
4787 static void
4788 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4790 dw_die_ref c;
4791 new_parent->die_child = old_parent->die_child;
4792 old_parent->die_child = NULL;
4793 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4796 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4797 matches TAG. */
4799 static void
4800 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4802 dw_die_ref c;
4804 c = die->die_child;
4805 if (c) do {
4806 dw_die_ref prev = c;
4807 c = c->die_sib;
4808 while (c->die_tag == tag)
4810 remove_child_with_prev (c, prev);
4811 /* Might have removed every child. */
4812 if (c == c->die_sib)
4813 return;
4814 c = c->die_sib;
4816 } while (c != die->die_child);
4819 /* Add a CHILD_DIE as the last child of DIE. */
4821 static void
4822 add_child_die (dw_die_ref die, dw_die_ref child_die)
4824 /* FIXME this should probably be an assert. */
4825 if (! die || ! child_die)
4826 return;
4827 gcc_assert (die != child_die);
4829 child_die->die_parent = die;
4830 if (die->die_child)
4832 child_die->die_sib = die->die_child->die_sib;
4833 die->die_child->die_sib = child_die;
4835 else
4836 child_die->die_sib = child_die;
4837 die->die_child = child_die;
4840 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4841 is the specification, to the end of PARENT's list of children.
4842 This is done by removing and re-adding it. */
4844 static void
4845 splice_child_die (dw_die_ref parent, dw_die_ref child)
4847 dw_die_ref p;
4849 /* We want the declaration DIE from inside the class, not the
4850 specification DIE at toplevel. */
4851 if (child->die_parent != parent)
4853 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4855 if (tmp)
4856 child = tmp;
4859 gcc_assert (child->die_parent == parent
4860 || (child->die_parent
4861 == get_AT_ref (parent, DW_AT_specification)));
4863 for (p = child->die_parent->die_child; ; p = p->die_sib)
4864 if (p->die_sib == child)
4866 remove_child_with_prev (child, p);
4867 break;
4870 add_child_die (parent, child);
4873 /* Return a pointer to a newly created DIE node. */
4875 static inline dw_die_ref
4876 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4878 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4880 die->die_tag = tag_value;
4882 if (parent_die != NULL)
4883 add_child_die (parent_die, die);
4884 else
4886 limbo_die_node *limbo_node;
4888 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4889 limbo_node->die = die;
4890 limbo_node->created_for = t;
4891 limbo_node->next = limbo_die_list;
4892 limbo_die_list = limbo_node;
4895 return die;
4898 /* Return the DIE associated with the given type specifier. */
4900 static inline dw_die_ref
4901 lookup_type_die (tree type)
4903 return TYPE_SYMTAB_DIE (type);
4906 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4907 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4908 anonymous type instead the one of the naming typedef. */
4910 static inline dw_die_ref
4911 strip_naming_typedef (tree type, dw_die_ref type_die)
4913 if (type
4914 && TREE_CODE (type) == RECORD_TYPE
4915 && type_die
4916 && type_die->die_tag == DW_TAG_typedef
4917 && is_naming_typedef_decl (TYPE_NAME (type)))
4918 type_die = get_AT_ref (type_die, DW_AT_type);
4919 return type_die;
4922 /* Like lookup_type_die, but if type is an anonymous type named by a
4923 typedef[1], return the DIE of the anonymous type instead the one of
4924 the naming typedef. This is because in gen_typedef_die, we did
4925 equate the anonymous struct named by the typedef with the DIE of
4926 the naming typedef. So by default, lookup_type_die on an anonymous
4927 struct yields the DIE of the naming typedef.
4929 [1]: Read the comment of is_naming_typedef_decl to learn about what
4930 a naming typedef is. */
4932 static inline dw_die_ref
4933 lookup_type_die_strip_naming_typedef (tree type)
4935 dw_die_ref die = lookup_type_die (type);
4936 return strip_naming_typedef (type, die);
4939 /* Equate a DIE to a given type specifier. */
4941 static inline void
4942 equate_type_number_to_die (tree type, dw_die_ref type_die)
4944 TYPE_SYMTAB_DIE (type) = type_die;
4947 /* Returns a hash value for X (which really is a die_struct). */
4949 inline hashval_t
4950 decl_die_hasher::hash (die_node *x)
4952 return (hashval_t) x->decl_id;
4955 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
4957 inline bool
4958 decl_die_hasher::equal (die_node *x, tree y)
4960 return (x->decl_id == DECL_UID (y));
4963 /* Return the DIE associated with a given declaration. */
4965 static inline dw_die_ref
4966 lookup_decl_die (tree decl)
4968 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
4971 /* Returns a hash value for X (which really is a var_loc_list). */
4973 inline hashval_t
4974 decl_loc_hasher::hash (var_loc_list *x)
4976 return (hashval_t) x->decl_id;
4979 /* Return nonzero if decl_id of var_loc_list X is the same as
4980 UID of decl *Y. */
4982 inline bool
4983 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
4985 return (x->decl_id == DECL_UID (y));
4988 /* Return the var_loc list associated with a given declaration. */
4990 static inline var_loc_list *
4991 lookup_decl_loc (const_tree decl)
4993 if (!decl_loc_table)
4994 return NULL;
4995 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
4998 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5000 inline hashval_t
5001 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5003 return (hashval_t) x->decl_id;
5006 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5007 UID of decl *Y. */
5009 inline bool
5010 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5012 return (x->decl_id == DECL_UID (y));
5015 /* Equate a DIE to a particular declaration. */
5017 static void
5018 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5020 unsigned int decl_id = DECL_UID (decl);
5022 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5023 decl_die->decl_id = decl_id;
5026 /* Return how many bits covers PIECE EXPR_LIST. */
5028 static int
5029 decl_piece_bitsize (rtx piece)
5031 int ret = (int) GET_MODE (piece);
5032 if (ret)
5033 return ret;
5034 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5035 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5036 return INTVAL (XEXP (XEXP (piece, 0), 0));
5039 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5041 static rtx *
5042 decl_piece_varloc_ptr (rtx piece)
5044 if ((int) GET_MODE (piece))
5045 return &XEXP (piece, 0);
5046 else
5047 return &XEXP (XEXP (piece, 0), 1);
5050 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5051 Next is the chain of following piece nodes. */
5053 static rtx_expr_list *
5054 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5056 if (bitsize <= (int) MAX_MACHINE_MODE)
5057 return alloc_EXPR_LIST (bitsize, loc_note, next);
5058 else
5059 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5060 GEN_INT (bitsize),
5061 loc_note), next);
5064 /* Return rtx that should be stored into loc field for
5065 LOC_NOTE and BITPOS/BITSIZE. */
5067 static rtx
5068 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5069 HOST_WIDE_INT bitsize)
5071 if (bitsize != -1)
5073 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5074 if (bitpos != 0)
5075 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5077 return loc_note;
5080 /* This function either modifies location piece list *DEST in
5081 place (if SRC and INNER is NULL), or copies location piece list
5082 *SRC to *DEST while modifying it. Location BITPOS is modified
5083 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5084 not copied and if needed some padding around it is added.
5085 When modifying in place, DEST should point to EXPR_LIST where
5086 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5087 to the start of the whole list and INNER points to the EXPR_LIST
5088 where earlier pieces cover PIECE_BITPOS bits. */
5090 static void
5091 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5092 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5093 HOST_WIDE_INT bitsize, rtx loc_note)
5095 int diff;
5096 bool copy = inner != NULL;
5098 if (copy)
5100 /* First copy all nodes preceding the current bitpos. */
5101 while (src != inner)
5103 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5104 decl_piece_bitsize (*src), NULL_RTX);
5105 dest = &XEXP (*dest, 1);
5106 src = &XEXP (*src, 1);
5109 /* Add padding if needed. */
5110 if (bitpos != piece_bitpos)
5112 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5113 copy ? NULL_RTX : *dest);
5114 dest = &XEXP (*dest, 1);
5116 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5118 gcc_assert (!copy);
5119 /* A piece with correct bitpos and bitsize already exist,
5120 just update the location for it and return. */
5121 *decl_piece_varloc_ptr (*dest) = loc_note;
5122 return;
5124 /* Add the piece that changed. */
5125 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5126 dest = &XEXP (*dest, 1);
5127 /* Skip over pieces that overlap it. */
5128 diff = bitpos - piece_bitpos + bitsize;
5129 if (!copy)
5130 src = dest;
5131 while (diff > 0 && *src)
5133 rtx piece = *src;
5134 diff -= decl_piece_bitsize (piece);
5135 if (copy)
5136 src = &XEXP (piece, 1);
5137 else
5139 *src = XEXP (piece, 1);
5140 free_EXPR_LIST_node (piece);
5143 /* Add padding if needed. */
5144 if (diff < 0 && *src)
5146 if (!copy)
5147 dest = src;
5148 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5149 dest = &XEXP (*dest, 1);
5151 if (!copy)
5152 return;
5153 /* Finally copy all nodes following it. */
5154 while (*src)
5156 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5157 decl_piece_bitsize (*src), NULL_RTX);
5158 dest = &XEXP (*dest, 1);
5159 src = &XEXP (*src, 1);
5163 /* Add a variable location node to the linked list for DECL. */
5165 static struct var_loc_node *
5166 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5168 unsigned int decl_id;
5169 var_loc_list *temp;
5170 struct var_loc_node *loc = NULL;
5171 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5173 if (TREE_CODE (decl) == VAR_DECL
5174 && DECL_HAS_DEBUG_EXPR_P (decl))
5176 tree realdecl = DECL_DEBUG_EXPR (decl);
5177 if (handled_component_p (realdecl)
5178 || (TREE_CODE (realdecl) == MEM_REF
5179 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5181 HOST_WIDE_INT maxsize;
5182 tree innerdecl;
5183 innerdecl
5184 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5185 if (!DECL_P (innerdecl)
5186 || DECL_IGNORED_P (innerdecl)
5187 || TREE_STATIC (innerdecl)
5188 || bitsize <= 0
5189 || bitpos + bitsize > 256
5190 || bitsize != maxsize)
5191 return NULL;
5192 decl = innerdecl;
5196 decl_id = DECL_UID (decl);
5197 var_loc_list **slot
5198 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5199 if (*slot == NULL)
5201 temp = ggc_cleared_alloc<var_loc_list> ();
5202 temp->decl_id = decl_id;
5203 *slot = temp;
5205 else
5206 temp = *slot;
5208 /* For PARM_DECLs try to keep around the original incoming value,
5209 even if that means we'll emit a zero-range .debug_loc entry. */
5210 if (temp->last
5211 && temp->first == temp->last
5212 && TREE_CODE (decl) == PARM_DECL
5213 && NOTE_P (temp->first->loc)
5214 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5215 && DECL_INCOMING_RTL (decl)
5216 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5217 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5218 == GET_CODE (DECL_INCOMING_RTL (decl))
5219 && prev_real_insn (temp->first->loc) == NULL_RTX
5220 && (bitsize != -1
5221 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5222 NOTE_VAR_LOCATION_LOC (loc_note))
5223 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5224 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5226 loc = ggc_cleared_alloc<var_loc_node> ();
5227 temp->first->next = loc;
5228 temp->last = loc;
5229 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5231 else if (temp->last)
5233 struct var_loc_node *last = temp->last, *unused = NULL;
5234 rtx *piece_loc = NULL, last_loc_note;
5235 int piece_bitpos = 0;
5236 if (last->next)
5238 last = last->next;
5239 gcc_assert (last->next == NULL);
5241 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5243 piece_loc = &last->loc;
5246 int cur_bitsize = decl_piece_bitsize (*piece_loc);
5247 if (piece_bitpos + cur_bitsize > bitpos)
5248 break;
5249 piece_bitpos += cur_bitsize;
5250 piece_loc = &XEXP (*piece_loc, 1);
5252 while (*piece_loc);
5254 /* TEMP->LAST here is either pointer to the last but one or
5255 last element in the chained list, LAST is pointer to the
5256 last element. */
5257 if (label && strcmp (last->label, label) == 0)
5259 /* For SRA optimized variables if there weren't any real
5260 insns since last note, just modify the last node. */
5261 if (piece_loc != NULL)
5263 adjust_piece_list (piece_loc, NULL, NULL,
5264 bitpos, piece_bitpos, bitsize, loc_note);
5265 return NULL;
5267 /* If the last note doesn't cover any instructions, remove it. */
5268 if (temp->last != last)
5270 temp->last->next = NULL;
5271 unused = last;
5272 last = temp->last;
5273 gcc_assert (strcmp (last->label, label) != 0);
5275 else
5277 gcc_assert (temp->first == temp->last
5278 || (temp->first->next == temp->last
5279 && TREE_CODE (decl) == PARM_DECL));
5280 memset (temp->last, '\0', sizeof (*temp->last));
5281 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5282 return temp->last;
5285 if (bitsize == -1 && NOTE_P (last->loc))
5286 last_loc_note = last->loc;
5287 else if (piece_loc != NULL
5288 && *piece_loc != NULL_RTX
5289 && piece_bitpos == bitpos
5290 && decl_piece_bitsize (*piece_loc) == bitsize)
5291 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5292 else
5293 last_loc_note = NULL_RTX;
5294 /* If the current location is the same as the end of the list,
5295 and either both or neither of the locations is uninitialized,
5296 we have nothing to do. */
5297 if (last_loc_note == NULL_RTX
5298 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5299 NOTE_VAR_LOCATION_LOC (loc_note)))
5300 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5301 != NOTE_VAR_LOCATION_STATUS (loc_note))
5302 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5303 == VAR_INIT_STATUS_UNINITIALIZED)
5304 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5305 == VAR_INIT_STATUS_UNINITIALIZED))))
5307 /* Add LOC to the end of list and update LAST. If the last
5308 element of the list has been removed above, reuse its
5309 memory for the new node, otherwise allocate a new one. */
5310 if (unused)
5312 loc = unused;
5313 memset (loc, '\0', sizeof (*loc));
5315 else
5316 loc = ggc_cleared_alloc<var_loc_node> ();
5317 if (bitsize == -1 || piece_loc == NULL)
5318 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5319 else
5320 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5321 bitpos, piece_bitpos, bitsize, loc_note);
5322 last->next = loc;
5323 /* Ensure TEMP->LAST will point either to the new last but one
5324 element of the chain, or to the last element in it. */
5325 if (last != temp->last)
5326 temp->last = last;
5328 else if (unused)
5329 ggc_free (unused);
5331 else
5333 loc = ggc_cleared_alloc<var_loc_node> ();
5334 temp->first = loc;
5335 temp->last = loc;
5336 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5338 return loc;
5341 /* Keep track of the number of spaces used to indent the
5342 output of the debugging routines that print the structure of
5343 the DIE internal representation. */
5344 static int print_indent;
5346 /* Indent the line the number of spaces given by print_indent. */
5348 static inline void
5349 print_spaces (FILE *outfile)
5351 fprintf (outfile, "%*s", print_indent, "");
5354 /* Print a type signature in hex. */
5356 static inline void
5357 print_signature (FILE *outfile, char *sig)
5359 int i;
5361 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5362 fprintf (outfile, "%02x", sig[i] & 0xff);
5365 /* Print the information associated with a given DIE, and its children.
5366 This routine is a debugging aid only. */
5368 static void
5369 print_die (dw_die_ref die, FILE *outfile)
5371 dw_attr_ref a;
5372 dw_die_ref c;
5373 unsigned ix;
5375 print_spaces (outfile);
5376 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5377 die->die_offset, dwarf_tag_name (die->die_tag),
5378 (void*) die);
5379 print_spaces (outfile);
5380 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5381 fprintf (outfile, " offset: %ld", die->die_offset);
5382 fprintf (outfile, " mark: %d\n", die->die_mark);
5384 if (die->comdat_type_p)
5386 print_spaces (outfile);
5387 fprintf (outfile, " signature: ");
5388 print_signature (outfile, die->die_id.die_type_node->signature);
5389 fprintf (outfile, "\n");
5392 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5394 print_spaces (outfile);
5395 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5397 switch (AT_class (a))
5399 case dw_val_class_addr:
5400 fprintf (outfile, "address");
5401 break;
5402 case dw_val_class_offset:
5403 fprintf (outfile, "offset");
5404 break;
5405 case dw_val_class_loc:
5406 fprintf (outfile, "location descriptor");
5407 break;
5408 case dw_val_class_loc_list:
5409 fprintf (outfile, "location list -> label:%s",
5410 AT_loc_list (a)->ll_symbol);
5411 break;
5412 case dw_val_class_range_list:
5413 fprintf (outfile, "range list");
5414 break;
5415 case dw_val_class_const:
5416 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5417 break;
5418 case dw_val_class_unsigned_const:
5419 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5420 break;
5421 case dw_val_class_const_double:
5422 fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5423 HOST_WIDE_INT_PRINT_UNSIGNED")",
5424 a->dw_attr_val.v.val_double.high,
5425 a->dw_attr_val.v.val_double.low);
5426 break;
5427 case dw_val_class_wide_int:
5429 int i = a->dw_attr_val.v.val_wide->get_len ();
5430 fprintf (outfile, "constant (");
5431 gcc_assert (i > 0);
5432 if (a->dw_attr_val.v.val_wide->elt (i - 1) == 0)
5433 fprintf (outfile, "0x");
5434 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5435 a->dw_attr_val.v.val_wide->elt (--i));
5436 while (--i >= 0)
5437 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5438 a->dw_attr_val.v.val_wide->elt (i));
5439 fprintf (outfile, ")");
5440 break;
5442 case dw_val_class_vec:
5443 fprintf (outfile, "floating-point or vector constant");
5444 break;
5445 case dw_val_class_flag:
5446 fprintf (outfile, "%u", AT_flag (a));
5447 break;
5448 case dw_val_class_die_ref:
5449 if (AT_ref (a) != NULL)
5451 if (AT_ref (a)->comdat_type_p)
5453 fprintf (outfile, "die -> signature: ");
5454 print_signature (outfile,
5455 AT_ref (a)->die_id.die_type_node->signature);
5457 else if (AT_ref (a)->die_id.die_symbol)
5458 fprintf (outfile, "die -> label: %s",
5459 AT_ref (a)->die_id.die_symbol);
5460 else
5461 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5462 fprintf (outfile, " (%p)", (void *) AT_ref (a));
5464 else
5465 fprintf (outfile, "die -> <null>");
5466 break;
5467 case dw_val_class_vms_delta:
5468 fprintf (outfile, "delta: @slotcount(%s-%s)",
5469 AT_vms_delta2 (a), AT_vms_delta1 (a));
5470 break;
5471 case dw_val_class_lbl_id:
5472 case dw_val_class_lineptr:
5473 case dw_val_class_macptr:
5474 case dw_val_class_high_pc:
5475 fprintf (outfile, "label: %s", AT_lbl (a));
5476 break;
5477 case dw_val_class_str:
5478 if (AT_string (a) != NULL)
5479 fprintf (outfile, "\"%s\"", AT_string (a));
5480 else
5481 fprintf (outfile, "<null>");
5482 break;
5483 case dw_val_class_file:
5484 fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5485 AT_file (a)->emitted_number);
5486 break;
5487 case dw_val_class_data8:
5489 int i;
5491 for (i = 0; i < 8; i++)
5492 fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5493 break;
5495 default:
5496 break;
5499 fprintf (outfile, "\n");
5502 if (die->die_child != NULL)
5504 print_indent += 4;
5505 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5506 print_indent -= 4;
5508 if (print_indent == 0)
5509 fprintf (outfile, "\n");
5512 /* Print the information collected for a given DIE. */
5514 DEBUG_FUNCTION void
5515 debug_dwarf_die (dw_die_ref die)
5517 print_die (die, stderr);
5520 DEBUG_FUNCTION void
5521 debug (die_struct &ref)
5523 print_die (&ref, stderr);
5526 DEBUG_FUNCTION void
5527 debug (die_struct *ptr)
5529 if (ptr)
5530 debug (*ptr);
5531 else
5532 fprintf (stderr, "<nil>\n");
5536 /* Print all DWARF information collected for the compilation unit.
5537 This routine is a debugging aid only. */
5539 DEBUG_FUNCTION void
5540 debug_dwarf (void)
5542 print_indent = 0;
5543 print_die (comp_unit_die (), stderr);
5546 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5547 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5548 DIE that marks the start of the DIEs for this include file. */
5550 static dw_die_ref
5551 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5553 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5554 dw_die_ref new_unit = gen_compile_unit_die (filename);
5556 new_unit->die_sib = old_unit;
5557 return new_unit;
5560 /* Close an include-file CU and reopen the enclosing one. */
5562 static dw_die_ref
5563 pop_compile_unit (dw_die_ref old_unit)
5565 dw_die_ref new_unit = old_unit->die_sib;
5567 old_unit->die_sib = NULL;
5568 return new_unit;
5571 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5572 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5573 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5575 /* Calculate the checksum of a location expression. */
5577 static inline void
5578 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5580 int tem;
5581 inchash::hash hstate;
5582 hashval_t hash;
5584 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5585 CHECKSUM (tem);
5586 hash_loc_operands (loc, hstate);
5587 hash = hstate.end();
5588 CHECKSUM (hash);
5591 /* Calculate the checksum of an attribute. */
5593 static void
5594 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5596 dw_loc_descr_ref loc;
5597 rtx r;
5599 CHECKSUM (at->dw_attr);
5601 /* We don't care that this was compiled with a different compiler
5602 snapshot; if the output is the same, that's what matters. */
5603 if (at->dw_attr == DW_AT_producer)
5604 return;
5606 switch (AT_class (at))
5608 case dw_val_class_const:
5609 CHECKSUM (at->dw_attr_val.v.val_int);
5610 break;
5611 case dw_val_class_unsigned_const:
5612 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5613 break;
5614 case dw_val_class_const_double:
5615 CHECKSUM (at->dw_attr_val.v.val_double);
5616 break;
5617 case dw_val_class_wide_int:
5618 CHECKSUM (*at->dw_attr_val.v.val_wide);
5619 break;
5620 case dw_val_class_vec:
5621 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5622 (at->dw_attr_val.v.val_vec.length
5623 * at->dw_attr_val.v.val_vec.elt_size));
5624 break;
5625 case dw_val_class_flag:
5626 CHECKSUM (at->dw_attr_val.v.val_flag);
5627 break;
5628 case dw_val_class_str:
5629 CHECKSUM_STRING (AT_string (at));
5630 break;
5632 case dw_val_class_addr:
5633 r = AT_addr (at);
5634 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5635 CHECKSUM_STRING (XSTR (r, 0));
5636 break;
5638 case dw_val_class_offset:
5639 CHECKSUM (at->dw_attr_val.v.val_offset);
5640 break;
5642 case dw_val_class_loc:
5643 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5644 loc_checksum (loc, ctx);
5645 break;
5647 case dw_val_class_die_ref:
5648 die_checksum (AT_ref (at), ctx, mark);
5649 break;
5651 case dw_val_class_fde_ref:
5652 case dw_val_class_vms_delta:
5653 case dw_val_class_lbl_id:
5654 case dw_val_class_lineptr:
5655 case dw_val_class_macptr:
5656 case dw_val_class_high_pc:
5657 break;
5659 case dw_val_class_file:
5660 CHECKSUM_STRING (AT_file (at)->filename);
5661 break;
5663 case dw_val_class_data8:
5664 CHECKSUM (at->dw_attr_val.v.val_data8);
5665 break;
5667 default:
5668 break;
5672 /* Calculate the checksum of a DIE. */
5674 static void
5675 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5677 dw_die_ref c;
5678 dw_attr_ref a;
5679 unsigned ix;
5681 /* To avoid infinite recursion. */
5682 if (die->die_mark)
5684 CHECKSUM (die->die_mark);
5685 return;
5687 die->die_mark = ++(*mark);
5689 CHECKSUM (die->die_tag);
5691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5692 attr_checksum (a, ctx, mark);
5694 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5697 #undef CHECKSUM
5698 #undef CHECKSUM_BLOCK
5699 #undef CHECKSUM_STRING
5701 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5702 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5703 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5704 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5705 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5706 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5707 #define CHECKSUM_ATTR(FOO) \
5708 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5710 /* Calculate the checksum of a number in signed LEB128 format. */
5712 static void
5713 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5715 unsigned char byte;
5716 bool more;
5718 while (1)
5720 byte = (value & 0x7f);
5721 value >>= 7;
5722 more = !((value == 0 && (byte & 0x40) == 0)
5723 || (value == -1 && (byte & 0x40) != 0));
5724 if (more)
5725 byte |= 0x80;
5726 CHECKSUM (byte);
5727 if (!more)
5728 break;
5732 /* Calculate the checksum of a number in unsigned LEB128 format. */
5734 static void
5735 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5737 while (1)
5739 unsigned char byte = (value & 0x7f);
5740 value >>= 7;
5741 if (value != 0)
5742 /* More bytes to follow. */
5743 byte |= 0x80;
5744 CHECKSUM (byte);
5745 if (value == 0)
5746 break;
5750 /* Checksum the context of the DIE. This adds the names of any
5751 surrounding namespaces or structures to the checksum. */
5753 static void
5754 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5756 const char *name;
5757 dw_die_ref spec;
5758 int tag = die->die_tag;
5760 if (tag != DW_TAG_namespace
5761 && tag != DW_TAG_structure_type
5762 && tag != DW_TAG_class_type)
5763 return;
5765 name = get_AT_string (die, DW_AT_name);
5767 spec = get_AT_ref (die, DW_AT_specification);
5768 if (spec != NULL)
5769 die = spec;
5771 if (die->die_parent != NULL)
5772 checksum_die_context (die->die_parent, ctx);
5774 CHECKSUM_ULEB128 ('C');
5775 CHECKSUM_ULEB128 (tag);
5776 if (name != NULL)
5777 CHECKSUM_STRING (name);
5780 /* Calculate the checksum of a location expression. */
5782 static inline void
5783 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5785 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5786 were emitted as a DW_FORM_sdata instead of a location expression. */
5787 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5789 CHECKSUM_ULEB128 (DW_FORM_sdata);
5790 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5791 return;
5794 /* Otherwise, just checksum the raw location expression. */
5795 while (loc != NULL)
5797 inchash::hash hstate;
5798 hashval_t hash;
5800 CHECKSUM_ULEB128 (loc->dtprel);
5801 CHECKSUM_ULEB128 (loc->dw_loc_opc);
5802 hash_loc_operands (loc, hstate);
5803 hash = hstate.end ();
5804 CHECKSUM (hash);
5805 loc = loc->dw_loc_next;
5809 /* Calculate the checksum of an attribute. */
5811 static void
5812 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5813 struct md5_ctx *ctx, int *mark)
5815 dw_loc_descr_ref loc;
5816 rtx r;
5818 if (AT_class (at) == dw_val_class_die_ref)
5820 dw_die_ref target_die = AT_ref (at);
5822 /* For pointer and reference types, we checksum only the (qualified)
5823 name of the target type (if there is a name). For friend entries,
5824 we checksum only the (qualified) name of the target type or function.
5825 This allows the checksum to remain the same whether the target type
5826 is complete or not. */
5827 if ((at->dw_attr == DW_AT_type
5828 && (tag == DW_TAG_pointer_type
5829 || tag == DW_TAG_reference_type
5830 || tag == DW_TAG_rvalue_reference_type
5831 || tag == DW_TAG_ptr_to_member_type))
5832 || (at->dw_attr == DW_AT_friend
5833 && tag == DW_TAG_friend))
5835 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5837 if (name_attr != NULL)
5839 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5841 if (decl == NULL)
5842 decl = target_die;
5843 CHECKSUM_ULEB128 ('N');
5844 CHECKSUM_ULEB128 (at->dw_attr);
5845 if (decl->die_parent != NULL)
5846 checksum_die_context (decl->die_parent, ctx);
5847 CHECKSUM_ULEB128 ('E');
5848 CHECKSUM_STRING (AT_string (name_attr));
5849 return;
5853 /* For all other references to another DIE, we check to see if the
5854 target DIE has already been visited. If it has, we emit a
5855 backward reference; if not, we descend recursively. */
5856 if (target_die->die_mark > 0)
5858 CHECKSUM_ULEB128 ('R');
5859 CHECKSUM_ULEB128 (at->dw_attr);
5860 CHECKSUM_ULEB128 (target_die->die_mark);
5862 else
5864 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5866 if (decl == NULL)
5867 decl = target_die;
5868 target_die->die_mark = ++(*mark);
5869 CHECKSUM_ULEB128 ('T');
5870 CHECKSUM_ULEB128 (at->dw_attr);
5871 if (decl->die_parent != NULL)
5872 checksum_die_context (decl->die_parent, ctx);
5873 die_checksum_ordered (target_die, ctx, mark);
5875 return;
5878 CHECKSUM_ULEB128 ('A');
5879 CHECKSUM_ULEB128 (at->dw_attr);
5881 switch (AT_class (at))
5883 case dw_val_class_const:
5884 CHECKSUM_ULEB128 (DW_FORM_sdata);
5885 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5886 break;
5888 case dw_val_class_unsigned_const:
5889 CHECKSUM_ULEB128 (DW_FORM_sdata);
5890 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5891 break;
5893 case dw_val_class_const_double:
5894 CHECKSUM_ULEB128 (DW_FORM_block);
5895 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5896 CHECKSUM (at->dw_attr_val.v.val_double);
5897 break;
5899 case dw_val_class_wide_int:
5900 CHECKSUM_ULEB128 (DW_FORM_block);
5901 CHECKSUM_ULEB128 (sizeof (*at->dw_attr_val.v.val_wide));
5902 CHECKSUM (*at->dw_attr_val.v.val_wide);
5903 break;
5905 case dw_val_class_vec:
5906 CHECKSUM_ULEB128 (DW_FORM_block);
5907 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
5908 * at->dw_attr_val.v.val_vec.elt_size);
5909 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5910 (at->dw_attr_val.v.val_vec.length
5911 * at->dw_attr_val.v.val_vec.elt_size));
5912 break;
5914 case dw_val_class_flag:
5915 CHECKSUM_ULEB128 (DW_FORM_flag);
5916 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5917 break;
5919 case dw_val_class_str:
5920 CHECKSUM_ULEB128 (DW_FORM_string);
5921 CHECKSUM_STRING (AT_string (at));
5922 break;
5924 case dw_val_class_addr:
5925 r = AT_addr (at);
5926 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5927 CHECKSUM_ULEB128 (DW_FORM_string);
5928 CHECKSUM_STRING (XSTR (r, 0));
5929 break;
5931 case dw_val_class_offset:
5932 CHECKSUM_ULEB128 (DW_FORM_sdata);
5933 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5934 break;
5936 case dw_val_class_loc:
5937 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5938 loc_checksum_ordered (loc, ctx);
5939 break;
5941 case dw_val_class_fde_ref:
5942 case dw_val_class_lbl_id:
5943 case dw_val_class_lineptr:
5944 case dw_val_class_macptr:
5945 case dw_val_class_high_pc:
5946 break;
5948 case dw_val_class_file:
5949 CHECKSUM_ULEB128 (DW_FORM_string);
5950 CHECKSUM_STRING (AT_file (at)->filename);
5951 break;
5953 case dw_val_class_data8:
5954 CHECKSUM (at->dw_attr_val.v.val_data8);
5955 break;
5957 default:
5958 break;
5962 struct checksum_attributes
5964 dw_attr_ref at_name;
5965 dw_attr_ref at_type;
5966 dw_attr_ref at_friend;
5967 dw_attr_ref at_accessibility;
5968 dw_attr_ref at_address_class;
5969 dw_attr_ref at_allocated;
5970 dw_attr_ref at_artificial;
5971 dw_attr_ref at_associated;
5972 dw_attr_ref at_binary_scale;
5973 dw_attr_ref at_bit_offset;
5974 dw_attr_ref at_bit_size;
5975 dw_attr_ref at_bit_stride;
5976 dw_attr_ref at_byte_size;
5977 dw_attr_ref at_byte_stride;
5978 dw_attr_ref at_const_value;
5979 dw_attr_ref at_containing_type;
5980 dw_attr_ref at_count;
5981 dw_attr_ref at_data_location;
5982 dw_attr_ref at_data_member_location;
5983 dw_attr_ref at_decimal_scale;
5984 dw_attr_ref at_decimal_sign;
5985 dw_attr_ref at_default_value;
5986 dw_attr_ref at_digit_count;
5987 dw_attr_ref at_discr;
5988 dw_attr_ref at_discr_list;
5989 dw_attr_ref at_discr_value;
5990 dw_attr_ref at_encoding;
5991 dw_attr_ref at_endianity;
5992 dw_attr_ref at_explicit;
5993 dw_attr_ref at_is_optional;
5994 dw_attr_ref at_location;
5995 dw_attr_ref at_lower_bound;
5996 dw_attr_ref at_mutable;
5997 dw_attr_ref at_ordering;
5998 dw_attr_ref at_picture_string;
5999 dw_attr_ref at_prototyped;
6000 dw_attr_ref at_small;
6001 dw_attr_ref at_segment;
6002 dw_attr_ref at_string_length;
6003 dw_attr_ref at_threads_scaled;
6004 dw_attr_ref at_upper_bound;
6005 dw_attr_ref at_use_location;
6006 dw_attr_ref at_use_UTF8;
6007 dw_attr_ref at_variable_parameter;
6008 dw_attr_ref at_virtuality;
6009 dw_attr_ref at_visibility;
6010 dw_attr_ref at_vtable_elem_location;
6013 /* Collect the attributes that we will want to use for the checksum. */
6015 static void
6016 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6018 dw_attr_ref a;
6019 unsigned ix;
6021 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6023 switch (a->dw_attr)
6025 case DW_AT_name:
6026 attrs->at_name = a;
6027 break;
6028 case DW_AT_type:
6029 attrs->at_type = a;
6030 break;
6031 case DW_AT_friend:
6032 attrs->at_friend = a;
6033 break;
6034 case DW_AT_accessibility:
6035 attrs->at_accessibility = a;
6036 break;
6037 case DW_AT_address_class:
6038 attrs->at_address_class = a;
6039 break;
6040 case DW_AT_allocated:
6041 attrs->at_allocated = a;
6042 break;
6043 case DW_AT_artificial:
6044 attrs->at_artificial = a;
6045 break;
6046 case DW_AT_associated:
6047 attrs->at_associated = a;
6048 break;
6049 case DW_AT_binary_scale:
6050 attrs->at_binary_scale = a;
6051 break;
6052 case DW_AT_bit_offset:
6053 attrs->at_bit_offset = a;
6054 break;
6055 case DW_AT_bit_size:
6056 attrs->at_bit_size = a;
6057 break;
6058 case DW_AT_bit_stride:
6059 attrs->at_bit_stride = a;
6060 break;
6061 case DW_AT_byte_size:
6062 attrs->at_byte_size = a;
6063 break;
6064 case DW_AT_byte_stride:
6065 attrs->at_byte_stride = a;
6066 break;
6067 case DW_AT_const_value:
6068 attrs->at_const_value = a;
6069 break;
6070 case DW_AT_containing_type:
6071 attrs->at_containing_type = a;
6072 break;
6073 case DW_AT_count:
6074 attrs->at_count = a;
6075 break;
6076 case DW_AT_data_location:
6077 attrs->at_data_location = a;
6078 break;
6079 case DW_AT_data_member_location:
6080 attrs->at_data_member_location = a;
6081 break;
6082 case DW_AT_decimal_scale:
6083 attrs->at_decimal_scale = a;
6084 break;
6085 case DW_AT_decimal_sign:
6086 attrs->at_decimal_sign = a;
6087 break;
6088 case DW_AT_default_value:
6089 attrs->at_default_value = a;
6090 break;
6091 case DW_AT_digit_count:
6092 attrs->at_digit_count = a;
6093 break;
6094 case DW_AT_discr:
6095 attrs->at_discr = a;
6096 break;
6097 case DW_AT_discr_list:
6098 attrs->at_discr_list = a;
6099 break;
6100 case DW_AT_discr_value:
6101 attrs->at_discr_value = a;
6102 break;
6103 case DW_AT_encoding:
6104 attrs->at_encoding = a;
6105 break;
6106 case DW_AT_endianity:
6107 attrs->at_endianity = a;
6108 break;
6109 case DW_AT_explicit:
6110 attrs->at_explicit = a;
6111 break;
6112 case DW_AT_is_optional:
6113 attrs->at_is_optional = a;
6114 break;
6115 case DW_AT_location:
6116 attrs->at_location = a;
6117 break;
6118 case DW_AT_lower_bound:
6119 attrs->at_lower_bound = a;
6120 break;
6121 case DW_AT_mutable:
6122 attrs->at_mutable = a;
6123 break;
6124 case DW_AT_ordering:
6125 attrs->at_ordering = a;
6126 break;
6127 case DW_AT_picture_string:
6128 attrs->at_picture_string = a;
6129 break;
6130 case DW_AT_prototyped:
6131 attrs->at_prototyped = a;
6132 break;
6133 case DW_AT_small:
6134 attrs->at_small = a;
6135 break;
6136 case DW_AT_segment:
6137 attrs->at_segment = a;
6138 break;
6139 case DW_AT_string_length:
6140 attrs->at_string_length = a;
6141 break;
6142 case DW_AT_threads_scaled:
6143 attrs->at_threads_scaled = a;
6144 break;
6145 case DW_AT_upper_bound:
6146 attrs->at_upper_bound = a;
6147 break;
6148 case DW_AT_use_location:
6149 attrs->at_use_location = a;
6150 break;
6151 case DW_AT_use_UTF8:
6152 attrs->at_use_UTF8 = a;
6153 break;
6154 case DW_AT_variable_parameter:
6155 attrs->at_variable_parameter = a;
6156 break;
6157 case DW_AT_virtuality:
6158 attrs->at_virtuality = a;
6159 break;
6160 case DW_AT_visibility:
6161 attrs->at_visibility = a;
6162 break;
6163 case DW_AT_vtable_elem_location:
6164 attrs->at_vtable_elem_location = a;
6165 break;
6166 default:
6167 break;
6172 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6174 static void
6175 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6177 dw_die_ref c;
6178 dw_die_ref decl;
6179 struct checksum_attributes attrs;
6181 CHECKSUM_ULEB128 ('D');
6182 CHECKSUM_ULEB128 (die->die_tag);
6184 memset (&attrs, 0, sizeof (attrs));
6186 decl = get_AT_ref (die, DW_AT_specification);
6187 if (decl != NULL)
6188 collect_checksum_attributes (&attrs, decl);
6189 collect_checksum_attributes (&attrs, die);
6191 CHECKSUM_ATTR (attrs.at_name);
6192 CHECKSUM_ATTR (attrs.at_accessibility);
6193 CHECKSUM_ATTR (attrs.at_address_class);
6194 CHECKSUM_ATTR (attrs.at_allocated);
6195 CHECKSUM_ATTR (attrs.at_artificial);
6196 CHECKSUM_ATTR (attrs.at_associated);
6197 CHECKSUM_ATTR (attrs.at_binary_scale);
6198 CHECKSUM_ATTR (attrs.at_bit_offset);
6199 CHECKSUM_ATTR (attrs.at_bit_size);
6200 CHECKSUM_ATTR (attrs.at_bit_stride);
6201 CHECKSUM_ATTR (attrs.at_byte_size);
6202 CHECKSUM_ATTR (attrs.at_byte_stride);
6203 CHECKSUM_ATTR (attrs.at_const_value);
6204 CHECKSUM_ATTR (attrs.at_containing_type);
6205 CHECKSUM_ATTR (attrs.at_count);
6206 CHECKSUM_ATTR (attrs.at_data_location);
6207 CHECKSUM_ATTR (attrs.at_data_member_location);
6208 CHECKSUM_ATTR (attrs.at_decimal_scale);
6209 CHECKSUM_ATTR (attrs.at_decimal_sign);
6210 CHECKSUM_ATTR (attrs.at_default_value);
6211 CHECKSUM_ATTR (attrs.at_digit_count);
6212 CHECKSUM_ATTR (attrs.at_discr);
6213 CHECKSUM_ATTR (attrs.at_discr_list);
6214 CHECKSUM_ATTR (attrs.at_discr_value);
6215 CHECKSUM_ATTR (attrs.at_encoding);
6216 CHECKSUM_ATTR (attrs.at_endianity);
6217 CHECKSUM_ATTR (attrs.at_explicit);
6218 CHECKSUM_ATTR (attrs.at_is_optional);
6219 CHECKSUM_ATTR (attrs.at_location);
6220 CHECKSUM_ATTR (attrs.at_lower_bound);
6221 CHECKSUM_ATTR (attrs.at_mutable);
6222 CHECKSUM_ATTR (attrs.at_ordering);
6223 CHECKSUM_ATTR (attrs.at_picture_string);
6224 CHECKSUM_ATTR (attrs.at_prototyped);
6225 CHECKSUM_ATTR (attrs.at_small);
6226 CHECKSUM_ATTR (attrs.at_segment);
6227 CHECKSUM_ATTR (attrs.at_string_length);
6228 CHECKSUM_ATTR (attrs.at_threads_scaled);
6229 CHECKSUM_ATTR (attrs.at_upper_bound);
6230 CHECKSUM_ATTR (attrs.at_use_location);
6231 CHECKSUM_ATTR (attrs.at_use_UTF8);
6232 CHECKSUM_ATTR (attrs.at_variable_parameter);
6233 CHECKSUM_ATTR (attrs.at_virtuality);
6234 CHECKSUM_ATTR (attrs.at_visibility);
6235 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6236 CHECKSUM_ATTR (attrs.at_type);
6237 CHECKSUM_ATTR (attrs.at_friend);
6239 /* Checksum the child DIEs. */
6240 c = die->die_child;
6241 if (c) do {
6242 dw_attr_ref name_attr;
6244 c = c->die_sib;
6245 name_attr = get_AT (c, DW_AT_name);
6246 if (is_template_instantiation (c))
6248 /* Ignore instantiations of member type and function templates. */
6250 else if (name_attr != NULL
6251 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6253 /* Use a shallow checksum for named nested types and member
6254 functions. */
6255 CHECKSUM_ULEB128 ('S');
6256 CHECKSUM_ULEB128 (c->die_tag);
6257 CHECKSUM_STRING (AT_string (name_attr));
6259 else
6261 /* Use a deep checksum for other children. */
6262 /* Mark this DIE so it gets processed when unmarking. */
6263 if (c->die_mark == 0)
6264 c->die_mark = -1;
6265 die_checksum_ordered (c, ctx, mark);
6267 } while (c != die->die_child);
6269 CHECKSUM_ULEB128 (0);
6272 /* Add a type name and tag to a hash. */
6273 static void
6274 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6276 CHECKSUM_ULEB128 (tag);
6277 CHECKSUM_STRING (name);
6280 #undef CHECKSUM
6281 #undef CHECKSUM_STRING
6282 #undef CHECKSUM_ATTR
6283 #undef CHECKSUM_LEB128
6284 #undef CHECKSUM_ULEB128
6286 /* Generate the type signature for DIE. This is computed by generating an
6287 MD5 checksum over the DIE's tag, its relevant attributes, and its
6288 children. Attributes that are references to other DIEs are processed
6289 by recursion, using the MARK field to prevent infinite recursion.
6290 If the DIE is nested inside a namespace or another type, we also
6291 need to include that context in the signature. The lower 64 bits
6292 of the resulting MD5 checksum comprise the signature. */
6294 static void
6295 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6297 int mark;
6298 const char *name;
6299 unsigned char checksum[16];
6300 struct md5_ctx ctx;
6301 dw_die_ref decl;
6302 dw_die_ref parent;
6304 name = get_AT_string (die, DW_AT_name);
6305 decl = get_AT_ref (die, DW_AT_specification);
6306 parent = get_die_parent (die);
6308 /* First, compute a signature for just the type name (and its surrounding
6309 context, if any. This is stored in the type unit DIE for link-time
6310 ODR (one-definition rule) checking. */
6312 if (is_cxx () && name != NULL)
6314 md5_init_ctx (&ctx);
6316 /* Checksum the names of surrounding namespaces and structures. */
6317 if (parent != NULL)
6318 checksum_die_context (parent, &ctx);
6320 /* Checksum the current DIE. */
6321 die_odr_checksum (die->die_tag, name, &ctx);
6322 md5_finish_ctx (&ctx, checksum);
6324 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6327 /* Next, compute the complete type signature. */
6329 md5_init_ctx (&ctx);
6330 mark = 1;
6331 die->die_mark = mark;
6333 /* Checksum the names of surrounding namespaces and structures. */
6334 if (parent != NULL)
6335 checksum_die_context (parent, &ctx);
6337 /* Checksum the DIE and its children. */
6338 die_checksum_ordered (die, &ctx, &mark);
6339 unmark_all_dies (die);
6340 md5_finish_ctx (&ctx, checksum);
6342 /* Store the signature in the type node and link the type DIE and the
6343 type node together. */
6344 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6345 DWARF_TYPE_SIGNATURE_SIZE);
6346 die->comdat_type_p = true;
6347 die->die_id.die_type_node = type_node;
6348 type_node->type_die = die;
6350 /* If the DIE is a specification, link its declaration to the type node
6351 as well. */
6352 if (decl != NULL)
6354 decl->comdat_type_p = true;
6355 decl->die_id.die_type_node = type_node;
6359 /* Do the location expressions look same? */
6360 static inline int
6361 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6363 return loc1->dw_loc_opc == loc2->dw_loc_opc
6364 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6365 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6368 /* Do the values look the same? */
6369 static int
6370 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6372 dw_loc_descr_ref loc1, loc2;
6373 rtx r1, r2;
6375 if (v1->val_class != v2->val_class)
6376 return 0;
6378 switch (v1->val_class)
6380 case dw_val_class_const:
6381 return v1->v.val_int == v2->v.val_int;
6382 case dw_val_class_unsigned_const:
6383 return v1->v.val_unsigned == v2->v.val_unsigned;
6384 case dw_val_class_const_double:
6385 return v1->v.val_double.high == v2->v.val_double.high
6386 && v1->v.val_double.low == v2->v.val_double.low;
6387 case dw_val_class_wide_int:
6388 return *v1->v.val_wide == *v2->v.val_wide;
6389 case dw_val_class_vec:
6390 if (v1->v.val_vec.length != v2->v.val_vec.length
6391 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6392 return 0;
6393 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6394 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6395 return 0;
6396 return 1;
6397 case dw_val_class_flag:
6398 return v1->v.val_flag == v2->v.val_flag;
6399 case dw_val_class_str:
6400 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6402 case dw_val_class_addr:
6403 r1 = v1->v.val_addr;
6404 r2 = v2->v.val_addr;
6405 if (GET_CODE (r1) != GET_CODE (r2))
6406 return 0;
6407 return !rtx_equal_p (r1, r2);
6409 case dw_val_class_offset:
6410 return v1->v.val_offset == v2->v.val_offset;
6412 case dw_val_class_loc:
6413 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6414 loc1 && loc2;
6415 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6416 if (!same_loc_p (loc1, loc2, mark))
6417 return 0;
6418 return !loc1 && !loc2;
6420 case dw_val_class_die_ref:
6421 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6423 case dw_val_class_fde_ref:
6424 case dw_val_class_vms_delta:
6425 case dw_val_class_lbl_id:
6426 case dw_val_class_lineptr:
6427 case dw_val_class_macptr:
6428 case dw_val_class_high_pc:
6429 return 1;
6431 case dw_val_class_file:
6432 return v1->v.val_file == v2->v.val_file;
6434 case dw_val_class_data8:
6435 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6437 default:
6438 return 1;
6442 /* Do the attributes look the same? */
6444 static int
6445 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6447 if (at1->dw_attr != at2->dw_attr)
6448 return 0;
6450 /* We don't care that this was compiled with a different compiler
6451 snapshot; if the output is the same, that's what matters. */
6452 if (at1->dw_attr == DW_AT_producer)
6453 return 1;
6455 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6458 /* Do the dies look the same? */
6460 static int
6461 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6463 dw_die_ref c1, c2;
6464 dw_attr_ref a1;
6465 unsigned ix;
6467 /* To avoid infinite recursion. */
6468 if (die1->die_mark)
6469 return die1->die_mark == die2->die_mark;
6470 die1->die_mark = die2->die_mark = ++(*mark);
6472 if (die1->die_tag != die2->die_tag)
6473 return 0;
6475 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6476 return 0;
6478 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6479 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6480 return 0;
6482 c1 = die1->die_child;
6483 c2 = die2->die_child;
6484 if (! c1)
6486 if (c2)
6487 return 0;
6489 else
6490 for (;;)
6492 if (!same_die_p (c1, c2, mark))
6493 return 0;
6494 c1 = c1->die_sib;
6495 c2 = c2->die_sib;
6496 if (c1 == die1->die_child)
6498 if (c2 == die2->die_child)
6499 break;
6500 else
6501 return 0;
6505 return 1;
6508 /* Do the dies look the same? Wrapper around same_die_p. */
6510 static int
6511 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6513 int mark = 0;
6514 int ret = same_die_p (die1, die2, &mark);
6516 unmark_all_dies (die1);
6517 unmark_all_dies (die2);
6519 return ret;
6522 /* The prefix to attach to symbols on DIEs in the current comdat debug
6523 info section. */
6524 static const char *comdat_symbol_id;
6526 /* The index of the current symbol within the current comdat CU. */
6527 static unsigned int comdat_symbol_number;
6529 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6530 children, and set comdat_symbol_id accordingly. */
6532 static void
6533 compute_section_prefix (dw_die_ref unit_die)
6535 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6536 const char *base = die_name ? lbasename (die_name) : "anonymous";
6537 char *name = XALLOCAVEC (char, strlen (base) + 64);
6538 char *p;
6539 int i, mark;
6540 unsigned char checksum[16];
6541 struct md5_ctx ctx;
6543 /* Compute the checksum of the DIE, then append part of it as hex digits to
6544 the name filename of the unit. */
6546 md5_init_ctx (&ctx);
6547 mark = 0;
6548 die_checksum (unit_die, &ctx, &mark);
6549 unmark_all_dies (unit_die);
6550 md5_finish_ctx (&ctx, checksum);
6552 sprintf (name, "%s.", base);
6553 clean_symbol_name (name);
6555 p = name + strlen (name);
6556 for (i = 0; i < 4; i++)
6558 sprintf (p, "%.2x", checksum[i]);
6559 p += 2;
6562 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6563 comdat_symbol_number = 0;
6566 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6568 static int
6569 is_type_die (dw_die_ref die)
6571 switch (die->die_tag)
6573 case DW_TAG_array_type:
6574 case DW_TAG_class_type:
6575 case DW_TAG_interface_type:
6576 case DW_TAG_enumeration_type:
6577 case DW_TAG_pointer_type:
6578 case DW_TAG_reference_type:
6579 case DW_TAG_rvalue_reference_type:
6580 case DW_TAG_string_type:
6581 case DW_TAG_structure_type:
6582 case DW_TAG_subroutine_type:
6583 case DW_TAG_union_type:
6584 case DW_TAG_ptr_to_member_type:
6585 case DW_TAG_set_type:
6586 case DW_TAG_subrange_type:
6587 case DW_TAG_base_type:
6588 case DW_TAG_const_type:
6589 case DW_TAG_file_type:
6590 case DW_TAG_packed_type:
6591 case DW_TAG_volatile_type:
6592 case DW_TAG_typedef:
6593 return 1;
6594 default:
6595 return 0;
6599 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6600 Basically, we want to choose the bits that are likely to be shared between
6601 compilations (types) and leave out the bits that are specific to individual
6602 compilations (functions). */
6604 static int
6605 is_comdat_die (dw_die_ref c)
6607 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6608 we do for stabs. The advantage is a greater likelihood of sharing between
6609 objects that don't include headers in the same order (and therefore would
6610 put the base types in a different comdat). jason 8/28/00 */
6612 if (c->die_tag == DW_TAG_base_type)
6613 return 0;
6615 if (c->die_tag == DW_TAG_pointer_type
6616 || c->die_tag == DW_TAG_reference_type
6617 || c->die_tag == DW_TAG_rvalue_reference_type
6618 || c->die_tag == DW_TAG_const_type
6619 || c->die_tag == DW_TAG_volatile_type)
6621 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6623 return t ? is_comdat_die (t) : 0;
6626 return is_type_die (c);
6629 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6630 compilation unit. */
6632 static int
6633 is_symbol_die (dw_die_ref c)
6635 return (is_type_die (c)
6636 || is_declaration_die (c)
6637 || c->die_tag == DW_TAG_namespace
6638 || c->die_tag == DW_TAG_module);
6641 /* Returns true iff C is a compile-unit DIE. */
6643 static inline bool
6644 is_cu_die (dw_die_ref c)
6646 return c && c->die_tag == DW_TAG_compile_unit;
6649 /* Returns true iff C is a unit DIE of some sort. */
6651 static inline bool
6652 is_unit_die (dw_die_ref c)
6654 return c && (c->die_tag == DW_TAG_compile_unit
6655 || c->die_tag == DW_TAG_partial_unit
6656 || c->die_tag == DW_TAG_type_unit);
6659 /* Returns true iff C is a namespace DIE. */
6661 static inline bool
6662 is_namespace_die (dw_die_ref c)
6664 return c && c->die_tag == DW_TAG_namespace;
6667 /* Returns true iff C is a class or structure DIE. */
6669 static inline bool
6670 is_class_die (dw_die_ref c)
6672 return c && (c->die_tag == DW_TAG_class_type
6673 || c->die_tag == DW_TAG_structure_type);
6676 /* Return non-zero if this DIE is a template parameter. */
6678 static inline bool
6679 is_template_parameter (dw_die_ref die)
6681 switch (die->die_tag)
6683 case DW_TAG_template_type_param:
6684 case DW_TAG_template_value_param:
6685 case DW_TAG_GNU_template_template_param:
6686 case DW_TAG_GNU_template_parameter_pack:
6687 return true;
6688 default:
6689 return false;
6693 /* Return non-zero if this DIE represents a template instantiation. */
6695 static inline bool
6696 is_template_instantiation (dw_die_ref die)
6698 dw_die_ref c;
6700 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6701 return false;
6702 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6703 return false;
6706 static char *
6707 gen_internal_sym (const char *prefix)
6709 char buf[256];
6711 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6712 return xstrdup (buf);
6715 /* Assign symbols to all worthy DIEs under DIE. */
6717 static void
6718 assign_symbol_names (dw_die_ref die)
6720 dw_die_ref c;
6722 if (is_symbol_die (die) && !die->comdat_type_p)
6724 if (comdat_symbol_id)
6726 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6728 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6729 comdat_symbol_id, comdat_symbol_number++);
6730 die->die_id.die_symbol = xstrdup (p);
6732 else
6733 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6736 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6739 struct cu_hash_table_entry
6741 dw_die_ref cu;
6742 unsigned min_comdat_num, max_comdat_num;
6743 struct cu_hash_table_entry *next;
6746 /* Helpers to manipulate hash table of CUs. */
6748 struct cu_hash_table_entry_hasher
6750 typedef cu_hash_table_entry value_type;
6751 typedef die_struct compare_type;
6752 static inline hashval_t hash (const value_type *);
6753 static inline bool equal (const value_type *, const compare_type *);
6754 static inline void remove (value_type *);
6757 inline hashval_t
6758 cu_hash_table_entry_hasher::hash (const value_type *entry)
6760 return htab_hash_string (entry->cu->die_id.die_symbol);
6763 inline bool
6764 cu_hash_table_entry_hasher::equal (const value_type *entry1,
6765 const compare_type *entry2)
6767 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6770 inline void
6771 cu_hash_table_entry_hasher::remove (value_type *entry)
6773 struct cu_hash_table_entry *next;
6775 while (entry)
6777 next = entry->next;
6778 free (entry);
6779 entry = next;
6783 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6785 /* Check whether we have already seen this CU and set up SYM_NUM
6786 accordingly. */
6787 static int
6788 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
6790 struct cu_hash_table_entry dummy;
6791 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6793 dummy.max_comdat_num = 0;
6795 slot = htable->find_slot_with_hash (cu,
6796 htab_hash_string (cu->die_id.die_symbol),
6797 INSERT);
6798 entry = *slot;
6800 for (; entry; last = entry, entry = entry->next)
6802 if (same_die_p_wrap (cu, entry->cu))
6803 break;
6806 if (entry)
6808 *sym_num = entry->min_comdat_num;
6809 return 1;
6812 entry = XCNEW (struct cu_hash_table_entry);
6813 entry->cu = cu;
6814 entry->min_comdat_num = *sym_num = last->max_comdat_num;
6815 entry->next = *slot;
6816 *slot = entry;
6818 return 0;
6821 /* Record SYM_NUM to record of CU in HTABLE. */
6822 static void
6823 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
6824 unsigned int sym_num)
6826 struct cu_hash_table_entry **slot, *entry;
6828 slot = htable->find_slot_with_hash (cu,
6829 htab_hash_string (cu->die_id.die_symbol),
6830 NO_INSERT);
6831 entry = *slot;
6833 entry->max_comdat_num = sym_num;
6836 /* Traverse the DIE (which is always comp_unit_die), and set up
6837 additional compilation units for each of the include files we see
6838 bracketed by BINCL/EINCL. */
6840 static void
6841 break_out_includes (dw_die_ref die)
6843 dw_die_ref c;
6844 dw_die_ref unit = NULL;
6845 limbo_die_node *node, **pnode;
6847 c = die->die_child;
6848 if (c) do {
6849 dw_die_ref prev = c;
6850 c = c->die_sib;
6851 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6852 || (unit && is_comdat_die (c)))
6854 dw_die_ref next = c->die_sib;
6856 /* This DIE is for a secondary CU; remove it from the main one. */
6857 remove_child_with_prev (c, prev);
6859 if (c->die_tag == DW_TAG_GNU_BINCL)
6860 unit = push_new_compile_unit (unit, c);
6861 else if (c->die_tag == DW_TAG_GNU_EINCL)
6862 unit = pop_compile_unit (unit);
6863 else
6864 add_child_die (unit, c);
6865 c = next;
6866 if (c == die->die_child)
6867 break;
6869 } while (c != die->die_child);
6871 #if 0
6872 /* We can only use this in debugging, since the frontend doesn't check
6873 to make sure that we leave every include file we enter. */
6874 gcc_assert (!unit);
6875 #endif
6877 assign_symbol_names (die);
6878 cu_hash_type cu_hash_table (10);
6879 for (node = limbo_die_list, pnode = &limbo_die_list;
6880 node;
6881 node = node->next)
6883 int is_dupl;
6885 compute_section_prefix (node->die);
6886 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
6887 &comdat_symbol_number);
6888 assign_symbol_names (node->die);
6889 if (is_dupl)
6890 *pnode = node->next;
6891 else
6893 pnode = &node->next;
6894 record_comdat_symbol_number (node->die, &cu_hash_table,
6895 comdat_symbol_number);
6900 /* Return non-zero if this DIE is a declaration. */
6902 static int
6903 is_declaration_die (dw_die_ref die)
6905 dw_attr_ref a;
6906 unsigned ix;
6908 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6909 if (a->dw_attr == DW_AT_declaration)
6910 return 1;
6912 return 0;
6915 /* Return non-zero if this DIE is nested inside a subprogram. */
6917 static int
6918 is_nested_in_subprogram (dw_die_ref die)
6920 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6922 if (decl == NULL)
6923 decl = die;
6924 return local_scope_p (decl);
6927 /* Return non-zero if this DIE contains a defining declaration of a
6928 subprogram. */
6930 static int
6931 contains_subprogram_definition (dw_die_ref die)
6933 dw_die_ref c;
6935 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6936 return 1;
6937 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
6938 return 0;
6941 /* Return non-zero if this is a type DIE that should be moved to a
6942 COMDAT .debug_types section. */
6944 static int
6945 should_move_die_to_comdat (dw_die_ref die)
6947 switch (die->die_tag)
6949 case DW_TAG_class_type:
6950 case DW_TAG_structure_type:
6951 case DW_TAG_enumeration_type:
6952 case DW_TAG_union_type:
6953 /* Don't move declarations, inlined instances, types nested in a
6954 subprogram, or types that contain subprogram definitions. */
6955 if (is_declaration_die (die)
6956 || get_AT (die, DW_AT_abstract_origin)
6957 || is_nested_in_subprogram (die)
6958 || contains_subprogram_definition (die))
6959 return 0;
6960 return 1;
6961 case DW_TAG_array_type:
6962 case DW_TAG_interface_type:
6963 case DW_TAG_pointer_type:
6964 case DW_TAG_reference_type:
6965 case DW_TAG_rvalue_reference_type:
6966 case DW_TAG_string_type:
6967 case DW_TAG_subroutine_type:
6968 case DW_TAG_ptr_to_member_type:
6969 case DW_TAG_set_type:
6970 case DW_TAG_subrange_type:
6971 case DW_TAG_base_type:
6972 case DW_TAG_const_type:
6973 case DW_TAG_file_type:
6974 case DW_TAG_packed_type:
6975 case DW_TAG_volatile_type:
6976 case DW_TAG_typedef:
6977 default:
6978 return 0;
6982 /* Make a clone of DIE. */
6984 static dw_die_ref
6985 clone_die (dw_die_ref die)
6987 dw_die_ref clone;
6988 dw_attr_ref a;
6989 unsigned ix;
6991 clone = ggc_cleared_alloc<die_node> ();
6992 clone->die_tag = die->die_tag;
6994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6995 add_dwarf_attr (clone, a);
6997 return clone;
7000 /* Make a clone of the tree rooted at DIE. */
7002 static dw_die_ref
7003 clone_tree (dw_die_ref die)
7005 dw_die_ref c;
7006 dw_die_ref clone = clone_die (die);
7008 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7010 return clone;
7013 /* Make a clone of DIE as a declaration. */
7015 static dw_die_ref
7016 clone_as_declaration (dw_die_ref die)
7018 dw_die_ref clone;
7019 dw_die_ref decl;
7020 dw_attr_ref a;
7021 unsigned ix;
7023 /* If the DIE is already a declaration, just clone it. */
7024 if (is_declaration_die (die))
7025 return clone_die (die);
7027 /* If the DIE is a specification, just clone its declaration DIE. */
7028 decl = get_AT_ref (die, DW_AT_specification);
7029 if (decl != NULL)
7031 clone = clone_die (decl);
7032 if (die->comdat_type_p)
7033 add_AT_die_ref (clone, DW_AT_signature, die);
7034 return clone;
7037 clone = ggc_cleared_alloc<die_node> ();
7038 clone->die_tag = die->die_tag;
7040 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7042 /* We don't want to copy over all attributes.
7043 For example we don't want DW_AT_byte_size because otherwise we will no
7044 longer have a declaration and GDB will treat it as a definition. */
7046 switch (a->dw_attr)
7048 case DW_AT_abstract_origin:
7049 case DW_AT_artificial:
7050 case DW_AT_containing_type:
7051 case DW_AT_external:
7052 case DW_AT_name:
7053 case DW_AT_type:
7054 case DW_AT_virtuality:
7055 case DW_AT_linkage_name:
7056 case DW_AT_MIPS_linkage_name:
7057 add_dwarf_attr (clone, a);
7058 break;
7059 case DW_AT_byte_size:
7060 default:
7061 break;
7065 if (die->comdat_type_p)
7066 add_AT_die_ref (clone, DW_AT_signature, die);
7068 add_AT_flag (clone, DW_AT_declaration, 1);
7069 return clone;
7073 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7075 struct decl_table_entry
7077 dw_die_ref orig;
7078 dw_die_ref copy;
7081 /* Helpers to manipulate hash table of copied declarations. */
7083 /* Hashtable helpers. */
7085 struct decl_table_entry_hasher : typed_free_remove <decl_table_entry>
7087 typedef decl_table_entry value_type;
7088 typedef die_struct compare_type;
7089 static inline hashval_t hash (const value_type *);
7090 static inline bool equal (const value_type *, const compare_type *);
7093 inline hashval_t
7094 decl_table_entry_hasher::hash (const value_type *entry)
7096 return htab_hash_pointer (entry->orig);
7099 inline bool
7100 decl_table_entry_hasher::equal (const value_type *entry1,
7101 const compare_type *entry2)
7103 return entry1->orig == entry2;
7106 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7108 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7109 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7110 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7111 to check if the ancestor has already been copied into UNIT. */
7113 static dw_die_ref
7114 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7115 decl_hash_type *decl_table)
7117 dw_die_ref parent = die->die_parent;
7118 dw_die_ref new_parent = unit;
7119 dw_die_ref copy;
7120 decl_table_entry **slot = NULL;
7121 struct decl_table_entry *entry = NULL;
7123 if (decl_table)
7125 /* Check if the entry has already been copied to UNIT. */
7126 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7127 INSERT);
7128 if (*slot != HTAB_EMPTY_ENTRY)
7130 entry = *slot;
7131 return entry->copy;
7134 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7135 entry = XCNEW (struct decl_table_entry);
7136 entry->orig = die;
7137 entry->copy = NULL;
7138 *slot = entry;
7141 if (parent != NULL)
7143 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7144 if (spec != NULL)
7145 parent = spec;
7146 if (!is_unit_die (parent))
7147 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7150 copy = clone_as_declaration (die);
7151 add_child_die (new_parent, copy);
7153 if (decl_table)
7155 /* Record the pointer to the copy. */
7156 entry->copy = copy;
7159 return copy;
7161 /* Copy the declaration context to the new type unit DIE. This includes
7162 any surrounding namespace or type declarations. If the DIE has an
7163 AT_specification attribute, it also includes attributes and children
7164 attached to the specification, and returns a pointer to the original
7165 parent of the declaration DIE. Returns NULL otherwise. */
7167 static dw_die_ref
7168 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7170 dw_die_ref decl;
7171 dw_die_ref new_decl;
7172 dw_die_ref orig_parent = NULL;
7174 decl = get_AT_ref (die, DW_AT_specification);
7175 if (decl == NULL)
7176 decl = die;
7177 else
7179 unsigned ix;
7180 dw_die_ref c;
7181 dw_attr_ref a;
7183 /* The original DIE will be changed to a declaration, and must
7184 be moved to be a child of the original declaration DIE. */
7185 orig_parent = decl->die_parent;
7187 /* Copy the type node pointer from the new DIE to the original
7188 declaration DIE so we can forward references later. */
7189 decl->comdat_type_p = true;
7190 decl->die_id.die_type_node = die->die_id.die_type_node;
7192 remove_AT (die, DW_AT_specification);
7194 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7196 if (a->dw_attr != DW_AT_name
7197 && a->dw_attr != DW_AT_declaration
7198 && a->dw_attr != DW_AT_external)
7199 add_dwarf_attr (die, a);
7202 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7205 if (decl->die_parent != NULL
7206 && !is_unit_die (decl->die_parent))
7208 new_decl = copy_ancestor_tree (unit, decl, NULL);
7209 if (new_decl != NULL)
7211 remove_AT (new_decl, DW_AT_signature);
7212 add_AT_specification (die, new_decl);
7216 return orig_parent;
7219 /* Generate the skeleton ancestor tree for the given NODE, then clone
7220 the DIE and add the clone into the tree. */
7222 static void
7223 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7225 if (node->new_die != NULL)
7226 return;
7228 node->new_die = clone_as_declaration (node->old_die);
7230 if (node->parent != NULL)
7232 generate_skeleton_ancestor_tree (node->parent);
7233 add_child_die (node->parent->new_die, node->new_die);
7237 /* Generate a skeleton tree of DIEs containing any declarations that are
7238 found in the original tree. We traverse the tree looking for declaration
7239 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7241 static void
7242 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7244 skeleton_chain_node node;
7245 dw_die_ref c;
7246 dw_die_ref first;
7247 dw_die_ref prev = NULL;
7248 dw_die_ref next = NULL;
7250 node.parent = parent;
7252 first = c = parent->old_die->die_child;
7253 if (c)
7254 next = c->die_sib;
7255 if (c) do {
7256 if (prev == NULL || prev->die_sib == c)
7257 prev = c;
7258 c = next;
7259 next = (c == first ? NULL : c->die_sib);
7260 node.old_die = c;
7261 node.new_die = NULL;
7262 if (is_declaration_die (c))
7264 if (is_template_instantiation (c))
7266 /* Instantiated templates do not need to be cloned into the
7267 type unit. Just move the DIE and its children back to
7268 the skeleton tree (in the main CU). */
7269 remove_child_with_prev (c, prev);
7270 add_child_die (parent->new_die, c);
7271 c = prev;
7273 else
7275 /* Clone the existing DIE, move the original to the skeleton
7276 tree (which is in the main CU), and put the clone, with
7277 all the original's children, where the original came from
7278 (which is about to be moved to the type unit). */
7279 dw_die_ref clone = clone_die (c);
7280 move_all_children (c, clone);
7282 /* If the original has a DW_AT_object_pointer attribute,
7283 it would now point to a child DIE just moved to the
7284 cloned tree, so we need to remove that attribute from
7285 the original. */
7286 remove_AT (c, DW_AT_object_pointer);
7288 replace_child (c, clone, prev);
7289 generate_skeleton_ancestor_tree (parent);
7290 add_child_die (parent->new_die, c);
7291 node.new_die = c;
7292 c = clone;
7295 generate_skeleton_bottom_up (&node);
7296 } while (next != NULL);
7299 /* Wrapper function for generate_skeleton_bottom_up. */
7301 static dw_die_ref
7302 generate_skeleton (dw_die_ref die)
7304 skeleton_chain_node node;
7306 node.old_die = die;
7307 node.new_die = NULL;
7308 node.parent = NULL;
7310 /* If this type definition is nested inside another type,
7311 and is not an instantiation of a template, always leave
7312 at least a declaration in its place. */
7313 if (die->die_parent != NULL
7314 && is_type_die (die->die_parent)
7315 && !is_template_instantiation (die))
7316 node.new_die = clone_as_declaration (die);
7318 generate_skeleton_bottom_up (&node);
7319 return node.new_die;
7322 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7323 declaration. The original DIE is moved to a new compile unit so that
7324 existing references to it follow it to the new location. If any of the
7325 original DIE's descendants is a declaration, we need to replace the
7326 original DIE with a skeleton tree and move the declarations back into the
7327 skeleton tree. */
7329 static dw_die_ref
7330 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7331 dw_die_ref prev)
7333 dw_die_ref skeleton, orig_parent;
7335 /* Copy the declaration context to the type unit DIE. If the returned
7336 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7337 that DIE. */
7338 orig_parent = copy_declaration_context (unit, child);
7340 skeleton = generate_skeleton (child);
7341 if (skeleton == NULL)
7342 remove_child_with_prev (child, prev);
7343 else
7345 skeleton->comdat_type_p = true;
7346 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7348 /* If the original DIE was a specification, we need to put
7349 the skeleton under the parent DIE of the declaration.
7350 This leaves the original declaration in the tree, but
7351 it will be pruned later since there are no longer any
7352 references to it. */
7353 if (orig_parent != NULL)
7355 remove_child_with_prev (child, prev);
7356 add_child_die (orig_parent, skeleton);
7358 else
7359 replace_child (child, skeleton, prev);
7362 return skeleton;
7365 /* Traverse the DIE and set up additional .debug_types sections for each
7366 type worthy of being placed in a COMDAT section. */
7368 static void
7369 break_out_comdat_types (dw_die_ref die)
7371 dw_die_ref c;
7372 dw_die_ref first;
7373 dw_die_ref prev = NULL;
7374 dw_die_ref next = NULL;
7375 dw_die_ref unit = NULL;
7377 first = c = die->die_child;
7378 if (c)
7379 next = c->die_sib;
7380 if (c) do {
7381 if (prev == NULL || prev->die_sib == c)
7382 prev = c;
7383 c = next;
7384 next = (c == first ? NULL : c->die_sib);
7385 if (should_move_die_to_comdat (c))
7387 dw_die_ref replacement;
7388 comdat_type_node_ref type_node;
7390 /* Break out nested types into their own type units. */
7391 break_out_comdat_types (c);
7393 /* Create a new type unit DIE as the root for the new tree, and
7394 add it to the list of comdat types. */
7395 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7396 add_AT_unsigned (unit, DW_AT_language,
7397 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7398 type_node = ggc_cleared_alloc<comdat_type_node> ();
7399 type_node->root_die = unit;
7400 type_node->next = comdat_type_list;
7401 comdat_type_list = type_node;
7403 /* Generate the type signature. */
7404 generate_type_signature (c, type_node);
7406 /* Copy the declaration context, attributes, and children of the
7407 declaration into the new type unit DIE, then remove this DIE
7408 from the main CU (or replace it with a skeleton if necessary). */
7409 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7410 type_node->skeleton_die = replacement;
7412 /* Add the DIE to the new compunit. */
7413 add_child_die (unit, c);
7415 if (replacement != NULL)
7416 c = replacement;
7418 else if (c->die_tag == DW_TAG_namespace
7419 || c->die_tag == DW_TAG_class_type
7420 || c->die_tag == DW_TAG_structure_type
7421 || c->die_tag == DW_TAG_union_type)
7423 /* Look for nested types that can be broken out. */
7424 break_out_comdat_types (c);
7426 } while (next != NULL);
7429 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7430 Enter all the cloned children into the hash table decl_table. */
7432 static dw_die_ref
7433 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7435 dw_die_ref c;
7436 dw_die_ref clone;
7437 struct decl_table_entry *entry;
7438 decl_table_entry **slot;
7440 if (die->die_tag == DW_TAG_subprogram)
7441 clone = clone_as_declaration (die);
7442 else
7443 clone = clone_die (die);
7445 slot = decl_table->find_slot_with_hash (die,
7446 htab_hash_pointer (die), INSERT);
7448 /* Assert that DIE isn't in the hash table yet. If it would be there
7449 before, the ancestors would be necessarily there as well, therefore
7450 clone_tree_partial wouldn't be called. */
7451 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7453 entry = XCNEW (struct decl_table_entry);
7454 entry->orig = die;
7455 entry->copy = clone;
7456 *slot = entry;
7458 if (die->die_tag != DW_TAG_subprogram)
7459 FOR_EACH_CHILD (die, c,
7460 add_child_die (clone, clone_tree_partial (c, decl_table)));
7462 return clone;
7465 /* Walk the DIE and its children, looking for references to incomplete
7466 or trivial types that are unmarked (i.e., that are not in the current
7467 type_unit). */
7469 static void
7470 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7472 dw_die_ref c;
7473 dw_attr_ref a;
7474 unsigned ix;
7476 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7478 if (AT_class (a) == dw_val_class_die_ref)
7480 dw_die_ref targ = AT_ref (a);
7481 decl_table_entry **slot;
7482 struct decl_table_entry *entry;
7484 if (targ->die_mark != 0 || targ->comdat_type_p)
7485 continue;
7487 slot = decl_table->find_slot_with_hash (targ,
7488 htab_hash_pointer (targ),
7489 INSERT);
7491 if (*slot != HTAB_EMPTY_ENTRY)
7493 /* TARG has already been copied, so we just need to
7494 modify the reference to point to the copy. */
7495 entry = *slot;
7496 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7498 else
7500 dw_die_ref parent = unit;
7501 dw_die_ref copy = clone_die (targ);
7503 /* Record in DECL_TABLE that TARG has been copied.
7504 Need to do this now, before the recursive call,
7505 because DECL_TABLE may be expanded and SLOT
7506 would no longer be a valid pointer. */
7507 entry = XCNEW (struct decl_table_entry);
7508 entry->orig = targ;
7509 entry->copy = copy;
7510 *slot = entry;
7512 /* If TARG is not a declaration DIE, we need to copy its
7513 children. */
7514 if (!is_declaration_die (targ))
7516 FOR_EACH_CHILD (
7517 targ, c,
7518 add_child_die (copy,
7519 clone_tree_partial (c, decl_table)));
7522 /* Make sure the cloned tree is marked as part of the
7523 type unit. */
7524 mark_dies (copy);
7526 /* If TARG has surrounding context, copy its ancestor tree
7527 into the new type unit. */
7528 if (targ->die_parent != NULL
7529 && !is_unit_die (targ->die_parent))
7530 parent = copy_ancestor_tree (unit, targ->die_parent,
7531 decl_table);
7533 add_child_die (parent, copy);
7534 a->dw_attr_val.v.val_die_ref.die = copy;
7536 /* Make sure the newly-copied DIE is walked. If it was
7537 installed in a previously-added context, it won't
7538 get visited otherwise. */
7539 if (parent != unit)
7541 /* Find the highest point of the newly-added tree,
7542 mark each node along the way, and walk from there. */
7543 parent->die_mark = 1;
7544 while (parent->die_parent
7545 && parent->die_parent->die_mark == 0)
7547 parent = parent->die_parent;
7548 parent->die_mark = 1;
7550 copy_decls_walk (unit, parent, decl_table);
7556 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7559 /* Copy declarations for "unworthy" types into the new comdat section.
7560 Incomplete types, modified types, and certain other types aren't broken
7561 out into comdat sections of their own, so they don't have a signature,
7562 and we need to copy the declaration into the same section so that we
7563 don't have an external reference. */
7565 static void
7566 copy_decls_for_unworthy_types (dw_die_ref unit)
7568 mark_dies (unit);
7569 decl_hash_type decl_table (10);
7570 copy_decls_walk (unit, unit, &decl_table);
7571 unmark_dies (unit);
7574 /* Traverse the DIE and add a sibling attribute if it may have the
7575 effect of speeding up access to siblings. To save some space,
7576 avoid generating sibling attributes for DIE's without children. */
7578 static void
7579 add_sibling_attributes (dw_die_ref die)
7581 dw_die_ref c;
7583 if (! die->die_child)
7584 return;
7586 if (die->die_parent && die != die->die_parent->die_child)
7587 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7589 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7592 /* Output all location lists for the DIE and its children. */
7594 static void
7595 output_location_lists (dw_die_ref die)
7597 dw_die_ref c;
7598 dw_attr_ref a;
7599 unsigned ix;
7601 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7602 if (AT_class (a) == dw_val_class_loc_list)
7603 output_loc_list (AT_loc_list (a));
7605 FOR_EACH_CHILD (die, c, output_location_lists (c));
7608 /* We want to limit the number of external references, because they are
7609 larger than local references: a relocation takes multiple words, and
7610 even a sig8 reference is always eight bytes, whereas a local reference
7611 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7612 So if we encounter multiple external references to the same type DIE, we
7613 make a local typedef stub for it and redirect all references there.
7615 This is the element of the hash table for keeping track of these
7616 references. */
7618 struct external_ref
7620 dw_die_ref type;
7621 dw_die_ref stub;
7622 unsigned n_refs;
7625 /* Hashtable helpers. */
7627 struct external_ref_hasher : typed_free_remove <external_ref>
7629 typedef external_ref value_type;
7630 typedef external_ref compare_type;
7631 static inline hashval_t hash (const value_type *);
7632 static inline bool equal (const value_type *, const compare_type *);
7635 inline hashval_t
7636 external_ref_hasher::hash (const value_type *r)
7638 dw_die_ref die = r->type;
7639 hashval_t h = 0;
7641 /* We can't use the address of the DIE for hashing, because
7642 that will make the order of the stub DIEs non-deterministic. */
7643 if (! die->comdat_type_p)
7644 /* We have a symbol; use it to compute a hash. */
7645 h = htab_hash_string (die->die_id.die_symbol);
7646 else
7648 /* We have a type signature; use a subset of the bits as the hash.
7649 The 8-byte signature is at least as large as hashval_t. */
7650 comdat_type_node_ref type_node = die->die_id.die_type_node;
7651 memcpy (&h, type_node->signature, sizeof (h));
7653 return h;
7656 inline bool
7657 external_ref_hasher::equal (const value_type *r1, const compare_type *r2)
7659 return r1->type == r2->type;
7662 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7664 /* Return a pointer to the external_ref for references to DIE. */
7666 static struct external_ref *
7667 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7669 struct external_ref ref, *ref_p;
7670 external_ref **slot;
7672 ref.type = die;
7673 slot = map->find_slot (&ref, INSERT);
7674 if (*slot != HTAB_EMPTY_ENTRY)
7675 return *slot;
7677 ref_p = XCNEW (struct external_ref);
7678 ref_p->type = die;
7679 *slot = ref_p;
7680 return ref_p;
7683 /* Subroutine of optimize_external_refs, below.
7685 If we see a type skeleton, record it as our stub. If we see external
7686 references, remember how many we've seen. */
7688 static void
7689 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7691 dw_die_ref c;
7692 dw_attr_ref a;
7693 unsigned ix;
7694 struct external_ref *ref_p;
7696 if (is_type_die (die)
7697 && (c = get_AT_ref (die, DW_AT_signature)))
7699 /* This is a local skeleton; use it for local references. */
7700 ref_p = lookup_external_ref (map, c);
7701 ref_p->stub = die;
7704 /* Scan the DIE references, and remember any that refer to DIEs from
7705 other CUs (i.e. those which are not marked). */
7706 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7707 if (AT_class (a) == dw_val_class_die_ref
7708 && (c = AT_ref (a))->die_mark == 0
7709 && is_type_die (c))
7711 ref_p = lookup_external_ref (map, c);
7712 ref_p->n_refs++;
7715 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7718 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7719 points to an external_ref, DATA is the CU we're processing. If we don't
7720 already have a local stub, and we have multiple refs, build a stub. */
7723 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7725 struct external_ref *ref_p = *slot;
7727 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7729 /* We have multiple references to this type, so build a small stub.
7730 Both of these forms are a bit dodgy from the perspective of the
7731 DWARF standard, since technically they should have names. */
7732 dw_die_ref cu = data;
7733 dw_die_ref type = ref_p->type;
7734 dw_die_ref stub = NULL;
7736 if (type->comdat_type_p)
7738 /* If we refer to this type via sig8, use AT_signature. */
7739 stub = new_die (type->die_tag, cu, NULL_TREE);
7740 add_AT_die_ref (stub, DW_AT_signature, type);
7742 else
7744 /* Otherwise, use a typedef with no name. */
7745 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7746 add_AT_die_ref (stub, DW_AT_type, type);
7749 stub->die_mark++;
7750 ref_p->stub = stub;
7752 return 1;
7755 /* DIE is a unit; look through all the DIE references to see if there are
7756 any external references to types, and if so, create local stubs for
7757 them which will be applied in build_abbrev_table. This is useful because
7758 references to local DIEs are smaller. */
7760 static external_ref_hash_type *
7761 optimize_external_refs (dw_die_ref die)
7763 external_ref_hash_type *map = new external_ref_hash_type (10);
7764 optimize_external_refs_1 (die, map);
7765 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7766 return map;
7769 /* The format of each DIE (and its attribute value pairs) is encoded in an
7770 abbreviation table. This routine builds the abbreviation table and assigns
7771 a unique abbreviation id for each abbreviation entry. The children of each
7772 die are visited recursively. */
7774 static void
7775 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7777 unsigned long abbrev_id;
7778 unsigned int n_alloc;
7779 dw_die_ref c;
7780 dw_attr_ref a;
7781 unsigned ix;
7783 /* Scan the DIE references, and replace any that refer to
7784 DIEs from other CUs (i.e. those which are not marked) with
7785 the local stubs we built in optimize_external_refs. */
7786 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7787 if (AT_class (a) == dw_val_class_die_ref
7788 && (c = AT_ref (a))->die_mark == 0)
7790 struct external_ref *ref_p;
7791 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7793 ref_p = lookup_external_ref (extern_map, c);
7794 if (ref_p->stub && ref_p->stub != die)
7795 change_AT_die_ref (a, ref_p->stub);
7796 else
7797 /* We aren't changing this reference, so mark it external. */
7798 set_AT_ref_external (a, 1);
7801 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7803 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7804 dw_attr_ref die_a, abbrev_a;
7805 unsigned ix;
7806 bool ok = true;
7808 if (abbrev->die_tag != die->die_tag)
7809 continue;
7810 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7811 continue;
7813 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7814 continue;
7816 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7818 abbrev_a = &(*abbrev->die_attr)[ix];
7819 if ((abbrev_a->dw_attr != die_a->dw_attr)
7820 || (value_format (abbrev_a) != value_format (die_a)))
7822 ok = false;
7823 break;
7826 if (ok)
7827 break;
7830 if (abbrev_id >= abbrev_die_table_in_use)
7832 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7834 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7835 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7836 n_alloc);
7838 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7839 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7840 abbrev_die_table_allocated = n_alloc;
7843 ++abbrev_die_table_in_use;
7844 abbrev_die_table[abbrev_id] = die;
7847 die->die_abbrev = abbrev_id;
7848 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7851 /* Return the power-of-two number of bytes necessary to represent VALUE. */
7853 static int
7854 constant_size (unsigned HOST_WIDE_INT value)
7856 int log;
7858 if (value == 0)
7859 log = 0;
7860 else
7861 log = floor_log2 (value);
7863 log = log / 8;
7864 log = 1 << (floor_log2 (log) + 1);
7866 return log;
7869 /* Return the size of a DIE as it is represented in the
7870 .debug_info section. */
7872 static unsigned long
7873 size_of_die (dw_die_ref die)
7875 unsigned long size = 0;
7876 dw_attr_ref a;
7877 unsigned ix;
7878 enum dwarf_form form;
7880 size += size_of_uleb128 (die->die_abbrev);
7881 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7883 switch (AT_class (a))
7885 case dw_val_class_addr:
7886 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7888 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7889 size += size_of_uleb128 (AT_index (a));
7891 else
7892 size += DWARF2_ADDR_SIZE;
7893 break;
7894 case dw_val_class_offset:
7895 size += DWARF_OFFSET_SIZE;
7896 break;
7897 case dw_val_class_loc:
7899 unsigned long lsize = size_of_locs (AT_loc (a));
7901 /* Block length. */
7902 if (dwarf_version >= 4)
7903 size += size_of_uleb128 (lsize);
7904 else
7905 size += constant_size (lsize);
7906 size += lsize;
7908 break;
7909 case dw_val_class_loc_list:
7910 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7912 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7913 size += size_of_uleb128 (AT_index (a));
7915 else
7916 size += DWARF_OFFSET_SIZE;
7917 break;
7918 case dw_val_class_range_list:
7919 size += DWARF_OFFSET_SIZE;
7920 break;
7921 case dw_val_class_const:
7922 size += size_of_sleb128 (AT_int (a));
7923 break;
7924 case dw_val_class_unsigned_const:
7926 int csize = constant_size (AT_unsigned (a));
7927 if (dwarf_version == 3
7928 && a->dw_attr == DW_AT_data_member_location
7929 && csize >= 4)
7930 size += size_of_uleb128 (AT_unsigned (a));
7931 else
7932 size += csize;
7934 break;
7935 case dw_val_class_const_double:
7936 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7937 if (HOST_BITS_PER_WIDE_INT >= 64)
7938 size++; /* block */
7939 break;
7940 case dw_val_class_wide_int:
7941 size += (get_full_len (*a->dw_attr_val.v.val_wide)
7942 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
7943 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
7944 > 64)
7945 size++; /* block */
7946 break;
7947 case dw_val_class_vec:
7948 size += constant_size (a->dw_attr_val.v.val_vec.length
7949 * a->dw_attr_val.v.val_vec.elt_size)
7950 + a->dw_attr_val.v.val_vec.length
7951 * a->dw_attr_val.v.val_vec.elt_size; /* block */
7952 break;
7953 case dw_val_class_flag:
7954 if (dwarf_version >= 4)
7955 /* Currently all add_AT_flag calls pass in 1 as last argument,
7956 so DW_FORM_flag_present can be used. If that ever changes,
7957 we'll need to use DW_FORM_flag and have some optimization
7958 in build_abbrev_table that will change those to
7959 DW_FORM_flag_present if it is set to 1 in all DIEs using
7960 the same abbrev entry. */
7961 gcc_assert (a->dw_attr_val.v.val_flag == 1);
7962 else
7963 size += 1;
7964 break;
7965 case dw_val_class_die_ref:
7966 if (AT_ref_external (a))
7968 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7969 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
7970 is sized by target address length, whereas in DWARF3
7971 it's always sized as an offset. */
7972 if (use_debug_types)
7973 size += DWARF_TYPE_SIGNATURE_SIZE;
7974 else if (dwarf_version == 2)
7975 size += DWARF2_ADDR_SIZE;
7976 else
7977 size += DWARF_OFFSET_SIZE;
7979 else
7980 size += DWARF_OFFSET_SIZE;
7981 break;
7982 case dw_val_class_fde_ref:
7983 size += DWARF_OFFSET_SIZE;
7984 break;
7985 case dw_val_class_lbl_id:
7986 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7988 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7989 size += size_of_uleb128 (AT_index (a));
7991 else
7992 size += DWARF2_ADDR_SIZE;
7993 break;
7994 case dw_val_class_lineptr:
7995 case dw_val_class_macptr:
7996 size += DWARF_OFFSET_SIZE;
7997 break;
7998 case dw_val_class_str:
7999 form = AT_string_form (a);
8000 if (form == DW_FORM_strp)
8001 size += DWARF_OFFSET_SIZE;
8002 else if (form == DW_FORM_GNU_str_index)
8003 size += size_of_uleb128 (AT_index (a));
8004 else
8005 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8006 break;
8007 case dw_val_class_file:
8008 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8009 break;
8010 case dw_val_class_data8:
8011 size += 8;
8012 break;
8013 case dw_val_class_vms_delta:
8014 size += DWARF_OFFSET_SIZE;
8015 break;
8016 case dw_val_class_high_pc:
8017 size += DWARF2_ADDR_SIZE;
8018 break;
8019 default:
8020 gcc_unreachable ();
8024 return size;
8027 /* Size the debugging information associated with a given DIE. Visits the
8028 DIE's children recursively. Updates the global variable next_die_offset, on
8029 each time through. Uses the current value of next_die_offset to update the
8030 die_offset field in each DIE. */
8032 static void
8033 calc_die_sizes (dw_die_ref die)
8035 dw_die_ref c;
8037 gcc_assert (die->die_offset == 0
8038 || (unsigned long int) die->die_offset == next_die_offset);
8039 die->die_offset = next_die_offset;
8040 next_die_offset += size_of_die (die);
8042 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8044 if (die->die_child != NULL)
8045 /* Count the null byte used to terminate sibling lists. */
8046 next_die_offset += 1;
8049 /* Size just the base type children at the start of the CU.
8050 This is needed because build_abbrev needs to size locs
8051 and sizing of type based stack ops needs to know die_offset
8052 values for the base types. */
8054 static void
8055 calc_base_type_die_sizes (void)
8057 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8058 unsigned int i;
8059 dw_die_ref base_type;
8060 #if ENABLE_ASSERT_CHECKING
8061 dw_die_ref prev = comp_unit_die ()->die_child;
8062 #endif
8064 die_offset += size_of_die (comp_unit_die ());
8065 for (i = 0; base_types.iterate (i, &base_type); i++)
8067 #if ENABLE_ASSERT_CHECKING
8068 gcc_assert (base_type->die_offset == 0
8069 && prev->die_sib == base_type
8070 && base_type->die_child == NULL
8071 && base_type->die_abbrev);
8072 prev = base_type;
8073 #endif
8074 base_type->die_offset = die_offset;
8075 die_offset += size_of_die (base_type);
8079 /* Set the marks for a die and its children. We do this so
8080 that we know whether or not a reference needs to use FORM_ref_addr; only
8081 DIEs in the same CU will be marked. We used to clear out the offset
8082 and use that as the flag, but ran into ordering problems. */
8084 static void
8085 mark_dies (dw_die_ref die)
8087 dw_die_ref c;
8089 gcc_assert (!die->die_mark);
8091 die->die_mark = 1;
8092 FOR_EACH_CHILD (die, c, mark_dies (c));
8095 /* Clear the marks for a die and its children. */
8097 static void
8098 unmark_dies (dw_die_ref die)
8100 dw_die_ref c;
8102 if (! use_debug_types)
8103 gcc_assert (die->die_mark);
8105 die->die_mark = 0;
8106 FOR_EACH_CHILD (die, c, unmark_dies (c));
8109 /* Clear the marks for a die, its children and referred dies. */
8111 static void
8112 unmark_all_dies (dw_die_ref die)
8114 dw_die_ref c;
8115 dw_attr_ref a;
8116 unsigned ix;
8118 if (!die->die_mark)
8119 return;
8120 die->die_mark = 0;
8122 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8124 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8125 if (AT_class (a) == dw_val_class_die_ref)
8126 unmark_all_dies (AT_ref (a));
8129 /* Calculate if the entry should appear in the final output file. It may be
8130 from a pruned a type. */
8132 static bool
8133 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8135 /* By limiting gnu pubnames to definitions only, gold can generate a
8136 gdb index without entries for declarations, which don't include
8137 enough information to be useful. */
8138 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8139 return false;
8141 if (table == pubname_table)
8143 /* Enumerator names are part of the pubname table, but the
8144 parent DW_TAG_enumeration_type die may have been pruned.
8145 Don't output them if that is the case. */
8146 if (p->die->die_tag == DW_TAG_enumerator &&
8147 (p->die->die_parent == NULL
8148 || !p->die->die_parent->die_perennial_p))
8149 return false;
8151 /* Everything else in the pubname table is included. */
8152 return true;
8155 /* The pubtypes table shouldn't include types that have been
8156 pruned. */
8157 return (p->die->die_offset != 0
8158 || !flag_eliminate_unused_debug_types);
8161 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8162 generated for the compilation unit. */
8164 static unsigned long
8165 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8167 unsigned long size;
8168 unsigned i;
8169 pubname_ref p;
8170 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8172 size = DWARF_PUBNAMES_HEADER_SIZE;
8173 FOR_EACH_VEC_ELT (*names, i, p)
8174 if (include_pubname_in_output (names, p))
8175 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8177 size += DWARF_OFFSET_SIZE;
8178 return size;
8181 /* Return the size of the information in the .debug_aranges section. */
8183 static unsigned long
8184 size_of_aranges (void)
8186 unsigned long size;
8188 size = DWARF_ARANGES_HEADER_SIZE;
8190 /* Count the address/length pair for this compilation unit. */
8191 if (text_section_used)
8192 size += 2 * DWARF2_ADDR_SIZE;
8193 if (cold_text_section_used)
8194 size += 2 * DWARF2_ADDR_SIZE;
8195 if (have_multiple_function_sections)
8197 unsigned fde_idx;
8198 dw_fde_ref fde;
8200 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8202 if (DECL_IGNORED_P (fde->decl))
8203 continue;
8204 if (!fde->in_std_section)
8205 size += 2 * DWARF2_ADDR_SIZE;
8206 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8207 size += 2 * DWARF2_ADDR_SIZE;
8211 /* Count the two zero words used to terminated the address range table. */
8212 size += 2 * DWARF2_ADDR_SIZE;
8213 return size;
8216 /* Select the encoding of an attribute value. */
8218 static enum dwarf_form
8219 value_format (dw_attr_ref a)
8221 switch (AT_class (a))
8223 case dw_val_class_addr:
8224 /* Only very few attributes allow DW_FORM_addr. */
8225 switch (a->dw_attr)
8227 case DW_AT_low_pc:
8228 case DW_AT_high_pc:
8229 case DW_AT_entry_pc:
8230 case DW_AT_trampoline:
8231 return (AT_index (a) == NOT_INDEXED
8232 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8233 default:
8234 break;
8236 switch (DWARF2_ADDR_SIZE)
8238 case 1:
8239 return DW_FORM_data1;
8240 case 2:
8241 return DW_FORM_data2;
8242 case 4:
8243 return DW_FORM_data4;
8244 case 8:
8245 return DW_FORM_data8;
8246 default:
8247 gcc_unreachable ();
8249 case dw_val_class_range_list:
8250 case dw_val_class_loc_list:
8251 if (dwarf_version >= 4)
8252 return DW_FORM_sec_offset;
8253 /* FALLTHRU */
8254 case dw_val_class_vms_delta:
8255 case dw_val_class_offset:
8256 switch (DWARF_OFFSET_SIZE)
8258 case 4:
8259 return DW_FORM_data4;
8260 case 8:
8261 return DW_FORM_data8;
8262 default:
8263 gcc_unreachable ();
8265 case dw_val_class_loc:
8266 if (dwarf_version >= 4)
8267 return DW_FORM_exprloc;
8268 switch (constant_size (size_of_locs (AT_loc (a))))
8270 case 1:
8271 return DW_FORM_block1;
8272 case 2:
8273 return DW_FORM_block2;
8274 case 4:
8275 return DW_FORM_block4;
8276 default:
8277 gcc_unreachable ();
8279 case dw_val_class_const:
8280 return DW_FORM_sdata;
8281 case dw_val_class_unsigned_const:
8282 switch (constant_size (AT_unsigned (a)))
8284 case 1:
8285 return DW_FORM_data1;
8286 case 2:
8287 return DW_FORM_data2;
8288 case 4:
8289 /* In DWARF3 DW_AT_data_member_location with
8290 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8291 constant, so we need to use DW_FORM_udata if we need
8292 a large constant. */
8293 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8294 return DW_FORM_udata;
8295 return DW_FORM_data4;
8296 case 8:
8297 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8298 return DW_FORM_udata;
8299 return DW_FORM_data8;
8300 default:
8301 gcc_unreachable ();
8303 case dw_val_class_const_double:
8304 switch (HOST_BITS_PER_WIDE_INT)
8306 case 8:
8307 return DW_FORM_data2;
8308 case 16:
8309 return DW_FORM_data4;
8310 case 32:
8311 return DW_FORM_data8;
8312 case 64:
8313 default:
8314 return DW_FORM_block1;
8316 case dw_val_class_wide_int:
8317 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8319 case 8:
8320 return DW_FORM_data1;
8321 case 16:
8322 return DW_FORM_data2;
8323 case 32:
8324 return DW_FORM_data4;
8325 case 64:
8326 return DW_FORM_data8;
8327 default:
8328 return DW_FORM_block1;
8330 case dw_val_class_vec:
8331 switch (constant_size (a->dw_attr_val.v.val_vec.length
8332 * a->dw_attr_val.v.val_vec.elt_size))
8334 case 1:
8335 return DW_FORM_block1;
8336 case 2:
8337 return DW_FORM_block2;
8338 case 4:
8339 return DW_FORM_block4;
8340 default:
8341 gcc_unreachable ();
8343 case dw_val_class_flag:
8344 if (dwarf_version >= 4)
8346 /* Currently all add_AT_flag calls pass in 1 as last argument,
8347 so DW_FORM_flag_present can be used. If that ever changes,
8348 we'll need to use DW_FORM_flag and have some optimization
8349 in build_abbrev_table that will change those to
8350 DW_FORM_flag_present if it is set to 1 in all DIEs using
8351 the same abbrev entry. */
8352 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8353 return DW_FORM_flag_present;
8355 return DW_FORM_flag;
8356 case dw_val_class_die_ref:
8357 if (AT_ref_external (a))
8358 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8359 else
8360 return DW_FORM_ref;
8361 case dw_val_class_fde_ref:
8362 return DW_FORM_data;
8363 case dw_val_class_lbl_id:
8364 return (AT_index (a) == NOT_INDEXED
8365 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8366 case dw_val_class_lineptr:
8367 case dw_val_class_macptr:
8368 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8369 case dw_val_class_str:
8370 return AT_string_form (a);
8371 case dw_val_class_file:
8372 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8374 case 1:
8375 return DW_FORM_data1;
8376 case 2:
8377 return DW_FORM_data2;
8378 case 4:
8379 return DW_FORM_data4;
8380 default:
8381 gcc_unreachable ();
8384 case dw_val_class_data8:
8385 return DW_FORM_data8;
8387 case dw_val_class_high_pc:
8388 switch (DWARF2_ADDR_SIZE)
8390 case 1:
8391 return DW_FORM_data1;
8392 case 2:
8393 return DW_FORM_data2;
8394 case 4:
8395 return DW_FORM_data4;
8396 case 8:
8397 return DW_FORM_data8;
8398 default:
8399 gcc_unreachable ();
8402 default:
8403 gcc_unreachable ();
8407 /* Output the encoding of an attribute value. */
8409 static void
8410 output_value_format (dw_attr_ref a)
8412 enum dwarf_form form = value_format (a);
8414 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8417 /* Given a die and id, produce the appropriate abbreviations. */
8419 static void
8420 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8422 unsigned ix;
8423 dw_attr_ref a_attr;
8425 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8426 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8427 dwarf_tag_name (abbrev->die_tag));
8429 if (abbrev->die_child != NULL)
8430 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8431 else
8432 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8434 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8436 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8437 dwarf_attr_name (a_attr->dw_attr));
8438 output_value_format (a_attr);
8441 dw2_asm_output_data (1, 0, NULL);
8442 dw2_asm_output_data (1, 0, NULL);
8446 /* Output the .debug_abbrev section which defines the DIE abbreviation
8447 table. */
8449 static void
8450 output_abbrev_section (void)
8452 unsigned long abbrev_id;
8454 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8455 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8457 /* Terminate the table. */
8458 dw2_asm_output_data (1, 0, NULL);
8461 /* Output a symbol we can use to refer to this DIE from another CU. */
8463 static inline void
8464 output_die_symbol (dw_die_ref die)
8466 const char *sym = die->die_id.die_symbol;
8468 gcc_assert (!die->comdat_type_p);
8470 if (sym == 0)
8471 return;
8473 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8474 /* We make these global, not weak; if the target doesn't support
8475 .linkonce, it doesn't support combining the sections, so debugging
8476 will break. */
8477 targetm.asm_out.globalize_label (asm_out_file, sym);
8479 ASM_OUTPUT_LABEL (asm_out_file, sym);
8482 /* Return a new location list, given the begin and end range, and the
8483 expression. */
8485 static inline dw_loc_list_ref
8486 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8487 const char *section)
8489 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8491 retlist->begin = begin;
8492 retlist->begin_entry = NULL;
8493 retlist->end = end;
8494 retlist->expr = expr;
8495 retlist->section = section;
8497 return retlist;
8500 /* Generate a new internal symbol for this location list node, if it
8501 hasn't got one yet. */
8503 static inline void
8504 gen_llsym (dw_loc_list_ref list)
8506 gcc_assert (!list->ll_symbol);
8507 list->ll_symbol = gen_internal_sym ("LLST");
8510 /* Output the location list given to us. */
8512 static void
8513 output_loc_list (dw_loc_list_ref list_head)
8515 dw_loc_list_ref curr = list_head;
8517 if (list_head->emitted)
8518 return;
8519 list_head->emitted = true;
8521 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8523 /* Walk the location list, and output each range + expression. */
8524 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8526 unsigned long size;
8527 /* Don't output an entry that starts and ends at the same address. */
8528 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8529 continue;
8530 size = size_of_locs (curr->expr);
8531 /* If the expression is too large, drop it on the floor. We could
8532 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8533 in the expression, but >= 64KB expressions for a single value
8534 in a single range are unlikely very useful. */
8535 if (size > 0xffff)
8536 continue;
8537 if (dwarf_split_debug_info)
8539 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8540 "Location list start/length entry (%s)",
8541 list_head->ll_symbol);
8542 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8543 "Location list range start index (%s)",
8544 curr->begin);
8545 /* The length field is 4 bytes. If we ever need to support
8546 an 8-byte length, we can add a new DW_LLE code or fall back
8547 to DW_LLE_GNU_start_end_entry. */
8548 dw2_asm_output_delta (4, curr->end, curr->begin,
8549 "Location list range length (%s)",
8550 list_head->ll_symbol);
8552 else if (!have_multiple_function_sections)
8554 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8555 "Location list begin address (%s)",
8556 list_head->ll_symbol);
8557 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8558 "Location list end address (%s)",
8559 list_head->ll_symbol);
8561 else
8563 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8564 "Location list begin address (%s)",
8565 list_head->ll_symbol);
8566 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8567 "Location list end address (%s)",
8568 list_head->ll_symbol);
8571 /* Output the block length for this list of location operations. */
8572 gcc_assert (size <= 0xffff);
8573 dw2_asm_output_data (2, size, "%s", "Location expression size");
8575 output_loc_sequence (curr->expr, -1);
8578 if (dwarf_split_debug_info)
8579 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8580 "Location list terminator (%s)",
8581 list_head->ll_symbol);
8582 else
8584 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8585 "Location list terminator begin (%s)",
8586 list_head->ll_symbol);
8587 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8588 "Location list terminator end (%s)",
8589 list_head->ll_symbol);
8593 /* Output a range_list offset into the debug_range section. Emit a
8594 relocated reference if val_entry is NULL, otherwise, emit an
8595 indirect reference. */
8597 static void
8598 output_range_list_offset (dw_attr_ref a)
8600 const char *name = dwarf_attr_name (a->dw_attr);
8602 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8604 char *p = strchr (ranges_section_label, '\0');
8605 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8606 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8607 debug_ranges_section, "%s", name);
8608 *p = '\0';
8610 else
8611 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8612 "%s (offset from %s)", name, ranges_section_label);
8615 /* Output the offset into the debug_loc section. */
8617 static void
8618 output_loc_list_offset (dw_attr_ref a)
8620 char *sym = AT_loc_list (a)->ll_symbol;
8622 gcc_assert (sym);
8623 if (dwarf_split_debug_info)
8624 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8625 "%s", dwarf_attr_name (a->dw_attr));
8626 else
8627 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8628 "%s", dwarf_attr_name (a->dw_attr));
8631 /* Output an attribute's index or value appropriately. */
8633 static void
8634 output_attr_index_or_value (dw_attr_ref a)
8636 const char *name = dwarf_attr_name (a->dw_attr);
8638 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8640 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8641 return;
8643 switch (AT_class (a))
8645 case dw_val_class_addr:
8646 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8647 break;
8648 case dw_val_class_high_pc:
8649 case dw_val_class_lbl_id:
8650 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8651 break;
8652 case dw_val_class_loc_list:
8653 output_loc_list_offset (a);
8654 break;
8655 default:
8656 gcc_unreachable ();
8660 /* Output a type signature. */
8662 static inline void
8663 output_signature (const char *sig, const char *name)
8665 int i;
8667 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8668 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8671 /* Output the DIE and its attributes. Called recursively to generate
8672 the definitions of each child DIE. */
8674 static void
8675 output_die (dw_die_ref die)
8677 dw_attr_ref a;
8678 dw_die_ref c;
8679 unsigned long size;
8680 unsigned ix;
8682 /* If someone in another CU might refer to us, set up a symbol for
8683 them to point to. */
8684 if (! die->comdat_type_p && die->die_id.die_symbol)
8685 output_die_symbol (die);
8687 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8688 (unsigned long)die->die_offset,
8689 dwarf_tag_name (die->die_tag));
8691 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8693 const char *name = dwarf_attr_name (a->dw_attr);
8695 switch (AT_class (a))
8697 case dw_val_class_addr:
8698 output_attr_index_or_value (a);
8699 break;
8701 case dw_val_class_offset:
8702 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8703 "%s", name);
8704 break;
8706 case dw_val_class_range_list:
8707 output_range_list_offset (a);
8708 break;
8710 case dw_val_class_loc:
8711 size = size_of_locs (AT_loc (a));
8713 /* Output the block length for this list of location operations. */
8714 if (dwarf_version >= 4)
8715 dw2_asm_output_data_uleb128 (size, "%s", name);
8716 else
8717 dw2_asm_output_data (constant_size (size), size, "%s", name);
8719 output_loc_sequence (AT_loc (a), -1);
8720 break;
8722 case dw_val_class_const:
8723 /* ??? It would be slightly more efficient to use a scheme like is
8724 used for unsigned constants below, but gdb 4.x does not sign
8725 extend. Gdb 5.x does sign extend. */
8726 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8727 break;
8729 case dw_val_class_unsigned_const:
8731 int csize = constant_size (AT_unsigned (a));
8732 if (dwarf_version == 3
8733 && a->dw_attr == DW_AT_data_member_location
8734 && csize >= 4)
8735 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8736 else
8737 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8739 break;
8741 case dw_val_class_const_double:
8743 unsigned HOST_WIDE_INT first, second;
8745 if (HOST_BITS_PER_WIDE_INT >= 64)
8746 dw2_asm_output_data (1,
8747 HOST_BITS_PER_DOUBLE_INT
8748 / HOST_BITS_PER_CHAR,
8749 NULL);
8751 if (WORDS_BIG_ENDIAN)
8753 first = a->dw_attr_val.v.val_double.high;
8754 second = a->dw_attr_val.v.val_double.low;
8756 else
8758 first = a->dw_attr_val.v.val_double.low;
8759 second = a->dw_attr_val.v.val_double.high;
8762 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8763 first, "%s", name);
8764 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8765 second, NULL);
8767 break;
8769 case dw_val_class_wide_int:
8771 int i;
8772 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8773 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8774 if (len * HOST_BITS_PER_WIDE_INT > 64)
8775 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8776 NULL);
8778 if (WORDS_BIG_ENDIAN)
8779 for (i = len - 1; i >= 0; --i)
8781 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8782 name);
8783 name = NULL;
8785 else
8786 for (i = 0; i < len; ++i)
8788 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8789 name);
8790 name = NULL;
8793 break;
8795 case dw_val_class_vec:
8797 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8798 unsigned int len = a->dw_attr_val.v.val_vec.length;
8799 unsigned int i;
8800 unsigned char *p;
8802 dw2_asm_output_data (constant_size (len * elt_size),
8803 len * elt_size, "%s", name);
8804 if (elt_size > sizeof (HOST_WIDE_INT))
8806 elt_size /= 2;
8807 len *= 2;
8809 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8810 i < len;
8811 i++, p += elt_size)
8812 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8813 "fp or vector constant word %u", i);
8814 break;
8817 case dw_val_class_flag:
8818 if (dwarf_version >= 4)
8820 /* Currently all add_AT_flag calls pass in 1 as last argument,
8821 so DW_FORM_flag_present can be used. If that ever changes,
8822 we'll need to use DW_FORM_flag and have some optimization
8823 in build_abbrev_table that will change those to
8824 DW_FORM_flag_present if it is set to 1 in all DIEs using
8825 the same abbrev entry. */
8826 gcc_assert (AT_flag (a) == 1);
8827 if (flag_debug_asm)
8828 fprintf (asm_out_file, "\t\t\t%s %s\n",
8829 ASM_COMMENT_START, name);
8830 break;
8832 dw2_asm_output_data (1, AT_flag (a), "%s", name);
8833 break;
8835 case dw_val_class_loc_list:
8836 output_attr_index_or_value (a);
8837 break;
8839 case dw_val_class_die_ref:
8840 if (AT_ref_external (a))
8842 if (AT_ref (a)->comdat_type_p)
8844 comdat_type_node_ref type_node =
8845 AT_ref (a)->die_id.die_type_node;
8847 gcc_assert (type_node);
8848 output_signature (type_node->signature, name);
8850 else
8852 const char *sym = AT_ref (a)->die_id.die_symbol;
8853 int size;
8855 gcc_assert (sym);
8856 /* In DWARF2, DW_FORM_ref_addr is sized by target address
8857 length, whereas in DWARF3 it's always sized as an
8858 offset. */
8859 if (dwarf_version == 2)
8860 size = DWARF2_ADDR_SIZE;
8861 else
8862 size = DWARF_OFFSET_SIZE;
8863 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8864 name);
8867 else
8869 gcc_assert (AT_ref (a)->die_offset);
8870 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8871 "%s", name);
8873 break;
8875 case dw_val_class_fde_ref:
8877 char l1[20];
8879 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8880 a->dw_attr_val.v.val_fde_index * 2);
8881 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8882 "%s", name);
8884 break;
8886 case dw_val_class_vms_delta:
8887 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8888 AT_vms_delta2 (a), AT_vms_delta1 (a),
8889 "%s", name);
8890 break;
8892 case dw_val_class_lbl_id:
8893 output_attr_index_or_value (a);
8894 break;
8896 case dw_val_class_lineptr:
8897 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8898 debug_line_section, "%s", name);
8899 break;
8901 case dw_val_class_macptr:
8902 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8903 debug_macinfo_section, "%s", name);
8904 break;
8906 case dw_val_class_str:
8907 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8908 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8909 a->dw_attr_val.v.val_str->label,
8910 debug_str_section,
8911 "%s: \"%s\"", name, AT_string (a));
8912 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8913 dw2_asm_output_data_uleb128 (AT_index (a),
8914 "%s: \"%s\"", name, AT_string (a));
8915 else
8916 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8917 break;
8919 case dw_val_class_file:
8921 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8923 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8924 a->dw_attr_val.v.val_file->filename);
8925 break;
8928 case dw_val_class_data8:
8930 int i;
8932 for (i = 0; i < 8; i++)
8933 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8934 i == 0 ? "%s" : NULL, name);
8935 break;
8938 case dw_val_class_high_pc:
8939 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8940 get_AT_low_pc (die), "DW_AT_high_pc");
8941 break;
8943 default:
8944 gcc_unreachable ();
8948 FOR_EACH_CHILD (die, c, output_die (c));
8950 /* Add null byte to terminate sibling list. */
8951 if (die->die_child != NULL)
8952 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8953 (unsigned long) die->die_offset);
8956 /* Output the compilation unit that appears at the beginning of the
8957 .debug_info section, and precedes the DIE descriptions. */
8959 static void
8960 output_compilation_unit_header (void)
8962 int ver = dwarf_version;
8964 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8965 dw2_asm_output_data (4, 0xffffffff,
8966 "Initial length escape value indicating 64-bit DWARF extension");
8967 dw2_asm_output_data (DWARF_OFFSET_SIZE,
8968 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8969 "Length of Compilation Unit Info");
8970 dw2_asm_output_data (2, ver, "DWARF version number");
8971 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8972 debug_abbrev_section,
8973 "Offset Into Abbrev. Section");
8974 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8977 /* Output the compilation unit DIE and its children. */
8979 static void
8980 output_comp_unit (dw_die_ref die, int output_if_empty)
8982 const char *secname, *oldsym;
8983 char *tmp;
8985 /* Unless we are outputting main CU, we may throw away empty ones. */
8986 if (!output_if_empty && die->die_child == NULL)
8987 return;
8989 /* Even if there are no children of this DIE, we must output the information
8990 about the compilation unit. Otherwise, on an empty translation unit, we
8991 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
8992 will then complain when examining the file. First mark all the DIEs in
8993 this CU so we know which get local refs. */
8994 mark_dies (die);
8996 external_ref_hash_type *extern_map = optimize_external_refs (die);
8998 build_abbrev_table (die, extern_map);
9000 delete extern_map;
9002 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9003 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9004 calc_die_sizes (die);
9006 oldsym = die->die_id.die_symbol;
9007 if (oldsym)
9009 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9011 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9012 secname = tmp;
9013 die->die_id.die_symbol = NULL;
9014 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9016 else
9018 switch_to_section (debug_info_section);
9019 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9020 info_section_emitted = true;
9023 /* Output debugging information. */
9024 output_compilation_unit_header ();
9025 output_die (die);
9027 /* Leave the marks on the main CU, so we can check them in
9028 output_pubnames. */
9029 if (oldsym)
9031 unmark_dies (die);
9032 die->die_id.die_symbol = oldsym;
9036 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9037 and .debug_pubtypes. This is configured per-target, but can be
9038 overridden by the -gpubnames or -gno-pubnames options. */
9040 static inline bool
9041 want_pubnames (void)
9043 if (debug_info_level <= DINFO_LEVEL_TERSE)
9044 return false;
9045 if (debug_generate_pub_sections != -1)
9046 return debug_generate_pub_sections;
9047 return targetm.want_debug_pub_sections;
9050 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9052 static void
9053 add_AT_pubnames (dw_die_ref die)
9055 if (want_pubnames ())
9056 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9059 /* Add a string attribute value to a skeleton DIE. */
9061 static inline void
9062 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9063 const char *str)
9065 dw_attr_node attr;
9066 struct indirect_string_node *node;
9068 if (! skeleton_debug_str_hash)
9069 skeleton_debug_str_hash
9070 = hash_table<indirect_string_hasher>::create_ggc (10);
9072 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9073 find_string_form (node);
9074 if (node->form == DW_FORM_GNU_str_index)
9075 node->form = DW_FORM_strp;
9077 attr.dw_attr = attr_kind;
9078 attr.dw_attr_val.val_class = dw_val_class_str;
9079 attr.dw_attr_val.val_entry = NULL;
9080 attr.dw_attr_val.v.val_str = node;
9081 add_dwarf_attr (die, &attr);
9084 /* Helper function to generate top-level dies for skeleton debug_info and
9085 debug_types. */
9087 static void
9088 add_top_level_skeleton_die_attrs (dw_die_ref die)
9090 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9091 const char *comp_dir = comp_dir_string ();
9093 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9094 if (comp_dir != NULL)
9095 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9096 add_AT_pubnames (die);
9097 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9100 /* Output skeleton debug sections that point to the dwo file. */
9102 static void
9103 output_skeleton_debug_sections (dw_die_ref comp_unit)
9105 /* These attributes will be found in the full debug_info section. */
9106 remove_AT (comp_unit, DW_AT_producer);
9107 remove_AT (comp_unit, DW_AT_language);
9109 switch_to_section (debug_skeleton_info_section);
9110 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9112 /* Produce the skeleton compilation-unit header. This one differs enough from
9113 a normal CU header that it's better not to call output_compilation_unit
9114 header. */
9115 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9116 dw2_asm_output_data (4, 0xffffffff,
9117 "Initial length escape value indicating 64-bit DWARF extension");
9119 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9120 DWARF_COMPILE_UNIT_HEADER_SIZE
9121 - DWARF_INITIAL_LENGTH_SIZE
9122 + size_of_die (comp_unit),
9123 "Length of Compilation Unit Info");
9124 dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9125 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9126 debug_abbrev_section,
9127 "Offset Into Abbrev. Section");
9128 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9130 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9131 output_die (comp_unit);
9133 /* Build the skeleton debug_abbrev section. */
9134 switch_to_section (debug_skeleton_abbrev_section);
9135 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9137 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9139 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9142 /* Output a comdat type unit DIE and its children. */
9144 static void
9145 output_comdat_type_unit (comdat_type_node *node)
9147 const char *secname;
9148 char *tmp;
9149 int i;
9150 #if defined (OBJECT_FORMAT_ELF)
9151 tree comdat_key;
9152 #endif
9154 /* First mark all the DIEs in this CU so we know which get local refs. */
9155 mark_dies (node->root_die);
9157 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9159 build_abbrev_table (node->root_die, extern_map);
9161 delete extern_map;
9162 extern_map = NULL;
9164 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9165 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9166 calc_die_sizes (node->root_die);
9168 #if defined (OBJECT_FORMAT_ELF)
9169 if (!dwarf_split_debug_info)
9170 secname = ".debug_types";
9171 else
9172 secname = ".debug_types.dwo";
9174 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9175 sprintf (tmp, "wt.");
9176 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9177 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9178 comdat_key = get_identifier (tmp);
9179 targetm.asm_out.named_section (secname,
9180 SECTION_DEBUG | SECTION_LINKONCE,
9181 comdat_key);
9182 #else
9183 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9184 sprintf (tmp, ".gnu.linkonce.wt.");
9185 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9186 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9187 secname = tmp;
9188 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9189 #endif
9191 /* Output debugging information. */
9192 output_compilation_unit_header ();
9193 output_signature (node->signature, "Type Signature");
9194 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9195 "Offset to Type DIE");
9196 output_die (node->root_die);
9198 unmark_dies (node->root_die);
9201 /* Return the DWARF2/3 pubname associated with a decl. */
9203 static const char *
9204 dwarf2_name (tree decl, int scope)
9206 if (DECL_NAMELESS (decl))
9207 return NULL;
9208 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9211 /* Add a new entry to .debug_pubnames if appropriate. */
9213 static void
9214 add_pubname_string (const char *str, dw_die_ref die)
9216 pubname_entry e;
9218 e.die = die;
9219 e.name = xstrdup (str);
9220 vec_safe_push (pubname_table, e);
9223 static void
9224 add_pubname (tree decl, dw_die_ref die)
9226 if (!want_pubnames ())
9227 return;
9229 /* Don't add items to the table when we expect that the consumer will have
9230 just read the enclosing die. For example, if the consumer is looking at a
9231 class_member, it will either be inside the class already, or will have just
9232 looked up the class to find the member. Either way, searching the class is
9233 faster than searching the index. */
9234 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9235 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9237 const char *name = dwarf2_name (decl, 1);
9239 if (name)
9240 add_pubname_string (name, die);
9244 /* Add an enumerator to the pubnames section. */
9246 static void
9247 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9249 pubname_entry e;
9251 gcc_assert (scope_name);
9252 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9253 e.die = die;
9254 vec_safe_push (pubname_table, e);
9257 /* Add a new entry to .debug_pubtypes if appropriate. */
9259 static void
9260 add_pubtype (tree decl, dw_die_ref die)
9262 pubname_entry e;
9264 if (!want_pubnames ())
9265 return;
9267 if ((TREE_PUBLIC (decl)
9268 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9269 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9271 tree scope = NULL;
9272 const char *scope_name = "";
9273 const char *sep = is_cxx () ? "::" : ".";
9274 const char *name;
9276 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9277 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9279 scope_name = lang_hooks.dwarf_name (scope, 1);
9280 if (scope_name != NULL && scope_name[0] != '\0')
9281 scope_name = concat (scope_name, sep, NULL);
9282 else
9283 scope_name = "";
9286 if (TYPE_P (decl))
9287 name = type_tag (decl);
9288 else
9289 name = lang_hooks.dwarf_name (decl, 1);
9291 /* If we don't have a name for the type, there's no point in adding
9292 it to the table. */
9293 if (name != NULL && name[0] != '\0')
9295 e.die = die;
9296 e.name = concat (scope_name, name, NULL);
9297 vec_safe_push (pubtype_table, e);
9300 /* Although it might be more consistent to add the pubinfo for the
9301 enumerators as their dies are created, they should only be added if the
9302 enum type meets the criteria above. So rather than re-check the parent
9303 enum type whenever an enumerator die is created, just output them all
9304 here. This isn't protected by the name conditional because anonymous
9305 enums don't have names. */
9306 if (die->die_tag == DW_TAG_enumeration_type)
9308 dw_die_ref c;
9310 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9315 /* Output a single entry in the pubnames table. */
9317 static void
9318 output_pubname (dw_offset die_offset, pubname_entry *entry)
9320 dw_die_ref die = entry->die;
9321 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9323 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9325 if (debug_generate_pub_sections == 2)
9327 /* This logic follows gdb's method for determining the value of the flag
9328 byte. */
9329 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9330 switch (die->die_tag)
9332 case DW_TAG_typedef:
9333 case DW_TAG_base_type:
9334 case DW_TAG_subrange_type:
9335 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9336 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9337 break;
9338 case DW_TAG_enumerator:
9339 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9340 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9341 if (!is_cxx () && !is_java ())
9342 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9343 break;
9344 case DW_TAG_subprogram:
9345 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9346 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9347 if (!is_ada ())
9348 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9349 break;
9350 case DW_TAG_constant:
9351 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9352 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9353 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9354 break;
9355 case DW_TAG_variable:
9356 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9357 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9358 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9359 break;
9360 case DW_TAG_namespace:
9361 case DW_TAG_imported_declaration:
9362 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9363 break;
9364 case DW_TAG_class_type:
9365 case DW_TAG_interface_type:
9366 case DW_TAG_structure_type:
9367 case DW_TAG_union_type:
9368 case DW_TAG_enumeration_type:
9369 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9370 if (!is_cxx () && !is_java ())
9371 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9372 break;
9373 default:
9374 /* An unusual tag. Leave the flag-byte empty. */
9375 break;
9377 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9378 "GDB-index flags");
9381 dw2_asm_output_nstring (entry->name, -1, "external name");
9385 /* Output the public names table used to speed up access to externally
9386 visible names; or the public types table used to find type definitions. */
9388 static void
9389 output_pubnames (vec<pubname_entry, va_gc> *names)
9391 unsigned i;
9392 unsigned long pubnames_length = size_of_pubnames (names);
9393 pubname_ref pub;
9395 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9396 dw2_asm_output_data (4, 0xffffffff,
9397 "Initial length escape value indicating 64-bit DWARF extension");
9398 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9400 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9401 dw2_asm_output_data (2, 2, "DWARF Version");
9403 if (dwarf_split_debug_info)
9404 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9405 debug_skeleton_info_section,
9406 "Offset of Compilation Unit Info");
9407 else
9408 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9409 debug_info_section,
9410 "Offset of Compilation Unit Info");
9411 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9412 "Compilation Unit Length");
9414 FOR_EACH_VEC_ELT (*names, i, pub)
9416 if (include_pubname_in_output (names, pub))
9418 dw_offset die_offset = pub->die->die_offset;
9420 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9421 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9422 gcc_assert (pub->die->die_mark);
9424 /* If we're putting types in their own .debug_types sections,
9425 the .debug_pubtypes table will still point to the compile
9426 unit (not the type unit), so we want to use the offset of
9427 the skeleton DIE (if there is one). */
9428 if (pub->die->comdat_type_p && names == pubtype_table)
9430 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9432 if (type_node != NULL)
9433 die_offset = (type_node->skeleton_die != NULL
9434 ? type_node->skeleton_die->die_offset
9435 : comp_unit_die ()->die_offset);
9438 output_pubname (die_offset, pub);
9442 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9445 /* Output public names and types tables if necessary. */
9447 static void
9448 output_pubtables (void)
9450 if (!want_pubnames () || !info_section_emitted)
9451 return;
9453 switch_to_section (debug_pubnames_section);
9454 output_pubnames (pubname_table);
9455 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9456 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9457 simply won't look for the section. */
9458 switch_to_section (debug_pubtypes_section);
9459 output_pubnames (pubtype_table);
9463 /* Output the information that goes into the .debug_aranges table.
9464 Namely, define the beginning and ending address range of the
9465 text section generated for this compilation unit. */
9467 static void
9468 output_aranges (unsigned long aranges_length)
9470 unsigned i;
9472 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9473 dw2_asm_output_data (4, 0xffffffff,
9474 "Initial length escape value indicating 64-bit DWARF extension");
9475 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9476 "Length of Address Ranges Info");
9477 /* Version number for aranges is still 2, even in DWARF3. */
9478 dw2_asm_output_data (2, 2, "DWARF Version");
9479 if (dwarf_split_debug_info)
9480 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9481 debug_skeleton_info_section,
9482 "Offset of Compilation Unit Info");
9483 else
9484 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9485 debug_info_section,
9486 "Offset of Compilation Unit Info");
9487 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9488 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9490 /* We need to align to twice the pointer size here. */
9491 if (DWARF_ARANGES_PAD_SIZE)
9493 /* Pad using a 2 byte words so that padding is correct for any
9494 pointer size. */
9495 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9496 2 * DWARF2_ADDR_SIZE);
9497 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9498 dw2_asm_output_data (2, 0, NULL);
9501 /* It is necessary not to output these entries if the sections were
9502 not used; if the sections were not used, the length will be 0 and
9503 the address may end up as 0 if the section is discarded by ld
9504 --gc-sections, leaving an invalid (0, 0) entry that can be
9505 confused with the terminator. */
9506 if (text_section_used)
9508 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9509 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9510 text_section_label, "Length");
9512 if (cold_text_section_used)
9514 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9515 "Address");
9516 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9517 cold_text_section_label, "Length");
9520 if (have_multiple_function_sections)
9522 unsigned fde_idx;
9523 dw_fde_ref fde;
9525 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9527 if (DECL_IGNORED_P (fde->decl))
9528 continue;
9529 if (!fde->in_std_section)
9531 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9532 "Address");
9533 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9534 fde->dw_fde_begin, "Length");
9536 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9538 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9539 "Address");
9540 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9541 fde->dw_fde_second_begin, "Length");
9546 /* Output the terminator words. */
9547 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9548 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9551 /* Add a new entry to .debug_ranges. Return the offset at which it
9552 was placed. */
9554 static unsigned int
9555 add_ranges_num (int num)
9557 unsigned int in_use = ranges_table_in_use;
9559 if (in_use == ranges_table_allocated)
9561 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9562 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9563 ranges_table_allocated);
9564 memset (ranges_table + ranges_table_in_use, 0,
9565 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9568 ranges_table[in_use].num = num;
9569 ranges_table_in_use = in_use + 1;
9571 return in_use * 2 * DWARF2_ADDR_SIZE;
9574 /* Add a new entry to .debug_ranges corresponding to a block, or a
9575 range terminator if BLOCK is NULL. */
9577 static unsigned int
9578 add_ranges (const_tree block)
9580 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9583 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9584 When using dwarf_split_debug_info, address attributes in dies destined
9585 for the final executable should be direct references--setting the
9586 parameter force_direct ensures this behavior. */
9588 static void
9589 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9590 bool *added, bool force_direct)
9592 unsigned int in_use = ranges_by_label_in_use;
9593 unsigned int offset;
9595 if (in_use == ranges_by_label_allocated)
9597 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9598 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9599 ranges_by_label,
9600 ranges_by_label_allocated);
9601 memset (ranges_by_label + ranges_by_label_in_use, 0,
9602 RANGES_TABLE_INCREMENT
9603 * sizeof (struct dw_ranges_by_label_struct));
9606 ranges_by_label[in_use].begin = begin;
9607 ranges_by_label[in_use].end = end;
9608 ranges_by_label_in_use = in_use + 1;
9610 offset = add_ranges_num (-(int)in_use - 1);
9611 if (!*added)
9613 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9614 *added = true;
9618 static void
9619 output_ranges (void)
9621 unsigned i;
9622 static const char *const start_fmt = "Offset %#x";
9623 const char *fmt = start_fmt;
9625 for (i = 0; i < ranges_table_in_use; i++)
9627 int block_num = ranges_table[i].num;
9629 if (block_num > 0)
9631 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9632 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9634 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9635 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9637 /* If all code is in the text section, then the compilation
9638 unit base address defaults to DW_AT_low_pc, which is the
9639 base of the text section. */
9640 if (!have_multiple_function_sections)
9642 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9643 text_section_label,
9644 fmt, i * 2 * DWARF2_ADDR_SIZE);
9645 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9646 text_section_label, NULL);
9649 /* Otherwise, the compilation unit base address is zero,
9650 which allows us to use absolute addresses, and not worry
9651 about whether the target supports cross-section
9652 arithmetic. */
9653 else
9655 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9656 fmt, i * 2 * DWARF2_ADDR_SIZE);
9657 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9660 fmt = NULL;
9663 /* Negative block_num stands for an index into ranges_by_label. */
9664 else if (block_num < 0)
9666 int lab_idx = - block_num - 1;
9668 if (!have_multiple_function_sections)
9670 gcc_unreachable ();
9671 #if 0
9672 /* If we ever use add_ranges_by_labels () for a single
9673 function section, all we have to do is to take out
9674 the #if 0 above. */
9675 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9676 ranges_by_label[lab_idx].begin,
9677 text_section_label,
9678 fmt, i * 2 * DWARF2_ADDR_SIZE);
9679 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9680 ranges_by_label[lab_idx].end,
9681 text_section_label, NULL);
9682 #endif
9684 else
9686 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9687 ranges_by_label[lab_idx].begin,
9688 fmt, i * 2 * DWARF2_ADDR_SIZE);
9689 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9690 ranges_by_label[lab_idx].end,
9691 NULL);
9694 else
9696 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9697 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9698 fmt = start_fmt;
9703 /* Data structure containing information about input files. */
9704 struct file_info
9706 const char *path; /* Complete file name. */
9707 const char *fname; /* File name part. */
9708 int length; /* Length of entire string. */
9709 struct dwarf_file_data * file_idx; /* Index in input file table. */
9710 int dir_idx; /* Index in directory table. */
9713 /* Data structure containing information about directories with source
9714 files. */
9715 struct dir_info
9717 const char *path; /* Path including directory name. */
9718 int length; /* Path length. */
9719 int prefix; /* Index of directory entry which is a prefix. */
9720 int count; /* Number of files in this directory. */
9721 int dir_idx; /* Index of directory used as base. */
9724 /* Callback function for file_info comparison. We sort by looking at
9725 the directories in the path. */
9727 static int
9728 file_info_cmp (const void *p1, const void *p2)
9730 const struct file_info *const s1 = (const struct file_info *) p1;
9731 const struct file_info *const s2 = (const struct file_info *) p2;
9732 const unsigned char *cp1;
9733 const unsigned char *cp2;
9735 /* Take care of file names without directories. We need to make sure that
9736 we return consistent values to qsort since some will get confused if
9737 we return the same value when identical operands are passed in opposite
9738 orders. So if neither has a directory, return 0 and otherwise return
9739 1 or -1 depending on which one has the directory. */
9740 if ((s1->path == s1->fname || s2->path == s2->fname))
9741 return (s2->path == s2->fname) - (s1->path == s1->fname);
9743 cp1 = (const unsigned char *) s1->path;
9744 cp2 = (const unsigned char *) s2->path;
9746 while (1)
9748 ++cp1;
9749 ++cp2;
9750 /* Reached the end of the first path? If so, handle like above. */
9751 if ((cp1 == (const unsigned char *) s1->fname)
9752 || (cp2 == (const unsigned char *) s2->fname))
9753 return ((cp2 == (const unsigned char *) s2->fname)
9754 - (cp1 == (const unsigned char *) s1->fname));
9756 /* Character of current path component the same? */
9757 else if (*cp1 != *cp2)
9758 return *cp1 - *cp2;
9762 struct file_name_acquire_data
9764 struct file_info *files;
9765 int used_files;
9766 int max_files;
9769 /* Traversal function for the hash table. */
9772 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9774 struct dwarf_file_data *d = *slot;
9775 struct file_info *fi;
9776 const char *f;
9778 gcc_assert (fnad->max_files >= d->emitted_number);
9780 if (! d->emitted_number)
9781 return 1;
9783 gcc_assert (fnad->max_files != fnad->used_files);
9785 fi = fnad->files + fnad->used_files++;
9787 /* Skip all leading "./". */
9788 f = d->filename;
9789 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9790 f += 2;
9792 /* Create a new array entry. */
9793 fi->path = f;
9794 fi->length = strlen (f);
9795 fi->file_idx = d;
9797 /* Search for the file name part. */
9798 f = strrchr (f, DIR_SEPARATOR);
9799 #if defined (DIR_SEPARATOR_2)
9801 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9803 if (g != NULL)
9805 if (f == NULL || f < g)
9806 f = g;
9809 #endif
9811 fi->fname = f == NULL ? fi->path : f + 1;
9812 return 1;
9815 /* Output the directory table and the file name table. We try to minimize
9816 the total amount of memory needed. A heuristic is used to avoid large
9817 slowdowns with many input files. */
9819 static void
9820 output_file_names (void)
9822 struct file_name_acquire_data fnad;
9823 int numfiles;
9824 struct file_info *files;
9825 struct dir_info *dirs;
9826 int *saved;
9827 int *savehere;
9828 int *backmap;
9829 int ndirs;
9830 int idx_offset;
9831 int i;
9833 if (!last_emitted_file)
9835 dw2_asm_output_data (1, 0, "End directory table");
9836 dw2_asm_output_data (1, 0, "End file name table");
9837 return;
9840 numfiles = last_emitted_file->emitted_number;
9842 /* Allocate the various arrays we need. */
9843 files = XALLOCAVEC (struct file_info, numfiles);
9844 dirs = XALLOCAVEC (struct dir_info, numfiles);
9846 fnad.files = files;
9847 fnad.used_files = 0;
9848 fnad.max_files = numfiles;
9849 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
9850 gcc_assert (fnad.used_files == fnad.max_files);
9852 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9854 /* Find all the different directories used. */
9855 dirs[0].path = files[0].path;
9856 dirs[0].length = files[0].fname - files[0].path;
9857 dirs[0].prefix = -1;
9858 dirs[0].count = 1;
9859 dirs[0].dir_idx = 0;
9860 files[0].dir_idx = 0;
9861 ndirs = 1;
9863 for (i = 1; i < numfiles; i++)
9864 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9865 && memcmp (dirs[ndirs - 1].path, files[i].path,
9866 dirs[ndirs - 1].length) == 0)
9868 /* Same directory as last entry. */
9869 files[i].dir_idx = ndirs - 1;
9870 ++dirs[ndirs - 1].count;
9872 else
9874 int j;
9876 /* This is a new directory. */
9877 dirs[ndirs].path = files[i].path;
9878 dirs[ndirs].length = files[i].fname - files[i].path;
9879 dirs[ndirs].count = 1;
9880 dirs[ndirs].dir_idx = ndirs;
9881 files[i].dir_idx = ndirs;
9883 /* Search for a prefix. */
9884 dirs[ndirs].prefix = -1;
9885 for (j = 0; j < ndirs; j++)
9886 if (dirs[j].length < dirs[ndirs].length
9887 && dirs[j].length > 1
9888 && (dirs[ndirs].prefix == -1
9889 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9890 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9891 dirs[ndirs].prefix = j;
9893 ++ndirs;
9896 /* Now to the actual work. We have to find a subset of the directories which
9897 allow expressing the file name using references to the directory table
9898 with the least amount of characters. We do not do an exhaustive search
9899 where we would have to check out every combination of every single
9900 possible prefix. Instead we use a heuristic which provides nearly optimal
9901 results in most cases and never is much off. */
9902 saved = XALLOCAVEC (int, ndirs);
9903 savehere = XALLOCAVEC (int, ndirs);
9905 memset (saved, '\0', ndirs * sizeof (saved[0]));
9906 for (i = 0; i < ndirs; i++)
9908 int j;
9909 int total;
9911 /* We can always save some space for the current directory. But this
9912 does not mean it will be enough to justify adding the directory. */
9913 savehere[i] = dirs[i].length;
9914 total = (savehere[i] - saved[i]) * dirs[i].count;
9916 for (j = i + 1; j < ndirs; j++)
9918 savehere[j] = 0;
9919 if (saved[j] < dirs[i].length)
9921 /* Determine whether the dirs[i] path is a prefix of the
9922 dirs[j] path. */
9923 int k;
9925 k = dirs[j].prefix;
9926 while (k != -1 && k != (int) i)
9927 k = dirs[k].prefix;
9929 if (k == (int) i)
9931 /* Yes it is. We can possibly save some memory by
9932 writing the filenames in dirs[j] relative to
9933 dirs[i]. */
9934 savehere[j] = dirs[i].length;
9935 total += (savehere[j] - saved[j]) * dirs[j].count;
9940 /* Check whether we can save enough to justify adding the dirs[i]
9941 directory. */
9942 if (total > dirs[i].length + 1)
9944 /* It's worthwhile adding. */
9945 for (j = i; j < ndirs; j++)
9946 if (savehere[j] > 0)
9948 /* Remember how much we saved for this directory so far. */
9949 saved[j] = savehere[j];
9951 /* Remember the prefix directory. */
9952 dirs[j].dir_idx = i;
9957 /* Emit the directory name table. */
9958 idx_offset = dirs[0].length > 0 ? 1 : 0;
9959 for (i = 1 - idx_offset; i < ndirs; i++)
9960 dw2_asm_output_nstring (dirs[i].path,
9961 dirs[i].length
9962 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9963 "Directory Entry: %#x", i + idx_offset);
9965 dw2_asm_output_data (1, 0, "End directory table");
9967 /* We have to emit them in the order of emitted_number since that's
9968 used in the debug info generation. To do this efficiently we
9969 generate a back-mapping of the indices first. */
9970 backmap = XALLOCAVEC (int, numfiles);
9971 for (i = 0; i < numfiles; i++)
9972 backmap[files[i].file_idx->emitted_number - 1] = i;
9974 /* Now write all the file names. */
9975 for (i = 0; i < numfiles; i++)
9977 int file_idx = backmap[i];
9978 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9980 #ifdef VMS_DEBUGGING_INFO
9981 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9983 /* Setting these fields can lead to debugger miscomparisons,
9984 but VMS Debug requires them to be set correctly. */
9986 int ver;
9987 long long cdt;
9988 long siz;
9989 int maxfilelen = strlen (files[file_idx].path)
9990 + dirs[dir_idx].length
9991 + MAX_VMS_VERSION_LEN + 1;
9992 char *filebuf = XALLOCAVEC (char, maxfilelen);
9994 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9995 snprintf (filebuf, maxfilelen, "%s;%d",
9996 files[file_idx].path + dirs[dir_idx].length, ver);
9998 dw2_asm_output_nstring
9999 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10001 /* Include directory index. */
10002 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10004 /* Modification time. */
10005 dw2_asm_output_data_uleb128
10006 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10007 ? cdt : 0,
10008 NULL);
10010 /* File length in bytes. */
10011 dw2_asm_output_data_uleb128
10012 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10013 ? siz : 0,
10014 NULL);
10015 #else
10016 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10017 "File Entry: %#x", (unsigned) i + 1);
10019 /* Include directory index. */
10020 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10022 /* Modification time. */
10023 dw2_asm_output_data_uleb128 (0, NULL);
10025 /* File length in bytes. */
10026 dw2_asm_output_data_uleb128 (0, NULL);
10027 #endif /* VMS_DEBUGGING_INFO */
10030 dw2_asm_output_data (1, 0, "End file name table");
10034 /* Output one line number table into the .debug_line section. */
10036 static void
10037 output_one_line_info_table (dw_line_info_table *table)
10039 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10040 unsigned int current_line = 1;
10041 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10042 dw_line_info_entry *ent;
10043 size_t i;
10045 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10047 switch (ent->opcode)
10049 case LI_set_address:
10050 /* ??? Unfortunately, we have little choice here currently, and
10051 must always use the most general form. GCC does not know the
10052 address delta itself, so we can't use DW_LNS_advance_pc. Many
10053 ports do have length attributes which will give an upper bound
10054 on the address range. We could perhaps use length attributes
10055 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10056 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10058 /* This can handle any delta. This takes
10059 4+DWARF2_ADDR_SIZE bytes. */
10060 dw2_asm_output_data (1, 0, "set address %s", line_label);
10061 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10062 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10063 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10064 break;
10066 case LI_set_line:
10067 if (ent->val == current_line)
10069 /* We still need to start a new row, so output a copy insn. */
10070 dw2_asm_output_data (1, DW_LNS_copy,
10071 "copy line %u", current_line);
10073 else
10075 int line_offset = ent->val - current_line;
10076 int line_delta = line_offset - DWARF_LINE_BASE;
10078 current_line = ent->val;
10079 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10081 /* This can handle deltas from -10 to 234, using the current
10082 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10083 This takes 1 byte. */
10084 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10085 "line %u", current_line);
10087 else
10089 /* This can handle any delta. This takes at least 4 bytes,
10090 depending on the value being encoded. */
10091 dw2_asm_output_data (1, DW_LNS_advance_line,
10092 "advance to line %u", current_line);
10093 dw2_asm_output_data_sleb128 (line_offset, NULL);
10094 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10097 break;
10099 case LI_set_file:
10100 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10101 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10102 break;
10104 case LI_set_column:
10105 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10106 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10107 break;
10109 case LI_negate_stmt:
10110 current_is_stmt = !current_is_stmt;
10111 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10112 "is_stmt %d", current_is_stmt);
10113 break;
10115 case LI_set_prologue_end:
10116 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10117 "set prologue end");
10118 break;
10120 case LI_set_epilogue_begin:
10121 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10122 "set epilogue begin");
10123 break;
10125 case LI_set_discriminator:
10126 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10127 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10128 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10129 dw2_asm_output_data_uleb128 (ent->val, NULL);
10130 break;
10134 /* Emit debug info for the address of the end of the table. */
10135 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10136 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10137 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10138 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10140 dw2_asm_output_data (1, 0, "end sequence");
10141 dw2_asm_output_data_uleb128 (1, NULL);
10142 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10145 /* Output the source line number correspondence information. This
10146 information goes into the .debug_line section. */
10148 static void
10149 output_line_info (bool prologue_only)
10151 char l1[20], l2[20], p1[20], p2[20];
10152 int ver = dwarf_version;
10153 bool saw_one = false;
10154 int opc;
10156 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10157 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10158 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10159 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10161 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10162 dw2_asm_output_data (4, 0xffffffff,
10163 "Initial length escape value indicating 64-bit DWARF extension");
10164 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10165 "Length of Source Line Info");
10166 ASM_OUTPUT_LABEL (asm_out_file, l1);
10168 dw2_asm_output_data (2, ver, "DWARF Version");
10169 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10170 ASM_OUTPUT_LABEL (asm_out_file, p1);
10172 /* Define the architecture-dependent minimum instruction length (in bytes).
10173 In this implementation of DWARF, this field is used for information
10174 purposes only. Since GCC generates assembly language, we have no
10175 a priori knowledge of how many instruction bytes are generated for each
10176 source line, and therefore can use only the DW_LNE_set_address and
10177 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10178 this as '1', which is "correct enough" for all architectures,
10179 and don't let the target override. */
10180 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10182 if (ver >= 4)
10183 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10184 "Maximum Operations Per Instruction");
10185 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10186 "Default is_stmt_start flag");
10187 dw2_asm_output_data (1, DWARF_LINE_BASE,
10188 "Line Base Value (Special Opcodes)");
10189 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10190 "Line Range Value (Special Opcodes)");
10191 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10192 "Special Opcode Base");
10194 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10196 int n_op_args;
10197 switch (opc)
10199 case DW_LNS_advance_pc:
10200 case DW_LNS_advance_line:
10201 case DW_LNS_set_file:
10202 case DW_LNS_set_column:
10203 case DW_LNS_fixed_advance_pc:
10204 case DW_LNS_set_isa:
10205 n_op_args = 1;
10206 break;
10207 default:
10208 n_op_args = 0;
10209 break;
10212 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10213 opc, n_op_args);
10216 /* Write out the information about the files we use. */
10217 output_file_names ();
10218 ASM_OUTPUT_LABEL (asm_out_file, p2);
10219 if (prologue_only)
10221 /* Output the marker for the end of the line number info. */
10222 ASM_OUTPUT_LABEL (asm_out_file, l2);
10223 return;
10226 if (separate_line_info)
10228 dw_line_info_table *table;
10229 size_t i;
10231 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10232 if (table->in_use)
10234 output_one_line_info_table (table);
10235 saw_one = true;
10238 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10240 output_one_line_info_table (cold_text_section_line_info);
10241 saw_one = true;
10244 /* ??? Some Darwin linkers crash on a .debug_line section with no
10245 sequences. Further, merely a DW_LNE_end_sequence entry is not
10246 sufficient -- the address column must also be initialized.
10247 Make sure to output at least one set_address/end_sequence pair,
10248 choosing .text since that section is always present. */
10249 if (text_section_line_info->in_use || !saw_one)
10250 output_one_line_info_table (text_section_line_info);
10252 /* Output the marker for the end of the line number info. */
10253 ASM_OUTPUT_LABEL (asm_out_file, l2);
10256 /* Given a pointer to a tree node for some base type, return a pointer to
10257 a DIE that describes the given type.
10259 This routine must only be called for GCC type nodes that correspond to
10260 Dwarf base (fundamental) types. */
10262 static dw_die_ref
10263 base_type_die (tree type)
10265 dw_die_ref base_type_result;
10266 enum dwarf_type encoding;
10268 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10269 return 0;
10271 /* If this is a subtype that should not be emitted as a subrange type,
10272 use the base type. See subrange_type_for_debug_p. */
10273 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10274 type = TREE_TYPE (type);
10276 switch (TREE_CODE (type))
10278 case INTEGER_TYPE:
10279 if ((dwarf_version >= 4 || !dwarf_strict)
10280 && TYPE_NAME (type)
10281 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10282 && DECL_IS_BUILTIN (TYPE_NAME (type))
10283 && DECL_NAME (TYPE_NAME (type)))
10285 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10286 if (strcmp (name, "char16_t") == 0
10287 || strcmp (name, "char32_t") == 0)
10289 encoding = DW_ATE_UTF;
10290 break;
10293 if (TYPE_STRING_FLAG (type))
10295 if (TYPE_UNSIGNED (type))
10296 encoding = DW_ATE_unsigned_char;
10297 else
10298 encoding = DW_ATE_signed_char;
10300 else if (TYPE_UNSIGNED (type))
10301 encoding = DW_ATE_unsigned;
10302 else
10303 encoding = DW_ATE_signed;
10304 break;
10306 case REAL_TYPE:
10307 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10309 if (dwarf_version >= 3 || !dwarf_strict)
10310 encoding = DW_ATE_decimal_float;
10311 else
10312 encoding = DW_ATE_lo_user;
10314 else
10315 encoding = DW_ATE_float;
10316 break;
10318 case FIXED_POINT_TYPE:
10319 if (!(dwarf_version >= 3 || !dwarf_strict))
10320 encoding = DW_ATE_lo_user;
10321 else if (TYPE_UNSIGNED (type))
10322 encoding = DW_ATE_unsigned_fixed;
10323 else
10324 encoding = DW_ATE_signed_fixed;
10325 break;
10327 /* Dwarf2 doesn't know anything about complex ints, so use
10328 a user defined type for it. */
10329 case COMPLEX_TYPE:
10330 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10331 encoding = DW_ATE_complex_float;
10332 else
10333 encoding = DW_ATE_lo_user;
10334 break;
10336 case BOOLEAN_TYPE:
10337 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10338 encoding = DW_ATE_boolean;
10339 break;
10341 default:
10342 /* No other TREE_CODEs are Dwarf fundamental types. */
10343 gcc_unreachable ();
10346 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10348 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10349 int_size_in_bytes (type));
10350 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10351 add_pubtype (type, base_type_result);
10353 return base_type_result;
10356 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10357 named 'auto' in its type: return true for it, false otherwise. */
10359 static inline bool
10360 is_cxx_auto (tree type)
10362 if (is_cxx ())
10364 tree name = TYPE_IDENTIFIER (type);
10365 if (name == get_identifier ("auto")
10366 || name == get_identifier ("decltype(auto)"))
10367 return true;
10369 return false;
10372 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10373 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10375 static inline int
10376 is_base_type (tree type)
10378 switch (TREE_CODE (type))
10380 case ERROR_MARK:
10381 case VOID_TYPE:
10382 case INTEGER_TYPE:
10383 case REAL_TYPE:
10384 case FIXED_POINT_TYPE:
10385 case COMPLEX_TYPE:
10386 case BOOLEAN_TYPE:
10387 return 1;
10389 case ARRAY_TYPE:
10390 case RECORD_TYPE:
10391 case UNION_TYPE:
10392 case QUAL_UNION_TYPE:
10393 case ENUMERAL_TYPE:
10394 case FUNCTION_TYPE:
10395 case METHOD_TYPE:
10396 case POINTER_TYPE:
10397 case REFERENCE_TYPE:
10398 case NULLPTR_TYPE:
10399 case OFFSET_TYPE:
10400 case LANG_TYPE:
10401 case VECTOR_TYPE:
10402 return 0;
10404 default:
10405 if (is_cxx_auto (type))
10406 return 0;
10407 gcc_unreachable ();
10410 return 0;
10413 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10414 node, return the size in bits for the type if it is a constant, or else
10415 return the alignment for the type if the type's size is not constant, or
10416 else return BITS_PER_WORD if the type actually turns out to be an
10417 ERROR_MARK node. */
10419 static inline unsigned HOST_WIDE_INT
10420 simple_type_size_in_bits (const_tree type)
10422 if (TREE_CODE (type) == ERROR_MARK)
10423 return BITS_PER_WORD;
10424 else if (TYPE_SIZE (type) == NULL_TREE)
10425 return 0;
10426 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10427 return tree_to_uhwi (TYPE_SIZE (type));
10428 else
10429 return TYPE_ALIGN (type);
10432 /* Similarly, but return an offset_int instead of UHWI. */
10434 static inline offset_int
10435 offset_int_type_size_in_bits (const_tree type)
10437 if (TREE_CODE (type) == ERROR_MARK)
10438 return BITS_PER_WORD;
10439 else if (TYPE_SIZE (type) == NULL_TREE)
10440 return 0;
10441 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10442 return wi::to_offset (TYPE_SIZE (type));
10443 else
10444 return TYPE_ALIGN (type);
10447 /* Given a pointer to a tree node for a subrange type, return a pointer
10448 to a DIE that describes the given type. */
10450 static dw_die_ref
10451 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10453 dw_die_ref subrange_die;
10454 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10456 if (context_die == NULL)
10457 context_die = comp_unit_die ();
10459 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10461 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10463 /* The size of the subrange type and its base type do not match,
10464 so we need to generate a size attribute for the subrange type. */
10465 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10468 if (low)
10469 add_bound_info (subrange_die, DW_AT_lower_bound, low);
10470 if (high)
10471 add_bound_info (subrange_die, DW_AT_upper_bound, high);
10473 return subrange_die;
10476 /* Returns the (const and/or volatile) cv_qualifiers associated with
10477 the decl node. This will normally be augmented with the
10478 cv_qualifiers of the underlying type in add_type_attribute. */
10480 static int
10481 decl_quals (const_tree decl)
10483 return ((TREE_READONLY (decl)
10484 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10485 | (TREE_THIS_VOLATILE (decl)
10486 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10489 /* Determine the TYPE whose qualifiers match the largest strict subset
10490 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10491 qualifiers outside QUAL_MASK. */
10493 static int
10494 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10496 tree t;
10497 int best_rank = 0, best_qual = 0, max_rank;
10499 type_quals &= qual_mask;
10500 max_rank = popcount_hwi (type_quals) - 1;
10502 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10503 t = TYPE_NEXT_VARIANT (t))
10505 int q = TYPE_QUALS (t) & qual_mask;
10507 if ((q & type_quals) == q && q != type_quals
10508 && check_base_type (t, type))
10510 int rank = popcount_hwi (q);
10512 if (rank > best_rank)
10514 best_rank = rank;
10515 best_qual = q;
10520 return best_qual;
10523 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10524 entry that chains various modifiers in front of the given type. */
10526 static dw_die_ref
10527 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10529 enum tree_code code = TREE_CODE (type);
10530 dw_die_ref mod_type_die;
10531 dw_die_ref sub_die = NULL;
10532 tree item_type = NULL;
10533 tree qualified_type;
10534 tree name, low, high;
10535 dw_die_ref mod_scope;
10536 /* Only these cv-qualifiers are currently handled. */
10537 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10538 | TYPE_QUAL_RESTRICT);
10540 if (code == ERROR_MARK)
10541 return NULL;
10543 cv_quals &= cv_qual_mask;
10545 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10546 tag modifier (and not an attribute) old consumers won't be able
10547 to handle it. */
10548 if (dwarf_version < 3)
10549 cv_quals &= ~TYPE_QUAL_RESTRICT;
10551 /* See if we already have the appropriately qualified variant of
10552 this type. */
10553 qualified_type = get_qualified_type (type, cv_quals);
10555 if (qualified_type == sizetype
10556 && TYPE_NAME (qualified_type)
10557 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10559 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10561 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10562 && TYPE_PRECISION (t)
10563 == TYPE_PRECISION (qualified_type)
10564 && TYPE_UNSIGNED (t)
10565 == TYPE_UNSIGNED (qualified_type));
10566 qualified_type = t;
10569 /* If we do, then we can just use its DIE, if it exists. */
10570 if (qualified_type)
10572 mod_type_die = lookup_type_die (qualified_type);
10573 if (mod_type_die)
10574 return mod_type_die;
10577 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10579 /* Handle C typedef types. */
10580 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10581 && !DECL_ARTIFICIAL (name))
10583 tree dtype = TREE_TYPE (name);
10585 if (qualified_type == dtype)
10587 /* For a named type, use the typedef. */
10588 gen_type_die (qualified_type, context_die);
10589 return lookup_type_die (qualified_type);
10591 else
10593 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10594 dquals &= cv_qual_mask;
10595 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10596 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10597 /* cv-unqualified version of named type. Just use
10598 the unnamed type to which it refers. */
10599 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10600 cv_quals, context_die);
10601 /* Else cv-qualified version of named type; fall through. */
10605 mod_scope = scope_die_for (type, context_die);
10607 if (cv_quals)
10609 struct qual_info { int q; enum dwarf_tag t; };
10610 static const struct qual_info qual_info[] =
10612 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10613 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10614 { TYPE_QUAL_CONST, DW_TAG_const_type },
10616 int sub_quals;
10617 unsigned i;
10619 /* Determine a lesser qualified type that most closely matches
10620 this one. Then generate DW_TAG_* entries for the remaining
10621 qualifiers. */
10622 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10623 cv_qual_mask);
10624 mod_type_die = modified_type_die (type, sub_quals, context_die);
10626 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10627 if (qual_info[i].q & cv_quals & ~sub_quals)
10629 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10630 if (mod_type_die)
10631 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10632 mod_type_die = d;
10635 else if (code == POINTER_TYPE)
10637 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10638 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10639 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10640 item_type = TREE_TYPE (type);
10641 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10642 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10643 TYPE_ADDR_SPACE (item_type));
10645 else if (code == REFERENCE_TYPE)
10647 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10648 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10649 type);
10650 else
10651 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10652 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10653 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10654 item_type = TREE_TYPE (type);
10655 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10656 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10657 TYPE_ADDR_SPACE (item_type));
10659 else if (code == INTEGER_TYPE
10660 && TREE_TYPE (type) != NULL_TREE
10661 && subrange_type_for_debug_p (type, &low, &high))
10663 mod_type_die = subrange_type_die (type, low, high, context_die);
10664 item_type = TREE_TYPE (type);
10666 else if (is_base_type (type))
10667 mod_type_die = base_type_die (type);
10668 else
10670 gen_type_die (type, context_die);
10672 /* We have to get the type_main_variant here (and pass that to the
10673 `lookup_type_die' routine) because the ..._TYPE node we have
10674 might simply be a *copy* of some original type node (where the
10675 copy was created to help us keep track of typedef names) and
10676 that copy might have a different TYPE_UID from the original
10677 ..._TYPE node. */
10678 if (TREE_CODE (type) != VECTOR_TYPE)
10679 return lookup_type_die (type_main_variant (type));
10680 else
10681 /* Vectors have the debugging information in the type,
10682 not the main variant. */
10683 return lookup_type_die (type);
10686 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10687 don't output a DW_TAG_typedef, since there isn't one in the
10688 user's program; just attach a DW_AT_name to the type.
10689 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10690 if the base type already has the same name. */
10691 if (name
10692 && ((TREE_CODE (name) != TYPE_DECL
10693 && (qualified_type == TYPE_MAIN_VARIANT (type)
10694 || (cv_quals == TYPE_UNQUALIFIED)))
10695 || (TREE_CODE (name) == TYPE_DECL
10696 && TREE_TYPE (name) == qualified_type
10697 && DECL_NAME (name))))
10699 if (TREE_CODE (name) == TYPE_DECL)
10700 /* Could just call add_name_and_src_coords_attributes here,
10701 but since this is a builtin type it doesn't have any
10702 useful source coordinates anyway. */
10703 name = DECL_NAME (name);
10704 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10706 /* This probably indicates a bug. */
10707 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10709 name = TYPE_IDENTIFIER (type);
10710 add_name_attribute (mod_type_die,
10711 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10714 if (qualified_type)
10715 equate_type_number_to_die (qualified_type, mod_type_die);
10717 if (item_type)
10718 /* We must do this after the equate_type_number_to_die call, in case
10719 this is a recursive type. This ensures that the modified_type_die
10720 recursion will terminate even if the type is recursive. Recursive
10721 types are possible in Ada. */
10722 sub_die = modified_type_die (item_type,
10723 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10724 context_die);
10726 if (sub_die != NULL)
10727 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10729 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10730 if (TYPE_ARTIFICIAL (type))
10731 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10733 return mod_type_die;
10736 /* Generate DIEs for the generic parameters of T.
10737 T must be either a generic type or a generic function.
10738 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10740 static void
10741 gen_generic_params_dies (tree t)
10743 tree parms, args;
10744 int parms_num, i;
10745 dw_die_ref die = NULL;
10746 int non_default;
10748 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10749 return;
10751 if (TYPE_P (t))
10752 die = lookup_type_die (t);
10753 else if (DECL_P (t))
10754 die = lookup_decl_die (t);
10756 gcc_assert (die);
10758 parms = lang_hooks.get_innermost_generic_parms (t);
10759 if (!parms)
10760 /* T has no generic parameter. It means T is neither a generic type
10761 or function. End of story. */
10762 return;
10764 parms_num = TREE_VEC_LENGTH (parms);
10765 args = lang_hooks.get_innermost_generic_args (t);
10766 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10767 non_default = int_cst_value (TREE_CHAIN (args));
10768 else
10769 non_default = TREE_VEC_LENGTH (args);
10770 for (i = 0; i < parms_num; i++)
10772 tree parm, arg, arg_pack_elems;
10773 dw_die_ref parm_die;
10775 parm = TREE_VEC_ELT (parms, i);
10776 arg = TREE_VEC_ELT (args, i);
10777 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10778 gcc_assert (parm && TREE_VALUE (parm) && arg);
10780 if (parm && TREE_VALUE (parm) && arg)
10782 /* If PARM represents a template parameter pack,
10783 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10784 by DW_TAG_template_*_parameter DIEs for the argument
10785 pack elements of ARG. Note that ARG would then be
10786 an argument pack. */
10787 if (arg_pack_elems)
10788 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
10789 arg_pack_elems,
10790 die);
10791 else
10792 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
10793 true /* emit name */, die);
10794 if (i >= non_default)
10795 add_AT_flag (parm_die, DW_AT_default_value, 1);
10800 /* Create and return a DIE for PARM which should be
10801 the representation of a generic type parameter.
10802 For instance, in the C++ front end, PARM would be a template parameter.
10803 ARG is the argument to PARM.
10804 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10805 name of the PARM.
10806 PARENT_DIE is the parent DIE which the new created DIE should be added to,
10807 as a child node. */
10809 static dw_die_ref
10810 generic_parameter_die (tree parm, tree arg,
10811 bool emit_name_p,
10812 dw_die_ref parent_die)
10814 dw_die_ref tmpl_die = NULL;
10815 const char *name = NULL;
10817 if (!parm || !DECL_NAME (parm) || !arg)
10818 return NULL;
10820 /* We support non-type generic parameters and arguments,
10821 type generic parameters and arguments, as well as
10822 generic generic parameters (a.k.a. template template parameters in C++)
10823 and arguments. */
10824 if (TREE_CODE (parm) == PARM_DECL)
10825 /* PARM is a nontype generic parameter */
10826 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10827 else if (TREE_CODE (parm) == TYPE_DECL)
10828 /* PARM is a type generic parameter. */
10829 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10830 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10831 /* PARM is a generic generic parameter.
10832 Its DIE is a GNU extension. It shall have a
10833 DW_AT_name attribute to represent the name of the template template
10834 parameter, and a DW_AT_GNU_template_name attribute to represent the
10835 name of the template template argument. */
10836 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10837 parent_die, parm);
10838 else
10839 gcc_unreachable ();
10841 if (tmpl_die)
10843 tree tmpl_type;
10845 /* If PARM is a generic parameter pack, it means we are
10846 emitting debug info for a template argument pack element.
10847 In other terms, ARG is a template argument pack element.
10848 In that case, we don't emit any DW_AT_name attribute for
10849 the die. */
10850 if (emit_name_p)
10852 name = IDENTIFIER_POINTER (DECL_NAME (parm));
10853 gcc_assert (name);
10854 add_AT_string (tmpl_die, DW_AT_name, name);
10857 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10859 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10860 TMPL_DIE should have a child DW_AT_type attribute that is set
10861 to the type of the argument to PARM, which is ARG.
10862 If PARM is a type generic parameter, TMPL_DIE should have a
10863 child DW_AT_type that is set to ARG. */
10864 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10865 add_type_attribute (tmpl_die, tmpl_type,
10866 (TREE_THIS_VOLATILE (tmpl_type)
10867 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
10868 parent_die);
10870 else
10872 /* So TMPL_DIE is a DIE representing a
10873 a generic generic template parameter, a.k.a template template
10874 parameter in C++ and arg is a template. */
10876 /* The DW_AT_GNU_template_name attribute of the DIE must be set
10877 to the name of the argument. */
10878 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10879 if (name)
10880 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10883 if (TREE_CODE (parm) == PARM_DECL)
10884 /* So PARM is a non-type generic parameter.
10885 DWARF3 5.6.8 says we must set a DW_AT_const_value child
10886 attribute of TMPL_DIE which value represents the value
10887 of ARG.
10888 We must be careful here:
10889 The value of ARG might reference some function decls.
10890 We might currently be emitting debug info for a generic
10891 type and types are emitted before function decls, we don't
10892 know if the function decls referenced by ARG will actually be
10893 emitted after cgraph computations.
10894 So must defer the generation of the DW_AT_const_value to
10895 after cgraph is ready. */
10896 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10899 return tmpl_die;
10902 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
10903 PARM_PACK must be a template parameter pack. The returned DIE
10904 will be child DIE of PARENT_DIE. */
10906 static dw_die_ref
10907 template_parameter_pack_die (tree parm_pack,
10908 tree parm_pack_args,
10909 dw_die_ref parent_die)
10911 dw_die_ref die;
10912 int j;
10914 gcc_assert (parent_die && parm_pack);
10916 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10917 add_name_and_src_coords_attributes (die, parm_pack);
10918 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10919 generic_parameter_die (parm_pack,
10920 TREE_VEC_ELT (parm_pack_args, j),
10921 false /* Don't emit DW_AT_name */,
10922 die);
10923 return die;
10926 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10927 an enumerated type. */
10929 static inline int
10930 type_is_enum (const_tree type)
10932 return TREE_CODE (type) == ENUMERAL_TYPE;
10935 /* Return the DBX register number described by a given RTL node. */
10937 static unsigned int
10938 dbx_reg_number (const_rtx rtl)
10940 unsigned regno = REGNO (rtl);
10942 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10944 #ifdef LEAF_REG_REMAP
10945 if (crtl->uses_only_leaf_regs)
10947 int leaf_reg = LEAF_REG_REMAP (regno);
10948 if (leaf_reg != -1)
10949 regno = (unsigned) leaf_reg;
10951 #endif
10953 regno = DBX_REGISTER_NUMBER (regno);
10954 gcc_assert (regno != INVALID_REGNUM);
10955 return regno;
10958 /* Optionally add a DW_OP_piece term to a location description expression.
10959 DW_OP_piece is only added if the location description expression already
10960 doesn't end with DW_OP_piece. */
10962 static void
10963 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10965 dw_loc_descr_ref loc;
10967 if (*list_head != NULL)
10969 /* Find the end of the chain. */
10970 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10973 if (loc->dw_loc_opc != DW_OP_piece)
10974 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10978 /* Return a location descriptor that designates a machine register or
10979 zero if there is none. */
10981 static dw_loc_descr_ref
10982 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10984 rtx regs;
10986 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10987 return 0;
10989 /* We only use "frame base" when we're sure we're talking about the
10990 post-prologue local stack frame. We do this by *not* running
10991 register elimination until this point, and recognizing the special
10992 argument pointer and soft frame pointer rtx's.
10993 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
10994 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10995 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10997 dw_loc_descr_ref result = NULL;
10999 if (dwarf_version >= 4 || !dwarf_strict)
11001 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11002 initialized);
11003 if (result)
11004 add_loc_descr (&result,
11005 new_loc_descr (DW_OP_stack_value, 0, 0));
11007 return result;
11010 regs = targetm.dwarf_register_span (rtl);
11012 if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
11013 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11014 else
11016 unsigned int dbx_regnum = dbx_reg_number (rtl);
11017 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11018 return 0;
11019 return one_reg_loc_descriptor (dbx_regnum, initialized);
11023 /* Return a location descriptor that designates a machine register for
11024 a given hard register number. */
11026 static dw_loc_descr_ref
11027 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11029 dw_loc_descr_ref reg_loc_descr;
11031 if (regno <= 31)
11032 reg_loc_descr
11033 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11034 else
11035 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11037 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11038 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11040 return reg_loc_descr;
11043 /* Given an RTL of a register, return a location descriptor that
11044 designates a value that spans more than one register. */
11046 static dw_loc_descr_ref
11047 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11048 enum var_init_status initialized)
11050 int size, i;
11051 dw_loc_descr_ref loc_result = NULL;
11053 /* Simple, contiguous registers. */
11054 if (regs == NULL_RTX)
11056 unsigned reg = REGNO (rtl);
11057 int nregs;
11059 #ifdef LEAF_REG_REMAP
11060 if (crtl->uses_only_leaf_regs)
11062 int leaf_reg = LEAF_REG_REMAP (reg);
11063 if (leaf_reg != -1)
11064 reg = (unsigned) leaf_reg;
11066 #endif
11068 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11069 nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
11071 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11073 loc_result = NULL;
11074 while (nregs--)
11076 dw_loc_descr_ref t;
11078 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11079 VAR_INIT_STATUS_INITIALIZED);
11080 add_loc_descr (&loc_result, t);
11081 add_loc_descr_op_piece (&loc_result, size);
11082 ++reg;
11084 return loc_result;
11087 /* Now onto stupid register sets in non contiguous locations. */
11089 gcc_assert (GET_CODE (regs) == PARALLEL);
11091 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11092 loc_result = NULL;
11094 for (i = 0; i < XVECLEN (regs, 0); ++i)
11096 dw_loc_descr_ref t;
11098 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11099 VAR_INIT_STATUS_INITIALIZED);
11100 add_loc_descr (&loc_result, t);
11101 add_loc_descr_op_piece (&loc_result, size);
11104 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11105 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11106 return loc_result;
11109 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11111 /* Return a location descriptor that designates a constant i,
11112 as a compound operation from constant (i >> shift), constant shift
11113 and DW_OP_shl. */
11115 static dw_loc_descr_ref
11116 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11118 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11119 add_loc_descr (&ret, int_loc_descriptor (shift));
11120 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11121 return ret;
11124 /* Return a location descriptor that designates a constant. */
11126 static dw_loc_descr_ref
11127 int_loc_descriptor (HOST_WIDE_INT i)
11129 enum dwarf_location_atom op;
11131 /* Pick the smallest representation of a constant, rather than just
11132 defaulting to the LEB encoding. */
11133 if (i >= 0)
11135 int clz = clz_hwi (i);
11136 int ctz = ctz_hwi (i);
11137 if (i <= 31)
11138 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11139 else if (i <= 0xff)
11140 op = DW_OP_const1u;
11141 else if (i <= 0xffff)
11142 op = DW_OP_const2u;
11143 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11144 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11145 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11146 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11147 while DW_OP_const4u is 5 bytes. */
11148 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11149 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11150 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11151 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11152 while DW_OP_const4u is 5 bytes. */
11153 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11154 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11155 op = DW_OP_const4u;
11156 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11157 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11158 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11159 while DW_OP_constu of constant >= 0x100000000 takes at least
11160 6 bytes. */
11161 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11162 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11163 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11164 >= HOST_BITS_PER_WIDE_INT)
11165 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11166 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11167 while DW_OP_constu takes in this case at least 6 bytes. */
11168 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11169 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11170 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11171 && size_of_uleb128 (i) > 6)
11172 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11173 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11174 else
11175 op = DW_OP_constu;
11177 else
11179 if (i >= -0x80)
11180 op = DW_OP_const1s;
11181 else if (i >= -0x8000)
11182 op = DW_OP_const2s;
11183 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11185 if (size_of_int_loc_descriptor (i) < 5)
11187 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11188 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11189 return ret;
11191 op = DW_OP_const4s;
11193 else
11195 if (size_of_int_loc_descriptor (i)
11196 < (unsigned long) 1 + size_of_sleb128 (i))
11198 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11199 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11200 return ret;
11202 op = DW_OP_consts;
11206 return new_loc_descr (op, i, 0);
11209 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11210 without actually allocating it. */
11212 static unsigned long
11213 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11215 return size_of_int_loc_descriptor (i >> shift)
11216 + size_of_int_loc_descriptor (shift)
11217 + 1;
11220 /* Return size_of_locs (int_loc_descriptor (i)) without
11221 actually allocating it. */
11223 static unsigned long
11224 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11226 unsigned long s;
11228 if (i >= 0)
11230 int clz, ctz;
11231 if (i <= 31)
11232 return 1;
11233 else if (i <= 0xff)
11234 return 2;
11235 else if (i <= 0xffff)
11236 return 3;
11237 clz = clz_hwi (i);
11238 ctz = ctz_hwi (i);
11239 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11240 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11241 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11242 - clz - 5);
11243 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11244 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11245 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11246 - clz - 8);
11247 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11248 return 5;
11249 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11250 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11251 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11252 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11253 - clz - 8);
11254 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11255 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11256 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11257 - clz - 16);
11258 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11259 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11260 && s > 6)
11261 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11262 - clz - 32);
11263 else
11264 return 1 + s;
11266 else
11268 if (i >= -0x80)
11269 return 2;
11270 else if (i >= -0x8000)
11271 return 3;
11272 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11274 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11276 s = size_of_int_loc_descriptor (-i) + 1;
11277 if (s < 5)
11278 return s;
11280 return 5;
11282 else
11284 unsigned long r = 1 + size_of_sleb128 (i);
11285 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11287 s = size_of_int_loc_descriptor (-i) + 1;
11288 if (s < r)
11289 return s;
11291 return r;
11296 /* Return loc description representing "address" of integer value.
11297 This can appear only as toplevel expression. */
11299 static dw_loc_descr_ref
11300 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11302 int litsize;
11303 dw_loc_descr_ref loc_result = NULL;
11305 if (!(dwarf_version >= 4 || !dwarf_strict))
11306 return NULL;
11308 litsize = size_of_int_loc_descriptor (i);
11309 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11310 is more compact. For DW_OP_stack_value we need:
11311 litsize + 1 (DW_OP_stack_value)
11312 and for DW_OP_implicit_value:
11313 1 (DW_OP_implicit_value) + 1 (length) + size. */
11314 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11316 loc_result = int_loc_descriptor (i);
11317 add_loc_descr (&loc_result,
11318 new_loc_descr (DW_OP_stack_value, 0, 0));
11319 return loc_result;
11322 loc_result = new_loc_descr (DW_OP_implicit_value,
11323 size, 0);
11324 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11325 loc_result->dw_loc_oprnd2.v.val_int = i;
11326 return loc_result;
11329 /* Return a location descriptor that designates a base+offset location. */
11331 static dw_loc_descr_ref
11332 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11333 enum var_init_status initialized)
11335 unsigned int regno;
11336 dw_loc_descr_ref result;
11337 dw_fde_ref fde = cfun->fde;
11339 /* We only use "frame base" when we're sure we're talking about the
11340 post-prologue local stack frame. We do this by *not* running
11341 register elimination until this point, and recognizing the special
11342 argument pointer and soft frame pointer rtx's. */
11343 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11345 rtx elim = (ira_use_lra_p
11346 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11347 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11349 if (elim != reg)
11351 if (GET_CODE (elim) == PLUS)
11353 offset += INTVAL (XEXP (elim, 1));
11354 elim = XEXP (elim, 0);
11356 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11357 && (elim == hard_frame_pointer_rtx
11358 || elim == stack_pointer_rtx))
11359 || elim == (frame_pointer_needed
11360 ? hard_frame_pointer_rtx
11361 : stack_pointer_rtx));
11363 /* If drap register is used to align stack, use frame
11364 pointer + offset to access stack variables. If stack
11365 is aligned without drap, use stack pointer + offset to
11366 access stack variables. */
11367 if (crtl->stack_realign_tried
11368 && reg == frame_pointer_rtx)
11370 int base_reg
11371 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11372 ? HARD_FRAME_POINTER_REGNUM
11373 : REGNO (elim));
11374 return new_reg_loc_descr (base_reg, offset);
11377 gcc_assert (frame_pointer_fb_offset_valid);
11378 offset += frame_pointer_fb_offset;
11379 return new_loc_descr (DW_OP_fbreg, offset, 0);
11383 regno = REGNO (reg);
11384 #ifdef LEAF_REG_REMAP
11385 if (crtl->uses_only_leaf_regs)
11387 int leaf_reg = LEAF_REG_REMAP (regno);
11388 if (leaf_reg != -1)
11389 regno = (unsigned) leaf_reg;
11391 #endif
11392 regno = DWARF_FRAME_REGNUM (regno);
11394 if (!optimize && fde
11395 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11397 /* Use cfa+offset to represent the location of arguments passed
11398 on the stack when drap is used to align stack.
11399 Only do this when not optimizing, for optimized code var-tracking
11400 is supposed to track where the arguments live and the register
11401 used as vdrap or drap in some spot might be used for something
11402 else in other part of the routine. */
11403 return new_loc_descr (DW_OP_fbreg, offset, 0);
11406 if (regno <= 31)
11407 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11408 offset, 0);
11409 else
11410 result = new_loc_descr (DW_OP_bregx, regno, offset);
11412 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11413 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11415 return result;
11418 /* Return true if this RTL expression describes a base+offset calculation. */
11420 static inline int
11421 is_based_loc (const_rtx rtl)
11423 return (GET_CODE (rtl) == PLUS
11424 && ((REG_P (XEXP (rtl, 0))
11425 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11426 && CONST_INT_P (XEXP (rtl, 1)))));
11429 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11430 failed. */
11432 static dw_loc_descr_ref
11433 tls_mem_loc_descriptor (rtx mem)
11435 tree base;
11436 dw_loc_descr_ref loc_result;
11438 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11439 return NULL;
11441 base = get_base_address (MEM_EXPR (mem));
11442 if (base == NULL
11443 || TREE_CODE (base) != VAR_DECL
11444 || !DECL_THREAD_LOCAL_P (base))
11445 return NULL;
11447 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11448 if (loc_result == NULL)
11449 return NULL;
11451 if (MEM_OFFSET (mem))
11452 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11454 return loc_result;
11457 /* Output debug info about reason why we failed to expand expression as dwarf
11458 expression. */
11460 static void
11461 expansion_failed (tree expr, rtx rtl, char const *reason)
11463 if (dump_file && (dump_flags & TDF_DETAILS))
11465 fprintf (dump_file, "Failed to expand as dwarf: ");
11466 if (expr)
11467 print_generic_expr (dump_file, expr, dump_flags);
11468 if (rtl)
11470 fprintf (dump_file, "\n");
11471 print_rtl (dump_file, rtl);
11473 fprintf (dump_file, "\nReason: %s\n", reason);
11477 /* Helper function for const_ok_for_output. */
11479 static bool
11480 const_ok_for_output_1 (rtx rtl)
11482 if (GET_CODE (rtl) == UNSPEC)
11484 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11485 we can't express it in the debug info. */
11486 #ifdef ENABLE_CHECKING
11487 /* Don't complain about TLS UNSPECs, those are just too hard to
11488 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11489 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11490 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11491 if (XVECLEN (rtl, 0) == 0
11492 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11493 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11494 inform (current_function_decl
11495 ? DECL_SOURCE_LOCATION (current_function_decl)
11496 : UNKNOWN_LOCATION,
11497 #if NUM_UNSPEC_VALUES > 0
11498 "non-delegitimized UNSPEC %s (%d) found in variable location",
11499 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11500 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11501 XINT (rtl, 1));
11502 #else
11503 "non-delegitimized UNSPEC %d found in variable location",
11504 XINT (rtl, 1));
11505 #endif
11506 #endif
11507 expansion_failed (NULL_TREE, rtl,
11508 "UNSPEC hasn't been delegitimized.\n");
11509 return false;
11512 if (targetm.const_not_ok_for_debug_p (rtl))
11514 expansion_failed (NULL_TREE, rtl,
11515 "Expression rejected for debug by the backend.\n");
11516 return false;
11519 /* FIXME: Refer to PR60655. It is possible for simplification
11520 of rtl expressions in var tracking to produce such expressions.
11521 We should really identify / validate expressions
11522 enclosed in CONST that can be handled by assemblers on various
11523 targets and only handle legitimate cases here. */
11524 if (GET_CODE (rtl) != SYMBOL_REF)
11526 if (GET_CODE (rtl) == NOT)
11527 return false;
11528 return true;
11531 if (CONSTANT_POOL_ADDRESS_P (rtl))
11533 bool marked;
11534 get_pool_constant_mark (rtl, &marked);
11535 /* If all references to this pool constant were optimized away,
11536 it was not output and thus we can't represent it. */
11537 if (!marked)
11539 expansion_failed (NULL_TREE, rtl,
11540 "Constant was removed from constant pool.\n");
11541 return false;
11545 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11546 return false;
11548 /* Avoid references to external symbols in debug info, on several targets
11549 the linker might even refuse to link when linking a shared library,
11550 and in many other cases the relocations for .debug_info/.debug_loc are
11551 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11552 to be defined within the same shared library or executable are fine. */
11553 if (SYMBOL_REF_EXTERNAL_P (rtl))
11555 tree decl = SYMBOL_REF_DECL (rtl);
11557 if (decl == NULL || !targetm.binds_local_p (decl))
11559 expansion_failed (NULL_TREE, rtl,
11560 "Symbol not defined in current TU.\n");
11561 return false;
11565 return true;
11568 /* Return true if constant RTL can be emitted in DW_OP_addr or
11569 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11570 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11572 static bool
11573 const_ok_for_output (rtx rtl)
11575 if (GET_CODE (rtl) == SYMBOL_REF)
11576 return const_ok_for_output_1 (rtl);
11578 if (GET_CODE (rtl) == CONST)
11580 subrtx_var_iterator::array_type array;
11581 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11582 if (!const_ok_for_output_1 (*iter))
11583 return false;
11584 return true;
11587 return true;
11590 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11591 if possible, NULL otherwise. */
11593 static dw_die_ref
11594 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11596 dw_die_ref type_die;
11597 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11599 if (type == NULL)
11600 return NULL;
11601 switch (TREE_CODE (type))
11603 case INTEGER_TYPE:
11604 case REAL_TYPE:
11605 break;
11606 default:
11607 return NULL;
11609 type_die = lookup_type_die (type);
11610 if (!type_die)
11611 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11612 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11613 return NULL;
11614 return type_die;
11617 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11618 type matching MODE, or, if MODE is narrower than or as wide as
11619 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11620 possible. */
11622 static dw_loc_descr_ref
11623 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11625 enum machine_mode outer_mode = mode;
11626 dw_die_ref type_die;
11627 dw_loc_descr_ref cvt;
11629 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11631 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11632 return op;
11634 type_die = base_type_for_mode (outer_mode, 1);
11635 if (type_die == NULL)
11636 return NULL;
11637 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11638 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11639 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11640 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11641 add_loc_descr (&op, cvt);
11642 return op;
11645 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11647 static dw_loc_descr_ref
11648 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11649 dw_loc_descr_ref op1)
11651 dw_loc_descr_ref ret = op0;
11652 add_loc_descr (&ret, op1);
11653 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11654 if (STORE_FLAG_VALUE != 1)
11656 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11657 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11659 return ret;
11662 /* Return location descriptor for signed comparison OP RTL. */
11664 static dw_loc_descr_ref
11665 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11666 enum machine_mode mem_mode)
11668 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11669 dw_loc_descr_ref op0, op1;
11670 int shift;
11672 if (op_mode == VOIDmode)
11673 op_mode = GET_MODE (XEXP (rtl, 1));
11674 if (op_mode == VOIDmode)
11675 return NULL;
11677 if (dwarf_strict
11678 && (GET_MODE_CLASS (op_mode) != MODE_INT
11679 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11680 return NULL;
11682 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11683 VAR_INIT_STATUS_INITIALIZED);
11684 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11685 VAR_INIT_STATUS_INITIALIZED);
11687 if (op0 == NULL || op1 == NULL)
11688 return NULL;
11690 if (GET_MODE_CLASS (op_mode) != MODE_INT
11691 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11692 return compare_loc_descriptor (op, op0, op1);
11694 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11696 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11697 dw_loc_descr_ref cvt;
11699 if (type_die == NULL)
11700 return NULL;
11701 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11702 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11703 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11704 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11705 add_loc_descr (&op0, cvt);
11706 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11707 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11708 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11709 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11710 add_loc_descr (&op1, cvt);
11711 return compare_loc_descriptor (op, op0, op1);
11714 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11715 /* For eq/ne, if the operands are known to be zero-extended,
11716 there is no need to do the fancy shifting up. */
11717 if (op == DW_OP_eq || op == DW_OP_ne)
11719 dw_loc_descr_ref last0, last1;
11720 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11722 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11724 /* deref_size zero extends, and for constants we can check
11725 whether they are zero extended or not. */
11726 if (((last0->dw_loc_opc == DW_OP_deref_size
11727 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11728 || (CONST_INT_P (XEXP (rtl, 0))
11729 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11730 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11731 && ((last1->dw_loc_opc == DW_OP_deref_size
11732 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11733 || (CONST_INT_P (XEXP (rtl, 1))
11734 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11735 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11736 return compare_loc_descriptor (op, op0, op1);
11738 /* EQ/NE comparison against constant in narrower type than
11739 DWARF2_ADDR_SIZE can be performed either as
11740 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11741 DW_OP_{eq,ne}
11743 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11744 DW_OP_{eq,ne}. Pick whatever is shorter. */
11745 if (CONST_INT_P (XEXP (rtl, 1))
11746 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11747 && (size_of_int_loc_descriptor (shift) + 1
11748 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11749 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11750 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11751 & GET_MODE_MASK (op_mode))))
11753 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11754 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11755 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11756 & GET_MODE_MASK (op_mode));
11757 return compare_loc_descriptor (op, op0, op1);
11760 add_loc_descr (&op0, int_loc_descriptor (shift));
11761 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11762 if (CONST_INT_P (XEXP (rtl, 1)))
11763 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11764 else
11766 add_loc_descr (&op1, int_loc_descriptor (shift));
11767 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11769 return compare_loc_descriptor (op, op0, op1);
11772 /* Return location descriptor for unsigned comparison OP RTL. */
11774 static dw_loc_descr_ref
11775 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11776 enum machine_mode mem_mode)
11778 enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11779 dw_loc_descr_ref op0, op1;
11781 if (op_mode == VOIDmode)
11782 op_mode = GET_MODE (XEXP (rtl, 1));
11783 if (op_mode == VOIDmode)
11784 return NULL;
11785 if (GET_MODE_CLASS (op_mode) != MODE_INT)
11786 return NULL;
11788 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11789 return NULL;
11791 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11792 VAR_INIT_STATUS_INITIALIZED);
11793 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11794 VAR_INIT_STATUS_INITIALIZED);
11796 if (op0 == NULL || op1 == NULL)
11797 return NULL;
11799 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11801 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11802 dw_loc_descr_ref last0, last1;
11803 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11805 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11807 if (CONST_INT_P (XEXP (rtl, 0)))
11808 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11809 /* deref_size zero extends, so no need to mask it again. */
11810 else if (last0->dw_loc_opc != DW_OP_deref_size
11811 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11813 add_loc_descr (&op0, int_loc_descriptor (mask));
11814 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11816 if (CONST_INT_P (XEXP (rtl, 1)))
11817 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11818 /* deref_size zero extends, so no need to mask it again. */
11819 else if (last1->dw_loc_opc != DW_OP_deref_size
11820 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11822 add_loc_descr (&op1, int_loc_descriptor (mask));
11823 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11826 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11828 HOST_WIDE_INT bias = 1;
11829 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11830 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11831 if (CONST_INT_P (XEXP (rtl, 1)))
11832 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11833 + INTVAL (XEXP (rtl, 1)));
11834 else
11835 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11836 bias, 0));
11838 return compare_loc_descriptor (op, op0, op1);
11841 /* Return location descriptor for {U,S}{MIN,MAX}. */
11843 static dw_loc_descr_ref
11844 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11845 enum machine_mode mem_mode)
11847 enum dwarf_location_atom op;
11848 dw_loc_descr_ref op0, op1, ret;
11849 dw_loc_descr_ref bra_node, drop_node;
11851 if (dwarf_strict
11852 && (GET_MODE_CLASS (mode) != MODE_INT
11853 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11854 return NULL;
11856 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11857 VAR_INIT_STATUS_INITIALIZED);
11858 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11859 VAR_INIT_STATUS_INITIALIZED);
11861 if (op0 == NULL || op1 == NULL)
11862 return NULL;
11864 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11865 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11866 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11867 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11869 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11871 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11872 add_loc_descr (&op0, int_loc_descriptor (mask));
11873 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11874 add_loc_descr (&op1, int_loc_descriptor (mask));
11875 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11877 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11879 HOST_WIDE_INT bias = 1;
11880 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11881 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11882 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11885 else if (GET_MODE_CLASS (mode) == MODE_INT
11886 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11888 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11889 add_loc_descr (&op0, int_loc_descriptor (shift));
11890 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11891 add_loc_descr (&op1, int_loc_descriptor (shift));
11892 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11894 else if (GET_MODE_CLASS (mode) == MODE_INT
11895 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11897 dw_die_ref type_die = base_type_for_mode (mode, 0);
11898 dw_loc_descr_ref cvt;
11899 if (type_die == NULL)
11900 return NULL;
11901 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11902 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11903 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11904 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11905 add_loc_descr (&op0, cvt);
11906 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11907 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11908 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11909 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11910 add_loc_descr (&op1, cvt);
11913 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11914 op = DW_OP_lt;
11915 else
11916 op = DW_OP_gt;
11917 ret = op0;
11918 add_loc_descr (&ret, op1);
11919 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11920 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11921 add_loc_descr (&ret, bra_node);
11922 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11923 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11924 add_loc_descr (&ret, drop_node);
11925 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11926 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11927 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11928 && GET_MODE_CLASS (mode) == MODE_INT
11929 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11930 ret = convert_descriptor_to_mode (mode, ret);
11931 return ret;
11934 /* Helper function for mem_loc_descriptor. Perform OP binary op,
11935 but after converting arguments to type_die, afterwards
11936 convert back to unsigned. */
11938 static dw_loc_descr_ref
11939 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11940 enum machine_mode mode, enum machine_mode mem_mode)
11942 dw_loc_descr_ref cvt, op0, op1;
11944 if (type_die == NULL)
11945 return NULL;
11946 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11947 VAR_INIT_STATUS_INITIALIZED);
11948 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11949 VAR_INIT_STATUS_INITIALIZED);
11950 if (op0 == NULL || op1 == NULL)
11951 return NULL;
11952 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11953 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11954 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11955 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11956 add_loc_descr (&op0, cvt);
11957 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11958 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11959 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11960 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11961 add_loc_descr (&op1, cvt);
11962 add_loc_descr (&op0, op1);
11963 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11964 return convert_descriptor_to_mode (mode, op0);
11967 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11968 const0 is DW_OP_lit0 or corresponding typed constant,
11969 const1 is DW_OP_lit1 or corresponding typed constant
11970 and constMSB is constant with just the MSB bit set
11971 for the mode):
11972 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11973 L1: const0 DW_OP_swap
11974 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11975 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11976 L3: DW_OP_drop
11977 L4: DW_OP_nop
11979 CTZ is similar:
11980 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11981 L1: const0 DW_OP_swap
11982 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11983 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11984 L3: DW_OP_drop
11985 L4: DW_OP_nop
11987 FFS is similar:
11988 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11989 L1: const1 DW_OP_swap
11990 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11991 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11992 L3: DW_OP_drop
11993 L4: DW_OP_nop */
11995 static dw_loc_descr_ref
11996 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11997 enum machine_mode mem_mode)
11999 dw_loc_descr_ref op0, ret, tmp;
12000 HOST_WIDE_INT valv;
12001 dw_loc_descr_ref l1jump, l1label;
12002 dw_loc_descr_ref l2jump, l2label;
12003 dw_loc_descr_ref l3jump, l3label;
12004 dw_loc_descr_ref l4jump, l4label;
12005 rtx msb;
12007 if (GET_MODE_CLASS (mode) != MODE_INT
12008 || GET_MODE (XEXP (rtl, 0)) != mode)
12009 return NULL;
12011 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12012 VAR_INIT_STATUS_INITIALIZED);
12013 if (op0 == NULL)
12014 return NULL;
12015 ret = op0;
12016 if (GET_CODE (rtl) == CLZ)
12018 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12019 valv = GET_MODE_BITSIZE (mode);
12021 else if (GET_CODE (rtl) == FFS)
12022 valv = 0;
12023 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12024 valv = GET_MODE_BITSIZE (mode);
12025 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12026 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12027 add_loc_descr (&ret, l1jump);
12028 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12029 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12030 VAR_INIT_STATUS_INITIALIZED);
12031 if (tmp == NULL)
12032 return NULL;
12033 add_loc_descr (&ret, tmp);
12034 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12035 add_loc_descr (&ret, l4jump);
12036 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12037 ? const1_rtx : const0_rtx,
12038 mode, mem_mode,
12039 VAR_INIT_STATUS_INITIALIZED);
12040 if (l1label == NULL)
12041 return NULL;
12042 add_loc_descr (&ret, l1label);
12043 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12044 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12045 add_loc_descr (&ret, l2label);
12046 if (GET_CODE (rtl) != CLZ)
12047 msb = const1_rtx;
12048 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12049 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12050 << (GET_MODE_BITSIZE (mode) - 1));
12051 else
12052 msb = immed_wide_int_const
12053 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12054 GET_MODE_PRECISION (mode)), mode);
12055 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12056 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12057 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12058 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12059 else
12060 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12061 VAR_INIT_STATUS_INITIALIZED);
12062 if (tmp == NULL)
12063 return NULL;
12064 add_loc_descr (&ret, tmp);
12065 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12066 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12067 add_loc_descr (&ret, l3jump);
12068 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12069 VAR_INIT_STATUS_INITIALIZED);
12070 if (tmp == NULL)
12071 return NULL;
12072 add_loc_descr (&ret, tmp);
12073 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12074 ? DW_OP_shl : DW_OP_shr, 0, 0));
12075 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12076 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12077 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12078 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12079 add_loc_descr (&ret, l2jump);
12080 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12081 add_loc_descr (&ret, l3label);
12082 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12083 add_loc_descr (&ret, l4label);
12084 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12085 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12086 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12087 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12088 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12089 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12090 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12091 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12092 return ret;
12095 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12096 const1 is DW_OP_lit1 or corresponding typed constant):
12097 const0 DW_OP_swap
12098 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12099 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12100 L2: DW_OP_drop
12102 PARITY is similar:
12103 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12104 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12105 L2: DW_OP_drop */
12107 static dw_loc_descr_ref
12108 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
12109 enum machine_mode mem_mode)
12111 dw_loc_descr_ref op0, ret, tmp;
12112 dw_loc_descr_ref l1jump, l1label;
12113 dw_loc_descr_ref l2jump, l2label;
12115 if (GET_MODE_CLASS (mode) != MODE_INT
12116 || GET_MODE (XEXP (rtl, 0)) != mode)
12117 return NULL;
12119 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12120 VAR_INIT_STATUS_INITIALIZED);
12121 if (op0 == NULL)
12122 return NULL;
12123 ret = op0;
12124 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12125 VAR_INIT_STATUS_INITIALIZED);
12126 if (tmp == NULL)
12127 return NULL;
12128 add_loc_descr (&ret, tmp);
12129 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12130 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12131 add_loc_descr (&ret, l1label);
12132 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12133 add_loc_descr (&ret, l2jump);
12134 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12135 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12136 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12137 VAR_INIT_STATUS_INITIALIZED);
12138 if (tmp == NULL)
12139 return NULL;
12140 add_loc_descr (&ret, tmp);
12141 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12142 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12143 ? DW_OP_plus : DW_OP_xor, 0, 0));
12144 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12145 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12146 VAR_INIT_STATUS_INITIALIZED);
12147 add_loc_descr (&ret, tmp);
12148 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12149 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12150 add_loc_descr (&ret, l1jump);
12151 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12152 add_loc_descr (&ret, l2label);
12153 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12154 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12155 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12156 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12157 return ret;
12160 /* BSWAP (constS is initial shift count, either 56 or 24):
12161 constS const0
12162 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12163 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12164 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12165 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12166 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12168 static dw_loc_descr_ref
12169 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
12170 enum machine_mode mem_mode)
12172 dw_loc_descr_ref op0, ret, tmp;
12173 dw_loc_descr_ref l1jump, l1label;
12174 dw_loc_descr_ref l2jump, l2label;
12176 if (GET_MODE_CLASS (mode) != MODE_INT
12177 || BITS_PER_UNIT != 8
12178 || (GET_MODE_BITSIZE (mode) != 32
12179 && GET_MODE_BITSIZE (mode) != 64))
12180 return NULL;
12182 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12183 VAR_INIT_STATUS_INITIALIZED);
12184 if (op0 == NULL)
12185 return NULL;
12187 ret = op0;
12188 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12189 mode, mem_mode,
12190 VAR_INIT_STATUS_INITIALIZED);
12191 if (tmp == NULL)
12192 return NULL;
12193 add_loc_descr (&ret, tmp);
12194 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12195 VAR_INIT_STATUS_INITIALIZED);
12196 if (tmp == NULL)
12197 return NULL;
12198 add_loc_descr (&ret, tmp);
12199 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12200 add_loc_descr (&ret, l1label);
12201 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12202 mode, mem_mode,
12203 VAR_INIT_STATUS_INITIALIZED);
12204 add_loc_descr (&ret, tmp);
12205 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12206 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12207 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12208 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12209 VAR_INIT_STATUS_INITIALIZED);
12210 if (tmp == NULL)
12211 return NULL;
12212 add_loc_descr (&ret, tmp);
12213 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12214 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12215 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12216 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12217 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12218 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12219 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12220 VAR_INIT_STATUS_INITIALIZED);
12221 add_loc_descr (&ret, tmp);
12222 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12223 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12224 add_loc_descr (&ret, l2jump);
12225 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12226 VAR_INIT_STATUS_INITIALIZED);
12227 add_loc_descr (&ret, tmp);
12228 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12229 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12230 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12231 add_loc_descr (&ret, l1jump);
12232 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12233 add_loc_descr (&ret, l2label);
12234 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12235 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12236 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12237 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12238 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12239 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12240 return ret;
12243 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12244 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12245 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12246 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12248 ROTATERT is similar:
12249 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12250 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12251 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12253 static dw_loc_descr_ref
12254 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
12255 enum machine_mode mem_mode)
12257 rtx rtlop1 = XEXP (rtl, 1);
12258 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12259 int i;
12261 if (GET_MODE_CLASS (mode) != MODE_INT)
12262 return NULL;
12264 if (GET_MODE (rtlop1) != VOIDmode
12265 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12266 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12267 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12268 VAR_INIT_STATUS_INITIALIZED);
12269 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12270 VAR_INIT_STATUS_INITIALIZED);
12271 if (op0 == NULL || op1 == NULL)
12272 return NULL;
12273 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12274 for (i = 0; i < 2; i++)
12276 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12277 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12278 mode, mem_mode,
12279 VAR_INIT_STATUS_INITIALIZED);
12280 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12281 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12282 ? DW_OP_const4u
12283 : HOST_BITS_PER_WIDE_INT == 64
12284 ? DW_OP_const8u : DW_OP_constu,
12285 GET_MODE_MASK (mode), 0);
12286 else
12287 mask[i] = NULL;
12288 if (mask[i] == NULL)
12289 return NULL;
12290 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12292 ret = op0;
12293 add_loc_descr (&ret, op1);
12294 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12295 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12296 if (GET_CODE (rtl) == ROTATERT)
12298 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12299 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12300 GET_MODE_BITSIZE (mode), 0));
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12303 if (mask[0] != NULL)
12304 add_loc_descr (&ret, mask[0]);
12305 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12306 if (mask[1] != NULL)
12308 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12309 add_loc_descr (&ret, mask[1]);
12310 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12312 if (GET_CODE (rtl) == ROTATE)
12314 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12315 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12316 GET_MODE_BITSIZE (mode), 0));
12318 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12319 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12320 return ret;
12323 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12324 for DEBUG_PARAMETER_REF RTL. */
12326 static dw_loc_descr_ref
12327 parameter_ref_descriptor (rtx rtl)
12329 dw_loc_descr_ref ret;
12330 dw_die_ref ref;
12332 if (dwarf_strict)
12333 return NULL;
12334 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12335 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12336 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12337 if (ref)
12339 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12340 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12341 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12343 else
12345 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12346 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12348 return ret;
12351 /* The following routine converts the RTL for a variable or parameter
12352 (resident in memory) into an equivalent Dwarf representation of a
12353 mechanism for getting the address of that same variable onto the top of a
12354 hypothetical "address evaluation" stack.
12356 When creating memory location descriptors, we are effectively transforming
12357 the RTL for a memory-resident object into its Dwarf postfix expression
12358 equivalent. This routine recursively descends an RTL tree, turning
12359 it into Dwarf postfix code as it goes.
12361 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12363 MEM_MODE is the mode of the memory reference, needed to handle some
12364 autoincrement addressing modes.
12366 Return 0 if we can't represent the location. */
12368 dw_loc_descr_ref
12369 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12370 enum machine_mode mem_mode,
12371 enum var_init_status initialized)
12373 dw_loc_descr_ref mem_loc_result = NULL;
12374 enum dwarf_location_atom op;
12375 dw_loc_descr_ref op0, op1;
12376 rtx inner = NULL_RTX;
12378 if (mode == VOIDmode)
12379 mode = GET_MODE (rtl);
12381 /* Note that for a dynamically sized array, the location we will generate a
12382 description of here will be the lowest numbered location which is
12383 actually within the array. That's *not* necessarily the same as the
12384 zeroth element of the array. */
12386 rtl = targetm.delegitimize_address (rtl);
12388 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12389 return NULL;
12391 switch (GET_CODE (rtl))
12393 case POST_INC:
12394 case POST_DEC:
12395 case POST_MODIFY:
12396 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12398 case SUBREG:
12399 /* The case of a subreg may arise when we have a local (register)
12400 variable or a formal (register) parameter which doesn't quite fill
12401 up an entire register. For now, just assume that it is
12402 legitimate to make the Dwarf info refer to the whole register which
12403 contains the given subreg. */
12404 if (!subreg_lowpart_p (rtl))
12405 break;
12406 inner = SUBREG_REG (rtl);
12407 case TRUNCATE:
12408 if (inner == NULL_RTX)
12409 inner = XEXP (rtl, 0);
12410 if (GET_MODE_CLASS (mode) == MODE_INT
12411 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12412 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12413 #ifdef POINTERS_EXTEND_UNSIGNED
12414 || (mode == Pmode && mem_mode != VOIDmode)
12415 #endif
12417 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12419 mem_loc_result = mem_loc_descriptor (inner,
12420 GET_MODE (inner),
12421 mem_mode, initialized);
12422 break;
12424 if (dwarf_strict)
12425 break;
12426 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12427 break;
12428 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12429 && (GET_MODE_CLASS (mode) != MODE_INT
12430 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12431 break;
12432 else
12434 dw_die_ref type_die;
12435 dw_loc_descr_ref cvt;
12437 mem_loc_result = mem_loc_descriptor (inner,
12438 GET_MODE (inner),
12439 mem_mode, initialized);
12440 if (mem_loc_result == NULL)
12441 break;
12442 type_die = base_type_for_mode (mode,
12443 GET_MODE_CLASS (mode) == MODE_INT);
12444 if (type_die == NULL)
12446 mem_loc_result = NULL;
12447 break;
12449 if (GET_MODE_SIZE (mode)
12450 != GET_MODE_SIZE (GET_MODE (inner)))
12451 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12452 else
12453 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12454 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12455 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12456 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12457 add_loc_descr (&mem_loc_result, cvt);
12459 break;
12461 case REG:
12462 if (GET_MODE_CLASS (mode) != MODE_INT
12463 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12464 && rtl != arg_pointer_rtx
12465 && rtl != frame_pointer_rtx
12466 #ifdef POINTERS_EXTEND_UNSIGNED
12467 && (mode != Pmode || mem_mode == VOIDmode)
12468 #endif
12471 dw_die_ref type_die;
12472 unsigned int dbx_regnum;
12474 if (dwarf_strict)
12475 break;
12476 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12477 break;
12478 type_die = base_type_for_mode (mode,
12479 GET_MODE_CLASS (mode) == MODE_INT);
12480 if (type_die == NULL)
12481 break;
12483 dbx_regnum = dbx_reg_number (rtl);
12484 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12485 break;
12486 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12487 dbx_regnum, 0);
12488 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12489 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12490 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12491 break;
12493 /* Whenever a register number forms a part of the description of the
12494 method for calculating the (dynamic) address of a memory resident
12495 object, DWARF rules require the register number be referred to as
12496 a "base register". This distinction is not based in any way upon
12497 what category of register the hardware believes the given register
12498 belongs to. This is strictly DWARF terminology we're dealing with
12499 here. Note that in cases where the location of a memory-resident
12500 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12501 OP_CONST (0)) the actual DWARF location descriptor that we generate
12502 may just be OP_BASEREG (basereg). This may look deceptively like
12503 the object in question was allocated to a register (rather than in
12504 memory) so DWARF consumers need to be aware of the subtle
12505 distinction between OP_REG and OP_BASEREG. */
12506 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12507 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12508 else if (stack_realign_drap
12509 && crtl->drap_reg
12510 && crtl->args.internal_arg_pointer == rtl
12511 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12513 /* If RTL is internal_arg_pointer, which has been optimized
12514 out, use DRAP instead. */
12515 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12516 VAR_INIT_STATUS_INITIALIZED);
12518 break;
12520 case SIGN_EXTEND:
12521 case ZERO_EXTEND:
12522 if (GET_MODE_CLASS (mode) != MODE_INT)
12523 break;
12524 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12525 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12526 if (op0 == 0)
12527 break;
12528 else if (GET_CODE (rtl) == ZERO_EXTEND
12529 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12530 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12531 < HOST_BITS_PER_WIDE_INT
12532 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12533 to expand zero extend as two shifts instead of
12534 masking. */
12535 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12537 enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12538 mem_loc_result = op0;
12539 add_loc_descr (&mem_loc_result,
12540 int_loc_descriptor (GET_MODE_MASK (imode)));
12541 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12543 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12545 int shift = DWARF2_ADDR_SIZE
12546 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12547 shift *= BITS_PER_UNIT;
12548 if (GET_CODE (rtl) == SIGN_EXTEND)
12549 op = DW_OP_shra;
12550 else
12551 op = DW_OP_shr;
12552 mem_loc_result = op0;
12553 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12554 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12555 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12556 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12558 else if (!dwarf_strict)
12560 dw_die_ref type_die1, type_die2;
12561 dw_loc_descr_ref cvt;
12563 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12564 GET_CODE (rtl) == ZERO_EXTEND);
12565 if (type_die1 == NULL)
12566 break;
12567 type_die2 = base_type_for_mode (mode, 1);
12568 if (type_die2 == NULL)
12569 break;
12570 mem_loc_result = op0;
12571 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12572 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12573 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12574 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12575 add_loc_descr (&mem_loc_result, cvt);
12576 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12577 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12578 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12579 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12580 add_loc_descr (&mem_loc_result, cvt);
12582 break;
12584 case MEM:
12586 rtx new_rtl = avoid_constant_pool_reference (rtl);
12587 if (new_rtl != rtl)
12589 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12590 initialized);
12591 if (mem_loc_result != NULL)
12592 return mem_loc_result;
12595 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12596 get_address_mode (rtl), mode,
12597 VAR_INIT_STATUS_INITIALIZED);
12598 if (mem_loc_result == NULL)
12599 mem_loc_result = tls_mem_loc_descriptor (rtl);
12600 if (mem_loc_result != NULL)
12602 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12603 || GET_MODE_CLASS (mode) != MODE_INT)
12605 dw_die_ref type_die;
12606 dw_loc_descr_ref deref;
12608 if (dwarf_strict)
12609 return NULL;
12610 type_die
12611 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12612 if (type_die == NULL)
12613 return NULL;
12614 deref = new_loc_descr (DW_OP_GNU_deref_type,
12615 GET_MODE_SIZE (mode), 0);
12616 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12617 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12618 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12619 add_loc_descr (&mem_loc_result, deref);
12621 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12622 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12623 else
12624 add_loc_descr (&mem_loc_result,
12625 new_loc_descr (DW_OP_deref_size,
12626 GET_MODE_SIZE (mode), 0));
12628 break;
12630 case LO_SUM:
12631 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12633 case LABEL_REF:
12634 /* Some ports can transform a symbol ref into a label ref, because
12635 the symbol ref is too far away and has to be dumped into a constant
12636 pool. */
12637 case CONST:
12638 case SYMBOL_REF:
12639 if ((GET_MODE_CLASS (mode) != MODE_INT
12640 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12641 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12642 #ifdef POINTERS_EXTEND_UNSIGNED
12643 && (mode != Pmode || mem_mode == VOIDmode)
12644 #endif
12646 break;
12647 if (GET_CODE (rtl) == SYMBOL_REF
12648 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12650 dw_loc_descr_ref temp;
12652 /* If this is not defined, we have no way to emit the data. */
12653 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12654 break;
12656 temp = new_addr_loc_descr (rtl, dtprel_true);
12658 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12659 add_loc_descr (&mem_loc_result, temp);
12661 break;
12664 if (!const_ok_for_output (rtl))
12665 break;
12667 symref:
12668 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12669 vec_safe_push (used_rtx_array, rtl);
12670 break;
12672 case CONCAT:
12673 case CONCATN:
12674 case VAR_LOCATION:
12675 case DEBUG_IMPLICIT_PTR:
12676 expansion_failed (NULL_TREE, rtl,
12677 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12678 return 0;
12680 case ENTRY_VALUE:
12681 if (dwarf_strict)
12682 return NULL;
12683 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12685 if (GET_MODE_CLASS (mode) != MODE_INT
12686 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12687 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12688 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12689 else
12691 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12692 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12693 return NULL;
12694 op0 = one_reg_loc_descriptor (dbx_regnum,
12695 VAR_INIT_STATUS_INITIALIZED);
12698 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12699 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12701 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12702 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12703 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12704 return NULL;
12706 else
12707 gcc_unreachable ();
12708 if (op0 == NULL)
12709 return NULL;
12710 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12711 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12712 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12713 break;
12715 case DEBUG_PARAMETER_REF:
12716 mem_loc_result = parameter_ref_descriptor (rtl);
12717 break;
12719 case PRE_MODIFY:
12720 /* Extract the PLUS expression nested inside and fall into
12721 PLUS code below. */
12722 rtl = XEXP (rtl, 1);
12723 goto plus;
12725 case PRE_INC:
12726 case PRE_DEC:
12727 /* Turn these into a PLUS expression and fall into the PLUS code
12728 below. */
12729 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12730 gen_int_mode (GET_CODE (rtl) == PRE_INC
12731 ? GET_MODE_UNIT_SIZE (mem_mode)
12732 : -GET_MODE_UNIT_SIZE (mem_mode),
12733 mode));
12735 /* ... fall through ... */
12737 case PLUS:
12738 plus:
12739 if (is_based_loc (rtl)
12740 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12741 || XEXP (rtl, 0) == arg_pointer_rtx
12742 || XEXP (rtl, 0) == frame_pointer_rtx)
12743 && GET_MODE_CLASS (mode) == MODE_INT)
12744 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12745 INTVAL (XEXP (rtl, 1)),
12746 VAR_INIT_STATUS_INITIALIZED);
12747 else
12749 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12750 VAR_INIT_STATUS_INITIALIZED);
12751 if (mem_loc_result == 0)
12752 break;
12754 if (CONST_INT_P (XEXP (rtl, 1))
12755 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12756 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12757 else
12759 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12760 VAR_INIT_STATUS_INITIALIZED);
12761 if (op1 == 0)
12762 return NULL;
12763 add_loc_descr (&mem_loc_result, op1);
12764 add_loc_descr (&mem_loc_result,
12765 new_loc_descr (DW_OP_plus, 0, 0));
12768 break;
12770 /* If a pseudo-reg is optimized away, it is possible for it to
12771 be replaced with a MEM containing a multiply or shift. */
12772 case MINUS:
12773 op = DW_OP_minus;
12774 goto do_binop;
12776 case MULT:
12777 op = DW_OP_mul;
12778 goto do_binop;
12780 case DIV:
12781 if (!dwarf_strict
12782 && GET_MODE_CLASS (mode) == MODE_INT
12783 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12785 mem_loc_result = typed_binop (DW_OP_div, rtl,
12786 base_type_for_mode (mode, 0),
12787 mode, mem_mode);
12788 break;
12790 op = DW_OP_div;
12791 goto do_binop;
12793 case UMOD:
12794 op = DW_OP_mod;
12795 goto do_binop;
12797 case ASHIFT:
12798 op = DW_OP_shl;
12799 goto do_shift;
12801 case ASHIFTRT:
12802 op = DW_OP_shra;
12803 goto do_shift;
12805 case LSHIFTRT:
12806 op = DW_OP_shr;
12807 goto do_shift;
12809 do_shift:
12810 if (GET_MODE_CLASS (mode) != MODE_INT)
12811 break;
12812 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12813 VAR_INIT_STATUS_INITIALIZED);
12815 rtx rtlop1 = XEXP (rtl, 1);
12816 if (GET_MODE (rtlop1) != VOIDmode
12817 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12818 < GET_MODE_BITSIZE (mode))
12819 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12820 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12821 VAR_INIT_STATUS_INITIALIZED);
12824 if (op0 == 0 || op1 == 0)
12825 break;
12827 mem_loc_result = op0;
12828 add_loc_descr (&mem_loc_result, op1);
12829 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12830 break;
12832 case AND:
12833 op = DW_OP_and;
12834 goto do_binop;
12836 case IOR:
12837 op = DW_OP_or;
12838 goto do_binop;
12840 case XOR:
12841 op = DW_OP_xor;
12842 goto do_binop;
12844 do_binop:
12845 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12846 VAR_INIT_STATUS_INITIALIZED);
12847 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12848 VAR_INIT_STATUS_INITIALIZED);
12850 if (op0 == 0 || op1 == 0)
12851 break;
12853 mem_loc_result = op0;
12854 add_loc_descr (&mem_loc_result, op1);
12855 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12856 break;
12858 case MOD:
12859 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12861 mem_loc_result = typed_binop (DW_OP_mod, rtl,
12862 base_type_for_mode (mode, 0),
12863 mode, mem_mode);
12864 break;
12867 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12868 VAR_INIT_STATUS_INITIALIZED);
12869 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12870 VAR_INIT_STATUS_INITIALIZED);
12872 if (op0 == 0 || op1 == 0)
12873 break;
12875 mem_loc_result = op0;
12876 add_loc_descr (&mem_loc_result, op1);
12877 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12878 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12879 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12880 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12881 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12882 break;
12884 case UDIV:
12885 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12887 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12889 op = DW_OP_div;
12890 goto do_binop;
12892 mem_loc_result = typed_binop (DW_OP_div, rtl,
12893 base_type_for_mode (mode, 1),
12894 mode, mem_mode);
12896 break;
12898 case NOT:
12899 op = DW_OP_not;
12900 goto do_unop;
12902 case ABS:
12903 op = DW_OP_abs;
12904 goto do_unop;
12906 case NEG:
12907 op = DW_OP_neg;
12908 goto do_unop;
12910 do_unop:
12911 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12912 VAR_INIT_STATUS_INITIALIZED);
12914 if (op0 == 0)
12915 break;
12917 mem_loc_result = op0;
12918 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12919 break;
12921 case CONST_INT:
12922 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12923 #ifdef POINTERS_EXTEND_UNSIGNED
12924 || (mode == Pmode
12925 && mem_mode != VOIDmode
12926 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12927 #endif
12930 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12931 break;
12933 if (!dwarf_strict
12934 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12935 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12937 dw_die_ref type_die = base_type_for_mode (mode, 1);
12938 enum machine_mode amode;
12939 if (type_die == NULL)
12940 return NULL;
12941 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12942 MODE_INT, 0);
12943 if (INTVAL (rtl) >= 0
12944 && amode != BLKmode
12945 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12946 /* const DW_OP_GNU_convert <XXX> vs.
12947 DW_OP_GNU_const_type <XXX, 1, const>. */
12948 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12949 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12951 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12952 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12953 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12954 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12955 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12956 add_loc_descr (&mem_loc_result, op0);
12957 return mem_loc_result;
12959 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12960 INTVAL (rtl));
12961 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12962 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12963 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12964 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12965 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12966 else
12968 mem_loc_result->dw_loc_oprnd2.val_class
12969 = dw_val_class_const_double;
12970 mem_loc_result->dw_loc_oprnd2.v.val_double
12971 = double_int::from_shwi (INTVAL (rtl));
12974 break;
12976 case CONST_DOUBLE:
12977 if (!dwarf_strict)
12979 dw_die_ref type_die;
12981 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
12982 CONST_DOUBLE rtx could represent either a large integer
12983 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
12984 the value is always a floating point constant.
12986 When it is an integer, a CONST_DOUBLE is used whenever
12987 the constant requires 2 HWIs to be adequately represented.
12988 We output CONST_DOUBLEs as blocks. */
12989 if (mode == VOIDmode
12990 || (GET_MODE (rtl) == VOIDmode
12991 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12992 break;
12993 type_die = base_type_for_mode (mode,
12994 GET_MODE_CLASS (mode) == MODE_INT);
12995 if (type_die == NULL)
12996 return NULL;
12997 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12998 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12999 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13000 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13001 #if TARGET_SUPPORTS_WIDE_INT == 0
13002 if (!SCALAR_FLOAT_MODE_P (mode))
13004 mem_loc_result->dw_loc_oprnd2.val_class
13005 = dw_val_class_const_double;
13006 mem_loc_result->dw_loc_oprnd2.v.val_double
13007 = rtx_to_double_int (rtl);
13009 else
13010 #endif
13012 unsigned int length = GET_MODE_SIZE (mode);
13013 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13015 insert_float (rtl, array);
13016 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13017 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13018 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13019 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13022 break;
13024 case CONST_WIDE_INT:
13025 if (!dwarf_strict)
13027 dw_die_ref type_die;
13029 type_die = base_type_for_mode (mode,
13030 GET_MODE_CLASS (mode) == MODE_INT);
13031 if (type_die == NULL)
13032 return NULL;
13033 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13034 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13035 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13036 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13037 mem_loc_result->dw_loc_oprnd2.val_class
13038 = dw_val_class_wide_int;
13039 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13040 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13042 break;
13044 case EQ:
13045 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13046 break;
13048 case GE:
13049 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13050 break;
13052 case GT:
13053 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13054 break;
13056 case LE:
13057 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13058 break;
13060 case LT:
13061 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13062 break;
13064 case NE:
13065 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13066 break;
13068 case GEU:
13069 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13070 break;
13072 case GTU:
13073 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13074 break;
13076 case LEU:
13077 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13078 break;
13080 case LTU:
13081 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13082 break;
13084 case UMIN:
13085 case UMAX:
13086 if (GET_MODE_CLASS (mode) != MODE_INT)
13087 break;
13088 /* FALLTHRU */
13089 case SMIN:
13090 case SMAX:
13091 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13092 break;
13094 case ZERO_EXTRACT:
13095 case SIGN_EXTRACT:
13096 if (CONST_INT_P (XEXP (rtl, 1))
13097 && CONST_INT_P (XEXP (rtl, 2))
13098 && ((unsigned) INTVAL (XEXP (rtl, 1))
13099 + (unsigned) INTVAL (XEXP (rtl, 2))
13100 <= GET_MODE_BITSIZE (mode))
13101 && GET_MODE_CLASS (mode) == MODE_INT
13102 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13103 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13105 int shift, size;
13106 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13107 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13108 if (op0 == 0)
13109 break;
13110 if (GET_CODE (rtl) == SIGN_EXTRACT)
13111 op = DW_OP_shra;
13112 else
13113 op = DW_OP_shr;
13114 mem_loc_result = op0;
13115 size = INTVAL (XEXP (rtl, 1));
13116 shift = INTVAL (XEXP (rtl, 2));
13117 if (BITS_BIG_ENDIAN)
13118 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13119 - shift - size;
13120 if (shift + size != (int) DWARF2_ADDR_SIZE)
13122 add_loc_descr (&mem_loc_result,
13123 int_loc_descriptor (DWARF2_ADDR_SIZE
13124 - shift - size));
13125 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13127 if (size != (int) DWARF2_ADDR_SIZE)
13129 add_loc_descr (&mem_loc_result,
13130 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13131 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13134 break;
13136 case IF_THEN_ELSE:
13138 dw_loc_descr_ref op2, bra_node, drop_node;
13139 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13140 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13141 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13142 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13143 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13144 VAR_INIT_STATUS_INITIALIZED);
13145 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13146 VAR_INIT_STATUS_INITIALIZED);
13147 if (op0 == NULL || op1 == NULL || op2 == NULL)
13148 break;
13150 mem_loc_result = op1;
13151 add_loc_descr (&mem_loc_result, op2);
13152 add_loc_descr (&mem_loc_result, op0);
13153 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13154 add_loc_descr (&mem_loc_result, bra_node);
13155 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13156 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13157 add_loc_descr (&mem_loc_result, drop_node);
13158 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13159 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13161 break;
13163 case FLOAT_EXTEND:
13164 case FLOAT_TRUNCATE:
13165 case FLOAT:
13166 case UNSIGNED_FLOAT:
13167 case FIX:
13168 case UNSIGNED_FIX:
13169 if (!dwarf_strict)
13171 dw_die_ref type_die;
13172 dw_loc_descr_ref cvt;
13174 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13175 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13176 if (op0 == NULL)
13177 break;
13178 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13179 && (GET_CODE (rtl) == FLOAT
13180 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13181 <= DWARF2_ADDR_SIZE))
13183 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13184 GET_CODE (rtl) == UNSIGNED_FLOAT);
13185 if (type_die == NULL)
13186 break;
13187 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13188 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13189 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13190 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13191 add_loc_descr (&op0, cvt);
13193 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13194 if (type_die == NULL)
13195 break;
13196 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13197 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13198 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13199 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13200 add_loc_descr (&op0, cvt);
13201 if (GET_MODE_CLASS (mode) == MODE_INT
13202 && (GET_CODE (rtl) == FIX
13203 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13205 op0 = convert_descriptor_to_mode (mode, op0);
13206 if (op0 == NULL)
13207 break;
13209 mem_loc_result = op0;
13211 break;
13213 case CLZ:
13214 case CTZ:
13215 case FFS:
13216 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13217 break;
13219 case POPCOUNT:
13220 case PARITY:
13221 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13222 break;
13224 case BSWAP:
13225 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13226 break;
13228 case ROTATE:
13229 case ROTATERT:
13230 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13231 break;
13233 case COMPARE:
13234 /* In theory, we could implement the above. */
13235 /* DWARF cannot represent the unsigned compare operations
13236 natively. */
13237 case SS_MULT:
13238 case US_MULT:
13239 case SS_DIV:
13240 case US_DIV:
13241 case SS_PLUS:
13242 case US_PLUS:
13243 case SS_MINUS:
13244 case US_MINUS:
13245 case SS_NEG:
13246 case US_NEG:
13247 case SS_ABS:
13248 case SS_ASHIFT:
13249 case US_ASHIFT:
13250 case SS_TRUNCATE:
13251 case US_TRUNCATE:
13252 case UNORDERED:
13253 case ORDERED:
13254 case UNEQ:
13255 case UNGE:
13256 case UNGT:
13257 case UNLE:
13258 case UNLT:
13259 case LTGT:
13260 case FRACT_CONVERT:
13261 case UNSIGNED_FRACT_CONVERT:
13262 case SAT_FRACT:
13263 case UNSIGNED_SAT_FRACT:
13264 case SQRT:
13265 case ASM_OPERANDS:
13266 case VEC_MERGE:
13267 case VEC_SELECT:
13268 case VEC_CONCAT:
13269 case VEC_DUPLICATE:
13270 case UNSPEC:
13271 case HIGH:
13272 case FMA:
13273 case STRICT_LOW_PART:
13274 case CONST_VECTOR:
13275 case CONST_FIXED:
13276 case CLRSB:
13277 case CLOBBER:
13278 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13279 can't express it in the debug info. This can happen e.g. with some
13280 TLS UNSPECs. */
13281 break;
13283 case CONST_STRING:
13284 resolve_one_addr (&rtl);
13285 goto symref;
13287 default:
13288 #ifdef ENABLE_CHECKING
13289 print_rtl (stderr, rtl);
13290 gcc_unreachable ();
13291 #else
13292 break;
13293 #endif
13296 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13297 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13299 return mem_loc_result;
13302 /* Return a descriptor that describes the concatenation of two locations.
13303 This is typically a complex variable. */
13305 static dw_loc_descr_ref
13306 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13308 dw_loc_descr_ref cc_loc_result = NULL;
13309 dw_loc_descr_ref x0_ref
13310 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13311 dw_loc_descr_ref x1_ref
13312 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13314 if (x0_ref == 0 || x1_ref == 0)
13315 return 0;
13317 cc_loc_result = x0_ref;
13318 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13320 add_loc_descr (&cc_loc_result, x1_ref);
13321 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13323 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13324 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13326 return cc_loc_result;
13329 /* Return a descriptor that describes the concatenation of N
13330 locations. */
13332 static dw_loc_descr_ref
13333 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13335 unsigned int i;
13336 dw_loc_descr_ref cc_loc_result = NULL;
13337 unsigned int n = XVECLEN (concatn, 0);
13339 for (i = 0; i < n; ++i)
13341 dw_loc_descr_ref ref;
13342 rtx x = XVECEXP (concatn, 0, i);
13344 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13345 if (ref == NULL)
13346 return NULL;
13348 add_loc_descr (&cc_loc_result, ref);
13349 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13352 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13353 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13355 return cc_loc_result;
13358 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13359 for DEBUG_IMPLICIT_PTR RTL. */
13361 static dw_loc_descr_ref
13362 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13364 dw_loc_descr_ref ret;
13365 dw_die_ref ref;
13367 if (dwarf_strict)
13368 return NULL;
13369 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13370 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13371 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13372 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13373 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13374 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13375 if (ref)
13377 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13378 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13379 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13381 else
13383 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13384 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13386 return ret;
13389 /* Output a proper Dwarf location descriptor for a variable or parameter
13390 which is either allocated in a register or in a memory location. For a
13391 register, we just generate an OP_REG and the register number. For a
13392 memory location we provide a Dwarf postfix expression describing how to
13393 generate the (dynamic) address of the object onto the address stack.
13395 MODE is mode of the decl if this loc_descriptor is going to be used in
13396 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13397 allowed, VOIDmode otherwise.
13399 If we don't know how to describe it, return 0. */
13401 static dw_loc_descr_ref
13402 loc_descriptor (rtx rtl, enum machine_mode mode,
13403 enum var_init_status initialized)
13405 dw_loc_descr_ref loc_result = NULL;
13407 switch (GET_CODE (rtl))
13409 case SUBREG:
13410 /* The case of a subreg may arise when we have a local (register)
13411 variable or a formal (register) parameter which doesn't quite fill
13412 up an entire register. For now, just assume that it is
13413 legitimate to make the Dwarf info refer to the whole register which
13414 contains the given subreg. */
13415 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13416 loc_result = loc_descriptor (SUBREG_REG (rtl),
13417 GET_MODE (SUBREG_REG (rtl)), initialized);
13418 else
13419 goto do_default;
13420 break;
13422 case REG:
13423 loc_result = reg_loc_descriptor (rtl, initialized);
13424 break;
13426 case MEM:
13427 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13428 GET_MODE (rtl), initialized);
13429 if (loc_result == NULL)
13430 loc_result = tls_mem_loc_descriptor (rtl);
13431 if (loc_result == NULL)
13433 rtx new_rtl = avoid_constant_pool_reference (rtl);
13434 if (new_rtl != rtl)
13435 loc_result = loc_descriptor (new_rtl, mode, initialized);
13437 break;
13439 case CONCAT:
13440 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13441 initialized);
13442 break;
13444 case CONCATN:
13445 loc_result = concatn_loc_descriptor (rtl, initialized);
13446 break;
13448 case VAR_LOCATION:
13449 /* Single part. */
13450 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13452 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13453 if (GET_CODE (loc) == EXPR_LIST)
13454 loc = XEXP (loc, 0);
13455 loc_result = loc_descriptor (loc, mode, initialized);
13456 break;
13459 rtl = XEXP (rtl, 1);
13460 /* FALLTHRU */
13462 case PARALLEL:
13464 rtvec par_elems = XVEC (rtl, 0);
13465 int num_elem = GET_NUM_ELEM (par_elems);
13466 enum machine_mode mode;
13467 int i;
13469 /* Create the first one, so we have something to add to. */
13470 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13471 VOIDmode, initialized);
13472 if (loc_result == NULL)
13473 return NULL;
13474 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13475 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13476 for (i = 1; i < num_elem; i++)
13478 dw_loc_descr_ref temp;
13480 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13481 VOIDmode, initialized);
13482 if (temp == NULL)
13483 return NULL;
13484 add_loc_descr (&loc_result, temp);
13485 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13486 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13489 break;
13491 case CONST_INT:
13492 if (mode != VOIDmode && mode != BLKmode)
13493 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13494 INTVAL (rtl));
13495 break;
13497 case CONST_DOUBLE:
13498 if (mode == VOIDmode)
13499 mode = GET_MODE (rtl);
13501 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13503 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13505 /* Note that a CONST_DOUBLE rtx could represent either an integer
13506 or a floating-point constant. A CONST_DOUBLE is used whenever
13507 the constant requires more than one word in order to be
13508 adequately represented. We output CONST_DOUBLEs as blocks. */
13509 loc_result = new_loc_descr (DW_OP_implicit_value,
13510 GET_MODE_SIZE (mode), 0);
13511 #if TARGET_SUPPORTS_WIDE_INT == 0
13512 if (!SCALAR_FLOAT_MODE_P (mode))
13514 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13515 loc_result->dw_loc_oprnd2.v.val_double
13516 = rtx_to_double_int (rtl);
13518 else
13519 #endif
13521 unsigned int length = GET_MODE_SIZE (mode);
13522 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13524 insert_float (rtl, array);
13525 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13526 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13527 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13528 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13531 break;
13533 case CONST_WIDE_INT:
13534 if (mode == VOIDmode)
13535 mode = GET_MODE (rtl);
13537 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13539 loc_result = new_loc_descr (DW_OP_implicit_value,
13540 GET_MODE_SIZE (mode), 0);
13541 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13542 loc_result->dw_loc_oprnd2.v.val_wide = ggc_cleared_alloc<wide_int> ();
13543 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13545 break;
13547 case CONST_VECTOR:
13548 if (mode == VOIDmode)
13549 mode = GET_MODE (rtl);
13551 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13553 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13554 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13555 unsigned char *array
13556 = ggc_vec_alloc<unsigned char> (length * elt_size);
13557 unsigned int i;
13558 unsigned char *p;
13559 enum machine_mode imode = GET_MODE_INNER (mode);
13561 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13562 switch (GET_MODE_CLASS (mode))
13564 case MODE_VECTOR_INT:
13565 for (i = 0, p = array; i < length; i++, p += elt_size)
13567 rtx elt = CONST_VECTOR_ELT (rtl, i);
13568 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13570 break;
13572 case MODE_VECTOR_FLOAT:
13573 for (i = 0, p = array; i < length; i++, p += elt_size)
13575 rtx elt = CONST_VECTOR_ELT (rtl, i);
13576 insert_float (elt, p);
13578 break;
13580 default:
13581 gcc_unreachable ();
13584 loc_result = new_loc_descr (DW_OP_implicit_value,
13585 length * elt_size, 0);
13586 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13587 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13588 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13589 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13591 break;
13593 case CONST:
13594 if (mode == VOIDmode
13595 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13596 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13597 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13599 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13600 break;
13602 /* FALLTHROUGH */
13603 case SYMBOL_REF:
13604 if (!const_ok_for_output (rtl))
13605 break;
13606 case LABEL_REF:
13607 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13608 && (dwarf_version >= 4 || !dwarf_strict))
13610 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13611 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13612 vec_safe_push (used_rtx_array, rtl);
13614 break;
13616 case DEBUG_IMPLICIT_PTR:
13617 loc_result = implicit_ptr_descriptor (rtl, 0);
13618 break;
13620 case PLUS:
13621 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13622 && CONST_INT_P (XEXP (rtl, 1)))
13624 loc_result
13625 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13626 break;
13628 /* FALLTHRU */
13629 do_default:
13630 default:
13631 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13632 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13633 && dwarf_version >= 4)
13634 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13636 /* Value expression. */
13637 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13638 if (loc_result)
13639 add_loc_descr (&loc_result,
13640 new_loc_descr (DW_OP_stack_value, 0, 0));
13642 break;
13645 return loc_result;
13648 /* We need to figure out what section we should use as the base for the
13649 address ranges where a given location is valid.
13650 1. If this particular DECL has a section associated with it, use that.
13651 2. If this function has a section associated with it, use that.
13652 3. Otherwise, use the text section.
13653 XXX: If you split a variable across multiple sections, we won't notice. */
13655 static const char *
13656 secname_for_decl (const_tree decl)
13658 const char *secname;
13660 if (VAR_OR_FUNCTION_DECL_P (decl)
13661 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13662 && DECL_SECTION_NAME (decl))
13663 secname = DECL_SECTION_NAME (decl);
13664 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13665 secname = DECL_SECTION_NAME (current_function_decl);
13666 else if (cfun && in_cold_section_p)
13667 secname = crtl->subsections.cold_section_label;
13668 else
13669 secname = text_section_label;
13671 return secname;
13674 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13676 static bool
13677 decl_by_reference_p (tree decl)
13679 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13680 || TREE_CODE (decl) == VAR_DECL)
13681 && DECL_BY_REFERENCE (decl));
13684 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13685 for VARLOC. */
13687 static dw_loc_descr_ref
13688 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13689 enum var_init_status initialized)
13691 int have_address = 0;
13692 dw_loc_descr_ref descr;
13693 enum machine_mode mode;
13695 if (want_address != 2)
13697 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13698 /* Single part. */
13699 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13701 varloc = PAT_VAR_LOCATION_LOC (varloc);
13702 if (GET_CODE (varloc) == EXPR_LIST)
13703 varloc = XEXP (varloc, 0);
13704 mode = GET_MODE (varloc);
13705 if (MEM_P (varloc))
13707 rtx addr = XEXP (varloc, 0);
13708 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13709 mode, initialized);
13710 if (descr)
13711 have_address = 1;
13712 else
13714 rtx x = avoid_constant_pool_reference (varloc);
13715 if (x != varloc)
13716 descr = mem_loc_descriptor (x, mode, VOIDmode,
13717 initialized);
13720 else
13721 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13723 else
13724 return 0;
13726 else
13728 if (GET_CODE (varloc) == VAR_LOCATION)
13729 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13730 else
13731 mode = DECL_MODE (loc);
13732 descr = loc_descriptor (varloc, mode, initialized);
13733 have_address = 1;
13736 if (!descr)
13737 return 0;
13739 if (want_address == 2 && !have_address
13740 && (dwarf_version >= 4 || !dwarf_strict))
13742 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13744 expansion_failed (loc, NULL_RTX,
13745 "DWARF address size mismatch");
13746 return 0;
13748 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13749 have_address = 1;
13751 /* Show if we can't fill the request for an address. */
13752 if (want_address && !have_address)
13754 expansion_failed (loc, NULL_RTX,
13755 "Want address and only have value");
13756 return 0;
13759 /* If we've got an address and don't want one, dereference. */
13760 if (!want_address && have_address)
13762 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13763 enum dwarf_location_atom op;
13765 if (size > DWARF2_ADDR_SIZE || size == -1)
13767 expansion_failed (loc, NULL_RTX,
13768 "DWARF address size mismatch");
13769 return 0;
13771 else if (size == DWARF2_ADDR_SIZE)
13772 op = DW_OP_deref;
13773 else
13774 op = DW_OP_deref_size;
13776 add_loc_descr (&descr, new_loc_descr (op, size, 0));
13779 return descr;
13782 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13783 if it is not possible. */
13785 static dw_loc_descr_ref
13786 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13788 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13789 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13790 else if (dwarf_version >= 3 || !dwarf_strict)
13791 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13792 else
13793 return NULL;
13796 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13797 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
13799 static dw_loc_descr_ref
13800 dw_sra_loc_expr (tree decl, rtx loc)
13802 rtx p;
13803 unsigned int padsize = 0;
13804 dw_loc_descr_ref descr, *descr_tail;
13805 unsigned HOST_WIDE_INT decl_size;
13806 rtx varloc;
13807 enum var_init_status initialized;
13809 if (DECL_SIZE (decl) == NULL
13810 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
13811 return NULL;
13813 decl_size = tree_to_uhwi (DECL_SIZE (decl));
13814 descr = NULL;
13815 descr_tail = &descr;
13817 for (p = loc; p; p = XEXP (p, 1))
13819 unsigned int bitsize = decl_piece_bitsize (p);
13820 rtx loc_note = *decl_piece_varloc_ptr (p);
13821 dw_loc_descr_ref cur_descr;
13822 dw_loc_descr_ref *tail, last = NULL;
13823 unsigned int opsize = 0;
13825 if (loc_note == NULL_RTX
13826 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13828 padsize += bitsize;
13829 continue;
13831 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13832 varloc = NOTE_VAR_LOCATION (loc_note);
13833 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13834 if (cur_descr == NULL)
13836 padsize += bitsize;
13837 continue;
13840 /* Check that cur_descr either doesn't use
13841 DW_OP_*piece operations, or their sum is equal
13842 to bitsize. Otherwise we can't embed it. */
13843 for (tail = &cur_descr; *tail != NULL;
13844 tail = &(*tail)->dw_loc_next)
13845 if ((*tail)->dw_loc_opc == DW_OP_piece)
13847 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13848 * BITS_PER_UNIT;
13849 last = *tail;
13851 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13853 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13854 last = *tail;
13857 if (last != NULL && opsize != bitsize)
13859 padsize += bitsize;
13860 /* Discard the current piece of the descriptor and release any
13861 addr_table entries it uses. */
13862 remove_loc_list_addr_table_entries (cur_descr);
13863 continue;
13866 /* If there is a hole, add DW_OP_*piece after empty DWARF
13867 expression, which means that those bits are optimized out. */
13868 if (padsize)
13870 if (padsize > decl_size)
13872 remove_loc_list_addr_table_entries (cur_descr);
13873 goto discard_descr;
13875 decl_size -= padsize;
13876 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13877 if (*descr_tail == NULL)
13879 remove_loc_list_addr_table_entries (cur_descr);
13880 goto discard_descr;
13882 descr_tail = &(*descr_tail)->dw_loc_next;
13883 padsize = 0;
13885 *descr_tail = cur_descr;
13886 descr_tail = tail;
13887 if (bitsize > decl_size)
13888 goto discard_descr;
13889 decl_size -= bitsize;
13890 if (last == NULL)
13892 HOST_WIDE_INT offset = 0;
13893 if (GET_CODE (varloc) == VAR_LOCATION
13894 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13896 varloc = PAT_VAR_LOCATION_LOC (varloc);
13897 if (GET_CODE (varloc) == EXPR_LIST)
13898 varloc = XEXP (varloc, 0);
13902 if (GET_CODE (varloc) == CONST
13903 || GET_CODE (varloc) == SIGN_EXTEND
13904 || GET_CODE (varloc) == ZERO_EXTEND)
13905 varloc = XEXP (varloc, 0);
13906 else if (GET_CODE (varloc) == SUBREG)
13907 varloc = SUBREG_REG (varloc);
13908 else
13909 break;
13911 while (1);
13912 /* DW_OP_bit_size offset should be zero for register
13913 or implicit location descriptions and empty location
13914 descriptions, but for memory addresses needs big endian
13915 adjustment. */
13916 if (MEM_P (varloc))
13918 unsigned HOST_WIDE_INT memsize
13919 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13920 if (memsize != bitsize)
13922 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13923 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13924 goto discard_descr;
13925 if (memsize < bitsize)
13926 goto discard_descr;
13927 if (BITS_BIG_ENDIAN)
13928 offset = memsize - bitsize;
13932 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13933 if (*descr_tail == NULL)
13934 goto discard_descr;
13935 descr_tail = &(*descr_tail)->dw_loc_next;
13939 /* If there were any non-empty expressions, add padding till the end of
13940 the decl. */
13941 if (descr != NULL && decl_size != 0)
13943 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13944 if (*descr_tail == NULL)
13945 goto discard_descr;
13947 return descr;
13949 discard_descr:
13950 /* Discard the descriptor and release any addr_table entries it uses. */
13951 remove_loc_list_addr_table_entries (descr);
13952 return NULL;
13955 /* Return the dwarf representation of the location list LOC_LIST of
13956 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
13957 function. */
13959 static dw_loc_list_ref
13960 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13962 const char *endname, *secname;
13963 rtx varloc;
13964 enum var_init_status initialized;
13965 struct var_loc_node *node;
13966 dw_loc_descr_ref descr;
13967 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13968 dw_loc_list_ref list = NULL;
13969 dw_loc_list_ref *listp = &list;
13971 /* Now that we know what section we are using for a base,
13972 actually construct the list of locations.
13973 The first location information is what is passed to the
13974 function that creates the location list, and the remaining
13975 locations just get added on to that list.
13976 Note that we only know the start address for a location
13977 (IE location changes), so to build the range, we use
13978 the range [current location start, next location start].
13979 This means we have to special case the last node, and generate
13980 a range of [last location start, end of function label]. */
13982 secname = secname_for_decl (decl);
13984 for (node = loc_list->first; node; node = node->next)
13985 if (GET_CODE (node->loc) == EXPR_LIST
13986 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13988 if (GET_CODE (node->loc) == EXPR_LIST)
13990 /* This requires DW_OP_{,bit_}piece, which is not usable
13991 inside DWARF expressions. */
13992 if (want_address != 2)
13993 continue;
13994 descr = dw_sra_loc_expr (decl, node->loc);
13995 if (descr == NULL)
13996 continue;
13998 else
14000 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14001 varloc = NOTE_VAR_LOCATION (node->loc);
14002 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14004 if (descr)
14006 bool range_across_switch = false;
14007 /* If section switch happens in between node->label
14008 and node->next->label (or end of function) and
14009 we can't emit it as a single entry list,
14010 emit two ranges, first one ending at the end
14011 of first partition and second one starting at the
14012 beginning of second partition. */
14013 if (node == loc_list->last_before_switch
14014 && (node != loc_list->first || loc_list->first->next)
14015 && current_function_decl)
14017 endname = cfun->fde->dw_fde_end;
14018 range_across_switch = true;
14020 /* The variable has a location between NODE->LABEL and
14021 NODE->NEXT->LABEL. */
14022 else if (node->next)
14023 endname = node->next->label;
14024 /* If the variable has a location at the last label
14025 it keeps its location until the end of function. */
14026 else if (!current_function_decl)
14027 endname = text_end_label;
14028 else
14030 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14031 current_function_funcdef_no);
14032 endname = ggc_strdup (label_id);
14035 *listp = new_loc_list (descr, node->label, endname, secname);
14036 if (TREE_CODE (decl) == PARM_DECL
14037 && node == loc_list->first
14038 && NOTE_P (node->loc)
14039 && strcmp (node->label, endname) == 0)
14040 (*listp)->force = true;
14041 listp = &(*listp)->dw_loc_next;
14043 if (range_across_switch)
14045 if (GET_CODE (node->loc) == EXPR_LIST)
14046 descr = dw_sra_loc_expr (decl, node->loc);
14047 else
14049 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14050 varloc = NOTE_VAR_LOCATION (node->loc);
14051 descr = dw_loc_list_1 (decl, varloc, want_address,
14052 initialized);
14054 gcc_assert (descr);
14055 /* The variable has a location between NODE->LABEL and
14056 NODE->NEXT->LABEL. */
14057 if (node->next)
14058 endname = node->next->label;
14059 else
14060 endname = cfun->fde->dw_fde_second_end;
14061 *listp = new_loc_list (descr,
14062 cfun->fde->dw_fde_second_begin,
14063 endname, secname);
14064 listp = &(*listp)->dw_loc_next;
14069 /* Try to avoid the overhead of a location list emitting a location
14070 expression instead, but only if we didn't have more than one
14071 location entry in the first place. If some entries were not
14072 representable, we don't want to pretend a single entry that was
14073 applies to the entire scope in which the variable is
14074 available. */
14075 if (list && loc_list->first->next)
14076 gen_llsym (list);
14078 return list;
14081 /* Return if the loc_list has only single element and thus can be represented
14082 as location description. */
14084 static bool
14085 single_element_loc_list_p (dw_loc_list_ref list)
14087 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14088 return !list->ll_symbol;
14091 /* To each location in list LIST add loc descr REF. */
14093 static void
14094 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14096 dw_loc_descr_ref copy;
14097 add_loc_descr (&list->expr, ref);
14098 list = list->dw_loc_next;
14099 while (list)
14101 copy = ggc_alloc<dw_loc_descr_node> ();
14102 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14103 add_loc_descr (&list->expr, copy);
14104 while (copy->dw_loc_next)
14106 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14107 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14108 copy->dw_loc_next = new_copy;
14109 copy = new_copy;
14111 list = list->dw_loc_next;
14115 /* Given two lists RET and LIST
14116 produce location list that is result of adding expression in LIST
14117 to expression in RET on each position in program.
14118 Might be destructive on both RET and LIST.
14120 TODO: We handle only simple cases of RET or LIST having at most one
14121 element. General case would inolve sorting the lists in program order
14122 and merging them that will need some additional work.
14123 Adding that will improve quality of debug info especially for SRA-ed
14124 structures. */
14126 static void
14127 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14129 if (!list)
14130 return;
14131 if (!*ret)
14133 *ret = list;
14134 return;
14136 if (!list->dw_loc_next)
14138 add_loc_descr_to_each (*ret, list->expr);
14139 return;
14141 if (!(*ret)->dw_loc_next)
14143 add_loc_descr_to_each (list, (*ret)->expr);
14144 *ret = list;
14145 return;
14147 expansion_failed (NULL_TREE, NULL_RTX,
14148 "Don't know how to merge two non-trivial"
14149 " location lists.\n");
14150 *ret = NULL;
14151 return;
14154 /* LOC is constant expression. Try a luck, look it up in constant
14155 pool and return its loc_descr of its address. */
14157 static dw_loc_descr_ref
14158 cst_pool_loc_descr (tree loc)
14160 /* Get an RTL for this, if something has been emitted. */
14161 rtx rtl = lookup_constant_def (loc);
14163 if (!rtl || !MEM_P (rtl))
14165 gcc_assert (!rtl);
14166 return 0;
14168 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14170 /* TODO: We might get more coverage if we was actually delaying expansion
14171 of all expressions till end of compilation when constant pools are fully
14172 populated. */
14173 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14175 expansion_failed (loc, NULL_RTX,
14176 "CST value in contant pool but not marked.");
14177 return 0;
14179 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14180 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14183 /* Return dw_loc_list representing address of addr_expr LOC
14184 by looking for inner INDIRECT_REF expression and turning
14185 it into simple arithmetics. */
14187 static dw_loc_list_ref
14188 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14190 tree obj, offset;
14191 HOST_WIDE_INT bitsize, bitpos, bytepos;
14192 enum machine_mode mode;
14193 int unsignedp, volatilep = 0;
14194 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14196 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14197 &bitsize, &bitpos, &offset, &mode,
14198 &unsignedp, &volatilep, false);
14199 STRIP_NOPS (obj);
14200 if (bitpos % BITS_PER_UNIT)
14202 expansion_failed (loc, NULL_RTX, "bitfield access");
14203 return 0;
14205 if (!INDIRECT_REF_P (obj))
14207 expansion_failed (obj,
14208 NULL_RTX, "no indirect ref in inner refrence");
14209 return 0;
14211 if (!offset && !bitpos)
14212 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14213 else if (toplev
14214 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14215 && (dwarf_version >= 4 || !dwarf_strict))
14217 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14218 if (!list_ret)
14219 return 0;
14220 if (offset)
14222 /* Variable offset. */
14223 list_ret1 = loc_list_from_tree (offset, 0);
14224 if (list_ret1 == 0)
14225 return 0;
14226 add_loc_list (&list_ret, list_ret1);
14227 if (!list_ret)
14228 return 0;
14229 add_loc_descr_to_each (list_ret,
14230 new_loc_descr (DW_OP_plus, 0, 0));
14232 bytepos = bitpos / BITS_PER_UNIT;
14233 if (bytepos > 0)
14234 add_loc_descr_to_each (list_ret,
14235 new_loc_descr (DW_OP_plus_uconst,
14236 bytepos, 0));
14237 else if (bytepos < 0)
14238 loc_list_plus_const (list_ret, bytepos);
14239 add_loc_descr_to_each (list_ret,
14240 new_loc_descr (DW_OP_stack_value, 0, 0));
14242 return list_ret;
14246 /* Generate Dwarf location list representing LOC.
14247 If WANT_ADDRESS is false, expression computing LOC will be computed
14248 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14249 if WANT_ADDRESS is 2, expression computing address useable in location
14250 will be returned (i.e. DW_OP_reg can be used
14251 to refer to register values). */
14253 static dw_loc_list_ref
14254 loc_list_from_tree (tree loc, int want_address)
14256 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14257 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14258 int have_address = 0;
14259 enum dwarf_location_atom op;
14261 /* ??? Most of the time we do not take proper care for sign/zero
14262 extending the values properly. Hopefully this won't be a real
14263 problem... */
14265 switch (TREE_CODE (loc))
14267 case ERROR_MARK:
14268 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14269 return 0;
14271 case PLACEHOLDER_EXPR:
14272 /* This case involves extracting fields from an object to determine the
14273 position of other fields. We don't try to encode this here. The
14274 only user of this is Ada, which encodes the needed information using
14275 the names of types. */
14276 expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
14277 return 0;
14279 case CALL_EXPR:
14280 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14281 /* There are no opcodes for these operations. */
14282 return 0;
14284 case PREINCREMENT_EXPR:
14285 case PREDECREMENT_EXPR:
14286 case POSTINCREMENT_EXPR:
14287 case POSTDECREMENT_EXPR:
14288 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14289 /* There are no opcodes for these operations. */
14290 return 0;
14292 case ADDR_EXPR:
14293 /* If we already want an address, see if there is INDIRECT_REF inside
14294 e.g. for &this->field. */
14295 if (want_address)
14297 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14298 (loc, want_address == 2);
14299 if (list_ret)
14300 have_address = 1;
14301 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14302 && (ret = cst_pool_loc_descr (loc)))
14303 have_address = 1;
14305 /* Otherwise, process the argument and look for the address. */
14306 if (!list_ret && !ret)
14307 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14308 else
14310 if (want_address)
14311 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14312 return NULL;
14314 break;
14316 case VAR_DECL:
14317 if (DECL_THREAD_LOCAL_P (loc))
14319 rtx rtl;
14320 enum dwarf_location_atom tls_op;
14321 enum dtprel_bool dtprel = dtprel_false;
14323 if (targetm.have_tls)
14325 /* If this is not defined, we have no way to emit the
14326 data. */
14327 if (!targetm.asm_out.output_dwarf_dtprel)
14328 return 0;
14330 /* The way DW_OP_GNU_push_tls_address is specified, we
14331 can only look up addresses of objects in the current
14332 module. We used DW_OP_addr as first op, but that's
14333 wrong, because DW_OP_addr is relocated by the debug
14334 info consumer, while DW_OP_GNU_push_tls_address
14335 operand shouldn't be. */
14336 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14337 return 0;
14338 dtprel = dtprel_true;
14339 tls_op = DW_OP_GNU_push_tls_address;
14341 else
14343 if (!targetm.emutls.debug_form_tls_address
14344 || !(dwarf_version >= 3 || !dwarf_strict))
14345 return 0;
14346 /* We stuffed the control variable into the DECL_VALUE_EXPR
14347 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14348 no longer appear in gimple code. We used the control
14349 variable in specific so that we could pick it up here. */
14350 loc = DECL_VALUE_EXPR (loc);
14351 tls_op = DW_OP_form_tls_address;
14354 rtl = rtl_for_decl_location (loc);
14355 if (rtl == NULL_RTX)
14356 return 0;
14358 if (!MEM_P (rtl))
14359 return 0;
14360 rtl = XEXP (rtl, 0);
14361 if (! CONSTANT_P (rtl))
14362 return 0;
14364 ret = new_addr_loc_descr (rtl, dtprel);
14365 ret1 = new_loc_descr (tls_op, 0, 0);
14366 add_loc_descr (&ret, ret1);
14368 have_address = 1;
14369 break;
14371 /* FALLTHRU */
14373 case PARM_DECL:
14374 case RESULT_DECL:
14375 if (DECL_HAS_VALUE_EXPR_P (loc))
14376 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14377 want_address);
14378 /* FALLTHRU */
14380 case FUNCTION_DECL:
14382 rtx rtl;
14383 var_loc_list *loc_list = lookup_decl_loc (loc);
14385 if (loc_list && loc_list->first)
14387 list_ret = dw_loc_list (loc_list, loc, want_address);
14388 have_address = want_address != 0;
14389 break;
14391 rtl = rtl_for_decl_location (loc);
14392 if (rtl == NULL_RTX)
14394 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14395 return 0;
14397 else if (CONST_INT_P (rtl))
14399 HOST_WIDE_INT val = INTVAL (rtl);
14400 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14401 val &= GET_MODE_MASK (DECL_MODE (loc));
14402 ret = int_loc_descriptor (val);
14404 else if (GET_CODE (rtl) == CONST_STRING)
14406 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14407 return 0;
14409 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14410 ret = new_addr_loc_descr (rtl, dtprel_false);
14411 else
14413 enum machine_mode mode, mem_mode;
14415 /* Certain constructs can only be represented at top-level. */
14416 if (want_address == 2)
14418 ret = loc_descriptor (rtl, VOIDmode,
14419 VAR_INIT_STATUS_INITIALIZED);
14420 have_address = 1;
14422 else
14424 mode = GET_MODE (rtl);
14425 mem_mode = VOIDmode;
14426 if (MEM_P (rtl))
14428 mem_mode = mode;
14429 mode = get_address_mode (rtl);
14430 rtl = XEXP (rtl, 0);
14431 have_address = 1;
14433 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14434 VAR_INIT_STATUS_INITIALIZED);
14436 if (!ret)
14437 expansion_failed (loc, rtl,
14438 "failed to produce loc descriptor for rtl");
14441 break;
14443 case MEM_REF:
14444 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14446 have_address = 1;
14447 goto do_plus;
14449 /* Fallthru. */
14450 case INDIRECT_REF:
14451 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14452 have_address = 1;
14453 break;
14455 case TARGET_MEM_REF:
14456 case SSA_NAME:
14457 return NULL;
14459 case COMPOUND_EXPR:
14460 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14462 CASE_CONVERT:
14463 case VIEW_CONVERT_EXPR:
14464 case SAVE_EXPR:
14465 case MODIFY_EXPR:
14466 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14468 case COMPONENT_REF:
14469 case BIT_FIELD_REF:
14470 case ARRAY_REF:
14471 case ARRAY_RANGE_REF:
14472 case REALPART_EXPR:
14473 case IMAGPART_EXPR:
14475 tree obj, offset;
14476 HOST_WIDE_INT bitsize, bitpos, bytepos;
14477 enum machine_mode mode;
14478 int unsignedp, volatilep = 0;
14480 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14481 &unsignedp, &volatilep, false);
14483 gcc_assert (obj != loc);
14485 list_ret = loc_list_from_tree (obj,
14486 want_address == 2
14487 && !bitpos && !offset ? 2 : 1);
14488 /* TODO: We can extract value of the small expression via shifting even
14489 for nonzero bitpos. */
14490 if (list_ret == 0)
14491 return 0;
14492 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14494 expansion_failed (loc, NULL_RTX,
14495 "bitfield access");
14496 return 0;
14499 if (offset != NULL_TREE)
14501 /* Variable offset. */
14502 list_ret1 = loc_list_from_tree (offset, 0);
14503 if (list_ret1 == 0)
14504 return 0;
14505 add_loc_list (&list_ret, list_ret1);
14506 if (!list_ret)
14507 return 0;
14508 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14511 bytepos = bitpos / BITS_PER_UNIT;
14512 if (bytepos > 0)
14513 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14514 else if (bytepos < 0)
14515 loc_list_plus_const (list_ret, bytepos);
14517 have_address = 1;
14518 break;
14521 case INTEGER_CST:
14522 if ((want_address || !tree_fits_shwi_p (loc))
14523 && (ret = cst_pool_loc_descr (loc)))
14524 have_address = 1;
14525 else if (want_address == 2
14526 && tree_fits_shwi_p (loc)
14527 && (ret = address_of_int_loc_descriptor
14528 (int_size_in_bytes (TREE_TYPE (loc)),
14529 tree_to_shwi (loc))))
14530 have_address = 1;
14531 else if (tree_fits_shwi_p (loc))
14532 ret = int_loc_descriptor (tree_to_shwi (loc));
14533 else
14535 expansion_failed (loc, NULL_RTX,
14536 "Integer operand is not host integer");
14537 return 0;
14539 break;
14541 case CONSTRUCTOR:
14542 case REAL_CST:
14543 case STRING_CST:
14544 case COMPLEX_CST:
14545 if ((ret = cst_pool_loc_descr (loc)))
14546 have_address = 1;
14547 else
14548 /* We can construct small constants here using int_loc_descriptor. */
14549 expansion_failed (loc, NULL_RTX,
14550 "constructor or constant not in constant pool");
14551 break;
14553 case TRUTH_AND_EXPR:
14554 case TRUTH_ANDIF_EXPR:
14555 case BIT_AND_EXPR:
14556 op = DW_OP_and;
14557 goto do_binop;
14559 case TRUTH_XOR_EXPR:
14560 case BIT_XOR_EXPR:
14561 op = DW_OP_xor;
14562 goto do_binop;
14564 case TRUTH_OR_EXPR:
14565 case TRUTH_ORIF_EXPR:
14566 case BIT_IOR_EXPR:
14567 op = DW_OP_or;
14568 goto do_binop;
14570 case FLOOR_DIV_EXPR:
14571 case CEIL_DIV_EXPR:
14572 case ROUND_DIV_EXPR:
14573 case TRUNC_DIV_EXPR:
14574 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14575 return 0;
14576 op = DW_OP_div;
14577 goto do_binop;
14579 case MINUS_EXPR:
14580 op = DW_OP_minus;
14581 goto do_binop;
14583 case FLOOR_MOD_EXPR:
14584 case CEIL_MOD_EXPR:
14585 case ROUND_MOD_EXPR:
14586 case TRUNC_MOD_EXPR:
14587 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14589 op = DW_OP_mod;
14590 goto do_binop;
14592 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14593 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14594 if (list_ret == 0 || list_ret1 == 0)
14595 return 0;
14597 add_loc_list (&list_ret, list_ret1);
14598 if (list_ret == 0)
14599 return 0;
14600 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14601 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14602 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14603 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14604 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14605 break;
14607 case MULT_EXPR:
14608 op = DW_OP_mul;
14609 goto do_binop;
14611 case LSHIFT_EXPR:
14612 op = DW_OP_shl;
14613 goto do_binop;
14615 case RSHIFT_EXPR:
14616 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14617 goto do_binop;
14619 case POINTER_PLUS_EXPR:
14620 case PLUS_EXPR:
14621 do_plus:
14622 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14624 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14625 if (list_ret == 0)
14626 return 0;
14628 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14629 break;
14632 op = DW_OP_plus;
14633 goto do_binop;
14635 case LE_EXPR:
14636 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14637 return 0;
14639 op = DW_OP_le;
14640 goto do_binop;
14642 case GE_EXPR:
14643 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14644 return 0;
14646 op = DW_OP_ge;
14647 goto do_binop;
14649 case LT_EXPR:
14650 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14651 return 0;
14653 op = DW_OP_lt;
14654 goto do_binop;
14656 case GT_EXPR:
14657 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14658 return 0;
14660 op = DW_OP_gt;
14661 goto do_binop;
14663 case EQ_EXPR:
14664 op = DW_OP_eq;
14665 goto do_binop;
14667 case NE_EXPR:
14668 op = DW_OP_ne;
14669 goto do_binop;
14671 do_binop:
14672 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14673 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14674 if (list_ret == 0 || list_ret1 == 0)
14675 return 0;
14677 add_loc_list (&list_ret, list_ret1);
14678 if (list_ret == 0)
14679 return 0;
14680 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14681 break;
14683 case TRUTH_NOT_EXPR:
14684 case BIT_NOT_EXPR:
14685 op = DW_OP_not;
14686 goto do_unop;
14688 case ABS_EXPR:
14689 op = DW_OP_abs;
14690 goto do_unop;
14692 case NEGATE_EXPR:
14693 op = DW_OP_neg;
14694 goto do_unop;
14696 do_unop:
14697 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14698 if (list_ret == 0)
14699 return 0;
14701 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14702 break;
14704 case MIN_EXPR:
14705 case MAX_EXPR:
14707 const enum tree_code code =
14708 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14710 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14711 build2 (code, integer_type_node,
14712 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14713 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14716 /* ... fall through ... */
14718 case COND_EXPR:
14720 dw_loc_descr_ref lhs
14721 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14722 dw_loc_list_ref rhs
14723 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14724 dw_loc_descr_ref bra_node, jump_node, tmp;
14726 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14727 if (list_ret == 0 || lhs == 0 || rhs == 0)
14728 return 0;
14730 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14731 add_loc_descr_to_each (list_ret, bra_node);
14733 add_loc_list (&list_ret, rhs);
14734 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14735 add_loc_descr_to_each (list_ret, jump_node);
14737 add_loc_descr_to_each (list_ret, lhs);
14738 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14739 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14741 /* ??? Need a node to point the skip at. Use a nop. */
14742 tmp = new_loc_descr (DW_OP_nop, 0, 0);
14743 add_loc_descr_to_each (list_ret, tmp);
14744 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14745 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14747 break;
14749 case FIX_TRUNC_EXPR:
14750 return 0;
14752 default:
14753 /* Leave front-end specific codes as simply unknown. This comes
14754 up, for instance, with the C STMT_EXPR. */
14755 if ((unsigned int) TREE_CODE (loc)
14756 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14758 expansion_failed (loc, NULL_RTX,
14759 "language specific tree node");
14760 return 0;
14763 #ifdef ENABLE_CHECKING
14764 /* Otherwise this is a generic code; we should just lists all of
14765 these explicitly. We forgot one. */
14766 gcc_unreachable ();
14767 #else
14768 /* In a release build, we want to degrade gracefully: better to
14769 generate incomplete debugging information than to crash. */
14770 return NULL;
14771 #endif
14774 if (!ret && !list_ret)
14775 return 0;
14777 if (want_address == 2 && !have_address
14778 && (dwarf_version >= 4 || !dwarf_strict))
14780 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14782 expansion_failed (loc, NULL_RTX,
14783 "DWARF address size mismatch");
14784 return 0;
14786 if (ret)
14787 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14788 else
14789 add_loc_descr_to_each (list_ret,
14790 new_loc_descr (DW_OP_stack_value, 0, 0));
14791 have_address = 1;
14793 /* Show if we can't fill the request for an address. */
14794 if (want_address && !have_address)
14796 expansion_failed (loc, NULL_RTX,
14797 "Want address and only have value");
14798 return 0;
14801 gcc_assert (!ret || !list_ret);
14803 /* If we've got an address and don't want one, dereference. */
14804 if (!want_address && have_address)
14806 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14808 if (size > DWARF2_ADDR_SIZE || size == -1)
14810 expansion_failed (loc, NULL_RTX,
14811 "DWARF address size mismatch");
14812 return 0;
14814 else if (size == DWARF2_ADDR_SIZE)
14815 op = DW_OP_deref;
14816 else
14817 op = DW_OP_deref_size;
14819 if (ret)
14820 add_loc_descr (&ret, new_loc_descr (op, size, 0));
14821 else
14822 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14824 if (ret)
14825 list_ret = new_loc_list (ret, NULL, NULL, NULL);
14827 return list_ret;
14830 /* Same as above but return only single location expression. */
14831 static dw_loc_descr_ref
14832 loc_descriptor_from_tree (tree loc, int want_address)
14834 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14835 if (!ret)
14836 return NULL;
14837 if (ret->dw_loc_next)
14839 expansion_failed (loc, NULL_RTX,
14840 "Location list where only loc descriptor needed");
14841 return NULL;
14843 return ret->expr;
14846 /* Given a value, round it up to the lowest multiple of `boundary'
14847 which is not less than the value itself. */
14849 static inline HOST_WIDE_INT
14850 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14852 return (((value + boundary - 1) / boundary) * boundary);
14855 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14856 pointer to the declared type for the relevant field variable, or return
14857 `integer_type_node' if the given node turns out to be an
14858 ERROR_MARK node. */
14860 static inline tree
14861 field_type (const_tree decl)
14863 tree type;
14865 if (TREE_CODE (decl) == ERROR_MARK)
14866 return integer_type_node;
14868 type = DECL_BIT_FIELD_TYPE (decl);
14869 if (type == NULL_TREE)
14870 type = TREE_TYPE (decl);
14872 return type;
14875 /* Given a pointer to a tree node, return the alignment in bits for
14876 it, or else return BITS_PER_WORD if the node actually turns out to
14877 be an ERROR_MARK node. */
14879 static inline unsigned
14880 simple_type_align_in_bits (const_tree type)
14882 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14885 static inline unsigned
14886 simple_decl_align_in_bits (const_tree decl)
14888 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14891 /* Return the result of rounding T up to ALIGN. */
14893 static inline offset_int
14894 round_up_to_align (const offset_int &t, unsigned int align)
14896 return wi::udiv_trunc (t + align - 1, align) * align;
14899 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14900 lowest addressed byte of the "containing object" for the given FIELD_DECL,
14901 or return 0 if we are unable to determine what that offset is, either
14902 because the argument turns out to be a pointer to an ERROR_MARK node, or
14903 because the offset is actually variable. (We can't handle the latter case
14904 just yet). */
14906 static HOST_WIDE_INT
14907 field_byte_offset (const_tree decl)
14909 offset_int object_offset_in_bits;
14910 offset_int object_offset_in_bytes;
14911 offset_int bitpos_int;
14913 if (TREE_CODE (decl) == ERROR_MARK)
14914 return 0;
14916 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14918 /* We cannot yet cope with fields whose positions are variable, so
14919 for now, when we see such things, we simply return 0. Someday, we may
14920 be able to handle such cases, but it will be damn difficult. */
14921 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14922 return 0;
14924 bitpos_int = wi::to_offset (bit_position (decl));
14926 #ifdef PCC_BITFIELD_TYPE_MATTERS
14927 if (PCC_BITFIELD_TYPE_MATTERS)
14929 tree type;
14930 tree field_size_tree;
14931 offset_int deepest_bitpos;
14932 offset_int field_size_in_bits;
14933 unsigned int type_align_in_bits;
14934 unsigned int decl_align_in_bits;
14935 offset_int type_size_in_bits;
14937 type = field_type (decl);
14938 type_size_in_bits = offset_int_type_size_in_bits (type);
14939 type_align_in_bits = simple_type_align_in_bits (type);
14941 field_size_tree = DECL_SIZE (decl);
14943 /* The size could be unspecified if there was an error, or for
14944 a flexible array member. */
14945 if (!field_size_tree)
14946 field_size_tree = bitsize_zero_node;
14948 /* If the size of the field is not constant, use the type size. */
14949 if (TREE_CODE (field_size_tree) == INTEGER_CST)
14950 field_size_in_bits = wi::to_offset (field_size_tree);
14951 else
14952 field_size_in_bits = type_size_in_bits;
14954 decl_align_in_bits = simple_decl_align_in_bits (decl);
14956 /* The GCC front-end doesn't make any attempt to keep track of the
14957 starting bit offset (relative to the start of the containing
14958 structure type) of the hypothetical "containing object" for a
14959 bit-field. Thus, when computing the byte offset value for the
14960 start of the "containing object" of a bit-field, we must deduce
14961 this information on our own. This can be rather tricky to do in
14962 some cases. For example, handling the following structure type
14963 definition when compiling for an i386/i486 target (which only
14964 aligns long long's to 32-bit boundaries) can be very tricky:
14966 struct S { int field1; long long field2:31; };
14968 Fortunately, there is a simple rule-of-thumb which can be used
14969 in such cases. When compiling for an i386/i486, GCC will
14970 allocate 8 bytes for the structure shown above. It decides to
14971 do this based upon one simple rule for bit-field allocation.
14972 GCC allocates each "containing object" for each bit-field at
14973 the first (i.e. lowest addressed) legitimate alignment boundary
14974 (based upon the required minimum alignment for the declared
14975 type of the field) which it can possibly use, subject to the
14976 condition that there is still enough available space remaining
14977 in the containing object (when allocated at the selected point)
14978 to fully accommodate all of the bits of the bit-field itself.
14980 This simple rule makes it obvious why GCC allocates 8 bytes for
14981 each object of the structure type shown above. When looking
14982 for a place to allocate the "containing object" for `field2',
14983 the compiler simply tries to allocate a 64-bit "containing
14984 object" at each successive 32-bit boundary (starting at zero)
14985 until it finds a place to allocate that 64- bit field such that
14986 at least 31 contiguous (and previously unallocated) bits remain
14987 within that selected 64 bit field. (As it turns out, for the
14988 example above, the compiler finds it is OK to allocate the
14989 "containing object" 64-bit field at bit-offset zero within the
14990 structure type.)
14992 Here we attempt to work backwards from the limited set of facts
14993 we're given, and we try to deduce from those facts, where GCC
14994 must have believed that the containing object started (within
14995 the structure type). The value we deduce is then used (by the
14996 callers of this routine) to generate DW_AT_location and
14997 DW_AT_bit_offset attributes for fields (both bit-fields and, in
14998 the case of DW_AT_location, regular fields as well). */
15000 /* Figure out the bit-distance from the start of the structure to
15001 the "deepest" bit of the bit-field. */
15002 deepest_bitpos = bitpos_int + field_size_in_bits;
15004 /* This is the tricky part. Use some fancy footwork to deduce
15005 where the lowest addressed bit of the containing object must
15006 be. */
15007 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15009 /* Round up to type_align by default. This works best for
15010 bitfields. */
15011 object_offset_in_bits
15012 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15014 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15016 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15018 /* Round up to decl_align instead. */
15019 object_offset_in_bits
15020 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15023 else
15024 #endif /* PCC_BITFIELD_TYPE_MATTERS */
15025 object_offset_in_bits = bitpos_int;
15027 object_offset_in_bytes
15028 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15029 return object_offset_in_bytes.to_shwi ();
15032 /* The following routines define various Dwarf attributes and any data
15033 associated with them. */
15035 /* Add a location description attribute value to a DIE.
15037 This emits location attributes suitable for whole variables and
15038 whole parameters. Note that the location attributes for struct fields are
15039 generated by the routine `data_member_location_attribute' below. */
15041 static inline void
15042 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15043 dw_loc_list_ref descr)
15045 if (descr == 0)
15046 return;
15047 if (single_element_loc_list_p (descr))
15048 add_AT_loc (die, attr_kind, descr->expr);
15049 else
15050 add_AT_loc_list (die, attr_kind, descr);
15053 /* Add DW_AT_accessibility attribute to DIE if needed. */
15055 static void
15056 add_accessibility_attribute (dw_die_ref die, tree decl)
15058 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15059 children, otherwise the default is DW_ACCESS_public. In DWARF2
15060 the default has always been DW_ACCESS_public. */
15061 if (TREE_PROTECTED (decl))
15062 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15063 else if (TREE_PRIVATE (decl))
15065 if (dwarf_version == 2
15066 || die->die_parent == NULL
15067 || die->die_parent->die_tag != DW_TAG_class_type)
15068 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15070 else if (dwarf_version > 2
15071 && die->die_parent
15072 && die->die_parent->die_tag == DW_TAG_class_type)
15073 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15076 /* Attach the specialized form of location attribute used for data members of
15077 struct and union types. In the special case of a FIELD_DECL node which
15078 represents a bit-field, the "offset" part of this special location
15079 descriptor must indicate the distance in bytes from the lowest-addressed
15080 byte of the containing struct or union type to the lowest-addressed byte of
15081 the "containing object" for the bit-field. (See the `field_byte_offset'
15082 function above).
15084 For any given bit-field, the "containing object" is a hypothetical object
15085 (of some integral or enum type) within which the given bit-field lives. The
15086 type of this hypothetical "containing object" is always the same as the
15087 declared type of the individual bit-field itself (for GCC anyway... the
15088 DWARF spec doesn't actually mandate this). Note that it is the size (in
15089 bytes) of the hypothetical "containing object" which will be given in the
15090 DW_AT_byte_size attribute for this bit-field. (See the
15091 `byte_size_attribute' function below.) It is also used when calculating the
15092 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15093 function below.) */
15095 static void
15096 add_data_member_location_attribute (dw_die_ref die, tree decl)
15098 HOST_WIDE_INT offset;
15099 dw_loc_descr_ref loc_descr = 0;
15101 if (TREE_CODE (decl) == TREE_BINFO)
15103 /* We're working on the TAG_inheritance for a base class. */
15104 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15106 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15107 aren't at a fixed offset from all (sub)objects of the same
15108 type. We need to extract the appropriate offset from our
15109 vtable. The following dwarf expression means
15111 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15113 This is specific to the V3 ABI, of course. */
15115 dw_loc_descr_ref tmp;
15117 /* Make a copy of the object address. */
15118 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15119 add_loc_descr (&loc_descr, tmp);
15121 /* Extract the vtable address. */
15122 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15123 add_loc_descr (&loc_descr, tmp);
15125 /* Calculate the address of the offset. */
15126 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15127 gcc_assert (offset < 0);
15129 tmp = int_loc_descriptor (-offset);
15130 add_loc_descr (&loc_descr, tmp);
15131 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15132 add_loc_descr (&loc_descr, tmp);
15134 /* Extract the offset. */
15135 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15136 add_loc_descr (&loc_descr, tmp);
15138 /* Add it to the object address. */
15139 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15140 add_loc_descr (&loc_descr, tmp);
15142 else
15143 offset = tree_to_shwi (BINFO_OFFSET (decl));
15145 else
15146 offset = field_byte_offset (decl);
15148 if (! loc_descr)
15150 if (dwarf_version > 2)
15152 /* Don't need to output a location expression, just the constant. */
15153 if (offset < 0)
15154 add_AT_int (die, DW_AT_data_member_location, offset);
15155 else
15156 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15157 return;
15159 else
15161 enum dwarf_location_atom op;
15163 /* The DWARF2 standard says that we should assume that the structure
15164 address is already on the stack, so we can specify a structure
15165 field address by using DW_OP_plus_uconst. */
15166 op = DW_OP_plus_uconst;
15167 loc_descr = new_loc_descr (op, offset, 0);
15171 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15174 /* Writes integer values to dw_vec_const array. */
15176 static void
15177 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15179 while (size != 0)
15181 *dest++ = val & 0xff;
15182 val >>= 8;
15183 --size;
15187 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15189 static HOST_WIDE_INT
15190 extract_int (const unsigned char *src, unsigned int size)
15192 HOST_WIDE_INT val = 0;
15194 src += size;
15195 while (size != 0)
15197 val <<= 8;
15198 val |= *--src & 0xff;
15199 --size;
15201 return val;
15204 /* Writes wide_int values to dw_vec_const array. */
15206 static void
15207 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15209 int i;
15211 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15213 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15214 return;
15217 /* We'd have to extend this code to support odd sizes. */
15218 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15220 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15222 if (WORDS_BIG_ENDIAN)
15223 for (i = n - 1; i >= 0; i--)
15225 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15226 dest += sizeof (HOST_WIDE_INT);
15228 else
15229 for (i = 0; i < n; i++)
15231 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15232 dest += sizeof (HOST_WIDE_INT);
15236 /* Writes floating point values to dw_vec_const array. */
15238 static void
15239 insert_float (const_rtx rtl, unsigned char *array)
15241 REAL_VALUE_TYPE rv;
15242 long val[4];
15243 int i;
15245 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15246 real_to_target (val, &rv, GET_MODE (rtl));
15248 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15249 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15251 insert_int (val[i], 4, array);
15252 array += 4;
15256 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15257 does not have a "location" either in memory or in a register. These
15258 things can arise in GNU C when a constant is passed as an actual parameter
15259 to an inlined function. They can also arise in C++ where declared
15260 constants do not necessarily get memory "homes". */
15262 static bool
15263 add_const_value_attribute (dw_die_ref die, rtx rtl)
15265 switch (GET_CODE (rtl))
15267 case CONST_INT:
15269 HOST_WIDE_INT val = INTVAL (rtl);
15271 if (val < 0)
15272 add_AT_int (die, DW_AT_const_value, val);
15273 else
15274 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15276 return true;
15278 case CONST_WIDE_INT:
15279 add_AT_wide (die, DW_AT_const_value,
15280 std::make_pair (rtl, GET_MODE (rtl)));
15281 return true;
15283 case CONST_DOUBLE:
15284 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15285 floating-point constant. A CONST_DOUBLE is used whenever the
15286 constant requires more than one word in order to be adequately
15287 represented. */
15289 enum machine_mode mode = GET_MODE (rtl);
15291 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15292 add_AT_double (die, DW_AT_const_value,
15293 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15294 else
15296 unsigned int length = GET_MODE_SIZE (mode);
15297 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15299 insert_float (rtl, array);
15300 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15303 return true;
15305 case CONST_VECTOR:
15307 enum machine_mode mode = GET_MODE (rtl);
15308 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15309 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15310 unsigned char *array
15311 = ggc_vec_alloc<unsigned char> (length * elt_size);
15312 unsigned int i;
15313 unsigned char *p;
15314 enum machine_mode imode = GET_MODE_INNER (mode);
15316 switch (GET_MODE_CLASS (mode))
15318 case MODE_VECTOR_INT:
15319 for (i = 0, p = array; i < length; i++, p += elt_size)
15321 rtx elt = CONST_VECTOR_ELT (rtl, i);
15322 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15324 break;
15326 case MODE_VECTOR_FLOAT:
15327 for (i = 0, p = array; i < length; i++, p += elt_size)
15329 rtx elt = CONST_VECTOR_ELT (rtl, i);
15330 insert_float (elt, p);
15332 break;
15334 default:
15335 gcc_unreachable ();
15338 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15340 return true;
15342 case CONST_STRING:
15343 if (dwarf_version >= 4 || !dwarf_strict)
15345 dw_loc_descr_ref loc_result;
15346 resolve_one_addr (&rtl);
15347 rtl_addr:
15348 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15349 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15350 add_AT_loc (die, DW_AT_location, loc_result);
15351 vec_safe_push (used_rtx_array, rtl);
15352 return true;
15354 return false;
15356 case CONST:
15357 if (CONSTANT_P (XEXP (rtl, 0)))
15358 return add_const_value_attribute (die, XEXP (rtl, 0));
15359 /* FALLTHROUGH */
15360 case SYMBOL_REF:
15361 if (!const_ok_for_output (rtl))
15362 return false;
15363 case LABEL_REF:
15364 if (dwarf_version >= 4 || !dwarf_strict)
15365 goto rtl_addr;
15366 return false;
15368 case PLUS:
15369 /* In cases where an inlined instance of an inline function is passed
15370 the address of an `auto' variable (which is local to the caller) we
15371 can get a situation where the DECL_RTL of the artificial local
15372 variable (for the inlining) which acts as a stand-in for the
15373 corresponding formal parameter (of the inline function) will look
15374 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15375 exactly a compile-time constant expression, but it isn't the address
15376 of the (artificial) local variable either. Rather, it represents the
15377 *value* which the artificial local variable always has during its
15378 lifetime. We currently have no way to represent such quasi-constant
15379 values in Dwarf, so for now we just punt and generate nothing. */
15380 return false;
15382 case HIGH:
15383 case CONST_FIXED:
15384 return false;
15386 case MEM:
15387 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15388 && MEM_READONLY_P (rtl)
15389 && GET_MODE (rtl) == BLKmode)
15391 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15392 return true;
15394 return false;
15396 default:
15397 /* No other kinds of rtx should be possible here. */
15398 gcc_unreachable ();
15400 return false;
15403 /* Determine whether the evaluation of EXPR references any variables
15404 or functions which aren't otherwise used (and therefore may not be
15405 output). */
15406 static tree
15407 reference_to_unused (tree * tp, int * walk_subtrees,
15408 void * data ATTRIBUTE_UNUSED)
15410 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15411 *walk_subtrees = 0;
15413 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15414 && ! TREE_ASM_WRITTEN (*tp))
15415 return *tp;
15416 /* ??? The C++ FE emits debug information for using decls, so
15417 putting gcc_unreachable here falls over. See PR31899. For now
15418 be conservative. */
15419 else if (!symtab->global_info_ready
15420 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15421 return *tp;
15422 else if (TREE_CODE (*tp) == VAR_DECL)
15424 varpool_node *node = varpool_node::get (*tp);
15425 if (!node || !node->definition)
15426 return *tp;
15428 else if (TREE_CODE (*tp) == FUNCTION_DECL
15429 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15431 /* The call graph machinery must have finished analyzing,
15432 optimizing and gimplifying the CU by now.
15433 So if *TP has no call graph node associated
15434 to it, it means *TP will not be emitted. */
15435 if (!cgraph_node::get (*tp))
15436 return *tp;
15438 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15439 return *tp;
15441 return NULL_TREE;
15444 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15445 for use in a later add_const_value_attribute call. */
15447 static rtx
15448 rtl_for_decl_init (tree init, tree type)
15450 rtx rtl = NULL_RTX;
15452 STRIP_NOPS (init);
15454 /* If a variable is initialized with a string constant without embedded
15455 zeros, build CONST_STRING. */
15456 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15458 tree enttype = TREE_TYPE (type);
15459 tree domain = TYPE_DOMAIN (type);
15460 enum machine_mode mode = TYPE_MODE (enttype);
15462 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15463 && domain
15464 && integer_zerop (TYPE_MIN_VALUE (domain))
15465 && compare_tree_int (TYPE_MAX_VALUE (domain),
15466 TREE_STRING_LENGTH (init) - 1) == 0
15467 && ((size_t) TREE_STRING_LENGTH (init)
15468 == strlen (TREE_STRING_POINTER (init)) + 1))
15470 rtl = gen_rtx_CONST_STRING (VOIDmode,
15471 ggc_strdup (TREE_STRING_POINTER (init)));
15472 rtl = gen_rtx_MEM (BLKmode, rtl);
15473 MEM_READONLY_P (rtl) = 1;
15476 /* Other aggregates, and complex values, could be represented using
15477 CONCAT: FIXME! */
15478 else if (AGGREGATE_TYPE_P (type)
15479 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15480 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15481 || TREE_CODE (type) == COMPLEX_TYPE)
15483 /* Vectors only work if their mode is supported by the target.
15484 FIXME: generic vectors ought to work too. */
15485 else if (TREE_CODE (type) == VECTOR_TYPE
15486 && !VECTOR_MODE_P (TYPE_MODE (type)))
15488 /* If the initializer is something that we know will expand into an
15489 immediate RTL constant, expand it now. We must be careful not to
15490 reference variables which won't be output. */
15491 else if (initializer_constant_valid_p (init, type)
15492 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15494 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15495 possible. */
15496 if (TREE_CODE (type) == VECTOR_TYPE)
15497 switch (TREE_CODE (init))
15499 case VECTOR_CST:
15500 break;
15501 case CONSTRUCTOR:
15502 if (TREE_CONSTANT (init))
15504 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15505 bool constant_p = true;
15506 tree value;
15507 unsigned HOST_WIDE_INT ix;
15509 /* Even when ctor is constant, it might contain non-*_CST
15510 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15511 belong into VECTOR_CST nodes. */
15512 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15513 if (!CONSTANT_CLASS_P (value))
15515 constant_p = false;
15516 break;
15519 if (constant_p)
15521 init = build_vector_from_ctor (type, elts);
15522 break;
15525 /* FALLTHRU */
15527 default:
15528 return NULL;
15531 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15533 /* If expand_expr returns a MEM, it wasn't immediate. */
15534 gcc_assert (!rtl || !MEM_P (rtl));
15537 return rtl;
15540 /* Generate RTL for the variable DECL to represent its location. */
15542 static rtx
15543 rtl_for_decl_location (tree decl)
15545 rtx rtl;
15547 /* Here we have to decide where we are going to say the parameter "lives"
15548 (as far as the debugger is concerned). We only have a couple of
15549 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15551 DECL_RTL normally indicates where the parameter lives during most of the
15552 activation of the function. If optimization is enabled however, this
15553 could be either NULL or else a pseudo-reg. Both of those cases indicate
15554 that the parameter doesn't really live anywhere (as far as the code
15555 generation parts of GCC are concerned) during most of the function's
15556 activation. That will happen (for example) if the parameter is never
15557 referenced within the function.
15559 We could just generate a location descriptor here for all non-NULL
15560 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15561 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15562 where DECL_RTL is NULL or is a pseudo-reg.
15564 Note however that we can only get away with using DECL_INCOMING_RTL as
15565 a backup substitute for DECL_RTL in certain limited cases. In cases
15566 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15567 we can be sure that the parameter was passed using the same type as it is
15568 declared to have within the function, and that its DECL_INCOMING_RTL
15569 points us to a place where a value of that type is passed.
15571 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15572 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15573 because in these cases DECL_INCOMING_RTL points us to a value of some
15574 type which is *different* from the type of the parameter itself. Thus,
15575 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15576 such cases, the debugger would end up (for example) trying to fetch a
15577 `float' from a place which actually contains the first part of a
15578 `double'. That would lead to really incorrect and confusing
15579 output at debug-time.
15581 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15582 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15583 are a couple of exceptions however. On little-endian machines we can
15584 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15585 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15586 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15587 when (on a little-endian machine) a non-prototyped function has a
15588 parameter declared to be of type `short' or `char'. In such cases,
15589 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15590 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15591 passed `int' value. If the debugger then uses that address to fetch
15592 a `short' or a `char' (on a little-endian machine) the result will be
15593 the correct data, so we allow for such exceptional cases below.
15595 Note that our goal here is to describe the place where the given formal
15596 parameter lives during most of the function's activation (i.e. between the
15597 end of the prologue and the start of the epilogue). We'll do that as best
15598 as we can. Note however that if the given formal parameter is modified
15599 sometime during the execution of the function, then a stack backtrace (at
15600 debug-time) will show the function as having been called with the *new*
15601 value rather than the value which was originally passed in. This happens
15602 rarely enough that it is not a major problem, but it *is* a problem, and
15603 I'd like to fix it.
15605 A future version of dwarf2out.c may generate two additional attributes for
15606 any given DW_TAG_formal_parameter DIE which will describe the "passed
15607 type" and the "passed location" for the given formal parameter in addition
15608 to the attributes we now generate to indicate the "declared type" and the
15609 "active location" for each parameter. This additional set of attributes
15610 could be used by debuggers for stack backtraces. Separately, note that
15611 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15612 This happens (for example) for inlined-instances of inline function formal
15613 parameters which are never referenced. This really shouldn't be
15614 happening. All PARM_DECL nodes should get valid non-NULL
15615 DECL_INCOMING_RTL values. FIXME. */
15617 /* Use DECL_RTL as the "location" unless we find something better. */
15618 rtl = DECL_RTL_IF_SET (decl);
15620 /* When generating abstract instances, ignore everything except
15621 constants, symbols living in memory, and symbols living in
15622 fixed registers. */
15623 if (! reload_completed)
15625 if (rtl
15626 && (CONSTANT_P (rtl)
15627 || (MEM_P (rtl)
15628 && CONSTANT_P (XEXP (rtl, 0)))
15629 || (REG_P (rtl)
15630 && TREE_CODE (decl) == VAR_DECL
15631 && TREE_STATIC (decl))))
15633 rtl = targetm.delegitimize_address (rtl);
15634 return rtl;
15636 rtl = NULL_RTX;
15638 else if (TREE_CODE (decl) == PARM_DECL)
15640 if (rtl == NULL_RTX
15641 || is_pseudo_reg (rtl)
15642 || (MEM_P (rtl)
15643 && is_pseudo_reg (XEXP (rtl, 0))
15644 && DECL_INCOMING_RTL (decl)
15645 && MEM_P (DECL_INCOMING_RTL (decl))
15646 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15648 tree declared_type = TREE_TYPE (decl);
15649 tree passed_type = DECL_ARG_TYPE (decl);
15650 enum machine_mode dmode = TYPE_MODE (declared_type);
15651 enum machine_mode pmode = TYPE_MODE (passed_type);
15653 /* This decl represents a formal parameter which was optimized out.
15654 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15655 all cases where (rtl == NULL_RTX) just below. */
15656 if (dmode == pmode)
15657 rtl = DECL_INCOMING_RTL (decl);
15658 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15659 && SCALAR_INT_MODE_P (dmode)
15660 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15661 && DECL_INCOMING_RTL (decl))
15663 rtx inc = DECL_INCOMING_RTL (decl);
15664 if (REG_P (inc))
15665 rtl = inc;
15666 else if (MEM_P (inc))
15668 if (BYTES_BIG_ENDIAN)
15669 rtl = adjust_address_nv (inc, dmode,
15670 GET_MODE_SIZE (pmode)
15671 - GET_MODE_SIZE (dmode));
15672 else
15673 rtl = inc;
15678 /* If the parm was passed in registers, but lives on the stack, then
15679 make a big endian correction if the mode of the type of the
15680 parameter is not the same as the mode of the rtl. */
15681 /* ??? This is the same series of checks that are made in dbxout.c before
15682 we reach the big endian correction code there. It isn't clear if all
15683 of these checks are necessary here, but keeping them all is the safe
15684 thing to do. */
15685 else if (MEM_P (rtl)
15686 && XEXP (rtl, 0) != const0_rtx
15687 && ! CONSTANT_P (XEXP (rtl, 0))
15688 /* Not passed in memory. */
15689 && !MEM_P (DECL_INCOMING_RTL (decl))
15690 /* Not passed by invisible reference. */
15691 && (!REG_P (XEXP (rtl, 0))
15692 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15693 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15694 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15695 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15696 #endif
15698 /* Big endian correction check. */
15699 && BYTES_BIG_ENDIAN
15700 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15701 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15702 < UNITS_PER_WORD))
15704 enum machine_mode addr_mode = get_address_mode (rtl);
15705 int offset = (UNITS_PER_WORD
15706 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15708 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15709 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15712 else if (TREE_CODE (decl) == VAR_DECL
15713 && rtl
15714 && MEM_P (rtl)
15715 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15716 && BYTES_BIG_ENDIAN)
15718 enum machine_mode addr_mode = get_address_mode (rtl);
15719 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15720 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15722 /* If a variable is declared "register" yet is smaller than
15723 a register, then if we store the variable to memory, it
15724 looks like we're storing a register-sized value, when in
15725 fact we are not. We need to adjust the offset of the
15726 storage location to reflect the actual value's bytes,
15727 else gdb will not be able to display it. */
15728 if (rsize > dsize)
15729 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15730 plus_constant (addr_mode, XEXP (rtl, 0),
15731 rsize - dsize));
15734 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15735 and will have been substituted directly into all expressions that use it.
15736 C does not have such a concept, but C++ and other languages do. */
15737 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15738 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15740 if (rtl)
15741 rtl = targetm.delegitimize_address (rtl);
15743 /* If we don't look past the constant pool, we risk emitting a
15744 reference to a constant pool entry that isn't referenced from
15745 code, and thus is not emitted. */
15746 if (rtl)
15747 rtl = avoid_constant_pool_reference (rtl);
15749 /* Try harder to get a rtl. If this symbol ends up not being emitted
15750 in the current CU, resolve_addr will remove the expression referencing
15751 it. */
15752 if (rtl == NULL_RTX
15753 && TREE_CODE (decl) == VAR_DECL
15754 && !DECL_EXTERNAL (decl)
15755 && TREE_STATIC (decl)
15756 && DECL_NAME (decl)
15757 && !DECL_HARD_REGISTER (decl)
15758 && DECL_MODE (decl) != VOIDmode)
15760 rtl = make_decl_rtl_for_debug (decl);
15761 if (!MEM_P (rtl)
15762 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15763 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15764 rtl = NULL_RTX;
15767 return rtl;
15770 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
15771 returned. If so, the decl for the COMMON block is returned, and the
15772 value is the offset into the common block for the symbol. */
15774 static tree
15775 fortran_common (tree decl, HOST_WIDE_INT *value)
15777 tree val_expr, cvar;
15778 enum machine_mode mode;
15779 HOST_WIDE_INT bitsize, bitpos;
15780 tree offset;
15781 int unsignedp, volatilep = 0;
15783 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15784 it does not have a value (the offset into the common area), or if it
15785 is thread local (as opposed to global) then it isn't common, and shouldn't
15786 be handled as such. */
15787 if (TREE_CODE (decl) != VAR_DECL
15788 || !TREE_STATIC (decl)
15789 || !DECL_HAS_VALUE_EXPR_P (decl)
15790 || !is_fortran ())
15791 return NULL_TREE;
15793 val_expr = DECL_VALUE_EXPR (decl);
15794 if (TREE_CODE (val_expr) != COMPONENT_REF)
15795 return NULL_TREE;
15797 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15798 &mode, &unsignedp, &volatilep, true);
15800 if (cvar == NULL_TREE
15801 || TREE_CODE (cvar) != VAR_DECL
15802 || DECL_ARTIFICIAL (cvar)
15803 || !TREE_PUBLIC (cvar))
15804 return NULL_TREE;
15806 *value = 0;
15807 if (offset != NULL)
15809 if (!tree_fits_shwi_p (offset))
15810 return NULL_TREE;
15811 *value = tree_to_shwi (offset);
15813 if (bitpos != 0)
15814 *value += bitpos / BITS_PER_UNIT;
15816 return cvar;
15819 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15820 data attribute for a variable or a parameter. We generate the
15821 DW_AT_const_value attribute only in those cases where the given variable
15822 or parameter does not have a true "location" either in memory or in a
15823 register. This can happen (for example) when a constant is passed as an
15824 actual argument in a call to an inline function. (It's possible that
15825 these things can crop up in other ways also.) Note that one type of
15826 constant value which can be passed into an inlined function is a constant
15827 pointer. This can happen for example if an actual argument in an inlined
15828 function call evaluates to a compile-time constant address.
15830 CACHE_P is true if it is worth caching the location list for DECL,
15831 so that future calls can reuse it rather than regenerate it from scratch.
15832 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15833 since we will need to refer to them each time the function is inlined. */
15835 static bool
15836 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15837 enum dwarf_attribute attr)
15839 rtx rtl;
15840 dw_loc_list_ref list;
15841 var_loc_list *loc_list;
15842 cached_dw_loc_list *cache;
15844 if (TREE_CODE (decl) == ERROR_MARK)
15845 return false;
15847 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15848 || TREE_CODE (decl) == RESULT_DECL);
15850 /* Try to get some constant RTL for this decl, and use that as the value of
15851 the location. */
15853 rtl = rtl_for_decl_location (decl);
15854 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15855 && add_const_value_attribute (die, rtl))
15856 return true;
15858 /* See if we have single element location list that is equivalent to
15859 a constant value. That way we are better to use add_const_value_attribute
15860 rather than expanding constant value equivalent. */
15861 loc_list = lookup_decl_loc (decl);
15862 if (loc_list
15863 && loc_list->first
15864 && loc_list->first->next == NULL
15865 && NOTE_P (loc_list->first->loc)
15866 && NOTE_VAR_LOCATION (loc_list->first->loc)
15867 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15869 struct var_loc_node *node;
15871 node = loc_list->first;
15872 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15873 if (GET_CODE (rtl) == EXPR_LIST)
15874 rtl = XEXP (rtl, 0);
15875 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15876 && add_const_value_attribute (die, rtl))
15877 return true;
15879 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15880 list several times. See if we've already cached the contents. */
15881 list = NULL;
15882 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15883 cache_p = false;
15884 if (cache_p)
15886 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
15887 if (cache)
15888 list = cache->loc_list;
15890 if (list == NULL)
15892 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15893 /* It is usually worth caching this result if the decl is from
15894 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
15895 if (cache_p && list && list->dw_loc_next)
15897 cached_dw_loc_list **slot
15898 = cached_dw_loc_list_table->find_slot_with_hash (decl,
15899 DECL_UID (decl),
15900 INSERT);
15901 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
15902 cache->decl_id = DECL_UID (decl);
15903 cache->loc_list = list;
15904 *slot = cache;
15907 if (list)
15909 add_AT_location_description (die, attr, list);
15910 return true;
15912 /* None of that worked, so it must not really have a location;
15913 try adding a constant value attribute from the DECL_INITIAL. */
15914 return tree_add_const_value_attribute_for_decl (die, decl);
15917 /* Add VARIABLE and DIE into deferred locations list. */
15919 static void
15920 defer_location (tree variable, dw_die_ref die)
15922 deferred_locations entry;
15923 entry.variable = variable;
15924 entry.die = die;
15925 vec_safe_push (deferred_locations_list, entry);
15928 /* Helper function for tree_add_const_value_attribute. Natively encode
15929 initializer INIT into an array. Return true if successful. */
15931 static bool
15932 native_encode_initializer (tree init, unsigned char *array, int size)
15934 tree type;
15936 if (init == NULL_TREE)
15937 return false;
15939 STRIP_NOPS (init);
15940 switch (TREE_CODE (init))
15942 case STRING_CST:
15943 type = TREE_TYPE (init);
15944 if (TREE_CODE (type) == ARRAY_TYPE)
15946 tree enttype = TREE_TYPE (type);
15947 enum machine_mode mode = TYPE_MODE (enttype);
15949 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15950 return false;
15951 if (int_size_in_bytes (type) != size)
15952 return false;
15953 if (size > TREE_STRING_LENGTH (init))
15955 memcpy (array, TREE_STRING_POINTER (init),
15956 TREE_STRING_LENGTH (init));
15957 memset (array + TREE_STRING_LENGTH (init),
15958 '\0', size - TREE_STRING_LENGTH (init));
15960 else
15961 memcpy (array, TREE_STRING_POINTER (init), size);
15962 return true;
15964 return false;
15965 case CONSTRUCTOR:
15966 type = TREE_TYPE (init);
15967 if (int_size_in_bytes (type) != size)
15968 return false;
15969 if (TREE_CODE (type) == ARRAY_TYPE)
15971 HOST_WIDE_INT min_index;
15972 unsigned HOST_WIDE_INT cnt;
15973 int curpos = 0, fieldsize;
15974 constructor_elt *ce;
15976 if (TYPE_DOMAIN (type) == NULL_TREE
15977 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
15978 return false;
15980 fieldsize = int_size_in_bytes (TREE_TYPE (type));
15981 if (fieldsize <= 0)
15982 return false;
15984 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
15985 memset (array, '\0', size);
15986 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15988 tree val = ce->value;
15989 tree index = ce->index;
15990 int pos = curpos;
15991 if (index && TREE_CODE (index) == RANGE_EXPR)
15992 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
15993 * fieldsize;
15994 else if (index)
15995 pos = (tree_to_shwi (index) - min_index) * fieldsize;
15997 if (val)
15999 STRIP_NOPS (val);
16000 if (!native_encode_initializer (val, array + pos, fieldsize))
16001 return false;
16003 curpos = pos + fieldsize;
16004 if (index && TREE_CODE (index) == RANGE_EXPR)
16006 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16007 - tree_to_shwi (TREE_OPERAND (index, 0));
16008 while (count-- > 0)
16010 if (val)
16011 memcpy (array + curpos, array + pos, fieldsize);
16012 curpos += fieldsize;
16015 gcc_assert (curpos <= size);
16017 return true;
16019 else if (TREE_CODE (type) == RECORD_TYPE
16020 || TREE_CODE (type) == UNION_TYPE)
16022 tree field = NULL_TREE;
16023 unsigned HOST_WIDE_INT cnt;
16024 constructor_elt *ce;
16026 if (int_size_in_bytes (type) != size)
16027 return false;
16029 if (TREE_CODE (type) == RECORD_TYPE)
16030 field = TYPE_FIELDS (type);
16032 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16034 tree val = ce->value;
16035 int pos, fieldsize;
16037 if (ce->index != 0)
16038 field = ce->index;
16040 if (val)
16041 STRIP_NOPS (val);
16043 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16044 return false;
16046 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16047 && TYPE_DOMAIN (TREE_TYPE (field))
16048 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16049 return false;
16050 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16051 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16052 return false;
16053 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16054 pos = int_byte_position (field);
16055 gcc_assert (pos + fieldsize <= size);
16056 if (val
16057 && !native_encode_initializer (val, array + pos, fieldsize))
16058 return false;
16060 return true;
16062 return false;
16063 case VIEW_CONVERT_EXPR:
16064 case NON_LVALUE_EXPR:
16065 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16066 default:
16067 return native_encode_expr (init, array, size) == size;
16071 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16072 attribute is the const value T. */
16074 static bool
16075 tree_add_const_value_attribute (dw_die_ref die, tree t)
16077 tree init;
16078 tree type = TREE_TYPE (t);
16079 rtx rtl;
16081 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16082 return false;
16084 init = t;
16085 gcc_assert (!DECL_P (init));
16087 rtl = rtl_for_decl_init (init, type);
16088 if (rtl)
16089 return add_const_value_attribute (die, rtl);
16090 /* If the host and target are sane, try harder. */
16091 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16092 && initializer_constant_valid_p (init, type))
16094 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16095 if (size > 0 && (int) size == size)
16097 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16099 if (native_encode_initializer (init, array, size))
16101 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16102 return true;
16104 ggc_free (array);
16107 return false;
16110 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16111 attribute is the const value of T, where T is an integral constant
16112 variable with static storage duration
16113 (so it can't be a PARM_DECL or a RESULT_DECL). */
16115 static bool
16116 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16119 if (!decl
16120 || (TREE_CODE (decl) != VAR_DECL
16121 && TREE_CODE (decl) != CONST_DECL)
16122 || (TREE_CODE (decl) == VAR_DECL
16123 && !TREE_STATIC (decl)))
16124 return false;
16126 if (TREE_READONLY (decl)
16127 && ! TREE_THIS_VOLATILE (decl)
16128 && DECL_INITIAL (decl))
16129 /* OK */;
16130 else
16131 return false;
16133 /* Don't add DW_AT_const_value if abstract origin already has one. */
16134 if (get_AT (var_die, DW_AT_const_value))
16135 return false;
16137 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16140 /* Convert the CFI instructions for the current function into a
16141 location list. This is used for DW_AT_frame_base when we targeting
16142 a dwarf2 consumer that does not support the dwarf3
16143 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16144 expressions. */
16146 static dw_loc_list_ref
16147 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16149 int ix;
16150 dw_fde_ref fde;
16151 dw_loc_list_ref list, *list_tail;
16152 dw_cfi_ref cfi;
16153 dw_cfa_location last_cfa, next_cfa;
16154 const char *start_label, *last_label, *section;
16155 dw_cfa_location remember;
16157 fde = cfun->fde;
16158 gcc_assert (fde != NULL);
16160 section = secname_for_decl (current_function_decl);
16161 list_tail = &list;
16162 list = NULL;
16164 memset (&next_cfa, 0, sizeof (next_cfa));
16165 next_cfa.reg = INVALID_REGNUM;
16166 remember = next_cfa;
16168 start_label = fde->dw_fde_begin;
16170 /* ??? Bald assumption that the CIE opcode list does not contain
16171 advance opcodes. */
16172 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16173 lookup_cfa_1 (cfi, &next_cfa, &remember);
16175 last_cfa = next_cfa;
16176 last_label = start_label;
16178 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16180 /* If the first partition contained no CFI adjustments, the
16181 CIE opcodes apply to the whole first partition. */
16182 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16183 fde->dw_fde_begin, fde->dw_fde_end, section);
16184 list_tail =&(*list_tail)->dw_loc_next;
16185 start_label = last_label = fde->dw_fde_second_begin;
16188 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16190 switch (cfi->dw_cfi_opc)
16192 case DW_CFA_set_loc:
16193 case DW_CFA_advance_loc1:
16194 case DW_CFA_advance_loc2:
16195 case DW_CFA_advance_loc4:
16196 if (!cfa_equal_p (&last_cfa, &next_cfa))
16198 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16199 start_label, last_label, section);
16201 list_tail = &(*list_tail)->dw_loc_next;
16202 last_cfa = next_cfa;
16203 start_label = last_label;
16205 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16206 break;
16208 case DW_CFA_advance_loc:
16209 /* The encoding is complex enough that we should never emit this. */
16210 gcc_unreachable ();
16212 default:
16213 lookup_cfa_1 (cfi, &next_cfa, &remember);
16214 break;
16216 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16218 if (!cfa_equal_p (&last_cfa, &next_cfa))
16220 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16221 start_label, last_label, section);
16223 list_tail = &(*list_tail)->dw_loc_next;
16224 last_cfa = next_cfa;
16225 start_label = last_label;
16227 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16228 start_label, fde->dw_fde_end, section);
16229 list_tail = &(*list_tail)->dw_loc_next;
16230 start_label = last_label = fde->dw_fde_second_begin;
16234 if (!cfa_equal_p (&last_cfa, &next_cfa))
16236 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16237 start_label, last_label, section);
16238 list_tail = &(*list_tail)->dw_loc_next;
16239 start_label = last_label;
16242 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16243 start_label,
16244 fde->dw_fde_second_begin
16245 ? fde->dw_fde_second_end : fde->dw_fde_end,
16246 section);
16248 if (list && list->dw_loc_next)
16249 gen_llsym (list);
16251 return list;
16254 /* Compute a displacement from the "steady-state frame pointer" to the
16255 frame base (often the same as the CFA), and store it in
16256 frame_pointer_fb_offset. OFFSET is added to the displacement
16257 before the latter is negated. */
16259 static void
16260 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16262 rtx reg, elim;
16264 #ifdef FRAME_POINTER_CFA_OFFSET
16265 reg = frame_pointer_rtx;
16266 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16267 #else
16268 reg = arg_pointer_rtx;
16269 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16270 #endif
16272 elim = (ira_use_lra_p
16273 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16274 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16275 if (GET_CODE (elim) == PLUS)
16277 offset += INTVAL (XEXP (elim, 1));
16278 elim = XEXP (elim, 0);
16281 frame_pointer_fb_offset = -offset;
16283 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16284 in which to eliminate. This is because it's stack pointer isn't
16285 directly accessible as a register within the ISA. To work around
16286 this, assume that while we cannot provide a proper value for
16287 frame_pointer_fb_offset, we won't need one either. */
16288 frame_pointer_fb_offset_valid
16289 = ((SUPPORTS_STACK_ALIGNMENT
16290 && (elim == hard_frame_pointer_rtx
16291 || elim == stack_pointer_rtx))
16292 || elim == (frame_pointer_needed
16293 ? hard_frame_pointer_rtx
16294 : stack_pointer_rtx));
16297 /* Generate a DW_AT_name attribute given some string value to be included as
16298 the value of the attribute. */
16300 static void
16301 add_name_attribute (dw_die_ref die, const char *name_string)
16303 if (name_string != NULL && *name_string != 0)
16305 if (demangle_name_func)
16306 name_string = (*demangle_name_func) (name_string);
16308 add_AT_string (die, DW_AT_name, name_string);
16312 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16313 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16314 of TYPE accordingly.
16316 ??? This is a temporary measure until after we're able to generate
16317 regular DWARF for the complex Ada type system. */
16319 static void
16320 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16321 dw_die_ref context_die)
16323 tree dtype;
16324 dw_die_ref dtype_die;
16326 if (!lang_hooks.types.descriptive_type)
16327 return;
16329 dtype = lang_hooks.types.descriptive_type (type);
16330 if (!dtype)
16331 return;
16333 dtype_die = lookup_type_die (dtype);
16334 if (!dtype_die)
16336 gen_type_die (dtype, context_die);
16337 dtype_die = lookup_type_die (dtype);
16338 gcc_assert (dtype_die);
16341 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16344 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16346 static const char *
16347 comp_dir_string (void)
16349 const char *wd;
16350 char *wd1;
16351 static const char *cached_wd = NULL;
16353 if (cached_wd != NULL)
16354 return cached_wd;
16356 wd = get_src_pwd ();
16357 if (wd == NULL)
16358 return NULL;
16360 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16362 int wdlen;
16364 wdlen = strlen (wd);
16365 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16366 strcpy (wd1, wd);
16367 wd1 [wdlen] = DIR_SEPARATOR;
16368 wd1 [wdlen + 1] = 0;
16369 wd = wd1;
16372 cached_wd = remap_debug_filename (wd);
16373 return cached_wd;
16376 /* Generate a DW_AT_comp_dir attribute for DIE. */
16378 static void
16379 add_comp_dir_attribute (dw_die_ref die)
16381 const char * wd = comp_dir_string ();
16382 if (wd != NULL)
16383 add_AT_string (die, DW_AT_comp_dir, wd);
16386 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16387 default. */
16389 static int
16390 lower_bound_default (void)
16392 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16394 case DW_LANG_C:
16395 case DW_LANG_C89:
16396 case DW_LANG_C99:
16397 case DW_LANG_C_plus_plus:
16398 case DW_LANG_ObjC:
16399 case DW_LANG_ObjC_plus_plus:
16400 case DW_LANG_Java:
16401 return 0;
16402 case DW_LANG_Fortran77:
16403 case DW_LANG_Fortran90:
16404 case DW_LANG_Fortran95:
16405 return 1;
16406 case DW_LANG_UPC:
16407 case DW_LANG_D:
16408 case DW_LANG_Python:
16409 return dwarf_version >= 4 ? 0 : -1;
16410 case DW_LANG_Ada95:
16411 case DW_LANG_Ada83:
16412 case DW_LANG_Cobol74:
16413 case DW_LANG_Cobol85:
16414 case DW_LANG_Pascal83:
16415 case DW_LANG_Modula2:
16416 case DW_LANG_PLI:
16417 return dwarf_version >= 4 ? 1 : -1;
16418 default:
16419 return -1;
16423 /* Given a tree node describing an array bound (either lower or upper) output
16424 a representation for that bound. */
16426 static void
16427 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
16429 switch (TREE_CODE (bound))
16431 case ERROR_MARK:
16432 return;
16434 /* All fixed-bounds are represented by INTEGER_CST nodes. */
16435 case INTEGER_CST:
16437 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
16438 int dflt;
16440 /* Use the default if possible. */
16441 if (bound_attr == DW_AT_lower_bound
16442 && tree_fits_shwi_p (bound)
16443 && (dflt = lower_bound_default ()) != -1
16444 && tree_to_shwi (bound) == dflt)
16447 /* If HOST_WIDE_INT is big enough then represent the bound as
16448 a constant value. We need to choose a form based on
16449 whether the type is signed or unsigned. We cannot just
16450 call add_AT_unsigned if the value itself is positive
16451 (add_AT_unsigned might add the unsigned value encoded as
16452 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16453 bounds type and then sign extend any unsigned values found
16454 for signed types. This is needed only for
16455 DW_AT_{lower,upper}_bound, since for most other attributes,
16456 consumers will treat DW_FORM_data[1248] as unsigned values,
16457 regardless of the underlying type. */
16458 else if (prec <= HOST_BITS_PER_WIDE_INT
16459 || tree_fits_uhwi_p (bound))
16461 if (TYPE_UNSIGNED (TREE_TYPE (bound)))
16462 add_AT_unsigned (subrange_die, bound_attr,
16463 TREE_INT_CST_LOW (bound));
16464 else
16465 add_AT_int (subrange_die, bound_attr, TREE_INT_CST_LOW (bound));
16467 else
16468 /* Otherwise represent the bound as an unsigned value with
16469 the precision of its type. The precision and signedness
16470 of the type will be necessary to re-interpret it
16471 unambiguously. */
16472 add_AT_wide (subrange_die, bound_attr, bound);
16474 break;
16476 CASE_CONVERT:
16477 case VIEW_CONVERT_EXPR:
16478 add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
16479 break;
16481 case SAVE_EXPR:
16482 break;
16484 case VAR_DECL:
16485 case PARM_DECL:
16486 case RESULT_DECL:
16488 dw_die_ref decl_die = lookup_decl_die (bound);
16490 /* ??? Can this happen, or should the variable have been bound
16491 first? Probably it can, since I imagine that we try to create
16492 the types of parameters in the order in which they exist in
16493 the list, and won't have created a forward reference to a
16494 later parameter. */
16495 if (decl_die != NULL)
16497 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16498 break;
16501 /* FALLTHRU */
16503 default:
16505 /* Otherwise try to create a stack operation procedure to
16506 evaluate the value of the array bound. */
16508 dw_die_ref ctx, decl_die;
16509 dw_loc_list_ref list;
16511 list = loc_list_from_tree (bound, 2);
16512 if (list == NULL || single_element_loc_list_p (list))
16514 /* If DW_AT_*bound is not a reference nor constant, it is
16515 a DWARF expression rather than location description.
16516 For that loc_list_from_tree (bound, 0) is needed.
16517 If that fails to give a single element list,
16518 fall back to outputting this as a reference anyway. */
16519 dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
16520 if (list2 && single_element_loc_list_p (list2))
16522 add_AT_loc (subrange_die, bound_attr, list2->expr);
16523 break;
16526 if (list == NULL)
16527 break;
16529 if (current_function_decl == 0)
16530 ctx = comp_unit_die ();
16531 else
16532 ctx = lookup_decl_die (current_function_decl);
16534 decl_die = new_die (DW_TAG_variable, ctx, bound);
16535 add_AT_flag (decl_die, DW_AT_artificial, 1);
16536 add_type_attribute (decl_die, TREE_TYPE (bound), TYPE_QUAL_CONST, ctx);
16537 add_AT_location_description (decl_die, DW_AT_location, list);
16538 add_AT_die_ref (subrange_die, bound_attr, decl_die);
16539 break;
16544 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16545 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16546 Note that the block of subscript information for an array type also
16547 includes information about the element type of the given array type. */
16549 static void
16550 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16552 unsigned dimension_number;
16553 tree lower, upper;
16554 dw_die_ref subrange_die;
16556 for (dimension_number = 0;
16557 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16558 type = TREE_TYPE (type), dimension_number++)
16560 tree domain = TYPE_DOMAIN (type);
16562 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16563 break;
16565 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16566 and (in GNU C only) variable bounds. Handle all three forms
16567 here. */
16568 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16569 if (domain)
16571 /* We have an array type with specified bounds. */
16572 lower = TYPE_MIN_VALUE (domain);
16573 upper = TYPE_MAX_VALUE (domain);
16575 /* Define the index type. */
16576 if (TREE_TYPE (domain))
16578 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16579 TREE_TYPE field. We can't emit debug info for this
16580 because it is an unnamed integral type. */
16581 if (TREE_CODE (domain) == INTEGER_TYPE
16582 && TYPE_NAME (domain) == NULL_TREE
16583 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16584 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16586 else
16587 add_type_attribute (subrange_die, TREE_TYPE (domain),
16588 TYPE_UNQUALIFIED, type_die);
16591 /* ??? If upper is NULL, the array has unspecified length,
16592 but it does have a lower bound. This happens with Fortran
16593 dimension arr(N:*)
16594 Since the debugger is definitely going to need to know N
16595 to produce useful results, go ahead and output the lower
16596 bound solo, and hope the debugger can cope. */
16598 add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16599 if (upper)
16600 add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16603 /* Otherwise we have an array type with an unspecified length. The
16604 DWARF-2 spec does not say how to handle this; let's just leave out the
16605 bounds. */
16609 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16611 static void
16612 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16614 dw_die_ref decl_die;
16615 HOST_WIDE_INT size;
16617 switch (TREE_CODE (tree_node))
16619 case ERROR_MARK:
16620 size = 0;
16621 break;
16622 case ENUMERAL_TYPE:
16623 case RECORD_TYPE:
16624 case UNION_TYPE:
16625 case QUAL_UNION_TYPE:
16626 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16627 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16629 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16630 return;
16632 size = int_size_in_bytes (tree_node);
16633 break;
16634 case FIELD_DECL:
16635 /* For a data member of a struct or union, the DW_AT_byte_size is
16636 generally given as the number of bytes normally allocated for an
16637 object of the *declared* type of the member itself. This is true
16638 even for bit-fields. */
16639 size = int_size_in_bytes (field_type (tree_node));
16640 break;
16641 default:
16642 gcc_unreachable ();
16645 /* Note that `size' might be -1 when we get to this point. If it is, that
16646 indicates that the byte size of the entity in question is variable. We
16647 have no good way of expressing this fact in Dwarf at the present time,
16648 when location description was not used by the caller code instead. */
16649 if (size >= 0)
16650 add_AT_unsigned (die, DW_AT_byte_size, size);
16653 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16654 which specifies the distance in bits from the highest order bit of the
16655 "containing object" for the bit-field to the highest order bit of the
16656 bit-field itself.
16658 For any given bit-field, the "containing object" is a hypothetical object
16659 (of some integral or enum type) within which the given bit-field lives. The
16660 type of this hypothetical "containing object" is always the same as the
16661 declared type of the individual bit-field itself. The determination of the
16662 exact location of the "containing object" for a bit-field is rather
16663 complicated. It's handled by the `field_byte_offset' function (above).
16665 Note that it is the size (in bytes) of the hypothetical "containing object"
16666 which will be given in the DW_AT_byte_size attribute for this bit-field.
16667 (See `byte_size_attribute' above). */
16669 static inline void
16670 add_bit_offset_attribute (dw_die_ref die, tree decl)
16672 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16673 tree type = DECL_BIT_FIELD_TYPE (decl);
16674 HOST_WIDE_INT bitpos_int;
16675 HOST_WIDE_INT highest_order_object_bit_offset;
16676 HOST_WIDE_INT highest_order_field_bit_offset;
16677 HOST_WIDE_INT bit_offset;
16679 /* Must be a field and a bit field. */
16680 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16682 /* We can't yet handle bit-fields whose offsets are variable, so if we
16683 encounter such things, just return without generating any attribute
16684 whatsoever. Likewise for variable or too large size. */
16685 if (! tree_fits_shwi_p (bit_position (decl))
16686 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
16687 return;
16689 bitpos_int = int_bit_position (decl);
16691 /* Note that the bit offset is always the distance (in bits) from the
16692 highest-order bit of the "containing object" to the highest-order bit of
16693 the bit-field itself. Since the "high-order end" of any object or field
16694 is different on big-endian and little-endian machines, the computation
16695 below must take account of these differences. */
16696 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16697 highest_order_field_bit_offset = bitpos_int;
16699 if (! BYTES_BIG_ENDIAN)
16701 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
16702 highest_order_object_bit_offset += simple_type_size_in_bits (type);
16705 bit_offset
16706 = (! BYTES_BIG_ENDIAN
16707 ? highest_order_object_bit_offset - highest_order_field_bit_offset
16708 : highest_order_field_bit_offset - highest_order_object_bit_offset);
16710 if (bit_offset < 0)
16711 add_AT_int (die, DW_AT_bit_offset, bit_offset);
16712 else
16713 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16716 /* For a FIELD_DECL node which represents a bit field, output an attribute
16717 which specifies the length in bits of the given field. */
16719 static inline void
16720 add_bit_size_attribute (dw_die_ref die, tree decl)
16722 /* Must be a field and a bit field. */
16723 gcc_assert (TREE_CODE (decl) == FIELD_DECL
16724 && DECL_BIT_FIELD_TYPE (decl));
16726 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
16727 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
16730 /* If the compiled language is ANSI C, then add a 'prototyped'
16731 attribute, if arg types are given for the parameters of a function. */
16733 static inline void
16734 add_prototyped_attribute (dw_die_ref die, tree func_type)
16736 if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16737 && prototype_p (func_type))
16738 add_AT_flag (die, DW_AT_prototyped, 1);
16741 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
16742 by looking in either the type declaration or object declaration
16743 equate table. */
16745 static inline dw_die_ref
16746 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16748 dw_die_ref origin_die = NULL;
16750 if (TREE_CODE (origin) != FUNCTION_DECL)
16752 /* We may have gotten separated from the block for the inlined
16753 function, if we're in an exception handler or some such; make
16754 sure that the abstract function has been written out.
16756 Doing this for nested functions is wrong, however; functions are
16757 distinct units, and our context might not even be inline. */
16758 tree fn = origin;
16760 if (TYPE_P (fn))
16761 fn = TYPE_STUB_DECL (fn);
16763 fn = decl_function_context (fn);
16764 if (fn)
16765 dwarf2out_abstract_function (fn);
16768 if (DECL_P (origin))
16769 origin_die = lookup_decl_die (origin);
16770 else if (TYPE_P (origin))
16771 origin_die = lookup_type_die (origin);
16773 /* XXX: Functions that are never lowered don't always have correct block
16774 trees (in the case of java, they simply have no block tree, in some other
16775 languages). For these functions, there is nothing we can really do to
16776 output correct debug info for inlined functions in all cases. Rather
16777 than die, we'll just produce deficient debug info now, in that we will
16778 have variables without a proper abstract origin. In the future, when all
16779 functions are lowered, we should re-add a gcc_assert (origin_die)
16780 here. */
16782 if (origin_die)
16783 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16784 return origin_die;
16787 /* We do not currently support the pure_virtual attribute. */
16789 static inline void
16790 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16792 if (DECL_VINDEX (func_decl))
16794 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16796 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
16797 add_AT_loc (die, DW_AT_vtable_elem_location,
16798 new_loc_descr (DW_OP_constu,
16799 tree_to_shwi (DECL_VINDEX (func_decl)),
16800 0));
16802 /* GNU extension: Record what type this method came from originally. */
16803 if (debug_info_level > DINFO_LEVEL_TERSE
16804 && DECL_CONTEXT (func_decl))
16805 add_AT_die_ref (die, DW_AT_containing_type,
16806 lookup_type_die (DECL_CONTEXT (func_decl)));
16810 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16811 given decl. This used to be a vendor extension until after DWARF 4
16812 standardized it. */
16814 static void
16815 add_linkage_attr (dw_die_ref die, tree decl)
16817 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16819 /* Mimic what assemble_name_raw does with a leading '*'. */
16820 if (name[0] == '*')
16821 name = &name[1];
16823 if (dwarf_version >= 4)
16824 add_AT_string (die, DW_AT_linkage_name, name);
16825 else
16826 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16829 /* Add source coordinate attributes for the given decl. */
16831 static void
16832 add_src_coords_attributes (dw_die_ref die, tree decl)
16834 expanded_location s;
16836 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16837 return;
16838 s = expand_location (DECL_SOURCE_LOCATION (decl));
16839 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16840 add_AT_unsigned (die, DW_AT_decl_line, s.line);
16843 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
16845 static void
16846 add_linkage_name (dw_die_ref die, tree decl)
16848 if (debug_info_level > DINFO_LEVEL_NONE
16849 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16850 && TREE_PUBLIC (decl)
16851 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16852 && die->die_tag != DW_TAG_member)
16854 /* Defer until we have an assembler name set. */
16855 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16857 limbo_die_node *asm_name;
16859 asm_name = ggc_cleared_alloc<limbo_die_node> ();
16860 asm_name->die = die;
16861 asm_name->created_for = decl;
16862 asm_name->next = deferred_asm_name;
16863 deferred_asm_name = asm_name;
16865 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16866 add_linkage_attr (die, decl);
16870 /* Add a DW_AT_name attribute and source coordinate attribute for the
16871 given decl, but only if it actually has a name. */
16873 static void
16874 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16876 tree decl_name;
16878 decl_name = DECL_NAME (decl);
16879 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16881 const char *name = dwarf2_name (decl, 0);
16882 if (name)
16883 add_name_attribute (die, name);
16884 if (! DECL_ARTIFICIAL (decl))
16885 add_src_coords_attributes (die, decl);
16887 add_linkage_name (die, decl);
16890 #ifdef VMS_DEBUGGING_INFO
16891 /* Get the function's name, as described by its RTL. This may be different
16892 from the DECL_NAME name used in the source file. */
16893 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16895 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16896 XEXP (DECL_RTL (decl), 0), false);
16897 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16899 #endif /* VMS_DEBUGGING_INFO */
16902 #ifdef VMS_DEBUGGING_INFO
16903 /* Output the debug main pointer die for VMS */
16905 void
16906 dwarf2out_vms_debug_main_pointer (void)
16908 char label[MAX_ARTIFICIAL_LABEL_BYTES];
16909 dw_die_ref die;
16911 /* Allocate the VMS debug main subprogram die. */
16912 die = ggc_cleared_alloc<die_node> ();
16913 die->die_tag = DW_TAG_subprogram;
16914 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16915 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16916 current_function_funcdef_no);
16917 add_AT_lbl_id (die, DW_AT_entry_pc, label);
16919 /* Make it the first child of comp_unit_die (). */
16920 die->die_parent = comp_unit_die ();
16921 if (comp_unit_die ()->die_child)
16923 die->die_sib = comp_unit_die ()->die_child->die_sib;
16924 comp_unit_die ()->die_child->die_sib = die;
16926 else
16928 die->die_sib = die;
16929 comp_unit_die ()->die_child = die;
16932 #endif /* VMS_DEBUGGING_INFO */
16934 /* Push a new declaration scope. */
16936 static void
16937 push_decl_scope (tree scope)
16939 vec_safe_push (decl_scope_table, scope);
16942 /* Pop a declaration scope. */
16944 static inline void
16945 pop_decl_scope (void)
16947 decl_scope_table->pop ();
16950 /* walk_tree helper function for uses_local_type, below. */
16952 static tree
16953 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16955 if (!TYPE_P (*tp))
16956 *walk_subtrees = 0;
16957 else
16959 tree name = TYPE_NAME (*tp);
16960 if (name && DECL_P (name) && decl_function_context (name))
16961 return *tp;
16963 return NULL_TREE;
16966 /* If TYPE involves a function-local type (including a local typedef to a
16967 non-local type), returns that type; otherwise returns NULL_TREE. */
16969 static tree
16970 uses_local_type (tree type)
16972 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16973 return used;
16976 /* Return the DIE for the scope that immediately contains this type.
16977 Non-named types that do not involve a function-local type get global
16978 scope. Named types nested in namespaces or other types get their
16979 containing scope. All other types (i.e. function-local named types) get
16980 the current active scope. */
16982 static dw_die_ref
16983 scope_die_for (tree t, dw_die_ref context_die)
16985 dw_die_ref scope_die = NULL;
16986 tree containing_scope;
16988 /* Non-types always go in the current scope. */
16989 gcc_assert (TYPE_P (t));
16991 /* Use the scope of the typedef, rather than the scope of the type
16992 it refers to. */
16993 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16994 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16995 else
16996 containing_scope = TYPE_CONTEXT (t);
16998 /* Use the containing namespace if there is one. */
16999 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17001 if (context_die == lookup_decl_die (containing_scope))
17002 /* OK */;
17003 else if (debug_info_level > DINFO_LEVEL_TERSE)
17004 context_die = get_context_die (containing_scope);
17005 else
17006 containing_scope = NULL_TREE;
17009 /* Ignore function type "scopes" from the C frontend. They mean that
17010 a tagged type is local to a parmlist of a function declarator, but
17011 that isn't useful to DWARF. */
17012 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17013 containing_scope = NULL_TREE;
17015 if (SCOPE_FILE_SCOPE_P (containing_scope))
17017 /* If T uses a local type keep it local as well, to avoid references
17018 to function-local DIEs from outside the function. */
17019 if (current_function_decl && uses_local_type (t))
17020 scope_die = context_die;
17021 else
17022 scope_die = comp_unit_die ();
17024 else if (TYPE_P (containing_scope))
17026 /* For types, we can just look up the appropriate DIE. */
17027 if (debug_info_level > DINFO_LEVEL_TERSE)
17028 scope_die = get_context_die (containing_scope);
17029 else
17031 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17032 if (scope_die == NULL)
17033 scope_die = comp_unit_die ();
17036 else
17037 scope_die = context_die;
17039 return scope_die;
17042 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17044 static inline int
17045 local_scope_p (dw_die_ref context_die)
17047 for (; context_die; context_die = context_die->die_parent)
17048 if (context_die->die_tag == DW_TAG_inlined_subroutine
17049 || context_die->die_tag == DW_TAG_subprogram)
17050 return 1;
17052 return 0;
17055 /* Returns nonzero if CONTEXT_DIE is a class. */
17057 static inline int
17058 class_scope_p (dw_die_ref context_die)
17060 return (context_die
17061 && (context_die->die_tag == DW_TAG_structure_type
17062 || context_die->die_tag == DW_TAG_class_type
17063 || context_die->die_tag == DW_TAG_interface_type
17064 || context_die->die_tag == DW_TAG_union_type));
17067 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17068 whether or not to treat a DIE in this context as a declaration. */
17070 static inline int
17071 class_or_namespace_scope_p (dw_die_ref context_die)
17073 return (class_scope_p (context_die)
17074 || (context_die && context_die->die_tag == DW_TAG_namespace));
17077 /* Many forms of DIEs require a "type description" attribute. This
17078 routine locates the proper "type descriptor" die for the type given
17079 by 'type' plus any additional qualifiers given by 'cv_quals', and
17080 adds a DW_AT_type attribute below the given die. */
17082 static void
17083 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17084 dw_die_ref context_die)
17086 enum tree_code code = TREE_CODE (type);
17087 dw_die_ref type_die = NULL;
17089 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17090 or fixed-point type, use the inner type. This is because we have no
17091 support for unnamed types in base_type_die. This can happen if this is
17092 an Ada subrange type. Correct solution is emit a subrange type die. */
17093 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17094 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17095 type = TREE_TYPE (type), code = TREE_CODE (type);
17097 if (code == ERROR_MARK
17098 /* Handle a special case. For functions whose return type is void, we
17099 generate *no* type attribute. (Note that no object may have type
17100 `void', so this only applies to function return types). */
17101 || code == VOID_TYPE)
17102 return;
17104 type_die = modified_type_die (type,
17105 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17106 context_die);
17108 if (type_die != NULL)
17109 add_AT_die_ref (object_die, DW_AT_type, type_die);
17112 /* Given an object die, add the calling convention attribute for the
17113 function call type. */
17114 static void
17115 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17117 enum dwarf_calling_convention value = DW_CC_normal;
17119 value = ((enum dwarf_calling_convention)
17120 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17122 if (is_fortran ()
17123 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17125 /* DWARF 2 doesn't provide a way to identify a program's source-level
17126 entry point. DW_AT_calling_convention attributes are only meant
17127 to describe functions' calling conventions. However, lacking a
17128 better way to signal the Fortran main program, we used this for
17129 a long time, following existing custom. Now, DWARF 4 has
17130 DW_AT_main_subprogram, which we add below, but some tools still
17131 rely on the old way, which we thus keep. */
17132 value = DW_CC_program;
17134 if (dwarf_version >= 4 || !dwarf_strict)
17135 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17138 /* Only add the attribute if the backend requests it, and
17139 is not DW_CC_normal. */
17140 if (value && (value != DW_CC_normal))
17141 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17144 /* Given a tree pointer to a struct, class, union, or enum type node, return
17145 a pointer to the (string) tag name for the given type, or zero if the type
17146 was declared without a tag. */
17148 static const char *
17149 type_tag (const_tree type)
17151 const char *name = 0;
17153 if (TYPE_NAME (type) != 0)
17155 tree t = 0;
17157 /* Find the IDENTIFIER_NODE for the type name. */
17158 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17159 && !TYPE_NAMELESS (type))
17160 t = TYPE_NAME (type);
17162 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17163 a TYPE_DECL node, regardless of whether or not a `typedef' was
17164 involved. */
17165 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17166 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17168 /* We want to be extra verbose. Don't call dwarf_name if
17169 DECL_NAME isn't set. The default hook for decl_printable_name
17170 doesn't like that, and in this context it's correct to return
17171 0, instead of "<anonymous>" or the like. */
17172 if (DECL_NAME (TYPE_NAME (type))
17173 && !DECL_NAMELESS (TYPE_NAME (type)))
17174 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17177 /* Now get the name as a string, or invent one. */
17178 if (!name && t != 0)
17179 name = IDENTIFIER_POINTER (t);
17182 return (name == 0 || *name == '\0') ? 0 : name;
17185 /* Return the type associated with a data member, make a special check
17186 for bit field types. */
17188 static inline tree
17189 member_declared_type (const_tree member)
17191 return (DECL_BIT_FIELD_TYPE (member)
17192 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17195 /* Get the decl's label, as described by its RTL. This may be different
17196 from the DECL_NAME name used in the source file. */
17198 #if 0
17199 static const char *
17200 decl_start_label (tree decl)
17202 rtx x;
17203 const char *fnname;
17205 x = DECL_RTL (decl);
17206 gcc_assert (MEM_P (x));
17208 x = XEXP (x, 0);
17209 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17211 fnname = XSTR (x, 0);
17212 return fnname;
17214 #endif
17216 /* These routines generate the internal representation of the DIE's for
17217 the compilation unit. Debugging information is collected by walking
17218 the declaration trees passed in from dwarf2out_decl(). */
17220 static void
17221 gen_array_type_die (tree type, dw_die_ref context_die)
17223 dw_die_ref scope_die = scope_die_for (type, context_die);
17224 dw_die_ref array_die;
17226 /* GNU compilers represent multidimensional array types as sequences of one
17227 dimensional array types whose element types are themselves array types.
17228 We sometimes squish that down to a single array_type DIE with multiple
17229 subscripts in the Dwarf debugging info. The draft Dwarf specification
17230 say that we are allowed to do this kind of compression in C, because
17231 there is no difference between an array of arrays and a multidimensional
17232 array. We don't do this for Ada to remain as close as possible to the
17233 actual representation, which is especially important against the language
17234 flexibilty wrt arrays of variable size. */
17236 bool collapse_nested_arrays = !is_ada ();
17237 tree element_type;
17239 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17240 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17241 if (TYPE_STRING_FLAG (type)
17242 && TREE_CODE (type) == ARRAY_TYPE
17243 && is_fortran ()
17244 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17246 HOST_WIDE_INT size;
17248 array_die = new_die (DW_TAG_string_type, scope_die, type);
17249 add_name_attribute (array_die, type_tag (type));
17250 equate_type_number_to_die (type, array_die);
17251 size = int_size_in_bytes (type);
17252 if (size >= 0)
17253 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17254 else if (TYPE_DOMAIN (type) != NULL_TREE
17255 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17256 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17258 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17259 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17261 size = int_size_in_bytes (TREE_TYPE (szdecl));
17262 if (loc && size > 0)
17264 add_AT_location_description (array_die, DW_AT_string_length, loc);
17265 if (size != DWARF2_ADDR_SIZE)
17266 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17269 return;
17272 array_die = new_die (DW_TAG_array_type, scope_die, type);
17273 add_name_attribute (array_die, type_tag (type));
17274 equate_type_number_to_die (type, array_die);
17276 if (TREE_CODE (type) == VECTOR_TYPE)
17277 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17279 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17280 if (is_fortran ()
17281 && TREE_CODE (type) == ARRAY_TYPE
17282 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17283 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17284 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17286 #if 0
17287 /* We default the array ordering. SDB will probably do
17288 the right things even if DW_AT_ordering is not present. It's not even
17289 an issue until we start to get into multidimensional arrays anyway. If
17290 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17291 then we'll have to put the DW_AT_ordering attribute back in. (But if
17292 and when we find out that we need to put these in, we will only do so
17293 for multidimensional arrays. */
17294 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17295 #endif
17297 if (TREE_CODE (type) == VECTOR_TYPE)
17299 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17300 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17301 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
17302 add_bound_info (subrange_die, DW_AT_upper_bound,
17303 size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
17305 else
17306 add_subscript_info (array_die, type, collapse_nested_arrays);
17308 /* Add representation of the type of the elements of this array type and
17309 emit the corresponding DIE if we haven't done it already. */
17310 element_type = TREE_TYPE (type);
17311 if (collapse_nested_arrays)
17312 while (TREE_CODE (element_type) == ARRAY_TYPE)
17314 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17315 break;
17316 element_type = TREE_TYPE (element_type);
17319 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17321 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17322 if (TYPE_ARTIFICIAL (type))
17323 add_AT_flag (array_die, DW_AT_artificial, 1);
17325 if (get_AT (array_die, DW_AT_name))
17326 add_pubtype (type, array_die);
17329 static dw_loc_descr_ref
17330 descr_info_loc (tree val, tree base_decl)
17332 HOST_WIDE_INT size;
17333 dw_loc_descr_ref loc, loc2;
17334 enum dwarf_location_atom op;
17336 if (val == base_decl)
17337 return new_loc_descr (DW_OP_push_object_address, 0, 0);
17339 switch (TREE_CODE (val))
17341 CASE_CONVERT:
17342 return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17343 case VAR_DECL:
17344 return loc_descriptor_from_tree (val, 0);
17345 case INTEGER_CST:
17346 if (tree_fits_shwi_p (val))
17347 return int_loc_descriptor (tree_to_shwi (val));
17348 break;
17349 case INDIRECT_REF:
17350 size = int_size_in_bytes (TREE_TYPE (val));
17351 if (size < 0)
17352 break;
17353 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17354 if (!loc)
17355 break;
17356 if (size == DWARF2_ADDR_SIZE)
17357 add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17358 else
17359 add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17360 return loc;
17361 case POINTER_PLUS_EXPR:
17362 case PLUS_EXPR:
17363 if (tree_fits_uhwi_p (TREE_OPERAND (val, 1))
17364 && tree_to_uhwi (TREE_OPERAND (val, 1)) < 16384)
17366 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17367 if (!loc)
17368 break;
17369 loc_descr_plus_const (&loc, tree_to_shwi (TREE_OPERAND (val, 1)));
17371 else
17373 op = DW_OP_plus;
17374 do_binop:
17375 loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17376 if (!loc)
17377 break;
17378 loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17379 if (!loc2)
17380 break;
17381 add_loc_descr (&loc, loc2);
17382 add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17384 return loc;
17385 case MINUS_EXPR:
17386 op = DW_OP_minus;
17387 goto do_binop;
17388 case MULT_EXPR:
17389 op = DW_OP_mul;
17390 goto do_binop;
17391 case EQ_EXPR:
17392 op = DW_OP_eq;
17393 goto do_binop;
17394 case NE_EXPR:
17395 op = DW_OP_ne;
17396 goto do_binop;
17397 default:
17398 break;
17400 return NULL;
17403 static void
17404 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17405 tree val, tree base_decl)
17407 dw_loc_descr_ref loc;
17409 if (tree_fits_shwi_p (val))
17411 add_AT_unsigned (die, attr, tree_to_shwi (val));
17412 return;
17415 loc = descr_info_loc (val, base_decl);
17416 if (!loc)
17417 return;
17419 add_AT_loc (die, attr, loc);
17422 /* This routine generates DIE for array with hidden descriptor, details
17423 are filled into *info by a langhook. */
17425 static void
17426 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17427 dw_die_ref context_die)
17429 dw_die_ref scope_die = scope_die_for (type, context_die);
17430 dw_die_ref array_die;
17431 int dim;
17433 array_die = new_die (DW_TAG_array_type, scope_die, type);
17434 add_name_attribute (array_die, type_tag (type));
17435 equate_type_number_to_die (type, array_die);
17437 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17438 if (is_fortran ()
17439 && info->ndimensions >= 2)
17440 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17442 if (info->data_location)
17443 add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
17444 info->base_decl);
17445 if (info->associated)
17446 add_descr_info_field (array_die, DW_AT_associated, info->associated,
17447 info->base_decl);
17448 if (info->allocated)
17449 add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
17450 info->base_decl);
17452 for (dim = 0; dim < info->ndimensions; dim++)
17454 dw_die_ref subrange_die
17455 = new_die (DW_TAG_subrange_type, array_die, NULL);
17457 if (info->dimen[dim].lower_bound)
17459 /* If it is the default value, omit it. */
17460 int dflt;
17462 if (tree_fits_shwi_p (info->dimen[dim].lower_bound)
17463 && (dflt = lower_bound_default ()) != -1
17464 && tree_to_shwi (info->dimen[dim].lower_bound) == dflt)
17466 else
17467 add_descr_info_field (subrange_die, DW_AT_lower_bound,
17468 info->dimen[dim].lower_bound,
17469 info->base_decl);
17471 if (info->dimen[dim].upper_bound)
17472 add_descr_info_field (subrange_die, DW_AT_upper_bound,
17473 info->dimen[dim].upper_bound,
17474 info->base_decl);
17475 if (info->dimen[dim].stride)
17476 add_descr_info_field (subrange_die, DW_AT_byte_stride,
17477 info->dimen[dim].stride,
17478 info->base_decl);
17481 gen_type_die (info->element_type, context_die);
17482 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17483 context_die);
17485 if (get_AT (array_die, DW_AT_name))
17486 add_pubtype (type, array_die);
17489 #if 0
17490 static void
17491 gen_entry_point_die (tree decl, dw_die_ref context_die)
17493 tree origin = decl_ultimate_origin (decl);
17494 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17496 if (origin != NULL)
17497 add_abstract_origin_attribute (decl_die, origin);
17498 else
17500 add_name_and_src_coords_attributes (decl_die, decl);
17501 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17502 TYPE_UNQUALIFIED, context_die);
17505 if (DECL_ABSTRACT_P (decl))
17506 equate_decl_number_to_die (decl, decl_die);
17507 else
17508 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17510 #endif
17512 /* Walk through the list of incomplete types again, trying once more to
17513 emit full debugging info for them. */
17515 static void
17516 retry_incomplete_types (void)
17518 int i;
17520 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17521 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17522 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17525 /* Determine what tag to use for a record type. */
17527 static enum dwarf_tag
17528 record_type_tag (tree type)
17530 if (! lang_hooks.types.classify_record)
17531 return DW_TAG_structure_type;
17533 switch (lang_hooks.types.classify_record (type))
17535 case RECORD_IS_STRUCT:
17536 return DW_TAG_structure_type;
17538 case RECORD_IS_CLASS:
17539 return DW_TAG_class_type;
17541 case RECORD_IS_INTERFACE:
17542 if (dwarf_version >= 3 || !dwarf_strict)
17543 return DW_TAG_interface_type;
17544 return DW_TAG_structure_type;
17546 default:
17547 gcc_unreachable ();
17551 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17552 include all of the information about the enumeration values also. Each
17553 enumerated type name/value is listed as a child of the enumerated type
17554 DIE. */
17556 static dw_die_ref
17557 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17559 dw_die_ref type_die = lookup_type_die (type);
17561 if (type_die == NULL)
17563 type_die = new_die (DW_TAG_enumeration_type,
17564 scope_die_for (type, context_die), type);
17565 equate_type_number_to_die (type, type_die);
17566 add_name_attribute (type_die, type_tag (type));
17567 if (dwarf_version >= 4 || !dwarf_strict)
17569 if (ENUM_IS_SCOPED (type))
17570 add_AT_flag (type_die, DW_AT_enum_class, 1);
17571 if (ENUM_IS_OPAQUE (type))
17572 add_AT_flag (type_die, DW_AT_declaration, 1);
17575 else if (! TYPE_SIZE (type))
17576 return type_die;
17577 else
17578 remove_AT (type_die, DW_AT_declaration);
17580 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17581 given enum type is incomplete, do not generate the DW_AT_byte_size
17582 attribute or the DW_AT_element_list attribute. */
17583 if (TYPE_SIZE (type))
17585 tree link;
17587 TREE_ASM_WRITTEN (type) = 1;
17588 add_byte_size_attribute (type_die, type);
17589 if (dwarf_version >= 3 || !dwarf_strict)
17591 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17592 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17593 context_die);
17595 if (TYPE_STUB_DECL (type) != NULL_TREE)
17597 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17598 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17601 /* If the first reference to this type was as the return type of an
17602 inline function, then it may not have a parent. Fix this now. */
17603 if (type_die->die_parent == NULL)
17604 add_child_die (scope_die_for (type, context_die), type_die);
17606 for (link = TYPE_VALUES (type);
17607 link != NULL; link = TREE_CHAIN (link))
17609 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17610 tree value = TREE_VALUE (link);
17612 add_name_attribute (enum_die,
17613 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17615 if (TREE_CODE (value) == CONST_DECL)
17616 value = DECL_INITIAL (value);
17618 if (simple_type_size_in_bits (TREE_TYPE (value))
17619 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17621 /* For constant forms created by add_AT_unsigned DWARF
17622 consumers (GDB, elfutils, etc.) always zero extend
17623 the value. Only when the actual value is negative
17624 do we need to use add_AT_int to generate a constant
17625 form that can represent negative values. */
17626 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17627 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17628 add_AT_unsigned (enum_die, DW_AT_const_value,
17629 (unsigned HOST_WIDE_INT) val);
17630 else
17631 add_AT_int (enum_die, DW_AT_const_value, val);
17633 else
17634 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17635 that here. TODO: This should be re-worked to use correct
17636 signed/unsigned double tags for all cases. */
17637 add_AT_wide (enum_die, DW_AT_const_value, value);
17640 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17641 if (TYPE_ARTIFICIAL (type))
17642 add_AT_flag (type_die, DW_AT_artificial, 1);
17644 else
17645 add_AT_flag (type_die, DW_AT_declaration, 1);
17647 add_pubtype (type, type_die);
17649 return type_die;
17652 /* Generate a DIE to represent either a real live formal parameter decl or to
17653 represent just the type of some formal parameter position in some function
17654 type.
17656 Note that this routine is a bit unusual because its argument may be a
17657 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17658 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17659 node. If it's the former then this function is being called to output a
17660 DIE to represent a formal parameter object (or some inlining thereof). If
17661 it's the latter, then this function is only being called to output a
17662 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17663 argument type of some subprogram type.
17664 If EMIT_NAME_P is true, name and source coordinate attributes
17665 are emitted. */
17667 static dw_die_ref
17668 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17669 dw_die_ref context_die)
17671 tree node_or_origin = node ? node : origin;
17672 tree ultimate_origin;
17673 dw_die_ref parm_die
17674 = new_die (DW_TAG_formal_parameter, context_die, node);
17676 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17678 case tcc_declaration:
17679 ultimate_origin = decl_ultimate_origin (node_or_origin);
17680 if (node || ultimate_origin)
17681 origin = ultimate_origin;
17682 if (origin != NULL)
17683 add_abstract_origin_attribute (parm_die, origin);
17684 else if (emit_name_p)
17685 add_name_and_src_coords_attributes (parm_die, node);
17686 if (origin == NULL
17687 || (! DECL_ABSTRACT_P (node_or_origin)
17688 && variably_modified_type_p (TREE_TYPE (node_or_origin),
17689 decl_function_context
17690 (node_or_origin))))
17692 tree type = TREE_TYPE (node_or_origin);
17693 if (decl_by_reference_p (node_or_origin))
17694 add_type_attribute (parm_die, TREE_TYPE (type),
17695 TYPE_UNQUALIFIED, context_die);
17696 else
17697 add_type_attribute (parm_die, type,
17698 decl_quals (node_or_origin),
17699 context_die);
17701 if (origin == NULL && DECL_ARTIFICIAL (node))
17702 add_AT_flag (parm_die, DW_AT_artificial, 1);
17704 if (node && node != origin)
17705 equate_decl_number_to_die (node, parm_die);
17706 if (! DECL_ABSTRACT_P (node_or_origin))
17707 add_location_or_const_value_attribute (parm_die, node_or_origin,
17708 node == NULL, DW_AT_location);
17710 break;
17712 case tcc_type:
17713 /* We were called with some kind of a ..._TYPE node. */
17714 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
17715 context_die);
17716 break;
17718 default:
17719 gcc_unreachable ();
17722 return parm_die;
17725 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17726 children DW_TAG_formal_parameter DIEs representing the arguments of the
17727 parameter pack.
17729 PARM_PACK must be a function parameter pack.
17730 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17731 must point to the subsequent arguments of the function PACK_ARG belongs to.
17732 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17733 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17734 following the last one for which a DIE was generated. */
17736 static dw_die_ref
17737 gen_formal_parameter_pack_die (tree parm_pack,
17738 tree pack_arg,
17739 dw_die_ref subr_die,
17740 tree *next_arg)
17742 tree arg;
17743 dw_die_ref parm_pack_die;
17745 gcc_assert (parm_pack
17746 && lang_hooks.function_parameter_pack_p (parm_pack)
17747 && subr_die);
17749 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17750 add_src_coords_attributes (parm_pack_die, parm_pack);
17752 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17754 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17755 parm_pack))
17756 break;
17757 gen_formal_parameter_die (arg, NULL,
17758 false /* Don't emit name attribute. */,
17759 parm_pack_die);
17761 if (next_arg)
17762 *next_arg = arg;
17763 return parm_pack_die;
17766 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17767 at the end of an (ANSI prototyped) formal parameters list. */
17769 static void
17770 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17772 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17775 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17776 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17777 parameters as specified in some function type specification (except for
17778 those which appear as part of a function *definition*). */
17780 static void
17781 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17783 tree link;
17784 tree formal_type = NULL;
17785 tree first_parm_type;
17786 tree arg;
17788 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17790 arg = DECL_ARGUMENTS (function_or_method_type);
17791 function_or_method_type = TREE_TYPE (function_or_method_type);
17793 else
17794 arg = NULL_TREE;
17796 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17798 /* Make our first pass over the list of formal parameter types and output a
17799 DW_TAG_formal_parameter DIE for each one. */
17800 for (link = first_parm_type; link; )
17802 dw_die_ref parm_die;
17804 formal_type = TREE_VALUE (link);
17805 if (formal_type == void_type_node)
17806 break;
17808 /* Output a (nameless) DIE to represent the formal parameter itself. */
17809 parm_die = gen_formal_parameter_die (formal_type, NULL,
17810 true /* Emit name attribute. */,
17811 context_die);
17812 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17813 && link == first_parm_type)
17815 add_AT_flag (parm_die, DW_AT_artificial, 1);
17816 if (dwarf_version >= 3 || !dwarf_strict)
17817 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17819 else if (arg && DECL_ARTIFICIAL (arg))
17820 add_AT_flag (parm_die, DW_AT_artificial, 1);
17822 link = TREE_CHAIN (link);
17823 if (arg)
17824 arg = DECL_CHAIN (arg);
17827 /* If this function type has an ellipsis, add a
17828 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
17829 if (formal_type != void_type_node)
17830 gen_unspecified_parameters_die (function_or_method_type, context_die);
17832 /* Make our second (and final) pass over the list of formal parameter types
17833 and output DIEs to represent those types (as necessary). */
17834 for (link = TYPE_ARG_TYPES (function_or_method_type);
17835 link && TREE_VALUE (link);
17836 link = TREE_CHAIN (link))
17837 gen_type_die (TREE_VALUE (link), context_die);
17840 /* We want to generate the DIE for TYPE so that we can generate the
17841 die for MEMBER, which has been defined; we will need to refer back
17842 to the member declaration nested within TYPE. If we're trying to
17843 generate minimal debug info for TYPE, processing TYPE won't do the
17844 trick; we need to attach the member declaration by hand. */
17846 static void
17847 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17849 gen_type_die (type, context_die);
17851 /* If we're trying to avoid duplicate debug info, we may not have
17852 emitted the member decl for this function. Emit it now. */
17853 if (TYPE_STUB_DECL (type)
17854 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17855 && ! lookup_decl_die (member))
17857 dw_die_ref type_die;
17858 gcc_assert (!decl_ultimate_origin (member));
17860 push_decl_scope (type);
17861 type_die = lookup_type_die_strip_naming_typedef (type);
17862 if (TREE_CODE (member) == FUNCTION_DECL)
17863 gen_subprogram_die (member, type_die);
17864 else if (TREE_CODE (member) == FIELD_DECL)
17866 /* Ignore the nameless fields that are used to skip bits but handle
17867 C++ anonymous unions and structs. */
17868 if (DECL_NAME (member) != NULL_TREE
17869 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17870 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17872 gen_type_die (member_declared_type (member), type_die);
17873 gen_field_die (member, type_die);
17876 else
17877 gen_variable_die (member, NULL_TREE, type_die);
17879 pop_decl_scope ();
17883 /* Forward declare these functions, because they are mutually recursive
17884 with their set_block_* pairing functions. */
17885 static void set_decl_origin_self (tree);
17886 static void set_decl_abstract_flags (tree, int);
17888 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17889 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17890 that it points to the node itself, thus indicating that the node is its
17891 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17892 the given node is NULL, recursively descend the decl/block tree which
17893 it is the root of, and for each other ..._DECL or BLOCK node contained
17894 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17895 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17896 values to point to themselves. */
17898 static void
17899 set_block_origin_self (tree stmt)
17901 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17903 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17906 tree local_decl;
17908 for (local_decl = BLOCK_VARS (stmt);
17909 local_decl != NULL_TREE;
17910 local_decl = DECL_CHAIN (local_decl))
17911 if (! DECL_EXTERNAL (local_decl))
17912 set_decl_origin_self (local_decl); /* Potential recursion. */
17916 tree subblock;
17918 for (subblock = BLOCK_SUBBLOCKS (stmt);
17919 subblock != NULL_TREE;
17920 subblock = BLOCK_CHAIN (subblock))
17921 set_block_origin_self (subblock); /* Recurse. */
17926 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17927 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17928 node to so that it points to the node itself, thus indicating that the
17929 node represents its own (abstract) origin. Additionally, if the
17930 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17931 the decl/block tree of which the given node is the root of, and for
17932 each other ..._DECL or BLOCK node contained therein whose
17933 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17934 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17935 point to themselves. */
17937 static void
17938 set_decl_origin_self (tree decl)
17940 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17942 DECL_ABSTRACT_ORIGIN (decl) = decl;
17943 if (TREE_CODE (decl) == FUNCTION_DECL)
17945 tree arg;
17947 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17948 DECL_ABSTRACT_ORIGIN (arg) = arg;
17949 if (DECL_INITIAL (decl) != NULL_TREE
17950 && DECL_INITIAL (decl) != error_mark_node)
17951 set_block_origin_self (DECL_INITIAL (decl));
17956 /* Given a pointer to some BLOCK node, and a boolean value to set the
17957 "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17958 the given block, and for all local decls and all local sub-blocks
17959 (recursively) which are contained therein. */
17961 static void
17962 set_block_abstract_flags (tree stmt, int setting)
17964 tree local_decl;
17965 tree subblock;
17966 unsigned int i;
17968 BLOCK_ABSTRACT (stmt) = setting;
17970 for (local_decl = BLOCK_VARS (stmt);
17971 local_decl != NULL_TREE;
17972 local_decl = DECL_CHAIN (local_decl))
17973 if (! DECL_EXTERNAL (local_decl))
17974 set_decl_abstract_flags (local_decl, setting);
17976 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17978 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17979 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17980 || TREE_CODE (local_decl) == PARM_DECL)
17981 set_decl_abstract_flags (local_decl, setting);
17984 for (subblock = BLOCK_SUBBLOCKS (stmt);
17985 subblock != NULL_TREE;
17986 subblock = BLOCK_CHAIN (subblock))
17987 set_block_abstract_flags (subblock, setting);
17990 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17991 "abstract" flags to, set that value into the DECL_ABSTRACT_P flag for the
17992 given decl, and (in the case where the decl is a FUNCTION_DECL) also
17993 set the abstract flags for all of the parameters, local vars, local
17994 blocks and sub-blocks (recursively) to the same setting. */
17996 static void
17997 set_decl_abstract_flags (tree decl, int setting)
17999 DECL_ABSTRACT_P (decl) = setting;
18000 if (TREE_CODE (decl) == FUNCTION_DECL)
18002 tree arg;
18004 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18005 DECL_ABSTRACT_P (arg) = setting;
18006 if (DECL_INITIAL (decl) != NULL_TREE
18007 && DECL_INITIAL (decl) != error_mark_node)
18008 set_block_abstract_flags (DECL_INITIAL (decl), setting);
18012 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18013 may later generate inlined and/or out-of-line instances of. */
18015 static void
18016 dwarf2out_abstract_function (tree decl)
18018 dw_die_ref old_die;
18019 tree save_fn;
18020 tree context;
18021 int was_abstract;
18022 hash_table<decl_loc_hasher> *old_decl_loc_table;
18023 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18024 int old_call_site_count, old_tail_call_site_count;
18025 struct call_arg_loc_node *old_call_arg_locations;
18027 /* Make sure we have the actual abstract inline, not a clone. */
18028 decl = DECL_ORIGIN (decl);
18030 old_die = lookup_decl_die (decl);
18031 if (old_die && get_AT (old_die, DW_AT_inline))
18032 /* We've already generated the abstract instance. */
18033 return;
18035 /* We can be called while recursively when seeing block defining inlined subroutine
18036 DIE. Be sure to not clobber the outer location table nor use it or we would
18037 get locations in abstract instantces. */
18038 old_decl_loc_table = decl_loc_table;
18039 decl_loc_table = NULL;
18040 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18041 cached_dw_loc_list_table = NULL;
18042 old_call_arg_locations = call_arg_locations;
18043 call_arg_locations = NULL;
18044 old_call_site_count = call_site_count;
18045 call_site_count = -1;
18046 old_tail_call_site_count = tail_call_site_count;
18047 tail_call_site_count = -1;
18049 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18050 we don't get confused by DECL_ABSTRACT_P. */
18051 if (debug_info_level > DINFO_LEVEL_TERSE)
18053 context = decl_class_context (decl);
18054 if (context)
18055 gen_type_die_for_member
18056 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18059 /* Pretend we've just finished compiling this function. */
18060 save_fn = current_function_decl;
18061 current_function_decl = decl;
18063 was_abstract = DECL_ABSTRACT_P (decl);
18064 set_decl_abstract_flags (decl, 1);
18065 dwarf2out_decl (decl);
18066 if (! was_abstract)
18067 set_decl_abstract_flags (decl, 0);
18069 current_function_decl = save_fn;
18070 decl_loc_table = old_decl_loc_table;
18071 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18072 call_arg_locations = old_call_arg_locations;
18073 call_site_count = old_call_site_count;
18074 tail_call_site_count = old_tail_call_site_count;
18077 /* Helper function of premark_used_types() which gets called through
18078 htab_traverse.
18080 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18081 marked as unused by prune_unused_types. */
18083 bool
18084 premark_used_types_helper (tree const &type, void *)
18086 dw_die_ref die;
18088 die = lookup_type_die (type);
18089 if (die != NULL)
18090 die->die_perennial_p = 1;
18091 return true;
18094 /* Helper function of premark_types_used_by_global_vars which gets called
18095 through htab_traverse.
18097 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18098 marked as unused by prune_unused_types. The DIE of the type is marked
18099 only if the global variable using the type will actually be emitted. */
18102 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18103 void *)
18105 struct types_used_by_vars_entry *entry;
18106 dw_die_ref die;
18108 entry = (struct types_used_by_vars_entry *) *slot;
18109 gcc_assert (entry->type != NULL
18110 && entry->var_decl != NULL);
18111 die = lookup_type_die (entry->type);
18112 if (die)
18114 /* Ask cgraph if the global variable really is to be emitted.
18115 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18116 varpool_node *node = varpool_node::get (entry->var_decl);
18117 if (node && node->definition)
18119 die->die_perennial_p = 1;
18120 /* Keep the parent DIEs as well. */
18121 while ((die = die->die_parent) && die->die_perennial_p == 0)
18122 die->die_perennial_p = 1;
18125 return 1;
18128 /* Mark all members of used_types_hash as perennial. */
18130 static void
18131 premark_used_types (struct function *fun)
18133 if (fun && fun->used_types_hash)
18134 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18137 /* Mark all members of types_used_by_vars_entry as perennial. */
18139 static void
18140 premark_types_used_by_global_vars (void)
18142 if (types_used_by_vars_hash)
18143 types_used_by_vars_hash
18144 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18147 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18148 for CA_LOC call arg loc node. */
18150 static dw_die_ref
18151 gen_call_site_die (tree decl, dw_die_ref subr_die,
18152 struct call_arg_loc_node *ca_loc)
18154 dw_die_ref stmt_die = NULL, die;
18155 tree block = ca_loc->block;
18157 while (block
18158 && block != DECL_INITIAL (decl)
18159 && TREE_CODE (block) == BLOCK)
18161 if (block_map.length () > BLOCK_NUMBER (block))
18162 stmt_die = block_map[BLOCK_NUMBER (block)];
18163 if (stmt_die)
18164 break;
18165 block = BLOCK_SUPERCONTEXT (block);
18167 if (stmt_die == NULL)
18168 stmt_die = subr_die;
18169 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18170 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18171 if (ca_loc->tail_call_p)
18172 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18173 if (ca_loc->symbol_ref)
18175 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18176 if (tdie)
18177 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18178 else
18179 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18181 return die;
18184 /* Generate a DIE to represent a declared function (either file-scope or
18185 block-local). */
18187 static void
18188 gen_subprogram_die (tree decl, dw_die_ref context_die)
18190 tree origin = decl_ultimate_origin (decl);
18191 dw_die_ref subr_die;
18192 tree outer_scope;
18193 dw_die_ref old_die = lookup_decl_die (decl);
18194 int declaration = (current_function_decl != decl
18195 || class_or_namespace_scope_p (context_die));
18197 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18199 /* It is possible to have both DECL_ABSTRACT_P and DECLARATION be true if we
18200 started to generate the abstract instance of an inline, decided to output
18201 its containing class, and proceeded to emit the declaration of the inline
18202 from the member list for the class. If so, DECLARATION takes priority;
18203 we'll get back to the abstract instance when done with the class. */
18205 /* The class-scope declaration DIE must be the primary DIE. */
18206 if (origin && declaration && class_or_namespace_scope_p (context_die))
18208 origin = NULL;
18209 gcc_assert (!old_die);
18212 /* Now that the C++ front end lazily declares artificial member fns, we
18213 might need to retrofit the declaration into its class. */
18214 if (!declaration && !origin && !old_die
18215 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18216 && !class_or_namespace_scope_p (context_die)
18217 && debug_info_level > DINFO_LEVEL_TERSE)
18218 old_die = force_decl_die (decl);
18220 if (origin != NULL)
18222 gcc_assert (!declaration || local_scope_p (context_die));
18224 /* Fixup die_parent for the abstract instance of a nested
18225 inline function. */
18226 if (old_die && old_die->die_parent == NULL)
18227 add_child_die (context_die, old_die);
18229 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18230 add_abstract_origin_attribute (subr_die, origin);
18231 /* This is where the actual code for a cloned function is.
18232 Let's emit linkage name attribute for it. This helps
18233 debuggers to e.g, set breakpoints into
18234 constructors/destructors when the user asks "break
18235 K::K". */
18236 add_linkage_name (subr_die, decl);
18238 else if (old_die)
18240 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18241 struct dwarf_file_data * file_index = lookup_filename (s.file);
18243 if (!get_AT_flag (old_die, DW_AT_declaration)
18244 /* We can have a normal definition following an inline one in the
18245 case of redefinition of GNU C extern inlines.
18246 It seems reasonable to use AT_specification in this case. */
18247 && !get_AT (old_die, DW_AT_inline))
18249 /* Detect and ignore this case, where we are trying to output
18250 something we have already output. */
18251 return;
18254 /* If the definition comes from the same place as the declaration,
18255 maybe use the old DIE. We always want the DIE for this function
18256 that has the *_pc attributes to be under comp_unit_die so the
18257 debugger can find it. We also need to do this for abstract
18258 instances of inlines, since the spec requires the out-of-line copy
18259 to have the same parent. For local class methods, this doesn't
18260 apply; we just use the old DIE. */
18261 if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18262 && (DECL_ARTIFICIAL (decl)
18263 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18264 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18265 == (unsigned) s.line))))
18267 subr_die = old_die;
18269 /* Clear out the declaration attribute and the formal parameters.
18270 Do not remove all children, because it is possible that this
18271 declaration die was forced using force_decl_die(). In such
18272 cases die that forced declaration die (e.g. TAG_imported_module)
18273 is one of the children that we do not want to remove. */
18274 remove_AT (subr_die, DW_AT_declaration);
18275 remove_AT (subr_die, DW_AT_object_pointer);
18276 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18278 else
18280 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18281 add_AT_specification (subr_die, old_die);
18282 add_pubname (decl, subr_die);
18283 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18284 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18285 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18286 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18288 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18289 emit the real type on the definition die. */
18290 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18292 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18293 if (die == auto_die || die == decltype_auto_die)
18294 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18295 TYPE_UNQUALIFIED, context_die);
18299 else
18301 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18303 if (TREE_PUBLIC (decl))
18304 add_AT_flag (subr_die, DW_AT_external, 1);
18306 add_name_and_src_coords_attributes (subr_die, decl);
18307 add_pubname (decl, subr_die);
18308 if (debug_info_level > DINFO_LEVEL_TERSE)
18310 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18311 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18312 TYPE_UNQUALIFIED, context_die);
18315 add_pure_or_virtual_attribute (subr_die, decl);
18316 if (DECL_ARTIFICIAL (decl))
18317 add_AT_flag (subr_die, DW_AT_artificial, 1);
18319 add_accessibility_attribute (subr_die, decl);
18322 if (declaration)
18324 if (!old_die || !get_AT (old_die, DW_AT_inline))
18326 add_AT_flag (subr_die, DW_AT_declaration, 1);
18328 /* If this is an explicit function declaration then generate
18329 a DW_AT_explicit attribute. */
18330 if (lang_hooks.decls.function_decl_explicit_p (decl)
18331 && (dwarf_version >= 3 || !dwarf_strict))
18332 add_AT_flag (subr_die, DW_AT_explicit, 1);
18334 /* If this is a C++11 deleted special function member then generate
18335 a DW_AT_GNU_deleted attribute. */
18336 if (lang_hooks.decls.function_decl_deleted_p (decl)
18337 && (! dwarf_strict))
18338 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18340 /* The first time we see a member function, it is in the context of
18341 the class to which it belongs. We make sure of this by emitting
18342 the class first. The next time is the definition, which is
18343 handled above. The two may come from the same source text.
18345 Note that force_decl_die() forces function declaration die. It is
18346 later reused to represent definition. */
18347 equate_decl_number_to_die (decl, subr_die);
18350 else if (DECL_ABSTRACT_P (decl))
18352 if (DECL_DECLARED_INLINE_P (decl))
18354 if (cgraph_function_possibly_inlined_p (decl))
18355 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18356 else
18357 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18359 else
18361 if (cgraph_function_possibly_inlined_p (decl))
18362 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18363 else
18364 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18367 if (DECL_DECLARED_INLINE_P (decl)
18368 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18369 add_AT_flag (subr_die, DW_AT_artificial, 1);
18371 equate_decl_number_to_die (decl, subr_die);
18373 else if (!DECL_EXTERNAL (decl))
18375 HOST_WIDE_INT cfa_fb_offset;
18376 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18378 if (!old_die || !get_AT (old_die, DW_AT_inline))
18379 equate_decl_number_to_die (decl, subr_die);
18381 gcc_checking_assert (fun);
18382 if (!flag_reorder_blocks_and_partition)
18384 dw_fde_ref fde = fun->fde;
18385 if (fde->dw_fde_begin)
18387 /* We have already generated the labels. */
18388 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18389 fde->dw_fde_end, false);
18391 else
18393 /* Create start/end labels and add the range. */
18394 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18395 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18396 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18397 current_function_funcdef_no);
18398 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18399 current_function_funcdef_no);
18400 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18401 false);
18404 #if VMS_DEBUGGING_INFO
18405 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18406 Section 2.3 Prologue and Epilogue Attributes:
18407 When a breakpoint is set on entry to a function, it is generally
18408 desirable for execution to be suspended, not on the very first
18409 instruction of the function, but rather at a point after the
18410 function's frame has been set up, after any language defined local
18411 declaration processing has been completed, and before execution of
18412 the first statement of the function begins. Debuggers generally
18413 cannot properly determine where this point is. Similarly for a
18414 breakpoint set on exit from a function. The prologue and epilogue
18415 attributes allow a compiler to communicate the location(s) to use. */
18418 if (fde->dw_fde_vms_end_prologue)
18419 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18420 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18422 if (fde->dw_fde_vms_begin_epilogue)
18423 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18424 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18426 #endif
18429 else
18431 /* Generate pubnames entries for the split function code ranges. */
18432 dw_fde_ref fde = fun->fde;
18434 if (fde->dw_fde_second_begin)
18436 if (dwarf_version >= 3 || !dwarf_strict)
18438 /* We should use ranges for non-contiguous code section
18439 addresses. Use the actual code range for the initial
18440 section, since the HOT/COLD labels might precede an
18441 alignment offset. */
18442 bool range_list_added = false;
18443 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18444 fde->dw_fde_end, &range_list_added,
18445 false);
18446 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18447 fde->dw_fde_second_end,
18448 &range_list_added, false);
18449 if (range_list_added)
18450 add_ranges (NULL);
18452 else
18454 /* There is no real support in DW2 for this .. so we make
18455 a work-around. First, emit the pub name for the segment
18456 containing the function label. Then make and emit a
18457 simplified subprogram DIE for the second segment with the
18458 name pre-fixed by __hot/cold_sect_of_. We use the same
18459 linkage name for the second die so that gdb will find both
18460 sections when given "b foo". */
18461 const char *name = NULL;
18462 tree decl_name = DECL_NAME (decl);
18463 dw_die_ref seg_die;
18465 /* Do the 'primary' section. */
18466 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18467 fde->dw_fde_end, false);
18469 /* Build a minimal DIE for the secondary section. */
18470 seg_die = new_die (DW_TAG_subprogram,
18471 subr_die->die_parent, decl);
18473 if (TREE_PUBLIC (decl))
18474 add_AT_flag (seg_die, DW_AT_external, 1);
18476 if (decl_name != NULL
18477 && IDENTIFIER_POINTER (decl_name) != NULL)
18479 name = dwarf2_name (decl, 1);
18480 if (! DECL_ARTIFICIAL (decl))
18481 add_src_coords_attributes (seg_die, decl);
18483 add_linkage_name (seg_die, decl);
18485 gcc_assert (name != NULL);
18486 add_pure_or_virtual_attribute (seg_die, decl);
18487 if (DECL_ARTIFICIAL (decl))
18488 add_AT_flag (seg_die, DW_AT_artificial, 1);
18490 name = concat ("__second_sect_of_", name, NULL);
18491 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
18492 fde->dw_fde_second_end, false);
18493 add_name_attribute (seg_die, name);
18494 if (want_pubnames ())
18495 add_pubname_string (name, seg_die);
18498 else
18499 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
18500 false);
18503 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18505 /* We define the "frame base" as the function's CFA. This is more
18506 convenient for several reasons: (1) It's stable across the prologue
18507 and epilogue, which makes it better than just a frame pointer,
18508 (2) With dwarf3, there exists a one-byte encoding that allows us
18509 to reference the .debug_frame data by proxy, but failing that,
18510 (3) We can at least reuse the code inspection and interpretation
18511 code that determines the CFA position at various points in the
18512 function. */
18513 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
18515 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18516 add_AT_loc (subr_die, DW_AT_frame_base, op);
18518 else
18520 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18521 if (list->dw_loc_next)
18522 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18523 else
18524 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18527 /* Compute a displacement from the "steady-state frame pointer" to
18528 the CFA. The former is what all stack slots and argument slots
18529 will reference in the rtl; the latter is what we've told the
18530 debugger about. We'll need to adjust all frame_base references
18531 by this displacement. */
18532 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18534 if (fun->static_chain_decl)
18535 add_AT_location_description (subr_die, DW_AT_static_link,
18536 loc_list_from_tree (fun->static_chain_decl, 2));
18539 /* Generate child dies for template paramaters. */
18540 if (debug_info_level > DINFO_LEVEL_TERSE)
18541 gen_generic_params_dies (decl);
18543 /* Now output descriptions of the arguments for this function. This gets
18544 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18545 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18546 `...' at the end of the formal parameter list. In order to find out if
18547 there was a trailing ellipsis or not, we must instead look at the type
18548 associated with the FUNCTION_DECL. This will be a node of type
18549 FUNCTION_TYPE. If the chain of type nodes hanging off of this
18550 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18551 an ellipsis at the end. */
18553 /* In the case where we are describing a mere function declaration, all we
18554 need to do here (and all we *can* do here) is to describe the *types* of
18555 its formal parameters. */
18556 if (debug_info_level <= DINFO_LEVEL_TERSE)
18558 else if (declaration)
18559 gen_formal_types_die (decl, subr_die);
18560 else
18562 /* Generate DIEs to represent all known formal parameters. */
18563 tree parm = DECL_ARGUMENTS (decl);
18564 tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18565 tree generic_decl_parm = generic_decl
18566 ? DECL_ARGUMENTS (generic_decl)
18567 : NULL;
18569 /* Now we want to walk the list of parameters of the function and
18570 emit their relevant DIEs.
18572 We consider the case of DECL being an instance of a generic function
18573 as well as it being a normal function.
18575 If DECL is an instance of a generic function we walk the
18576 parameters of the generic function declaration _and_ the parameters of
18577 DECL itself. This is useful because we want to emit specific DIEs for
18578 function parameter packs and those are declared as part of the
18579 generic function declaration. In that particular case,
18580 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18581 That DIE has children DIEs representing the set of arguments
18582 of the pack. Note that the set of pack arguments can be empty.
18583 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18584 children DIE.
18586 Otherwise, we just consider the parameters of DECL. */
18587 while (generic_decl_parm || parm)
18589 if (generic_decl_parm
18590 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18591 gen_formal_parameter_pack_die (generic_decl_parm,
18592 parm, subr_die,
18593 &parm);
18594 else if (parm)
18596 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18598 if (parm == DECL_ARGUMENTS (decl)
18599 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18600 && parm_die
18601 && (dwarf_version >= 3 || !dwarf_strict))
18602 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18604 parm = DECL_CHAIN (parm);
18607 if (generic_decl_parm)
18608 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18611 /* Decide whether we need an unspecified_parameters DIE at the end.
18612 There are 2 more cases to do this for: 1) the ansi ... declaration -
18613 this is detectable when the end of the arg list is not a
18614 void_type_node 2) an unprototyped function declaration (not a
18615 definition). This just means that we have no info about the
18616 parameters at all. */
18617 if (prototype_p (TREE_TYPE (decl)))
18619 /* This is the prototyped case, check for.... */
18620 if (stdarg_p (TREE_TYPE (decl)))
18621 gen_unspecified_parameters_die (decl, subr_die);
18623 else if (DECL_INITIAL (decl) == NULL_TREE)
18624 gen_unspecified_parameters_die (decl, subr_die);
18627 /* Output Dwarf info for all of the stuff within the body of the function
18628 (if it has one - it may be just a declaration). */
18629 outer_scope = DECL_INITIAL (decl);
18631 /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18632 a function. This BLOCK actually represents the outermost binding contour
18633 for the function, i.e. the contour in which the function's formal
18634 parameters and labels get declared. Curiously, it appears that the front
18635 end doesn't actually put the PARM_DECL nodes for the current function onto
18636 the BLOCK_VARS list for this outer scope, but are strung off of the
18637 DECL_ARGUMENTS list for the function instead.
18639 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18640 the LABEL_DECL nodes for the function however, and we output DWARF info
18641 for those in decls_for_scope. Just within the `outer_scope' there will be
18642 a BLOCK node representing the function's outermost pair of curly braces,
18643 and any blocks used for the base and member initializers of a C++
18644 constructor function. */
18645 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18647 int call_site_note_count = 0;
18648 int tail_call_site_note_count = 0;
18650 /* Emit a DW_TAG_variable DIE for a named return value. */
18651 if (DECL_NAME (DECL_RESULT (decl)))
18652 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18654 decls_for_scope (outer_scope, subr_die, 0);
18656 if (call_arg_locations && !dwarf_strict)
18658 struct call_arg_loc_node *ca_loc;
18659 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18661 dw_die_ref die = NULL;
18662 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18663 rtx arg, next_arg;
18665 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18666 arg; arg = next_arg)
18668 dw_loc_descr_ref reg, val;
18669 enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18670 dw_die_ref cdie, tdie = NULL;
18672 next_arg = XEXP (arg, 1);
18673 if (REG_P (XEXP (XEXP (arg, 0), 0))
18674 && next_arg
18675 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18676 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18677 && REGNO (XEXP (XEXP (arg, 0), 0))
18678 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18679 next_arg = XEXP (next_arg, 1);
18680 if (mode == VOIDmode)
18682 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18683 if (mode == VOIDmode)
18684 mode = GET_MODE (XEXP (arg, 0));
18686 if (mode == VOIDmode || mode == BLKmode)
18687 continue;
18688 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18690 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18691 tloc = XEXP (XEXP (arg, 0), 1);
18692 continue;
18694 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18695 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18697 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18698 tlocc = XEXP (XEXP (arg, 0), 1);
18699 continue;
18701 reg = NULL;
18702 if (REG_P (XEXP (XEXP (arg, 0), 0)))
18703 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18704 VAR_INIT_STATUS_INITIALIZED);
18705 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18707 rtx mem = XEXP (XEXP (arg, 0), 0);
18708 reg = mem_loc_descriptor (XEXP (mem, 0),
18709 get_address_mode (mem),
18710 GET_MODE (mem),
18711 VAR_INIT_STATUS_INITIALIZED);
18713 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18714 == DEBUG_PARAMETER_REF)
18716 tree tdecl
18717 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18718 tdie = lookup_decl_die (tdecl);
18719 if (tdie == NULL)
18720 continue;
18722 else
18723 continue;
18724 if (reg == NULL
18725 && GET_CODE (XEXP (XEXP (arg, 0), 0))
18726 != DEBUG_PARAMETER_REF)
18727 continue;
18728 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18729 VOIDmode,
18730 VAR_INIT_STATUS_INITIALIZED);
18731 if (val == NULL)
18732 continue;
18733 if (die == NULL)
18734 die = gen_call_site_die (decl, subr_die, ca_loc);
18735 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18736 NULL_TREE);
18737 if (reg != NULL)
18738 add_AT_loc (cdie, DW_AT_location, reg);
18739 else if (tdie != NULL)
18740 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18741 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18742 if (next_arg != XEXP (arg, 1))
18744 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18745 if (mode == VOIDmode)
18746 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18747 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18748 0), 1),
18749 mode, VOIDmode,
18750 VAR_INIT_STATUS_INITIALIZED);
18751 if (val != NULL)
18752 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18755 if (die == NULL
18756 && (ca_loc->symbol_ref || tloc))
18757 die = gen_call_site_die (decl, subr_die, ca_loc);
18758 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18760 dw_loc_descr_ref tval = NULL;
18762 if (tloc != NULL_RTX)
18763 tval = mem_loc_descriptor (tloc,
18764 GET_MODE (tloc) == VOIDmode
18765 ? Pmode : GET_MODE (tloc),
18766 VOIDmode,
18767 VAR_INIT_STATUS_INITIALIZED);
18768 if (tval)
18769 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18770 else if (tlocc != NULL_RTX)
18772 tval = mem_loc_descriptor (tlocc,
18773 GET_MODE (tlocc) == VOIDmode
18774 ? Pmode : GET_MODE (tlocc),
18775 VOIDmode,
18776 VAR_INIT_STATUS_INITIALIZED);
18777 if (tval)
18778 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18779 tval);
18782 if (die != NULL)
18784 call_site_note_count++;
18785 if (ca_loc->tail_call_p)
18786 tail_call_site_note_count++;
18790 call_arg_locations = NULL;
18791 call_arg_loc_last = NULL;
18792 if (tail_call_site_count >= 0
18793 && tail_call_site_count == tail_call_site_note_count
18794 && !dwarf_strict)
18796 if (call_site_count >= 0
18797 && call_site_count == call_site_note_count)
18798 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18799 else
18800 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18802 call_site_count = -1;
18803 tail_call_site_count = -1;
18806 if (subr_die != old_die)
18807 /* Add the calling convention attribute if requested. */
18808 add_calling_convention_attribute (subr_die, decl);
18811 /* Returns a hash value for X (which really is a die_struct). */
18813 hashval_t
18814 block_die_hasher::hash (die_struct *d)
18816 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18819 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18820 as decl_id and die_parent of die_struct Y. */
18822 bool
18823 block_die_hasher::equal (die_struct *x, die_struct *y)
18825 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
18828 /* Generate a DIE to represent a declared data object.
18829 Either DECL or ORIGIN must be non-null. */
18831 static void
18832 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18834 HOST_WIDE_INT off = 0;
18835 tree com_decl;
18836 tree decl_or_origin = decl ? decl : origin;
18837 tree ultimate_origin;
18838 dw_die_ref var_die;
18839 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18840 dw_die_ref origin_die;
18841 bool declaration = (DECL_EXTERNAL (decl_or_origin)
18842 || class_or_namespace_scope_p (context_die));
18843 bool specialization_p = false;
18845 ultimate_origin = decl_ultimate_origin (decl_or_origin);
18846 if (decl || ultimate_origin)
18847 origin = ultimate_origin;
18848 com_decl = fortran_common (decl_or_origin, &off);
18850 /* Symbol in common gets emitted as a child of the common block, in the form
18851 of a data member. */
18852 if (com_decl)
18854 dw_die_ref com_die;
18855 dw_loc_list_ref loc;
18856 die_node com_die_arg;
18858 var_die = lookup_decl_die (decl_or_origin);
18859 if (var_die)
18861 if (get_AT (var_die, DW_AT_location) == NULL)
18863 loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18864 if (loc)
18866 if (off)
18868 /* Optimize the common case. */
18869 if (single_element_loc_list_p (loc)
18870 && loc->expr->dw_loc_opc == DW_OP_addr
18871 && loc->expr->dw_loc_next == NULL
18872 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18873 == SYMBOL_REF)
18875 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18876 loc->expr->dw_loc_oprnd1.v.val_addr
18877 = plus_constant (GET_MODE (x), x , off);
18879 else
18880 loc_list_plus_const (loc, off);
18882 add_AT_location_description (var_die, DW_AT_location, loc);
18883 remove_AT (var_die, DW_AT_declaration);
18886 return;
18889 if (common_block_die_table == NULL)
18890 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
18892 com_die_arg.decl_id = DECL_UID (com_decl);
18893 com_die_arg.die_parent = context_die;
18894 com_die = common_block_die_table->find (&com_die_arg);
18895 loc = loc_list_from_tree (com_decl, 2);
18896 if (com_die == NULL)
18898 const char *cnam
18899 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18900 die_node **slot;
18902 com_die = new_die (DW_TAG_common_block, context_die, decl);
18903 add_name_and_src_coords_attributes (com_die, com_decl);
18904 if (loc)
18906 add_AT_location_description (com_die, DW_AT_location, loc);
18907 /* Avoid sharing the same loc descriptor between
18908 DW_TAG_common_block and DW_TAG_variable. */
18909 loc = loc_list_from_tree (com_decl, 2);
18911 else if (DECL_EXTERNAL (decl))
18912 add_AT_flag (com_die, DW_AT_declaration, 1);
18913 if (want_pubnames ())
18914 add_pubname_string (cnam, com_die); /* ??? needed? */
18915 com_die->decl_id = DECL_UID (com_decl);
18916 slot = common_block_die_table->find_slot (com_die, INSERT);
18917 *slot = com_die;
18919 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18921 add_AT_location_description (com_die, DW_AT_location, loc);
18922 loc = loc_list_from_tree (com_decl, 2);
18923 remove_AT (com_die, DW_AT_declaration);
18925 var_die = new_die (DW_TAG_variable, com_die, decl);
18926 add_name_and_src_coords_attributes (var_die, decl);
18927 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
18928 context_die);
18929 add_AT_flag (var_die, DW_AT_external, 1);
18930 if (loc)
18932 if (off)
18934 /* Optimize the common case. */
18935 if (single_element_loc_list_p (loc)
18936 && loc->expr->dw_loc_opc == DW_OP_addr
18937 && loc->expr->dw_loc_next == NULL
18938 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18940 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18941 loc->expr->dw_loc_oprnd1.v.val_addr
18942 = plus_constant (GET_MODE (x), x, off);
18944 else
18945 loc_list_plus_const (loc, off);
18947 add_AT_location_description (var_die, DW_AT_location, loc);
18949 else if (DECL_EXTERNAL (decl))
18950 add_AT_flag (var_die, DW_AT_declaration, 1);
18951 equate_decl_number_to_die (decl, var_die);
18952 return;
18955 /* If the compiler emitted a definition for the DECL declaration
18956 and if we already emitted a DIE for it, don't emit a second
18957 DIE for it again. Allow re-declarations of DECLs that are
18958 inside functions, though. */
18959 if (old_die && declaration && !local_scope_p (context_die))
18960 return;
18962 /* For static data members, the declaration in the class is supposed
18963 to have DW_TAG_member tag; the specification should still be
18964 DW_TAG_variable referencing the DW_TAG_member DIE. */
18965 if (declaration && class_scope_p (context_die))
18966 var_die = new_die (DW_TAG_member, context_die, decl);
18967 else
18968 var_die = new_die (DW_TAG_variable, context_die, decl);
18970 origin_die = NULL;
18971 if (origin != NULL)
18972 origin_die = add_abstract_origin_attribute (var_die, origin);
18974 /* Loop unrolling can create multiple blocks that refer to the same
18975 static variable, so we must test for the DW_AT_declaration flag.
18977 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18978 copy decls and set the DECL_ABSTRACT_P flag on them instead of
18979 sharing them.
18981 ??? Duplicated blocks have been rewritten to use .debug_ranges.
18983 ??? The declare_in_namespace support causes us to get two DIEs for one
18984 variable, both of which are declarations. We want to avoid considering
18985 one to be a specification, so we must test that this DIE is not a
18986 declaration. */
18987 else if (old_die && TREE_STATIC (decl) && ! declaration
18988 && get_AT_flag (old_die, DW_AT_declaration) == 1)
18990 /* This is a definition of a C++ class level static. */
18991 add_AT_specification (var_die, old_die);
18992 specialization_p = true;
18993 if (DECL_NAME (decl))
18995 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18996 struct dwarf_file_data * file_index = lookup_filename (s.file);
18998 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18999 add_AT_file (var_die, DW_AT_decl_file, file_index);
19001 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19002 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19004 if (old_die->die_tag == DW_TAG_member)
19005 add_linkage_name (var_die, decl);
19008 else
19009 add_name_and_src_coords_attributes (var_die, decl);
19011 if ((origin == NULL && !specialization_p)
19012 || (origin != NULL
19013 && !DECL_ABSTRACT_P (decl_or_origin)
19014 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19015 decl_function_context
19016 (decl_or_origin))))
19018 tree type = TREE_TYPE (decl_or_origin);
19020 if (decl_by_reference_p (decl_or_origin))
19021 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19022 context_die);
19023 else
19024 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19025 context_die);
19028 if (origin == NULL && !specialization_p)
19030 if (TREE_PUBLIC (decl))
19031 add_AT_flag (var_die, DW_AT_external, 1);
19033 if (DECL_ARTIFICIAL (decl))
19034 add_AT_flag (var_die, DW_AT_artificial, 1);
19036 add_accessibility_attribute (var_die, decl);
19039 if (declaration)
19040 add_AT_flag (var_die, DW_AT_declaration, 1);
19042 if (decl && (DECL_ABSTRACT_P (decl) || declaration || old_die == NULL))
19043 equate_decl_number_to_die (decl, var_die);
19045 if (! declaration
19046 && (! DECL_ABSTRACT_P (decl_or_origin)
19047 /* Local static vars are shared between all clones/inlines,
19048 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19049 already set. */
19050 || (TREE_CODE (decl_or_origin) == VAR_DECL
19051 && TREE_STATIC (decl_or_origin)
19052 && DECL_RTL_SET_P (decl_or_origin)))
19053 /* When abstract origin already has DW_AT_location attribute, no need
19054 to add it again. */
19055 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19057 if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19058 && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19059 defer_location (decl_or_origin, var_die);
19060 else
19061 add_location_or_const_value_attribute (var_die, decl_or_origin,
19062 decl == NULL, DW_AT_location);
19063 add_pubname (decl_or_origin, var_die);
19065 else
19066 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19069 /* Generate a DIE to represent a named constant. */
19071 static void
19072 gen_const_die (tree decl, dw_die_ref context_die)
19074 dw_die_ref const_die;
19075 tree type = TREE_TYPE (decl);
19077 const_die = new_die (DW_TAG_constant, context_die, decl);
19078 add_name_and_src_coords_attributes (const_die, decl);
19079 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19080 if (TREE_PUBLIC (decl))
19081 add_AT_flag (const_die, DW_AT_external, 1);
19082 if (DECL_ARTIFICIAL (decl))
19083 add_AT_flag (const_die, DW_AT_artificial, 1);
19084 tree_add_const_value_attribute_for_decl (const_die, decl);
19087 /* Generate a DIE to represent a label identifier. */
19089 static void
19090 gen_label_die (tree decl, dw_die_ref context_die)
19092 tree origin = decl_ultimate_origin (decl);
19093 dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19094 rtx insn;
19095 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19097 if (origin != NULL)
19098 add_abstract_origin_attribute (lbl_die, origin);
19099 else
19100 add_name_and_src_coords_attributes (lbl_die, decl);
19102 if (DECL_ABSTRACT_P (decl))
19103 equate_decl_number_to_die (decl, lbl_die);
19104 else
19106 insn = DECL_RTL_IF_SET (decl);
19108 /* Deleted labels are programmer specified labels which have been
19109 eliminated because of various optimizations. We still emit them
19110 here so that it is possible to put breakpoints on them. */
19111 if (insn
19112 && (LABEL_P (insn)
19113 || ((NOTE_P (insn)
19114 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19116 /* When optimization is enabled (via -O) some parts of the compiler
19117 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19118 represent source-level labels which were explicitly declared by
19119 the user. This really shouldn't be happening though, so catch
19120 it if it ever does happen. */
19121 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19123 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19124 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19126 else if (insn
19127 && NOTE_P (insn)
19128 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19129 && CODE_LABEL_NUMBER (insn) != -1)
19131 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19132 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19137 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19138 attributes to the DIE for a block STMT, to describe where the inlined
19139 function was called from. This is similar to add_src_coords_attributes. */
19141 static inline void
19142 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19144 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19146 if (dwarf_version >= 3 || !dwarf_strict)
19148 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19149 add_AT_unsigned (die, DW_AT_call_line, s.line);
19154 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19155 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19157 static inline void
19158 add_high_low_attributes (tree stmt, dw_die_ref die)
19160 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19162 if (BLOCK_FRAGMENT_CHAIN (stmt)
19163 && (dwarf_version >= 3 || !dwarf_strict))
19165 tree chain, superblock = NULL_TREE;
19166 dw_die_ref pdie;
19167 dw_attr_ref attr = NULL;
19169 if (inlined_function_outer_scope_p (stmt))
19171 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19172 BLOCK_NUMBER (stmt));
19173 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19176 /* Optimize duplicate .debug_ranges lists or even tails of
19177 lists. If this BLOCK has same ranges as its supercontext,
19178 lookup DW_AT_ranges attribute in the supercontext (and
19179 recursively so), verify that the ranges_table contains the
19180 right values and use it instead of adding a new .debug_range. */
19181 for (chain = stmt, pdie = die;
19182 BLOCK_SAME_RANGE (chain);
19183 chain = BLOCK_SUPERCONTEXT (chain))
19185 dw_attr_ref new_attr;
19187 pdie = pdie->die_parent;
19188 if (pdie == NULL)
19189 break;
19190 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19191 break;
19192 new_attr = get_AT (pdie, DW_AT_ranges);
19193 if (new_attr == NULL
19194 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19195 break;
19196 attr = new_attr;
19197 superblock = BLOCK_SUPERCONTEXT (chain);
19199 if (attr != NULL
19200 && (ranges_table[attr->dw_attr_val.v.val_offset
19201 / 2 / DWARF2_ADDR_SIZE].num
19202 == BLOCK_NUMBER (superblock))
19203 && BLOCK_FRAGMENT_CHAIN (superblock))
19205 unsigned long off = attr->dw_attr_val.v.val_offset
19206 / 2 / DWARF2_ADDR_SIZE;
19207 unsigned long supercnt = 0, thiscnt = 0;
19208 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19209 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19211 ++supercnt;
19212 gcc_checking_assert (ranges_table[off + supercnt].num
19213 == BLOCK_NUMBER (chain));
19215 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19216 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19217 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19218 ++thiscnt;
19219 gcc_assert (supercnt >= thiscnt);
19220 add_AT_range_list (die, DW_AT_ranges,
19221 ((off + supercnt - thiscnt)
19222 * 2 * DWARF2_ADDR_SIZE),
19223 false);
19224 return;
19227 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19229 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19232 add_ranges (chain);
19233 chain = BLOCK_FRAGMENT_CHAIN (chain);
19235 while (chain);
19236 add_ranges (NULL);
19238 else
19240 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19241 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19242 BLOCK_NUMBER (stmt));
19243 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19244 BLOCK_NUMBER (stmt));
19245 add_AT_low_high_pc (die, label, label_high, false);
19249 /* Generate a DIE for a lexical block. */
19251 static void
19252 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19254 dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19256 if (call_arg_locations)
19258 if (block_map.length () <= BLOCK_NUMBER (stmt))
19259 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19260 block_map[BLOCK_NUMBER (stmt)] = stmt_die;
19263 if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19264 add_high_low_attributes (stmt, stmt_die);
19266 decls_for_scope (stmt, stmt_die, depth);
19269 /* Generate a DIE for an inlined subprogram. */
19271 static void
19272 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19274 tree decl;
19276 /* The instance of function that is effectively being inlined shall not
19277 be abstract. */
19278 gcc_assert (! BLOCK_ABSTRACT (stmt));
19280 decl = block_ultimate_origin (stmt);
19282 /* Emit info for the abstract instance first, if we haven't yet. We
19283 must emit this even if the block is abstract, otherwise when we
19284 emit the block below (or elsewhere), we may end up trying to emit
19285 a die whose origin die hasn't been emitted, and crashing. */
19286 dwarf2out_abstract_function (decl);
19288 if (! BLOCK_ABSTRACT (stmt))
19290 dw_die_ref subr_die
19291 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19293 if (call_arg_locations)
19295 if (block_map.length () <= BLOCK_NUMBER (stmt))
19296 block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
19297 block_map[BLOCK_NUMBER (stmt)] = subr_die;
19299 add_abstract_origin_attribute (subr_die, decl);
19300 if (TREE_ASM_WRITTEN (stmt))
19301 add_high_low_attributes (stmt, subr_die);
19302 add_call_src_coords_attributes (stmt, subr_die);
19304 decls_for_scope (stmt, subr_die, depth);
19308 /* Generate a DIE for a field in a record, or structure. */
19310 static void
19311 gen_field_die (tree decl, dw_die_ref context_die)
19313 dw_die_ref decl_die;
19315 if (TREE_TYPE (decl) == error_mark_node)
19316 return;
19318 decl_die = new_die (DW_TAG_member, context_die, decl);
19319 add_name_and_src_coords_attributes (decl_die, decl);
19320 add_type_attribute (decl_die, member_declared_type (decl),
19321 decl_quals (decl), context_die);
19323 if (DECL_BIT_FIELD_TYPE (decl))
19325 add_byte_size_attribute (decl_die, decl);
19326 add_bit_size_attribute (decl_die, decl);
19327 add_bit_offset_attribute (decl_die, decl);
19330 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19331 add_data_member_location_attribute (decl_die, decl);
19333 if (DECL_ARTIFICIAL (decl))
19334 add_AT_flag (decl_die, DW_AT_artificial, 1);
19336 add_accessibility_attribute (decl_die, decl);
19338 /* Equate decl number to die, so that we can look up this decl later on. */
19339 equate_decl_number_to_die (decl, decl_die);
19342 #if 0
19343 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19344 Use modified_type_die instead.
19345 We keep this code here just in case these types of DIEs may be needed to
19346 represent certain things in other languages (e.g. Pascal) someday. */
19348 static void
19349 gen_pointer_type_die (tree type, dw_die_ref context_die)
19351 dw_die_ref ptr_die
19352 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19354 equate_type_number_to_die (type, ptr_die);
19355 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19356 context_die);
19357 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19360 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19361 Use modified_type_die instead.
19362 We keep this code here just in case these types of DIEs may be needed to
19363 represent certain things in other languages (e.g. Pascal) someday. */
19365 static void
19366 gen_reference_type_die (tree type, dw_die_ref context_die)
19368 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19370 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19371 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19372 else
19373 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19375 equate_type_number_to_die (type, ref_die);
19376 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19377 context_die);
19378 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19380 #endif
19382 /* Generate a DIE for a pointer to a member type. */
19384 static void
19385 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19387 dw_die_ref ptr_die
19388 = new_die (DW_TAG_ptr_to_member_type,
19389 scope_die_for (type, context_die), type);
19391 equate_type_number_to_die (type, ptr_die);
19392 add_AT_die_ref (ptr_die, DW_AT_containing_type,
19393 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19394 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19395 context_die);
19398 typedef const char *dchar_p; /* For DEF_VEC_P. */
19400 static char *producer_string;
19402 /* Return a heap allocated producer string including command line options
19403 if -grecord-gcc-switches. */
19405 static char *
19406 gen_producer_string (void)
19408 size_t j;
19409 auto_vec<dchar_p> switches;
19410 const char *language_string = lang_hooks.name;
19411 char *producer, *tail;
19412 const char *p;
19413 size_t len = dwarf_record_gcc_switches ? 0 : 3;
19414 size_t plen = strlen (language_string) + 1 + strlen (version_string);
19416 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
19417 switch (save_decoded_options[j].opt_index)
19419 case OPT_o:
19420 case OPT_d:
19421 case OPT_dumpbase:
19422 case OPT_dumpdir:
19423 case OPT_auxbase:
19424 case OPT_auxbase_strip:
19425 case OPT_quiet:
19426 case OPT_version:
19427 case OPT_v:
19428 case OPT_w:
19429 case OPT_L:
19430 case OPT_D:
19431 case OPT_I:
19432 case OPT_U:
19433 case OPT_SPECIAL_unknown:
19434 case OPT_SPECIAL_ignore:
19435 case OPT_SPECIAL_program_name:
19436 case OPT_SPECIAL_input_file:
19437 case OPT_grecord_gcc_switches:
19438 case OPT_gno_record_gcc_switches:
19439 case OPT__output_pch_:
19440 case OPT_fdiagnostics_show_location_:
19441 case OPT_fdiagnostics_show_option:
19442 case OPT_fdiagnostics_show_caret:
19443 case OPT_fdiagnostics_color_:
19444 case OPT_fverbose_asm:
19445 case OPT____:
19446 case OPT__sysroot_:
19447 case OPT_nostdinc:
19448 case OPT_nostdinc__:
19449 /* Ignore these. */
19450 continue;
19451 default:
19452 if (cl_options[save_decoded_options[j].opt_index].flags
19453 & CL_NO_DWARF_RECORD)
19454 continue;
19455 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
19456 == '-');
19457 switch (save_decoded_options[j].canonical_option[0][1])
19459 case 'M':
19460 case 'i':
19461 case 'W':
19462 continue;
19463 case 'f':
19464 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
19465 "dump", 4) == 0)
19466 continue;
19467 break;
19468 default:
19469 break;
19471 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
19472 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
19473 break;
19476 producer = XNEWVEC (char, plen + 1 + len + 1);
19477 tail = producer;
19478 sprintf (tail, "%s %s", language_string, version_string);
19479 tail += plen;
19481 FOR_EACH_VEC_ELT (switches, j, p)
19483 len = strlen (p);
19484 *tail = ' ';
19485 memcpy (tail + 1, p, len);
19486 tail += len + 1;
19489 *tail = '\0';
19490 return producer;
19493 /* Generate the DIE for the compilation unit. */
19495 static dw_die_ref
19496 gen_compile_unit_die (const char *filename)
19498 dw_die_ref die;
19499 const char *language_string = lang_hooks.name;
19500 int language;
19502 die = new_die (DW_TAG_compile_unit, NULL, NULL);
19504 if (filename)
19506 add_name_attribute (die, filename);
19507 /* Don't add cwd for <built-in>. */
19508 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19509 add_comp_dir_attribute (die);
19512 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
19514 /* If our producer is LTO try to figure out a common language to use
19515 from the global list of translation units. */
19516 if (strcmp (language_string, "GNU GIMPLE") == 0)
19518 unsigned i;
19519 tree t;
19520 const char *common_lang = NULL;
19522 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
19524 if (!TRANSLATION_UNIT_LANGUAGE (t))
19525 continue;
19526 if (!common_lang)
19527 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19528 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19530 else if (strncmp (common_lang, "GNU C", 5) == 0
19531 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19532 /* Mixing C and C++ is ok, use C++ in that case. */
19533 common_lang = "GNU C++";
19534 else
19536 /* Fall back to C. */
19537 common_lang = NULL;
19538 break;
19542 if (common_lang)
19543 language_string = common_lang;
19546 language = DW_LANG_C89;
19547 if (strcmp (language_string, "GNU C++") == 0)
19548 language = DW_LANG_C_plus_plus;
19549 else if (strcmp (language_string, "GNU F77") == 0)
19550 language = DW_LANG_Fortran77;
19551 else if (strcmp (language_string, "GNU Pascal") == 0)
19552 language = DW_LANG_Pascal83;
19553 else if (dwarf_version >= 3 || !dwarf_strict)
19555 if (strcmp (language_string, "GNU Ada") == 0)
19556 language = DW_LANG_Ada95;
19557 else if (strcmp (language_string, "GNU Fortran") == 0)
19558 language = DW_LANG_Fortran95;
19559 else if (strcmp (language_string, "GNU Java") == 0)
19560 language = DW_LANG_Java;
19561 else if (strcmp (language_string, "GNU Objective-C") == 0)
19562 language = DW_LANG_ObjC;
19563 else if (strcmp (language_string, "GNU Objective-C++") == 0)
19564 language = DW_LANG_ObjC_plus_plus;
19565 else if (dwarf_version >= 5 || !dwarf_strict)
19567 if (strcmp (language_string, "GNU Go") == 0)
19568 language = DW_LANG_Go;
19571 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
19572 else if (strcmp (language_string, "GNU Fortran") == 0)
19573 language = DW_LANG_Fortran90;
19575 add_AT_unsigned (die, DW_AT_language, language);
19577 switch (language)
19579 case DW_LANG_Fortran77:
19580 case DW_LANG_Fortran90:
19581 case DW_LANG_Fortran95:
19582 /* Fortran has case insensitive identifiers and the front-end
19583 lowercases everything. */
19584 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19585 break;
19586 default:
19587 /* The default DW_ID_case_sensitive doesn't need to be specified. */
19588 break;
19590 return die;
19593 /* Generate the DIE for a base class. */
19595 static void
19596 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19598 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19600 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
19601 add_data_member_location_attribute (die, binfo);
19603 if (BINFO_VIRTUAL_P (binfo))
19604 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19606 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19607 children, otherwise the default is DW_ACCESS_public. In DWARF2
19608 the default has always been DW_ACCESS_private. */
19609 if (access == access_public_node)
19611 if (dwarf_version == 2
19612 || context_die->die_tag == DW_TAG_class_type)
19613 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19615 else if (access == access_protected_node)
19616 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19617 else if (dwarf_version > 2
19618 && context_die->die_tag != DW_TAG_class_type)
19619 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19622 /* Generate a DIE for a class member. */
19624 static void
19625 gen_member_die (tree type, dw_die_ref context_die)
19627 tree member;
19628 tree binfo = TYPE_BINFO (type);
19629 dw_die_ref child;
19631 /* If this is not an incomplete type, output descriptions of each of its
19632 members. Note that as we output the DIEs necessary to represent the
19633 members of this record or union type, we will also be trying to output
19634 DIEs to represent the *types* of those members. However the `type'
19635 function (above) will specifically avoid generating type DIEs for member
19636 types *within* the list of member DIEs for this (containing) type except
19637 for those types (of members) which are explicitly marked as also being
19638 members of this (containing) type themselves. The g++ front- end can
19639 force any given type to be treated as a member of some other (containing)
19640 type by setting the TYPE_CONTEXT of the given (member) type to point to
19641 the TREE node representing the appropriate (containing) type. */
19643 /* First output info about the base classes. */
19644 if (binfo)
19646 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19647 int i;
19648 tree base;
19650 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19651 gen_inheritance_die (base,
19652 (accesses ? (*accesses)[i] : access_public_node),
19653 context_die);
19656 /* Now output info about the data members and type members. */
19657 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19659 /* If we thought we were generating minimal debug info for TYPE
19660 and then changed our minds, some of the member declarations
19661 may have already been defined. Don't define them again, but
19662 do put them in the right order. */
19664 child = lookup_decl_die (member);
19665 if (child)
19666 splice_child_die (context_die, child);
19667 else
19668 gen_decl_die (member, NULL, context_die);
19671 /* Now output info about the function members (if any). */
19672 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19674 /* Don't include clones in the member list. */
19675 if (DECL_ABSTRACT_ORIGIN (member))
19676 continue;
19678 child = lookup_decl_die (member);
19679 if (child)
19680 splice_child_die (context_die, child);
19681 else
19682 gen_decl_die (member, NULL, context_die);
19686 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
19687 is set, we pretend that the type was never defined, so we only get the
19688 member DIEs needed by later specification DIEs. */
19690 static void
19691 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19692 enum debug_info_usage usage)
19694 dw_die_ref type_die = lookup_type_die (type);
19695 dw_die_ref scope_die = 0;
19696 int nested = 0;
19697 int complete = (TYPE_SIZE (type)
19698 && (! TYPE_STUB_DECL (type)
19699 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19700 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19701 complete = complete && should_emit_struct_debug (type, usage);
19703 if (type_die && ! complete)
19704 return;
19706 if (TYPE_CONTEXT (type) != NULL_TREE
19707 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19708 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19709 nested = 1;
19711 scope_die = scope_die_for (type, context_die);
19713 /* Generate child dies for template paramaters. */
19714 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19715 schedule_generic_params_dies_gen (type);
19717 if (! type_die || (nested && is_cu_die (scope_die)))
19718 /* First occurrence of type or toplevel definition of nested class. */
19720 dw_die_ref old_die = type_die;
19722 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19723 ? record_type_tag (type) : DW_TAG_union_type,
19724 scope_die, type);
19725 equate_type_number_to_die (type, type_die);
19726 if (old_die)
19727 add_AT_specification (type_die, old_die);
19728 else
19729 add_name_attribute (type_die, type_tag (type));
19731 else
19732 remove_AT (type_die, DW_AT_declaration);
19734 /* If this type has been completed, then give it a byte_size attribute and
19735 then give a list of members. */
19736 if (complete && !ns_decl)
19738 /* Prevent infinite recursion in cases where the type of some member of
19739 this type is expressed in terms of this type itself. */
19740 TREE_ASM_WRITTEN (type) = 1;
19741 add_byte_size_attribute (type_die, type);
19742 if (TYPE_STUB_DECL (type) != NULL_TREE)
19744 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19745 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19748 /* If the first reference to this type was as the return type of an
19749 inline function, then it may not have a parent. Fix this now. */
19750 if (type_die->die_parent == NULL)
19751 add_child_die (scope_die, type_die);
19753 push_decl_scope (type);
19754 gen_member_die (type, type_die);
19755 pop_decl_scope ();
19757 add_gnat_descriptive_type_attribute (type_die, type, context_die);
19758 if (TYPE_ARTIFICIAL (type))
19759 add_AT_flag (type_die, DW_AT_artificial, 1);
19761 /* GNU extension: Record what type our vtable lives in. */
19762 if (TYPE_VFIELD (type))
19764 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19766 gen_type_die (vtype, context_die);
19767 add_AT_die_ref (type_die, DW_AT_containing_type,
19768 lookup_type_die (vtype));
19771 else
19773 add_AT_flag (type_die, DW_AT_declaration, 1);
19775 /* We don't need to do this for function-local types. */
19776 if (TYPE_STUB_DECL (type)
19777 && ! decl_function_context (TYPE_STUB_DECL (type)))
19778 vec_safe_push (incomplete_types, type);
19781 if (get_AT (type_die, DW_AT_name))
19782 add_pubtype (type, type_die);
19785 /* Generate a DIE for a subroutine _type_. */
19787 static void
19788 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19790 tree return_type = TREE_TYPE (type);
19791 dw_die_ref subr_die
19792 = new_die (DW_TAG_subroutine_type,
19793 scope_die_for (type, context_die), type);
19795 equate_type_number_to_die (type, subr_die);
19796 add_prototyped_attribute (subr_die, type);
19797 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
19798 gen_formal_types_die (type, subr_die);
19800 if (get_AT (subr_die, DW_AT_name))
19801 add_pubtype (type, subr_die);
19804 /* Generate a DIE for a type definition. */
19806 static void
19807 gen_typedef_die (tree decl, dw_die_ref context_die)
19809 dw_die_ref type_die;
19810 tree origin;
19812 if (TREE_ASM_WRITTEN (decl))
19813 return;
19815 TREE_ASM_WRITTEN (decl) = 1;
19816 type_die = new_die (DW_TAG_typedef, context_die, decl);
19817 origin = decl_ultimate_origin (decl);
19818 if (origin != NULL)
19819 add_abstract_origin_attribute (type_die, origin);
19820 else
19822 tree type;
19824 add_name_and_src_coords_attributes (type_die, decl);
19825 if (DECL_ORIGINAL_TYPE (decl))
19827 type = DECL_ORIGINAL_TYPE (decl);
19829 gcc_assert (type != TREE_TYPE (decl));
19830 equate_type_number_to_die (TREE_TYPE (decl), type_die);
19832 else
19834 type = TREE_TYPE (decl);
19836 if (is_naming_typedef_decl (TYPE_NAME (type)))
19838 /* Here, we are in the case of decl being a typedef naming
19839 an anonymous type, e.g:
19840 typedef struct {...} foo;
19841 In that case TREE_TYPE (decl) is not a typedef variant
19842 type and TYPE_NAME of the anonymous type is set to the
19843 TYPE_DECL of the typedef. This construct is emitted by
19844 the C++ FE.
19846 TYPE is the anonymous struct named by the typedef
19847 DECL. As we need the DW_AT_type attribute of the
19848 DW_TAG_typedef to point to the DIE of TYPE, let's
19849 generate that DIE right away. add_type_attribute
19850 called below will then pick (via lookup_type_die) that
19851 anonymous struct DIE. */
19852 if (!TREE_ASM_WRITTEN (type))
19853 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19855 /* This is a GNU Extension. We are adding a
19856 DW_AT_linkage_name attribute to the DIE of the
19857 anonymous struct TYPE. The value of that attribute
19858 is the name of the typedef decl naming the anonymous
19859 struct. This greatly eases the work of consumers of
19860 this debug info. */
19861 add_linkage_attr (lookup_type_die (type), decl);
19865 add_type_attribute (type_die, type, decl_quals (decl), context_die);
19867 if (is_naming_typedef_decl (decl))
19868 /* We want that all subsequent calls to lookup_type_die with
19869 TYPE in argument yield the DW_TAG_typedef we have just
19870 created. */
19871 equate_type_number_to_die (type, type_die);
19873 add_accessibility_attribute (type_die, decl);
19876 if (DECL_ABSTRACT_P (decl))
19877 equate_decl_number_to_die (decl, type_die);
19879 if (get_AT (type_die, DW_AT_name))
19880 add_pubtype (decl, type_die);
19883 /* Generate a DIE for a struct, class, enum or union type. */
19885 static void
19886 gen_tagged_type_die (tree type,
19887 dw_die_ref context_die,
19888 enum debug_info_usage usage)
19890 int need_pop;
19892 if (type == NULL_TREE
19893 || !is_tagged_type (type))
19894 return;
19896 /* If this is a nested type whose containing class hasn't been written
19897 out yet, writing it out will cover this one, too. This does not apply
19898 to instantiations of member class templates; they need to be added to
19899 the containing class as they are generated. FIXME: This hurts the
19900 idea of combining type decls from multiple TUs, since we can't predict
19901 what set of template instantiations we'll get. */
19902 if (TYPE_CONTEXT (type)
19903 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19904 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19906 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19908 if (TREE_ASM_WRITTEN (type))
19909 return;
19911 /* If that failed, attach ourselves to the stub. */
19912 push_decl_scope (TYPE_CONTEXT (type));
19913 context_die = lookup_type_die (TYPE_CONTEXT (type));
19914 need_pop = 1;
19916 else if (TYPE_CONTEXT (type) != NULL_TREE
19917 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19919 /* If this type is local to a function that hasn't been written
19920 out yet, use a NULL context for now; it will be fixed up in
19921 decls_for_scope. */
19922 context_die = lookup_decl_die (TYPE_CONTEXT (type));
19923 /* A declaration DIE doesn't count; nested types need to go in the
19924 specification. */
19925 if (context_die && is_declaration_die (context_die))
19926 context_die = NULL;
19927 need_pop = 0;
19929 else
19931 context_die = declare_in_namespace (type, context_die);
19932 need_pop = 0;
19935 if (TREE_CODE (type) == ENUMERAL_TYPE)
19937 /* This might have been written out by the call to
19938 declare_in_namespace. */
19939 if (!TREE_ASM_WRITTEN (type))
19940 gen_enumeration_type_die (type, context_die);
19942 else
19943 gen_struct_or_union_type_die (type, context_die, usage);
19945 if (need_pop)
19946 pop_decl_scope ();
19948 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19949 it up if it is ever completed. gen_*_type_die will set it for us
19950 when appropriate. */
19953 /* Generate a type description DIE. */
19955 static void
19956 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19957 enum debug_info_usage usage)
19959 struct array_descr_info info;
19961 if (type == NULL_TREE || type == error_mark_node)
19962 return;
19964 if (TYPE_NAME (type) != NULL_TREE
19965 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19966 && is_redundant_typedef (TYPE_NAME (type))
19967 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19968 /* The DECL of this type is a typedef we don't want to emit debug
19969 info for but we want debug info for its underlying typedef.
19970 This can happen for e.g, the injected-class-name of a C++
19971 type. */
19972 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19974 /* If TYPE is a typedef type variant, let's generate debug info
19975 for the parent typedef which TYPE is a type of. */
19976 if (typedef_variant_p (type))
19978 if (TREE_ASM_WRITTEN (type))
19979 return;
19981 /* Prevent broken recursion; we can't hand off to the same type. */
19982 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19984 /* Give typedefs the right scope. */
19985 context_die = scope_die_for (type, context_die);
19987 TREE_ASM_WRITTEN (type) = 1;
19989 gen_decl_die (TYPE_NAME (type), NULL, context_die);
19990 return;
19993 /* If type is an anonymous tagged type named by a typedef, let's
19994 generate debug info for the typedef. */
19995 if (is_naming_typedef_decl (TYPE_NAME (type)))
19997 /* Use the DIE of the containing namespace as the parent DIE of
19998 the type description DIE we want to generate. */
19999 if (DECL_CONTEXT (TYPE_NAME (type))
20000 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20001 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20003 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20004 return;
20007 /* If this is an array type with hidden descriptor, handle it first. */
20008 if (!TREE_ASM_WRITTEN (type)
20009 && lang_hooks.types.get_array_descr_info
20010 && lang_hooks.types.get_array_descr_info (type, &info)
20011 && (dwarf_version >= 3 || !dwarf_strict))
20013 gen_descr_array_type_die (type, &info, context_die);
20014 TREE_ASM_WRITTEN (type) = 1;
20015 return;
20018 /* We are going to output a DIE to represent the unqualified version
20019 of this type (i.e. without any const or volatile qualifiers) so
20020 get the main variant (i.e. the unqualified version) of this type
20021 now. (Vectors are special because the debugging info is in the
20022 cloned type itself). */
20023 if (TREE_CODE (type) != VECTOR_TYPE)
20024 type = type_main_variant (type);
20026 if (TREE_ASM_WRITTEN (type))
20027 return;
20029 switch (TREE_CODE (type))
20031 case ERROR_MARK:
20032 break;
20034 case POINTER_TYPE:
20035 case REFERENCE_TYPE:
20036 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20037 ensures that the gen_type_die recursion will terminate even if the
20038 type is recursive. Recursive types are possible in Ada. */
20039 /* ??? We could perhaps do this for all types before the switch
20040 statement. */
20041 TREE_ASM_WRITTEN (type) = 1;
20043 /* For these types, all that is required is that we output a DIE (or a
20044 set of DIEs) to represent the "basis" type. */
20045 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20046 DINFO_USAGE_IND_USE);
20047 break;
20049 case OFFSET_TYPE:
20050 /* This code is used for C++ pointer-to-data-member types.
20051 Output a description of the relevant class type. */
20052 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20053 DINFO_USAGE_IND_USE);
20055 /* Output a description of the type of the object pointed to. */
20056 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20057 DINFO_USAGE_IND_USE);
20059 /* Now output a DIE to represent this pointer-to-data-member type
20060 itself. */
20061 gen_ptr_to_mbr_type_die (type, context_die);
20062 break;
20064 case FUNCTION_TYPE:
20065 /* Force out return type (in case it wasn't forced out already). */
20066 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20067 DINFO_USAGE_DIR_USE);
20068 gen_subroutine_type_die (type, context_die);
20069 break;
20071 case METHOD_TYPE:
20072 /* Force out return type (in case it wasn't forced out already). */
20073 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20074 DINFO_USAGE_DIR_USE);
20075 gen_subroutine_type_die (type, context_die);
20076 break;
20078 case ARRAY_TYPE:
20079 gen_array_type_die (type, context_die);
20080 break;
20082 case VECTOR_TYPE:
20083 gen_array_type_die (type, context_die);
20084 break;
20086 case ENUMERAL_TYPE:
20087 case RECORD_TYPE:
20088 case UNION_TYPE:
20089 case QUAL_UNION_TYPE:
20090 gen_tagged_type_die (type, context_die, usage);
20091 return;
20093 case VOID_TYPE:
20094 case INTEGER_TYPE:
20095 case REAL_TYPE:
20096 case FIXED_POINT_TYPE:
20097 case COMPLEX_TYPE:
20098 case BOOLEAN_TYPE:
20099 /* No DIEs needed for fundamental types. */
20100 break;
20102 case NULLPTR_TYPE:
20103 case LANG_TYPE:
20104 /* Just use DW_TAG_unspecified_type. */
20106 dw_die_ref type_die = lookup_type_die (type);
20107 if (type_die == NULL)
20109 tree name = TYPE_IDENTIFIER (type);
20110 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20111 type);
20112 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20113 equate_type_number_to_die (type, type_die);
20116 break;
20118 default:
20119 if (is_cxx_auto (type))
20121 tree name = TYPE_IDENTIFIER (type);
20122 dw_die_ref *die = (name == get_identifier ("auto")
20123 ? &auto_die : &decltype_auto_die);
20124 if (!*die)
20126 *die = new_die (DW_TAG_unspecified_type,
20127 comp_unit_die (), NULL_TREE);
20128 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20130 equate_type_number_to_die (type, *die);
20131 break;
20133 gcc_unreachable ();
20136 TREE_ASM_WRITTEN (type) = 1;
20139 static void
20140 gen_type_die (tree type, dw_die_ref context_die)
20142 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20145 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20146 things which are local to the given block. */
20148 static void
20149 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20151 int must_output_die = 0;
20152 bool inlined_func;
20154 /* Ignore blocks that are NULL. */
20155 if (stmt == NULL_TREE)
20156 return;
20158 inlined_func = inlined_function_outer_scope_p (stmt);
20160 /* If the block is one fragment of a non-contiguous block, do not
20161 process the variables, since they will have been done by the
20162 origin block. Do process subblocks. */
20163 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20165 tree sub;
20167 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20168 gen_block_die (sub, context_die, depth + 1);
20170 return;
20173 /* Determine if we need to output any Dwarf DIEs at all to represent this
20174 block. */
20175 if (inlined_func)
20176 /* The outer scopes for inlinings *must* always be represented. We
20177 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20178 must_output_die = 1;
20179 else
20181 /* Determine if this block directly contains any "significant"
20182 local declarations which we will need to output DIEs for. */
20183 if (debug_info_level > DINFO_LEVEL_TERSE)
20184 /* We are not in terse mode so *any* local declaration counts
20185 as being a "significant" one. */
20186 must_output_die = ((BLOCK_VARS (stmt) != NULL
20187 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20188 && (TREE_USED (stmt)
20189 || TREE_ASM_WRITTEN (stmt)
20190 || BLOCK_ABSTRACT (stmt)));
20191 else if ((TREE_USED (stmt)
20192 || TREE_ASM_WRITTEN (stmt)
20193 || BLOCK_ABSTRACT (stmt))
20194 && !dwarf2out_ignore_block (stmt))
20195 must_output_die = 1;
20198 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20199 DIE for any block which contains no significant local declarations at
20200 all. Rather, in such cases we just call `decls_for_scope' so that any
20201 needed Dwarf info for any sub-blocks will get properly generated. Note
20202 that in terse mode, our definition of what constitutes a "significant"
20203 local declaration gets restricted to include only inlined function
20204 instances and local (nested) function definitions. */
20205 if (must_output_die)
20207 if (inlined_func)
20209 /* If STMT block is abstract, that means we have been called
20210 indirectly from dwarf2out_abstract_function.
20211 That function rightfully marks the descendent blocks (of
20212 the abstract function it is dealing with) as being abstract,
20213 precisely to prevent us from emitting any
20214 DW_TAG_inlined_subroutine DIE as a descendent
20215 of an abstract function instance. So in that case, we should
20216 not call gen_inlined_subroutine_die.
20218 Later though, when cgraph asks dwarf2out to emit info
20219 for the concrete instance of the function decl into which
20220 the concrete instance of STMT got inlined, the later will lead
20221 to the generation of a DW_TAG_inlined_subroutine DIE. */
20222 if (! BLOCK_ABSTRACT (stmt))
20223 gen_inlined_subroutine_die (stmt, context_die, depth);
20225 else
20226 gen_lexical_block_die (stmt, context_die, depth);
20228 else
20229 decls_for_scope (stmt, context_die, depth);
20232 /* Process variable DECL (or variable with origin ORIGIN) within
20233 block STMT and add it to CONTEXT_DIE. */
20234 static void
20235 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20237 dw_die_ref die;
20238 tree decl_or_origin = decl ? decl : origin;
20240 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20241 die = lookup_decl_die (decl_or_origin);
20242 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20243 && TYPE_DECL_IS_STUB (decl_or_origin))
20244 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20245 else
20246 die = NULL;
20248 if (die != NULL && die->die_parent == NULL)
20249 add_child_die (context_die, die);
20250 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20251 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20252 stmt, context_die);
20253 else
20254 gen_decl_die (decl, origin, context_die);
20257 /* Generate all of the decls declared within a given scope and (recursively)
20258 all of its sub-blocks. */
20260 static void
20261 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20263 tree decl;
20264 unsigned int i;
20265 tree subblocks;
20267 /* Ignore NULL blocks. */
20268 if (stmt == NULL_TREE)
20269 return;
20271 /* Output the DIEs to represent all of the data objects and typedefs
20272 declared directly within this block but not within any nested
20273 sub-blocks. Also, nested function and tag DIEs have been
20274 generated with a parent of NULL; fix that up now. We don't
20275 have to do this if we're at -g1. */
20276 if (debug_info_level > DINFO_LEVEL_TERSE)
20278 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20279 process_scope_var (stmt, decl, NULL_TREE, context_die);
20280 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20281 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20282 context_die);
20285 /* Even if we're at -g1, we need to process the subblocks in order to get
20286 inlined call information. */
20288 /* Output the DIEs to represent all sub-blocks (and the items declared
20289 therein) of this block. */
20290 for (subblocks = BLOCK_SUBBLOCKS (stmt);
20291 subblocks != NULL;
20292 subblocks = BLOCK_CHAIN (subblocks))
20293 gen_block_die (subblocks, context_die, depth + 1);
20296 /* Is this a typedef we can avoid emitting? */
20298 static inline int
20299 is_redundant_typedef (const_tree decl)
20301 if (TYPE_DECL_IS_STUB (decl))
20302 return 1;
20304 if (DECL_ARTIFICIAL (decl)
20305 && DECL_CONTEXT (decl)
20306 && is_tagged_type (DECL_CONTEXT (decl))
20307 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20308 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20309 /* Also ignore the artificial member typedef for the class name. */
20310 return 1;
20312 return 0;
20315 /* Return TRUE if TYPE is a typedef that names a type for linkage
20316 purposes. This kind of typedefs is produced by the C++ FE for
20317 constructs like:
20319 typedef struct {...} foo;
20321 In that case, there is no typedef variant type produced for foo.
20322 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20323 struct type. */
20325 static bool
20326 is_naming_typedef_decl (const_tree decl)
20328 if (decl == NULL_TREE
20329 || TREE_CODE (decl) != TYPE_DECL
20330 || !is_tagged_type (TREE_TYPE (decl))
20331 || DECL_IS_BUILTIN (decl)
20332 || is_redundant_typedef (decl)
20333 /* It looks like Ada produces TYPE_DECLs that are very similar
20334 to C++ naming typedefs but that have different
20335 semantics. Let's be specific to c++ for now. */
20336 || !is_cxx ())
20337 return FALSE;
20339 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20340 && TYPE_NAME (TREE_TYPE (decl)) == decl
20341 && (TYPE_STUB_DECL (TREE_TYPE (decl))
20342 != TYPE_NAME (TREE_TYPE (decl))));
20345 /* Returns the DIE for a context. */
20347 static inline dw_die_ref
20348 get_context_die (tree context)
20350 if (context)
20352 /* Find die that represents this context. */
20353 if (TYPE_P (context))
20355 context = TYPE_MAIN_VARIANT (context);
20356 return strip_naming_typedef (context, force_type_die (context));
20358 else
20359 return force_decl_die (context);
20361 return comp_unit_die ();
20364 /* Returns the DIE for decl. A DIE will always be returned. */
20366 static dw_die_ref
20367 force_decl_die (tree decl)
20369 dw_die_ref decl_die;
20370 unsigned saved_external_flag;
20371 tree save_fn = NULL_TREE;
20372 decl_die = lookup_decl_die (decl);
20373 if (!decl_die)
20375 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20377 decl_die = lookup_decl_die (decl);
20378 if (decl_die)
20379 return decl_die;
20381 switch (TREE_CODE (decl))
20383 case FUNCTION_DECL:
20384 /* Clear current_function_decl, so that gen_subprogram_die thinks
20385 that this is a declaration. At this point, we just want to force
20386 declaration die. */
20387 save_fn = current_function_decl;
20388 current_function_decl = NULL_TREE;
20389 gen_subprogram_die (decl, context_die);
20390 current_function_decl = save_fn;
20391 break;
20393 case VAR_DECL:
20394 /* Set external flag to force declaration die. Restore it after
20395 gen_decl_die() call. */
20396 saved_external_flag = DECL_EXTERNAL (decl);
20397 DECL_EXTERNAL (decl) = 1;
20398 gen_decl_die (decl, NULL, context_die);
20399 DECL_EXTERNAL (decl) = saved_external_flag;
20400 break;
20402 case NAMESPACE_DECL:
20403 if (dwarf_version >= 3 || !dwarf_strict)
20404 dwarf2out_decl (decl);
20405 else
20406 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
20407 decl_die = comp_unit_die ();
20408 break;
20410 case TRANSLATION_UNIT_DECL:
20411 decl_die = comp_unit_die ();
20412 break;
20414 default:
20415 gcc_unreachable ();
20418 /* We should be able to find the DIE now. */
20419 if (!decl_die)
20420 decl_die = lookup_decl_die (decl);
20421 gcc_assert (decl_die);
20424 return decl_die;
20427 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
20428 always returned. */
20430 static dw_die_ref
20431 force_type_die (tree type)
20433 dw_die_ref type_die;
20435 type_die = lookup_type_die (type);
20436 if (!type_die)
20438 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20440 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
20441 context_die);
20442 gcc_assert (type_die);
20444 return type_die;
20447 /* Force out any required namespaces to be able to output DECL,
20448 and return the new context_die for it, if it's changed. */
20450 static dw_die_ref
20451 setup_namespace_context (tree thing, dw_die_ref context_die)
20453 tree context = (DECL_P (thing)
20454 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20455 if (context && TREE_CODE (context) == NAMESPACE_DECL)
20456 /* Force out the namespace. */
20457 context_die = force_decl_die (context);
20459 return context_die;
20462 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20463 type) within its namespace, if appropriate.
20465 For compatibility with older debuggers, namespace DIEs only contain
20466 declarations; all definitions are emitted at CU scope. */
20468 static dw_die_ref
20469 declare_in_namespace (tree thing, dw_die_ref context_die)
20471 dw_die_ref ns_context;
20473 if (debug_info_level <= DINFO_LEVEL_TERSE)
20474 return context_die;
20476 /* If this decl is from an inlined function, then don't try to emit it in its
20477 namespace, as we will get confused. It would have already been emitted
20478 when the abstract instance of the inline function was emitted anyways. */
20479 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20480 return context_die;
20482 ns_context = setup_namespace_context (thing, context_die);
20484 if (ns_context != context_die)
20486 if (is_fortran ())
20487 return ns_context;
20488 if (DECL_P (thing))
20489 gen_decl_die (thing, NULL, ns_context);
20490 else
20491 gen_type_die (thing, ns_context);
20493 return context_die;
20496 /* Generate a DIE for a namespace or namespace alias. */
20498 static void
20499 gen_namespace_die (tree decl, dw_die_ref context_die)
20501 dw_die_ref namespace_die;
20503 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20504 they are an alias of. */
20505 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20507 /* Output a real namespace or module. */
20508 context_die = setup_namespace_context (decl, comp_unit_die ());
20509 namespace_die = new_die (is_fortran ()
20510 ? DW_TAG_module : DW_TAG_namespace,
20511 context_die, decl);
20512 /* For Fortran modules defined in different CU don't add src coords. */
20513 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20515 const char *name = dwarf2_name (decl, 0);
20516 if (name)
20517 add_name_attribute (namespace_die, name);
20519 else
20520 add_name_and_src_coords_attributes (namespace_die, decl);
20521 if (DECL_EXTERNAL (decl))
20522 add_AT_flag (namespace_die, DW_AT_declaration, 1);
20523 equate_decl_number_to_die (decl, namespace_die);
20525 else
20527 /* Output a namespace alias. */
20529 /* Force out the namespace we are an alias of, if necessary. */
20530 dw_die_ref origin_die
20531 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20533 if (DECL_FILE_SCOPE_P (decl)
20534 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20535 context_die = setup_namespace_context (decl, comp_unit_die ());
20536 /* Now create the namespace alias DIE. */
20537 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20538 add_name_and_src_coords_attributes (namespace_die, decl);
20539 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20540 equate_decl_number_to_die (decl, namespace_die);
20542 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
20543 if (want_pubnames ())
20544 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
20547 /* Generate Dwarf debug information for a decl described by DECL.
20548 The return value is currently only meaningful for PARM_DECLs,
20549 for all other decls it returns NULL. */
20551 static dw_die_ref
20552 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20554 tree decl_or_origin = decl ? decl : origin;
20555 tree class_origin = NULL, ultimate_origin;
20557 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20558 return NULL;
20560 switch (TREE_CODE (decl_or_origin))
20562 case ERROR_MARK:
20563 break;
20565 case CONST_DECL:
20566 if (!is_fortran () && !is_ada ())
20568 /* The individual enumerators of an enum type get output when we output
20569 the Dwarf representation of the relevant enum type itself. */
20570 break;
20573 /* Emit its type. */
20574 gen_type_die (TREE_TYPE (decl), context_die);
20576 /* And its containing namespace. */
20577 context_die = declare_in_namespace (decl, context_die);
20579 gen_const_die (decl, context_die);
20580 break;
20582 case FUNCTION_DECL:
20583 /* Don't output any DIEs to represent mere function declarations,
20584 unless they are class members or explicit block externs. */
20585 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20586 && DECL_FILE_SCOPE_P (decl_or_origin)
20587 && (current_function_decl == NULL_TREE
20588 || DECL_ARTIFICIAL (decl_or_origin)))
20589 break;
20591 #if 0
20592 /* FIXME */
20593 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20594 on local redeclarations of global functions. That seems broken. */
20595 if (current_function_decl != decl)
20596 /* This is only a declaration. */;
20597 #endif
20599 /* If we're emitting a clone, emit info for the abstract instance. */
20600 if (origin || DECL_ORIGIN (decl) != decl)
20601 dwarf2out_abstract_function (origin
20602 ? DECL_ORIGIN (origin)
20603 : DECL_ABSTRACT_ORIGIN (decl));
20605 /* If we're emitting an out-of-line copy of an inline function,
20606 emit info for the abstract instance and set up to refer to it. */
20607 else if (cgraph_function_possibly_inlined_p (decl)
20608 && ! DECL_ABSTRACT_P (decl)
20609 && ! class_or_namespace_scope_p (context_die)
20610 /* dwarf2out_abstract_function won't emit a die if this is just
20611 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
20612 that case, because that works only if we have a die. */
20613 && DECL_INITIAL (decl) != NULL_TREE)
20615 dwarf2out_abstract_function (decl);
20616 set_decl_origin_self (decl);
20619 /* Otherwise we're emitting the primary DIE for this decl. */
20620 else if (debug_info_level > DINFO_LEVEL_TERSE)
20622 /* Before we describe the FUNCTION_DECL itself, make sure that we
20623 have its containing type. */
20624 if (!origin)
20625 origin = decl_class_context (decl);
20626 if (origin != NULL_TREE)
20627 gen_type_die (origin, context_die);
20629 /* And its return type. */
20630 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20632 /* And its virtual context. */
20633 if (DECL_VINDEX (decl) != NULL_TREE)
20634 gen_type_die (DECL_CONTEXT (decl), context_die);
20636 /* Make sure we have a member DIE for decl. */
20637 if (origin != NULL_TREE)
20638 gen_type_die_for_member (origin, decl, context_die);
20640 /* And its containing namespace. */
20641 context_die = declare_in_namespace (decl, context_die);
20644 /* Now output a DIE to represent the function itself. */
20645 if (decl)
20646 gen_subprogram_die (decl, context_die);
20647 break;
20649 case TYPE_DECL:
20650 /* If we are in terse mode, don't generate any DIEs to represent any
20651 actual typedefs. */
20652 if (debug_info_level <= DINFO_LEVEL_TERSE)
20653 break;
20655 /* In the special case of a TYPE_DECL node representing the declaration
20656 of some type tag, if the given TYPE_DECL is marked as having been
20657 instantiated from some other (original) TYPE_DECL node (e.g. one which
20658 was generated within the original definition of an inline function) we
20659 used to generate a special (abbreviated) DW_TAG_structure_type,
20660 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
20661 should be actually referencing those DIEs, as variable DIEs with that
20662 type would be emitted already in the abstract origin, so it was always
20663 removed during unused type prunning. Don't add anything in this
20664 case. */
20665 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20666 break;
20668 if (is_redundant_typedef (decl))
20669 gen_type_die (TREE_TYPE (decl), context_die);
20670 else
20671 /* Output a DIE to represent the typedef itself. */
20672 gen_typedef_die (decl, context_die);
20673 break;
20675 case LABEL_DECL:
20676 if (debug_info_level >= DINFO_LEVEL_NORMAL)
20677 gen_label_die (decl, context_die);
20678 break;
20680 case VAR_DECL:
20681 case RESULT_DECL:
20682 /* If we are in terse mode, don't generate any DIEs to represent any
20683 variable declarations or definitions. */
20684 if (debug_info_level <= DINFO_LEVEL_TERSE)
20685 break;
20687 /* Output any DIEs that are needed to specify the type of this data
20688 object. */
20689 if (decl_by_reference_p (decl_or_origin))
20690 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20691 else
20692 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20694 /* And its containing type. */
20695 class_origin = decl_class_context (decl_or_origin);
20696 if (class_origin != NULL_TREE)
20697 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20699 /* And its containing namespace. */
20700 context_die = declare_in_namespace (decl_or_origin, context_die);
20702 /* Now output the DIE to represent the data object itself. This gets
20703 complicated because of the possibility that the VAR_DECL really
20704 represents an inlined instance of a formal parameter for an inline
20705 function. */
20706 ultimate_origin = decl_ultimate_origin (decl_or_origin);
20707 if (ultimate_origin != NULL_TREE
20708 && TREE_CODE (ultimate_origin) == PARM_DECL)
20709 gen_formal_parameter_die (decl, origin,
20710 true /* Emit name attribute. */,
20711 context_die);
20712 else
20713 gen_variable_die (decl, origin, context_die);
20714 break;
20716 case FIELD_DECL:
20717 /* Ignore the nameless fields that are used to skip bits but handle C++
20718 anonymous unions and structs. */
20719 if (DECL_NAME (decl) != NULL_TREE
20720 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20721 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20723 gen_type_die (member_declared_type (decl), context_die);
20724 gen_field_die (decl, context_die);
20726 break;
20728 case PARM_DECL:
20729 if (DECL_BY_REFERENCE (decl_or_origin))
20730 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20731 else
20732 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20733 return gen_formal_parameter_die (decl, origin,
20734 true /* Emit name attribute. */,
20735 context_die);
20737 case NAMESPACE_DECL:
20738 case IMPORTED_DECL:
20739 if (dwarf_version >= 3 || !dwarf_strict)
20740 gen_namespace_die (decl, context_die);
20741 break;
20743 case NAMELIST_DECL:
20744 gen_namelist_decl (DECL_NAME (decl), context_die,
20745 NAMELIST_DECL_ASSOCIATED_DECL (decl));
20746 break;
20748 default:
20749 /* Probably some frontend-internal decl. Assume we don't care. */
20750 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20751 break;
20754 return NULL;
20757 /* Output debug information for global decl DECL. Called from toplev.c after
20758 compilation proper has finished. */
20760 static void
20761 dwarf2out_global_decl (tree decl)
20763 /* Output DWARF2 information for file-scope tentative data object
20764 declarations, file-scope (extern) function declarations (which
20765 had no corresponding body) and file-scope tagged type declarations
20766 and definitions which have not yet been forced out. */
20767 if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20768 dwarf2out_decl (decl);
20771 /* Output debug information for type decl DECL. Called from toplev.c
20772 and from language front ends (to record built-in types). */
20773 static void
20774 dwarf2out_type_decl (tree decl, int local)
20776 if (!local)
20777 dwarf2out_decl (decl);
20780 /* Output debug information for imported module or decl DECL.
20781 NAME is non-NULL name in the lexical block if the decl has been renamed.
20782 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20783 that DECL belongs to.
20784 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
20785 static void
20786 dwarf2out_imported_module_or_decl_1 (tree decl,
20787 tree name,
20788 tree lexical_block,
20789 dw_die_ref lexical_block_die)
20791 expanded_location xloc;
20792 dw_die_ref imported_die = NULL;
20793 dw_die_ref at_import_die;
20795 if (TREE_CODE (decl) == IMPORTED_DECL)
20797 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20798 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20799 gcc_assert (decl);
20801 else
20802 xloc = expand_location (input_location);
20804 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20806 at_import_die = force_type_die (TREE_TYPE (decl));
20807 /* For namespace N { typedef void T; } using N::T; base_type_die
20808 returns NULL, but DW_TAG_imported_declaration requires
20809 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
20810 if (!at_import_die)
20812 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20813 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20814 at_import_die = lookup_type_die (TREE_TYPE (decl));
20815 gcc_assert (at_import_die);
20818 else
20820 at_import_die = lookup_decl_die (decl);
20821 if (!at_import_die)
20823 /* If we're trying to avoid duplicate debug info, we may not have
20824 emitted the member decl for this field. Emit it now. */
20825 if (TREE_CODE (decl) == FIELD_DECL)
20827 tree type = DECL_CONTEXT (decl);
20829 if (TYPE_CONTEXT (type)
20830 && TYPE_P (TYPE_CONTEXT (type))
20831 && !should_emit_struct_debug (TYPE_CONTEXT (type),
20832 DINFO_USAGE_DIR_USE))
20833 return;
20834 gen_type_die_for_member (type, decl,
20835 get_context_die (TYPE_CONTEXT (type)));
20837 if (TREE_CODE (decl) == NAMELIST_DECL)
20838 at_import_die = gen_namelist_decl (DECL_NAME (decl),
20839 get_context_die (DECL_CONTEXT (decl)),
20840 NULL_TREE);
20841 else
20842 at_import_die = force_decl_die (decl);
20846 if (TREE_CODE (decl) == NAMESPACE_DECL)
20848 if (dwarf_version >= 3 || !dwarf_strict)
20849 imported_die = new_die (DW_TAG_imported_module,
20850 lexical_block_die,
20851 lexical_block);
20852 else
20853 return;
20855 else
20856 imported_die = new_die (DW_TAG_imported_declaration,
20857 lexical_block_die,
20858 lexical_block);
20860 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20861 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20862 if (name)
20863 add_AT_string (imported_die, DW_AT_name,
20864 IDENTIFIER_POINTER (name));
20865 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20868 /* Output debug information for imported module or decl DECL.
20869 NAME is non-NULL name in context if the decl has been renamed.
20870 CHILD is true if decl is one of the renamed decls as part of
20871 importing whole module. */
20873 static void
20874 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20875 bool child)
20877 /* dw_die_ref at_import_die; */
20878 dw_die_ref scope_die;
20880 if (debug_info_level <= DINFO_LEVEL_TERSE)
20881 return;
20883 gcc_assert (decl);
20885 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20886 We need decl DIE for reference and scope die. First, get DIE for the decl
20887 itself. */
20889 /* Get the scope die for decl context. Use comp_unit_die for global module
20890 or decl. If die is not found for non globals, force new die. */
20891 if (context
20892 && TYPE_P (context)
20893 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20894 return;
20896 if (!(dwarf_version >= 3 || !dwarf_strict))
20897 return;
20899 scope_die = get_context_die (context);
20901 if (child)
20903 gcc_assert (scope_die->die_child);
20904 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20905 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20906 scope_die = scope_die->die_child;
20909 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
20910 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20914 /* Output debug information for namelists. */
20916 static dw_die_ref
20917 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
20919 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
20920 tree value;
20921 unsigned i;
20923 if (debug_info_level <= DINFO_LEVEL_TERSE)
20924 return NULL;
20926 gcc_assert (scope_die != NULL);
20927 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
20928 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
20930 /* If there are no item_decls, we have a nondefining namelist, e.g.
20931 with USE association; hence, set DW_AT_declaration. */
20932 if (item_decls == NULL_TREE)
20934 add_AT_flag (nml_die, DW_AT_declaration, 1);
20935 return nml_die;
20938 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
20940 nml_item_ref_die = lookup_decl_die (value);
20941 if (!nml_item_ref_die)
20942 nml_item_ref_die = force_decl_die (value);
20944 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
20945 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
20947 return nml_die;
20951 /* Write the debugging output for DECL. */
20953 static void
20954 dwarf2out_decl (tree decl)
20956 dw_die_ref context_die = comp_unit_die ();
20958 switch (TREE_CODE (decl))
20960 case ERROR_MARK:
20961 return;
20963 case FUNCTION_DECL:
20964 /* What we would really like to do here is to filter out all mere
20965 file-scope declarations of file-scope functions which are never
20966 referenced later within this translation unit (and keep all of ones
20967 that *are* referenced later on) but we aren't clairvoyant, so we have
20968 no idea which functions will be referenced in the future (i.e. later
20969 on within the current translation unit). So here we just ignore all
20970 file-scope function declarations which are not also definitions. If
20971 and when the debugger needs to know something about these functions,
20972 it will have to hunt around and find the DWARF information associated
20973 with the definition of the function.
20975 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20976 nodes represent definitions and which ones represent mere
20977 declarations. We have to check DECL_INITIAL instead. That's because
20978 the C front-end supports some weird semantics for "extern inline"
20979 function definitions. These can get inlined within the current
20980 translation unit (and thus, we need to generate Dwarf info for their
20981 abstract instances so that the Dwarf info for the concrete inlined
20982 instances can have something to refer to) but the compiler never
20983 generates any out-of-lines instances of such things (despite the fact
20984 that they *are* definitions).
20986 The important point is that the C front-end marks these "extern
20987 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20988 them anyway. Note that the C++ front-end also plays some similar games
20989 for inline function definitions appearing within include files which
20990 also contain `#pragma interface' pragmas.
20992 If we are called from dwarf2out_abstract_function output a DIE
20993 anyway. We can end up here this way with early inlining and LTO
20994 where the inlined function is output in a different LTRANS unit
20995 or not at all. */
20996 if (DECL_INITIAL (decl) == NULL_TREE
20997 && ! DECL_ABSTRACT_P (decl))
20998 return;
21000 /* If we're a nested function, initially use a parent of NULL; if we're
21001 a plain function, this will be fixed up in decls_for_scope. If
21002 we're a method, it will be ignored, since we already have a DIE. */
21003 if (decl_function_context (decl)
21004 /* But if we're in terse mode, we don't care about scope. */
21005 && debug_info_level > DINFO_LEVEL_TERSE)
21006 context_die = NULL;
21007 break;
21009 case VAR_DECL:
21010 /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21011 declaration and if the declaration was never even referenced from
21012 within this entire compilation unit. We suppress these DIEs in
21013 order to save space in the .debug section (by eliminating entries
21014 which are probably useless). Note that we must not suppress
21015 block-local extern declarations (whether used or not) because that
21016 would screw-up the debugger's name lookup mechanism and cause it to
21017 miss things which really ought to be in scope at a given point. */
21018 if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21019 return;
21021 /* For local statics lookup proper context die. */
21022 if (TREE_STATIC (decl)
21023 && DECL_CONTEXT (decl)
21024 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
21025 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21027 /* If we are in terse mode, don't generate any DIEs to represent any
21028 variable declarations or definitions. */
21029 if (debug_info_level <= DINFO_LEVEL_TERSE)
21030 return;
21031 break;
21033 case CONST_DECL:
21034 if (debug_info_level <= DINFO_LEVEL_TERSE)
21035 return;
21036 if (!is_fortran () && !is_ada ())
21037 return;
21038 if (TREE_STATIC (decl) && decl_function_context (decl))
21039 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21040 break;
21042 case NAMESPACE_DECL:
21043 case IMPORTED_DECL:
21044 if (debug_info_level <= DINFO_LEVEL_TERSE)
21045 return;
21046 if (lookup_decl_die (decl) != NULL)
21047 return;
21048 break;
21050 case TYPE_DECL:
21051 /* Don't emit stubs for types unless they are needed by other DIEs. */
21052 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21053 return;
21055 /* Don't bother trying to generate any DIEs to represent any of the
21056 normal built-in types for the language we are compiling. */
21057 if (DECL_IS_BUILTIN (decl))
21058 return;
21060 /* If we are in terse mode, don't generate any DIEs for types. */
21061 if (debug_info_level <= DINFO_LEVEL_TERSE)
21062 return;
21064 /* If we're a function-scope tag, initially use a parent of NULL;
21065 this will be fixed up in decls_for_scope. */
21066 if (decl_function_context (decl))
21067 context_die = NULL;
21069 break;
21071 case NAMELIST_DECL:
21072 break;
21074 default:
21075 return;
21078 gen_decl_die (decl, NULL, context_die);
21081 /* Write the debugging output for DECL. */
21083 static void
21084 dwarf2out_function_decl (tree decl)
21086 dwarf2out_decl (decl);
21087 call_arg_locations = NULL;
21088 call_arg_loc_last = NULL;
21089 call_site_count = -1;
21090 tail_call_site_count = -1;
21091 block_map.release ();
21092 decl_loc_table->empty ();
21093 cached_dw_loc_list_table->empty ();
21096 /* Output a marker (i.e. a label) for the beginning of the generated code for
21097 a lexical block. */
21099 static void
21100 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21101 unsigned int blocknum)
21103 switch_to_section (current_function_section ());
21104 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21107 /* Output a marker (i.e. a label) for the end of the generated code for a
21108 lexical block. */
21110 static void
21111 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21113 switch_to_section (current_function_section ());
21114 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21117 /* Returns nonzero if it is appropriate not to emit any debugging
21118 information for BLOCK, because it doesn't contain any instructions.
21120 Don't allow this for blocks with nested functions or local classes
21121 as we would end up with orphans, and in the presence of scheduling
21122 we may end up calling them anyway. */
21124 static bool
21125 dwarf2out_ignore_block (const_tree block)
21127 tree decl;
21128 unsigned int i;
21130 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21131 if (TREE_CODE (decl) == FUNCTION_DECL
21132 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21133 return 0;
21134 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21136 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21137 if (TREE_CODE (decl) == FUNCTION_DECL
21138 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21139 return 0;
21142 return 1;
21145 /* Hash table routines for file_hash. */
21147 bool
21148 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21150 return filename_cmp (p1->filename, p2) == 0;
21153 hashval_t
21154 dwarf_file_hasher::hash (dwarf_file_data *p)
21156 return htab_hash_string (p->filename);
21159 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21160 dwarf2out.c) and return its "index". The index of each (known) filename is
21161 just a unique number which is associated with only that one filename. We
21162 need such numbers for the sake of generating labels (in the .debug_sfnames
21163 section) and references to those files numbers (in the .debug_srcinfo
21164 and.debug_macinfo sections). If the filename given as an argument is not
21165 found in our current list, add it to the list and assign it the next
21166 available unique index number. In order to speed up searches, we remember
21167 the index of the filename was looked up last. This handles the majority of
21168 all searches. */
21170 static struct dwarf_file_data *
21171 lookup_filename (const char *file_name)
21173 struct dwarf_file_data * created;
21175 /* Check to see if the file name that was searched on the previous
21176 call matches this file name. If so, return the index. */
21177 if (file_table_last_lookup
21178 && (file_name == file_table_last_lookup->filename
21179 || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21180 return file_table_last_lookup;
21182 /* Didn't match the previous lookup, search the table. */
21183 dwarf_file_data **slot
21184 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
21185 INSERT);
21186 if (*slot)
21187 return *slot;
21189 created = ggc_alloc<dwarf_file_data> ();
21190 created->filename = file_name;
21191 created->emitted_number = 0;
21192 *slot = created;
21193 return created;
21196 /* If the assembler will construct the file table, then translate the compiler
21197 internal file table number into the assembler file table number, and emit
21198 a .file directive if we haven't already emitted one yet. The file table
21199 numbers are different because we prune debug info for unused variables and
21200 types, which may include filenames. */
21202 static int
21203 maybe_emit_file (struct dwarf_file_data * fd)
21205 if (! fd->emitted_number)
21207 if (last_emitted_file)
21208 fd->emitted_number = last_emitted_file->emitted_number + 1;
21209 else
21210 fd->emitted_number = 1;
21211 last_emitted_file = fd;
21213 if (DWARF2_ASM_LINE_DEBUG_INFO)
21215 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21216 output_quoted_string (asm_out_file,
21217 remap_debug_filename (fd->filename));
21218 fputc ('\n', asm_out_file);
21222 return fd->emitted_number;
21225 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21226 That generation should happen after function debug info has been
21227 generated. The value of the attribute is the constant value of ARG. */
21229 static void
21230 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21232 die_arg_entry entry;
21234 if (!die || !arg)
21235 return;
21237 if (!tmpl_value_parm_die_table)
21238 vec_alloc (tmpl_value_parm_die_table, 32);
21240 entry.die = die;
21241 entry.arg = arg;
21242 vec_safe_push (tmpl_value_parm_die_table, entry);
21245 /* Return TRUE if T is an instance of generic type, FALSE
21246 otherwise. */
21248 static bool
21249 generic_type_p (tree t)
21251 if (t == NULL_TREE || !TYPE_P (t))
21252 return false;
21253 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21256 /* Schedule the generation of the generic parameter dies for the
21257 instance of generic type T. The proper generation itself is later
21258 done by gen_scheduled_generic_parms_dies. */
21260 static void
21261 schedule_generic_params_dies_gen (tree t)
21263 if (!generic_type_p (t))
21264 return;
21266 if (!generic_type_instances)
21267 vec_alloc (generic_type_instances, 256);
21269 vec_safe_push (generic_type_instances, t);
21272 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21273 by append_entry_to_tmpl_value_parm_die_table. This function must
21274 be called after function DIEs have been generated. */
21276 static void
21277 gen_remaining_tmpl_value_param_die_attribute (void)
21279 if (tmpl_value_parm_die_table)
21281 unsigned i;
21282 die_arg_entry *e;
21284 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
21285 tree_add_const_value_attribute (e->die, e->arg);
21289 /* Generate generic parameters DIEs for instances of generic types
21290 that have been previously scheduled by
21291 schedule_generic_params_dies_gen. This function must be called
21292 after all the types of the CU have been laid out. */
21294 static void
21295 gen_scheduled_generic_parms_dies (void)
21297 unsigned i;
21298 tree t;
21300 if (!generic_type_instances)
21301 return;
21303 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
21304 if (COMPLETE_TYPE_P (t))
21305 gen_generic_params_dies (t);
21309 /* Replace DW_AT_name for the decl with name. */
21311 static void
21312 dwarf2out_set_name (tree decl, tree name)
21314 dw_die_ref die;
21315 dw_attr_ref attr;
21316 const char *dname;
21318 die = TYPE_SYMTAB_DIE (decl);
21319 if (!die)
21320 return;
21322 dname = dwarf2_name (name, 0);
21323 if (!dname)
21324 return;
21326 attr = get_AT (die, DW_AT_name);
21327 if (attr)
21329 struct indirect_string_node *node;
21331 node = find_AT_string (dname);
21332 /* replace the string. */
21333 attr->dw_attr_val.v.val_str = node;
21336 else
21337 add_name_attribute (die, dname);
21340 /* True if before or during processing of the first function being emitted. */
21341 static bool in_first_function_p = true;
21342 /* True if loc_note during dwarf2out_var_location call might still be
21343 before first real instruction at address equal to .Ltext0. */
21344 static bool maybe_at_text_label_p = true;
21345 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
21346 static unsigned int first_loclabel_num_not_at_text_label;
21348 /* Called by the final INSN scan whenever we see a var location. We
21349 use it to drop labels in the right places, and throw the location in
21350 our lookup table. */
21352 static void
21353 dwarf2out_var_location (rtx_insn *loc_note)
21355 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21356 struct var_loc_node *newloc;
21357 rtx_insn *next_real, *next_note;
21358 static const char *last_label;
21359 static const char *last_postcall_label;
21360 static bool last_in_cold_section_p;
21361 static rtx_insn *expected_next_loc_note;
21362 tree decl;
21363 bool var_loc_p;
21365 if (!NOTE_P (loc_note))
21367 if (CALL_P (loc_note))
21369 call_site_count++;
21370 if (SIBLING_CALL_P (loc_note))
21371 tail_call_site_count++;
21373 return;
21376 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21377 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21378 return;
21380 /* Optimize processing a large consecutive sequence of location
21381 notes so we don't spend too much time in next_real_insn. If the
21382 next insn is another location note, remember the next_real_insn
21383 calculation for next time. */
21384 next_real = cached_next_real_insn;
21385 if (next_real)
21387 if (expected_next_loc_note != loc_note)
21388 next_real = NULL;
21391 next_note = NEXT_INSN (loc_note);
21392 if (! next_note
21393 || next_note->deleted ()
21394 || ! NOTE_P (next_note)
21395 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
21396 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
21397 next_note = NULL;
21399 if (! next_real)
21400 next_real = next_real_insn (loc_note);
21402 if (next_note)
21404 expected_next_loc_note = next_note;
21405 cached_next_real_insn = next_real;
21407 else
21408 cached_next_real_insn = NULL;
21410 /* If there are no instructions which would be affected by this note,
21411 don't do anything. */
21412 if (var_loc_p
21413 && next_real == NULL_RTX
21414 && !NOTE_DURING_CALL_P (loc_note))
21415 return;
21417 if (next_real == NULL_RTX)
21418 next_real = get_last_insn ();
21420 /* If there were any real insns between note we processed last time
21421 and this note (or if it is the first note), clear
21422 last_{,postcall_}label so that they are not reused this time. */
21423 if (last_var_location_insn == NULL_RTX
21424 || last_var_location_insn != next_real
21425 || last_in_cold_section_p != in_cold_section_p)
21427 last_label = NULL;
21428 last_postcall_label = NULL;
21431 if (var_loc_p)
21433 decl = NOTE_VAR_LOCATION_DECL (loc_note);
21434 newloc = add_var_loc_to_decl (decl, loc_note,
21435 NOTE_DURING_CALL_P (loc_note)
21436 ? last_postcall_label : last_label);
21437 if (newloc == NULL)
21438 return;
21440 else
21442 decl = NULL_TREE;
21443 newloc = NULL;
21446 /* If there were no real insns between note we processed last time
21447 and this note, use the label we emitted last time. Otherwise
21448 create a new label and emit it. */
21449 if (last_label == NULL)
21451 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21452 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21453 loclabel_num++;
21454 last_label = ggc_strdup (loclabel);
21455 /* See if loclabel might be equal to .Ltext0. If yes,
21456 bump first_loclabel_num_not_at_text_label. */
21457 if (!have_multiple_function_sections
21458 && in_first_function_p
21459 && maybe_at_text_label_p)
21461 static rtx_insn *last_start;
21462 rtx_insn *insn;
21463 for (insn = loc_note; insn; insn = previous_insn (insn))
21464 if (insn == last_start)
21465 break;
21466 else if (!NONDEBUG_INSN_P (insn))
21467 continue;
21468 else
21470 rtx body = PATTERN (insn);
21471 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
21472 continue;
21473 /* Inline asm could occupy zero bytes. */
21474 else if (GET_CODE (body) == ASM_INPUT
21475 || asm_noperands (body) >= 0)
21476 continue;
21477 #ifdef HAVE_attr_length
21478 else if (get_attr_min_length (insn) == 0)
21479 continue;
21480 #endif
21481 else
21483 /* Assume insn has non-zero length. */
21484 maybe_at_text_label_p = false;
21485 break;
21488 if (maybe_at_text_label_p)
21490 last_start = loc_note;
21491 first_loclabel_num_not_at_text_label = loclabel_num;
21496 if (!var_loc_p)
21498 struct call_arg_loc_node *ca_loc
21499 = ggc_cleared_alloc<call_arg_loc_node> ();
21500 rtx_insn *prev = prev_real_insn (loc_note);
21501 rtx x;
21502 ca_loc->call_arg_loc_note = loc_note;
21503 ca_loc->next = NULL;
21504 ca_loc->label = last_label;
21505 gcc_assert (prev
21506 && (CALL_P (prev)
21507 || (NONJUMP_INSN_P (prev)
21508 && GET_CODE (PATTERN (prev)) == SEQUENCE
21509 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21510 if (!CALL_P (prev))
21511 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
21512 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21513 x = get_call_rtx_from (PATTERN (prev));
21514 if (x)
21516 x = XEXP (XEXP (x, 0), 0);
21517 if (GET_CODE (x) == SYMBOL_REF
21518 && SYMBOL_REF_DECL (x)
21519 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21520 ca_loc->symbol_ref = x;
21522 ca_loc->block = insn_scope (prev);
21523 if (call_arg_locations)
21524 call_arg_loc_last->next = ca_loc;
21525 else
21526 call_arg_locations = ca_loc;
21527 call_arg_loc_last = ca_loc;
21529 else if (!NOTE_DURING_CALL_P (loc_note))
21530 newloc->label = last_label;
21531 else
21533 if (!last_postcall_label)
21535 sprintf (loclabel, "%s-1", last_label);
21536 last_postcall_label = ggc_strdup (loclabel);
21538 newloc->label = last_postcall_label;
21541 last_var_location_insn = next_real;
21542 last_in_cold_section_p = in_cold_section_p;
21545 /* Note in one location list that text section has changed. */
21548 var_location_switch_text_section_1 (var_loc_list **slot, void *)
21550 var_loc_list *list = *slot;
21551 if (list->first)
21552 list->last_before_switch
21553 = list->last->next ? list->last->next : list->last;
21554 return 1;
21557 /* Note in all location lists that text section has changed. */
21559 static void
21560 var_location_switch_text_section (void)
21562 if (decl_loc_table == NULL)
21563 return;
21565 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
21568 /* Create a new line number table. */
21570 static dw_line_info_table *
21571 new_line_info_table (void)
21573 dw_line_info_table *table;
21575 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
21576 table->file_num = 1;
21577 table->line_num = 1;
21578 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
21580 return table;
21583 /* Lookup the "current" table into which we emit line info, so
21584 that we don't have to do it for every source line. */
21586 static void
21587 set_cur_line_info_table (section *sec)
21589 dw_line_info_table *table;
21591 if (sec == text_section)
21592 table = text_section_line_info;
21593 else if (sec == cold_text_section)
21595 table = cold_text_section_line_info;
21596 if (!table)
21598 cold_text_section_line_info = table = new_line_info_table ();
21599 table->end_label = cold_end_label;
21602 else
21604 const char *end_label;
21606 if (flag_reorder_blocks_and_partition)
21608 if (in_cold_section_p)
21609 end_label = crtl->subsections.cold_section_end_label;
21610 else
21611 end_label = crtl->subsections.hot_section_end_label;
21613 else
21615 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21616 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
21617 current_function_funcdef_no);
21618 end_label = ggc_strdup (label);
21621 table = new_line_info_table ();
21622 table->end_label = end_label;
21624 vec_safe_push (separate_line_info, table);
21627 if (DWARF2_ASM_LINE_DEBUG_INFO)
21628 table->is_stmt = (cur_line_info_table
21629 ? cur_line_info_table->is_stmt
21630 : DWARF_LINE_DEFAULT_IS_STMT_START);
21631 cur_line_info_table = table;
21635 /* We need to reset the locations at the beginning of each
21636 function. We can't do this in the end_function hook, because the
21637 declarations that use the locations won't have been output when
21638 that hook is called. Also compute have_multiple_function_sections here. */
21640 static void
21641 dwarf2out_begin_function (tree fun)
21643 section *sec = function_section (fun);
21645 if (sec != text_section)
21646 have_multiple_function_sections = true;
21648 if (flag_reorder_blocks_and_partition && !cold_text_section)
21650 gcc_assert (current_function_decl == fun);
21651 cold_text_section = unlikely_text_section ();
21652 switch_to_section (cold_text_section);
21653 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21654 switch_to_section (sec);
21657 dwarf2out_note_section_used ();
21658 call_site_count = 0;
21659 tail_call_site_count = 0;
21661 set_cur_line_info_table (sec);
21664 /* Helper function of dwarf2out_end_function, called only after emitting
21665 the very first function into assembly. Check if some .debug_loc range
21666 might end with a .LVL* label that could be equal to .Ltext0.
21667 In that case we must force using absolute addresses in .debug_loc ranges,
21668 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
21669 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
21670 list terminator.
21671 Set have_multiple_function_sections to true in that case and
21672 terminate htab traversal. */
21675 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
21677 var_loc_list *entry = *slot;
21678 struct var_loc_node *node;
21680 node = entry->first;
21681 if (node && node->next && node->next->label)
21683 unsigned int i;
21684 const char *label = node->next->label;
21685 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
21687 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
21689 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
21690 if (strcmp (label, loclabel) == 0)
21692 have_multiple_function_sections = true;
21693 return 0;
21697 return 1;
21700 /* Hook called after emitting a function into assembly.
21701 This does something only for the very first function emitted. */
21703 static void
21704 dwarf2out_end_function (unsigned int)
21706 if (in_first_function_p
21707 && !have_multiple_function_sections
21708 && first_loclabel_num_not_at_text_label
21709 && decl_loc_table)
21710 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
21711 in_first_function_p = false;
21712 maybe_at_text_label_p = false;
21715 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
21717 static void
21718 push_dw_line_info_entry (dw_line_info_table *table,
21719 enum dw_line_info_opcode opcode, unsigned int val)
21721 dw_line_info_entry e;
21722 e.opcode = opcode;
21723 e.val = val;
21724 vec_safe_push (table->entries, e);
21727 /* Output a label to mark the beginning of a source code line entry
21728 and record information relating to this source line, in
21729 'line_info_table' for later output of the .debug_line section. */
21730 /* ??? The discriminator parameter ought to be unsigned. */
21732 static void
21733 dwarf2out_source_line (unsigned int line, const char *filename,
21734 int discriminator, bool is_stmt)
21736 unsigned int file_num;
21737 dw_line_info_table *table;
21739 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
21740 return;
21742 /* The discriminator column was added in dwarf4. Simplify the below
21743 by simply removing it if we're not supposed to output it. */
21744 if (dwarf_version < 4 && dwarf_strict)
21745 discriminator = 0;
21747 table = cur_line_info_table;
21748 file_num = maybe_emit_file (lookup_filename (filename));
21750 /* ??? TODO: Elide duplicate line number entries. Traditionally,
21751 the debugger has used the second (possibly duplicate) line number
21752 at the beginning of the function to mark the end of the prologue.
21753 We could eliminate any other duplicates within the function. For
21754 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21755 that second line number entry. */
21756 /* Recall that this end-of-prologue indication is *not* the same thing
21757 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
21758 to which the hook corresponds, follows the last insn that was
21759 emitted by gen_prologue. What we need is to precede the first insn
21760 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21761 insn that corresponds to something the user wrote. These may be
21762 very different locations once scheduling is enabled. */
21764 if (0 && file_num == table->file_num
21765 && line == table->line_num
21766 && discriminator == table->discrim_num
21767 && is_stmt == table->is_stmt)
21768 return;
21770 switch_to_section (current_function_section ());
21772 /* If requested, emit something human-readable. */
21773 if (flag_debug_asm)
21774 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21776 if (DWARF2_ASM_LINE_DEBUG_INFO)
21778 /* Emit the .loc directive understood by GNU as. */
21779 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21780 file_num, line, is_stmt, discriminator */
21781 fputs ("\t.loc ", asm_out_file);
21782 fprint_ul (asm_out_file, file_num);
21783 putc (' ', asm_out_file);
21784 fprint_ul (asm_out_file, line);
21785 putc (' ', asm_out_file);
21786 putc ('0', asm_out_file);
21788 if (is_stmt != table->is_stmt)
21790 fputs (" is_stmt ", asm_out_file);
21791 putc (is_stmt ? '1' : '0', asm_out_file);
21793 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21795 gcc_assert (discriminator > 0);
21796 fputs (" discriminator ", asm_out_file);
21797 fprint_ul (asm_out_file, (unsigned long) discriminator);
21799 putc ('\n', asm_out_file);
21801 else
21803 unsigned int label_num = ++line_info_label_num;
21805 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21807 push_dw_line_info_entry (table, LI_set_address, label_num);
21808 if (file_num != table->file_num)
21809 push_dw_line_info_entry (table, LI_set_file, file_num);
21810 if (discriminator != table->discrim_num)
21811 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21812 if (is_stmt != table->is_stmt)
21813 push_dw_line_info_entry (table, LI_negate_stmt, 0);
21814 push_dw_line_info_entry (table, LI_set_line, line);
21817 table->file_num = file_num;
21818 table->line_num = line;
21819 table->discrim_num = discriminator;
21820 table->is_stmt = is_stmt;
21821 table->in_use = true;
21824 /* Record the beginning of a new source file. */
21826 static void
21827 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21829 if (flag_eliminate_dwarf2_dups)
21831 /* Record the beginning of the file for break_out_includes. */
21832 dw_die_ref bincl_die;
21834 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21835 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21838 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21840 macinfo_entry e;
21841 e.code = DW_MACINFO_start_file;
21842 e.lineno = lineno;
21843 e.info = ggc_strdup (filename);
21844 vec_safe_push (macinfo_table, e);
21848 /* Record the end of a source file. */
21850 static void
21851 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21853 if (flag_eliminate_dwarf2_dups)
21854 /* Record the end of the file for break_out_includes. */
21855 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21857 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21859 macinfo_entry e;
21860 e.code = DW_MACINFO_end_file;
21861 e.lineno = lineno;
21862 e.info = NULL;
21863 vec_safe_push (macinfo_table, e);
21867 /* Called from debug_define in toplev.c. The `buffer' parameter contains
21868 the tail part of the directive line, i.e. the part which is past the
21869 initial whitespace, #, whitespace, directive-name, whitespace part. */
21871 static void
21872 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21873 const char *buffer ATTRIBUTE_UNUSED)
21875 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21877 macinfo_entry e;
21878 /* Insert a dummy first entry to be able to optimize the whole
21879 predefined macro block using DW_MACRO_GNU_transparent_include. */
21880 if (macinfo_table->is_empty () && lineno <= 1)
21882 e.code = 0;
21883 e.lineno = 0;
21884 e.info = NULL;
21885 vec_safe_push (macinfo_table, e);
21887 e.code = DW_MACINFO_define;
21888 e.lineno = lineno;
21889 e.info = ggc_strdup (buffer);
21890 vec_safe_push (macinfo_table, e);
21894 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
21895 the tail part of the directive line, i.e. the part which is past the
21896 initial whitespace, #, whitespace, directive-name, whitespace part. */
21898 static void
21899 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21900 const char *buffer ATTRIBUTE_UNUSED)
21902 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21904 macinfo_entry e;
21905 /* Insert a dummy first entry to be able to optimize the whole
21906 predefined macro block using DW_MACRO_GNU_transparent_include. */
21907 if (macinfo_table->is_empty () && lineno <= 1)
21909 e.code = 0;
21910 e.lineno = 0;
21911 e.info = NULL;
21912 vec_safe_push (macinfo_table, e);
21914 e.code = DW_MACINFO_undef;
21915 e.lineno = lineno;
21916 e.info = ggc_strdup (buffer);
21917 vec_safe_push (macinfo_table, e);
21921 /* Helpers to manipulate hash table of CUs. */
21923 struct macinfo_entry_hasher : typed_noop_remove <macinfo_entry>
21925 typedef macinfo_entry value_type;
21926 typedef macinfo_entry compare_type;
21927 static inline hashval_t hash (const value_type *);
21928 static inline bool equal (const value_type *, const compare_type *);
21931 inline hashval_t
21932 macinfo_entry_hasher::hash (const value_type *entry)
21934 return htab_hash_string (entry->info);
21937 inline bool
21938 macinfo_entry_hasher::equal (const value_type *entry1,
21939 const compare_type *entry2)
21941 return !strcmp (entry1->info, entry2->info);
21944 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
21946 /* Output a single .debug_macinfo entry. */
21948 static void
21949 output_macinfo_op (macinfo_entry *ref)
21951 int file_num;
21952 size_t len;
21953 struct indirect_string_node *node;
21954 char label[MAX_ARTIFICIAL_LABEL_BYTES];
21955 struct dwarf_file_data *fd;
21957 switch (ref->code)
21959 case DW_MACINFO_start_file:
21960 fd = lookup_filename (ref->info);
21961 file_num = maybe_emit_file (fd);
21962 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21963 dw2_asm_output_data_uleb128 (ref->lineno,
21964 "Included from line number %lu",
21965 (unsigned long) ref->lineno);
21966 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21967 break;
21968 case DW_MACINFO_end_file:
21969 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21970 break;
21971 case DW_MACINFO_define:
21972 case DW_MACINFO_undef:
21973 len = strlen (ref->info) + 1;
21974 if (!dwarf_strict
21975 && len > DWARF_OFFSET_SIZE
21976 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21977 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21979 ref->code = ref->code == DW_MACINFO_define
21980 ? DW_MACRO_GNU_define_indirect
21981 : DW_MACRO_GNU_undef_indirect;
21982 output_macinfo_op (ref);
21983 return;
21985 dw2_asm_output_data (1, ref->code,
21986 ref->code == DW_MACINFO_define
21987 ? "Define macro" : "Undefine macro");
21988 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21989 (unsigned long) ref->lineno);
21990 dw2_asm_output_nstring (ref->info, -1, "The macro");
21991 break;
21992 case DW_MACRO_GNU_define_indirect:
21993 case DW_MACRO_GNU_undef_indirect:
21994 node = find_AT_string (ref->info);
21995 gcc_assert (node
21996 && ((node->form == DW_FORM_strp)
21997 || (node->form == DW_FORM_GNU_str_index)));
21998 dw2_asm_output_data (1, ref->code,
21999 ref->code == DW_MACRO_GNU_define_indirect
22000 ? "Define macro indirect"
22001 : "Undefine macro indirect");
22002 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22003 (unsigned long) ref->lineno);
22004 if (node->form == DW_FORM_strp)
22005 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22006 debug_str_section, "The macro: \"%s\"",
22007 ref->info);
22008 else
22009 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22010 ref->info);
22011 break;
22012 case DW_MACRO_GNU_transparent_include:
22013 dw2_asm_output_data (1, ref->code, "Transparent include");
22014 ASM_GENERATE_INTERNAL_LABEL (label,
22015 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22016 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22017 break;
22018 default:
22019 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22020 ASM_COMMENT_START, (unsigned long) ref->code);
22021 break;
22025 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22026 other compilation unit .debug_macinfo sections. IDX is the first
22027 index of a define/undef, return the number of ops that should be
22028 emitted in a comdat .debug_macinfo section and emit
22029 a DW_MACRO_GNU_transparent_include entry referencing it.
22030 If the define/undef entry should be emitted normally, return 0. */
22032 static unsigned
22033 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22034 macinfo_hash_type **macinfo_htab)
22036 macinfo_entry *first, *second, *cur, *inc;
22037 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22038 unsigned char checksum[16];
22039 struct md5_ctx ctx;
22040 char *grp_name, *tail;
22041 const char *base;
22042 unsigned int i, count, encoded_filename_len, linebuf_len;
22043 macinfo_entry **slot;
22045 first = &(*macinfo_table)[idx];
22046 second = &(*macinfo_table)[idx + 1];
22048 /* Optimize only if there are at least two consecutive define/undef ops,
22049 and either all of them are before first DW_MACINFO_start_file
22050 with lineno {0,1} (i.e. predefined macro block), or all of them are
22051 in some included header file. */
22052 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22053 return 0;
22054 if (vec_safe_is_empty (files))
22056 if (first->lineno > 1 || second->lineno > 1)
22057 return 0;
22059 else if (first->lineno == 0)
22060 return 0;
22062 /* Find the last define/undef entry that can be grouped together
22063 with first and at the same time compute md5 checksum of their
22064 codes, linenumbers and strings. */
22065 md5_init_ctx (&ctx);
22066 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22067 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22068 break;
22069 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22070 break;
22071 else
22073 unsigned char code = cur->code;
22074 md5_process_bytes (&code, 1, &ctx);
22075 checksum_uleb128 (cur->lineno, &ctx);
22076 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22078 md5_finish_ctx (&ctx, checksum);
22079 count = i - idx;
22081 /* From the containing include filename (if any) pick up just
22082 usable characters from its basename. */
22083 if (vec_safe_is_empty (files))
22084 base = "";
22085 else
22086 base = lbasename (files->last ().info);
22087 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22088 if (ISIDNUM (base[i]) || base[i] == '.')
22089 encoded_filename_len++;
22090 /* Count . at the end. */
22091 if (encoded_filename_len)
22092 encoded_filename_len++;
22094 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22095 linebuf_len = strlen (linebuf);
22097 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22098 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22099 + 16 * 2 + 1);
22100 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22101 tail = grp_name + 4;
22102 if (encoded_filename_len)
22104 for (i = 0; base[i]; i++)
22105 if (ISIDNUM (base[i]) || base[i] == '.')
22106 *tail++ = base[i];
22107 *tail++ = '.';
22109 memcpy (tail, linebuf, linebuf_len);
22110 tail += linebuf_len;
22111 *tail++ = '.';
22112 for (i = 0; i < 16; i++)
22113 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22115 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22116 in the empty vector entry before the first define/undef. */
22117 inc = &(*macinfo_table)[idx - 1];
22118 inc->code = DW_MACRO_GNU_transparent_include;
22119 inc->lineno = 0;
22120 inc->info = ggc_strdup (grp_name);
22121 if (!*macinfo_htab)
22122 *macinfo_htab = new macinfo_hash_type (10);
22123 /* Avoid emitting duplicates. */
22124 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22125 if (*slot != NULL)
22127 inc->code = 0;
22128 inc->info = NULL;
22129 /* If such an entry has been used before, just emit
22130 a DW_MACRO_GNU_transparent_include op. */
22131 inc = *slot;
22132 output_macinfo_op (inc);
22133 /* And clear all macinfo_entry in the range to avoid emitting them
22134 in the second pass. */
22135 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22137 cur->code = 0;
22138 cur->info = NULL;
22141 else
22143 *slot = inc;
22144 inc->lineno = (*macinfo_htab)->elements ();
22145 output_macinfo_op (inc);
22147 return count;
22150 /* Save any strings needed by the macinfo table in the debug str
22151 table. All strings must be collected into the table by the time
22152 index_string is called. */
22154 static void
22155 save_macinfo_strings (void)
22157 unsigned len;
22158 unsigned i;
22159 macinfo_entry *ref;
22161 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
22163 switch (ref->code)
22165 /* Match the logic in output_macinfo_op to decide on
22166 indirect strings. */
22167 case DW_MACINFO_define:
22168 case DW_MACINFO_undef:
22169 len = strlen (ref->info) + 1;
22170 if (!dwarf_strict
22171 && len > DWARF_OFFSET_SIZE
22172 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22173 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22174 set_indirect_string (find_AT_string (ref->info));
22175 break;
22176 case DW_MACRO_GNU_define_indirect:
22177 case DW_MACRO_GNU_undef_indirect:
22178 set_indirect_string (find_AT_string (ref->info));
22179 break;
22180 default:
22181 break;
22186 /* Output macinfo section(s). */
22188 static void
22189 output_macinfo (void)
22191 unsigned i;
22192 unsigned long length = vec_safe_length (macinfo_table);
22193 macinfo_entry *ref;
22194 vec<macinfo_entry, va_gc> *files = NULL;
22195 macinfo_hash_type *macinfo_htab = NULL;
22197 if (! length)
22198 return;
22200 /* output_macinfo* uses these interchangeably. */
22201 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
22202 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
22203 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
22204 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
22206 /* For .debug_macro emit the section header. */
22207 if (!dwarf_strict)
22209 dw2_asm_output_data (2, 4, "DWARF macro version number");
22210 if (DWARF_OFFSET_SIZE == 8)
22211 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
22212 else
22213 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
22214 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
22215 (!dwarf_split_debug_info ? debug_line_section_label
22216 : debug_skeleton_line_section_label),
22217 debug_line_section, NULL);
22220 /* In the first loop, it emits the primary .debug_macinfo section
22221 and after each emitted op the macinfo_entry is cleared.
22222 If a longer range of define/undef ops can be optimized using
22223 DW_MACRO_GNU_transparent_include, the
22224 DW_MACRO_GNU_transparent_include op is emitted and kept in
22225 the vector before the first define/undef in the range and the
22226 whole range of define/undef ops is not emitted and kept. */
22227 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22229 switch (ref->code)
22231 case DW_MACINFO_start_file:
22232 vec_safe_push (files, *ref);
22233 break;
22234 case DW_MACINFO_end_file:
22235 if (!vec_safe_is_empty (files))
22236 files->pop ();
22237 break;
22238 case DW_MACINFO_define:
22239 case DW_MACINFO_undef:
22240 if (!dwarf_strict
22241 && HAVE_COMDAT_GROUP
22242 && vec_safe_length (files) != 1
22243 && i > 0
22244 && i + 1 < length
22245 && (*macinfo_table)[i - 1].code == 0)
22247 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
22248 if (count)
22250 i += count - 1;
22251 continue;
22254 break;
22255 case 0:
22256 /* A dummy entry may be inserted at the beginning to be able
22257 to optimize the whole block of predefined macros. */
22258 if (i == 0)
22259 continue;
22260 default:
22261 break;
22263 output_macinfo_op (ref);
22264 ref->info = NULL;
22265 ref->code = 0;
22268 if (!macinfo_htab)
22269 return;
22271 delete macinfo_htab;
22272 macinfo_htab = NULL;
22274 /* If any DW_MACRO_GNU_transparent_include were used, on those
22275 DW_MACRO_GNU_transparent_include entries terminate the
22276 current chain and switch to a new comdat .debug_macinfo
22277 section and emit the define/undef entries within it. */
22278 for (i = 0; macinfo_table->iterate (i, &ref); i++)
22279 switch (ref->code)
22281 case 0:
22282 continue;
22283 case DW_MACRO_GNU_transparent_include:
22285 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22286 tree comdat_key = get_identifier (ref->info);
22287 /* Terminate the previous .debug_macinfo section. */
22288 dw2_asm_output_data (1, 0, "End compilation unit");
22289 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
22290 SECTION_DEBUG
22291 | SECTION_LINKONCE,
22292 comdat_key);
22293 ASM_GENERATE_INTERNAL_LABEL (label,
22294 DEBUG_MACRO_SECTION_LABEL,
22295 ref->lineno);
22296 ASM_OUTPUT_LABEL (asm_out_file, label);
22297 ref->code = 0;
22298 ref->info = NULL;
22299 dw2_asm_output_data (2, 4, "DWARF macro version number");
22300 if (DWARF_OFFSET_SIZE == 8)
22301 dw2_asm_output_data (1, 1, "Flags: 64-bit");
22302 else
22303 dw2_asm_output_data (1, 0, "Flags: 32-bit");
22305 break;
22306 case DW_MACINFO_define:
22307 case DW_MACINFO_undef:
22308 output_macinfo_op (ref);
22309 ref->code = 0;
22310 ref->info = NULL;
22311 break;
22312 default:
22313 gcc_unreachable ();
22317 /* Set up for Dwarf output at the start of compilation. */
22319 static void
22320 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22322 /* Allocate the file_table. */
22323 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
22325 /* Allocate the decl_die_table. */
22326 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
22328 /* Allocate the decl_loc_table. */
22329 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
22331 /* Allocate the cached_dw_loc_list_table. */
22332 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
22334 /* Allocate the initial hunk of the decl_scope_table. */
22335 vec_alloc (decl_scope_table, 256);
22337 /* Allocate the initial hunk of the abbrev_die_table. */
22338 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
22339 (ABBREV_DIE_TABLE_INCREMENT);
22340 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22341 /* Zero-th entry is allocated, but unused. */
22342 abbrev_die_table_in_use = 1;
22344 /* Allocate the pubtypes and pubnames vectors. */
22345 vec_alloc (pubname_table, 32);
22346 vec_alloc (pubtype_table, 32);
22348 vec_alloc (incomplete_types, 64);
22350 vec_alloc (used_rtx_array, 32);
22352 if (!dwarf_split_debug_info)
22354 debug_info_section = get_section (DEBUG_INFO_SECTION,
22355 SECTION_DEBUG, NULL);
22356 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22357 SECTION_DEBUG, NULL);
22358 debug_loc_section = get_section (DEBUG_LOC_SECTION,
22359 SECTION_DEBUG, NULL);
22361 else
22363 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
22364 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22365 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
22366 SECTION_DEBUG | SECTION_EXCLUDE,
22367 NULL);
22368 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
22369 SECTION_DEBUG, NULL);
22370 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
22371 SECTION_DEBUG, NULL);
22372 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22373 SECTION_DEBUG, NULL);
22374 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
22375 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
22377 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
22378 the main .o, but the skeleton_line goes into the split off dwo. */
22379 debug_skeleton_line_section
22380 = get_section (DEBUG_DWO_LINE_SECTION,
22381 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22382 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
22383 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
22384 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
22385 SECTION_DEBUG | SECTION_EXCLUDE,
22386 NULL);
22387 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
22388 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
22389 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
22390 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
22391 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
22392 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
22394 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22395 SECTION_DEBUG, NULL);
22396 debug_macinfo_section = get_section (dwarf_strict
22397 ? DEBUG_MACINFO_SECTION
22398 : DEBUG_MACRO_SECTION,
22399 DEBUG_MACRO_SECTION_FLAGS, NULL);
22400 debug_line_section = get_section (DEBUG_LINE_SECTION,
22401 SECTION_DEBUG, NULL);
22402 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22403 SECTION_DEBUG, NULL);
22404 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22405 SECTION_DEBUG, NULL);
22406 debug_str_section = get_section (DEBUG_STR_SECTION,
22407 DEBUG_STR_SECTION_FLAGS, NULL);
22408 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22409 SECTION_DEBUG, NULL);
22410 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22411 SECTION_DEBUG, NULL);
22413 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22414 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22415 DEBUG_ABBREV_SECTION_LABEL, 0);
22416 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22417 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22418 COLD_TEXT_SECTION_LABEL, 0);
22419 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22421 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22422 DEBUG_INFO_SECTION_LABEL, 0);
22423 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22424 DEBUG_LINE_SECTION_LABEL, 0);
22425 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22426 DEBUG_RANGES_SECTION_LABEL, 0);
22427 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
22428 DEBUG_ADDR_SECTION_LABEL, 0);
22429 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22430 dwarf_strict
22431 ? DEBUG_MACINFO_SECTION_LABEL
22432 : DEBUG_MACRO_SECTION_LABEL, 0);
22433 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
22435 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22436 vec_alloc (macinfo_table, 64);
22438 switch_to_section (text_section);
22439 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22441 /* Make sure the line number table for .text always exists. */
22442 text_section_line_info = new_line_info_table ();
22443 text_section_line_info->end_label = text_end_label;
22446 /* Called before compile () starts outputtting functions, variables
22447 and toplevel asms into assembly. */
22449 static void
22450 dwarf2out_assembly_start (void)
22452 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22453 && dwarf2out_do_cfi_asm ()
22454 && (!(flag_unwind_tables || flag_exceptions)
22455 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
22456 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22459 /* A helper function for dwarf2out_finish called through
22460 htab_traverse. Assign a string its index. All strings must be
22461 collected into the table by the time index_string is called,
22462 because the indexing code relies on htab_traverse to traverse nodes
22463 in the same order for each run. */
22466 index_string (indirect_string_node **h, unsigned int *index)
22468 indirect_string_node *node = *h;
22470 find_string_form (node);
22471 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22473 gcc_assert (node->index == NO_INDEX_ASSIGNED);
22474 node->index = *index;
22475 *index += 1;
22477 return 1;
22480 /* A helper function for output_indirect_strings called through
22481 htab_traverse. Output the offset to a string and update the
22482 current offset. */
22485 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
22487 indirect_string_node *node = *h;
22489 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22491 /* Assert that this node has been assigned an index. */
22492 gcc_assert (node->index != NO_INDEX_ASSIGNED
22493 && node->index != NOT_INDEXED);
22494 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
22495 "indexed string 0x%x: %s", node->index, node->str);
22496 *offset += strlen (node->str) + 1;
22498 return 1;
22501 /* A helper function for dwarf2out_finish called through
22502 htab_traverse. Output the indexed string. */
22505 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
22507 struct indirect_string_node *node = *h;
22509 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
22511 /* Assert that the strings are output in the same order as their
22512 indexes were assigned. */
22513 gcc_assert (*cur_idx == node->index);
22514 assemble_string (node->str, strlen (node->str) + 1);
22515 *cur_idx += 1;
22517 return 1;
22520 /* A helper function for dwarf2out_finish called through
22521 htab_traverse. Emit one queued .debug_str string. */
22524 output_indirect_string (indirect_string_node **h, void *)
22526 struct indirect_string_node *node = *h;
22528 node->form = find_string_form (node);
22529 if (node->form == DW_FORM_strp && node->refcount > 0)
22531 ASM_OUTPUT_LABEL (asm_out_file, node->label);
22532 assemble_string (node->str, strlen (node->str) + 1);
22535 return 1;
22538 /* Output the indexed string table. */
22540 static void
22541 output_indirect_strings (void)
22543 switch_to_section (debug_str_section);
22544 if (!dwarf_split_debug_info)
22545 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22546 else
22548 unsigned int offset = 0;
22549 unsigned int cur_idx = 0;
22551 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
22553 switch_to_section (debug_str_offsets_section);
22554 debug_str_hash->traverse_noresize
22555 <unsigned int *, output_index_string_offset> (&offset);
22556 switch_to_section (debug_str_dwo_section);
22557 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
22558 (&cur_idx);
22562 /* Callback for htab_traverse to assign an index to an entry in the
22563 table, and to write that entry to the .debug_addr section. */
22566 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
22568 addr_table_entry *entry = *slot;
22570 if (entry->refcount == 0)
22572 gcc_assert (entry->index == NO_INDEX_ASSIGNED
22573 || entry->index == NOT_INDEXED);
22574 return 1;
22577 gcc_assert (entry->index == *cur_index);
22578 (*cur_index)++;
22580 switch (entry->kind)
22582 case ate_kind_rtx:
22583 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
22584 "0x%x", entry->index);
22585 break;
22586 case ate_kind_rtx_dtprel:
22587 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
22588 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
22589 DWARF2_ADDR_SIZE,
22590 entry->addr.rtl);
22591 fputc ('\n', asm_out_file);
22592 break;
22593 case ate_kind_label:
22594 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
22595 "0x%x", entry->index);
22596 break;
22597 default:
22598 gcc_unreachable ();
22600 return 1;
22603 /* Produce the .debug_addr section. */
22605 static void
22606 output_addr_table (void)
22608 unsigned int index = 0;
22609 if (addr_index_table == NULL || addr_index_table->size () == 0)
22610 return;
22612 switch_to_section (debug_addr_section);
22613 addr_index_table
22614 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
22617 #if ENABLE_ASSERT_CHECKING
22618 /* Verify that all marks are clear. */
22620 static void
22621 verify_marks_clear (dw_die_ref die)
22623 dw_die_ref c;
22625 gcc_assert (! die->die_mark);
22626 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22628 #endif /* ENABLE_ASSERT_CHECKING */
22630 /* Clear the marks for a die and its children.
22631 Be cool if the mark isn't set. */
22633 static void
22634 prune_unmark_dies (dw_die_ref die)
22636 dw_die_ref c;
22638 if (die->die_mark)
22639 die->die_mark = 0;
22640 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22643 /* Given DIE that we're marking as used, find any other dies
22644 it references as attributes and mark them as used. */
22646 static void
22647 prune_unused_types_walk_attribs (dw_die_ref die)
22649 dw_attr_ref a;
22650 unsigned ix;
22652 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22654 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22656 /* A reference to another DIE.
22657 Make sure that it will get emitted.
22658 If it was broken out into a comdat group, don't follow it. */
22659 if (! AT_ref (a)->comdat_type_p
22660 || a->dw_attr == DW_AT_specification)
22661 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22663 /* Set the string's refcount to 0 so that prune_unused_types_mark
22664 accounts properly for it. */
22665 if (AT_class (a) == dw_val_class_str)
22666 a->dw_attr_val.v.val_str->refcount = 0;
22670 /* Mark the generic parameters and arguments children DIEs of DIE. */
22672 static void
22673 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22675 dw_die_ref c;
22677 if (die == NULL || die->die_child == NULL)
22678 return;
22679 c = die->die_child;
22682 if (is_template_parameter (c))
22683 prune_unused_types_mark (c, 1);
22684 c = c->die_sib;
22685 } while (c && c != die->die_child);
22688 /* Mark DIE as being used. If DOKIDS is true, then walk down
22689 to DIE's children. */
22691 static void
22692 prune_unused_types_mark (dw_die_ref die, int dokids)
22694 dw_die_ref c;
22696 if (die->die_mark == 0)
22698 /* We haven't done this node yet. Mark it as used. */
22699 die->die_mark = 1;
22700 /* If this is the DIE of a generic type instantiation,
22701 mark the children DIEs that describe its generic parms and
22702 args. */
22703 prune_unused_types_mark_generic_parms_dies (die);
22705 /* We also have to mark its parents as used.
22706 (But we don't want to mark our parent's kids due to this,
22707 unless it is a class.) */
22708 if (die->die_parent)
22709 prune_unused_types_mark (die->die_parent,
22710 class_scope_p (die->die_parent));
22712 /* Mark any referenced nodes. */
22713 prune_unused_types_walk_attribs (die);
22715 /* If this node is a specification,
22716 also mark the definition, if it exists. */
22717 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22718 prune_unused_types_mark (die->die_definition, 1);
22721 if (dokids && die->die_mark != 2)
22723 /* We need to walk the children, but haven't done so yet.
22724 Remember that we've walked the kids. */
22725 die->die_mark = 2;
22727 /* If this is an array type, we need to make sure our
22728 kids get marked, even if they're types. If we're
22729 breaking out types into comdat sections, do this
22730 for all type definitions. */
22731 if (die->die_tag == DW_TAG_array_type
22732 || (use_debug_types
22733 && is_type_die (die) && ! is_declaration_die (die)))
22734 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22735 else
22736 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22740 /* For local classes, look if any static member functions were emitted
22741 and if so, mark them. */
22743 static void
22744 prune_unused_types_walk_local_classes (dw_die_ref die)
22746 dw_die_ref c;
22748 if (die->die_mark == 2)
22749 return;
22751 switch (die->die_tag)
22753 case DW_TAG_structure_type:
22754 case DW_TAG_union_type:
22755 case DW_TAG_class_type:
22756 break;
22758 case DW_TAG_subprogram:
22759 if (!get_AT_flag (die, DW_AT_declaration)
22760 || die->die_definition != NULL)
22761 prune_unused_types_mark (die, 1);
22762 return;
22764 default:
22765 return;
22768 /* Mark children. */
22769 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22772 /* Walk the tree DIE and mark types that we actually use. */
22774 static void
22775 prune_unused_types_walk (dw_die_ref die)
22777 dw_die_ref c;
22779 /* Don't do anything if this node is already marked and
22780 children have been marked as well. */
22781 if (die->die_mark == 2)
22782 return;
22784 switch (die->die_tag)
22786 case DW_TAG_structure_type:
22787 case DW_TAG_union_type:
22788 case DW_TAG_class_type:
22789 if (die->die_perennial_p)
22790 break;
22792 for (c = die->die_parent; c; c = c->die_parent)
22793 if (c->die_tag == DW_TAG_subprogram)
22794 break;
22796 /* Finding used static member functions inside of classes
22797 is needed just for local classes, because for other classes
22798 static member function DIEs with DW_AT_specification
22799 are emitted outside of the DW_TAG_*_type. If we ever change
22800 it, we'd need to call this even for non-local classes. */
22801 if (c)
22802 prune_unused_types_walk_local_classes (die);
22804 /* It's a type node --- don't mark it. */
22805 return;
22807 case DW_TAG_const_type:
22808 case DW_TAG_packed_type:
22809 case DW_TAG_pointer_type:
22810 case DW_TAG_reference_type:
22811 case DW_TAG_rvalue_reference_type:
22812 case DW_TAG_volatile_type:
22813 case DW_TAG_typedef:
22814 case DW_TAG_array_type:
22815 case DW_TAG_interface_type:
22816 case DW_TAG_friend:
22817 case DW_TAG_variant_part:
22818 case DW_TAG_enumeration_type:
22819 case DW_TAG_subroutine_type:
22820 case DW_TAG_string_type:
22821 case DW_TAG_set_type:
22822 case DW_TAG_subrange_type:
22823 case DW_TAG_ptr_to_member_type:
22824 case DW_TAG_file_type:
22825 if (die->die_perennial_p)
22826 break;
22828 /* It's a type node --- don't mark it. */
22829 return;
22831 default:
22832 /* Mark everything else. */
22833 break;
22836 if (die->die_mark == 0)
22838 die->die_mark = 1;
22840 /* Now, mark any dies referenced from here. */
22841 prune_unused_types_walk_attribs (die);
22844 die->die_mark = 2;
22846 /* Mark children. */
22847 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22850 /* Increment the string counts on strings referred to from DIE's
22851 attributes. */
22853 static void
22854 prune_unused_types_update_strings (dw_die_ref die)
22856 dw_attr_ref a;
22857 unsigned ix;
22859 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22860 if (AT_class (a) == dw_val_class_str)
22862 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22863 s->refcount++;
22864 /* Avoid unnecessarily putting strings that are used less than
22865 twice in the hash table. */
22866 if (s->refcount
22867 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22869 indirect_string_node **slot
22870 = debug_str_hash->find_slot_with_hash (s->str,
22871 htab_hash_string (s->str),
22872 INSERT);
22873 gcc_assert (*slot == NULL);
22874 *slot = s;
22879 /* Remove from the tree DIE any dies that aren't marked. */
22881 static void
22882 prune_unused_types_prune (dw_die_ref die)
22884 dw_die_ref c;
22886 gcc_assert (die->die_mark);
22887 prune_unused_types_update_strings (die);
22889 if (! die->die_child)
22890 return;
22892 c = die->die_child;
22893 do {
22894 dw_die_ref prev = c;
22895 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22896 if (c == die->die_child)
22898 /* No marked children between 'prev' and the end of the list. */
22899 if (prev == c)
22900 /* No marked children at all. */
22901 die->die_child = NULL;
22902 else
22904 prev->die_sib = c->die_sib;
22905 die->die_child = prev;
22907 return;
22910 if (c != prev->die_sib)
22911 prev->die_sib = c;
22912 prune_unused_types_prune (c);
22913 } while (c != die->die_child);
22916 /* Remove dies representing declarations that we never use. */
22918 static void
22919 prune_unused_types (void)
22921 unsigned int i;
22922 limbo_die_node *node;
22923 comdat_type_node *ctnode;
22924 pubname_ref pub;
22925 dw_die_ref base_type;
22927 #if ENABLE_ASSERT_CHECKING
22928 /* All the marks should already be clear. */
22929 verify_marks_clear (comp_unit_die ());
22930 for (node = limbo_die_list; node; node = node->next)
22931 verify_marks_clear (node->die);
22932 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22933 verify_marks_clear (ctnode->root_die);
22934 #endif /* ENABLE_ASSERT_CHECKING */
22936 /* Mark types that are used in global variables. */
22937 premark_types_used_by_global_vars ();
22939 /* Set the mark on nodes that are actually used. */
22940 prune_unused_types_walk (comp_unit_die ());
22941 for (node = limbo_die_list; node; node = node->next)
22942 prune_unused_types_walk (node->die);
22943 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22945 prune_unused_types_walk (ctnode->root_die);
22946 prune_unused_types_mark (ctnode->type_die, 1);
22949 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
22950 are unusual in that they are pubnames that are the children of pubtypes.
22951 They should only be marked via their parent DW_TAG_enumeration_type die,
22952 not as roots in themselves. */
22953 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22954 if (pub->die->die_tag != DW_TAG_enumerator)
22955 prune_unused_types_mark (pub->die, 1);
22956 for (i = 0; base_types.iterate (i, &base_type); i++)
22957 prune_unused_types_mark (base_type, 1);
22959 if (debug_str_hash)
22960 debug_str_hash->empty ();
22961 if (skeleton_debug_str_hash)
22962 skeleton_debug_str_hash->empty ();
22963 prune_unused_types_prune (comp_unit_die ());
22964 for (node = limbo_die_list; node; node = node->next)
22965 prune_unused_types_prune (node->die);
22966 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22967 prune_unused_types_prune (ctnode->root_die);
22969 /* Leave the marks clear. */
22970 prune_unmark_dies (comp_unit_die ());
22971 for (node = limbo_die_list; node; node = node->next)
22972 prune_unmark_dies (node->die);
22973 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22974 prune_unmark_dies (ctnode->root_die);
22977 /* Set the parameter to true if there are any relative pathnames in
22978 the file table. */
22980 file_table_relative_p (dwarf_file_data **slot, bool *p)
22982 struct dwarf_file_data *d = *slot;
22983 if (!IS_ABSOLUTE_PATH (d->filename))
22985 *p = true;
22986 return 0;
22988 return 1;
22991 /* Helpers to manipulate hash table of comdat type units. */
22993 struct comdat_type_hasher : typed_noop_remove <comdat_type_node>
22995 typedef comdat_type_node value_type;
22996 typedef comdat_type_node compare_type;
22997 static inline hashval_t hash (const value_type *);
22998 static inline bool equal (const value_type *, const compare_type *);
23001 inline hashval_t
23002 comdat_type_hasher::hash (const value_type *type_node)
23004 hashval_t h;
23005 memcpy (&h, type_node->signature, sizeof (h));
23006 return h;
23009 inline bool
23010 comdat_type_hasher::equal (const value_type *type_node_1,
23011 const compare_type *type_node_2)
23013 return (! memcmp (type_node_1->signature, type_node_2->signature,
23014 DWARF_TYPE_SIGNATURE_SIZE));
23017 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23018 to the location it would have been added, should we know its
23019 DECL_ASSEMBLER_NAME when we added other attributes. This will
23020 probably improve compactness of debug info, removing equivalent
23021 abbrevs, and hide any differences caused by deferring the
23022 computation of the assembler name, triggered by e.g. PCH. */
23024 static inline void
23025 move_linkage_attr (dw_die_ref die)
23027 unsigned ix = vec_safe_length (die->die_attr);
23028 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23030 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23031 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23033 while (--ix > 0)
23035 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23037 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23038 break;
23041 if (ix != vec_safe_length (die->die_attr) - 1)
23043 die->die_attr->pop ();
23044 die->die_attr->quick_insert (ix, linkage);
23048 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23049 referenced from typed stack ops and count how often they are used. */
23051 static void
23052 mark_base_types (dw_loc_descr_ref loc)
23054 dw_die_ref base_type = NULL;
23056 for (; loc; loc = loc->dw_loc_next)
23058 switch (loc->dw_loc_opc)
23060 case DW_OP_GNU_regval_type:
23061 case DW_OP_GNU_deref_type:
23062 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23063 break;
23064 case DW_OP_GNU_convert:
23065 case DW_OP_GNU_reinterpret:
23066 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23067 continue;
23068 /* FALLTHRU */
23069 case DW_OP_GNU_const_type:
23070 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23071 break;
23072 case DW_OP_GNU_entry_value:
23073 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23074 continue;
23075 default:
23076 continue;
23078 gcc_assert (base_type->die_parent == comp_unit_die ());
23079 if (base_type->die_mark)
23080 base_type->die_mark++;
23081 else
23083 base_types.safe_push (base_type);
23084 base_type->die_mark = 1;
23089 /* Comparison function for sorting marked base types. */
23091 static int
23092 base_type_cmp (const void *x, const void *y)
23094 dw_die_ref dx = *(const dw_die_ref *) x;
23095 dw_die_ref dy = *(const dw_die_ref *) y;
23096 unsigned int byte_size1, byte_size2;
23097 unsigned int encoding1, encoding2;
23098 if (dx->die_mark > dy->die_mark)
23099 return -1;
23100 if (dx->die_mark < dy->die_mark)
23101 return 1;
23102 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23103 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23104 if (byte_size1 < byte_size2)
23105 return 1;
23106 if (byte_size1 > byte_size2)
23107 return -1;
23108 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23109 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23110 if (encoding1 < encoding2)
23111 return 1;
23112 if (encoding1 > encoding2)
23113 return -1;
23114 return 0;
23117 /* Move base types marked by mark_base_types as early as possible
23118 in the CU, sorted by decreasing usage count both to make the
23119 uleb128 references as small as possible and to make sure they
23120 will have die_offset already computed by calc_die_sizes when
23121 sizes of typed stack loc ops is computed. */
23123 static void
23124 move_marked_base_types (void)
23126 unsigned int i;
23127 dw_die_ref base_type, die, c;
23129 if (base_types.is_empty ())
23130 return;
23132 /* Sort by decreasing usage count, they will be added again in that
23133 order later on. */
23134 base_types.qsort (base_type_cmp);
23135 die = comp_unit_die ();
23136 c = die->die_child;
23139 dw_die_ref prev = c;
23140 c = c->die_sib;
23141 while (c->die_mark)
23143 remove_child_with_prev (c, prev);
23144 /* As base types got marked, there must be at least
23145 one node other than DW_TAG_base_type. */
23146 gcc_assert (c != c->die_sib);
23147 c = c->die_sib;
23150 while (c != die->die_child);
23151 gcc_assert (die->die_child);
23152 c = die->die_child;
23153 for (i = 0; base_types.iterate (i, &base_type); i++)
23155 base_type->die_mark = 0;
23156 base_type->die_sib = c->die_sib;
23157 c->die_sib = base_type;
23158 c = base_type;
23162 /* Helper function for resolve_addr, attempt to resolve
23163 one CONST_STRING, return true if successful. Similarly verify that
23164 SYMBOL_REFs refer to variables emitted in the current CU. */
23166 static bool
23167 resolve_one_addr (rtx *addr)
23169 rtx rtl = *addr;
23171 if (GET_CODE (rtl) == CONST_STRING)
23173 size_t len = strlen (XSTR (rtl, 0)) + 1;
23174 tree t = build_string (len, XSTR (rtl, 0));
23175 tree tlen = size_int (len - 1);
23176 TREE_TYPE (t)
23177 = build_array_type (char_type_node, build_index_type (tlen));
23178 rtl = lookup_constant_def (t);
23179 if (!rtl || !MEM_P (rtl))
23180 return false;
23181 rtl = XEXP (rtl, 0);
23182 if (GET_CODE (rtl) == SYMBOL_REF
23183 && SYMBOL_REF_DECL (rtl)
23184 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23185 return false;
23186 vec_safe_push (used_rtx_array, rtl);
23187 *addr = rtl;
23188 return true;
23191 if (GET_CODE (rtl) == SYMBOL_REF
23192 && SYMBOL_REF_DECL (rtl))
23194 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23196 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23197 return false;
23199 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23200 return false;
23203 if (GET_CODE (rtl) == CONST)
23205 subrtx_ptr_iterator::array_type array;
23206 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
23207 if (!resolve_one_addr (*iter))
23208 return false;
23211 return true;
23214 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
23215 if possible, and create DW_TAG_dwarf_procedure that can be referenced
23216 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
23218 static rtx
23219 string_cst_pool_decl (tree t)
23221 rtx rtl = output_constant_def (t, 1);
23222 unsigned char *array;
23223 dw_loc_descr_ref l;
23224 tree decl;
23225 size_t len;
23226 dw_die_ref ref;
23228 if (!rtl || !MEM_P (rtl))
23229 return NULL_RTX;
23230 rtl = XEXP (rtl, 0);
23231 if (GET_CODE (rtl) != SYMBOL_REF
23232 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
23233 return NULL_RTX;
23235 decl = SYMBOL_REF_DECL (rtl);
23236 if (!lookup_decl_die (decl))
23238 len = TREE_STRING_LENGTH (t);
23239 vec_safe_push (used_rtx_array, rtl);
23240 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
23241 array = ggc_vec_alloc<unsigned char> (len);
23242 memcpy (array, TREE_STRING_POINTER (t), len);
23243 l = new_loc_descr (DW_OP_implicit_value, len, 0);
23244 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
23245 l->dw_loc_oprnd2.v.val_vec.length = len;
23246 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
23247 l->dw_loc_oprnd2.v.val_vec.array = array;
23248 add_AT_loc (ref, DW_AT_location, l);
23249 equate_decl_number_to_die (decl, ref);
23251 return rtl;
23254 /* Helper function of resolve_addr_in_expr. LOC is
23255 a DW_OP_addr followed by DW_OP_stack_value, either at the start
23256 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
23257 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
23258 with DW_OP_GNU_implicit_pointer if possible
23259 and return true, if unsuccessful, return false. */
23261 static bool
23262 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
23264 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
23265 HOST_WIDE_INT offset = 0;
23266 dw_die_ref ref = NULL;
23267 tree decl;
23269 if (GET_CODE (rtl) == CONST
23270 && GET_CODE (XEXP (rtl, 0)) == PLUS
23271 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
23273 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
23274 rtl = XEXP (XEXP (rtl, 0), 0);
23276 if (GET_CODE (rtl) == CONST_STRING)
23278 size_t len = strlen (XSTR (rtl, 0)) + 1;
23279 tree t = build_string (len, XSTR (rtl, 0));
23280 tree tlen = size_int (len - 1);
23282 TREE_TYPE (t)
23283 = build_array_type (char_type_node, build_index_type (tlen));
23284 rtl = string_cst_pool_decl (t);
23285 if (!rtl)
23286 return false;
23288 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
23290 decl = SYMBOL_REF_DECL (rtl);
23291 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
23293 ref = lookup_decl_die (decl);
23294 if (ref && (get_AT (ref, DW_AT_location)
23295 || get_AT (ref, DW_AT_const_value)))
23297 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
23298 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23299 loc->dw_loc_oprnd1.val_entry = NULL;
23300 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23301 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23302 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23303 loc->dw_loc_oprnd2.v.val_int = offset;
23304 return true;
23308 return false;
23311 /* Helper function for resolve_addr, handle one location
23312 expression, return false if at least one CONST_STRING or SYMBOL_REF in
23313 the location list couldn't be resolved. */
23315 static bool
23316 resolve_addr_in_expr (dw_loc_descr_ref loc)
23318 dw_loc_descr_ref keep = NULL;
23319 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
23320 switch (loc->dw_loc_opc)
23322 case DW_OP_addr:
23323 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23325 if ((prev == NULL
23326 || prev->dw_loc_opc == DW_OP_piece
23327 || prev->dw_loc_opc == DW_OP_bit_piece)
23328 && loc->dw_loc_next
23329 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
23330 && !dwarf_strict
23331 && optimize_one_addr_into_implicit_ptr (loc))
23332 break;
23333 return false;
23335 break;
23336 case DW_OP_GNU_addr_index:
23337 case DW_OP_GNU_const_index:
23338 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
23339 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
23341 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
23342 if (!resolve_one_addr (&rtl))
23343 return false;
23344 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
23345 loc->dw_loc_oprnd1.val_entry =
23346 add_addr_table_entry (rtl, ate_kind_rtx);
23348 break;
23349 case DW_OP_const4u:
23350 case DW_OP_const8u:
23351 if (loc->dtprel
23352 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
23353 return false;
23354 break;
23355 case DW_OP_plus_uconst:
23356 if (size_of_loc_descr (loc)
23357 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
23359 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
23361 dw_loc_descr_ref repl
23362 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
23363 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
23364 add_loc_descr (&repl, loc->dw_loc_next);
23365 *loc = *repl;
23367 break;
23368 case DW_OP_implicit_value:
23369 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23370 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
23371 return false;
23372 break;
23373 case DW_OP_GNU_implicit_pointer:
23374 case DW_OP_GNU_parameter_ref:
23375 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23377 dw_die_ref ref
23378 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23379 if (ref == NULL)
23380 return false;
23381 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23382 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23383 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23385 break;
23386 case DW_OP_GNU_const_type:
23387 case DW_OP_GNU_regval_type:
23388 case DW_OP_GNU_deref_type:
23389 case DW_OP_GNU_convert:
23390 case DW_OP_GNU_reinterpret:
23391 while (loc->dw_loc_next
23392 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
23394 dw_die_ref base1, base2;
23395 unsigned enc1, enc2, size1, size2;
23396 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23397 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23398 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
23399 else if (loc->dw_loc_oprnd1.val_class
23400 == dw_val_class_unsigned_const)
23401 break;
23402 else
23403 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
23404 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
23405 == dw_val_class_unsigned_const)
23406 break;
23407 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
23408 gcc_assert (base1->die_tag == DW_TAG_base_type
23409 && base2->die_tag == DW_TAG_base_type);
23410 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
23411 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
23412 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
23413 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
23414 if (size1 == size2
23415 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
23416 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
23417 && loc != keep)
23418 || enc1 == enc2))
23420 /* Optimize away next DW_OP_GNU_convert after
23421 adjusting LOC's base type die reference. */
23422 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
23423 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
23424 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
23425 else
23426 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
23427 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
23428 continue;
23430 /* Don't change integer DW_OP_GNU_convert after e.g. floating
23431 point typed stack entry. */
23432 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
23433 keep = loc->dw_loc_next;
23434 break;
23436 break;
23437 default:
23438 break;
23440 return true;
23443 /* Helper function of resolve_addr. DIE had DW_AT_location of
23444 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
23445 and DW_OP_addr couldn't be resolved. resolve_addr has already
23446 removed the DW_AT_location attribute. This function attempts to
23447 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
23448 to it or DW_AT_const_value attribute, if possible. */
23450 static void
23451 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
23453 if (TREE_CODE (decl) != VAR_DECL
23454 || lookup_decl_die (decl) != die
23455 || DECL_EXTERNAL (decl)
23456 || !TREE_STATIC (decl)
23457 || DECL_INITIAL (decl) == NULL_TREE
23458 || DECL_P (DECL_INITIAL (decl))
23459 || get_AT (die, DW_AT_const_value))
23460 return;
23462 tree init = DECL_INITIAL (decl);
23463 HOST_WIDE_INT offset = 0;
23464 /* For variables that have been optimized away and thus
23465 don't have a memory location, see if we can emit
23466 DW_AT_const_value instead. */
23467 if (tree_add_const_value_attribute (die, init))
23468 return;
23469 if (dwarf_strict)
23470 return;
23471 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
23472 and ADDR_EXPR refers to a decl that has DW_AT_location or
23473 DW_AT_const_value (but isn't addressable, otherwise
23474 resolving the original DW_OP_addr wouldn't fail), see if
23475 we can add DW_OP_GNU_implicit_pointer. */
23476 STRIP_NOPS (init);
23477 if (TREE_CODE (init) == POINTER_PLUS_EXPR
23478 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
23480 offset = tree_to_shwi (TREE_OPERAND (init, 1));
23481 init = TREE_OPERAND (init, 0);
23482 STRIP_NOPS (init);
23484 if (TREE_CODE (init) != ADDR_EXPR)
23485 return;
23486 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
23487 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
23488 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
23489 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
23490 && TREE_OPERAND (init, 0) != decl))
23492 dw_die_ref ref;
23493 dw_loc_descr_ref l;
23495 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
23497 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
23498 if (!rtl)
23499 return;
23500 decl = SYMBOL_REF_DECL (rtl);
23502 else
23503 decl = TREE_OPERAND (init, 0);
23504 ref = lookup_decl_die (decl);
23505 if (ref == NULL
23506 || (!get_AT (ref, DW_AT_location)
23507 && !get_AT (ref, DW_AT_const_value)))
23508 return;
23509 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
23510 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23511 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
23512 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
23513 add_AT_loc (die, DW_AT_location, l);
23517 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23518 an address in .rodata section if the string literal is emitted there,
23519 or remove the containing location list or replace DW_AT_const_value
23520 with DW_AT_location and empty location expression, if it isn't found
23521 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
23522 to something that has been emitted in the current CU. */
23524 static void
23525 resolve_addr (dw_die_ref die)
23527 dw_die_ref c;
23528 dw_attr_ref a;
23529 dw_loc_list_ref *curr, *start, loc;
23530 unsigned ix;
23532 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23533 switch (AT_class (a))
23535 case dw_val_class_loc_list:
23536 start = curr = AT_loc_list_ptr (a);
23537 loc = *curr;
23538 gcc_assert (loc);
23539 /* The same list can be referenced more than once. See if we have
23540 already recorded the result from a previous pass. */
23541 if (loc->replaced)
23542 *curr = loc->dw_loc_next;
23543 else if (!loc->resolved_addr)
23545 /* As things stand, we do not expect or allow one die to
23546 reference a suffix of another die's location list chain.
23547 References must be identical or completely separate.
23548 There is therefore no need to cache the result of this
23549 pass on any list other than the first; doing so
23550 would lead to unnecessary writes. */
23551 while (*curr)
23553 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23554 if (!resolve_addr_in_expr ((*curr)->expr))
23556 dw_loc_list_ref next = (*curr)->dw_loc_next;
23557 dw_loc_descr_ref l = (*curr)->expr;
23559 if (next && (*curr)->ll_symbol)
23561 gcc_assert (!next->ll_symbol);
23562 next->ll_symbol = (*curr)->ll_symbol;
23564 if (dwarf_split_debug_info)
23565 remove_loc_list_addr_table_entries (l);
23566 *curr = next;
23568 else
23570 mark_base_types ((*curr)->expr);
23571 curr = &(*curr)->dw_loc_next;
23574 if (loc == *start)
23575 loc->resolved_addr = 1;
23576 else
23578 loc->replaced = 1;
23579 loc->dw_loc_next = *start;
23582 if (!*start)
23584 remove_AT (die, a->dw_attr);
23585 ix--;
23587 break;
23588 case dw_val_class_loc:
23590 dw_loc_descr_ref l = AT_loc (a);
23591 /* For -gdwarf-2 don't attempt to optimize
23592 DW_AT_data_member_location containing
23593 DW_OP_plus_uconst - older consumers might
23594 rely on it being that op instead of a more complex,
23595 but shorter, location description. */
23596 if ((dwarf_version > 2
23597 || a->dw_attr != DW_AT_data_member_location
23598 || l == NULL
23599 || l->dw_loc_opc != DW_OP_plus_uconst
23600 || l->dw_loc_next != NULL)
23601 && !resolve_addr_in_expr (l))
23603 if (dwarf_split_debug_info)
23604 remove_loc_list_addr_table_entries (l);
23605 if (l != NULL
23606 && l->dw_loc_next == NULL
23607 && l->dw_loc_opc == DW_OP_addr
23608 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
23609 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
23610 && a->dw_attr == DW_AT_location)
23612 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
23613 remove_AT (die, a->dw_attr);
23614 ix--;
23615 optimize_location_into_implicit_ptr (die, decl);
23616 break;
23618 remove_AT (die, a->dw_attr);
23619 ix--;
23621 else
23622 mark_base_types (l);
23624 break;
23625 case dw_val_class_addr:
23626 if (a->dw_attr == DW_AT_const_value
23627 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
23629 if (AT_index (a) != NOT_INDEXED)
23630 remove_addr_table_entry (a->dw_attr_val.val_entry);
23631 remove_AT (die, a->dw_attr);
23632 ix--;
23634 if (die->die_tag == DW_TAG_GNU_call_site
23635 && a->dw_attr == DW_AT_abstract_origin)
23637 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23638 dw_die_ref tdie = lookup_decl_die (tdecl);
23639 if (tdie == NULL
23640 && DECL_EXTERNAL (tdecl)
23641 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23643 force_decl_die (tdecl);
23644 tdie = lookup_decl_die (tdecl);
23646 if (tdie)
23648 a->dw_attr_val.val_class = dw_val_class_die_ref;
23649 a->dw_attr_val.v.val_die_ref.die = tdie;
23650 a->dw_attr_val.v.val_die_ref.external = 0;
23652 else
23654 if (AT_index (a) != NOT_INDEXED)
23655 remove_addr_table_entry (a->dw_attr_val.val_entry);
23656 remove_AT (die, a->dw_attr);
23657 ix--;
23660 break;
23661 default:
23662 break;
23665 FOR_EACH_CHILD (die, c, resolve_addr (c));
23668 /* Helper routines for optimize_location_lists.
23669 This pass tries to share identical local lists in .debug_loc
23670 section. */
23672 /* Iteratively hash operands of LOC opcode into HSTATE. */
23674 static void
23675 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
23677 dw_val_ref val1 = &loc->dw_loc_oprnd1;
23678 dw_val_ref val2 = &loc->dw_loc_oprnd2;
23680 switch (loc->dw_loc_opc)
23682 case DW_OP_const4u:
23683 case DW_OP_const8u:
23684 if (loc->dtprel)
23685 goto hash_addr;
23686 /* FALLTHRU */
23687 case DW_OP_const1u:
23688 case DW_OP_const1s:
23689 case DW_OP_const2u:
23690 case DW_OP_const2s:
23691 case DW_OP_const4s:
23692 case DW_OP_const8s:
23693 case DW_OP_constu:
23694 case DW_OP_consts:
23695 case DW_OP_pick:
23696 case DW_OP_plus_uconst:
23697 case DW_OP_breg0:
23698 case DW_OP_breg1:
23699 case DW_OP_breg2:
23700 case DW_OP_breg3:
23701 case DW_OP_breg4:
23702 case DW_OP_breg5:
23703 case DW_OP_breg6:
23704 case DW_OP_breg7:
23705 case DW_OP_breg8:
23706 case DW_OP_breg9:
23707 case DW_OP_breg10:
23708 case DW_OP_breg11:
23709 case DW_OP_breg12:
23710 case DW_OP_breg13:
23711 case DW_OP_breg14:
23712 case DW_OP_breg15:
23713 case DW_OP_breg16:
23714 case DW_OP_breg17:
23715 case DW_OP_breg18:
23716 case DW_OP_breg19:
23717 case DW_OP_breg20:
23718 case DW_OP_breg21:
23719 case DW_OP_breg22:
23720 case DW_OP_breg23:
23721 case DW_OP_breg24:
23722 case DW_OP_breg25:
23723 case DW_OP_breg26:
23724 case DW_OP_breg27:
23725 case DW_OP_breg28:
23726 case DW_OP_breg29:
23727 case DW_OP_breg30:
23728 case DW_OP_breg31:
23729 case DW_OP_regx:
23730 case DW_OP_fbreg:
23731 case DW_OP_piece:
23732 case DW_OP_deref_size:
23733 case DW_OP_xderef_size:
23734 hstate.add_object (val1->v.val_int);
23735 break;
23736 case DW_OP_skip:
23737 case DW_OP_bra:
23739 int offset;
23741 gcc_assert (val1->val_class == dw_val_class_loc);
23742 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23743 hstate.add_object (offset);
23745 break;
23746 case DW_OP_implicit_value:
23747 hstate.add_object (val1->v.val_unsigned);
23748 switch (val2->val_class)
23750 case dw_val_class_const:
23751 hstate.add_object (val2->v.val_int);
23752 break;
23753 case dw_val_class_vec:
23755 unsigned int elt_size = val2->v.val_vec.elt_size;
23756 unsigned int len = val2->v.val_vec.length;
23758 hstate.add_int (elt_size);
23759 hstate.add_int (len);
23760 hstate.add (val2->v.val_vec.array, len * elt_size);
23762 break;
23763 case dw_val_class_const_double:
23764 hstate.add_object (val2->v.val_double.low);
23765 hstate.add_object (val2->v.val_double.high);
23766 break;
23767 case dw_val_class_wide_int:
23768 hstate.add_object (*val2->v.val_wide);
23769 break;
23770 case dw_val_class_addr:
23771 inchash::add_rtx (val2->v.val_addr, hstate);
23772 break;
23773 default:
23774 gcc_unreachable ();
23776 break;
23777 case DW_OP_bregx:
23778 case DW_OP_bit_piece:
23779 hstate.add_object (val1->v.val_int);
23780 hstate.add_object (val2->v.val_int);
23781 break;
23782 case DW_OP_addr:
23783 hash_addr:
23784 if (loc->dtprel)
23786 unsigned char dtprel = 0xd1;
23787 hstate.add_object (dtprel);
23789 inchash::add_rtx (val1->v.val_addr, hstate);
23790 break;
23791 case DW_OP_GNU_addr_index:
23792 case DW_OP_GNU_const_index:
23794 if (loc->dtprel)
23796 unsigned char dtprel = 0xd1;
23797 hstate.add_object (dtprel);
23799 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
23801 break;
23802 case DW_OP_GNU_implicit_pointer:
23803 hstate.add_int (val2->v.val_int);
23804 break;
23805 case DW_OP_GNU_entry_value:
23806 hstate.add_object (val1->v.val_loc);
23807 break;
23808 case DW_OP_GNU_regval_type:
23809 case DW_OP_GNU_deref_type:
23811 unsigned int byte_size
23812 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23813 unsigned int encoding
23814 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23815 hstate.add_object (val1->v.val_int);
23816 hstate.add_object (byte_size);
23817 hstate.add_object (encoding);
23819 break;
23820 case DW_OP_GNU_convert:
23821 case DW_OP_GNU_reinterpret:
23822 if (val1->val_class == dw_val_class_unsigned_const)
23824 hstate.add_object (val1->v.val_unsigned);
23825 break;
23827 /* FALLTHRU */
23828 case DW_OP_GNU_const_type:
23830 unsigned int byte_size
23831 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23832 unsigned int encoding
23833 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23834 hstate.add_object (byte_size);
23835 hstate.add_object (encoding);
23836 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23837 break;
23838 hstate.add_object (val2->val_class);
23839 switch (val2->val_class)
23841 case dw_val_class_const:
23842 hstate.add_object (val2->v.val_int);
23843 break;
23844 case dw_val_class_vec:
23846 unsigned int elt_size = val2->v.val_vec.elt_size;
23847 unsigned int len = val2->v.val_vec.length;
23849 hstate.add_object (elt_size);
23850 hstate.add_object (len);
23851 hstate.add (val2->v.val_vec.array, len * elt_size);
23853 break;
23854 case dw_val_class_const_double:
23855 hstate.add_object (val2->v.val_double.low);
23856 hstate.add_object (val2->v.val_double.high);
23857 break;
23858 case dw_val_class_wide_int:
23859 hstate.add_object (*val2->v.val_wide);
23860 break;
23861 default:
23862 gcc_unreachable ();
23865 break;
23867 default:
23868 /* Other codes have no operands. */
23869 break;
23873 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
23875 static inline void
23876 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
23878 dw_loc_descr_ref l;
23879 bool sizes_computed = false;
23880 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
23881 size_of_locs (loc);
23883 for (l = loc; l != NULL; l = l->dw_loc_next)
23885 enum dwarf_location_atom opc = l->dw_loc_opc;
23886 hstate.add_object (opc);
23887 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23889 size_of_locs (loc);
23890 sizes_computed = true;
23892 hash_loc_operands (l, hstate);
23896 /* Compute hash of the whole location list LIST_HEAD. */
23898 static inline void
23899 hash_loc_list (dw_loc_list_ref list_head)
23901 dw_loc_list_ref curr = list_head;
23902 inchash::hash hstate;
23904 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23906 hstate.add (curr->begin, strlen (curr->begin) + 1);
23907 hstate.add (curr->end, strlen (curr->end) + 1);
23908 if (curr->section)
23909 hstate.add (curr->section, strlen (curr->section) + 1);
23910 hash_locs (curr->expr, hstate);
23912 list_head->hash = hstate.end ();
23915 /* Return true if X and Y opcodes have the same operands. */
23917 static inline bool
23918 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23920 dw_val_ref valx1 = &x->dw_loc_oprnd1;
23921 dw_val_ref valx2 = &x->dw_loc_oprnd2;
23922 dw_val_ref valy1 = &y->dw_loc_oprnd1;
23923 dw_val_ref valy2 = &y->dw_loc_oprnd2;
23925 switch (x->dw_loc_opc)
23927 case DW_OP_const4u:
23928 case DW_OP_const8u:
23929 if (x->dtprel)
23930 goto hash_addr;
23931 /* FALLTHRU */
23932 case DW_OP_const1u:
23933 case DW_OP_const1s:
23934 case DW_OP_const2u:
23935 case DW_OP_const2s:
23936 case DW_OP_const4s:
23937 case DW_OP_const8s:
23938 case DW_OP_constu:
23939 case DW_OP_consts:
23940 case DW_OP_pick:
23941 case DW_OP_plus_uconst:
23942 case DW_OP_breg0:
23943 case DW_OP_breg1:
23944 case DW_OP_breg2:
23945 case DW_OP_breg3:
23946 case DW_OP_breg4:
23947 case DW_OP_breg5:
23948 case DW_OP_breg6:
23949 case DW_OP_breg7:
23950 case DW_OP_breg8:
23951 case DW_OP_breg9:
23952 case DW_OP_breg10:
23953 case DW_OP_breg11:
23954 case DW_OP_breg12:
23955 case DW_OP_breg13:
23956 case DW_OP_breg14:
23957 case DW_OP_breg15:
23958 case DW_OP_breg16:
23959 case DW_OP_breg17:
23960 case DW_OP_breg18:
23961 case DW_OP_breg19:
23962 case DW_OP_breg20:
23963 case DW_OP_breg21:
23964 case DW_OP_breg22:
23965 case DW_OP_breg23:
23966 case DW_OP_breg24:
23967 case DW_OP_breg25:
23968 case DW_OP_breg26:
23969 case DW_OP_breg27:
23970 case DW_OP_breg28:
23971 case DW_OP_breg29:
23972 case DW_OP_breg30:
23973 case DW_OP_breg31:
23974 case DW_OP_regx:
23975 case DW_OP_fbreg:
23976 case DW_OP_piece:
23977 case DW_OP_deref_size:
23978 case DW_OP_xderef_size:
23979 return valx1->v.val_int == valy1->v.val_int;
23980 case DW_OP_skip:
23981 case DW_OP_bra:
23982 /* If splitting debug info, the use of DW_OP_GNU_addr_index
23983 can cause irrelevant differences in dw_loc_addr. */
23984 gcc_assert (valx1->val_class == dw_val_class_loc
23985 && valy1->val_class == dw_val_class_loc
23986 && (dwarf_split_debug_info
23987 || x->dw_loc_addr == y->dw_loc_addr));
23988 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23989 case DW_OP_implicit_value:
23990 if (valx1->v.val_unsigned != valy1->v.val_unsigned
23991 || valx2->val_class != valy2->val_class)
23992 return false;
23993 switch (valx2->val_class)
23995 case dw_val_class_const:
23996 return valx2->v.val_int == valy2->v.val_int;
23997 case dw_val_class_vec:
23998 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23999 && valx2->v.val_vec.length == valy2->v.val_vec.length
24000 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24001 valx2->v.val_vec.elt_size
24002 * valx2->v.val_vec.length) == 0;
24003 case dw_val_class_const_double:
24004 return valx2->v.val_double.low == valy2->v.val_double.low
24005 && valx2->v.val_double.high == valy2->v.val_double.high;
24006 case dw_val_class_wide_int:
24007 return *valx2->v.val_wide == *valy2->v.val_wide;
24008 case dw_val_class_addr:
24009 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24010 default:
24011 gcc_unreachable ();
24013 case DW_OP_bregx:
24014 case DW_OP_bit_piece:
24015 return valx1->v.val_int == valy1->v.val_int
24016 && valx2->v.val_int == valy2->v.val_int;
24017 case DW_OP_addr:
24018 hash_addr:
24019 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24020 case DW_OP_GNU_addr_index:
24021 case DW_OP_GNU_const_index:
24023 rtx ax1 = valx1->val_entry->addr.rtl;
24024 rtx ay1 = valy1->val_entry->addr.rtl;
24025 return rtx_equal_p (ax1, ay1);
24027 case DW_OP_GNU_implicit_pointer:
24028 return valx1->val_class == dw_val_class_die_ref
24029 && valx1->val_class == valy1->val_class
24030 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24031 && valx2->v.val_int == valy2->v.val_int;
24032 case DW_OP_GNU_entry_value:
24033 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24034 case DW_OP_GNU_const_type:
24035 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24036 || valx2->val_class != valy2->val_class)
24037 return false;
24038 switch (valx2->val_class)
24040 case dw_val_class_const:
24041 return valx2->v.val_int == valy2->v.val_int;
24042 case dw_val_class_vec:
24043 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24044 && valx2->v.val_vec.length == valy2->v.val_vec.length
24045 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24046 valx2->v.val_vec.elt_size
24047 * valx2->v.val_vec.length) == 0;
24048 case dw_val_class_const_double:
24049 return valx2->v.val_double.low == valy2->v.val_double.low
24050 && valx2->v.val_double.high == valy2->v.val_double.high;
24051 case dw_val_class_wide_int:
24052 return *valx2->v.val_wide == *valy2->v.val_wide;
24053 default:
24054 gcc_unreachable ();
24056 case DW_OP_GNU_regval_type:
24057 case DW_OP_GNU_deref_type:
24058 return valx1->v.val_int == valy1->v.val_int
24059 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24060 case DW_OP_GNU_convert:
24061 case DW_OP_GNU_reinterpret:
24062 if (valx1->val_class != valy1->val_class)
24063 return false;
24064 if (valx1->val_class == dw_val_class_unsigned_const)
24065 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24066 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24067 case DW_OP_GNU_parameter_ref:
24068 return valx1->val_class == dw_val_class_die_ref
24069 && valx1->val_class == valy1->val_class
24070 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24071 default:
24072 /* Other codes have no operands. */
24073 return true;
24077 /* Return true if DWARF location expressions X and Y are the same. */
24079 static inline bool
24080 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24082 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24083 if (x->dw_loc_opc != y->dw_loc_opc
24084 || x->dtprel != y->dtprel
24085 || !compare_loc_operands (x, y))
24086 break;
24087 return x == NULL && y == NULL;
24090 /* Hashtable helpers. */
24092 struct loc_list_hasher : typed_noop_remove <dw_loc_list_struct>
24094 typedef dw_loc_list_struct value_type;
24095 typedef dw_loc_list_struct compare_type;
24096 static inline hashval_t hash (const value_type *);
24097 static inline bool equal (const value_type *, const compare_type *);
24100 /* Return precomputed hash of location list X. */
24102 inline hashval_t
24103 loc_list_hasher::hash (const value_type *x)
24105 return x->hash;
24108 /* Return true if location lists A and B are the same. */
24110 inline bool
24111 loc_list_hasher::equal (const value_type *a, const compare_type *b)
24113 if (a == b)
24114 return 1;
24115 if (a->hash != b->hash)
24116 return 0;
24117 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24118 if (strcmp (a->begin, b->begin) != 0
24119 || strcmp (a->end, b->end) != 0
24120 || (a->section == NULL) != (b->section == NULL)
24121 || (a->section && strcmp (a->section, b->section) != 0)
24122 || !compare_locs (a->expr, b->expr))
24123 break;
24124 return a == NULL && b == NULL;
24127 typedef hash_table<loc_list_hasher> loc_list_hash_type;
24130 /* Recursively optimize location lists referenced from DIE
24131 children and share them whenever possible. */
24133 static void
24134 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
24136 dw_die_ref c;
24137 dw_attr_ref a;
24138 unsigned ix;
24139 dw_loc_list_struct **slot;
24141 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24142 if (AT_class (a) == dw_val_class_loc_list)
24144 dw_loc_list_ref list = AT_loc_list (a);
24145 /* TODO: perform some optimizations here, before hashing
24146 it and storing into the hash table. */
24147 hash_loc_list (list);
24148 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
24149 if (*slot == NULL)
24150 *slot = list;
24151 else
24152 a->dw_attr_val.v.val_loc_list = *slot;
24155 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24159 /* Recursively assign each location list a unique index into the debug_addr
24160 section. */
24162 static void
24163 index_location_lists (dw_die_ref die)
24165 dw_die_ref c;
24166 dw_attr_ref a;
24167 unsigned ix;
24169 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24170 if (AT_class (a) == dw_val_class_loc_list)
24172 dw_loc_list_ref list = AT_loc_list (a);
24173 dw_loc_list_ref curr;
24174 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
24176 /* Don't index an entry that has already been indexed
24177 or won't be output. */
24178 if (curr->begin_entry != NULL
24179 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
24180 continue;
24182 curr->begin_entry
24183 = add_addr_table_entry (xstrdup (curr->begin),
24184 ate_kind_label);
24188 FOR_EACH_CHILD (die, c, index_location_lists (c));
24191 /* Optimize location lists referenced from DIE
24192 children and share them whenever possible. */
24194 static void
24195 optimize_location_lists (dw_die_ref die)
24197 loc_list_hash_type htab (500);
24198 optimize_location_lists_1 (die, &htab);
24201 /* Output stuff that dwarf requires at the end of every file,
24202 and generate the DWARF-2 debugging info. */
24204 static void
24205 dwarf2out_finish (const char *filename)
24207 limbo_die_node *node, *next_node;
24208 comdat_type_node *ctnode;
24209 unsigned int i;
24210 dw_die_ref main_comp_unit_die;
24212 /* PCH might result in DW_AT_producer string being restored from the
24213 header compilation, so always fill it with empty string initially
24214 and overwrite only here. */
24215 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
24216 producer_string = gen_producer_string ();
24217 producer->dw_attr_val.v.val_str->refcount--;
24218 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
24220 gen_scheduled_generic_parms_dies ();
24221 gen_remaining_tmpl_value_param_die_attribute ();
24223 /* Add the name for the main input file now. We delayed this from
24224 dwarf2out_init to avoid complications with PCH. */
24225 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24226 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
24227 add_comp_dir_attribute (comp_unit_die ());
24228 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24230 bool p = false;
24231 file_table->traverse<bool *, file_table_relative_p> (&p);
24232 if (p)
24233 add_comp_dir_attribute (comp_unit_die ());
24236 if (deferred_locations_list)
24237 for (i = 0; i < deferred_locations_list->length (); i++)
24239 add_location_or_const_value_attribute (
24240 (*deferred_locations_list)[i].die,
24241 (*deferred_locations_list)[i].variable,
24242 false,
24243 DW_AT_location);
24246 /* Traverse the limbo die list, and add parent/child links. The only
24247 dies without parents that should be here are concrete instances of
24248 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
24249 For concrete instances, we can get the parent die from the abstract
24250 instance. */
24251 for (node = limbo_die_list; node; node = next_node)
24253 dw_die_ref die = node->die;
24254 next_node = node->next;
24256 if (die->die_parent == NULL)
24258 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24260 if (origin && origin->die_parent)
24261 add_child_die (origin->die_parent, die);
24262 else if (is_cu_die (die))
24264 else if (seen_error ())
24265 /* It's OK to be confused by errors in the input. */
24266 add_child_die (comp_unit_die (), die);
24267 else
24269 /* In certain situations, the lexical block containing a
24270 nested function can be optimized away, which results
24271 in the nested function die being orphaned. Likewise
24272 with the return type of that nested function. Force
24273 this to be a child of the containing function.
24275 It may happen that even the containing function got fully
24276 inlined and optimized out. In that case we are lost and
24277 assign the empty child. This should not be big issue as
24278 the function is likely unreachable too. */
24279 gcc_assert (node->created_for);
24281 if (DECL_P (node->created_for))
24282 origin = get_context_die (DECL_CONTEXT (node->created_for));
24283 else if (TYPE_P (node->created_for))
24284 origin = scope_die_for (node->created_for, comp_unit_die ());
24285 else
24286 origin = comp_unit_die ();
24288 add_child_die (origin, die);
24293 limbo_die_list = NULL;
24295 #if ENABLE_ASSERT_CHECKING
24297 dw_die_ref die = comp_unit_die (), c;
24298 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24300 #endif
24301 resolve_addr (comp_unit_die ());
24302 move_marked_base_types ();
24304 for (node = deferred_asm_name; node; node = node->next)
24306 tree decl = node->created_for;
24307 /* When generating LTO bytecode we can not generate new assembler
24308 names at this point and all important decls got theirs via
24309 free-lang-data. */
24310 if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
24311 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24313 add_linkage_attr (node->die, decl);
24314 move_linkage_attr (node->die);
24318 deferred_asm_name = NULL;
24320 /* Walk through the list of incomplete types again, trying once more to
24321 emit full debugging info for them. */
24322 retry_incomplete_types ();
24324 if (flag_eliminate_unused_debug_types)
24325 prune_unused_types ();
24327 /* Generate separate COMDAT sections for type DIEs. */
24328 if (use_debug_types)
24330 break_out_comdat_types (comp_unit_die ());
24332 /* Each new type_unit DIE was added to the limbo die list when created.
24333 Since these have all been added to comdat_type_list, clear the
24334 limbo die list. */
24335 limbo_die_list = NULL;
24337 /* For each new comdat type unit, copy declarations for incomplete
24338 types to make the new unit self-contained (i.e., no direct
24339 references to the main compile unit). */
24340 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24341 copy_decls_for_unworthy_types (ctnode->root_die);
24342 copy_decls_for_unworthy_types (comp_unit_die ());
24344 /* In the process of copying declarations from one unit to another,
24345 we may have left some declarations behind that are no longer
24346 referenced. Prune them. */
24347 prune_unused_types ();
24350 /* Generate separate CUs for each of the include files we've seen.
24351 They will go into limbo_die_list. */
24352 if (flag_eliminate_dwarf2_dups)
24353 break_out_includes (comp_unit_die ());
24355 /* Traverse the DIE's and add add sibling attributes to those DIE's
24356 that have children. */
24357 add_sibling_attributes (comp_unit_die ());
24358 for (node = limbo_die_list; node; node = node->next)
24359 add_sibling_attributes (node->die);
24360 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24361 add_sibling_attributes (ctnode->root_die);
24363 /* When splitting DWARF info, we put some attributes in the
24364 skeleton compile_unit DIE that remains in the .o, while
24365 most attributes go in the DWO compile_unit_die. */
24366 if (dwarf_split_debug_info)
24367 main_comp_unit_die = gen_compile_unit_die (NULL);
24368 else
24369 main_comp_unit_die = comp_unit_die ();
24371 /* Output a terminator label for the .text section. */
24372 switch_to_section (text_section);
24373 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24374 if (cold_text_section)
24376 switch_to_section (cold_text_section);
24377 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24380 /* We can only use the low/high_pc attributes if all of the code was
24381 in .text. */
24382 if (!have_multiple_function_sections
24383 || (dwarf_version < 3 && dwarf_strict))
24385 /* Don't add if the CU has no associated code. */
24386 if (text_section_used)
24387 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
24388 text_end_label, true);
24390 else
24392 unsigned fde_idx;
24393 dw_fde_ref fde;
24394 bool range_list_added = false;
24396 if (text_section_used)
24397 add_ranges_by_labels (main_comp_unit_die, text_section_label,
24398 text_end_label, &range_list_added, true);
24399 if (cold_text_section_used)
24400 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
24401 cold_end_label, &range_list_added, true);
24403 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
24405 if (DECL_IGNORED_P (fde->decl))
24406 continue;
24407 if (!fde->in_std_section)
24408 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
24409 fde->dw_fde_end, &range_list_added,
24410 true);
24411 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24412 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
24413 fde->dw_fde_second_end, &range_list_added,
24414 true);
24417 if (range_list_added)
24419 /* We need to give .debug_loc and .debug_ranges an appropriate
24420 "base address". Use zero so that these addresses become
24421 absolute. Historically, we've emitted the unexpected
24422 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24423 Emit both to give time for other tools to adapt. */
24424 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
24425 if (! dwarf_strict && dwarf_version < 4)
24426 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
24428 add_ranges (NULL);
24432 if (debug_info_level >= DINFO_LEVEL_TERSE)
24433 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
24434 debug_line_section_label);
24436 if (have_macinfo)
24437 add_AT_macptr (comp_unit_die (),
24438 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
24439 macinfo_section_label);
24441 if (dwarf_split_debug_info)
24443 /* optimize_location_lists calculates the size of the lists,
24444 so index them first, and assign indices to the entries.
24445 Although optimize_location_lists will remove entries from
24446 the table, it only does so for duplicates, and therefore
24447 only reduces ref_counts to 1. */
24448 index_location_lists (comp_unit_die ());
24450 if (addr_index_table != NULL)
24452 unsigned int index = 0;
24453 addr_index_table
24454 ->traverse_noresize<unsigned int *, index_addr_table_entry>
24455 (&index);
24459 if (have_location_lists)
24460 optimize_location_lists (comp_unit_die ());
24462 save_macinfo_strings ();
24464 if (dwarf_split_debug_info)
24466 unsigned int index = 0;
24468 /* Add attributes common to skeleton compile_units and
24469 type_units. Because these attributes include strings, it
24470 must be done before freezing the string table. Top-level
24471 skeleton die attrs are added when the skeleton type unit is
24472 created, so ensure it is created by this point. */
24473 add_top_level_skeleton_die_attrs (main_comp_unit_die);
24474 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
24477 /* Output all of the compilation units. We put the main one last so that
24478 the offsets are available to output_pubnames. */
24479 for (node = limbo_die_list; node; node = node->next)
24480 output_comp_unit (node->die, 0);
24482 hash_table<comdat_type_hasher> comdat_type_table (100);
24483 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24485 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
24487 /* Don't output duplicate types. */
24488 if (*slot != HTAB_EMPTY_ENTRY)
24489 continue;
24491 /* Add a pointer to the line table for the main compilation unit
24492 so that the debugger can make sense of DW_AT_decl_file
24493 attributes. */
24494 if (debug_info_level >= DINFO_LEVEL_TERSE)
24495 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24496 (!dwarf_split_debug_info
24497 ? debug_line_section_label
24498 : debug_skeleton_line_section_label));
24500 output_comdat_type_unit (ctnode);
24501 *slot = ctnode;
24504 /* The AT_pubnames attribute needs to go in all skeleton dies, including
24505 both the main_cu and all skeleton TUs. Making this call unconditional
24506 would end up either adding a second copy of the AT_pubnames attribute, or
24507 requiring a special case in add_top_level_skeleton_die_attrs. */
24508 if (!dwarf_split_debug_info)
24509 add_AT_pubnames (comp_unit_die ());
24511 if (dwarf_split_debug_info)
24513 int mark;
24514 unsigned char checksum[16];
24515 struct md5_ctx ctx;
24517 /* Compute a checksum of the comp_unit to use as the dwo_id. */
24518 md5_init_ctx (&ctx);
24519 mark = 0;
24520 die_checksum (comp_unit_die (), &ctx, &mark);
24521 unmark_all_dies (comp_unit_die ());
24522 md5_finish_ctx (&ctx, checksum);
24524 /* Use the first 8 bytes of the checksum as the dwo_id,
24525 and add it to both comp-unit DIEs. */
24526 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
24527 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
24529 /* Add the base offset of the ranges table to the skeleton
24530 comp-unit DIE. */
24531 if (ranges_table_in_use)
24532 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
24533 ranges_section_label);
24535 switch_to_section (debug_addr_section);
24536 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
24537 output_addr_table ();
24540 /* Output the main compilation unit if non-empty or if .debug_macinfo
24541 or .debug_macro will be emitted. */
24542 output_comp_unit (comp_unit_die (), have_macinfo);
24544 if (dwarf_split_debug_info && info_section_emitted)
24545 output_skeleton_debug_sections (main_comp_unit_die);
24547 /* Output the abbreviation table. */
24548 if (abbrev_die_table_in_use != 1)
24550 switch_to_section (debug_abbrev_section);
24551 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24552 output_abbrev_section ();
24555 /* Output location list section if necessary. */
24556 if (have_location_lists)
24558 /* Output the location lists info. */
24559 switch_to_section (debug_loc_section);
24560 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24561 output_location_lists (comp_unit_die ());
24564 output_pubtables ();
24566 /* Output the address range information if a CU (.debug_info section)
24567 was emitted. We output an empty table even if we had no functions
24568 to put in it. This because the consumer has no way to tell the
24569 difference between an empty table that we omitted and failure to
24570 generate a table that would have contained data. */
24571 if (info_section_emitted)
24573 unsigned long aranges_length = size_of_aranges ();
24575 switch_to_section (debug_aranges_section);
24576 output_aranges (aranges_length);
24579 /* Output ranges section if necessary. */
24580 if (ranges_table_in_use)
24582 switch_to_section (debug_ranges_section);
24583 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24584 output_ranges ();
24587 /* Have to end the macro section. */
24588 if (have_macinfo)
24590 switch_to_section (debug_macinfo_section);
24591 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24592 output_macinfo ();
24593 dw2_asm_output_data (1, 0, "End compilation unit");
24596 /* Output the source line correspondence table. We must do this
24597 even if there is no line information. Otherwise, on an empty
24598 translation unit, we will generate a present, but empty,
24599 .debug_info section. IRIX 6.5 `nm' will then complain when
24600 examining the file. This is done late so that any filenames
24601 used by the debug_info section are marked as 'used'. */
24602 switch_to_section (debug_line_section);
24603 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24604 if (! DWARF2_ASM_LINE_DEBUG_INFO)
24605 output_line_info (false);
24607 if (dwarf_split_debug_info && info_section_emitted)
24609 switch_to_section (debug_skeleton_line_section);
24610 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
24611 output_line_info (true);
24614 /* If we emitted any indirect strings, output the string table too. */
24615 if (debug_str_hash || skeleton_debug_str_hash)
24616 output_indirect_strings ();
24619 #include "gt-dwarf2out.h"