gcc/
[official-gcc.git] / gcc / dwarf2out.c
blob11d11271c676ead6d9ca9d440c74807936af9d38
1 /* Output Dwarf2 format symbol table information from GCC.
2 Copyright (C) 1992-2015 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 "alias.h"
64 #include "symtab.h"
65 #include "tree.h"
66 #include "fold-const.h"
67 #include "stringpool.h"
68 #include "stor-layout.h"
69 #include "varasm.h"
70 #include "hard-reg-set.h"
71 #include "function.h"
72 #include "emit-rtl.h"
73 #include "version.h"
74 #include "flags.h"
75 #include "regs.h"
76 #include "rtlhash.h"
77 #include "insn-config.h"
78 #include "reload.h"
79 #include "output.h"
80 #include "expmed.h"
81 #include "dojump.h"
82 #include "explow.h"
83 #include "calls.h"
84 #include "stmt.h"
85 #include "expr.h"
86 #include "except.h"
87 #include "dwarf2.h"
88 #include "dwarf2out.h"
89 #include "dwarf2asm.h"
90 #include "toplev.h"
91 #include "md5.h"
92 #include "tm_p.h"
93 #include "diagnostic.h"
94 #include "tree-pretty-print.h"
95 #include "debug.h"
96 #include "target.h"
97 #include "common/common-target.h"
98 #include "langhooks.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "ira.h"
103 #include "lra.h"
104 #include "dumpfile.h"
105 #include "opts.h"
106 #include "tree-dfa.h"
107 #include "gdb/gdb-index.h"
108 #include "rtl-iter.h"
110 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
111 static rtx_insn *last_var_location_insn;
112 static rtx_insn *cached_next_real_insn;
113 static void dwarf2out_decl (tree);
115 #ifdef VMS_DEBUGGING_INFO
116 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
118 /* Define this macro to be a nonzero value if the directory specifications
119 which are output in the debug info should end with a separator. */
120 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
121 /* Define this macro to evaluate to a nonzero value if GCC should refrain
122 from generating indirect strings in DWARF2 debug information, for instance
123 if your target is stuck with an old version of GDB that is unable to
124 process them properly or uses VMS Debug. */
125 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
126 #else
127 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
128 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
129 #endif
131 /* ??? Poison these here until it can be done generically. They've been
132 totally replaced in this file; make sure it stays that way. */
133 #undef DWARF2_UNWIND_INFO
134 #undef DWARF2_FRAME_INFO
135 #if (GCC_VERSION >= 3000)
136 #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
137 #endif
139 /* The size of the target's pointer type. */
140 #ifndef PTR_SIZE
141 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
142 #endif
144 /* Array of RTXes referenced by the debugging information, which therefore
145 must be kept around forever. */
146 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
148 /* A pointer to the base of a list of incomplete types which might be
149 completed at some later time. incomplete_types_list needs to be a
150 vec<tree, va_gc> *because we want to tell the garbage collector about
151 it. */
152 static GTY(()) vec<tree, va_gc> *incomplete_types;
154 /* A pointer to the base of a table of references to declaration
155 scopes. This table is a display which tracks the nesting
156 of declaration scopes at the current scope and containing
157 scopes. This table is used to find the proper place to
158 define type declaration DIE's. */
159 static GTY(()) vec<tree, va_gc> *decl_scope_table;
161 /* Pointers to various DWARF2 sections. */
162 static GTY(()) section *debug_info_section;
163 static GTY(()) section *debug_skeleton_info_section;
164 static GTY(()) section *debug_abbrev_section;
165 static GTY(()) section *debug_skeleton_abbrev_section;
166 static GTY(()) section *debug_aranges_section;
167 static GTY(()) section *debug_addr_section;
168 static GTY(()) section *debug_macinfo_section;
169 static GTY(()) section *debug_line_section;
170 static GTY(()) section *debug_skeleton_line_section;
171 static GTY(()) section *debug_loc_section;
172 static GTY(()) section *debug_pubnames_section;
173 static GTY(()) section *debug_pubtypes_section;
174 static GTY(()) section *debug_str_section;
175 static GTY(()) section *debug_str_dwo_section;
176 static GTY(()) section *debug_str_offsets_section;
177 static GTY(()) section *debug_ranges_section;
178 static GTY(()) section *debug_frame_section;
180 /* Maximum size (in bytes) of an artificially generated label. */
181 #define MAX_ARTIFICIAL_LABEL_BYTES 30
183 /* According to the (draft) DWARF 3 specification, the initial length
184 should either be 4 or 12 bytes. When it's 12 bytes, the first 4
185 bytes are 0xffffffff, followed by the length stored in the next 8
186 bytes.
188 However, the SGI/MIPS ABI uses an initial length which is equal to
189 DWARF_OFFSET_SIZE. It is defined (elsewhere) accordingly. */
191 #ifndef DWARF_INITIAL_LENGTH_SIZE
192 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
193 #endif
195 /* Round SIZE up to the nearest BOUNDARY. */
196 #define DWARF_ROUND(SIZE,BOUNDARY) \
197 ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
199 /* CIE identifier. */
200 #if HOST_BITS_PER_WIDE_INT >= 64
201 #define DWARF_CIE_ID \
202 (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
203 #else
204 #define DWARF_CIE_ID DW_CIE_ID
205 #endif
208 /* A vector for a table that contains frame description
209 information for each routine. */
210 #define NOT_INDEXED (-1U)
211 #define NO_INDEX_ASSIGNED (-2U)
213 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
215 struct GTY((for_user)) indirect_string_node {
216 const char *str;
217 unsigned int refcount;
218 enum dwarf_form form;
219 char *label;
220 unsigned int index;
223 struct indirect_string_hasher : ggc_ptr_hash<indirect_string_node>
225 typedef const char *compare_type;
227 static hashval_t hash (indirect_string_node *);
228 static bool equal (indirect_string_node *, const char *);
231 static GTY (()) hash_table<indirect_string_hasher> *debug_str_hash;
233 /* With split_debug_info, both the comp_dir and dwo_name go in the
234 main object file, rather than the dwo, similar to the force_direct
235 parameter elsewhere but with additional complications:
237 1) The string is needed in both the main object file and the dwo.
238 That is, the comp_dir and dwo_name will appear in both places.
240 2) Strings can use three forms: DW_FORM_string, DW_FORM_strp or
241 DW_FORM_GNU_str_index.
243 3) GCC chooses the form to use late, depending on the size and
244 reference count.
246 Rather than forcing the all debug string handling functions and
247 callers to deal with these complications, simply use a separate,
248 special-cased string table for any attribute that should go in the
249 main object file. This limits the complexity to just the places
250 that need it. */
252 static GTY (()) hash_table<indirect_string_hasher> *skeleton_debug_str_hash;
254 static GTY(()) int dw2_string_counter;
256 /* True if the compilation unit places functions in more than one section. */
257 static GTY(()) bool have_multiple_function_sections = false;
259 /* Whether the default text and cold text sections have been used at all. */
261 static GTY(()) bool text_section_used = false;
262 static GTY(()) bool cold_text_section_used = false;
264 /* The default cold text section. */
265 static GTY(()) section *cold_text_section;
267 /* The DIE for C++14 'auto' in a function return type. */
268 static GTY(()) dw_die_ref auto_die;
270 /* The DIE for C++14 'decltype(auto)' in a function return type. */
271 static GTY(()) dw_die_ref decltype_auto_die;
273 /* Forward declarations for functions defined in this file. */
275 static char *stripattributes (const char *);
276 static void output_call_frame_info (int);
277 static void dwarf2out_note_section_used (void);
279 /* Personality decl of current unit. Used only when assembler does not support
280 personality CFI. */
281 static GTY(()) rtx current_unit_personality;
283 /* Data and reference forms for relocatable data. */
284 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
285 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
287 #ifndef DEBUG_FRAME_SECTION
288 #define DEBUG_FRAME_SECTION ".debug_frame"
289 #endif
291 #ifndef FUNC_BEGIN_LABEL
292 #define FUNC_BEGIN_LABEL "LFB"
293 #endif
295 #ifndef FUNC_END_LABEL
296 #define FUNC_END_LABEL "LFE"
297 #endif
299 #ifndef PROLOGUE_END_LABEL
300 #define PROLOGUE_END_LABEL "LPE"
301 #endif
303 #ifndef EPILOGUE_BEGIN_LABEL
304 #define EPILOGUE_BEGIN_LABEL "LEB"
305 #endif
307 #ifndef FRAME_BEGIN_LABEL
308 #define FRAME_BEGIN_LABEL "Lframe"
309 #endif
310 #define CIE_AFTER_SIZE_LABEL "LSCIE"
311 #define CIE_END_LABEL "LECIE"
312 #define FDE_LABEL "LSFDE"
313 #define FDE_AFTER_SIZE_LABEL "LASFDE"
314 #define FDE_END_LABEL "LEFDE"
315 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
316 #define LINE_NUMBER_END_LABEL "LELT"
317 #define LN_PROLOG_AS_LABEL "LASLTP"
318 #define LN_PROLOG_END_LABEL "LELTP"
319 #define DIE_LABEL_PREFIX "DW"
321 /* Match the base name of a file to the base name of a compilation unit. */
323 static int
324 matches_main_base (const char *path)
326 /* Cache the last query. */
327 static const char *last_path = NULL;
328 static int last_match = 0;
329 if (path != last_path)
331 const char *base;
332 int length = base_of_path (path, &base);
333 last_path = path;
334 last_match = (length == main_input_baselength
335 && memcmp (base, main_input_basename, length) == 0);
337 return last_match;
340 #ifdef DEBUG_DEBUG_STRUCT
342 static int
343 dump_struct_debug (tree type, enum debug_info_usage usage,
344 enum debug_struct_file criterion, int generic,
345 int matches, int result)
347 /* Find the type name. */
348 tree type_decl = TYPE_STUB_DECL (type);
349 tree t = type_decl;
350 const char *name = 0;
351 if (TREE_CODE (t) == TYPE_DECL)
352 t = DECL_NAME (t);
353 if (t)
354 name = IDENTIFIER_POINTER (t);
356 fprintf (stderr, " struct %d %s %s %s %s %d %p %s\n",
357 criterion,
358 DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
359 matches ? "bas" : "hdr",
360 generic ? "gen" : "ord",
361 usage == DINFO_USAGE_DFN ? ";" :
362 usage == DINFO_USAGE_DIR_USE ? "." : "*",
363 result,
364 (void*) type_decl, name);
365 return result;
367 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
368 dump_struct_debug (type, usage, criterion, generic, matches, result)
370 #else
372 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
373 (result)
375 #endif
377 /* Get the number of HOST_WIDE_INTs needed to represent the precision
378 of the number. */
380 static unsigned int
381 get_full_len (const wide_int &op)
383 return ((op.get_precision () + HOST_BITS_PER_WIDE_INT - 1)
384 / HOST_BITS_PER_WIDE_INT);
387 static bool
388 should_emit_struct_debug (tree type, enum debug_info_usage usage)
390 enum debug_struct_file criterion;
391 tree type_decl;
392 bool generic = lang_hooks.types.generic_p (type);
394 if (generic)
395 criterion = debug_struct_generic[usage];
396 else
397 criterion = debug_struct_ordinary[usage];
399 if (criterion == DINFO_STRUCT_FILE_NONE)
400 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
401 if (criterion == DINFO_STRUCT_FILE_ANY)
402 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
404 type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
406 if (type_decl != NULL)
408 if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
409 return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
411 if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
412 return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
415 return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
418 /* Return a pointer to a copy of the section string name S with all
419 attributes stripped off, and an asterisk prepended (for assemble_name). */
421 static inline char *
422 stripattributes (const char *s)
424 char *stripped = XNEWVEC (char, strlen (s) + 2);
425 char *p = stripped;
427 *p++ = '*';
429 while (*s && *s != ',')
430 *p++ = *s++;
432 *p = '\0';
433 return stripped;
436 /* Switch [BACK] to eh_frame_section. If we don't have an eh_frame_section,
437 switch to the data section instead, and write out a synthetic start label
438 for collect2 the first time around. */
440 static void
441 switch_to_eh_frame_section (bool back)
443 tree label;
445 #ifdef EH_FRAME_SECTION_NAME
446 if (eh_frame_section == 0)
448 int flags;
450 if (EH_TABLES_CAN_BE_READ_ONLY)
452 int fde_encoding;
453 int per_encoding;
454 int lsda_encoding;
456 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
457 /*global=*/0);
458 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
459 /*global=*/1);
460 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
461 /*global=*/0);
462 flags = ((! flag_pic
463 || ((fde_encoding & 0x70) != DW_EH_PE_absptr
464 && (fde_encoding & 0x70) != DW_EH_PE_aligned
465 && (per_encoding & 0x70) != DW_EH_PE_absptr
466 && (per_encoding & 0x70) != DW_EH_PE_aligned
467 && (lsda_encoding & 0x70) != DW_EH_PE_absptr
468 && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
469 ? 0 : SECTION_WRITE);
471 else
472 flags = SECTION_WRITE;
473 eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
475 #endif /* EH_FRAME_SECTION_NAME */
477 if (eh_frame_section)
478 switch_to_section (eh_frame_section);
479 else
481 /* We have no special eh_frame section. Put the information in
482 the data section and emit special labels to guide collect2. */
483 switch_to_section (data_section);
485 if (!back)
487 label = get_file_function_name ("F");
488 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
489 targetm.asm_out.globalize_label (asm_out_file,
490 IDENTIFIER_POINTER (label));
491 ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
496 /* Switch [BACK] to the eh or debug frame table section, depending on
497 FOR_EH. */
499 static void
500 switch_to_frame_table_section (int for_eh, bool back)
502 if (for_eh)
503 switch_to_eh_frame_section (back);
504 else
506 if (!debug_frame_section)
507 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
508 SECTION_DEBUG, NULL);
509 switch_to_section (debug_frame_section);
513 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used. */
515 enum dw_cfi_oprnd_type
516 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
518 switch (cfi)
520 case DW_CFA_nop:
521 case DW_CFA_GNU_window_save:
522 case DW_CFA_remember_state:
523 case DW_CFA_restore_state:
524 return dw_cfi_oprnd_unused;
526 case DW_CFA_set_loc:
527 case DW_CFA_advance_loc1:
528 case DW_CFA_advance_loc2:
529 case DW_CFA_advance_loc4:
530 case DW_CFA_MIPS_advance_loc8:
531 return dw_cfi_oprnd_addr;
533 case DW_CFA_offset:
534 case DW_CFA_offset_extended:
535 case DW_CFA_def_cfa:
536 case DW_CFA_offset_extended_sf:
537 case DW_CFA_def_cfa_sf:
538 case DW_CFA_restore:
539 case DW_CFA_restore_extended:
540 case DW_CFA_undefined:
541 case DW_CFA_same_value:
542 case DW_CFA_def_cfa_register:
543 case DW_CFA_register:
544 case DW_CFA_expression:
545 return dw_cfi_oprnd_reg_num;
547 case DW_CFA_def_cfa_offset:
548 case DW_CFA_GNU_args_size:
549 case DW_CFA_def_cfa_offset_sf:
550 return dw_cfi_oprnd_offset;
552 case DW_CFA_def_cfa_expression:
553 return dw_cfi_oprnd_loc;
555 default:
556 gcc_unreachable ();
560 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used. */
562 enum dw_cfi_oprnd_type
563 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
565 switch (cfi)
567 case DW_CFA_def_cfa:
568 case DW_CFA_def_cfa_sf:
569 case DW_CFA_offset:
570 case DW_CFA_offset_extended_sf:
571 case DW_CFA_offset_extended:
572 return dw_cfi_oprnd_offset;
574 case DW_CFA_register:
575 return dw_cfi_oprnd_reg_num;
577 case DW_CFA_expression:
578 return dw_cfi_oprnd_loc;
580 default:
581 return dw_cfi_oprnd_unused;
585 /* Output one FDE. */
587 static void
588 output_fde (dw_fde_ref fde, bool for_eh, bool second,
589 char *section_start_label, int fde_encoding, char *augmentation,
590 bool any_lsda_needed, int lsda_encoding)
592 const char *begin, *end;
593 static unsigned int j;
594 char l1[20], l2[20];
596 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
597 /* empty */ 0);
598 targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
599 for_eh + j);
600 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
601 ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
602 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
603 dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
604 " indicating 64-bit DWARF extension");
605 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
606 "FDE Length");
607 ASM_OUTPUT_LABEL (asm_out_file, l1);
609 if (for_eh)
610 dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
611 else
612 dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
613 debug_frame_section, "FDE CIE offset");
615 begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
616 end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
618 if (for_eh)
620 rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
621 SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
622 dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
623 "FDE initial location");
624 dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
625 end, begin, "FDE address range");
627 else
629 dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
630 dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
633 if (augmentation[0])
635 if (any_lsda_needed)
637 int size = size_of_encoded_value (lsda_encoding);
639 if (lsda_encoding == DW_EH_PE_aligned)
641 int offset = ( 4 /* Length */
642 + 4 /* CIE offset */
643 + 2 * size_of_encoded_value (fde_encoding)
644 + 1 /* Augmentation size */ );
645 int pad = -offset & (PTR_SIZE - 1);
647 size += pad;
648 gcc_assert (size_of_uleb128 (size) == 1);
651 dw2_asm_output_data_uleb128 (size, "Augmentation size");
653 if (fde->uses_eh_lsda)
655 ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
656 fde->funcdef_number);
657 dw2_asm_output_encoded_addr_rtx (lsda_encoding,
658 gen_rtx_SYMBOL_REF (Pmode, l1),
659 false,
660 "Language Specific Data Area");
662 else
664 if (lsda_encoding == DW_EH_PE_aligned)
665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
666 dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
667 "Language Specific Data Area (none)");
670 else
671 dw2_asm_output_data_uleb128 (0, "Augmentation size");
674 /* Loop through the Call Frame Instructions associated with this FDE. */
675 fde->dw_fde_current_label = begin;
677 size_t from, until, i;
679 from = 0;
680 until = vec_safe_length (fde->dw_fde_cfi);
682 if (fde->dw_fde_second_begin == NULL)
684 else if (!second)
685 until = fde->dw_fde_switch_cfi_index;
686 else
687 from = fde->dw_fde_switch_cfi_index;
689 for (i = from; i < until; i++)
690 output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
693 /* If we are to emit a ref/link from function bodies to their frame tables,
694 do it now. This is typically performed to make sure that tables
695 associated with functions are dragged with them and not discarded in
696 garbage collecting links. We need to do this on a per function basis to
697 cope with -ffunction-sections. */
699 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
700 /* Switch to the function section, emit the ref to the tables, and
701 switch *back* into the table section. */
702 switch_to_section (function_section (fde->decl));
703 ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
704 switch_to_frame_table_section (for_eh, true);
705 #endif
707 /* Pad the FDE out to an address sized boundary. */
708 ASM_OUTPUT_ALIGN (asm_out_file,
709 floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
710 ASM_OUTPUT_LABEL (asm_out_file, l2);
712 j += 2;
715 /* Return true if frame description entry FDE is needed for EH. */
717 static bool
718 fde_needed_for_eh_p (dw_fde_ref fde)
720 if (flag_asynchronous_unwind_tables)
721 return true;
723 if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
724 return true;
726 if (fde->uses_eh_lsda)
727 return true;
729 /* If exceptions are enabled, we have collected nothrow info. */
730 if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
731 return false;
733 return true;
736 /* Output the call frame information used to record information
737 that relates to calculating the frame pointer, and records the
738 location of saved registers. */
740 static void
741 output_call_frame_info (int for_eh)
743 unsigned int i;
744 dw_fde_ref fde;
745 dw_cfi_ref cfi;
746 char l1[20], l2[20], section_start_label[20];
747 bool any_lsda_needed = false;
748 char augmentation[6];
749 int augmentation_size;
750 int fde_encoding = DW_EH_PE_absptr;
751 int per_encoding = DW_EH_PE_absptr;
752 int lsda_encoding = DW_EH_PE_absptr;
753 int return_reg;
754 rtx personality = NULL;
755 int dw_cie_version;
757 /* Don't emit a CIE if there won't be any FDEs. */
758 if (!fde_vec)
759 return;
761 /* Nothing to do if the assembler's doing it all. */
762 if (dwarf2out_do_cfi_asm ())
763 return;
765 /* If we don't have any functions we'll want to unwind out of, don't emit
766 any EH unwind information. If we make FDEs linkonce, we may have to
767 emit an empty label for an FDE that wouldn't otherwise be emitted. We
768 want to avoid having an FDE kept around when the function it refers to
769 is discarded. Example where this matters: a primary function template
770 in C++ requires EH information, an explicit specialization doesn't. */
771 if (for_eh)
773 bool any_eh_needed = false;
775 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
777 if (fde->uses_eh_lsda)
778 any_eh_needed = any_lsda_needed = true;
779 else if (fde_needed_for_eh_p (fde))
780 any_eh_needed = true;
781 else if (TARGET_USES_WEAK_UNWIND_INFO)
782 targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
785 if (!any_eh_needed)
786 return;
789 /* We're going to be generating comments, so turn on app. */
790 if (flag_debug_asm)
791 app_enable ();
793 /* Switch to the proper frame section, first time. */
794 switch_to_frame_table_section (for_eh, false);
796 ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
797 ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
799 /* Output the CIE. */
800 ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
801 ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
802 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
803 dw2_asm_output_data (4, 0xffffffff,
804 "Initial length escape value indicating 64-bit DWARF extension");
805 dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
806 "Length of Common Information Entry");
807 ASM_OUTPUT_LABEL (asm_out_file, l1);
809 /* Now that the CIE pointer is PC-relative for EH,
810 use 0 to identify the CIE. */
811 dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
812 (for_eh ? 0 : DWARF_CIE_ID),
813 "CIE Identifier Tag");
815 /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
816 use CIE version 1, unless that would produce incorrect results
817 due to overflowing the return register column. */
818 return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
819 dw_cie_version = 1;
820 if (return_reg >= 256 || dwarf_version > 2)
821 dw_cie_version = 3;
822 dw2_asm_output_data (1, dw_cie_version, "CIE Version");
824 augmentation[0] = 0;
825 augmentation_size = 0;
827 personality = current_unit_personality;
828 if (for_eh)
830 char *p;
832 /* Augmentation:
833 z Indicates that a uleb128 is present to size the
834 augmentation section.
835 L Indicates the encoding (and thus presence) of
836 an LSDA pointer in the FDE augmentation.
837 R Indicates a non-default pointer encoding for
838 FDE code pointers.
839 P Indicates the presence of an encoding + language
840 personality routine in the CIE augmentation. */
842 fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
843 per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
844 lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
846 p = augmentation + 1;
847 if (personality)
849 *p++ = 'P';
850 augmentation_size += 1 + size_of_encoded_value (per_encoding);
851 assemble_external_libcall (personality);
853 if (any_lsda_needed)
855 *p++ = 'L';
856 augmentation_size += 1;
858 if (fde_encoding != DW_EH_PE_absptr)
860 *p++ = 'R';
861 augmentation_size += 1;
863 if (p > augmentation + 1)
865 augmentation[0] = 'z';
866 *p = '\0';
869 /* Ug. Some platforms can't do unaligned dynamic relocations at all. */
870 if (personality && per_encoding == DW_EH_PE_aligned)
872 int offset = ( 4 /* Length */
873 + 4 /* CIE Id */
874 + 1 /* CIE version */
875 + strlen (augmentation) + 1 /* Augmentation */
876 + size_of_uleb128 (1) /* Code alignment */
877 + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
878 + 1 /* RA column */
879 + 1 /* Augmentation size */
880 + 1 /* Personality encoding */ );
881 int pad = -offset & (PTR_SIZE - 1);
883 augmentation_size += pad;
885 /* Augmentations should be small, so there's scarce need to
886 iterate for a solution. Die if we exceed one uleb128 byte. */
887 gcc_assert (size_of_uleb128 (augmentation_size) == 1);
891 dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
892 if (dw_cie_version >= 4)
894 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
895 dw2_asm_output_data (1, 0, "CIE Segment Size");
897 dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
898 dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
899 "CIE Data Alignment Factor");
901 if (dw_cie_version == 1)
902 dw2_asm_output_data (1, return_reg, "CIE RA Column");
903 else
904 dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
906 if (augmentation[0])
908 dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
909 if (personality)
911 dw2_asm_output_data (1, per_encoding, "Personality (%s)",
912 eh_data_format_name (per_encoding));
913 dw2_asm_output_encoded_addr_rtx (per_encoding,
914 personality,
915 true, NULL);
918 if (any_lsda_needed)
919 dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
920 eh_data_format_name (lsda_encoding));
922 if (fde_encoding != DW_EH_PE_absptr)
923 dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
924 eh_data_format_name (fde_encoding));
927 FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
928 output_cfi (cfi, NULL, for_eh);
930 /* Pad the CIE out to an address sized boundary. */
931 ASM_OUTPUT_ALIGN (asm_out_file,
932 floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
933 ASM_OUTPUT_LABEL (asm_out_file, l2);
935 /* Loop through all of the FDE's. */
936 FOR_EACH_VEC_ELT (*fde_vec, i, fde)
938 unsigned int k;
940 /* Don't emit EH unwind info for leaf functions that don't need it. */
941 if (for_eh && !fde_needed_for_eh_p (fde))
942 continue;
944 for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
945 output_fde (fde, for_eh, k, section_start_label, fde_encoding,
946 augmentation, any_lsda_needed, lsda_encoding);
949 if (for_eh && targetm.terminate_dw2_eh_frame_info)
950 dw2_asm_output_data (4, 0, "End of Table");
952 /* Turn off app to make assembly quicker. */
953 if (flag_debug_asm)
954 app_disable ();
957 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed. */
959 static void
960 dwarf2out_do_cfi_startproc (bool second)
962 int enc;
963 rtx ref;
964 rtx personality = get_personality_function (current_function_decl);
966 fprintf (asm_out_file, "\t.cfi_startproc\n");
968 if (personality)
970 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
971 ref = personality;
973 /* ??? The GAS support isn't entirely consistent. We have to
974 handle indirect support ourselves, but PC-relative is done
975 in the assembler. Further, the assembler can't handle any
976 of the weirder relocation types. */
977 if (enc & DW_EH_PE_indirect)
978 ref = dw2_force_const_mem (ref, true);
980 fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
981 output_addr_const (asm_out_file, ref);
982 fputc ('\n', asm_out_file);
985 if (crtl->uses_eh_lsda)
987 char lab[20];
989 enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
990 ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
991 current_function_funcdef_no);
992 ref = gen_rtx_SYMBOL_REF (Pmode, lab);
993 SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
995 if (enc & DW_EH_PE_indirect)
996 ref = dw2_force_const_mem (ref, true);
998 fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
999 output_addr_const (asm_out_file, ref);
1000 fputc ('\n', asm_out_file);
1004 /* Allocate CURRENT_FDE. Immediately initialize all we can, noting that
1005 this allocation may be done before pass_final. */
1007 dw_fde_ref
1008 dwarf2out_alloc_current_fde (void)
1010 dw_fde_ref fde;
1012 fde = ggc_cleared_alloc<dw_fde_node> ();
1013 fde->decl = current_function_decl;
1014 fde->funcdef_number = current_function_funcdef_no;
1015 fde->fde_index = vec_safe_length (fde_vec);
1016 fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1017 fde->uses_eh_lsda = crtl->uses_eh_lsda;
1018 fde->nothrow = crtl->nothrow;
1019 fde->drap_reg = INVALID_REGNUM;
1020 fde->vdrap_reg = INVALID_REGNUM;
1022 /* Record the FDE associated with this function. */
1023 cfun->fde = fde;
1024 vec_safe_push (fde_vec, fde);
1026 return fde;
1029 /* Output a marker (i.e. a label) for the beginning of a function, before
1030 the prologue. */
1032 void
1033 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1034 const char *file ATTRIBUTE_UNUSED)
1036 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1037 char * dup_label;
1038 dw_fde_ref fde;
1039 section *fnsec;
1040 bool do_frame;
1042 current_function_func_begin_label = NULL;
1044 do_frame = dwarf2out_do_frame ();
1046 /* ??? current_function_func_begin_label is also used by except.c for
1047 call-site information. We must emit this label if it might be used. */
1048 if (!do_frame
1049 && (!flag_exceptions
1050 || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
1051 return;
1053 fnsec = function_section (current_function_decl);
1054 switch_to_section (fnsec);
1055 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1056 current_function_funcdef_no);
1057 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1058 current_function_funcdef_no);
1059 dup_label = xstrdup (label);
1060 current_function_func_begin_label = dup_label;
1062 /* We can elide the fde allocation if we're not emitting debug info. */
1063 if (!do_frame)
1064 return;
1066 /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1067 emit insns as rtx but bypass the bulk of rest_of_compilation, which
1068 would include pass_dwarf2_frame. If we've not created the FDE yet,
1069 do so now. */
1070 fde = cfun->fde;
1071 if (fde == NULL)
1072 fde = dwarf2out_alloc_current_fde ();
1074 /* Initialize the bits of CURRENT_FDE that were not available earlier. */
1075 fde->dw_fde_begin = dup_label;
1076 fde->dw_fde_current_label = dup_label;
1077 fde->in_std_section = (fnsec == text_section
1078 || (cold_text_section && fnsec == cold_text_section));
1080 /* We only want to output line number information for the genuine dwarf2
1081 prologue case, not the eh frame case. */
1082 #ifdef DWARF2_DEBUGGING_INFO
1083 if (file)
1084 dwarf2out_source_line (line, file, 0, true);
1085 #endif
1087 if (dwarf2out_do_cfi_asm ())
1088 dwarf2out_do_cfi_startproc (false);
1089 else
1091 rtx personality = get_personality_function (current_function_decl);
1092 if (!current_unit_personality)
1093 current_unit_personality = personality;
1095 /* We cannot keep a current personality per function as without CFI
1096 asm, at the point where we emit the CFI data, there is no current
1097 function anymore. */
1098 if (personality && current_unit_personality != personality)
1099 sorry ("multiple EH personalities are supported only with assemblers "
1100 "supporting .cfi_personality directive");
1104 /* Output a marker (i.e. a label) for the end of the generated code
1105 for a function prologue. This gets called *after* the prologue code has
1106 been generated. */
1108 void
1109 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1110 const char *file ATTRIBUTE_UNUSED)
1112 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1114 /* Output a label to mark the endpoint of the code generated for this
1115 function. */
1116 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1117 current_function_funcdef_no);
1118 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1119 current_function_funcdef_no);
1120 cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1123 /* Output a marker (i.e. a label) for the beginning of the generated code
1124 for a function epilogue. This gets called *before* the prologue code has
1125 been generated. */
1127 void
1128 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1129 const char *file ATTRIBUTE_UNUSED)
1131 dw_fde_ref fde = cfun->fde;
1132 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1134 if (fde->dw_fde_vms_begin_epilogue)
1135 return;
1137 /* Output a label to mark the endpoint of the code generated for this
1138 function. */
1139 ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1140 current_function_funcdef_no);
1141 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1142 current_function_funcdef_no);
1143 fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1146 /* Output a marker (i.e. a label) for the absolute end of the generated code
1147 for a function definition. This gets called *after* the epilogue code has
1148 been generated. */
1150 void
1151 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1152 const char *file ATTRIBUTE_UNUSED)
1154 dw_fde_ref fde;
1155 char label[MAX_ARTIFICIAL_LABEL_BYTES];
1157 last_var_location_insn = NULL;
1158 cached_next_real_insn = NULL;
1160 if (dwarf2out_do_cfi_asm ())
1161 fprintf (asm_out_file, "\t.cfi_endproc\n");
1163 /* Output a label to mark the endpoint of the code generated for this
1164 function. */
1165 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1166 current_function_funcdef_no);
1167 ASM_OUTPUT_LABEL (asm_out_file, label);
1168 fde = cfun->fde;
1169 gcc_assert (fde != NULL);
1170 if (fde->dw_fde_second_begin == NULL)
1171 fde->dw_fde_end = xstrdup (label);
1174 void
1175 dwarf2out_frame_finish (void)
1177 /* Output call frame information. */
1178 if (targetm.debug_unwind_info () == UI_DWARF2)
1179 output_call_frame_info (0);
1181 /* Output another copy for the unwinder. */
1182 if ((flag_unwind_tables || flag_exceptions)
1183 && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1184 output_call_frame_info (1);
1187 /* Note that the current function section is being used for code. */
1189 static void
1190 dwarf2out_note_section_used (void)
1192 section *sec = current_function_section ();
1193 if (sec == text_section)
1194 text_section_used = true;
1195 else if (sec == cold_text_section)
1196 cold_text_section_used = true;
1199 static void var_location_switch_text_section (void);
1200 static void set_cur_line_info_table (section *);
1202 void
1203 dwarf2out_switch_text_section (void)
1205 section *sect;
1206 dw_fde_ref fde = cfun->fde;
1208 gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1210 if (!in_cold_section_p)
1212 fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1213 fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1214 fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1216 else
1218 fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1219 fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1220 fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1222 have_multiple_function_sections = true;
1224 /* There is no need to mark used sections when not debugging. */
1225 if (cold_text_section != NULL)
1226 dwarf2out_note_section_used ();
1228 if (dwarf2out_do_cfi_asm ())
1229 fprintf (asm_out_file, "\t.cfi_endproc\n");
1231 /* Now do the real section switch. */
1232 sect = current_function_section ();
1233 switch_to_section (sect);
1235 fde->second_in_std_section
1236 = (sect == text_section
1237 || (cold_text_section && sect == cold_text_section));
1239 if (dwarf2out_do_cfi_asm ())
1240 dwarf2out_do_cfi_startproc (true);
1242 var_location_switch_text_section ();
1244 if (cold_text_section != NULL)
1245 set_cur_line_info_table (sect);
1248 /* And now, the subset of the debugging information support code necessary
1249 for emitting location expressions. */
1251 /* Data about a single source file. */
1252 struct GTY((for_user)) dwarf_file_data {
1253 const char * filename;
1254 int emitted_number;
1257 /* Describe an entry into the .debug_addr section. */
1259 enum ate_kind {
1260 ate_kind_rtx,
1261 ate_kind_rtx_dtprel,
1262 ate_kind_label
1265 typedef struct GTY((for_user)) addr_table_entry_struct {
1266 enum ate_kind kind;
1267 unsigned int refcount;
1268 unsigned int index;
1269 union addr_table_entry_struct_union
1271 rtx GTY ((tag ("0"))) rtl;
1272 char * GTY ((tag ("1"))) label;
1274 GTY ((desc ("%1.kind"))) addr;
1276 addr_table_entry;
1278 /* Location lists are ranges + location descriptions for that range,
1279 so you can track variables that are in different places over
1280 their entire life. */
1281 typedef struct GTY(()) dw_loc_list_struct {
1282 dw_loc_list_ref dw_loc_next;
1283 const char *begin; /* Label and addr_entry for start of range */
1284 addr_table_entry *begin_entry;
1285 const char *end; /* Label for end of range */
1286 char *ll_symbol; /* Label for beginning of location list.
1287 Only on head of list */
1288 const char *section; /* Section this loclist is relative to */
1289 dw_loc_descr_ref expr;
1290 hashval_t hash;
1291 /* True if all addresses in this and subsequent lists are known to be
1292 resolved. */
1293 bool resolved_addr;
1294 /* True if this list has been replaced by dw_loc_next. */
1295 bool replaced;
1296 bool emitted;
1297 /* True if the range should be emitted even if begin and end
1298 are the same. */
1299 bool force;
1300 } dw_loc_list_node;
1302 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1304 /* Convert a DWARF stack opcode into its string name. */
1306 static const char *
1307 dwarf_stack_op_name (unsigned int op)
1309 const char *name = get_DW_OP_name (op);
1311 if (name != NULL)
1312 return name;
1314 return "OP_<unknown>";
1317 /* Return a pointer to a newly allocated location description. Location
1318 descriptions are simple expression terms that can be strung
1319 together to form more complicated location (address) descriptions. */
1321 static inline dw_loc_descr_ref
1322 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1323 unsigned HOST_WIDE_INT oprnd2)
1325 dw_loc_descr_ref descr = ggc_cleared_alloc<dw_loc_descr_node> ();
1327 descr->dw_loc_opc = op;
1328 descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1329 descr->dw_loc_oprnd1.val_entry = NULL;
1330 descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1331 descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1332 descr->dw_loc_oprnd2.val_entry = NULL;
1333 descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1335 return descr;
1338 /* Return a pointer to a newly allocated location description for
1339 REG and OFFSET. */
1341 static inline dw_loc_descr_ref
1342 new_reg_loc_descr (unsigned int reg, unsigned HOST_WIDE_INT offset)
1344 if (reg <= 31)
1345 return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1346 offset, 0);
1347 else
1348 return new_loc_descr (DW_OP_bregx, reg, offset);
1351 /* Add a location description term to a location description expression. */
1353 static inline void
1354 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1356 dw_loc_descr_ref *d;
1358 /* Find the end of the chain. */
1359 for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1362 *d = descr;
1365 /* Compare two location operands for exact equality. */
1367 static bool
1368 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1370 if (a->val_class != b->val_class)
1371 return false;
1372 switch (a->val_class)
1374 case dw_val_class_none:
1375 return true;
1376 case dw_val_class_addr:
1377 return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1379 case dw_val_class_offset:
1380 case dw_val_class_unsigned_const:
1381 case dw_val_class_const:
1382 case dw_val_class_range_list:
1383 case dw_val_class_lineptr:
1384 case dw_val_class_macptr:
1385 /* These are all HOST_WIDE_INT, signed or unsigned. */
1386 return a->v.val_unsigned == b->v.val_unsigned;
1388 case dw_val_class_loc:
1389 return a->v.val_loc == b->v.val_loc;
1390 case dw_val_class_loc_list:
1391 return a->v.val_loc_list == b->v.val_loc_list;
1392 case dw_val_class_die_ref:
1393 return a->v.val_die_ref.die == b->v.val_die_ref.die;
1394 case dw_val_class_fde_ref:
1395 return a->v.val_fde_index == b->v.val_fde_index;
1396 case dw_val_class_lbl_id:
1397 case dw_val_class_high_pc:
1398 return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1399 case dw_val_class_str:
1400 return a->v.val_str == b->v.val_str;
1401 case dw_val_class_flag:
1402 return a->v.val_flag == b->v.val_flag;
1403 case dw_val_class_file:
1404 return a->v.val_file == b->v.val_file;
1405 case dw_val_class_decl_ref:
1406 return a->v.val_decl_ref == b->v.val_decl_ref;
1408 case dw_val_class_const_double:
1409 return (a->v.val_double.high == b->v.val_double.high
1410 && a->v.val_double.low == b->v.val_double.low);
1412 case dw_val_class_wide_int:
1413 return *a->v.val_wide == *b->v.val_wide;
1415 case dw_val_class_vec:
1417 size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1418 size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1420 return (a_len == b_len
1421 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1424 case dw_val_class_data8:
1425 return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1427 case dw_val_class_vms_delta:
1428 return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1429 && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1431 gcc_unreachable ();
1434 /* Compare two location atoms for exact equality. */
1436 static bool
1437 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1439 if (a->dw_loc_opc != b->dw_loc_opc)
1440 return false;
1442 /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1443 address size, but since we always allocate cleared storage it
1444 should be zero for other types of locations. */
1445 if (a->dtprel != b->dtprel)
1446 return false;
1448 return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1449 && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1452 /* Compare two complete location expressions for exact equality. */
1454 bool
1455 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1457 while (1)
1459 if (a == b)
1460 return true;
1461 if (a == NULL || b == NULL)
1462 return false;
1463 if (!loc_descr_equal_p_1 (a, b))
1464 return false;
1466 a = a->dw_loc_next;
1467 b = b->dw_loc_next;
1472 /* Add a constant OFFSET to a location expression. */
1474 static void
1475 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1477 dw_loc_descr_ref loc;
1478 HOST_WIDE_INT *p;
1480 gcc_assert (*list_head != NULL);
1482 if (!offset)
1483 return;
1485 /* Find the end of the chain. */
1486 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1489 p = NULL;
1490 if (loc->dw_loc_opc == DW_OP_fbreg
1491 || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1492 p = &loc->dw_loc_oprnd1.v.val_int;
1493 else if (loc->dw_loc_opc == DW_OP_bregx)
1494 p = &loc->dw_loc_oprnd2.v.val_int;
1496 /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1497 offset. Don't optimize if an signed integer overflow would happen. */
1498 if (p != NULL
1499 && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1500 || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1501 *p += offset;
1503 else if (offset > 0)
1504 loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1506 else
1508 loc->dw_loc_next = int_loc_descriptor (-offset);
1509 add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1513 /* Add a constant OFFSET to a location list. */
1515 static void
1516 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1518 dw_loc_list_ref d;
1519 for (d = list_head; d != NULL; d = d->dw_loc_next)
1520 loc_descr_plus_const (&d->expr, offset);
1523 #define DWARF_REF_SIZE \
1524 (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1526 static unsigned long int get_base_type_offset (dw_die_ref);
1528 /* Return the size of a location descriptor. */
1530 static unsigned long
1531 size_of_loc_descr (dw_loc_descr_ref loc)
1533 unsigned long size = 1;
1535 switch (loc->dw_loc_opc)
1537 case DW_OP_addr:
1538 size += DWARF2_ADDR_SIZE;
1539 break;
1540 case DW_OP_GNU_addr_index:
1541 case DW_OP_GNU_const_index:
1542 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1543 size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1544 break;
1545 case DW_OP_const1u:
1546 case DW_OP_const1s:
1547 size += 1;
1548 break;
1549 case DW_OP_const2u:
1550 case DW_OP_const2s:
1551 size += 2;
1552 break;
1553 case DW_OP_const4u:
1554 case DW_OP_const4s:
1555 size += 4;
1556 break;
1557 case DW_OP_const8u:
1558 case DW_OP_const8s:
1559 size += 8;
1560 break;
1561 case DW_OP_constu:
1562 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1563 break;
1564 case DW_OP_consts:
1565 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1566 break;
1567 case DW_OP_pick:
1568 size += 1;
1569 break;
1570 case DW_OP_plus_uconst:
1571 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1572 break;
1573 case DW_OP_skip:
1574 case DW_OP_bra:
1575 size += 2;
1576 break;
1577 case DW_OP_breg0:
1578 case DW_OP_breg1:
1579 case DW_OP_breg2:
1580 case DW_OP_breg3:
1581 case DW_OP_breg4:
1582 case DW_OP_breg5:
1583 case DW_OP_breg6:
1584 case DW_OP_breg7:
1585 case DW_OP_breg8:
1586 case DW_OP_breg9:
1587 case DW_OP_breg10:
1588 case DW_OP_breg11:
1589 case DW_OP_breg12:
1590 case DW_OP_breg13:
1591 case DW_OP_breg14:
1592 case DW_OP_breg15:
1593 case DW_OP_breg16:
1594 case DW_OP_breg17:
1595 case DW_OP_breg18:
1596 case DW_OP_breg19:
1597 case DW_OP_breg20:
1598 case DW_OP_breg21:
1599 case DW_OP_breg22:
1600 case DW_OP_breg23:
1601 case DW_OP_breg24:
1602 case DW_OP_breg25:
1603 case DW_OP_breg26:
1604 case DW_OP_breg27:
1605 case DW_OP_breg28:
1606 case DW_OP_breg29:
1607 case DW_OP_breg30:
1608 case DW_OP_breg31:
1609 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1610 break;
1611 case DW_OP_regx:
1612 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1613 break;
1614 case DW_OP_fbreg:
1615 size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1616 break;
1617 case DW_OP_bregx:
1618 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1619 size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1620 break;
1621 case DW_OP_piece:
1622 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1623 break;
1624 case DW_OP_bit_piece:
1625 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1626 size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1627 break;
1628 case DW_OP_deref_size:
1629 case DW_OP_xderef_size:
1630 size += 1;
1631 break;
1632 case DW_OP_call2:
1633 size += 2;
1634 break;
1635 case DW_OP_call4:
1636 size += 4;
1637 break;
1638 case DW_OP_call_ref:
1639 size += DWARF_REF_SIZE;
1640 break;
1641 case DW_OP_implicit_value:
1642 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1643 + loc->dw_loc_oprnd1.v.val_unsigned;
1644 break;
1645 case DW_OP_GNU_implicit_pointer:
1646 size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1647 break;
1648 case DW_OP_GNU_entry_value:
1650 unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1651 size += size_of_uleb128 (op_size) + op_size;
1652 break;
1654 case DW_OP_GNU_const_type:
1656 unsigned long o
1657 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1658 size += size_of_uleb128 (o) + 1;
1659 switch (loc->dw_loc_oprnd2.val_class)
1661 case dw_val_class_vec:
1662 size += loc->dw_loc_oprnd2.v.val_vec.length
1663 * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1664 break;
1665 case dw_val_class_const:
1666 size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1667 break;
1668 case dw_val_class_const_double:
1669 size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1670 break;
1671 case dw_val_class_wide_int:
1672 size += (get_full_len (*loc->dw_loc_oprnd2.v.val_wide)
1673 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
1674 break;
1675 default:
1676 gcc_unreachable ();
1678 break;
1680 case DW_OP_GNU_regval_type:
1682 unsigned long o
1683 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1684 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1685 + size_of_uleb128 (o);
1687 break;
1688 case DW_OP_GNU_deref_type:
1690 unsigned long o
1691 = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1692 size += 1 + size_of_uleb128 (o);
1694 break;
1695 case DW_OP_GNU_convert:
1696 case DW_OP_GNU_reinterpret:
1697 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1698 size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1699 else
1701 unsigned long o
1702 = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1703 size += size_of_uleb128 (o);
1705 break;
1706 case DW_OP_GNU_parameter_ref:
1707 size += 4;
1708 break;
1709 default:
1710 break;
1713 return size;
1716 /* Return the size of a series of location descriptors. */
1718 unsigned long
1719 size_of_locs (dw_loc_descr_ref loc)
1721 dw_loc_descr_ref l;
1722 unsigned long size;
1724 /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1725 field, to avoid writing to a PCH file. */
1726 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1728 if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1729 break;
1730 size += size_of_loc_descr (l);
1732 if (! l)
1733 return size;
1735 for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1737 l->dw_loc_addr = size;
1738 size += size_of_loc_descr (l);
1741 return size;
1744 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1745 static void get_ref_die_offset_label (char *, dw_die_ref);
1746 static unsigned long int get_ref_die_offset (dw_die_ref);
1748 /* Output location description stack opcode's operands (if any).
1749 The for_eh_or_skip parameter controls whether register numbers are
1750 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1751 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1752 info). This should be suppressed for the cases that have not been converted
1753 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
1755 static void
1756 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1758 dw_val_ref val1 = &loc->dw_loc_oprnd1;
1759 dw_val_ref val2 = &loc->dw_loc_oprnd2;
1761 switch (loc->dw_loc_opc)
1763 #ifdef DWARF2_DEBUGGING_INFO
1764 case DW_OP_const2u:
1765 case DW_OP_const2s:
1766 dw2_asm_output_data (2, val1->v.val_int, NULL);
1767 break;
1768 case DW_OP_const4u:
1769 if (loc->dtprel)
1771 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1772 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1773 val1->v.val_addr);
1774 fputc ('\n', asm_out_file);
1775 break;
1777 /* FALLTHRU */
1778 case DW_OP_const4s:
1779 dw2_asm_output_data (4, val1->v.val_int, NULL);
1780 break;
1781 case DW_OP_const8u:
1782 if (loc->dtprel)
1784 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1785 targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1786 val1->v.val_addr);
1787 fputc ('\n', asm_out_file);
1788 break;
1790 /* FALLTHRU */
1791 case DW_OP_const8s:
1792 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1793 dw2_asm_output_data (8, val1->v.val_int, NULL);
1794 break;
1795 case DW_OP_skip:
1796 case DW_OP_bra:
1798 int offset;
1800 gcc_assert (val1->val_class == dw_val_class_loc);
1801 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1803 dw2_asm_output_data (2, offset, NULL);
1805 break;
1806 case DW_OP_implicit_value:
1807 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1808 switch (val2->val_class)
1810 case dw_val_class_const:
1811 dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1812 break;
1813 case dw_val_class_vec:
1815 unsigned int elt_size = val2->v.val_vec.elt_size;
1816 unsigned int len = val2->v.val_vec.length;
1817 unsigned int i;
1818 unsigned char *p;
1820 if (elt_size > sizeof (HOST_WIDE_INT))
1822 elt_size /= 2;
1823 len *= 2;
1825 for (i = 0, p = val2->v.val_vec.array;
1826 i < len;
1827 i++, p += elt_size)
1828 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1829 "fp or vector constant word %u", i);
1831 break;
1832 case dw_val_class_const_double:
1834 unsigned HOST_WIDE_INT first, second;
1836 if (WORDS_BIG_ENDIAN)
1838 first = val2->v.val_double.high;
1839 second = val2->v.val_double.low;
1841 else
1843 first = val2->v.val_double.low;
1844 second = val2->v.val_double.high;
1846 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1847 first, NULL);
1848 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1849 second, NULL);
1851 break;
1852 case dw_val_class_wide_int:
1854 int i;
1855 int len = get_full_len (*val2->v.val_wide);
1856 if (WORDS_BIG_ENDIAN)
1857 for (i = len - 1; i >= 0; --i)
1858 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1859 val2->v.val_wide->elt (i), NULL);
1860 else
1861 for (i = 0; i < len; ++i)
1862 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1863 val2->v.val_wide->elt (i), NULL);
1865 break;
1866 case dw_val_class_addr:
1867 gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1868 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1869 break;
1870 default:
1871 gcc_unreachable ();
1873 break;
1874 #else
1875 case DW_OP_const2u:
1876 case DW_OP_const2s:
1877 case DW_OP_const4u:
1878 case DW_OP_const4s:
1879 case DW_OP_const8u:
1880 case DW_OP_const8s:
1881 case DW_OP_skip:
1882 case DW_OP_bra:
1883 case DW_OP_implicit_value:
1884 /* We currently don't make any attempt to make sure these are
1885 aligned properly like we do for the main unwind info, so
1886 don't support emitting things larger than a byte if we're
1887 only doing unwinding. */
1888 gcc_unreachable ();
1889 #endif
1890 case DW_OP_const1u:
1891 case DW_OP_const1s:
1892 dw2_asm_output_data (1, val1->v.val_int, NULL);
1893 break;
1894 case DW_OP_constu:
1895 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1896 break;
1897 case DW_OP_consts:
1898 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1899 break;
1900 case DW_OP_pick:
1901 dw2_asm_output_data (1, val1->v.val_int, NULL);
1902 break;
1903 case DW_OP_plus_uconst:
1904 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1905 break;
1906 case DW_OP_breg0:
1907 case DW_OP_breg1:
1908 case DW_OP_breg2:
1909 case DW_OP_breg3:
1910 case DW_OP_breg4:
1911 case DW_OP_breg5:
1912 case DW_OP_breg6:
1913 case DW_OP_breg7:
1914 case DW_OP_breg8:
1915 case DW_OP_breg9:
1916 case DW_OP_breg10:
1917 case DW_OP_breg11:
1918 case DW_OP_breg12:
1919 case DW_OP_breg13:
1920 case DW_OP_breg14:
1921 case DW_OP_breg15:
1922 case DW_OP_breg16:
1923 case DW_OP_breg17:
1924 case DW_OP_breg18:
1925 case DW_OP_breg19:
1926 case DW_OP_breg20:
1927 case DW_OP_breg21:
1928 case DW_OP_breg22:
1929 case DW_OP_breg23:
1930 case DW_OP_breg24:
1931 case DW_OP_breg25:
1932 case DW_OP_breg26:
1933 case DW_OP_breg27:
1934 case DW_OP_breg28:
1935 case DW_OP_breg29:
1936 case DW_OP_breg30:
1937 case DW_OP_breg31:
1938 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1939 break;
1940 case DW_OP_regx:
1942 unsigned r = val1->v.val_unsigned;
1943 if (for_eh_or_skip >= 0)
1944 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1945 gcc_assert (size_of_uleb128 (r)
1946 == size_of_uleb128 (val1->v.val_unsigned));
1947 dw2_asm_output_data_uleb128 (r, NULL);
1949 break;
1950 case DW_OP_fbreg:
1951 dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1952 break;
1953 case DW_OP_bregx:
1955 unsigned r = val1->v.val_unsigned;
1956 if (for_eh_or_skip >= 0)
1957 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1958 gcc_assert (size_of_uleb128 (r)
1959 == size_of_uleb128 (val1->v.val_unsigned));
1960 dw2_asm_output_data_uleb128 (r, NULL);
1961 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1963 break;
1964 case DW_OP_piece:
1965 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1966 break;
1967 case DW_OP_bit_piece:
1968 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1969 dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1970 break;
1971 case DW_OP_deref_size:
1972 case DW_OP_xderef_size:
1973 dw2_asm_output_data (1, val1->v.val_int, NULL);
1974 break;
1976 case DW_OP_addr:
1977 if (loc->dtprel)
1979 if (targetm.asm_out.output_dwarf_dtprel)
1981 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1982 DWARF2_ADDR_SIZE,
1983 val1->v.val_addr);
1984 fputc ('\n', asm_out_file);
1986 else
1987 gcc_unreachable ();
1989 else
1991 #ifdef DWARF2_DEBUGGING_INFO
1992 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1993 #else
1994 gcc_unreachable ();
1995 #endif
1997 break;
1999 case DW_OP_GNU_addr_index:
2000 case DW_OP_GNU_const_index:
2001 gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
2002 dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
2003 "(index into .debug_addr)");
2004 break;
2006 case DW_OP_GNU_implicit_pointer:
2008 char label[MAX_ARTIFICIAL_LABEL_BYTES
2009 + HOST_BITS_PER_WIDE_INT / 2 + 2];
2010 gcc_assert (val1->val_class == dw_val_class_die_ref);
2011 get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2012 dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2013 dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2015 break;
2017 case DW_OP_GNU_entry_value:
2018 dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2019 output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2020 break;
2022 case DW_OP_GNU_const_type:
2024 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2025 gcc_assert (o);
2026 dw2_asm_output_data_uleb128 (o, NULL);
2027 switch (val2->val_class)
2029 case dw_val_class_const:
2030 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2031 dw2_asm_output_data (1, l, NULL);
2032 dw2_asm_output_data (l, val2->v.val_int, NULL);
2033 break;
2034 case dw_val_class_vec:
2036 unsigned int elt_size = val2->v.val_vec.elt_size;
2037 unsigned int len = val2->v.val_vec.length;
2038 unsigned int i;
2039 unsigned char *p;
2041 l = len * elt_size;
2042 dw2_asm_output_data (1, l, NULL);
2043 if (elt_size > sizeof (HOST_WIDE_INT))
2045 elt_size /= 2;
2046 len *= 2;
2048 for (i = 0, p = val2->v.val_vec.array;
2049 i < len;
2050 i++, p += elt_size)
2051 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2052 "fp or vector constant word %u", i);
2054 break;
2055 case dw_val_class_const_double:
2057 unsigned HOST_WIDE_INT first, second;
2058 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2060 dw2_asm_output_data (1, 2 * l, NULL);
2061 if (WORDS_BIG_ENDIAN)
2063 first = val2->v.val_double.high;
2064 second = val2->v.val_double.low;
2066 else
2068 first = val2->v.val_double.low;
2069 second = val2->v.val_double.high;
2071 dw2_asm_output_data (l, first, NULL);
2072 dw2_asm_output_data (l, second, NULL);
2074 break;
2075 case dw_val_class_wide_int:
2077 int i;
2078 int len = get_full_len (*val2->v.val_wide);
2079 l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2081 dw2_asm_output_data (1, len * l, NULL);
2082 if (WORDS_BIG_ENDIAN)
2083 for (i = len - 1; i >= 0; --i)
2084 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2085 else
2086 for (i = 0; i < len; ++i)
2087 dw2_asm_output_data (l, val2->v.val_wide->elt (i), NULL);
2089 break;
2090 default:
2091 gcc_unreachable ();
2094 break;
2095 case DW_OP_GNU_regval_type:
2097 unsigned r = val1->v.val_unsigned;
2098 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2099 gcc_assert (o);
2100 if (for_eh_or_skip >= 0)
2102 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2103 gcc_assert (size_of_uleb128 (r)
2104 == size_of_uleb128 (val1->v.val_unsigned));
2106 dw2_asm_output_data_uleb128 (r, NULL);
2107 dw2_asm_output_data_uleb128 (o, NULL);
2109 break;
2110 case DW_OP_GNU_deref_type:
2112 unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2113 gcc_assert (o);
2114 dw2_asm_output_data (1, val1->v.val_int, NULL);
2115 dw2_asm_output_data_uleb128 (o, NULL);
2117 break;
2118 case DW_OP_GNU_convert:
2119 case DW_OP_GNU_reinterpret:
2120 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2121 dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2122 else
2124 unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2125 gcc_assert (o);
2126 dw2_asm_output_data_uleb128 (o, NULL);
2128 break;
2130 case DW_OP_GNU_parameter_ref:
2132 unsigned long o;
2133 gcc_assert (val1->val_class == dw_val_class_die_ref);
2134 o = get_ref_die_offset (val1->v.val_die_ref.die);
2135 dw2_asm_output_data (4, o, NULL);
2137 break;
2139 default:
2140 /* Other codes have no operands. */
2141 break;
2145 /* Output a sequence of location operations.
2146 The for_eh_or_skip parameter controls whether register numbers are
2147 converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2148 hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2149 info). This should be suppressed for the cases that have not been converted
2150 (i.e. symbolic debug info), by setting the parameter < 0. See PR47324. */
2152 void
2153 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2155 for (; loc != NULL; loc = loc->dw_loc_next)
2157 enum dwarf_location_atom opc = loc->dw_loc_opc;
2158 /* Output the opcode. */
2159 if (for_eh_or_skip >= 0
2160 && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2162 unsigned r = (opc - DW_OP_breg0);
2163 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2164 gcc_assert (r <= 31);
2165 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2167 else if (for_eh_or_skip >= 0
2168 && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2170 unsigned r = (opc - DW_OP_reg0);
2171 r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2172 gcc_assert (r <= 31);
2173 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2176 dw2_asm_output_data (1, opc,
2177 "%s", dwarf_stack_op_name (opc));
2179 /* Output the operand(s) (if any). */
2180 output_loc_operands (loc, for_eh_or_skip);
2184 /* Output location description stack opcode's operands (if any).
2185 The output is single bytes on a line, suitable for .cfi_escape. */
2187 static void
2188 output_loc_operands_raw (dw_loc_descr_ref loc)
2190 dw_val_ref val1 = &loc->dw_loc_oprnd1;
2191 dw_val_ref val2 = &loc->dw_loc_oprnd2;
2193 switch (loc->dw_loc_opc)
2195 case DW_OP_addr:
2196 case DW_OP_GNU_addr_index:
2197 case DW_OP_GNU_const_index:
2198 case DW_OP_implicit_value:
2199 /* We cannot output addresses in .cfi_escape, only bytes. */
2200 gcc_unreachable ();
2202 case DW_OP_const1u:
2203 case DW_OP_const1s:
2204 case DW_OP_pick:
2205 case DW_OP_deref_size:
2206 case DW_OP_xderef_size:
2207 fputc (',', asm_out_file);
2208 dw2_asm_output_data_raw (1, val1->v.val_int);
2209 break;
2211 case DW_OP_const2u:
2212 case DW_OP_const2s:
2213 fputc (',', asm_out_file);
2214 dw2_asm_output_data_raw (2, val1->v.val_int);
2215 break;
2217 case DW_OP_const4u:
2218 case DW_OP_const4s:
2219 fputc (',', asm_out_file);
2220 dw2_asm_output_data_raw (4, val1->v.val_int);
2221 break;
2223 case DW_OP_const8u:
2224 case DW_OP_const8s:
2225 gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2226 fputc (',', asm_out_file);
2227 dw2_asm_output_data_raw (8, val1->v.val_int);
2228 break;
2230 case DW_OP_skip:
2231 case DW_OP_bra:
2233 int offset;
2235 gcc_assert (val1->val_class == dw_val_class_loc);
2236 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2238 fputc (',', asm_out_file);
2239 dw2_asm_output_data_raw (2, offset);
2241 break;
2243 case DW_OP_regx:
2245 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2246 gcc_assert (size_of_uleb128 (r)
2247 == size_of_uleb128 (val1->v.val_unsigned));
2248 fputc (',', asm_out_file);
2249 dw2_asm_output_data_uleb128_raw (r);
2251 break;
2253 case DW_OP_constu:
2254 case DW_OP_plus_uconst:
2255 case DW_OP_piece:
2256 fputc (',', asm_out_file);
2257 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2258 break;
2260 case DW_OP_bit_piece:
2261 fputc (',', asm_out_file);
2262 dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2263 dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2264 break;
2266 case DW_OP_consts:
2267 case DW_OP_breg0:
2268 case DW_OP_breg1:
2269 case DW_OP_breg2:
2270 case DW_OP_breg3:
2271 case DW_OP_breg4:
2272 case DW_OP_breg5:
2273 case DW_OP_breg6:
2274 case DW_OP_breg7:
2275 case DW_OP_breg8:
2276 case DW_OP_breg9:
2277 case DW_OP_breg10:
2278 case DW_OP_breg11:
2279 case DW_OP_breg12:
2280 case DW_OP_breg13:
2281 case DW_OP_breg14:
2282 case DW_OP_breg15:
2283 case DW_OP_breg16:
2284 case DW_OP_breg17:
2285 case DW_OP_breg18:
2286 case DW_OP_breg19:
2287 case DW_OP_breg20:
2288 case DW_OP_breg21:
2289 case DW_OP_breg22:
2290 case DW_OP_breg23:
2291 case DW_OP_breg24:
2292 case DW_OP_breg25:
2293 case DW_OP_breg26:
2294 case DW_OP_breg27:
2295 case DW_OP_breg28:
2296 case DW_OP_breg29:
2297 case DW_OP_breg30:
2298 case DW_OP_breg31:
2299 case DW_OP_fbreg:
2300 fputc (',', asm_out_file);
2301 dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2302 break;
2304 case DW_OP_bregx:
2306 unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2307 gcc_assert (size_of_uleb128 (r)
2308 == size_of_uleb128 (val1->v.val_unsigned));
2309 fputc (',', asm_out_file);
2310 dw2_asm_output_data_uleb128_raw (r);
2311 fputc (',', asm_out_file);
2312 dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2314 break;
2316 case DW_OP_GNU_implicit_pointer:
2317 case DW_OP_GNU_entry_value:
2318 case DW_OP_GNU_const_type:
2319 case DW_OP_GNU_regval_type:
2320 case DW_OP_GNU_deref_type:
2321 case DW_OP_GNU_convert:
2322 case DW_OP_GNU_reinterpret:
2323 case DW_OP_GNU_parameter_ref:
2324 gcc_unreachable ();
2325 break;
2327 default:
2328 /* Other codes have no operands. */
2329 break;
2333 void
2334 output_loc_sequence_raw (dw_loc_descr_ref loc)
2336 while (1)
2338 enum dwarf_location_atom opc = loc->dw_loc_opc;
2339 /* Output the opcode. */
2340 if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2342 unsigned r = (opc - DW_OP_breg0);
2343 r = DWARF2_FRAME_REG_OUT (r, 1);
2344 gcc_assert (r <= 31);
2345 opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2347 else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2349 unsigned r = (opc - DW_OP_reg0);
2350 r = DWARF2_FRAME_REG_OUT (r, 1);
2351 gcc_assert (r <= 31);
2352 opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2354 /* Output the opcode. */
2355 fprintf (asm_out_file, "%#x", opc);
2356 output_loc_operands_raw (loc);
2358 if (!loc->dw_loc_next)
2359 break;
2360 loc = loc->dw_loc_next;
2362 fputc (',', asm_out_file);
2366 /* This function builds a dwarf location descriptor sequence from a
2367 dw_cfa_location, adding the given OFFSET to the result of the
2368 expression. */
2370 struct dw_loc_descr_node *
2371 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2373 struct dw_loc_descr_node *head, *tmp;
2375 offset += cfa->offset;
2377 if (cfa->indirect)
2379 head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2380 head->dw_loc_oprnd1.val_class = dw_val_class_const;
2381 head->dw_loc_oprnd1.val_entry = NULL;
2382 tmp = new_loc_descr (DW_OP_deref, 0, 0);
2383 add_loc_descr (&head, tmp);
2384 if (offset != 0)
2386 tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2387 add_loc_descr (&head, tmp);
2390 else
2391 head = new_reg_loc_descr (cfa->reg, offset);
2393 return head;
2396 /* This function builds a dwarf location descriptor sequence for
2397 the address at OFFSET from the CFA when stack is aligned to
2398 ALIGNMENT byte. */
2400 struct dw_loc_descr_node *
2401 build_cfa_aligned_loc (dw_cfa_location *cfa,
2402 HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2404 struct dw_loc_descr_node *head;
2405 unsigned int dwarf_fp
2406 = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2408 /* When CFA is defined as FP+OFFSET, emulate stack alignment. */
2409 if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2411 head = new_reg_loc_descr (dwarf_fp, 0);
2412 add_loc_descr (&head, int_loc_descriptor (alignment));
2413 add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2414 loc_descr_plus_const (&head, offset);
2416 else
2417 head = new_reg_loc_descr (dwarf_fp, offset);
2418 return head;
2421 /* And now, the support for symbolic debugging information. */
2423 /* .debug_str support. */
2425 static void dwarf2out_init (const char *);
2426 static void dwarf2out_finish (const char *);
2427 static void dwarf2out_early_finish (void);
2428 static void dwarf2out_assembly_start (void);
2429 static void dwarf2out_define (unsigned int, const char *);
2430 static void dwarf2out_undef (unsigned int, const char *);
2431 static void dwarf2out_start_source_file (unsigned, const char *);
2432 static void dwarf2out_end_source_file (unsigned);
2433 static void dwarf2out_function_decl (tree);
2434 static void dwarf2out_begin_block (unsigned, unsigned);
2435 static void dwarf2out_end_block (unsigned, unsigned);
2436 static bool dwarf2out_ignore_block (const_tree);
2437 static void dwarf2out_early_global_decl (tree);
2438 static void dwarf2out_late_global_decl (tree);
2439 static void dwarf2out_type_decl (tree, int);
2440 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2441 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2442 dw_die_ref);
2443 static void dwarf2out_abstract_function (tree);
2444 static void dwarf2out_var_location (rtx_insn *);
2445 static void dwarf2out_begin_function (tree);
2446 static void dwarf2out_end_function (unsigned int);
2447 static void dwarf2out_register_main_translation_unit (tree unit);
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_early_finish,
2457 dwarf2out_assembly_start,
2458 dwarf2out_define,
2459 dwarf2out_undef,
2460 dwarf2out_start_source_file,
2461 dwarf2out_end_source_file,
2462 dwarf2out_begin_block,
2463 dwarf2out_end_block,
2464 dwarf2out_ignore_block,
2465 dwarf2out_source_line,
2466 dwarf2out_begin_prologue,
2467 #if VMS_DEBUGGING_INFO
2468 dwarf2out_vms_end_prologue,
2469 dwarf2out_vms_begin_epilogue,
2470 #else
2471 debug_nothing_int_charstar,
2472 debug_nothing_int_charstar,
2473 #endif
2474 dwarf2out_end_epilogue,
2475 dwarf2out_begin_function,
2476 dwarf2out_end_function, /* end_function */
2477 dwarf2out_register_main_translation_unit,
2478 dwarf2out_function_decl, /* function_decl */
2479 dwarf2out_early_global_decl,
2480 dwarf2out_late_global_decl,
2481 dwarf2out_type_decl, /* type_decl */
2482 dwarf2out_imported_module_or_decl,
2483 debug_nothing_tree, /* deferred_inline_function */
2484 /* The DWARF 2 backend tries to reduce debugging bloat by not
2485 emitting the abstract description of inline functions until
2486 something tries to reference them. */
2487 dwarf2out_abstract_function, /* outlining_inline_function */
2488 debug_nothing_rtx_code_label, /* label */
2489 debug_nothing_int, /* handle_pch */
2490 dwarf2out_var_location,
2491 dwarf2out_switch_text_section,
2492 dwarf2out_set_name,
2493 1, /* start_end_main_source_file */
2494 TYPE_SYMTAB_IS_DIE /* tree_type_symtab_field */
2497 /* NOTE: In the comments in this file, many references are made to
2498 "Debugging Information Entries". This term is abbreviated as `DIE'
2499 throughout the remainder of this file. */
2501 /* An internal representation of the DWARF output is built, and then
2502 walked to generate the DWARF debugging info. The walk of the internal
2503 representation is done after the entire program has been compiled.
2504 The types below are used to describe the internal representation. */
2506 /* Whether to put type DIEs into their own section .debug_types instead
2507 of making them part of the .debug_info section. Only supported for
2508 Dwarf V4 or higher and the user didn't disable them through
2509 -fno-debug-types-section. It is more efficient to put them in a
2510 separate comdat sections since the linker will then be able to
2511 remove duplicates. But not all tools support .debug_types sections
2512 yet. */
2514 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2516 /* Various DIE's use offsets relative to the beginning of the
2517 .debug_info section to refer to each other. */
2519 typedef long int dw_offset;
2521 /* Define typedefs here to avoid circular dependencies. */
2523 typedef struct dw_attr_struct *dw_attr_ref;
2524 typedef struct dw_line_info_struct *dw_line_info_ref;
2525 typedef struct pubname_struct *pubname_ref;
2526 typedef struct dw_ranges_struct *dw_ranges_ref;
2527 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2528 typedef struct comdat_type_struct *comdat_type_node_ref;
2530 /* The entries in the line_info table more-or-less mirror the opcodes
2531 that are used in the real dwarf line table. Arrays of these entries
2532 are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2533 supported. */
2535 enum dw_line_info_opcode {
2536 /* Emit DW_LNE_set_address; the operand is the label index. */
2537 LI_set_address,
2539 /* Emit a row to the matrix with the given line. This may be done
2540 via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2541 special opcodes. */
2542 LI_set_line,
2544 /* Emit a DW_LNS_set_file. */
2545 LI_set_file,
2547 /* Emit a DW_LNS_set_column. */
2548 LI_set_column,
2550 /* Emit a DW_LNS_negate_stmt; the operand is ignored. */
2551 LI_negate_stmt,
2553 /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored. */
2554 LI_set_prologue_end,
2555 LI_set_epilogue_begin,
2557 /* Emit a DW_LNE_set_discriminator. */
2558 LI_set_discriminator
2561 typedef struct GTY(()) dw_line_info_struct {
2562 enum dw_line_info_opcode opcode;
2563 unsigned int val;
2564 } dw_line_info_entry;
2567 typedef struct GTY(()) dw_line_info_table_struct {
2568 /* The label that marks the end of this section. */
2569 const char *end_label;
2571 /* The values for the last row of the matrix, as collected in the table.
2572 These are used to minimize the changes to the next row. */
2573 unsigned int file_num;
2574 unsigned int line_num;
2575 unsigned int column_num;
2576 int discrim_num;
2577 bool is_stmt;
2578 bool in_use;
2580 vec<dw_line_info_entry, va_gc> *entries;
2581 } dw_line_info_table;
2583 typedef dw_line_info_table *dw_line_info_table_p;
2586 /* Each DIE attribute has a field specifying the attribute kind,
2587 a link to the next attribute in the chain, and an attribute value.
2588 Attributes are typically linked below the DIE they modify. */
2590 typedef struct GTY(()) dw_attr_struct {
2591 enum dwarf_attribute dw_attr;
2592 dw_val_node dw_attr_val;
2594 dw_attr_node;
2597 /* The Debugging Information Entry (DIE) structure. DIEs form a tree.
2598 The children of each node form a circular list linked by
2599 die_sib. die_child points to the node *before* the "first" child node. */
2601 typedef struct GTY((chain_circular ("%h.die_sib"), for_user)) die_struct {
2602 union die_symbol_or_type_node
2604 const char * GTY ((tag ("0"))) die_symbol;
2605 comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2607 GTY ((desc ("%0.comdat_type_p"))) die_id;
2608 vec<dw_attr_node, va_gc> *die_attr;
2609 dw_die_ref die_parent;
2610 dw_die_ref die_child;
2611 dw_die_ref die_sib;
2612 dw_die_ref die_definition; /* ref from a specification to its definition */
2613 dw_offset die_offset;
2614 unsigned long die_abbrev;
2615 int die_mark;
2616 unsigned int decl_id;
2617 enum dwarf_tag die_tag;
2618 /* Die is used and must not be pruned as unused. */
2619 BOOL_BITFIELD die_perennial_p : 1;
2620 BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2621 /* Lots of spare bits. */
2623 die_node;
2625 /* Set to TRUE while dwarf2out_early_global_decl is running. */
2626 static bool early_dwarf;
2627 struct set_early_dwarf {
2628 bool saved;
2629 set_early_dwarf () : saved(early_dwarf) { early_dwarf = true; }
2630 ~set_early_dwarf () { early_dwarf = saved; }
2633 /* Evaluate 'expr' while 'c' is set to each child of DIE in order. */
2634 #define FOR_EACH_CHILD(die, c, expr) do { \
2635 c = die->die_child; \
2636 if (c) do { \
2637 c = c->die_sib; \
2638 expr; \
2639 } while (c != die->die_child); \
2640 } while (0)
2642 /* The pubname structure */
2644 typedef struct GTY(()) pubname_struct {
2645 dw_die_ref die;
2646 const char *name;
2648 pubname_entry;
2651 struct GTY(()) dw_ranges_struct {
2652 /* If this is positive, it's a block number, otherwise it's a
2653 bitwise-negated index into dw_ranges_by_label. */
2654 int num;
2657 /* A structure to hold a macinfo entry. */
2659 typedef struct GTY(()) macinfo_struct {
2660 unsigned char code;
2661 unsigned HOST_WIDE_INT lineno;
2662 const char *info;
2664 macinfo_entry;
2667 struct GTY(()) dw_ranges_by_label_struct {
2668 const char *begin;
2669 const char *end;
2672 /* The comdat type node structure. */
2673 typedef struct GTY(()) comdat_type_struct
2675 dw_die_ref root_die;
2676 dw_die_ref type_die;
2677 dw_die_ref skeleton_die;
2678 char signature[DWARF_TYPE_SIGNATURE_SIZE];
2679 struct comdat_type_struct *next;
2681 comdat_type_node;
2683 /* A list of DIEs for which we can't determine ancestry (parent_die
2684 field) just yet. Later in dwarf2out_finish we will fill in the
2685 missing bits. */
2686 typedef struct GTY(()) limbo_die_struct {
2687 dw_die_ref die;
2688 /* The tree for which this DIE was created. We use this to
2689 determine ancestry later. */
2690 tree created_for;
2691 struct limbo_die_struct *next;
2693 limbo_die_node;
2695 typedef struct skeleton_chain_struct
2697 dw_die_ref old_die;
2698 dw_die_ref new_die;
2699 struct skeleton_chain_struct *parent;
2701 skeleton_chain_node;
2703 /* Define a macro which returns nonzero for a TYPE_DECL which was
2704 implicitly generated for a type.
2706 Note that, unlike the C front-end (which generates a NULL named
2707 TYPE_DECL node for each complete tagged type, each array type,
2708 and each function type node created) the C++ front-end generates
2709 a _named_ TYPE_DECL node for each tagged type node created.
2710 These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2711 generate a DW_TAG_typedef DIE for them. Likewise with the Ada
2712 front-end, but for each type, tagged or not. */
2714 #define TYPE_DECL_IS_STUB(decl) \
2715 (DECL_NAME (decl) == NULL_TREE \
2716 || (DECL_ARTIFICIAL (decl) \
2717 && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl))) \
2718 /* This is necessary for stub decls that \
2719 appear in nested inline functions. */ \
2720 || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2721 && (decl_ultimate_origin (decl) \
2722 == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2724 /* Information concerning the compilation unit's programming
2725 language, and compiler version. */
2727 /* Fixed size portion of the DWARF compilation unit header. */
2728 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2729 (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2731 /* Fixed size portion of the DWARF comdat type unit header. */
2732 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2733 (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2734 + DWARF_OFFSET_SIZE)
2736 /* Fixed size portion of public names info. */
2737 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2739 /* Fixed size portion of the address range info. */
2740 #define DWARF_ARANGES_HEADER_SIZE \
2741 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2742 DWARF2_ADDR_SIZE * 2) \
2743 - DWARF_INITIAL_LENGTH_SIZE)
2745 /* Size of padding portion in the address range info. It must be
2746 aligned to twice the pointer size. */
2747 #define DWARF_ARANGES_PAD_SIZE \
2748 (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2749 DWARF2_ADDR_SIZE * 2) \
2750 - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2752 /* Use assembler line directives if available. */
2753 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2754 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2755 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2756 #else
2757 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2758 #endif
2759 #endif
2761 /* Minimum line offset in a special line info. opcode.
2762 This value was chosen to give a reasonable range of values. */
2763 #define DWARF_LINE_BASE -10
2765 /* First special line opcode - leave room for the standard opcodes. */
2766 #define DWARF_LINE_OPCODE_BASE ((int)DW_LNS_set_isa + 1)
2768 /* Range of line offsets in a special line info. opcode. */
2769 #define DWARF_LINE_RANGE (254-DWARF_LINE_OPCODE_BASE+1)
2771 /* Flag that indicates the initial value of the is_stmt_start flag.
2772 In the present implementation, we do not mark any lines as
2773 the beginning of a source statement, because that information
2774 is not made available by the GCC front-end. */
2775 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2777 /* Maximum number of operations per instruction bundle. */
2778 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2779 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2780 #endif
2782 /* This location is used by calc_die_sizes() to keep track
2783 the offset of each DIE within the .debug_info section. */
2784 static unsigned long next_die_offset;
2786 /* Record the root of the DIE's built for the current compilation unit. */
2787 static GTY(()) dw_die_ref single_comp_unit_die;
2789 /* A list of type DIEs that have been separated into comdat sections. */
2790 static GTY(()) comdat_type_node *comdat_type_list;
2792 /* A list of DIEs with a NULL parent waiting to be relocated. */
2793 static GTY(()) limbo_die_node *limbo_die_list;
2795 /* A list of DIEs for which we may have to generate
2796 DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set. */
2797 static GTY(()) limbo_die_node *deferred_asm_name;
2799 struct dwarf_file_hasher : ggc_ptr_hash<dwarf_file_data>
2801 typedef const char *compare_type;
2803 static hashval_t hash (dwarf_file_data *);
2804 static bool equal (dwarf_file_data *, const char *);
2807 /* Filenames referenced by this compilation unit. */
2808 static GTY(()) hash_table<dwarf_file_hasher> *file_table;
2810 struct decl_die_hasher : ggc_ptr_hash<die_node>
2812 typedef tree compare_type;
2814 static hashval_t hash (die_node *);
2815 static bool equal (die_node *, tree);
2817 /* A hash table of references to DIE's that describe declarations.
2818 The key is a DECL_UID() which is a unique number identifying each decl. */
2819 static GTY (()) hash_table<decl_die_hasher> *decl_die_table;
2821 struct block_die_hasher : ggc_ptr_hash<die_struct>
2823 static hashval_t hash (die_struct *);
2824 static bool equal (die_struct *, die_struct *);
2827 /* A hash table of references to DIE's that describe COMMON blocks.
2828 The key is DECL_UID() ^ die_parent. */
2829 static GTY (()) hash_table<block_die_hasher> *common_block_die_table;
2831 typedef struct GTY(()) die_arg_entry_struct {
2832 dw_die_ref die;
2833 tree arg;
2834 } die_arg_entry;
2837 /* Node of the variable location list. */
2838 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2839 /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2840 EXPR_LIST chain. For small bitsizes, bitsize is encoded
2841 in mode of the EXPR_LIST node and first EXPR_LIST operand
2842 is either NOTE_INSN_VAR_LOCATION for a piece with a known
2843 location or NULL for padding. For larger bitsizes,
2844 mode is 0 and first operand is a CONCAT with bitsize
2845 as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2846 NULL as second operand. */
2847 rtx GTY (()) loc;
2848 const char * GTY (()) label;
2849 struct var_loc_node * GTY (()) next;
2852 /* Variable location list. */
2853 struct GTY ((for_user)) var_loc_list_def {
2854 struct var_loc_node * GTY (()) first;
2856 /* Pointer to the last but one or last element of the
2857 chained list. If the list is empty, both first and
2858 last are NULL, if the list contains just one node
2859 or the last node certainly is not redundant, it points
2860 to the last node, otherwise points to the last but one.
2861 Do not mark it for GC because it is marked through the chain. */
2862 struct var_loc_node * GTY ((skip ("%h"))) last;
2864 /* Pointer to the last element before section switch,
2865 if NULL, either sections weren't switched or first
2866 is after section switch. */
2867 struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2869 /* DECL_UID of the variable decl. */
2870 unsigned int decl_id;
2872 typedef struct var_loc_list_def var_loc_list;
2874 /* Call argument location list. */
2875 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2876 rtx GTY (()) call_arg_loc_note;
2877 const char * GTY (()) label;
2878 tree GTY (()) block;
2879 bool tail_call_p;
2880 rtx GTY (()) symbol_ref;
2881 struct call_arg_loc_node * GTY (()) next;
2885 struct decl_loc_hasher : ggc_ptr_hash<var_loc_list>
2887 typedef const_tree compare_type;
2889 static hashval_t hash (var_loc_list *);
2890 static bool equal (var_loc_list *, const_tree);
2893 /* Table of decl location linked lists. */
2894 static GTY (()) hash_table<decl_loc_hasher> *decl_loc_table;
2896 /* Head and tail of call_arg_loc chain. */
2897 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2898 static struct call_arg_loc_node *call_arg_loc_last;
2900 /* Number of call sites in the current function. */
2901 static int call_site_count = -1;
2902 /* Number of tail call sites in the current function. */
2903 static int tail_call_site_count = -1;
2905 /* A cached location list. */
2906 struct GTY ((for_user)) cached_dw_loc_list_def {
2907 /* The DECL_UID of the decl that this entry describes. */
2908 unsigned int decl_id;
2910 /* The cached location list. */
2911 dw_loc_list_ref loc_list;
2913 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2915 struct dw_loc_list_hasher : ggc_ptr_hash<cached_dw_loc_list>
2918 typedef const_tree compare_type;
2920 static hashval_t hash (cached_dw_loc_list *);
2921 static bool equal (cached_dw_loc_list *, const_tree);
2924 /* Table of cached location lists. */
2925 static GTY (()) hash_table<dw_loc_list_hasher> *cached_dw_loc_list_table;
2927 /* A pointer to the base of a list of references to DIE's that
2928 are uniquely identified by their tag, presence/absence of
2929 children DIE's, and list of attribute/value pairs. */
2930 static GTY((length ("abbrev_die_table_allocated")))
2931 dw_die_ref *abbrev_die_table;
2933 /* Number of elements currently allocated for abbrev_die_table. */
2934 static GTY(()) unsigned abbrev_die_table_allocated;
2936 /* Number of elements in abbrev_die_table currently in use. */
2937 static GTY(()) unsigned abbrev_die_table_in_use;
2939 /* Size (in elements) of increments by which we may expand the
2940 abbrev_die_table. */
2941 #define ABBREV_DIE_TABLE_INCREMENT 256
2943 /* A global counter for generating labels for line number data. */
2944 static unsigned int line_info_label_num;
2946 /* The current table to which we should emit line number information
2947 for the current function. This will be set up at the beginning of
2948 assembly for the function. */
2949 static dw_line_info_table *cur_line_info_table;
2951 /* The two default tables of line number info. */
2952 static GTY(()) dw_line_info_table *text_section_line_info;
2953 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2955 /* The set of all non-default tables of line number info. */
2956 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2958 /* A flag to tell pubnames/types export if there is an info section to
2959 refer to. */
2960 static bool info_section_emitted;
2962 /* A pointer to the base of a table that contains a list of publicly
2963 accessible names. */
2964 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2966 /* A pointer to the base of a table that contains a list of publicly
2967 accessible types. */
2968 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2970 /* A pointer to the base of a table that contains a list of macro
2971 defines/undefines (and file start/end markers). */
2972 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2974 /* True if .debug_macinfo or .debug_macros section is going to be
2975 emitted. */
2976 #define have_macinfo \
2977 (debug_info_level >= DINFO_LEVEL_VERBOSE \
2978 && !macinfo_table->is_empty ())
2980 /* Array of dies for which we should generate .debug_ranges info. */
2981 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2983 /* Number of elements currently allocated for ranges_table. */
2984 static GTY(()) unsigned ranges_table_allocated;
2986 /* Number of elements in ranges_table currently in use. */
2987 static GTY(()) unsigned ranges_table_in_use;
2989 /* Array of pairs of labels referenced in ranges_table. */
2990 static GTY ((length ("ranges_by_label_allocated")))
2991 dw_ranges_by_label_ref ranges_by_label;
2993 /* Number of elements currently allocated for ranges_by_label. */
2994 static GTY(()) unsigned ranges_by_label_allocated;
2996 /* Number of elements in ranges_by_label currently in use. */
2997 static GTY(()) unsigned ranges_by_label_in_use;
2999 /* Size (in elements) of increments by which we may expand the
3000 ranges_table. */
3001 #define RANGES_TABLE_INCREMENT 64
3003 /* Whether we have location lists that need outputting */
3004 static GTY(()) bool have_location_lists;
3006 /* Unique label counter. */
3007 static GTY(()) unsigned int loclabel_num;
3009 /* Unique label counter for point-of-call tables. */
3010 static GTY(()) unsigned int poc_label_num;
3012 /* The last file entry emitted by maybe_emit_file(). */
3013 static GTY(()) struct dwarf_file_data * last_emitted_file;
3015 /* Number of internal labels generated by gen_internal_sym(). */
3016 static GTY(()) int label_num;
3018 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
3020 /* Instances of generic types for which we need to generate debug
3021 info that describe their generic parameters and arguments. That
3022 generation needs to happen once all types are properly laid out so
3023 we do it at the end of compilation. */
3024 static GTY(()) vec<tree, va_gc> *generic_type_instances;
3026 /* Offset from the "steady-state frame pointer" to the frame base,
3027 within the current function. */
3028 static HOST_WIDE_INT frame_pointer_fb_offset;
3029 static bool frame_pointer_fb_offset_valid;
3031 static vec<dw_die_ref> base_types;
3033 /* Flags to represent a set of attribute classes for attributes that represent
3034 a scalar value (bounds, pointers, ...). */
3035 enum dw_scalar_form
3037 dw_scalar_form_constant = 0x01,
3038 dw_scalar_form_exprloc = 0x02,
3039 dw_scalar_form_reference = 0x04
3042 /* Forward declarations for functions defined in this file. */
3044 static int is_pseudo_reg (const_rtx);
3045 static tree type_main_variant (tree);
3046 static int is_tagged_type (const_tree);
3047 static const char *dwarf_tag_name (unsigned);
3048 static const char *dwarf_attr_name (unsigned);
3049 static const char *dwarf_form_name (unsigned);
3050 static tree decl_ultimate_origin (const_tree);
3051 static tree decl_class_context (tree);
3052 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3053 static inline enum dw_val_class AT_class (dw_attr_ref);
3054 static inline unsigned int AT_index (dw_attr_ref);
3055 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3056 static inline unsigned AT_flag (dw_attr_ref);
3057 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3058 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3059 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3060 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3061 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3062 HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3063 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3064 unsigned int, unsigned char *);
3065 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3066 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3067 static inline const char *AT_string (dw_attr_ref);
3068 static enum dwarf_form AT_string_form (dw_attr_ref);
3069 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3070 static void add_AT_specification (dw_die_ref, dw_die_ref);
3071 static inline dw_die_ref AT_ref (dw_attr_ref);
3072 static inline int AT_ref_external (dw_attr_ref);
3073 static inline void set_AT_ref_external (dw_attr_ref, int);
3074 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3075 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3076 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3077 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3078 dw_loc_list_ref);
3079 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3080 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
3081 static void remove_addr_table_entry (addr_table_entry *);
3082 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
3083 static inline rtx AT_addr (dw_attr_ref);
3084 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3085 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3086 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3087 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3088 unsigned HOST_WIDE_INT);
3089 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3090 unsigned long, bool);
3091 static inline const char *AT_lbl (dw_attr_ref);
3092 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3093 static const char *get_AT_low_pc (dw_die_ref);
3094 static const char *get_AT_hi_pc (dw_die_ref);
3095 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3096 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3097 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3098 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3099 static bool is_cxx (void);
3100 static bool is_fortran (void);
3101 static bool is_ada (void);
3102 static bool remove_AT (dw_die_ref, enum dwarf_attribute);
3103 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3104 static void add_child_die (dw_die_ref, dw_die_ref);
3105 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3106 static dw_die_ref lookup_type_die (tree);
3107 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3108 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3109 static void equate_type_number_to_die (tree, dw_die_ref);
3110 static dw_die_ref lookup_decl_die (tree);
3111 static var_loc_list *lookup_decl_loc (const_tree);
3112 static void equate_decl_number_to_die (tree, dw_die_ref);
3113 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3114 static void print_spaces (FILE *);
3115 static void print_die (dw_die_ref, FILE *);
3116 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3117 static dw_die_ref pop_compile_unit (dw_die_ref);
3118 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3119 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3120 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3121 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3122 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3123 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3124 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3125 struct md5_ctx *, int *);
3126 struct checksum_attributes;
3127 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3128 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3129 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3130 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3131 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3132 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3133 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3134 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3135 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3136 static void compute_section_prefix (dw_die_ref);
3137 static int is_type_die (dw_die_ref);
3138 static int is_comdat_die (dw_die_ref);
3139 static int is_symbol_die (dw_die_ref);
3140 static inline bool is_template_instantiation (dw_die_ref);
3141 static void assign_symbol_names (dw_die_ref);
3142 static void break_out_includes (dw_die_ref);
3143 static int is_declaration_die (dw_die_ref);
3144 static int should_move_die_to_comdat (dw_die_ref);
3145 static dw_die_ref clone_as_declaration (dw_die_ref);
3146 static dw_die_ref clone_die (dw_die_ref);
3147 static dw_die_ref clone_tree (dw_die_ref);
3148 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3149 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3150 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3151 static dw_die_ref generate_skeleton (dw_die_ref);
3152 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3153 dw_die_ref,
3154 dw_die_ref);
3155 static void break_out_comdat_types (dw_die_ref);
3156 static void copy_decls_for_unworthy_types (dw_die_ref);
3158 static void add_sibling_attributes (dw_die_ref);
3159 static void output_location_lists (dw_die_ref);
3160 static int constant_size (unsigned HOST_WIDE_INT);
3161 static unsigned long size_of_die (dw_die_ref);
3162 static void calc_die_sizes (dw_die_ref);
3163 static void calc_base_type_die_sizes (void);
3164 static void mark_dies (dw_die_ref);
3165 static void unmark_dies (dw_die_ref);
3166 static void unmark_all_dies (dw_die_ref);
3167 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3168 static unsigned long size_of_aranges (void);
3169 static enum dwarf_form value_format (dw_attr_ref);
3170 static void output_value_format (dw_attr_ref);
3171 static void output_abbrev_section (void);
3172 static void output_die_abbrevs (unsigned long, dw_die_ref);
3173 static void output_die_symbol (dw_die_ref);
3174 static void output_die (dw_die_ref);
3175 static void output_compilation_unit_header (void);
3176 static void output_comp_unit (dw_die_ref, int);
3177 static void output_comdat_type_unit (comdat_type_node *);
3178 static const char *dwarf2_name (tree, int);
3179 static void add_pubname (tree, dw_die_ref);
3180 static void add_enumerator_pubname (const char *, dw_die_ref);
3181 static void add_pubname_string (const char *, dw_die_ref);
3182 static void add_pubtype (tree, dw_die_ref);
3183 static void output_pubnames (vec<pubname_entry, va_gc> *);
3184 static void output_aranges (unsigned long);
3185 static unsigned int add_ranges_num (int);
3186 static unsigned int add_ranges (const_tree);
3187 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3188 bool *, bool);
3189 static void output_ranges (void);
3190 static dw_line_info_table *new_line_info_table (void);
3191 static void output_line_info (bool);
3192 static void output_file_names (void);
3193 static dw_die_ref base_type_die (tree);
3194 static int is_base_type (tree);
3195 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3196 static int decl_quals (const_tree);
3197 static dw_die_ref modified_type_die (tree, int, dw_die_ref);
3198 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3199 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3200 static int type_is_enum (const_tree);
3201 static unsigned int dbx_reg_number (const_rtx);
3202 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3203 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3204 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3205 enum var_init_status);
3206 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3207 enum var_init_status);
3208 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3209 enum var_init_status);
3210 static int is_based_loc (const_rtx);
3211 static bool resolve_one_addr (rtx *);
3212 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3213 enum var_init_status);
3214 static dw_loc_descr_ref loc_descriptor (rtx, machine_mode mode,
3215 enum var_init_status);
3216 struct loc_descr_context;
3217 static dw_loc_list_ref loc_list_from_tree (tree, int,
3218 const struct loc_descr_context *);
3219 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int,
3220 const struct loc_descr_context *);
3221 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3222 static tree field_type (const_tree);
3223 static unsigned int simple_type_align_in_bits (const_tree);
3224 static unsigned int simple_decl_align_in_bits (const_tree);
3225 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3226 static HOST_WIDE_INT field_byte_offset (const_tree);
3227 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3228 dw_loc_list_ref);
3229 static void add_data_member_location_attribute (dw_die_ref, tree);
3230 static bool add_const_value_attribute (dw_die_ref, rtx);
3231 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3232 static void insert_wide_int (const wide_int &, unsigned char *, int);
3233 static void insert_float (const_rtx, unsigned char *);
3234 static rtx rtl_for_decl_location (tree);
3235 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3236 enum dwarf_attribute);
3237 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3238 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3239 static void add_name_attribute (dw_die_ref, const char *);
3240 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3241 static void add_comp_dir_attribute (dw_die_ref);
3242 static void add_scalar_info (dw_die_ref, enum dwarf_attribute, tree, int,
3243 const struct loc_descr_context *);
3244 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree,
3245 const struct loc_descr_context *);
3246 static void add_subscript_info (dw_die_ref, tree, bool);
3247 static void add_byte_size_attribute (dw_die_ref, tree);
3248 static void add_bit_offset_attribute (dw_die_ref, tree);
3249 static void add_bit_size_attribute (dw_die_ref, tree);
3250 static void add_prototyped_attribute (dw_die_ref, tree);
3251 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3252 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3253 static void add_src_coords_attributes (dw_die_ref, tree);
3254 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3255 static void push_decl_scope (tree);
3256 static void pop_decl_scope (void);
3257 static dw_die_ref scope_die_for (tree, dw_die_ref);
3258 static inline int local_scope_p (dw_die_ref);
3259 static inline int class_scope_p (dw_die_ref);
3260 static inline int class_or_namespace_scope_p (dw_die_ref);
3261 static void add_type_attribute (dw_die_ref, tree, int, dw_die_ref);
3262 static void add_calling_convention_attribute (dw_die_ref, tree);
3263 static const char *type_tag (const_tree);
3264 static tree member_declared_type (const_tree);
3265 #if 0
3266 static const char *decl_start_label (tree);
3267 #endif
3268 static void gen_array_type_die (tree, dw_die_ref);
3269 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3270 #if 0
3271 static void gen_entry_point_die (tree, dw_die_ref);
3272 #endif
3273 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3274 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3275 static dw_die_ref gen_formal_parameter_pack_die (tree, tree, dw_die_ref, tree*);
3276 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3277 static void gen_formal_types_die (tree, dw_die_ref);
3278 static void gen_subprogram_die (tree, dw_die_ref);
3279 static void gen_variable_die (tree, tree, dw_die_ref);
3280 static void gen_const_die (tree, dw_die_ref);
3281 static void gen_label_die (tree, dw_die_ref);
3282 static void gen_lexical_block_die (tree, dw_die_ref);
3283 static void gen_inlined_subroutine_die (tree, dw_die_ref);
3284 static void gen_field_die (tree, dw_die_ref);
3285 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3286 static dw_die_ref gen_compile_unit_die (const char *);
3287 static void gen_inheritance_die (tree, tree, dw_die_ref);
3288 static void gen_member_die (tree, dw_die_ref);
3289 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3290 enum debug_info_usage);
3291 static void gen_subroutine_type_die (tree, dw_die_ref);
3292 static void gen_typedef_die (tree, dw_die_ref);
3293 static void gen_type_die (tree, dw_die_ref);
3294 static void gen_block_die (tree, dw_die_ref);
3295 static void decls_for_scope (tree, dw_die_ref);
3296 static inline int is_redundant_typedef (const_tree);
3297 static bool is_naming_typedef_decl (const_tree);
3298 static inline dw_die_ref get_context_die (tree);
3299 static void gen_namespace_die (tree, dw_die_ref);
3300 static dw_die_ref gen_namelist_decl (tree, dw_die_ref, tree);
3301 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3302 static dw_die_ref force_decl_die (tree);
3303 static dw_die_ref force_type_die (tree);
3304 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3305 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3306 static struct dwarf_file_data * lookup_filename (const char *);
3307 static void retry_incomplete_types (void);
3308 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3309 static void gen_generic_params_dies (tree);
3310 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3311 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3312 static void splice_child_die (dw_die_ref, dw_die_ref);
3313 static int file_info_cmp (const void *, const void *);
3314 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3315 const char *, const char *);
3316 static void output_loc_list (dw_loc_list_ref);
3317 static char *gen_internal_sym (const char *);
3318 static bool want_pubnames (void);
3320 static void prune_unmark_dies (dw_die_ref);
3321 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3322 static void prune_unused_types_mark (dw_die_ref, int);
3323 static void prune_unused_types_walk (dw_die_ref);
3324 static void prune_unused_types_walk_attribs (dw_die_ref);
3325 static void prune_unused_types_prune (dw_die_ref);
3326 static void prune_unused_types (void);
3327 static int maybe_emit_file (struct dwarf_file_data *fd);
3328 static inline const char *AT_vms_delta1 (dw_attr_ref);
3329 static inline const char *AT_vms_delta2 (dw_attr_ref);
3330 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3331 const char *, const char *);
3332 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3333 static void gen_remaining_tmpl_value_param_die_attribute (void);
3334 static bool generic_type_p (tree);
3335 static void schedule_generic_params_dies_gen (tree t);
3336 static void gen_scheduled_generic_parms_dies (void);
3338 static const char *comp_dir_string (void);
3340 static void hash_loc_operands (dw_loc_descr_ref, inchash::hash &);
3342 /* enum for tracking thread-local variables whose address is really an offset
3343 relative to the TLS pointer, which will need link-time relocation, but will
3344 not need relocation by the DWARF consumer. */
3346 enum dtprel_bool
3348 dtprel_false = 0,
3349 dtprel_true = 1
3352 /* Return the operator to use for an address of a variable. For dtprel_true, we
3353 use DW_OP_const*. For regular variables, which need both link-time
3354 relocation and consumer-level relocation (e.g., to account for shared objects
3355 loaded at a random address), we use DW_OP_addr*. */
3357 static inline enum dwarf_location_atom
3358 dw_addr_op (enum dtprel_bool dtprel)
3360 if (dtprel == dtprel_true)
3361 return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3362 : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3363 else
3364 return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3367 /* Return a pointer to a newly allocated address location description. If
3368 dwarf_split_debug_info is true, then record the address with the appropriate
3369 relocation. */
3370 static inline dw_loc_descr_ref
3371 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3373 dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3375 ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3376 ref->dw_loc_oprnd1.v.val_addr = addr;
3377 ref->dtprel = dtprel;
3378 if (dwarf_split_debug_info)
3379 ref->dw_loc_oprnd1.val_entry
3380 = add_addr_table_entry (addr,
3381 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3382 else
3383 ref->dw_loc_oprnd1.val_entry = NULL;
3385 return ref;
3388 /* Section names used to hold DWARF debugging information. */
3390 #ifndef DEBUG_INFO_SECTION
3391 #define DEBUG_INFO_SECTION ".debug_info"
3392 #endif
3393 #ifndef DEBUG_DWO_INFO_SECTION
3394 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3395 #endif
3396 #ifndef DEBUG_ABBREV_SECTION
3397 #define DEBUG_ABBREV_SECTION ".debug_abbrev"
3398 #endif
3399 #ifndef DEBUG_DWO_ABBREV_SECTION
3400 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3401 #endif
3402 #ifndef DEBUG_ARANGES_SECTION
3403 #define DEBUG_ARANGES_SECTION ".debug_aranges"
3404 #endif
3405 #ifndef DEBUG_ADDR_SECTION
3406 #define DEBUG_ADDR_SECTION ".debug_addr"
3407 #endif
3408 #ifndef DEBUG_NORM_MACINFO_SECTION
3409 #define DEBUG_NORM_MACINFO_SECTION ".debug_macinfo"
3410 #endif
3411 #ifndef DEBUG_DWO_MACINFO_SECTION
3412 #define DEBUG_DWO_MACINFO_SECTION ".debug_macinfo.dwo"
3413 #endif
3414 #ifndef DEBUG_MACINFO_SECTION
3415 #define DEBUG_MACINFO_SECTION \
3416 (!dwarf_split_debug_info \
3417 ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3418 #endif
3419 #ifndef DEBUG_NORM_MACRO_SECTION
3420 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3421 #endif
3422 #ifndef DEBUG_DWO_MACRO_SECTION
3423 #define DEBUG_DWO_MACRO_SECTION ".debug_macro.dwo"
3424 #endif
3425 #ifndef DEBUG_MACRO_SECTION
3426 #define DEBUG_MACRO_SECTION \
3427 (!dwarf_split_debug_info \
3428 ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3429 #endif
3430 #ifndef DEBUG_LINE_SECTION
3431 #define DEBUG_LINE_SECTION ".debug_line"
3432 #endif
3433 #ifndef DEBUG_DWO_LINE_SECTION
3434 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3435 #endif
3436 #ifndef DEBUG_LOC_SECTION
3437 #define DEBUG_LOC_SECTION ".debug_loc"
3438 #endif
3439 #ifndef DEBUG_DWO_LOC_SECTION
3440 #define DEBUG_DWO_LOC_SECTION ".debug_loc.dwo"
3441 #endif
3442 #ifndef DEBUG_PUBNAMES_SECTION
3443 #define DEBUG_PUBNAMES_SECTION \
3444 ((debug_generate_pub_sections == 2) \
3445 ? ".debug_gnu_pubnames" : ".debug_pubnames")
3446 #endif
3447 #ifndef DEBUG_PUBTYPES_SECTION
3448 #define DEBUG_PUBTYPES_SECTION \
3449 ((debug_generate_pub_sections == 2) \
3450 ? ".debug_gnu_pubtypes" : ".debug_pubtypes")
3451 #endif
3452 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3453 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3454 #ifndef DEBUG_STR_OFFSETS_SECTION
3455 #define DEBUG_STR_OFFSETS_SECTION \
3456 (!dwarf_split_debug_info \
3457 ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3458 #endif
3459 #ifndef DEBUG_STR_DWO_SECTION
3460 #define DEBUG_STR_DWO_SECTION ".debug_str.dwo"
3461 #endif
3462 #ifndef DEBUG_STR_SECTION
3463 #define DEBUG_STR_SECTION ".debug_str"
3464 #endif
3465 #ifndef DEBUG_RANGES_SECTION
3466 #define DEBUG_RANGES_SECTION ".debug_ranges"
3467 #endif
3469 /* Standard ELF section names for compiled code and data. */
3470 #ifndef TEXT_SECTION_NAME
3471 #define TEXT_SECTION_NAME ".text"
3472 #endif
3474 /* Section flags for .debug_macinfo/.debug_macro section. */
3475 #define DEBUG_MACRO_SECTION_FLAGS \
3476 (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3478 /* Section flags for .debug_str section. */
3479 #define DEBUG_STR_SECTION_FLAGS \
3480 (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3481 ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1 \
3482 : SECTION_DEBUG)
3484 /* Section flags for .debug_str.dwo section. */
3485 #define DEBUG_STR_DWO_SECTION_FLAGS (SECTION_DEBUG | SECTION_EXCLUDE)
3487 /* Labels we insert at beginning sections we can reference instead of
3488 the section names themselves. */
3490 #ifndef TEXT_SECTION_LABEL
3491 #define TEXT_SECTION_LABEL "Ltext"
3492 #endif
3493 #ifndef COLD_TEXT_SECTION_LABEL
3494 #define COLD_TEXT_SECTION_LABEL "Ltext_cold"
3495 #endif
3496 #ifndef DEBUG_LINE_SECTION_LABEL
3497 #define DEBUG_LINE_SECTION_LABEL "Ldebug_line"
3498 #endif
3499 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3500 #define DEBUG_SKELETON_LINE_SECTION_LABEL "Lskeleton_debug_line"
3501 #endif
3502 #ifndef DEBUG_INFO_SECTION_LABEL
3503 #define DEBUG_INFO_SECTION_LABEL "Ldebug_info"
3504 #endif
3505 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3506 #define DEBUG_SKELETON_INFO_SECTION_LABEL "Lskeleton_debug_info"
3507 #endif
3508 #ifndef DEBUG_ABBREV_SECTION_LABEL
3509 #define DEBUG_ABBREV_SECTION_LABEL "Ldebug_abbrev"
3510 #endif
3511 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3512 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3513 #endif
3514 #ifndef DEBUG_ADDR_SECTION_LABEL
3515 #define DEBUG_ADDR_SECTION_LABEL "Ldebug_addr"
3516 #endif
3517 #ifndef DEBUG_LOC_SECTION_LABEL
3518 #define DEBUG_LOC_SECTION_LABEL "Ldebug_loc"
3519 #endif
3520 #ifndef DEBUG_RANGES_SECTION_LABEL
3521 #define DEBUG_RANGES_SECTION_LABEL "Ldebug_ranges"
3522 #endif
3523 #ifndef DEBUG_MACINFO_SECTION_LABEL
3524 #define DEBUG_MACINFO_SECTION_LABEL "Ldebug_macinfo"
3525 #endif
3526 #ifndef DEBUG_MACRO_SECTION_LABEL
3527 #define DEBUG_MACRO_SECTION_LABEL "Ldebug_macro"
3528 #endif
3529 #define SKELETON_COMP_DIE_ABBREV 1
3530 #define SKELETON_TYPE_DIE_ABBREV 2
3532 /* Definitions of defaults for formats and names of various special
3533 (artificial) labels which may be generated within this file (when the -g
3534 options is used and DWARF2_DEBUGGING_INFO is in effect.
3535 If necessary, these may be overridden from within the tm.h file, but
3536 typically, overriding these defaults is unnecessary. */
3538 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3539 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3540 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3541 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3542 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3543 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3544 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3545 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3546 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3547 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3548 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3549 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3550 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3551 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3553 #ifndef TEXT_END_LABEL
3554 #define TEXT_END_LABEL "Letext"
3555 #endif
3556 #ifndef COLD_END_LABEL
3557 #define COLD_END_LABEL "Letext_cold"
3558 #endif
3559 #ifndef BLOCK_BEGIN_LABEL
3560 #define BLOCK_BEGIN_LABEL "LBB"
3561 #endif
3562 #ifndef BLOCK_END_LABEL
3563 #define BLOCK_END_LABEL "LBE"
3564 #endif
3565 #ifndef LINE_CODE_LABEL
3566 #define LINE_CODE_LABEL "LM"
3567 #endif
3570 /* Return the root of the DIE's built for the current compilation unit. */
3571 static dw_die_ref
3572 comp_unit_die (void)
3574 if (!single_comp_unit_die)
3575 single_comp_unit_die = gen_compile_unit_die (NULL);
3576 return single_comp_unit_die;
3579 /* We allow a language front-end to designate a function that is to be
3580 called to "demangle" any name before it is put into a DIE. */
3582 static const char *(*demangle_name_func) (const char *);
3584 void
3585 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3587 demangle_name_func = func;
3590 /* Test if rtl node points to a pseudo register. */
3592 static inline int
3593 is_pseudo_reg (const_rtx rtl)
3595 return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3596 || (GET_CODE (rtl) == SUBREG
3597 && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3600 /* Return a reference to a type, with its const and volatile qualifiers
3601 removed. */
3603 static inline tree
3604 type_main_variant (tree type)
3606 type = TYPE_MAIN_VARIANT (type);
3608 /* ??? There really should be only one main variant among any group of
3609 variants of a given type (and all of the MAIN_VARIANT values for all
3610 members of the group should point to that one type) but sometimes the C
3611 front-end messes this up for array types, so we work around that bug
3612 here. */
3613 if (TREE_CODE (type) == ARRAY_TYPE)
3614 while (type != TYPE_MAIN_VARIANT (type))
3615 type = TYPE_MAIN_VARIANT (type);
3617 return type;
3620 /* Return nonzero if the given type node represents a tagged type. */
3622 static inline int
3623 is_tagged_type (const_tree type)
3625 enum tree_code code = TREE_CODE (type);
3627 return (code == RECORD_TYPE || code == UNION_TYPE
3628 || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3631 /* Set label to debug_info_section_label + die_offset of a DIE reference. */
3633 static void
3634 get_ref_die_offset_label (char *label, dw_die_ref ref)
3636 sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3639 /* Return die_offset of a DIE reference to a base type. */
3641 static unsigned long int
3642 get_base_type_offset (dw_die_ref ref)
3644 if (ref->die_offset)
3645 return ref->die_offset;
3646 if (comp_unit_die ()->die_abbrev)
3648 calc_base_type_die_sizes ();
3649 gcc_assert (ref->die_offset);
3651 return ref->die_offset;
3654 /* Return die_offset of a DIE reference other than base type. */
3656 static unsigned long int
3657 get_ref_die_offset (dw_die_ref ref)
3659 gcc_assert (ref->die_offset);
3660 return ref->die_offset;
3663 /* Convert a DIE tag into its string name. */
3665 static const char *
3666 dwarf_tag_name (unsigned int tag)
3668 const char *name = get_DW_TAG_name (tag);
3670 if (name != NULL)
3671 return name;
3673 return "DW_TAG_<unknown>";
3676 /* Convert a DWARF attribute code into its string name. */
3678 static const char *
3679 dwarf_attr_name (unsigned int attr)
3681 const char *name;
3683 switch (attr)
3685 #if VMS_DEBUGGING_INFO
3686 case DW_AT_HP_prologue:
3687 return "DW_AT_HP_prologue";
3688 #else
3689 case DW_AT_MIPS_loop_unroll_factor:
3690 return "DW_AT_MIPS_loop_unroll_factor";
3691 #endif
3693 #if VMS_DEBUGGING_INFO
3694 case DW_AT_HP_epilogue:
3695 return "DW_AT_HP_epilogue";
3696 #else
3697 case DW_AT_MIPS_stride:
3698 return "DW_AT_MIPS_stride";
3699 #endif
3702 name = get_DW_AT_name (attr);
3704 if (name != NULL)
3705 return name;
3707 return "DW_AT_<unknown>";
3710 /* Convert a DWARF value form code into its string name. */
3712 static const char *
3713 dwarf_form_name (unsigned int form)
3715 const char *name = get_DW_FORM_name (form);
3717 if (name != NULL)
3718 return name;
3720 return "DW_FORM_<unknown>";
3723 /* Determine the "ultimate origin" of a decl. The decl may be an inlined
3724 instance of an inlined instance of a decl which is local to an inline
3725 function, so we have to trace all of the way back through the origin chain
3726 to find out what sort of node actually served as the original seed for the
3727 given block. */
3729 static tree
3730 decl_ultimate_origin (const_tree decl)
3732 if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3733 return NULL_TREE;
3735 /* DECL_ABSTRACT_ORIGIN can point to itself; ignore that if
3736 we're trying to output the abstract instance of this function. */
3737 if (DECL_ABSTRACT_P (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3738 return NULL_TREE;
3740 /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3741 most distant ancestor, this should never happen. */
3742 gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3744 return DECL_ABSTRACT_ORIGIN (decl);
3747 /* Get the class to which DECL belongs, if any. In g++, the DECL_CONTEXT
3748 of a virtual function may refer to a base class, so we check the 'this'
3749 parameter. */
3751 static tree
3752 decl_class_context (tree decl)
3754 tree context = NULL_TREE;
3756 if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3757 context = DECL_CONTEXT (decl);
3758 else
3759 context = TYPE_MAIN_VARIANT
3760 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3762 if (context && !TYPE_P (context))
3763 context = NULL_TREE;
3765 return context;
3768 /* Add an attribute/value pair to a DIE. */
3770 static inline void
3771 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3773 /* Maybe this should be an assert? */
3774 if (die == NULL)
3775 return;
3777 vec_safe_reserve (die->die_attr, 1);
3778 vec_safe_push (die->die_attr, *attr);
3781 static inline enum dw_val_class
3782 AT_class (dw_attr_ref a)
3784 return a->dw_attr_val.val_class;
3787 /* Return the index for any attribute that will be referenced with a
3788 DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index. String indices
3789 are stored in dw_attr_val.v.val_str for reference counting
3790 pruning. */
3792 static inline unsigned int
3793 AT_index (dw_attr_ref a)
3795 if (AT_class (a) == dw_val_class_str)
3796 return a->dw_attr_val.v.val_str->index;
3797 else if (a->dw_attr_val.val_entry != NULL)
3798 return a->dw_attr_val.val_entry->index;
3799 return NOT_INDEXED;
3802 /* Add a flag value attribute to a DIE. */
3804 static inline void
3805 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3807 dw_attr_node attr;
3809 attr.dw_attr = attr_kind;
3810 attr.dw_attr_val.val_class = dw_val_class_flag;
3811 attr.dw_attr_val.val_entry = NULL;
3812 attr.dw_attr_val.v.val_flag = flag;
3813 add_dwarf_attr (die, &attr);
3816 static inline unsigned
3817 AT_flag (dw_attr_ref a)
3819 gcc_assert (a && AT_class (a) == dw_val_class_flag);
3820 return a->dw_attr_val.v.val_flag;
3823 /* Add a signed integer attribute value to a DIE. */
3825 static inline void
3826 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3828 dw_attr_node attr;
3830 attr.dw_attr = attr_kind;
3831 attr.dw_attr_val.val_class = dw_val_class_const;
3832 attr.dw_attr_val.val_entry = NULL;
3833 attr.dw_attr_val.v.val_int = int_val;
3834 add_dwarf_attr (die, &attr);
3837 static inline HOST_WIDE_INT
3838 AT_int (dw_attr_ref a)
3840 gcc_assert (a && AT_class (a) == dw_val_class_const);
3841 return a->dw_attr_val.v.val_int;
3844 /* Add an unsigned integer attribute value to a DIE. */
3846 static inline void
3847 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3848 unsigned HOST_WIDE_INT unsigned_val)
3850 dw_attr_node attr;
3852 attr.dw_attr = attr_kind;
3853 attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3854 attr.dw_attr_val.val_entry = NULL;
3855 attr.dw_attr_val.v.val_unsigned = unsigned_val;
3856 add_dwarf_attr (die, &attr);
3859 static inline unsigned HOST_WIDE_INT
3860 AT_unsigned (dw_attr_ref a)
3862 gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3863 return a->dw_attr_val.v.val_unsigned;
3866 /* Add an unsigned wide integer attribute value to a DIE. */
3868 static inline void
3869 add_AT_wide (dw_die_ref die, enum dwarf_attribute attr_kind,
3870 const wide_int& w)
3872 dw_attr_node attr;
3874 attr.dw_attr = attr_kind;
3875 attr.dw_attr_val.val_class = dw_val_class_wide_int;
3876 attr.dw_attr_val.val_entry = NULL;
3877 attr.dw_attr_val.v.val_wide = ggc_alloc<wide_int> ();
3878 *attr.dw_attr_val.v.val_wide = w;
3879 add_dwarf_attr (die, &attr);
3882 /* Add an unsigned double integer attribute value to a DIE. */
3884 static inline void
3885 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3886 HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3888 dw_attr_node attr;
3890 attr.dw_attr = attr_kind;
3891 attr.dw_attr_val.val_class = dw_val_class_const_double;
3892 attr.dw_attr_val.val_entry = NULL;
3893 attr.dw_attr_val.v.val_double.high = high;
3894 attr.dw_attr_val.v.val_double.low = low;
3895 add_dwarf_attr (die, &attr);
3898 /* Add a floating point attribute value to a DIE and return it. */
3900 static inline void
3901 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3902 unsigned int length, unsigned int elt_size, unsigned char *array)
3904 dw_attr_node attr;
3906 attr.dw_attr = attr_kind;
3907 attr.dw_attr_val.val_class = dw_val_class_vec;
3908 attr.dw_attr_val.val_entry = NULL;
3909 attr.dw_attr_val.v.val_vec.length = length;
3910 attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3911 attr.dw_attr_val.v.val_vec.array = array;
3912 add_dwarf_attr (die, &attr);
3915 /* Add an 8-byte data attribute value to a DIE. */
3917 static inline void
3918 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3919 unsigned char data8[8])
3921 dw_attr_node attr;
3923 attr.dw_attr = attr_kind;
3924 attr.dw_attr_val.val_class = dw_val_class_data8;
3925 attr.dw_attr_val.val_entry = NULL;
3926 memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3927 add_dwarf_attr (die, &attr);
3930 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE. When using
3931 dwarf_split_debug_info, address attributes in dies destined for the
3932 final executable have force_direct set to avoid using indexed
3933 references. */
3935 static inline void
3936 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3937 bool force_direct)
3939 dw_attr_node attr;
3940 char * lbl_id;
3942 lbl_id = xstrdup (lbl_low);
3943 attr.dw_attr = DW_AT_low_pc;
3944 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3945 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3946 if (dwarf_split_debug_info && !force_direct)
3947 attr.dw_attr_val.val_entry
3948 = add_addr_table_entry (lbl_id, ate_kind_label);
3949 else
3950 attr.dw_attr_val.val_entry = NULL;
3951 add_dwarf_attr (die, &attr);
3953 attr.dw_attr = DW_AT_high_pc;
3954 if (dwarf_version < 4)
3955 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3956 else
3957 attr.dw_attr_val.val_class = dw_val_class_high_pc;
3958 lbl_id = xstrdup (lbl_high);
3959 attr.dw_attr_val.v.val_lbl_id = lbl_id;
3960 if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3961 && dwarf_split_debug_info && !force_direct)
3962 attr.dw_attr_val.val_entry
3963 = add_addr_table_entry (lbl_id, ate_kind_label);
3964 else
3965 attr.dw_attr_val.val_entry = NULL;
3966 add_dwarf_attr (die, &attr);
3969 /* Hash and equality functions for debug_str_hash. */
3971 hashval_t
3972 indirect_string_hasher::hash (indirect_string_node *x)
3974 return htab_hash_string (x->str);
3977 bool
3978 indirect_string_hasher::equal (indirect_string_node *x1, const char *x2)
3980 return strcmp (x1->str, x2) == 0;
3983 /* Add STR to the given string hash table. */
3985 static struct indirect_string_node *
3986 find_AT_string_in_table (const char *str,
3987 hash_table<indirect_string_hasher> *table)
3989 struct indirect_string_node *node;
3991 indirect_string_node **slot
3992 = table->find_slot_with_hash (str, htab_hash_string (str), INSERT);
3993 if (*slot == NULL)
3995 node = ggc_cleared_alloc<indirect_string_node> ();
3996 node->str = ggc_strdup (str);
3997 *slot = node;
3999 else
4000 node = *slot;
4002 node->refcount++;
4003 return node;
4006 /* Add STR to the indirect string hash table. */
4008 static struct indirect_string_node *
4009 find_AT_string (const char *str)
4011 if (! debug_str_hash)
4012 debug_str_hash = hash_table<indirect_string_hasher>::create_ggc (10);
4014 return find_AT_string_in_table (str, debug_str_hash);
4017 /* Add a string attribute value to a DIE. */
4019 static inline void
4020 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4022 dw_attr_node attr;
4023 struct indirect_string_node *node;
4025 node = find_AT_string (str);
4027 attr.dw_attr = attr_kind;
4028 attr.dw_attr_val.val_class = dw_val_class_str;
4029 attr.dw_attr_val.val_entry = NULL;
4030 attr.dw_attr_val.v.val_str = node;
4031 add_dwarf_attr (die, &attr);
4034 static inline const char *
4035 AT_string (dw_attr_ref a)
4037 gcc_assert (a && AT_class (a) == dw_val_class_str);
4038 return a->dw_attr_val.v.val_str->str;
4041 /* Call this function directly to bypass AT_string_form's logic to put
4042 the string inline in the die. */
4044 static void
4045 set_indirect_string (struct indirect_string_node *node)
4047 char label[32];
4048 /* Already indirect is a no op. */
4049 if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
4051 gcc_assert (node->label);
4052 return;
4054 ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4055 ++dw2_string_counter;
4056 node->label = xstrdup (label);
4058 if (!dwarf_split_debug_info)
4060 node->form = DW_FORM_strp;
4061 node->index = NOT_INDEXED;
4063 else
4065 node->form = DW_FORM_GNU_str_index;
4066 node->index = NO_INDEX_ASSIGNED;
4070 /* Find out whether a string should be output inline in DIE
4071 or out-of-line in .debug_str section. */
4073 static enum dwarf_form
4074 find_string_form (struct indirect_string_node *node)
4076 unsigned int len;
4078 if (node->form)
4079 return node->form;
4081 len = strlen (node->str) + 1;
4083 /* If the string is shorter or equal to the size of the reference, it is
4084 always better to put it inline. */
4085 if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4086 return node->form = DW_FORM_string;
4088 /* If we cannot expect the linker to merge strings in .debug_str
4089 section, only put it into .debug_str if it is worth even in this
4090 single module. */
4091 if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4092 || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4093 && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4094 return node->form = DW_FORM_string;
4096 set_indirect_string (node);
4098 return node->form;
4101 /* Find out whether the string referenced from the attribute should be
4102 output inline in DIE or out-of-line in .debug_str section. */
4104 static enum dwarf_form
4105 AT_string_form (dw_attr_ref a)
4107 gcc_assert (a && AT_class (a) == dw_val_class_str);
4108 return find_string_form (a->dw_attr_val.v.val_str);
4111 /* Add a DIE reference attribute value to a DIE. */
4113 static inline void
4114 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4116 dw_attr_node attr;
4118 #ifdef ENABLE_CHECKING
4119 gcc_assert (targ_die != NULL);
4120 #else
4121 /* With LTO we can end up trying to reference something we didn't create
4122 a DIE for. Avoid crashing later on a NULL referenced DIE. */
4123 if (targ_die == NULL)
4124 return;
4125 #endif
4127 attr.dw_attr = attr_kind;
4128 attr.dw_attr_val.val_class = dw_val_class_die_ref;
4129 attr.dw_attr_val.val_entry = NULL;
4130 attr.dw_attr_val.v.val_die_ref.die = targ_die;
4131 attr.dw_attr_val.v.val_die_ref.external = 0;
4132 add_dwarf_attr (die, &attr);
4135 /* Change DIE reference REF to point to NEW_DIE instead. */
4137 static inline void
4138 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
4140 gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
4141 ref->dw_attr_val.v.val_die_ref.die = new_die;
4142 ref->dw_attr_val.v.val_die_ref.external = 0;
4145 /* Add an AT_specification attribute to a DIE, and also make the back
4146 pointer from the specification to the definition. */
4148 static inline void
4149 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4151 add_AT_die_ref (die, DW_AT_specification, targ_die);
4152 gcc_assert (!targ_die->die_definition);
4153 targ_die->die_definition = die;
4156 static inline dw_die_ref
4157 AT_ref (dw_attr_ref a)
4159 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4160 return a->dw_attr_val.v.val_die_ref.die;
4163 static inline int
4164 AT_ref_external (dw_attr_ref a)
4166 if (a && AT_class (a) == dw_val_class_die_ref)
4167 return a->dw_attr_val.v.val_die_ref.external;
4169 return 0;
4172 static inline void
4173 set_AT_ref_external (dw_attr_ref a, int i)
4175 gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4176 a->dw_attr_val.v.val_die_ref.external = i;
4179 /* Add an FDE reference attribute value to a DIE. */
4181 static inline void
4182 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4184 dw_attr_node attr;
4186 attr.dw_attr = attr_kind;
4187 attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4188 attr.dw_attr_val.val_entry = NULL;
4189 attr.dw_attr_val.v.val_fde_index = targ_fde;
4190 add_dwarf_attr (die, &attr);
4193 /* Add a location description attribute value to a DIE. */
4195 static inline void
4196 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4198 dw_attr_node attr;
4200 attr.dw_attr = attr_kind;
4201 attr.dw_attr_val.val_class = dw_val_class_loc;
4202 attr.dw_attr_val.val_entry = NULL;
4203 attr.dw_attr_val.v.val_loc = loc;
4204 add_dwarf_attr (die, &attr);
4207 static inline dw_loc_descr_ref
4208 AT_loc (dw_attr_ref a)
4210 gcc_assert (a && AT_class (a) == dw_val_class_loc);
4211 return a->dw_attr_val.v.val_loc;
4214 static inline void
4215 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4217 dw_attr_node attr;
4219 attr.dw_attr = attr_kind;
4220 attr.dw_attr_val.val_class = dw_val_class_loc_list;
4221 attr.dw_attr_val.val_entry = NULL;
4222 attr.dw_attr_val.v.val_loc_list = loc_list;
4223 add_dwarf_attr (die, &attr);
4224 have_location_lists = true;
4227 static inline dw_loc_list_ref
4228 AT_loc_list (dw_attr_ref a)
4230 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4231 return a->dw_attr_val.v.val_loc_list;
4234 static inline dw_loc_list_ref *
4235 AT_loc_list_ptr (dw_attr_ref a)
4237 gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4238 return &a->dw_attr_val.v.val_loc_list;
4241 struct addr_hasher : ggc_ptr_hash<addr_table_entry>
4243 static hashval_t hash (addr_table_entry *);
4244 static bool equal (addr_table_entry *, addr_table_entry *);
4247 /* Table of entries into the .debug_addr section. */
4249 static GTY (()) hash_table<addr_hasher> *addr_index_table;
4251 /* Hash an address_table_entry. */
4253 hashval_t
4254 addr_hasher::hash (addr_table_entry *a)
4256 inchash::hash hstate;
4257 switch (a->kind)
4259 case ate_kind_rtx:
4260 hstate.add_int (0);
4261 break;
4262 case ate_kind_rtx_dtprel:
4263 hstate.add_int (1);
4264 break;
4265 case ate_kind_label:
4266 return htab_hash_string (a->addr.label);
4267 default:
4268 gcc_unreachable ();
4270 inchash::add_rtx (a->addr.rtl, hstate);
4271 return hstate.end ();
4274 /* Determine equality for two address_table_entries. */
4276 bool
4277 addr_hasher::equal (addr_table_entry *a1, addr_table_entry *a2)
4279 if (a1->kind != a2->kind)
4280 return 0;
4281 switch (a1->kind)
4283 case ate_kind_rtx:
4284 case ate_kind_rtx_dtprel:
4285 return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4286 case ate_kind_label:
4287 return strcmp (a1->addr.label, a2->addr.label) == 0;
4288 default:
4289 gcc_unreachable ();
4293 /* Initialize an addr_table_entry. */
4295 void
4296 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4298 e->kind = kind;
4299 switch (kind)
4301 case ate_kind_rtx:
4302 case ate_kind_rtx_dtprel:
4303 e->addr.rtl = (rtx) addr;
4304 break;
4305 case ate_kind_label:
4306 e->addr.label = (char *) addr;
4307 break;
4309 e->refcount = 0;
4310 e->index = NO_INDEX_ASSIGNED;
4313 /* Add attr to the address table entry to the table. Defer setting an
4314 index until output time. */
4316 static addr_table_entry *
4317 add_addr_table_entry (void *addr, enum ate_kind kind)
4319 addr_table_entry *node;
4320 addr_table_entry finder;
4322 gcc_assert (dwarf_split_debug_info);
4323 if (! addr_index_table)
4324 addr_index_table = hash_table<addr_hasher>::create_ggc (10);
4325 init_addr_table_entry (&finder, kind, addr);
4326 addr_table_entry **slot = addr_index_table->find_slot (&finder, INSERT);
4328 if (*slot == HTAB_EMPTY_ENTRY)
4330 node = ggc_cleared_alloc<addr_table_entry> ();
4331 init_addr_table_entry (node, kind, addr);
4332 *slot = node;
4334 else
4335 node = *slot;
4337 node->refcount++;
4338 return node;
4341 /* Remove an entry from the addr table by decrementing its refcount.
4342 Strictly, decrementing the refcount would be enough, but the
4343 assertion that the entry is actually in the table has found
4344 bugs. */
4346 static void
4347 remove_addr_table_entry (addr_table_entry *entry)
4349 gcc_assert (dwarf_split_debug_info && addr_index_table);
4350 /* After an index is assigned, the table is frozen. */
4351 gcc_assert (entry->refcount > 0 && entry->index == NO_INDEX_ASSIGNED);
4352 entry->refcount--;
4355 /* Given a location list, remove all addresses it refers to from the
4356 address_table. */
4358 static void
4359 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4361 for (; descr; descr = descr->dw_loc_next)
4362 if (descr->dw_loc_oprnd1.val_entry != NULL)
4364 gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4365 remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4369 /* A helper function for dwarf2out_finish called through
4370 htab_traverse. Assign an addr_table_entry its index. All entries
4371 must be collected into the table when this function is called,
4372 because the indexing code relies on htab_traverse to traverse nodes
4373 in the same order for each run. */
4376 index_addr_table_entry (addr_table_entry **h, unsigned int *index)
4378 addr_table_entry *node = *h;
4380 /* Don't index unreferenced nodes. */
4381 if (node->refcount == 0)
4382 return 1;
4384 gcc_assert (node->index == NO_INDEX_ASSIGNED);
4385 node->index = *index;
4386 *index += 1;
4388 return 1;
4391 /* Add an address constant attribute value to a DIE. When using
4392 dwarf_split_debug_info, address attributes in dies destined for the
4393 final executable should be direct references--setting the parameter
4394 force_direct ensures this behavior. */
4396 static inline void
4397 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4398 bool force_direct)
4400 dw_attr_node attr;
4402 attr.dw_attr = attr_kind;
4403 attr.dw_attr_val.val_class = dw_val_class_addr;
4404 attr.dw_attr_val.v.val_addr = addr;
4405 if (dwarf_split_debug_info && !force_direct)
4406 attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4407 else
4408 attr.dw_attr_val.val_entry = NULL;
4409 add_dwarf_attr (die, &attr);
4412 /* Get the RTX from to an address DIE attribute. */
4414 static inline rtx
4415 AT_addr (dw_attr_ref a)
4417 gcc_assert (a && AT_class (a) == dw_val_class_addr);
4418 return a->dw_attr_val.v.val_addr;
4421 /* Add a file attribute value to a DIE. */
4423 static inline void
4424 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4425 struct dwarf_file_data *fd)
4427 dw_attr_node attr;
4429 attr.dw_attr = attr_kind;
4430 attr.dw_attr_val.val_class = dw_val_class_file;
4431 attr.dw_attr_val.val_entry = NULL;
4432 attr.dw_attr_val.v.val_file = fd;
4433 add_dwarf_attr (die, &attr);
4436 /* Get the dwarf_file_data from a file DIE attribute. */
4438 static inline struct dwarf_file_data *
4439 AT_file (dw_attr_ref a)
4441 gcc_assert (a && AT_class (a) == dw_val_class_file);
4442 return a->dw_attr_val.v.val_file;
4445 /* Add a vms delta attribute value to a DIE. */
4447 static inline void
4448 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4449 const char *lbl1, const char *lbl2)
4451 dw_attr_node attr;
4453 attr.dw_attr = attr_kind;
4454 attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4455 attr.dw_attr_val.val_entry = NULL;
4456 attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4457 attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4458 add_dwarf_attr (die, &attr);
4461 /* Add a label identifier attribute value to a DIE. */
4463 static inline void
4464 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4465 const char *lbl_id)
4467 dw_attr_node attr;
4469 attr.dw_attr = attr_kind;
4470 attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4471 attr.dw_attr_val.val_entry = NULL;
4472 attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4473 if (dwarf_split_debug_info)
4474 attr.dw_attr_val.val_entry
4475 = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4476 ate_kind_label);
4477 add_dwarf_attr (die, &attr);
4480 /* Add a section offset attribute value to a DIE, an offset into the
4481 debug_line section. */
4483 static inline void
4484 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4485 const char *label)
4487 dw_attr_node attr;
4489 attr.dw_attr = attr_kind;
4490 attr.dw_attr_val.val_class = dw_val_class_lineptr;
4491 attr.dw_attr_val.val_entry = NULL;
4492 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4493 add_dwarf_attr (die, &attr);
4496 /* Add a section offset attribute value to a DIE, an offset into the
4497 debug_macinfo section. */
4499 static inline void
4500 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4501 const char *label)
4503 dw_attr_node attr;
4505 attr.dw_attr = attr_kind;
4506 attr.dw_attr_val.val_class = dw_val_class_macptr;
4507 attr.dw_attr_val.val_entry = NULL;
4508 attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4509 add_dwarf_attr (die, &attr);
4512 /* Add an offset attribute value to a DIE. */
4514 static inline void
4515 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4516 unsigned HOST_WIDE_INT offset)
4518 dw_attr_node attr;
4520 attr.dw_attr = attr_kind;
4521 attr.dw_attr_val.val_class = dw_val_class_offset;
4522 attr.dw_attr_val.val_entry = NULL;
4523 attr.dw_attr_val.v.val_offset = offset;
4524 add_dwarf_attr (die, &attr);
4527 /* Add a range_list attribute value to a DIE. When using
4528 dwarf_split_debug_info, address attributes in dies destined for the
4529 final executable should be direct references--setting the parameter
4530 force_direct ensures this behavior. */
4532 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4533 #define RELOCATED_OFFSET (NULL)
4535 static void
4536 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4537 long unsigned int offset, bool force_direct)
4539 dw_attr_node attr;
4541 attr.dw_attr = attr_kind;
4542 attr.dw_attr_val.val_class = dw_val_class_range_list;
4543 /* For the range_list attribute, use val_entry to store whether the
4544 offset should follow split-debug-info or normal semantics. This
4545 value is read in output_range_list_offset. */
4546 if (dwarf_split_debug_info && !force_direct)
4547 attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4548 else
4549 attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4550 attr.dw_attr_val.v.val_offset = offset;
4551 add_dwarf_attr (die, &attr);
4554 /* Return the start label of a delta attribute. */
4556 static inline const char *
4557 AT_vms_delta1 (dw_attr_ref a)
4559 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4560 return a->dw_attr_val.v.val_vms_delta.lbl1;
4563 /* Return the end label of a delta attribute. */
4565 static inline const char *
4566 AT_vms_delta2 (dw_attr_ref a)
4568 gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4569 return a->dw_attr_val.v.val_vms_delta.lbl2;
4572 static inline const char *
4573 AT_lbl (dw_attr_ref a)
4575 gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4576 || AT_class (a) == dw_val_class_lineptr
4577 || AT_class (a) == dw_val_class_macptr
4578 || AT_class (a) == dw_val_class_high_pc));
4579 return a->dw_attr_val.v.val_lbl_id;
4582 /* Get the attribute of type attr_kind. */
4584 static dw_attr_ref
4585 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4587 dw_attr_ref a;
4588 unsigned ix;
4589 dw_die_ref spec = NULL;
4591 if (! die)
4592 return NULL;
4594 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4595 if (a->dw_attr == attr_kind)
4596 return a;
4597 else if (a->dw_attr == DW_AT_specification
4598 || a->dw_attr == DW_AT_abstract_origin)
4599 spec = AT_ref (a);
4601 if (spec)
4602 return get_AT (spec, attr_kind);
4604 return NULL;
4607 /* Returns the parent of the declaration of DIE. */
4609 static dw_die_ref
4610 get_die_parent (dw_die_ref die)
4612 dw_die_ref t;
4614 if (!die)
4615 return NULL;
4617 if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4618 || (t = get_AT_ref (die, DW_AT_specification)))
4619 die = t;
4621 return die->die_parent;
4624 /* Return the "low pc" attribute value, typically associated with a subprogram
4625 DIE. Return null if the "low pc" attribute is either not present, or if it
4626 cannot be represented as an assembler label identifier. */
4628 static inline const char *
4629 get_AT_low_pc (dw_die_ref die)
4631 dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4633 return a ? AT_lbl (a) : NULL;
4636 /* Return the "high pc" attribute value, typically associated with a subprogram
4637 DIE. Return null if the "high pc" attribute is either not present, or if it
4638 cannot be represented as an assembler label identifier. */
4640 static inline const char *
4641 get_AT_hi_pc (dw_die_ref die)
4643 dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4645 return a ? AT_lbl (a) : NULL;
4648 /* Return the value of the string attribute designated by ATTR_KIND, or
4649 NULL if it is not present. */
4651 static inline const char *
4652 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4654 dw_attr_ref a = get_AT (die, attr_kind);
4656 return a ? AT_string (a) : NULL;
4659 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4660 if it is not present. */
4662 static inline int
4663 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4665 dw_attr_ref a = get_AT (die, attr_kind);
4667 return a ? AT_flag (a) : 0;
4670 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4671 if it is not present. */
4673 static inline unsigned
4674 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4676 dw_attr_ref a = get_AT (die, attr_kind);
4678 return a ? AT_unsigned (a) : 0;
4681 static inline dw_die_ref
4682 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4684 dw_attr_ref a = get_AT (die, attr_kind);
4686 return a ? AT_ref (a) : NULL;
4689 static inline struct dwarf_file_data *
4690 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4692 dw_attr_ref a = get_AT (die, attr_kind);
4694 return a ? AT_file (a) : NULL;
4697 /* Return TRUE if the language is C++. */
4699 static inline bool
4700 is_cxx (void)
4702 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4704 return (lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus
4705 || lang == DW_LANG_C_plus_plus_11 || lang == DW_LANG_C_plus_plus_14);
4708 /* Return TRUE if the language is Java. */
4710 static inline bool
4711 is_java (void)
4713 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4715 return lang == DW_LANG_Java;
4718 /* Return TRUE if the language is Fortran. */
4720 static inline bool
4721 is_fortran (void)
4723 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4725 return (lang == DW_LANG_Fortran77
4726 || lang == DW_LANG_Fortran90
4727 || lang == DW_LANG_Fortran95
4728 || lang == DW_LANG_Fortran03
4729 || lang == DW_LANG_Fortran08);
4732 /* Return TRUE if the language is Ada. */
4734 static inline bool
4735 is_ada (void)
4737 unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4739 return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4742 /* Remove the specified attribute if present. Return TRUE if removal
4743 was successful. */
4745 static bool
4746 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4748 dw_attr_ref a;
4749 unsigned ix;
4751 if (! die)
4752 return false;
4754 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4755 if (a->dw_attr == attr_kind)
4757 if (AT_class (a) == dw_val_class_str)
4758 if (a->dw_attr_val.v.val_str->refcount)
4759 a->dw_attr_val.v.val_str->refcount--;
4761 /* vec::ordered_remove should help reduce the number of abbrevs
4762 that are needed. */
4763 die->die_attr->ordered_remove (ix);
4764 return true;
4766 return false;
4769 /* Remove CHILD from its parent. PREV must have the property that
4770 PREV->DIE_SIB == CHILD. Does not alter CHILD. */
4772 static void
4773 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4775 gcc_assert (child->die_parent == prev->die_parent);
4776 gcc_assert (prev->die_sib == child);
4777 if (prev == child)
4779 gcc_assert (child->die_parent->die_child == child);
4780 prev = NULL;
4782 else
4783 prev->die_sib = child->die_sib;
4784 if (child->die_parent->die_child == child)
4785 child->die_parent->die_child = prev;
4788 /* Replace OLD_CHILD with NEW_CHILD. PREV must have the property that
4789 PREV->DIE_SIB == OLD_CHILD. Does not alter OLD_CHILD. */
4791 static void
4792 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4794 dw_die_ref parent = old_child->die_parent;
4796 gcc_assert (parent == prev->die_parent);
4797 gcc_assert (prev->die_sib == old_child);
4799 new_child->die_parent = parent;
4800 if (prev == old_child)
4802 gcc_assert (parent->die_child == old_child);
4803 new_child->die_sib = new_child;
4805 else
4807 prev->die_sib = new_child;
4808 new_child->die_sib = old_child->die_sib;
4810 if (old_child->die_parent->die_child == old_child)
4811 old_child->die_parent->die_child = new_child;
4814 /* Move all children from OLD_PARENT to NEW_PARENT. */
4816 static void
4817 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4819 dw_die_ref c;
4820 new_parent->die_child = old_parent->die_child;
4821 old_parent->die_child = NULL;
4822 FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4825 /* Remove child DIE whose die_tag is TAG. Do nothing if no child
4826 matches TAG. */
4828 static void
4829 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4831 dw_die_ref c;
4833 c = die->die_child;
4834 if (c) do {
4835 dw_die_ref prev = c;
4836 c = c->die_sib;
4837 while (c->die_tag == tag)
4839 remove_child_with_prev (c, prev);
4840 c->die_parent = NULL;
4841 /* Might have removed every child. */
4842 if (c == c->die_sib)
4843 return;
4844 c = c->die_sib;
4846 } while (c != die->die_child);
4849 /* Add a CHILD_DIE as the last child of DIE. */
4851 static void
4852 add_child_die (dw_die_ref die, dw_die_ref child_die)
4854 /* FIXME this should probably be an assert. */
4855 if (! die || ! child_die)
4856 return;
4857 gcc_assert (die != child_die);
4859 child_die->die_parent = die;
4860 if (die->die_child)
4862 child_die->die_sib = die->die_child->die_sib;
4863 die->die_child->die_sib = child_die;
4865 else
4866 child_die->die_sib = child_die;
4867 die->die_child = child_die;
4870 /* Unassociate CHILD from its parent, and make its parent be
4871 NEW_PARENT. */
4873 static void
4874 reparent_child (dw_die_ref child, dw_die_ref new_parent)
4876 for (dw_die_ref p = child->die_parent->die_child; ; p = p->die_sib)
4877 if (p->die_sib == child)
4879 remove_child_with_prev (child, p);
4880 break;
4882 add_child_die (new_parent, child);
4885 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4886 is the specification, to the end of PARENT's list of children.
4887 This is done by removing and re-adding it. */
4889 static void
4890 splice_child_die (dw_die_ref parent, dw_die_ref child)
4892 /* We want the declaration DIE from inside the class, not the
4893 specification DIE at toplevel. */
4894 if (child->die_parent != parent)
4896 dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4898 if (tmp)
4899 child = tmp;
4902 gcc_assert (child->die_parent == parent
4903 || (child->die_parent
4904 == get_AT_ref (parent, DW_AT_specification)));
4906 reparent_child (child, parent);
4909 /* Create and return a new die with a parent of PARENT_DIE. If
4910 PARENT_DIE is NULL, the new DIE is placed in limbo and an
4911 associated tree T must be supplied to determine parenthood
4912 later. */
4914 static inline dw_die_ref
4915 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4917 dw_die_ref die = ggc_cleared_alloc<die_node> ();
4919 die->die_tag = tag_value;
4921 if (parent_die != NULL)
4922 add_child_die (parent_die, die);
4923 else
4925 limbo_die_node *limbo_node;
4927 /* No DIEs created after early dwarf should end up in limbo,
4928 because the limbo list should not persist past LTO
4929 streaming. */
4930 if (tag_value != DW_TAG_compile_unit
4931 /* These are allowed because they're generated while
4932 breaking out COMDAT units late. */
4933 && tag_value != DW_TAG_type_unit
4934 && !early_dwarf
4935 /* Allow nested functions to live in limbo because they will
4936 only temporarily live there, as decls_for_scope will fix
4937 them up. */
4938 && (TREE_CODE (t) != FUNCTION_DECL
4939 || !decl_function_context (t))
4940 /* Same as nested functions above but for types. Types that
4941 are local to a function will be fixed in
4942 decls_for_scope. */
4943 && (!RECORD_OR_UNION_TYPE_P (t)
4944 || !TYPE_CONTEXT (t)
4945 || TREE_CODE (TYPE_CONTEXT (t)) != FUNCTION_DECL)
4946 /* FIXME debug-early: Allow late limbo DIE creation for LTO,
4947 especially in the ltrans stage, but once we implement LTO
4948 dwarf streaming, we should remove this exception. */
4949 && !in_lto_p)
4951 fprintf (stderr, "symbol ended up in limbo too late:");
4952 debug_generic_stmt (t);
4953 gcc_unreachable ();
4956 limbo_node = ggc_cleared_alloc<limbo_die_node> ();
4957 limbo_node->die = die;
4958 limbo_node->created_for = t;
4959 limbo_node->next = limbo_die_list;
4960 limbo_die_list = limbo_node;
4963 return die;
4966 /* Return the DIE associated with the given type specifier. */
4968 static inline dw_die_ref
4969 lookup_type_die (tree type)
4971 return TYPE_SYMTAB_DIE (type);
4974 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4975 anonymous type named by the typedef TYPE_DIE, return the DIE of the
4976 anonymous type instead the one of the naming typedef. */
4978 static inline dw_die_ref
4979 strip_naming_typedef (tree type, dw_die_ref type_die)
4981 if (type
4982 && TREE_CODE (type) == RECORD_TYPE
4983 && type_die
4984 && type_die->die_tag == DW_TAG_typedef
4985 && is_naming_typedef_decl (TYPE_NAME (type)))
4986 type_die = get_AT_ref (type_die, DW_AT_type);
4987 return type_die;
4990 /* Like lookup_type_die, but if type is an anonymous type named by a
4991 typedef[1], return the DIE of the anonymous type instead the one of
4992 the naming typedef. This is because in gen_typedef_die, we did
4993 equate the anonymous struct named by the typedef with the DIE of
4994 the naming typedef. So by default, lookup_type_die on an anonymous
4995 struct yields the DIE of the naming typedef.
4997 [1]: Read the comment of is_naming_typedef_decl to learn about what
4998 a naming typedef is. */
5000 static inline dw_die_ref
5001 lookup_type_die_strip_naming_typedef (tree type)
5003 dw_die_ref die = lookup_type_die (type);
5004 return strip_naming_typedef (type, die);
5007 /* Equate a DIE to a given type specifier. */
5009 static inline void
5010 equate_type_number_to_die (tree type, dw_die_ref type_die)
5012 TYPE_SYMTAB_DIE (type) = type_die;
5015 /* Returns a hash value for X (which really is a die_struct). */
5017 inline hashval_t
5018 decl_die_hasher::hash (die_node *x)
5020 return (hashval_t) x->decl_id;
5023 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
5025 inline bool
5026 decl_die_hasher::equal (die_node *x, tree y)
5028 return (x->decl_id == DECL_UID (y));
5031 /* Return the DIE associated with a given declaration. */
5033 static inline dw_die_ref
5034 lookup_decl_die (tree decl)
5036 return decl_die_table->find_with_hash (decl, DECL_UID (decl));
5039 /* Returns a hash value for X (which really is a var_loc_list). */
5041 inline hashval_t
5042 decl_loc_hasher::hash (var_loc_list *x)
5044 return (hashval_t) x->decl_id;
5047 /* Return nonzero if decl_id of var_loc_list X is the same as
5048 UID of decl *Y. */
5050 inline bool
5051 decl_loc_hasher::equal (var_loc_list *x, const_tree y)
5053 return (x->decl_id == DECL_UID (y));
5056 /* Return the var_loc list associated with a given declaration. */
5058 static inline var_loc_list *
5059 lookup_decl_loc (const_tree decl)
5061 if (!decl_loc_table)
5062 return NULL;
5063 return decl_loc_table->find_with_hash (decl, DECL_UID (decl));
5066 /* Returns a hash value for X (which really is a cached_dw_loc_list_list). */
5068 inline hashval_t
5069 dw_loc_list_hasher::hash (cached_dw_loc_list *x)
5071 return (hashval_t) x->decl_id;
5074 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5075 UID of decl *Y. */
5077 inline bool
5078 dw_loc_list_hasher::equal (cached_dw_loc_list *x, const_tree y)
5080 return (x->decl_id == DECL_UID (y));
5083 /* Equate a DIE to a particular declaration. */
5085 static void
5086 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5088 unsigned int decl_id = DECL_UID (decl);
5090 *decl_die_table->find_slot_with_hash (decl, decl_id, INSERT) = decl_die;
5091 decl_die->decl_id = decl_id;
5094 /* Return how many bits covers PIECE EXPR_LIST. */
5096 static HOST_WIDE_INT
5097 decl_piece_bitsize (rtx piece)
5099 int ret = (int) GET_MODE (piece);
5100 if (ret)
5101 return ret;
5102 gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5103 && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5104 return INTVAL (XEXP (XEXP (piece, 0), 0));
5107 /* Return pointer to the location of location note in PIECE EXPR_LIST. */
5109 static rtx *
5110 decl_piece_varloc_ptr (rtx piece)
5112 if ((int) GET_MODE (piece))
5113 return &XEXP (piece, 0);
5114 else
5115 return &XEXP (XEXP (piece, 0), 1);
5118 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5119 Next is the chain of following piece nodes. */
5121 static rtx_expr_list *
5122 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5124 if (bitsize > 0 && bitsize <= (int) MAX_MACHINE_MODE)
5125 return alloc_EXPR_LIST (bitsize, loc_note, next);
5126 else
5127 return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5128 GEN_INT (bitsize),
5129 loc_note), next);
5132 /* Return rtx that should be stored into loc field for
5133 LOC_NOTE and BITPOS/BITSIZE. */
5135 static rtx
5136 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5137 HOST_WIDE_INT bitsize)
5139 if (bitsize != -1)
5141 loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5142 if (bitpos != 0)
5143 loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5145 return loc_note;
5148 /* This function either modifies location piece list *DEST in
5149 place (if SRC and INNER is NULL), or copies location piece list
5150 *SRC to *DEST while modifying it. Location BITPOS is modified
5151 to contain LOC_NOTE, any pieces overlapping it are removed resp.
5152 not copied and if needed some padding around it is added.
5153 When modifying in place, DEST should point to EXPR_LIST where
5154 earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5155 to the start of the whole list and INNER points to the EXPR_LIST
5156 where earlier pieces cover PIECE_BITPOS bits. */
5158 static void
5159 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5160 HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5161 HOST_WIDE_INT bitsize, rtx loc_note)
5163 HOST_WIDE_INT diff;
5164 bool copy = inner != NULL;
5166 if (copy)
5168 /* First copy all nodes preceding the current bitpos. */
5169 while (src != inner)
5171 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5172 decl_piece_bitsize (*src), NULL_RTX);
5173 dest = &XEXP (*dest, 1);
5174 src = &XEXP (*src, 1);
5177 /* Add padding if needed. */
5178 if (bitpos != piece_bitpos)
5180 *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5181 copy ? NULL_RTX : *dest);
5182 dest = &XEXP (*dest, 1);
5184 else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5186 gcc_assert (!copy);
5187 /* A piece with correct bitpos and bitsize already exist,
5188 just update the location for it and return. */
5189 *decl_piece_varloc_ptr (*dest) = loc_note;
5190 return;
5192 /* Add the piece that changed. */
5193 *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5194 dest = &XEXP (*dest, 1);
5195 /* Skip over pieces that overlap it. */
5196 diff = bitpos - piece_bitpos + bitsize;
5197 if (!copy)
5198 src = dest;
5199 while (diff > 0 && *src)
5201 rtx piece = *src;
5202 diff -= decl_piece_bitsize (piece);
5203 if (copy)
5204 src = &XEXP (piece, 1);
5205 else
5207 *src = XEXP (piece, 1);
5208 free_EXPR_LIST_node (piece);
5211 /* Add padding if needed. */
5212 if (diff < 0 && *src)
5214 if (!copy)
5215 dest = src;
5216 *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5217 dest = &XEXP (*dest, 1);
5219 if (!copy)
5220 return;
5221 /* Finally copy all nodes following it. */
5222 while (*src)
5224 *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5225 decl_piece_bitsize (*src), NULL_RTX);
5226 dest = &XEXP (*dest, 1);
5227 src = &XEXP (*src, 1);
5231 /* Add a variable location node to the linked list for DECL. */
5233 static struct var_loc_node *
5234 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5236 unsigned int decl_id;
5237 var_loc_list *temp;
5238 struct var_loc_node *loc = NULL;
5239 HOST_WIDE_INT bitsize = -1, bitpos = -1;
5241 if (TREE_CODE (decl) == VAR_DECL
5242 && DECL_HAS_DEBUG_EXPR_P (decl))
5244 tree realdecl = DECL_DEBUG_EXPR (decl);
5245 if (handled_component_p (realdecl)
5246 || (TREE_CODE (realdecl) == MEM_REF
5247 && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR))
5249 HOST_WIDE_INT maxsize;
5250 tree innerdecl;
5251 innerdecl
5252 = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5253 if (!DECL_P (innerdecl)
5254 || DECL_IGNORED_P (innerdecl)
5255 || TREE_STATIC (innerdecl)
5256 || bitsize <= 0
5257 || bitpos + bitsize > 256
5258 || bitsize != maxsize)
5259 return NULL;
5260 decl = innerdecl;
5264 decl_id = DECL_UID (decl);
5265 var_loc_list **slot
5266 = decl_loc_table->find_slot_with_hash (decl, decl_id, INSERT);
5267 if (*slot == NULL)
5269 temp = ggc_cleared_alloc<var_loc_list> ();
5270 temp->decl_id = decl_id;
5271 *slot = temp;
5273 else
5274 temp = *slot;
5276 /* For PARM_DECLs try to keep around the original incoming value,
5277 even if that means we'll emit a zero-range .debug_loc entry. */
5278 if (temp->last
5279 && temp->first == temp->last
5280 && TREE_CODE (decl) == PARM_DECL
5281 && NOTE_P (temp->first->loc)
5282 && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5283 && DECL_INCOMING_RTL (decl)
5284 && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5285 && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5286 == GET_CODE (DECL_INCOMING_RTL (decl))
5287 && prev_real_insn (temp->first->loc) == NULL_RTX
5288 && (bitsize != -1
5289 || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5290 NOTE_VAR_LOCATION_LOC (loc_note))
5291 || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5292 != NOTE_VAR_LOCATION_STATUS (loc_note))))
5294 loc = ggc_cleared_alloc<var_loc_node> ();
5295 temp->first->next = loc;
5296 temp->last = loc;
5297 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5299 else if (temp->last)
5301 struct var_loc_node *last = temp->last, *unused = NULL;
5302 rtx *piece_loc = NULL, last_loc_note;
5303 HOST_WIDE_INT piece_bitpos = 0;
5304 if (last->next)
5306 last = last->next;
5307 gcc_assert (last->next == NULL);
5309 if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5311 piece_loc = &last->loc;
5314 HOST_WIDE_INT cur_bitsize = decl_piece_bitsize (*piece_loc);
5315 if (piece_bitpos + cur_bitsize > bitpos)
5316 break;
5317 piece_bitpos += cur_bitsize;
5318 piece_loc = &XEXP (*piece_loc, 1);
5320 while (*piece_loc);
5322 /* TEMP->LAST here is either pointer to the last but one or
5323 last element in the chained list, LAST is pointer to the
5324 last element. */
5325 if (label && strcmp (last->label, label) == 0)
5327 /* For SRA optimized variables if there weren't any real
5328 insns since last note, just modify the last node. */
5329 if (piece_loc != NULL)
5331 adjust_piece_list (piece_loc, NULL, NULL,
5332 bitpos, piece_bitpos, bitsize, loc_note);
5333 return NULL;
5335 /* If the last note doesn't cover any instructions, remove it. */
5336 if (temp->last != last)
5338 temp->last->next = NULL;
5339 unused = last;
5340 last = temp->last;
5341 gcc_assert (strcmp (last->label, label) != 0);
5343 else
5345 gcc_assert (temp->first == temp->last
5346 || (temp->first->next == temp->last
5347 && TREE_CODE (decl) == PARM_DECL));
5348 memset (temp->last, '\0', sizeof (*temp->last));
5349 temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5350 return temp->last;
5353 if (bitsize == -1 && NOTE_P (last->loc))
5354 last_loc_note = last->loc;
5355 else if (piece_loc != NULL
5356 && *piece_loc != NULL_RTX
5357 && piece_bitpos == bitpos
5358 && decl_piece_bitsize (*piece_loc) == bitsize)
5359 last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5360 else
5361 last_loc_note = NULL_RTX;
5362 /* If the current location is the same as the end of the list,
5363 and either both or neither of the locations is uninitialized,
5364 we have nothing to do. */
5365 if (last_loc_note == NULL_RTX
5366 || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5367 NOTE_VAR_LOCATION_LOC (loc_note)))
5368 || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5369 != NOTE_VAR_LOCATION_STATUS (loc_note))
5370 && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5371 == VAR_INIT_STATUS_UNINITIALIZED)
5372 || (NOTE_VAR_LOCATION_STATUS (loc_note)
5373 == VAR_INIT_STATUS_UNINITIALIZED))))
5375 /* Add LOC to the end of list and update LAST. If the last
5376 element of the list has been removed above, reuse its
5377 memory for the new node, otherwise allocate a new one. */
5378 if (unused)
5380 loc = unused;
5381 memset (loc, '\0', sizeof (*loc));
5383 else
5384 loc = ggc_cleared_alloc<var_loc_node> ();
5385 if (bitsize == -1 || piece_loc == NULL)
5386 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5387 else
5388 adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5389 bitpos, piece_bitpos, bitsize, loc_note);
5390 last->next = loc;
5391 /* Ensure TEMP->LAST will point either to the new last but one
5392 element of the chain, or to the last element in it. */
5393 if (last != temp->last)
5394 temp->last = last;
5396 else if (unused)
5397 ggc_free (unused);
5399 else
5401 loc = ggc_cleared_alloc<var_loc_node> ();
5402 temp->first = loc;
5403 temp->last = loc;
5404 loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5406 return loc;
5409 /* Keep track of the number of spaces used to indent the
5410 output of the debugging routines that print the structure of
5411 the DIE internal representation. */
5412 static int print_indent;
5414 /* Indent the line the number of spaces given by print_indent. */
5416 static inline void
5417 print_spaces (FILE *outfile)
5419 fprintf (outfile, "%*s", print_indent, "");
5422 /* Print a type signature in hex. */
5424 static inline void
5425 print_signature (FILE *outfile, char *sig)
5427 int i;
5429 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5430 fprintf (outfile, "%02x", sig[i] & 0xff);
5433 static void print_loc_descr (dw_loc_descr_ref, FILE *);
5435 /* Print the value associated to the VAL DWARF value node to OUTFILE. If
5436 RECURSE, output location descriptor operations. */
5438 static void
5439 print_dw_val (dw_val_node *val, bool recurse, FILE *outfile)
5441 switch (val->val_class)
5443 case dw_val_class_addr:
5444 fprintf (outfile, "address");
5445 break;
5446 case dw_val_class_offset:
5447 fprintf (outfile, "offset");
5448 break;
5449 case dw_val_class_loc:
5450 fprintf (outfile, "location descriptor");
5451 if (val->v.val_loc == NULL)
5452 fprintf (outfile, " -> <null>\n");
5453 else if (recurse)
5455 fprintf (outfile, ":\n");
5456 print_indent += 4;
5457 print_loc_descr (val->v.val_loc, outfile);
5458 print_indent -= 4;
5460 else
5461 fprintf (outfile, " (%p)\n", (void *) val->v.val_loc);
5462 break;
5463 case dw_val_class_loc_list:
5464 fprintf (outfile, "location list -> label:%s",
5465 val->v.val_loc_list->ll_symbol);
5466 break;
5467 case dw_val_class_range_list:
5468 fprintf (outfile, "range list");
5469 break;
5470 case dw_val_class_const:
5471 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, val->v.val_int);
5472 break;
5473 case dw_val_class_unsigned_const:
5474 fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, val->v.val_unsigned);
5475 break;
5476 case dw_val_class_const_double:
5477 fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_DEC","\
5478 HOST_WIDE_INT_PRINT_UNSIGNED")",
5479 val->v.val_double.high,
5480 val->v.val_double.low);
5481 break;
5482 case dw_val_class_wide_int:
5484 int i = val->v.val_wide->get_len ();
5485 fprintf (outfile, "constant (");
5486 gcc_assert (i > 0);
5487 if (val->v.val_wide->elt (i - 1) == 0)
5488 fprintf (outfile, "0x");
5489 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX,
5490 val->v.val_wide->elt (--i));
5491 while (--i >= 0)
5492 fprintf (outfile, HOST_WIDE_INT_PRINT_PADDED_HEX,
5493 val->v.val_wide->elt (i));
5494 fprintf (outfile, ")");
5495 break;
5497 case dw_val_class_vec:
5498 fprintf (outfile, "floating-point or vector constant");
5499 break;
5500 case dw_val_class_flag:
5501 fprintf (outfile, "%u", val->v.val_flag);
5502 break;
5503 case dw_val_class_die_ref:
5504 if (val->v.val_die_ref.die != NULL)
5506 dw_die_ref die = val->v.val_die_ref.die;
5508 if (die->comdat_type_p)
5510 fprintf (outfile, "die -> signature: ");
5511 print_signature (outfile,
5512 die->die_id.die_type_node->signature);
5514 else if (die->die_id.die_symbol)
5515 fprintf (outfile, "die -> label: %s", die->die_id.die_symbol);
5516 else
5517 fprintf (outfile, "die -> %ld", die->die_offset);
5518 fprintf (outfile, " (%p)", (void *) die);
5520 else
5521 fprintf (outfile, "die -> <null>");
5522 break;
5523 case dw_val_class_vms_delta:
5524 fprintf (outfile, "delta: @slotcount(%s-%s)",
5525 val->v.val_vms_delta.lbl2, val->v.val_vms_delta.lbl1);
5526 break;
5527 case dw_val_class_lbl_id:
5528 case dw_val_class_lineptr:
5529 case dw_val_class_macptr:
5530 case dw_val_class_high_pc:
5531 fprintf (outfile, "label: %s", val->v.val_lbl_id);
5532 break;
5533 case dw_val_class_str:
5534 if (val->v.val_str->str != NULL)
5535 fprintf (outfile, "\"%s\"", val->v.val_str->str);
5536 else
5537 fprintf (outfile, "<null>");
5538 break;
5539 case dw_val_class_file:
5540 fprintf (outfile, "\"%s\" (%d)", val->v.val_file->filename,
5541 val->v.val_file->emitted_number);
5542 break;
5543 case dw_val_class_data8:
5545 int i;
5547 for (i = 0; i < 8; i++)
5548 fprintf (outfile, "%02x", val->v.val_data8[i]);
5549 break;
5551 default:
5552 break;
5556 /* Likewise, for a DIE attribute. */
5558 static void
5559 print_attribute (dw_attr_ref a, bool recurse, FILE *outfile)
5561 print_dw_val (&a->dw_attr_val, recurse, outfile);
5565 /* Print the list of operands in the LOC location description to OUTFILE. This
5566 routine is a debugging aid only. */
5568 static void
5569 print_loc_descr (dw_loc_descr_ref loc, FILE *outfile)
5571 dw_loc_descr_ref l = loc;
5573 if (loc == NULL)
5575 print_spaces (outfile);
5576 fprintf (outfile, "<null>\n");
5577 return;
5580 for (l = loc; l != NULL; l = l->dw_loc_next)
5582 print_spaces (outfile);
5583 fprintf (outfile, "(%p) %s",
5584 (void *) l,
5585 dwarf_stack_op_name (l->dw_loc_opc));
5586 if (l->dw_loc_oprnd1.val_class != dw_val_class_none)
5588 fprintf (outfile, " ");
5589 print_dw_val (&l->dw_loc_oprnd1, false, outfile);
5591 if (l->dw_loc_oprnd2.val_class != dw_val_class_none)
5593 fprintf (outfile, ", ");
5594 print_dw_val (&l->dw_loc_oprnd2, false, outfile);
5596 fprintf (outfile, "\n");
5600 /* Print the information associated with a given DIE, and its children.
5601 This routine is a debugging aid only. */
5603 static void
5604 print_die (dw_die_ref die, FILE *outfile)
5606 dw_attr_ref a;
5607 dw_die_ref c;
5608 unsigned ix;
5610 print_spaces (outfile);
5611 fprintf (outfile, "DIE %4ld: %s (%p)\n",
5612 die->die_offset, dwarf_tag_name (die->die_tag),
5613 (void*) die);
5614 print_spaces (outfile);
5615 fprintf (outfile, " abbrev id: %lu", die->die_abbrev);
5616 fprintf (outfile, " offset: %ld", die->die_offset);
5617 fprintf (outfile, " mark: %d\n", die->die_mark);
5619 if (die->comdat_type_p)
5621 print_spaces (outfile);
5622 fprintf (outfile, " signature: ");
5623 print_signature (outfile, die->die_id.die_type_node->signature);
5624 fprintf (outfile, "\n");
5627 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5629 print_spaces (outfile);
5630 fprintf (outfile, " %s: ", dwarf_attr_name (a->dw_attr));
5632 print_attribute (a, true, outfile);
5633 fprintf (outfile, "\n");
5636 if (die->die_child != NULL)
5638 print_indent += 4;
5639 FOR_EACH_CHILD (die, c, print_die (c, outfile));
5640 print_indent -= 4;
5642 if (print_indent == 0)
5643 fprintf (outfile, "\n");
5646 /* Print the list of operations in the LOC location description. */
5648 DEBUG_FUNCTION void
5649 debug_dwarf_loc_descr (dw_loc_descr_ref loc)
5651 print_loc_descr (loc, stderr);
5654 /* Print the information collected for a given DIE. */
5656 DEBUG_FUNCTION void
5657 debug_dwarf_die (dw_die_ref die)
5659 print_die (die, stderr);
5662 DEBUG_FUNCTION void
5663 debug (die_struct &ref)
5665 print_die (&ref, stderr);
5668 DEBUG_FUNCTION void
5669 debug (die_struct *ptr)
5671 if (ptr)
5672 debug (*ptr);
5673 else
5674 fprintf (stderr, "<nil>\n");
5678 /* Print all DWARF information collected for the compilation unit.
5679 This routine is a debugging aid only. */
5681 DEBUG_FUNCTION void
5682 debug_dwarf (void)
5684 print_indent = 0;
5685 print_die (comp_unit_die (), stderr);
5688 #ifdef ENABLE_CHECKING
5689 /* Sanity checks on DIEs. */
5691 static void
5692 check_die (dw_die_ref die)
5694 unsigned ix;
5695 dw_attr_ref a;
5696 bool inline_found = false;
5697 int n_location = 0, n_low_pc = 0, n_high_pc = 0, n_artificial = 0;
5698 int n_decl_line = 0, n_decl_file = 0;
5699 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5701 switch (a->dw_attr)
5703 case DW_AT_inline:
5704 if (a->dw_attr_val.v.val_unsigned)
5705 inline_found = true;
5706 break;
5707 case DW_AT_location:
5708 ++n_location;
5709 break;
5710 case DW_AT_low_pc:
5711 ++n_low_pc;
5712 break;
5713 case DW_AT_high_pc:
5714 ++n_high_pc;
5715 break;
5716 case DW_AT_artificial:
5717 ++n_artificial;
5718 break;
5719 case DW_AT_decl_line:
5720 ++n_decl_line;
5721 break;
5722 case DW_AT_decl_file:
5723 ++n_decl_file;
5724 break;
5725 default:
5726 break;
5729 if (n_location > 1 || n_low_pc > 1 || n_high_pc > 1 || n_artificial > 1
5730 || n_decl_line > 1 || n_decl_file > 1)
5732 fprintf (stderr, "Duplicate attributes in DIE:\n");
5733 debug_dwarf_die (die);
5734 gcc_unreachable ();
5736 if (inline_found)
5738 /* A debugging information entry that is a member of an abstract
5739 instance tree [that has DW_AT_inline] should not contain any
5740 attributes which describe aspects of the subroutine which vary
5741 between distinct inlined expansions or distinct out-of-line
5742 expansions. */
5743 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5744 gcc_assert (a->dw_attr != DW_AT_low_pc
5745 && a->dw_attr != DW_AT_high_pc
5746 && a->dw_attr != DW_AT_location
5747 && a->dw_attr != DW_AT_frame_base
5748 && a->dw_attr != DW_AT_GNU_all_call_sites);
5751 #endif
5753 /* Start a new compilation unit DIE for an include file. OLD_UNIT is the CU
5754 for the enclosing include file, if any. BINCL_DIE is the DW_TAG_GNU_BINCL
5755 DIE that marks the start of the DIEs for this include file. */
5757 static dw_die_ref
5758 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5760 const char *filename = get_AT_string (bincl_die, DW_AT_name);
5761 dw_die_ref new_unit = gen_compile_unit_die (filename);
5763 new_unit->die_sib = old_unit;
5764 return new_unit;
5767 /* Close an include-file CU and reopen the enclosing one. */
5769 static dw_die_ref
5770 pop_compile_unit (dw_die_ref old_unit)
5772 dw_die_ref new_unit = old_unit->die_sib;
5774 old_unit->die_sib = NULL;
5775 return new_unit;
5778 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5779 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5780 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5782 /* Calculate the checksum of a location expression. */
5784 static inline void
5785 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5787 int tem;
5788 inchash::hash hstate;
5789 hashval_t hash;
5791 tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5792 CHECKSUM (tem);
5793 hash_loc_operands (loc, hstate);
5794 hash = hstate.end();
5795 CHECKSUM (hash);
5798 /* Calculate the checksum of an attribute. */
5800 static void
5801 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5803 dw_loc_descr_ref loc;
5804 rtx r;
5806 CHECKSUM (at->dw_attr);
5808 /* We don't care that this was compiled with a different compiler
5809 snapshot; if the output is the same, that's what matters. */
5810 if (at->dw_attr == DW_AT_producer)
5811 return;
5813 switch (AT_class (at))
5815 case dw_val_class_const:
5816 CHECKSUM (at->dw_attr_val.v.val_int);
5817 break;
5818 case dw_val_class_unsigned_const:
5819 CHECKSUM (at->dw_attr_val.v.val_unsigned);
5820 break;
5821 case dw_val_class_const_double:
5822 CHECKSUM (at->dw_attr_val.v.val_double);
5823 break;
5824 case dw_val_class_wide_int:
5825 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
5826 get_full_len (*at->dw_attr_val.v.val_wide)
5827 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
5828 break;
5829 case dw_val_class_vec:
5830 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
5831 (at->dw_attr_val.v.val_vec.length
5832 * at->dw_attr_val.v.val_vec.elt_size));
5833 break;
5834 case dw_val_class_flag:
5835 CHECKSUM (at->dw_attr_val.v.val_flag);
5836 break;
5837 case dw_val_class_str:
5838 CHECKSUM_STRING (AT_string (at));
5839 break;
5841 case dw_val_class_addr:
5842 r = AT_addr (at);
5843 gcc_assert (GET_CODE (r) == SYMBOL_REF);
5844 CHECKSUM_STRING (XSTR (r, 0));
5845 break;
5847 case dw_val_class_offset:
5848 CHECKSUM (at->dw_attr_val.v.val_offset);
5849 break;
5851 case dw_val_class_loc:
5852 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5853 loc_checksum (loc, ctx);
5854 break;
5856 case dw_val_class_die_ref:
5857 die_checksum (AT_ref (at), ctx, mark);
5858 break;
5860 case dw_val_class_fde_ref:
5861 case dw_val_class_vms_delta:
5862 case dw_val_class_lbl_id:
5863 case dw_val_class_lineptr:
5864 case dw_val_class_macptr:
5865 case dw_val_class_high_pc:
5866 break;
5868 case dw_val_class_file:
5869 CHECKSUM_STRING (AT_file (at)->filename);
5870 break;
5872 case dw_val_class_data8:
5873 CHECKSUM (at->dw_attr_val.v.val_data8);
5874 break;
5876 default:
5877 break;
5881 /* Calculate the checksum of a DIE. */
5883 static void
5884 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5886 dw_die_ref c;
5887 dw_attr_ref a;
5888 unsigned ix;
5890 /* To avoid infinite recursion. */
5891 if (die->die_mark)
5893 CHECKSUM (die->die_mark);
5894 return;
5896 die->die_mark = ++(*mark);
5898 CHECKSUM (die->die_tag);
5900 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5901 attr_checksum (a, ctx, mark);
5903 FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5906 #undef CHECKSUM
5907 #undef CHECKSUM_BLOCK
5908 #undef CHECKSUM_STRING
5910 /* For DWARF-4 types, include the trailing NULL when checksumming strings. */
5911 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5912 #define CHECKSUM_BLOCK(FOO, SIZE) md5_process_bytes ((FOO), (SIZE), ctx)
5913 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5914 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5915 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5916 #define CHECKSUM_ATTR(FOO) \
5917 if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5919 /* Calculate the checksum of a number in signed LEB128 format. */
5921 static void
5922 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5924 unsigned char byte;
5925 bool more;
5927 while (1)
5929 byte = (value & 0x7f);
5930 value >>= 7;
5931 more = !((value == 0 && (byte & 0x40) == 0)
5932 || (value == -1 && (byte & 0x40) != 0));
5933 if (more)
5934 byte |= 0x80;
5935 CHECKSUM (byte);
5936 if (!more)
5937 break;
5941 /* Calculate the checksum of a number in unsigned LEB128 format. */
5943 static void
5944 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5946 while (1)
5948 unsigned char byte = (value & 0x7f);
5949 value >>= 7;
5950 if (value != 0)
5951 /* More bytes to follow. */
5952 byte |= 0x80;
5953 CHECKSUM (byte);
5954 if (value == 0)
5955 break;
5959 /* Checksum the context of the DIE. This adds the names of any
5960 surrounding namespaces or structures to the checksum. */
5962 static void
5963 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5965 const char *name;
5966 dw_die_ref spec;
5967 int tag = die->die_tag;
5969 if (tag != DW_TAG_namespace
5970 && tag != DW_TAG_structure_type
5971 && tag != DW_TAG_class_type)
5972 return;
5974 name = get_AT_string (die, DW_AT_name);
5976 spec = get_AT_ref (die, DW_AT_specification);
5977 if (spec != NULL)
5978 die = spec;
5980 if (die->die_parent != NULL)
5981 checksum_die_context (die->die_parent, ctx);
5983 CHECKSUM_ULEB128 ('C');
5984 CHECKSUM_ULEB128 (tag);
5985 if (name != NULL)
5986 CHECKSUM_STRING (name);
5989 /* Calculate the checksum of a location expression. */
5991 static inline void
5992 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5994 /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5995 were emitted as a DW_FORM_sdata instead of a location expression. */
5996 if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5998 CHECKSUM_ULEB128 (DW_FORM_sdata);
5999 CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6000 return;
6003 /* Otherwise, just checksum the raw location expression. */
6004 while (loc != NULL)
6006 inchash::hash hstate;
6007 hashval_t hash;
6009 CHECKSUM_ULEB128 (loc->dtprel);
6010 CHECKSUM_ULEB128 (loc->dw_loc_opc);
6011 hash_loc_operands (loc, hstate);
6012 hash = hstate.end ();
6013 CHECKSUM (hash);
6014 loc = loc->dw_loc_next;
6018 /* Calculate the checksum of an attribute. */
6020 static void
6021 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6022 struct md5_ctx *ctx, int *mark)
6024 dw_loc_descr_ref loc;
6025 rtx r;
6027 if (AT_class (at) == dw_val_class_die_ref)
6029 dw_die_ref target_die = AT_ref (at);
6031 /* For pointer and reference types, we checksum only the (qualified)
6032 name of the target type (if there is a name). For friend entries,
6033 we checksum only the (qualified) name of the target type or function.
6034 This allows the checksum to remain the same whether the target type
6035 is complete or not. */
6036 if ((at->dw_attr == DW_AT_type
6037 && (tag == DW_TAG_pointer_type
6038 || tag == DW_TAG_reference_type
6039 || tag == DW_TAG_rvalue_reference_type
6040 || tag == DW_TAG_ptr_to_member_type))
6041 || (at->dw_attr == DW_AT_friend
6042 && tag == DW_TAG_friend))
6044 dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6046 if (name_attr != NULL)
6048 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6050 if (decl == NULL)
6051 decl = target_die;
6052 CHECKSUM_ULEB128 ('N');
6053 CHECKSUM_ULEB128 (at->dw_attr);
6054 if (decl->die_parent != NULL)
6055 checksum_die_context (decl->die_parent, ctx);
6056 CHECKSUM_ULEB128 ('E');
6057 CHECKSUM_STRING (AT_string (name_attr));
6058 return;
6062 /* For all other references to another DIE, we check to see if the
6063 target DIE has already been visited. If it has, we emit a
6064 backward reference; if not, we descend recursively. */
6065 if (target_die->die_mark > 0)
6067 CHECKSUM_ULEB128 ('R');
6068 CHECKSUM_ULEB128 (at->dw_attr);
6069 CHECKSUM_ULEB128 (target_die->die_mark);
6071 else
6073 dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6075 if (decl == NULL)
6076 decl = target_die;
6077 target_die->die_mark = ++(*mark);
6078 CHECKSUM_ULEB128 ('T');
6079 CHECKSUM_ULEB128 (at->dw_attr);
6080 if (decl->die_parent != NULL)
6081 checksum_die_context (decl->die_parent, ctx);
6082 die_checksum_ordered (target_die, ctx, mark);
6084 return;
6087 CHECKSUM_ULEB128 ('A');
6088 CHECKSUM_ULEB128 (at->dw_attr);
6090 switch (AT_class (at))
6092 case dw_val_class_const:
6093 CHECKSUM_ULEB128 (DW_FORM_sdata);
6094 CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6095 break;
6097 case dw_val_class_unsigned_const:
6098 CHECKSUM_ULEB128 (DW_FORM_sdata);
6099 CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6100 break;
6102 case dw_val_class_const_double:
6103 CHECKSUM_ULEB128 (DW_FORM_block);
6104 CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6105 CHECKSUM (at->dw_attr_val.v.val_double);
6106 break;
6108 case dw_val_class_wide_int:
6109 CHECKSUM_ULEB128 (DW_FORM_block);
6110 CHECKSUM_ULEB128 (get_full_len (*at->dw_attr_val.v.val_wide)
6111 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
6112 CHECKSUM_BLOCK (at->dw_attr_val.v.val_wide->get_val (),
6113 get_full_len (*at->dw_attr_val.v.val_wide)
6114 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
6115 break;
6117 case dw_val_class_vec:
6118 CHECKSUM_ULEB128 (DW_FORM_block);
6119 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_vec.length
6120 * at->dw_attr_val.v.val_vec.elt_size);
6121 CHECKSUM_BLOCK (at->dw_attr_val.v.val_vec.array,
6122 (at->dw_attr_val.v.val_vec.length
6123 * at->dw_attr_val.v.val_vec.elt_size));
6124 break;
6126 case dw_val_class_flag:
6127 CHECKSUM_ULEB128 (DW_FORM_flag);
6128 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6129 break;
6131 case dw_val_class_str:
6132 CHECKSUM_ULEB128 (DW_FORM_string);
6133 CHECKSUM_STRING (AT_string (at));
6134 break;
6136 case dw_val_class_addr:
6137 r = AT_addr (at);
6138 gcc_assert (GET_CODE (r) == SYMBOL_REF);
6139 CHECKSUM_ULEB128 (DW_FORM_string);
6140 CHECKSUM_STRING (XSTR (r, 0));
6141 break;
6143 case dw_val_class_offset:
6144 CHECKSUM_ULEB128 (DW_FORM_sdata);
6145 CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6146 break;
6148 case dw_val_class_loc:
6149 for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6150 loc_checksum_ordered (loc, ctx);
6151 break;
6153 case dw_val_class_fde_ref:
6154 case dw_val_class_lbl_id:
6155 case dw_val_class_lineptr:
6156 case dw_val_class_macptr:
6157 case dw_val_class_high_pc:
6158 break;
6160 case dw_val_class_file:
6161 CHECKSUM_ULEB128 (DW_FORM_string);
6162 CHECKSUM_STRING (AT_file (at)->filename);
6163 break;
6165 case dw_val_class_data8:
6166 CHECKSUM (at->dw_attr_val.v.val_data8);
6167 break;
6169 default:
6170 break;
6174 struct checksum_attributes
6176 dw_attr_ref at_name;
6177 dw_attr_ref at_type;
6178 dw_attr_ref at_friend;
6179 dw_attr_ref at_accessibility;
6180 dw_attr_ref at_address_class;
6181 dw_attr_ref at_allocated;
6182 dw_attr_ref at_artificial;
6183 dw_attr_ref at_associated;
6184 dw_attr_ref at_binary_scale;
6185 dw_attr_ref at_bit_offset;
6186 dw_attr_ref at_bit_size;
6187 dw_attr_ref at_bit_stride;
6188 dw_attr_ref at_byte_size;
6189 dw_attr_ref at_byte_stride;
6190 dw_attr_ref at_const_value;
6191 dw_attr_ref at_containing_type;
6192 dw_attr_ref at_count;
6193 dw_attr_ref at_data_location;
6194 dw_attr_ref at_data_member_location;
6195 dw_attr_ref at_decimal_scale;
6196 dw_attr_ref at_decimal_sign;
6197 dw_attr_ref at_default_value;
6198 dw_attr_ref at_digit_count;
6199 dw_attr_ref at_discr;
6200 dw_attr_ref at_discr_list;
6201 dw_attr_ref at_discr_value;
6202 dw_attr_ref at_encoding;
6203 dw_attr_ref at_endianity;
6204 dw_attr_ref at_explicit;
6205 dw_attr_ref at_is_optional;
6206 dw_attr_ref at_location;
6207 dw_attr_ref at_lower_bound;
6208 dw_attr_ref at_mutable;
6209 dw_attr_ref at_ordering;
6210 dw_attr_ref at_picture_string;
6211 dw_attr_ref at_prototyped;
6212 dw_attr_ref at_small;
6213 dw_attr_ref at_segment;
6214 dw_attr_ref at_string_length;
6215 dw_attr_ref at_threads_scaled;
6216 dw_attr_ref at_upper_bound;
6217 dw_attr_ref at_use_location;
6218 dw_attr_ref at_use_UTF8;
6219 dw_attr_ref at_variable_parameter;
6220 dw_attr_ref at_virtuality;
6221 dw_attr_ref at_visibility;
6222 dw_attr_ref at_vtable_elem_location;
6225 /* Collect the attributes that we will want to use for the checksum. */
6227 static void
6228 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6230 dw_attr_ref a;
6231 unsigned ix;
6233 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6235 switch (a->dw_attr)
6237 case DW_AT_name:
6238 attrs->at_name = a;
6239 break;
6240 case DW_AT_type:
6241 attrs->at_type = a;
6242 break;
6243 case DW_AT_friend:
6244 attrs->at_friend = a;
6245 break;
6246 case DW_AT_accessibility:
6247 attrs->at_accessibility = a;
6248 break;
6249 case DW_AT_address_class:
6250 attrs->at_address_class = a;
6251 break;
6252 case DW_AT_allocated:
6253 attrs->at_allocated = a;
6254 break;
6255 case DW_AT_artificial:
6256 attrs->at_artificial = a;
6257 break;
6258 case DW_AT_associated:
6259 attrs->at_associated = a;
6260 break;
6261 case DW_AT_binary_scale:
6262 attrs->at_binary_scale = a;
6263 break;
6264 case DW_AT_bit_offset:
6265 attrs->at_bit_offset = a;
6266 break;
6267 case DW_AT_bit_size:
6268 attrs->at_bit_size = a;
6269 break;
6270 case DW_AT_bit_stride:
6271 attrs->at_bit_stride = a;
6272 break;
6273 case DW_AT_byte_size:
6274 attrs->at_byte_size = a;
6275 break;
6276 case DW_AT_byte_stride:
6277 attrs->at_byte_stride = a;
6278 break;
6279 case DW_AT_const_value:
6280 attrs->at_const_value = a;
6281 break;
6282 case DW_AT_containing_type:
6283 attrs->at_containing_type = a;
6284 break;
6285 case DW_AT_count:
6286 attrs->at_count = a;
6287 break;
6288 case DW_AT_data_location:
6289 attrs->at_data_location = a;
6290 break;
6291 case DW_AT_data_member_location:
6292 attrs->at_data_member_location = a;
6293 break;
6294 case DW_AT_decimal_scale:
6295 attrs->at_decimal_scale = a;
6296 break;
6297 case DW_AT_decimal_sign:
6298 attrs->at_decimal_sign = a;
6299 break;
6300 case DW_AT_default_value:
6301 attrs->at_default_value = a;
6302 break;
6303 case DW_AT_digit_count:
6304 attrs->at_digit_count = a;
6305 break;
6306 case DW_AT_discr:
6307 attrs->at_discr = a;
6308 break;
6309 case DW_AT_discr_list:
6310 attrs->at_discr_list = a;
6311 break;
6312 case DW_AT_discr_value:
6313 attrs->at_discr_value = a;
6314 break;
6315 case DW_AT_encoding:
6316 attrs->at_encoding = a;
6317 break;
6318 case DW_AT_endianity:
6319 attrs->at_endianity = a;
6320 break;
6321 case DW_AT_explicit:
6322 attrs->at_explicit = a;
6323 break;
6324 case DW_AT_is_optional:
6325 attrs->at_is_optional = a;
6326 break;
6327 case DW_AT_location:
6328 attrs->at_location = a;
6329 break;
6330 case DW_AT_lower_bound:
6331 attrs->at_lower_bound = a;
6332 break;
6333 case DW_AT_mutable:
6334 attrs->at_mutable = a;
6335 break;
6336 case DW_AT_ordering:
6337 attrs->at_ordering = a;
6338 break;
6339 case DW_AT_picture_string:
6340 attrs->at_picture_string = a;
6341 break;
6342 case DW_AT_prototyped:
6343 attrs->at_prototyped = a;
6344 break;
6345 case DW_AT_small:
6346 attrs->at_small = a;
6347 break;
6348 case DW_AT_segment:
6349 attrs->at_segment = a;
6350 break;
6351 case DW_AT_string_length:
6352 attrs->at_string_length = a;
6353 break;
6354 case DW_AT_threads_scaled:
6355 attrs->at_threads_scaled = a;
6356 break;
6357 case DW_AT_upper_bound:
6358 attrs->at_upper_bound = a;
6359 break;
6360 case DW_AT_use_location:
6361 attrs->at_use_location = a;
6362 break;
6363 case DW_AT_use_UTF8:
6364 attrs->at_use_UTF8 = a;
6365 break;
6366 case DW_AT_variable_parameter:
6367 attrs->at_variable_parameter = a;
6368 break;
6369 case DW_AT_virtuality:
6370 attrs->at_virtuality = a;
6371 break;
6372 case DW_AT_visibility:
6373 attrs->at_visibility = a;
6374 break;
6375 case DW_AT_vtable_elem_location:
6376 attrs->at_vtable_elem_location = a;
6377 break;
6378 default:
6379 break;
6384 /* Calculate the checksum of a DIE, using an ordered subset of attributes. */
6386 static void
6387 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6389 dw_die_ref c;
6390 dw_die_ref decl;
6391 struct checksum_attributes attrs;
6393 CHECKSUM_ULEB128 ('D');
6394 CHECKSUM_ULEB128 (die->die_tag);
6396 memset (&attrs, 0, sizeof (attrs));
6398 decl = get_AT_ref (die, DW_AT_specification);
6399 if (decl != NULL)
6400 collect_checksum_attributes (&attrs, decl);
6401 collect_checksum_attributes (&attrs, die);
6403 CHECKSUM_ATTR (attrs.at_name);
6404 CHECKSUM_ATTR (attrs.at_accessibility);
6405 CHECKSUM_ATTR (attrs.at_address_class);
6406 CHECKSUM_ATTR (attrs.at_allocated);
6407 CHECKSUM_ATTR (attrs.at_artificial);
6408 CHECKSUM_ATTR (attrs.at_associated);
6409 CHECKSUM_ATTR (attrs.at_binary_scale);
6410 CHECKSUM_ATTR (attrs.at_bit_offset);
6411 CHECKSUM_ATTR (attrs.at_bit_size);
6412 CHECKSUM_ATTR (attrs.at_bit_stride);
6413 CHECKSUM_ATTR (attrs.at_byte_size);
6414 CHECKSUM_ATTR (attrs.at_byte_stride);
6415 CHECKSUM_ATTR (attrs.at_const_value);
6416 CHECKSUM_ATTR (attrs.at_containing_type);
6417 CHECKSUM_ATTR (attrs.at_count);
6418 CHECKSUM_ATTR (attrs.at_data_location);
6419 CHECKSUM_ATTR (attrs.at_data_member_location);
6420 CHECKSUM_ATTR (attrs.at_decimal_scale);
6421 CHECKSUM_ATTR (attrs.at_decimal_sign);
6422 CHECKSUM_ATTR (attrs.at_default_value);
6423 CHECKSUM_ATTR (attrs.at_digit_count);
6424 CHECKSUM_ATTR (attrs.at_discr);
6425 CHECKSUM_ATTR (attrs.at_discr_list);
6426 CHECKSUM_ATTR (attrs.at_discr_value);
6427 CHECKSUM_ATTR (attrs.at_encoding);
6428 CHECKSUM_ATTR (attrs.at_endianity);
6429 CHECKSUM_ATTR (attrs.at_explicit);
6430 CHECKSUM_ATTR (attrs.at_is_optional);
6431 CHECKSUM_ATTR (attrs.at_location);
6432 CHECKSUM_ATTR (attrs.at_lower_bound);
6433 CHECKSUM_ATTR (attrs.at_mutable);
6434 CHECKSUM_ATTR (attrs.at_ordering);
6435 CHECKSUM_ATTR (attrs.at_picture_string);
6436 CHECKSUM_ATTR (attrs.at_prototyped);
6437 CHECKSUM_ATTR (attrs.at_small);
6438 CHECKSUM_ATTR (attrs.at_segment);
6439 CHECKSUM_ATTR (attrs.at_string_length);
6440 CHECKSUM_ATTR (attrs.at_threads_scaled);
6441 CHECKSUM_ATTR (attrs.at_upper_bound);
6442 CHECKSUM_ATTR (attrs.at_use_location);
6443 CHECKSUM_ATTR (attrs.at_use_UTF8);
6444 CHECKSUM_ATTR (attrs.at_variable_parameter);
6445 CHECKSUM_ATTR (attrs.at_virtuality);
6446 CHECKSUM_ATTR (attrs.at_visibility);
6447 CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6448 CHECKSUM_ATTR (attrs.at_type);
6449 CHECKSUM_ATTR (attrs.at_friend);
6451 /* Checksum the child DIEs. */
6452 c = die->die_child;
6453 if (c) do {
6454 dw_attr_ref name_attr;
6456 c = c->die_sib;
6457 name_attr = get_AT (c, DW_AT_name);
6458 if (is_template_instantiation (c))
6460 /* Ignore instantiations of member type and function templates. */
6462 else if (name_attr != NULL
6463 && (is_type_die (c) || c->die_tag == DW_TAG_subprogram))
6465 /* Use a shallow checksum for named nested types and member
6466 functions. */
6467 CHECKSUM_ULEB128 ('S');
6468 CHECKSUM_ULEB128 (c->die_tag);
6469 CHECKSUM_STRING (AT_string (name_attr));
6471 else
6473 /* Use a deep checksum for other children. */
6474 /* Mark this DIE so it gets processed when unmarking. */
6475 if (c->die_mark == 0)
6476 c->die_mark = -1;
6477 die_checksum_ordered (c, ctx, mark);
6479 } while (c != die->die_child);
6481 CHECKSUM_ULEB128 (0);
6484 /* Add a type name and tag to a hash. */
6485 static void
6486 die_odr_checksum (int tag, const char *name, md5_ctx *ctx)
6488 CHECKSUM_ULEB128 (tag);
6489 CHECKSUM_STRING (name);
6492 #undef CHECKSUM
6493 #undef CHECKSUM_STRING
6494 #undef CHECKSUM_ATTR
6495 #undef CHECKSUM_LEB128
6496 #undef CHECKSUM_ULEB128
6498 /* Generate the type signature for DIE. This is computed by generating an
6499 MD5 checksum over the DIE's tag, its relevant attributes, and its
6500 children. Attributes that are references to other DIEs are processed
6501 by recursion, using the MARK field to prevent infinite recursion.
6502 If the DIE is nested inside a namespace or another type, we also
6503 need to include that context in the signature. The lower 64 bits
6504 of the resulting MD5 checksum comprise the signature. */
6506 static void
6507 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6509 int mark;
6510 const char *name;
6511 unsigned char checksum[16];
6512 struct md5_ctx ctx;
6513 dw_die_ref decl;
6514 dw_die_ref parent;
6516 name = get_AT_string (die, DW_AT_name);
6517 decl = get_AT_ref (die, DW_AT_specification);
6518 parent = get_die_parent (die);
6520 /* First, compute a signature for just the type name (and its surrounding
6521 context, if any. This is stored in the type unit DIE for link-time
6522 ODR (one-definition rule) checking. */
6524 if (is_cxx () && name != NULL)
6526 md5_init_ctx (&ctx);
6528 /* Checksum the names of surrounding namespaces and structures. */
6529 if (parent != NULL)
6530 checksum_die_context (parent, &ctx);
6532 /* Checksum the current DIE. */
6533 die_odr_checksum (die->die_tag, name, &ctx);
6534 md5_finish_ctx (&ctx, checksum);
6536 add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6539 /* Next, compute the complete type signature. */
6541 md5_init_ctx (&ctx);
6542 mark = 1;
6543 die->die_mark = mark;
6545 /* Checksum the names of surrounding namespaces and structures. */
6546 if (parent != NULL)
6547 checksum_die_context (parent, &ctx);
6549 /* Checksum the DIE and its children. */
6550 die_checksum_ordered (die, &ctx, &mark);
6551 unmark_all_dies (die);
6552 md5_finish_ctx (&ctx, checksum);
6554 /* Store the signature in the type node and link the type DIE and the
6555 type node together. */
6556 memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6557 DWARF_TYPE_SIGNATURE_SIZE);
6558 die->comdat_type_p = true;
6559 die->die_id.die_type_node = type_node;
6560 type_node->type_die = die;
6562 /* If the DIE is a specification, link its declaration to the type node
6563 as well. */
6564 if (decl != NULL)
6566 decl->comdat_type_p = true;
6567 decl->die_id.die_type_node = type_node;
6571 /* Do the location expressions look same? */
6572 static inline int
6573 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6575 return loc1->dw_loc_opc == loc2->dw_loc_opc
6576 && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6577 && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6580 /* Do the values look the same? */
6581 static int
6582 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6584 dw_loc_descr_ref loc1, loc2;
6585 rtx r1, r2;
6587 if (v1->val_class != v2->val_class)
6588 return 0;
6590 switch (v1->val_class)
6592 case dw_val_class_const:
6593 return v1->v.val_int == v2->v.val_int;
6594 case dw_val_class_unsigned_const:
6595 return v1->v.val_unsigned == v2->v.val_unsigned;
6596 case dw_val_class_const_double:
6597 return v1->v.val_double.high == v2->v.val_double.high
6598 && v1->v.val_double.low == v2->v.val_double.low;
6599 case dw_val_class_wide_int:
6600 return *v1->v.val_wide == *v2->v.val_wide;
6601 case dw_val_class_vec:
6602 if (v1->v.val_vec.length != v2->v.val_vec.length
6603 || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6604 return 0;
6605 if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6606 v1->v.val_vec.length * v1->v.val_vec.elt_size))
6607 return 0;
6608 return 1;
6609 case dw_val_class_flag:
6610 return v1->v.val_flag == v2->v.val_flag;
6611 case dw_val_class_str:
6612 return !strcmp (v1->v.val_str->str, v2->v.val_str->str);
6614 case dw_val_class_addr:
6615 r1 = v1->v.val_addr;
6616 r2 = v2->v.val_addr;
6617 if (GET_CODE (r1) != GET_CODE (r2))
6618 return 0;
6619 return !rtx_equal_p (r1, r2);
6621 case dw_val_class_offset:
6622 return v1->v.val_offset == v2->v.val_offset;
6624 case dw_val_class_loc:
6625 for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6626 loc1 && loc2;
6627 loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6628 if (!same_loc_p (loc1, loc2, mark))
6629 return 0;
6630 return !loc1 && !loc2;
6632 case dw_val_class_die_ref:
6633 return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6635 case dw_val_class_fde_ref:
6636 case dw_val_class_vms_delta:
6637 case dw_val_class_lbl_id:
6638 case dw_val_class_lineptr:
6639 case dw_val_class_macptr:
6640 case dw_val_class_high_pc:
6641 return 1;
6643 case dw_val_class_file:
6644 return v1->v.val_file == v2->v.val_file;
6646 case dw_val_class_data8:
6647 return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6649 default:
6650 return 1;
6654 /* Do the attributes look the same? */
6656 static int
6657 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6659 if (at1->dw_attr != at2->dw_attr)
6660 return 0;
6662 /* We don't care that this was compiled with a different compiler
6663 snapshot; if the output is the same, that's what matters. */
6664 if (at1->dw_attr == DW_AT_producer)
6665 return 1;
6667 return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6670 /* Do the dies look the same? */
6672 static int
6673 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6675 dw_die_ref c1, c2;
6676 dw_attr_ref a1;
6677 unsigned ix;
6679 /* To avoid infinite recursion. */
6680 if (die1->die_mark)
6681 return die1->die_mark == die2->die_mark;
6682 die1->die_mark = die2->die_mark = ++(*mark);
6684 if (die1->die_tag != die2->die_tag)
6685 return 0;
6687 if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6688 return 0;
6690 FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6691 if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6692 return 0;
6694 c1 = die1->die_child;
6695 c2 = die2->die_child;
6696 if (! c1)
6698 if (c2)
6699 return 0;
6701 else
6702 for (;;)
6704 if (!same_die_p (c1, c2, mark))
6705 return 0;
6706 c1 = c1->die_sib;
6707 c2 = c2->die_sib;
6708 if (c1 == die1->die_child)
6710 if (c2 == die2->die_child)
6711 break;
6712 else
6713 return 0;
6717 return 1;
6720 /* Do the dies look the same? Wrapper around same_die_p. */
6722 static int
6723 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6725 int mark = 0;
6726 int ret = same_die_p (die1, die2, &mark);
6728 unmark_all_dies (die1);
6729 unmark_all_dies (die2);
6731 return ret;
6734 /* The prefix to attach to symbols on DIEs in the current comdat debug
6735 info section. */
6736 static const char *comdat_symbol_id;
6738 /* The index of the current symbol within the current comdat CU. */
6739 static unsigned int comdat_symbol_number;
6741 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6742 children, and set comdat_symbol_id accordingly. */
6744 static void
6745 compute_section_prefix (dw_die_ref unit_die)
6747 const char *die_name = get_AT_string (unit_die, DW_AT_name);
6748 const char *base = die_name ? lbasename (die_name) : "anonymous";
6749 char *name = XALLOCAVEC (char, strlen (base) + 64);
6750 char *p;
6751 int i, mark;
6752 unsigned char checksum[16];
6753 struct md5_ctx ctx;
6755 /* Compute the checksum of the DIE, then append part of it as hex digits to
6756 the name filename of the unit. */
6758 md5_init_ctx (&ctx);
6759 mark = 0;
6760 die_checksum (unit_die, &ctx, &mark);
6761 unmark_all_dies (unit_die);
6762 md5_finish_ctx (&ctx, checksum);
6764 sprintf (name, "%s.", base);
6765 clean_symbol_name (name);
6767 p = name + strlen (name);
6768 for (i = 0; i < 4; i++)
6770 sprintf (p, "%.2x", checksum[i]);
6771 p += 2;
6774 comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6775 comdat_symbol_number = 0;
6778 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P. */
6780 static int
6781 is_type_die (dw_die_ref die)
6783 switch (die->die_tag)
6785 case DW_TAG_array_type:
6786 case DW_TAG_class_type:
6787 case DW_TAG_interface_type:
6788 case DW_TAG_enumeration_type:
6789 case DW_TAG_pointer_type:
6790 case DW_TAG_reference_type:
6791 case DW_TAG_rvalue_reference_type:
6792 case DW_TAG_string_type:
6793 case DW_TAG_structure_type:
6794 case DW_TAG_subroutine_type:
6795 case DW_TAG_union_type:
6796 case DW_TAG_ptr_to_member_type:
6797 case DW_TAG_set_type:
6798 case DW_TAG_subrange_type:
6799 case DW_TAG_base_type:
6800 case DW_TAG_const_type:
6801 case DW_TAG_file_type:
6802 case DW_TAG_packed_type:
6803 case DW_TAG_volatile_type:
6804 case DW_TAG_typedef:
6805 return 1;
6806 default:
6807 return 0;
6811 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6812 Basically, we want to choose the bits that are likely to be shared between
6813 compilations (types) and leave out the bits that are specific to individual
6814 compilations (functions). */
6816 static int
6817 is_comdat_die (dw_die_ref c)
6819 /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6820 we do for stabs. The advantage is a greater likelihood of sharing between
6821 objects that don't include headers in the same order (and therefore would
6822 put the base types in a different comdat). jason 8/28/00 */
6824 if (c->die_tag == DW_TAG_base_type)
6825 return 0;
6827 if (c->die_tag == DW_TAG_pointer_type
6828 || c->die_tag == DW_TAG_reference_type
6829 || c->die_tag == DW_TAG_rvalue_reference_type
6830 || c->die_tag == DW_TAG_const_type
6831 || c->die_tag == DW_TAG_volatile_type)
6833 dw_die_ref t = get_AT_ref (c, DW_AT_type);
6835 return t ? is_comdat_die (t) : 0;
6838 return is_type_die (c);
6841 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6842 compilation unit. */
6844 static int
6845 is_symbol_die (dw_die_ref c)
6847 return (is_type_die (c)
6848 || is_declaration_die (c)
6849 || c->die_tag == DW_TAG_namespace
6850 || c->die_tag == DW_TAG_module);
6853 /* Returns true iff C is a compile-unit DIE. */
6855 static inline bool
6856 is_cu_die (dw_die_ref c)
6858 return c && c->die_tag == DW_TAG_compile_unit;
6861 /* Returns true iff C is a unit DIE of some sort. */
6863 static inline bool
6864 is_unit_die (dw_die_ref c)
6866 return c && (c->die_tag == DW_TAG_compile_unit
6867 || c->die_tag == DW_TAG_partial_unit
6868 || c->die_tag == DW_TAG_type_unit);
6871 /* Returns true iff C is a namespace DIE. */
6873 static inline bool
6874 is_namespace_die (dw_die_ref c)
6876 return c && c->die_tag == DW_TAG_namespace;
6879 /* Returns true iff C is a class or structure DIE. */
6881 static inline bool
6882 is_class_die (dw_die_ref c)
6884 return c && (c->die_tag == DW_TAG_class_type
6885 || c->die_tag == DW_TAG_structure_type);
6888 /* Return non-zero if this DIE is a template parameter. */
6890 static inline bool
6891 is_template_parameter (dw_die_ref die)
6893 switch (die->die_tag)
6895 case DW_TAG_template_type_param:
6896 case DW_TAG_template_value_param:
6897 case DW_TAG_GNU_template_template_param:
6898 case DW_TAG_GNU_template_parameter_pack:
6899 return true;
6900 default:
6901 return false;
6905 /* Return non-zero if this DIE represents a template instantiation. */
6907 static inline bool
6908 is_template_instantiation (dw_die_ref die)
6910 dw_die_ref c;
6912 if (!is_type_die (die) && die->die_tag != DW_TAG_subprogram)
6913 return false;
6914 FOR_EACH_CHILD (die, c, if (is_template_parameter (c)) return true);
6915 return false;
6918 static char *
6919 gen_internal_sym (const char *prefix)
6921 char buf[256];
6923 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6924 return xstrdup (buf);
6927 /* Assign symbols to all worthy DIEs under DIE. */
6929 static void
6930 assign_symbol_names (dw_die_ref die)
6932 dw_die_ref c;
6934 if (is_symbol_die (die) && !die->comdat_type_p)
6936 if (comdat_symbol_id)
6938 char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6940 sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6941 comdat_symbol_id, comdat_symbol_number++);
6942 die->die_id.die_symbol = xstrdup (p);
6944 else
6945 die->die_id.die_symbol = gen_internal_sym ("LDIE");
6948 FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6951 struct cu_hash_table_entry
6953 dw_die_ref cu;
6954 unsigned min_comdat_num, max_comdat_num;
6955 struct cu_hash_table_entry *next;
6958 /* Helpers to manipulate hash table of CUs. */
6960 struct cu_hash_table_entry_hasher : pointer_hash <cu_hash_table_entry>
6962 typedef die_struct *compare_type;
6963 static inline hashval_t hash (const cu_hash_table_entry *);
6964 static inline bool equal (const cu_hash_table_entry *, const die_struct *);
6965 static inline void remove (cu_hash_table_entry *);
6968 inline hashval_t
6969 cu_hash_table_entry_hasher::hash (const cu_hash_table_entry *entry)
6971 return htab_hash_string (entry->cu->die_id.die_symbol);
6974 inline bool
6975 cu_hash_table_entry_hasher::equal (const cu_hash_table_entry *entry1,
6976 const die_struct *entry2)
6978 return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6981 inline void
6982 cu_hash_table_entry_hasher::remove (cu_hash_table_entry *entry)
6984 struct cu_hash_table_entry *next;
6986 while (entry)
6988 next = entry->next;
6989 free (entry);
6990 entry = next;
6994 typedef hash_table<cu_hash_table_entry_hasher> cu_hash_type;
6996 /* Check whether we have already seen this CU and set up SYM_NUM
6997 accordingly. */
6998 static int
6999 check_duplicate_cu (dw_die_ref cu, cu_hash_type *htable, unsigned int *sym_num)
7001 struct cu_hash_table_entry dummy;
7002 struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7004 dummy.max_comdat_num = 0;
7006 slot = htable->find_slot_with_hash (cu,
7007 htab_hash_string (cu->die_id.die_symbol),
7008 INSERT);
7009 entry = *slot;
7011 for (; entry; last = entry, entry = entry->next)
7013 if (same_die_p_wrap (cu, entry->cu))
7014 break;
7017 if (entry)
7019 *sym_num = entry->min_comdat_num;
7020 return 1;
7023 entry = XCNEW (struct cu_hash_table_entry);
7024 entry->cu = cu;
7025 entry->min_comdat_num = *sym_num = last->max_comdat_num;
7026 entry->next = *slot;
7027 *slot = entry;
7029 return 0;
7032 /* Record SYM_NUM to record of CU in HTABLE. */
7033 static void
7034 record_comdat_symbol_number (dw_die_ref cu, cu_hash_type *htable,
7035 unsigned int sym_num)
7037 struct cu_hash_table_entry **slot, *entry;
7039 slot = htable->find_slot_with_hash (cu,
7040 htab_hash_string (cu->die_id.die_symbol),
7041 NO_INSERT);
7042 entry = *slot;
7044 entry->max_comdat_num = sym_num;
7047 /* Traverse the DIE (which is always comp_unit_die), and set up
7048 additional compilation units for each of the include files we see
7049 bracketed by BINCL/EINCL. */
7051 static void
7052 break_out_includes (dw_die_ref die)
7054 dw_die_ref c;
7055 dw_die_ref unit = NULL;
7056 limbo_die_node *node, **pnode;
7058 c = die->die_child;
7059 if (c) do {
7060 dw_die_ref prev = c;
7061 c = c->die_sib;
7062 while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7063 || (unit && is_comdat_die (c)))
7065 dw_die_ref next = c->die_sib;
7067 /* This DIE is for a secondary CU; remove it from the main one. */
7068 remove_child_with_prev (c, prev);
7070 if (c->die_tag == DW_TAG_GNU_BINCL)
7071 unit = push_new_compile_unit (unit, c);
7072 else if (c->die_tag == DW_TAG_GNU_EINCL)
7073 unit = pop_compile_unit (unit);
7074 else
7075 add_child_die (unit, c);
7076 c = next;
7077 if (c == die->die_child)
7078 break;
7080 } while (c != die->die_child);
7082 #if 0
7083 /* We can only use this in debugging, since the frontend doesn't check
7084 to make sure that we leave every include file we enter. */
7085 gcc_assert (!unit);
7086 #endif
7088 assign_symbol_names (die);
7089 cu_hash_type cu_hash_table (10);
7090 for (node = limbo_die_list, pnode = &limbo_die_list;
7091 node;
7092 node = node->next)
7094 int is_dupl;
7096 compute_section_prefix (node->die);
7097 is_dupl = check_duplicate_cu (node->die, &cu_hash_table,
7098 &comdat_symbol_number);
7099 assign_symbol_names (node->die);
7100 if (is_dupl)
7101 *pnode = node->next;
7102 else
7104 pnode = &node->next;
7105 record_comdat_symbol_number (node->die, &cu_hash_table,
7106 comdat_symbol_number);
7111 /* Return non-zero if this DIE is a declaration. */
7113 static int
7114 is_declaration_die (dw_die_ref die)
7116 dw_attr_ref a;
7117 unsigned ix;
7119 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7120 if (a->dw_attr == DW_AT_declaration)
7121 return 1;
7123 return 0;
7126 /* Return non-zero if this DIE is nested inside a subprogram. */
7128 static int
7129 is_nested_in_subprogram (dw_die_ref die)
7131 dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7133 if (decl == NULL)
7134 decl = die;
7135 return local_scope_p (decl);
7138 /* Return non-zero if this DIE contains a defining declaration of a
7139 subprogram. */
7141 static int
7142 contains_subprogram_definition (dw_die_ref die)
7144 dw_die_ref c;
7146 if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7147 return 1;
7148 FOR_EACH_CHILD (die, c, if (contains_subprogram_definition (c)) return 1);
7149 return 0;
7152 /* Return non-zero if this is a type DIE that should be moved to a
7153 COMDAT .debug_types section. */
7155 static int
7156 should_move_die_to_comdat (dw_die_ref die)
7158 switch (die->die_tag)
7160 case DW_TAG_class_type:
7161 case DW_TAG_structure_type:
7162 case DW_TAG_enumeration_type:
7163 case DW_TAG_union_type:
7164 /* Don't move declarations, inlined instances, types nested in a
7165 subprogram, or types that contain subprogram definitions. */
7166 if (is_declaration_die (die)
7167 || get_AT (die, DW_AT_abstract_origin)
7168 || is_nested_in_subprogram (die)
7169 || contains_subprogram_definition (die))
7170 return 0;
7171 return 1;
7172 case DW_TAG_array_type:
7173 case DW_TAG_interface_type:
7174 case DW_TAG_pointer_type:
7175 case DW_TAG_reference_type:
7176 case DW_TAG_rvalue_reference_type:
7177 case DW_TAG_string_type:
7178 case DW_TAG_subroutine_type:
7179 case DW_TAG_ptr_to_member_type:
7180 case DW_TAG_set_type:
7181 case DW_TAG_subrange_type:
7182 case DW_TAG_base_type:
7183 case DW_TAG_const_type:
7184 case DW_TAG_file_type:
7185 case DW_TAG_packed_type:
7186 case DW_TAG_volatile_type:
7187 case DW_TAG_typedef:
7188 default:
7189 return 0;
7193 /* Make a clone of DIE. */
7195 static dw_die_ref
7196 clone_die (dw_die_ref die)
7198 dw_die_ref clone;
7199 dw_attr_ref a;
7200 unsigned ix;
7202 clone = ggc_cleared_alloc<die_node> ();
7203 clone->die_tag = die->die_tag;
7205 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7206 add_dwarf_attr (clone, a);
7208 return clone;
7211 /* Make a clone of the tree rooted at DIE. */
7213 static dw_die_ref
7214 clone_tree (dw_die_ref die)
7216 dw_die_ref c;
7217 dw_die_ref clone = clone_die (die);
7219 FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree (c)));
7221 return clone;
7224 /* Make a clone of DIE as a declaration. */
7226 static dw_die_ref
7227 clone_as_declaration (dw_die_ref die)
7229 dw_die_ref clone;
7230 dw_die_ref decl;
7231 dw_attr_ref a;
7232 unsigned ix;
7234 /* If the DIE is already a declaration, just clone it. */
7235 if (is_declaration_die (die))
7236 return clone_die (die);
7238 /* If the DIE is a specification, just clone its declaration DIE. */
7239 decl = get_AT_ref (die, DW_AT_specification);
7240 if (decl != NULL)
7242 clone = clone_die (decl);
7243 if (die->comdat_type_p)
7244 add_AT_die_ref (clone, DW_AT_signature, die);
7245 return clone;
7248 clone = ggc_cleared_alloc<die_node> ();
7249 clone->die_tag = die->die_tag;
7251 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7253 /* We don't want to copy over all attributes.
7254 For example we don't want DW_AT_byte_size because otherwise we will no
7255 longer have a declaration and GDB will treat it as a definition. */
7257 switch (a->dw_attr)
7259 case DW_AT_abstract_origin:
7260 case DW_AT_artificial:
7261 case DW_AT_containing_type:
7262 case DW_AT_external:
7263 case DW_AT_name:
7264 case DW_AT_type:
7265 case DW_AT_virtuality:
7266 case DW_AT_linkage_name:
7267 case DW_AT_MIPS_linkage_name:
7268 add_dwarf_attr (clone, a);
7269 break;
7270 case DW_AT_byte_size:
7271 default:
7272 break;
7276 if (die->comdat_type_p)
7277 add_AT_die_ref (clone, DW_AT_signature, die);
7279 add_AT_flag (clone, DW_AT_declaration, 1);
7280 return clone;
7284 /* Structure to map a DIE in one CU to its copy in a comdat type unit. */
7286 struct decl_table_entry
7288 dw_die_ref orig;
7289 dw_die_ref copy;
7292 /* Helpers to manipulate hash table of copied declarations. */
7294 /* Hashtable helpers. */
7296 struct decl_table_entry_hasher : free_ptr_hash <decl_table_entry>
7298 typedef die_struct *compare_type;
7299 static inline hashval_t hash (const decl_table_entry *);
7300 static inline bool equal (const decl_table_entry *, const die_struct *);
7303 inline hashval_t
7304 decl_table_entry_hasher::hash (const decl_table_entry *entry)
7306 return htab_hash_pointer (entry->orig);
7309 inline bool
7310 decl_table_entry_hasher::equal (const decl_table_entry *entry1,
7311 const die_struct *entry2)
7313 return entry1->orig == entry2;
7316 typedef hash_table<decl_table_entry_hasher> decl_hash_type;
7318 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7319 or type unit entry, to a new tree. Adds the new tree to UNIT and returns
7320 a pointer to the copy of DIE. If DECL_TABLE is provided, it is used
7321 to check if the ancestor has already been copied into UNIT. */
7323 static dw_die_ref
7324 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die,
7325 decl_hash_type *decl_table)
7327 dw_die_ref parent = die->die_parent;
7328 dw_die_ref new_parent = unit;
7329 dw_die_ref copy;
7330 decl_table_entry **slot = NULL;
7331 struct decl_table_entry *entry = NULL;
7333 if (decl_table)
7335 /* Check if the entry has already been copied to UNIT. */
7336 slot = decl_table->find_slot_with_hash (die, htab_hash_pointer (die),
7337 INSERT);
7338 if (*slot != HTAB_EMPTY_ENTRY)
7340 entry = *slot;
7341 return entry->copy;
7344 /* Record in DECL_TABLE that DIE has been copied to UNIT. */
7345 entry = XCNEW (struct decl_table_entry);
7346 entry->orig = die;
7347 entry->copy = NULL;
7348 *slot = entry;
7351 if (parent != NULL)
7353 dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7354 if (spec != NULL)
7355 parent = spec;
7356 if (!is_unit_die (parent))
7357 new_parent = copy_ancestor_tree (unit, parent, decl_table);
7360 copy = clone_as_declaration (die);
7361 add_child_die (new_parent, copy);
7363 if (decl_table)
7365 /* Record the pointer to the copy. */
7366 entry->copy = copy;
7369 return copy;
7371 /* Copy the declaration context to the new type unit DIE. This includes
7372 any surrounding namespace or type declarations. If the DIE has an
7373 AT_specification attribute, it also includes attributes and children
7374 attached to the specification, and returns a pointer to the original
7375 parent of the declaration DIE. Returns NULL otherwise. */
7377 static dw_die_ref
7378 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7380 dw_die_ref decl;
7381 dw_die_ref new_decl;
7382 dw_die_ref orig_parent = NULL;
7384 decl = get_AT_ref (die, DW_AT_specification);
7385 if (decl == NULL)
7386 decl = die;
7387 else
7389 unsigned ix;
7390 dw_die_ref c;
7391 dw_attr_ref a;
7393 /* The original DIE will be changed to a declaration, and must
7394 be moved to be a child of the original declaration DIE. */
7395 orig_parent = decl->die_parent;
7397 /* Copy the type node pointer from the new DIE to the original
7398 declaration DIE so we can forward references later. */
7399 decl->comdat_type_p = true;
7400 decl->die_id.die_type_node = die->die_id.die_type_node;
7402 remove_AT (die, DW_AT_specification);
7404 FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
7406 if (a->dw_attr != DW_AT_name
7407 && a->dw_attr != DW_AT_declaration
7408 && a->dw_attr != DW_AT_external)
7409 add_dwarf_attr (die, a);
7412 FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree (c)));
7415 if (decl->die_parent != NULL
7416 && !is_unit_die (decl->die_parent))
7418 new_decl = copy_ancestor_tree (unit, decl, NULL);
7419 if (new_decl != NULL)
7421 remove_AT (new_decl, DW_AT_signature);
7422 add_AT_specification (die, new_decl);
7426 return orig_parent;
7429 /* Generate the skeleton ancestor tree for the given NODE, then clone
7430 the DIE and add the clone into the tree. */
7432 static void
7433 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7435 if (node->new_die != NULL)
7436 return;
7438 node->new_die = clone_as_declaration (node->old_die);
7440 if (node->parent != NULL)
7442 generate_skeleton_ancestor_tree (node->parent);
7443 add_child_die (node->parent->new_die, node->new_die);
7447 /* Generate a skeleton tree of DIEs containing any declarations that are
7448 found in the original tree. We traverse the tree looking for declaration
7449 DIEs, and construct the skeleton from the bottom up whenever we find one. */
7451 static void
7452 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7454 skeleton_chain_node node;
7455 dw_die_ref c;
7456 dw_die_ref first;
7457 dw_die_ref prev = NULL;
7458 dw_die_ref next = NULL;
7460 node.parent = parent;
7462 first = c = parent->old_die->die_child;
7463 if (c)
7464 next = c->die_sib;
7465 if (c) do {
7466 if (prev == NULL || prev->die_sib == c)
7467 prev = c;
7468 c = next;
7469 next = (c == first ? NULL : c->die_sib);
7470 node.old_die = c;
7471 node.new_die = NULL;
7472 if (is_declaration_die (c))
7474 if (is_template_instantiation (c))
7476 /* Instantiated templates do not need to be cloned into the
7477 type unit. Just move the DIE and its children back to
7478 the skeleton tree (in the main CU). */
7479 remove_child_with_prev (c, prev);
7480 add_child_die (parent->new_die, c);
7481 c = prev;
7483 else
7485 /* Clone the existing DIE, move the original to the skeleton
7486 tree (which is in the main CU), and put the clone, with
7487 all the original's children, where the original came from
7488 (which is about to be moved to the type unit). */
7489 dw_die_ref clone = clone_die (c);
7490 move_all_children (c, clone);
7492 /* If the original has a DW_AT_object_pointer attribute,
7493 it would now point to a child DIE just moved to the
7494 cloned tree, so we need to remove that attribute from
7495 the original. */
7496 remove_AT (c, DW_AT_object_pointer);
7498 replace_child (c, clone, prev);
7499 generate_skeleton_ancestor_tree (parent);
7500 add_child_die (parent->new_die, c);
7501 node.new_die = c;
7502 c = clone;
7505 generate_skeleton_bottom_up (&node);
7506 } while (next != NULL);
7509 /* Wrapper function for generate_skeleton_bottom_up. */
7511 static dw_die_ref
7512 generate_skeleton (dw_die_ref die)
7514 skeleton_chain_node node;
7516 node.old_die = die;
7517 node.new_die = NULL;
7518 node.parent = NULL;
7520 /* If this type definition is nested inside another type,
7521 and is not an instantiation of a template, always leave
7522 at least a declaration in its place. */
7523 if (die->die_parent != NULL
7524 && is_type_die (die->die_parent)
7525 && !is_template_instantiation (die))
7526 node.new_die = clone_as_declaration (die);
7528 generate_skeleton_bottom_up (&node);
7529 return node.new_die;
7532 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
7533 declaration. The original DIE is moved to a new compile unit so that
7534 existing references to it follow it to the new location. If any of the
7535 original DIE's descendants is a declaration, we need to replace the
7536 original DIE with a skeleton tree and move the declarations back into the
7537 skeleton tree. */
7539 static dw_die_ref
7540 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
7541 dw_die_ref prev)
7543 dw_die_ref skeleton, orig_parent;
7545 /* Copy the declaration context to the type unit DIE. If the returned
7546 ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
7547 that DIE. */
7548 orig_parent = copy_declaration_context (unit, child);
7550 skeleton = generate_skeleton (child);
7551 if (skeleton == NULL)
7552 remove_child_with_prev (child, prev);
7553 else
7555 skeleton->comdat_type_p = true;
7556 skeleton->die_id.die_type_node = child->die_id.die_type_node;
7558 /* If the original DIE was a specification, we need to put
7559 the skeleton under the parent DIE of the declaration.
7560 This leaves the original declaration in the tree, but
7561 it will be pruned later since there are no longer any
7562 references to it. */
7563 if (orig_parent != NULL)
7565 remove_child_with_prev (child, prev);
7566 add_child_die (orig_parent, skeleton);
7568 else
7569 replace_child (child, skeleton, prev);
7572 return skeleton;
7575 /* Traverse the DIE and set up additional .debug_types sections for each
7576 type worthy of being placed in a COMDAT section. */
7578 static void
7579 break_out_comdat_types (dw_die_ref die)
7581 dw_die_ref c;
7582 dw_die_ref first;
7583 dw_die_ref prev = NULL;
7584 dw_die_ref next = NULL;
7585 dw_die_ref unit = NULL;
7587 first = c = die->die_child;
7588 if (c)
7589 next = c->die_sib;
7590 if (c) do {
7591 if (prev == NULL || prev->die_sib == c)
7592 prev = c;
7593 c = next;
7594 next = (c == first ? NULL : c->die_sib);
7595 if (should_move_die_to_comdat (c))
7597 dw_die_ref replacement;
7598 comdat_type_node_ref type_node;
7600 /* Break out nested types into their own type units. */
7601 break_out_comdat_types (c);
7603 /* Create a new type unit DIE as the root for the new tree, and
7604 add it to the list of comdat types. */
7605 unit = new_die (DW_TAG_type_unit, NULL, NULL);
7606 add_AT_unsigned (unit, DW_AT_language,
7607 get_AT_unsigned (comp_unit_die (), DW_AT_language));
7608 type_node = ggc_cleared_alloc<comdat_type_node> ();
7609 type_node->root_die = unit;
7610 type_node->next = comdat_type_list;
7611 comdat_type_list = type_node;
7613 /* Generate the type signature. */
7614 generate_type_signature (c, type_node);
7616 /* Copy the declaration context, attributes, and children of the
7617 declaration into the new type unit DIE, then remove this DIE
7618 from the main CU (or replace it with a skeleton if necessary). */
7619 replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7620 type_node->skeleton_die = replacement;
7622 /* Add the DIE to the new compunit. */
7623 add_child_die (unit, c);
7625 if (replacement != NULL)
7626 c = replacement;
7628 else if (c->die_tag == DW_TAG_namespace
7629 || c->die_tag == DW_TAG_class_type
7630 || c->die_tag == DW_TAG_structure_type
7631 || c->die_tag == DW_TAG_union_type)
7633 /* Look for nested types that can be broken out. */
7634 break_out_comdat_types (c);
7636 } while (next != NULL);
7639 /* Like clone_tree, but copy DW_TAG_subprogram DIEs as declarations.
7640 Enter all the cloned children into the hash table decl_table. */
7642 static dw_die_ref
7643 clone_tree_partial (dw_die_ref die, decl_hash_type *decl_table)
7645 dw_die_ref c;
7646 dw_die_ref clone;
7647 struct decl_table_entry *entry;
7648 decl_table_entry **slot;
7650 if (die->die_tag == DW_TAG_subprogram)
7651 clone = clone_as_declaration (die);
7652 else
7653 clone = clone_die (die);
7655 slot = decl_table->find_slot_with_hash (die,
7656 htab_hash_pointer (die), INSERT);
7658 /* Assert that DIE isn't in the hash table yet. If it would be there
7659 before, the ancestors would be necessarily there as well, therefore
7660 clone_tree_partial wouldn't be called. */
7661 gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7663 entry = XCNEW (struct decl_table_entry);
7664 entry->orig = die;
7665 entry->copy = clone;
7666 *slot = entry;
7668 if (die->die_tag != DW_TAG_subprogram)
7669 FOR_EACH_CHILD (die, c,
7670 add_child_die (clone, clone_tree_partial (c, decl_table)));
7672 return clone;
7675 /* Walk the DIE and its children, looking for references to incomplete
7676 or trivial types that are unmarked (i.e., that are not in the current
7677 type_unit). */
7679 static void
7680 copy_decls_walk (dw_die_ref unit, dw_die_ref die, decl_hash_type *decl_table)
7682 dw_die_ref c;
7683 dw_attr_ref a;
7684 unsigned ix;
7686 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7688 if (AT_class (a) == dw_val_class_die_ref)
7690 dw_die_ref targ = AT_ref (a);
7691 decl_table_entry **slot;
7692 struct decl_table_entry *entry;
7694 if (targ->die_mark != 0 || targ->comdat_type_p)
7695 continue;
7697 slot = decl_table->find_slot_with_hash (targ,
7698 htab_hash_pointer (targ),
7699 INSERT);
7701 if (*slot != HTAB_EMPTY_ENTRY)
7703 /* TARG has already been copied, so we just need to
7704 modify the reference to point to the copy. */
7705 entry = *slot;
7706 a->dw_attr_val.v.val_die_ref.die = entry->copy;
7708 else
7710 dw_die_ref parent = unit;
7711 dw_die_ref copy = clone_die (targ);
7713 /* Record in DECL_TABLE that TARG has been copied.
7714 Need to do this now, before the recursive call,
7715 because DECL_TABLE may be expanded and SLOT
7716 would no longer be a valid pointer. */
7717 entry = XCNEW (struct decl_table_entry);
7718 entry->orig = targ;
7719 entry->copy = copy;
7720 *slot = entry;
7722 /* If TARG is not a declaration DIE, we need to copy its
7723 children. */
7724 if (!is_declaration_die (targ))
7726 FOR_EACH_CHILD (
7727 targ, c,
7728 add_child_die (copy,
7729 clone_tree_partial (c, decl_table)));
7732 /* Make sure the cloned tree is marked as part of the
7733 type unit. */
7734 mark_dies (copy);
7736 /* If TARG has surrounding context, copy its ancestor tree
7737 into the new type unit. */
7738 if (targ->die_parent != NULL
7739 && !is_unit_die (targ->die_parent))
7740 parent = copy_ancestor_tree (unit, targ->die_parent,
7741 decl_table);
7743 add_child_die (parent, copy);
7744 a->dw_attr_val.v.val_die_ref.die = copy;
7746 /* Make sure the newly-copied DIE is walked. If it was
7747 installed in a previously-added context, it won't
7748 get visited otherwise. */
7749 if (parent != unit)
7751 /* Find the highest point of the newly-added tree,
7752 mark each node along the way, and walk from there. */
7753 parent->die_mark = 1;
7754 while (parent->die_parent
7755 && parent->die_parent->die_mark == 0)
7757 parent = parent->die_parent;
7758 parent->die_mark = 1;
7760 copy_decls_walk (unit, parent, decl_table);
7766 FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7769 /* Copy declarations for "unworthy" types into the new comdat section.
7770 Incomplete types, modified types, and certain other types aren't broken
7771 out into comdat sections of their own, so they don't have a signature,
7772 and we need to copy the declaration into the same section so that we
7773 don't have an external reference. */
7775 static void
7776 copy_decls_for_unworthy_types (dw_die_ref unit)
7778 mark_dies (unit);
7779 decl_hash_type decl_table (10);
7780 copy_decls_walk (unit, unit, &decl_table);
7781 unmark_dies (unit);
7784 /* Traverse the DIE and add a sibling attribute if it may have the
7785 effect of speeding up access to siblings. To save some space,
7786 avoid generating sibling attributes for DIE's without children. */
7788 static void
7789 add_sibling_attributes (dw_die_ref die)
7791 dw_die_ref c;
7793 if (! die->die_child)
7794 return;
7796 if (die->die_parent && die != die->die_parent->die_child)
7797 add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7799 FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7802 /* Output all location lists for the DIE and its children. */
7804 static void
7805 output_location_lists (dw_die_ref die)
7807 dw_die_ref c;
7808 dw_attr_ref a;
7809 unsigned ix;
7811 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7812 if (AT_class (a) == dw_val_class_loc_list)
7813 output_loc_list (AT_loc_list (a));
7815 FOR_EACH_CHILD (die, c, output_location_lists (c));
7818 /* We want to limit the number of external references, because they are
7819 larger than local references: a relocation takes multiple words, and
7820 even a sig8 reference is always eight bytes, whereas a local reference
7821 can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7822 So if we encounter multiple external references to the same type DIE, we
7823 make a local typedef stub for it and redirect all references there.
7825 This is the element of the hash table for keeping track of these
7826 references. */
7828 struct external_ref
7830 dw_die_ref type;
7831 dw_die_ref stub;
7832 unsigned n_refs;
7835 /* Hashtable helpers. */
7837 struct external_ref_hasher : free_ptr_hash <external_ref>
7839 static inline hashval_t hash (const external_ref *);
7840 static inline bool equal (const external_ref *, const external_ref *);
7843 inline hashval_t
7844 external_ref_hasher::hash (const external_ref *r)
7846 dw_die_ref die = r->type;
7847 hashval_t h = 0;
7849 /* We can't use the address of the DIE for hashing, because
7850 that will make the order of the stub DIEs non-deterministic. */
7851 if (! die->comdat_type_p)
7852 /* We have a symbol; use it to compute a hash. */
7853 h = htab_hash_string (die->die_id.die_symbol);
7854 else
7856 /* We have a type signature; use a subset of the bits as the hash.
7857 The 8-byte signature is at least as large as hashval_t. */
7858 comdat_type_node_ref type_node = die->die_id.die_type_node;
7859 memcpy (&h, type_node->signature, sizeof (h));
7861 return h;
7864 inline bool
7865 external_ref_hasher::equal (const external_ref *r1, const external_ref *r2)
7867 return r1->type == r2->type;
7870 typedef hash_table<external_ref_hasher> external_ref_hash_type;
7872 /* Return a pointer to the external_ref for references to DIE. */
7874 static struct external_ref *
7875 lookup_external_ref (external_ref_hash_type *map, dw_die_ref die)
7877 struct external_ref ref, *ref_p;
7878 external_ref **slot;
7880 ref.type = die;
7881 slot = map->find_slot (&ref, INSERT);
7882 if (*slot != HTAB_EMPTY_ENTRY)
7883 return *slot;
7885 ref_p = XCNEW (struct external_ref);
7886 ref_p->type = die;
7887 *slot = ref_p;
7888 return ref_p;
7891 /* Subroutine of optimize_external_refs, below.
7893 If we see a type skeleton, record it as our stub. If we see external
7894 references, remember how many we've seen. */
7896 static void
7897 optimize_external_refs_1 (dw_die_ref die, external_ref_hash_type *map)
7899 dw_die_ref c;
7900 dw_attr_ref a;
7901 unsigned ix;
7902 struct external_ref *ref_p;
7904 if (is_type_die (die)
7905 && (c = get_AT_ref (die, DW_AT_signature)))
7907 /* This is a local skeleton; use it for local references. */
7908 ref_p = lookup_external_ref (map, c);
7909 ref_p->stub = die;
7912 /* Scan the DIE references, and remember any that refer to DIEs from
7913 other CUs (i.e. those which are not marked). */
7914 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7915 if (AT_class (a) == dw_val_class_die_ref
7916 && (c = AT_ref (a))->die_mark == 0
7917 && is_type_die (c))
7919 ref_p = lookup_external_ref (map, c);
7920 ref_p->n_refs++;
7923 FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7926 /* htab_traverse callback function for optimize_external_refs, below. SLOT
7927 points to an external_ref, DATA is the CU we're processing. If we don't
7928 already have a local stub, and we have multiple refs, build a stub. */
7931 dwarf2_build_local_stub (external_ref **slot, dw_die_ref data)
7933 struct external_ref *ref_p = *slot;
7935 if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7937 /* We have multiple references to this type, so build a small stub.
7938 Both of these forms are a bit dodgy from the perspective of the
7939 DWARF standard, since technically they should have names. */
7940 dw_die_ref cu = data;
7941 dw_die_ref type = ref_p->type;
7942 dw_die_ref stub = NULL;
7944 if (type->comdat_type_p)
7946 /* If we refer to this type via sig8, use AT_signature. */
7947 stub = new_die (type->die_tag, cu, NULL_TREE);
7948 add_AT_die_ref (stub, DW_AT_signature, type);
7950 else
7952 /* Otherwise, use a typedef with no name. */
7953 stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7954 add_AT_die_ref (stub, DW_AT_type, type);
7957 stub->die_mark++;
7958 ref_p->stub = stub;
7960 return 1;
7963 /* DIE is a unit; look through all the DIE references to see if there are
7964 any external references to types, and if so, create local stubs for
7965 them which will be applied in build_abbrev_table. This is useful because
7966 references to local DIEs are smaller. */
7968 static external_ref_hash_type *
7969 optimize_external_refs (dw_die_ref die)
7971 external_ref_hash_type *map = new external_ref_hash_type (10);
7972 optimize_external_refs_1 (die, map);
7973 map->traverse <dw_die_ref, dwarf2_build_local_stub> (die);
7974 return map;
7977 /* The format of each DIE (and its attribute value pairs) is encoded in an
7978 abbreviation table. This routine builds the abbreviation table and assigns
7979 a unique abbreviation id for each abbreviation entry. The children of each
7980 die are visited recursively. */
7982 static void
7983 build_abbrev_table (dw_die_ref die, external_ref_hash_type *extern_map)
7985 unsigned long abbrev_id;
7986 unsigned int n_alloc;
7987 dw_die_ref c;
7988 dw_attr_ref a;
7989 unsigned ix;
7991 /* Scan the DIE references, and replace any that refer to
7992 DIEs from other CUs (i.e. those which are not marked) with
7993 the local stubs we built in optimize_external_refs. */
7994 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7995 if (AT_class (a) == dw_val_class_die_ref
7996 && (c = AT_ref (a))->die_mark == 0)
7998 struct external_ref *ref_p;
7999 gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
8001 ref_p = lookup_external_ref (extern_map, c);
8002 if (ref_p->stub && ref_p->stub != die)
8003 change_AT_die_ref (a, ref_p->stub);
8004 else
8005 /* We aren't changing this reference, so mark it external. */
8006 set_AT_ref_external (a, 1);
8009 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8011 dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8012 dw_attr_ref die_a, abbrev_a;
8013 unsigned ix;
8014 bool ok = true;
8016 if (abbrev->die_tag != die->die_tag)
8017 continue;
8018 if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8019 continue;
8021 if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
8022 continue;
8024 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
8026 abbrev_a = &(*abbrev->die_attr)[ix];
8027 if ((abbrev_a->dw_attr != die_a->dw_attr)
8028 || (value_format (abbrev_a) != value_format (die_a)))
8030 ok = false;
8031 break;
8034 if (ok)
8035 break;
8038 if (abbrev_id >= abbrev_die_table_in_use)
8040 if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8042 n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8043 abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8044 n_alloc);
8046 memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8047 (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8048 abbrev_die_table_allocated = n_alloc;
8051 ++abbrev_die_table_in_use;
8052 abbrev_die_table[abbrev_id] = die;
8055 die->die_abbrev = abbrev_id;
8056 FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
8059 /* Return the power-of-two number of bytes necessary to represent VALUE. */
8061 static int
8062 constant_size (unsigned HOST_WIDE_INT value)
8064 int log;
8066 if (value == 0)
8067 log = 0;
8068 else
8069 log = floor_log2 (value);
8071 log = log / 8;
8072 log = 1 << (floor_log2 (log) + 1);
8074 return log;
8077 /* Return the size of a DIE as it is represented in the
8078 .debug_info section. */
8080 static unsigned long
8081 size_of_die (dw_die_ref die)
8083 unsigned long size = 0;
8084 dw_attr_ref a;
8085 unsigned ix;
8086 enum dwarf_form form;
8088 size += size_of_uleb128 (die->die_abbrev);
8089 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8091 switch (AT_class (a))
8093 case dw_val_class_addr:
8094 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8096 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8097 size += size_of_uleb128 (AT_index (a));
8099 else
8100 size += DWARF2_ADDR_SIZE;
8101 break;
8102 case dw_val_class_offset:
8103 size += DWARF_OFFSET_SIZE;
8104 break;
8105 case dw_val_class_loc:
8107 unsigned long lsize = size_of_locs (AT_loc (a));
8109 /* Block length. */
8110 if (dwarf_version >= 4)
8111 size += size_of_uleb128 (lsize);
8112 else
8113 size += constant_size (lsize);
8114 size += lsize;
8116 break;
8117 case dw_val_class_loc_list:
8118 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8120 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8121 size += size_of_uleb128 (AT_index (a));
8123 else
8124 size += DWARF_OFFSET_SIZE;
8125 break;
8126 case dw_val_class_range_list:
8127 size += DWARF_OFFSET_SIZE;
8128 break;
8129 case dw_val_class_const:
8130 size += size_of_sleb128 (AT_int (a));
8131 break;
8132 case dw_val_class_unsigned_const:
8134 int csize = constant_size (AT_unsigned (a));
8135 if (dwarf_version == 3
8136 && a->dw_attr == DW_AT_data_member_location
8137 && csize >= 4)
8138 size += size_of_uleb128 (AT_unsigned (a));
8139 else
8140 size += csize;
8142 break;
8143 case dw_val_class_const_double:
8144 size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
8145 if (HOST_BITS_PER_WIDE_INT >= 64)
8146 size++; /* block */
8147 break;
8148 case dw_val_class_wide_int:
8149 size += (get_full_len (*a->dw_attr_val.v.val_wide)
8150 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
8151 if (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT
8152 > 64)
8153 size++; /* block */
8154 break;
8155 case dw_val_class_vec:
8156 size += constant_size (a->dw_attr_val.v.val_vec.length
8157 * a->dw_attr_val.v.val_vec.elt_size)
8158 + a->dw_attr_val.v.val_vec.length
8159 * a->dw_attr_val.v.val_vec.elt_size; /* block */
8160 break;
8161 case dw_val_class_flag:
8162 if (dwarf_version >= 4)
8163 /* Currently all add_AT_flag calls pass in 1 as last argument,
8164 so DW_FORM_flag_present can be used. If that ever changes,
8165 we'll need to use DW_FORM_flag and have some optimization
8166 in build_abbrev_table that will change those to
8167 DW_FORM_flag_present if it is set to 1 in all DIEs using
8168 the same abbrev entry. */
8169 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8170 else
8171 size += 1;
8172 break;
8173 case dw_val_class_die_ref:
8174 if (AT_ref_external (a))
8176 /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8177 we use DW_FORM_ref_addr. In DWARF2, DW_FORM_ref_addr
8178 is sized by target address length, whereas in DWARF3
8179 it's always sized as an offset. */
8180 if (use_debug_types)
8181 size += DWARF_TYPE_SIGNATURE_SIZE;
8182 else if (dwarf_version == 2)
8183 size += DWARF2_ADDR_SIZE;
8184 else
8185 size += DWARF_OFFSET_SIZE;
8187 else
8188 size += DWARF_OFFSET_SIZE;
8189 break;
8190 case dw_val_class_fde_ref:
8191 size += DWARF_OFFSET_SIZE;
8192 break;
8193 case dw_val_class_lbl_id:
8194 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8196 gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
8197 size += size_of_uleb128 (AT_index (a));
8199 else
8200 size += DWARF2_ADDR_SIZE;
8201 break;
8202 case dw_val_class_lineptr:
8203 case dw_val_class_macptr:
8204 size += DWARF_OFFSET_SIZE;
8205 break;
8206 case dw_val_class_str:
8207 form = AT_string_form (a);
8208 if (form == DW_FORM_strp)
8209 size += DWARF_OFFSET_SIZE;
8210 else if (form == DW_FORM_GNU_str_index)
8211 size += size_of_uleb128 (AT_index (a));
8212 else
8213 size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8214 break;
8215 case dw_val_class_file:
8216 size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8217 break;
8218 case dw_val_class_data8:
8219 size += 8;
8220 break;
8221 case dw_val_class_vms_delta:
8222 size += DWARF_OFFSET_SIZE;
8223 break;
8224 case dw_val_class_high_pc:
8225 size += DWARF2_ADDR_SIZE;
8226 break;
8227 default:
8228 gcc_unreachable ();
8232 return size;
8235 /* Size the debugging information associated with a given DIE. Visits the
8236 DIE's children recursively. Updates the global variable next_die_offset, on
8237 each time through. Uses the current value of next_die_offset to update the
8238 die_offset field in each DIE. */
8240 static void
8241 calc_die_sizes (dw_die_ref die)
8243 dw_die_ref c;
8245 gcc_assert (die->die_offset == 0
8246 || (unsigned long int) die->die_offset == next_die_offset);
8247 die->die_offset = next_die_offset;
8248 next_die_offset += size_of_die (die);
8250 FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8252 if (die->die_child != NULL)
8253 /* Count the null byte used to terminate sibling lists. */
8254 next_die_offset += 1;
8257 /* Size just the base type children at the start of the CU.
8258 This is needed because build_abbrev needs to size locs
8259 and sizing of type based stack ops needs to know die_offset
8260 values for the base types. */
8262 static void
8263 calc_base_type_die_sizes (void)
8265 unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8266 unsigned int i;
8267 dw_die_ref base_type;
8268 #if ENABLE_ASSERT_CHECKING
8269 dw_die_ref prev = comp_unit_die ()->die_child;
8270 #endif
8272 die_offset += size_of_die (comp_unit_die ());
8273 for (i = 0; base_types.iterate (i, &base_type); i++)
8275 #if ENABLE_ASSERT_CHECKING
8276 gcc_assert (base_type->die_offset == 0
8277 && prev->die_sib == base_type
8278 && base_type->die_child == NULL
8279 && base_type->die_abbrev);
8280 prev = base_type;
8281 #endif
8282 base_type->die_offset = die_offset;
8283 die_offset += size_of_die (base_type);
8287 /* Set the marks for a die and its children. We do this so
8288 that we know whether or not a reference needs to use FORM_ref_addr; only
8289 DIEs in the same CU will be marked. We used to clear out the offset
8290 and use that as the flag, but ran into ordering problems. */
8292 static void
8293 mark_dies (dw_die_ref die)
8295 dw_die_ref c;
8297 gcc_assert (!die->die_mark);
8299 die->die_mark = 1;
8300 FOR_EACH_CHILD (die, c, mark_dies (c));
8303 /* Clear the marks for a die and its children. */
8305 static void
8306 unmark_dies (dw_die_ref die)
8308 dw_die_ref c;
8310 if (! use_debug_types)
8311 gcc_assert (die->die_mark);
8313 die->die_mark = 0;
8314 FOR_EACH_CHILD (die, c, unmark_dies (c));
8317 /* Clear the marks for a die, its children and referred dies. */
8319 static void
8320 unmark_all_dies (dw_die_ref die)
8322 dw_die_ref c;
8323 dw_attr_ref a;
8324 unsigned ix;
8326 if (!die->die_mark)
8327 return;
8328 die->die_mark = 0;
8330 FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8332 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8333 if (AT_class (a) == dw_val_class_die_ref)
8334 unmark_all_dies (AT_ref (a));
8337 /* Calculate if the entry should appear in the final output file. It may be
8338 from a pruned a type. */
8340 static bool
8341 include_pubname_in_output (vec<pubname_entry, va_gc> *table, pubname_entry *p)
8343 /* By limiting gnu pubnames to definitions only, gold can generate a
8344 gdb index without entries for declarations, which don't include
8345 enough information to be useful. */
8346 if (debug_generate_pub_sections == 2 && is_declaration_die (p->die))
8347 return false;
8349 if (table == pubname_table)
8351 /* Enumerator names are part of the pubname table, but the
8352 parent DW_TAG_enumeration_type die may have been pruned.
8353 Don't output them if that is the case. */
8354 if (p->die->die_tag == DW_TAG_enumerator &&
8355 (p->die->die_parent == NULL
8356 || !p->die->die_parent->die_perennial_p))
8357 return false;
8359 /* Everything else in the pubname table is included. */
8360 return true;
8363 /* The pubtypes table shouldn't include types that have been
8364 pruned. */
8365 return (p->die->die_offset != 0
8366 || !flag_eliminate_unused_debug_types);
8369 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8370 generated for the compilation unit. */
8372 static unsigned long
8373 size_of_pubnames (vec<pubname_entry, va_gc> *names)
8375 unsigned long size;
8376 unsigned i;
8377 pubname_ref p;
8378 int space_for_flags = (debug_generate_pub_sections == 2) ? 1 : 0;
8380 size = DWARF_PUBNAMES_HEADER_SIZE;
8381 FOR_EACH_VEC_ELT (*names, i, p)
8382 if (include_pubname_in_output (names, p))
8383 size += strlen (p->name) + DWARF_OFFSET_SIZE + 1 + space_for_flags;
8385 size += DWARF_OFFSET_SIZE;
8386 return size;
8389 /* Return the size of the information in the .debug_aranges section. */
8391 static unsigned long
8392 size_of_aranges (void)
8394 unsigned long size;
8396 size = DWARF_ARANGES_HEADER_SIZE;
8398 /* Count the address/length pair for this compilation unit. */
8399 if (text_section_used)
8400 size += 2 * DWARF2_ADDR_SIZE;
8401 if (cold_text_section_used)
8402 size += 2 * DWARF2_ADDR_SIZE;
8403 if (have_multiple_function_sections)
8405 unsigned fde_idx;
8406 dw_fde_ref fde;
8408 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
8410 if (DECL_IGNORED_P (fde->decl))
8411 continue;
8412 if (!fde->in_std_section)
8413 size += 2 * DWARF2_ADDR_SIZE;
8414 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8415 size += 2 * DWARF2_ADDR_SIZE;
8419 /* Count the two zero words used to terminated the address range table. */
8420 size += 2 * DWARF2_ADDR_SIZE;
8421 return size;
8424 /* Select the encoding of an attribute value. */
8426 static enum dwarf_form
8427 value_format (dw_attr_ref a)
8429 switch (AT_class (a))
8431 case dw_val_class_addr:
8432 /* Only very few attributes allow DW_FORM_addr. */
8433 switch (a->dw_attr)
8435 case DW_AT_low_pc:
8436 case DW_AT_high_pc:
8437 case DW_AT_entry_pc:
8438 case DW_AT_trampoline:
8439 return (AT_index (a) == NOT_INDEXED
8440 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8441 default:
8442 break;
8444 switch (DWARF2_ADDR_SIZE)
8446 case 1:
8447 return DW_FORM_data1;
8448 case 2:
8449 return DW_FORM_data2;
8450 case 4:
8451 return DW_FORM_data4;
8452 case 8:
8453 return DW_FORM_data8;
8454 default:
8455 gcc_unreachable ();
8457 case dw_val_class_range_list:
8458 case dw_val_class_loc_list:
8459 if (dwarf_version >= 4)
8460 return DW_FORM_sec_offset;
8461 /* FALLTHRU */
8462 case dw_val_class_vms_delta:
8463 case dw_val_class_offset:
8464 switch (DWARF_OFFSET_SIZE)
8466 case 4:
8467 return DW_FORM_data4;
8468 case 8:
8469 return DW_FORM_data8;
8470 default:
8471 gcc_unreachable ();
8473 case dw_val_class_loc:
8474 if (dwarf_version >= 4)
8475 return DW_FORM_exprloc;
8476 switch (constant_size (size_of_locs (AT_loc (a))))
8478 case 1:
8479 return DW_FORM_block1;
8480 case 2:
8481 return DW_FORM_block2;
8482 case 4:
8483 return DW_FORM_block4;
8484 default:
8485 gcc_unreachable ();
8487 case dw_val_class_const:
8488 return DW_FORM_sdata;
8489 case dw_val_class_unsigned_const:
8490 switch (constant_size (AT_unsigned (a)))
8492 case 1:
8493 return DW_FORM_data1;
8494 case 2:
8495 return DW_FORM_data2;
8496 case 4:
8497 /* In DWARF3 DW_AT_data_member_location with
8498 DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
8499 constant, so we need to use DW_FORM_udata if we need
8500 a large constant. */
8501 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8502 return DW_FORM_udata;
8503 return DW_FORM_data4;
8504 case 8:
8505 if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
8506 return DW_FORM_udata;
8507 return DW_FORM_data8;
8508 default:
8509 gcc_unreachable ();
8511 case dw_val_class_const_double:
8512 switch (HOST_BITS_PER_WIDE_INT)
8514 case 8:
8515 return DW_FORM_data2;
8516 case 16:
8517 return DW_FORM_data4;
8518 case 32:
8519 return DW_FORM_data8;
8520 case 64:
8521 default:
8522 return DW_FORM_block1;
8524 case dw_val_class_wide_int:
8525 switch (get_full_len (*a->dw_attr_val.v.val_wide) * HOST_BITS_PER_WIDE_INT)
8527 case 8:
8528 return DW_FORM_data1;
8529 case 16:
8530 return DW_FORM_data2;
8531 case 32:
8532 return DW_FORM_data4;
8533 case 64:
8534 return DW_FORM_data8;
8535 default:
8536 return DW_FORM_block1;
8538 case dw_val_class_vec:
8539 switch (constant_size (a->dw_attr_val.v.val_vec.length
8540 * a->dw_attr_val.v.val_vec.elt_size))
8542 case 1:
8543 return DW_FORM_block1;
8544 case 2:
8545 return DW_FORM_block2;
8546 case 4:
8547 return DW_FORM_block4;
8548 default:
8549 gcc_unreachable ();
8551 case dw_val_class_flag:
8552 if (dwarf_version >= 4)
8554 /* Currently all add_AT_flag calls pass in 1 as last argument,
8555 so DW_FORM_flag_present can be used. If that ever changes,
8556 we'll need to use DW_FORM_flag and have some optimization
8557 in build_abbrev_table that will change those to
8558 DW_FORM_flag_present if it is set to 1 in all DIEs using
8559 the same abbrev entry. */
8560 gcc_assert (a->dw_attr_val.v.val_flag == 1);
8561 return DW_FORM_flag_present;
8563 return DW_FORM_flag;
8564 case dw_val_class_die_ref:
8565 if (AT_ref_external (a))
8566 return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8567 else
8568 return DW_FORM_ref;
8569 case dw_val_class_fde_ref:
8570 return DW_FORM_data;
8571 case dw_val_class_lbl_id:
8572 return (AT_index (a) == NOT_INDEXED
8573 ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8574 case dw_val_class_lineptr:
8575 case dw_val_class_macptr:
8576 return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8577 case dw_val_class_str:
8578 return AT_string_form (a);
8579 case dw_val_class_file:
8580 switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8582 case 1:
8583 return DW_FORM_data1;
8584 case 2:
8585 return DW_FORM_data2;
8586 case 4:
8587 return DW_FORM_data4;
8588 default:
8589 gcc_unreachable ();
8592 case dw_val_class_data8:
8593 return DW_FORM_data8;
8595 case dw_val_class_high_pc:
8596 switch (DWARF2_ADDR_SIZE)
8598 case 1:
8599 return DW_FORM_data1;
8600 case 2:
8601 return DW_FORM_data2;
8602 case 4:
8603 return DW_FORM_data4;
8604 case 8:
8605 return DW_FORM_data8;
8606 default:
8607 gcc_unreachable ();
8610 default:
8611 gcc_unreachable ();
8615 /* Output the encoding of an attribute value. */
8617 static void
8618 output_value_format (dw_attr_ref a)
8620 enum dwarf_form form = value_format (a);
8622 dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8625 /* Given a die and id, produce the appropriate abbreviations. */
8627 static void
8628 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8630 unsigned ix;
8631 dw_attr_ref a_attr;
8633 dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8634 dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8635 dwarf_tag_name (abbrev->die_tag));
8637 if (abbrev->die_child != NULL)
8638 dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8639 else
8640 dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8642 for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8644 dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8645 dwarf_attr_name (a_attr->dw_attr));
8646 output_value_format (a_attr);
8649 dw2_asm_output_data (1, 0, NULL);
8650 dw2_asm_output_data (1, 0, NULL);
8654 /* Output the .debug_abbrev section which defines the DIE abbreviation
8655 table. */
8657 static void
8658 output_abbrev_section (void)
8660 unsigned long abbrev_id;
8662 for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8663 output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8665 /* Terminate the table. */
8666 dw2_asm_output_data (1, 0, NULL);
8669 /* Output a symbol we can use to refer to this DIE from another CU. */
8671 static inline void
8672 output_die_symbol (dw_die_ref die)
8674 const char *sym = die->die_id.die_symbol;
8676 gcc_assert (!die->comdat_type_p);
8678 if (sym == 0)
8679 return;
8681 if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8682 /* We make these global, not weak; if the target doesn't support
8683 .linkonce, it doesn't support combining the sections, so debugging
8684 will break. */
8685 targetm.asm_out.globalize_label (asm_out_file, sym);
8687 ASM_OUTPUT_LABEL (asm_out_file, sym);
8690 /* Return a new location list, given the begin and end range, and the
8691 expression. */
8693 static inline dw_loc_list_ref
8694 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8695 const char *section)
8697 dw_loc_list_ref retlist = ggc_cleared_alloc<dw_loc_list_node> ();
8699 retlist->begin = begin;
8700 retlist->begin_entry = NULL;
8701 retlist->end = end;
8702 retlist->expr = expr;
8703 retlist->section = section;
8705 return retlist;
8708 /* Generate a new internal symbol for this location list node, if it
8709 hasn't got one yet. */
8711 static inline void
8712 gen_llsym (dw_loc_list_ref list)
8714 gcc_assert (!list->ll_symbol);
8715 list->ll_symbol = gen_internal_sym ("LLST");
8718 /* Output the location list given to us. */
8720 static void
8721 output_loc_list (dw_loc_list_ref list_head)
8723 dw_loc_list_ref curr = list_head;
8725 if (list_head->emitted)
8726 return;
8727 list_head->emitted = true;
8729 ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8731 /* Walk the location list, and output each range + expression. */
8732 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8734 unsigned long size;
8735 /* Don't output an entry that starts and ends at the same address. */
8736 if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8737 continue;
8738 size = size_of_locs (curr->expr);
8739 /* If the expression is too large, drop it on the floor. We could
8740 perhaps put it into DW_TAG_dwarf_procedure and refer to that
8741 in the expression, but >= 64KB expressions for a single value
8742 in a single range are unlikely very useful. */
8743 if (size > 0xffff)
8744 continue;
8745 if (dwarf_split_debug_info)
8747 dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8748 "Location list start/length entry (%s)",
8749 list_head->ll_symbol);
8750 dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8751 "Location list range start index (%s)",
8752 curr->begin);
8753 /* The length field is 4 bytes. If we ever need to support
8754 an 8-byte length, we can add a new DW_LLE code or fall back
8755 to DW_LLE_GNU_start_end_entry. */
8756 dw2_asm_output_delta (4, curr->end, curr->begin,
8757 "Location list range length (%s)",
8758 list_head->ll_symbol);
8760 else if (!have_multiple_function_sections)
8762 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8763 "Location list begin address (%s)",
8764 list_head->ll_symbol);
8765 dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8766 "Location list end address (%s)",
8767 list_head->ll_symbol);
8769 else
8771 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8772 "Location list begin address (%s)",
8773 list_head->ll_symbol);
8774 dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8775 "Location list end address (%s)",
8776 list_head->ll_symbol);
8779 /* Output the block length for this list of location operations. */
8780 gcc_assert (size <= 0xffff);
8781 dw2_asm_output_data (2, size, "%s", "Location expression size");
8783 output_loc_sequence (curr->expr, -1);
8786 if (dwarf_split_debug_info)
8787 dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8788 "Location list terminator (%s)",
8789 list_head->ll_symbol);
8790 else
8792 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8793 "Location list terminator begin (%s)",
8794 list_head->ll_symbol);
8795 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8796 "Location list terminator end (%s)",
8797 list_head->ll_symbol);
8801 /* Output a range_list offset into the debug_range section. Emit a
8802 relocated reference if val_entry is NULL, otherwise, emit an
8803 indirect reference. */
8805 static void
8806 output_range_list_offset (dw_attr_ref a)
8808 const char *name = dwarf_attr_name (a->dw_attr);
8810 if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8812 char *p = strchr (ranges_section_label, '\0');
8813 sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8814 dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8815 debug_ranges_section, "%s", name);
8816 *p = '\0';
8818 else
8819 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8820 "%s (offset from %s)", name, ranges_section_label);
8823 /* Output the offset into the debug_loc section. */
8825 static void
8826 output_loc_list_offset (dw_attr_ref a)
8828 char *sym = AT_loc_list (a)->ll_symbol;
8830 gcc_assert (sym);
8831 if (dwarf_split_debug_info)
8832 dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8833 "%s", dwarf_attr_name (a->dw_attr));
8834 else
8835 dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8836 "%s", dwarf_attr_name (a->dw_attr));
8839 /* Output an attribute's index or value appropriately. */
8841 static void
8842 output_attr_index_or_value (dw_attr_ref a)
8844 const char *name = dwarf_attr_name (a->dw_attr);
8846 if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8848 dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8849 return;
8851 switch (AT_class (a))
8853 case dw_val_class_addr:
8854 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8855 break;
8856 case dw_val_class_high_pc:
8857 case dw_val_class_lbl_id:
8858 dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8859 break;
8860 case dw_val_class_loc_list:
8861 output_loc_list_offset (a);
8862 break;
8863 default:
8864 gcc_unreachable ();
8868 /* Output a type signature. */
8870 static inline void
8871 output_signature (const char *sig, const char *name)
8873 int i;
8875 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8876 dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8879 /* Output the DIE and its attributes. Called recursively to generate
8880 the definitions of each child DIE. */
8882 static void
8883 output_die (dw_die_ref die)
8885 dw_attr_ref a;
8886 dw_die_ref c;
8887 unsigned long size;
8888 unsigned ix;
8890 /* If someone in another CU might refer to us, set up a symbol for
8891 them to point to. */
8892 if (! die->comdat_type_p && die->die_id.die_symbol)
8893 output_die_symbol (die);
8895 dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8896 (unsigned long)die->die_offset,
8897 dwarf_tag_name (die->die_tag));
8899 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8901 const char *name = dwarf_attr_name (a->dw_attr);
8903 switch (AT_class (a))
8905 case dw_val_class_addr:
8906 output_attr_index_or_value (a);
8907 break;
8909 case dw_val_class_offset:
8910 dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8911 "%s", name);
8912 break;
8914 case dw_val_class_range_list:
8915 output_range_list_offset (a);
8916 break;
8918 case dw_val_class_loc:
8919 size = size_of_locs (AT_loc (a));
8921 /* Output the block length for this list of location operations. */
8922 if (dwarf_version >= 4)
8923 dw2_asm_output_data_uleb128 (size, "%s", name);
8924 else
8925 dw2_asm_output_data (constant_size (size), size, "%s", name);
8927 output_loc_sequence (AT_loc (a), -1);
8928 break;
8930 case dw_val_class_const:
8931 /* ??? It would be slightly more efficient to use a scheme like is
8932 used for unsigned constants below, but gdb 4.x does not sign
8933 extend. Gdb 5.x does sign extend. */
8934 dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8935 break;
8937 case dw_val_class_unsigned_const:
8939 int csize = constant_size (AT_unsigned (a));
8940 if (dwarf_version == 3
8941 && a->dw_attr == DW_AT_data_member_location
8942 && csize >= 4)
8943 dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8944 else
8945 dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8947 break;
8949 case dw_val_class_const_double:
8951 unsigned HOST_WIDE_INT first, second;
8953 if (HOST_BITS_PER_WIDE_INT >= 64)
8954 dw2_asm_output_data (1,
8955 HOST_BITS_PER_DOUBLE_INT
8956 / HOST_BITS_PER_CHAR,
8957 NULL);
8959 if (WORDS_BIG_ENDIAN)
8961 first = a->dw_attr_val.v.val_double.high;
8962 second = a->dw_attr_val.v.val_double.low;
8964 else
8966 first = a->dw_attr_val.v.val_double.low;
8967 second = a->dw_attr_val.v.val_double.high;
8970 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8971 first, "%s", name);
8972 dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8973 second, NULL);
8975 break;
8977 case dw_val_class_wide_int:
8979 int i;
8980 int len = get_full_len (*a->dw_attr_val.v.val_wide);
8981 int l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8982 if (len * HOST_BITS_PER_WIDE_INT > 64)
8983 dw2_asm_output_data (1, get_full_len (*a->dw_attr_val.v.val_wide) * l,
8984 NULL);
8986 if (WORDS_BIG_ENDIAN)
8987 for (i = len - 1; i >= 0; --i)
8989 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8990 "%s", name);
8991 name = NULL;
8993 else
8994 for (i = 0; i < len; ++i)
8996 dw2_asm_output_data (l, a->dw_attr_val.v.val_wide->elt (i),
8997 "%s", name);
8998 name = NULL;
9001 break;
9003 case dw_val_class_vec:
9005 unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9006 unsigned int len = a->dw_attr_val.v.val_vec.length;
9007 unsigned int i;
9008 unsigned char *p;
9010 dw2_asm_output_data (constant_size (len * elt_size),
9011 len * elt_size, "%s", name);
9012 if (elt_size > sizeof (HOST_WIDE_INT))
9014 elt_size /= 2;
9015 len *= 2;
9017 for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9018 i < len;
9019 i++, p += elt_size)
9020 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9021 "fp or vector constant word %u", i);
9022 break;
9025 case dw_val_class_flag:
9026 if (dwarf_version >= 4)
9028 /* Currently all add_AT_flag calls pass in 1 as last argument,
9029 so DW_FORM_flag_present can be used. If that ever changes,
9030 we'll need to use DW_FORM_flag and have some optimization
9031 in build_abbrev_table that will change those to
9032 DW_FORM_flag_present if it is set to 1 in all DIEs using
9033 the same abbrev entry. */
9034 gcc_assert (AT_flag (a) == 1);
9035 if (flag_debug_asm)
9036 fprintf (asm_out_file, "\t\t\t%s %s\n",
9037 ASM_COMMENT_START, name);
9038 break;
9040 dw2_asm_output_data (1, AT_flag (a), "%s", name);
9041 break;
9043 case dw_val_class_loc_list:
9044 output_attr_index_or_value (a);
9045 break;
9047 case dw_val_class_die_ref:
9048 if (AT_ref_external (a))
9050 if (AT_ref (a)->comdat_type_p)
9052 comdat_type_node_ref type_node =
9053 AT_ref (a)->die_id.die_type_node;
9055 gcc_assert (type_node);
9056 output_signature (type_node->signature, name);
9058 else
9060 const char *sym = AT_ref (a)->die_id.die_symbol;
9061 int size;
9063 gcc_assert (sym);
9064 /* In DWARF2, DW_FORM_ref_addr is sized by target address
9065 length, whereas in DWARF3 it's always sized as an
9066 offset. */
9067 if (dwarf_version == 2)
9068 size = DWARF2_ADDR_SIZE;
9069 else
9070 size = DWARF_OFFSET_SIZE;
9071 dw2_asm_output_offset (size, sym, debug_info_section, "%s",
9072 name);
9075 else
9077 gcc_assert (AT_ref (a)->die_offset);
9078 dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9079 "%s", name);
9081 break;
9083 case dw_val_class_fde_ref:
9085 char l1[20];
9087 ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9088 a->dw_attr_val.v.val_fde_index * 2);
9089 dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9090 "%s", name);
9092 break;
9094 case dw_val_class_vms_delta:
9095 #ifdef ASM_OUTPUT_DWARF_VMS_DELTA
9096 dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
9097 AT_vms_delta2 (a), AT_vms_delta1 (a),
9098 "%s", name);
9099 #else
9100 dw2_asm_output_delta (DWARF_OFFSET_SIZE,
9101 AT_vms_delta2 (a), AT_vms_delta1 (a),
9102 "%s", name);
9103 #endif
9104 break;
9106 case dw_val_class_lbl_id:
9107 output_attr_index_or_value (a);
9108 break;
9110 case dw_val_class_lineptr:
9111 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9112 debug_line_section, "%s", name);
9113 break;
9115 case dw_val_class_macptr:
9116 dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9117 debug_macinfo_section, "%s", name);
9118 break;
9120 case dw_val_class_str:
9121 if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
9122 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9123 a->dw_attr_val.v.val_str->label,
9124 debug_str_section,
9125 "%s: \"%s\"", name, AT_string (a));
9126 else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
9127 dw2_asm_output_data_uleb128 (AT_index (a),
9128 "%s: \"%s\"", name, AT_string (a));
9129 else
9130 dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9131 break;
9133 case dw_val_class_file:
9135 int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9137 dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9138 a->dw_attr_val.v.val_file->filename);
9139 break;
9142 case dw_val_class_data8:
9144 int i;
9146 for (i = 0; i < 8; i++)
9147 dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9148 i == 0 ? "%s" : NULL, name);
9149 break;
9152 case dw_val_class_high_pc:
9153 dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
9154 get_AT_low_pc (die), "DW_AT_high_pc");
9155 break;
9157 default:
9158 gcc_unreachable ();
9162 FOR_EACH_CHILD (die, c, output_die (c));
9164 /* Add null byte to terminate sibling list. */
9165 if (die->die_child != NULL)
9166 dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9167 (unsigned long) die->die_offset);
9170 /* Output the compilation unit that appears at the beginning of the
9171 .debug_info section, and precedes the DIE descriptions. */
9173 static void
9174 output_compilation_unit_header (void)
9176 /* We don't support actual DWARFv5 units yet, we just use some
9177 DWARFv5 draft DIE tags in DWARFv4 format. */
9178 int ver = dwarf_version < 5 ? dwarf_version : 4;
9180 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9181 dw2_asm_output_data (4, 0xffffffff,
9182 "Initial length escape value indicating 64-bit DWARF extension");
9183 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9184 next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9185 "Length of Compilation Unit Info");
9186 dw2_asm_output_data (2, ver, "DWARF version number");
9187 dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9188 debug_abbrev_section,
9189 "Offset Into Abbrev. Section");
9190 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9193 /* Output the compilation unit DIE and its children. */
9195 static void
9196 output_comp_unit (dw_die_ref die, int output_if_empty)
9198 const char *secname, *oldsym;
9199 char *tmp;
9201 /* Unless we are outputting main CU, we may throw away empty ones. */
9202 if (!output_if_empty && die->die_child == NULL)
9203 return;
9205 /* Even if there are no children of this DIE, we must output the information
9206 about the compilation unit. Otherwise, on an empty translation unit, we
9207 will generate a present, but empty, .debug_info section. IRIX 6.5 `nm'
9208 will then complain when examining the file. First mark all the DIEs in
9209 this CU so we know which get local refs. */
9210 mark_dies (die);
9212 external_ref_hash_type *extern_map = optimize_external_refs (die);
9214 build_abbrev_table (die, extern_map);
9216 delete extern_map;
9218 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9219 next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9220 calc_die_sizes (die);
9222 oldsym = die->die_id.die_symbol;
9223 if (oldsym)
9225 tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9227 sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9228 secname = tmp;
9229 die->die_id.die_symbol = NULL;
9230 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9232 else
9234 switch_to_section (debug_info_section);
9235 ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9236 info_section_emitted = true;
9239 /* Output debugging information. */
9240 output_compilation_unit_header ();
9241 output_die (die);
9243 /* Leave the marks on the main CU, so we can check them in
9244 output_pubnames. */
9245 if (oldsym)
9247 unmark_dies (die);
9248 die->die_id.die_symbol = oldsym;
9252 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
9253 and .debug_pubtypes. This is configured per-target, but can be
9254 overridden by the -gpubnames or -gno-pubnames options. */
9256 static inline bool
9257 want_pubnames (void)
9259 if (debug_info_level <= DINFO_LEVEL_TERSE)
9260 return false;
9261 if (debug_generate_pub_sections != -1)
9262 return debug_generate_pub_sections;
9263 return targetm.want_debug_pub_sections;
9266 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes. */
9268 static void
9269 add_AT_pubnames (dw_die_ref die)
9271 if (want_pubnames ())
9272 add_AT_flag (die, DW_AT_GNU_pubnames, 1);
9275 /* Add a string attribute value to a skeleton DIE. */
9277 static inline void
9278 add_skeleton_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind,
9279 const char *str)
9281 dw_attr_node attr;
9282 struct indirect_string_node *node;
9284 if (! skeleton_debug_str_hash)
9285 skeleton_debug_str_hash
9286 = hash_table<indirect_string_hasher>::create_ggc (10);
9288 node = find_AT_string_in_table (str, skeleton_debug_str_hash);
9289 find_string_form (node);
9290 if (node->form == DW_FORM_GNU_str_index)
9291 node->form = DW_FORM_strp;
9293 attr.dw_attr = attr_kind;
9294 attr.dw_attr_val.val_class = dw_val_class_str;
9295 attr.dw_attr_val.val_entry = NULL;
9296 attr.dw_attr_val.v.val_str = node;
9297 add_dwarf_attr (die, &attr);
9300 /* Helper function to generate top-level dies for skeleton debug_info and
9301 debug_types. */
9303 static void
9304 add_top_level_skeleton_die_attrs (dw_die_ref die)
9306 const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
9307 const char *comp_dir = comp_dir_string ();
9309 add_skeleton_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
9310 if (comp_dir != NULL)
9311 add_skeleton_AT_string (die, DW_AT_comp_dir, comp_dir);
9312 add_AT_pubnames (die);
9313 add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
9316 /* Output skeleton debug sections that point to the dwo file. */
9318 static void
9319 output_skeleton_debug_sections (dw_die_ref comp_unit)
9321 /* We don't support actual DWARFv5 units yet, we just use some
9322 DWARFv5 draft DIE tags in DWARFv4 format. */
9323 int ver = dwarf_version < 5 ? dwarf_version : 4;
9325 /* These attributes will be found in the full debug_info section. */
9326 remove_AT (comp_unit, DW_AT_producer);
9327 remove_AT (comp_unit, DW_AT_language);
9329 switch_to_section (debug_skeleton_info_section);
9330 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
9332 /* Produce the skeleton compilation-unit header. This one differs enough from
9333 a normal CU header that it's better not to call output_compilation_unit
9334 header. */
9335 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9336 dw2_asm_output_data (4, 0xffffffff,
9337 "Initial length escape value indicating 64-bit DWARF extension");
9339 dw2_asm_output_data (DWARF_OFFSET_SIZE,
9340 DWARF_COMPILE_UNIT_HEADER_SIZE
9341 - DWARF_INITIAL_LENGTH_SIZE
9342 + size_of_die (comp_unit),
9343 "Length of Compilation Unit Info");
9344 dw2_asm_output_data (2, ver, "DWARF version number");
9345 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
9346 debug_abbrev_section,
9347 "Offset Into Abbrev. Section");
9348 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9350 comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
9351 output_die (comp_unit);
9353 /* Build the skeleton debug_abbrev section. */
9354 switch_to_section (debug_skeleton_abbrev_section);
9355 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
9357 output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
9359 dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
9362 /* Output a comdat type unit DIE and its children. */
9364 static void
9365 output_comdat_type_unit (comdat_type_node *node)
9367 const char *secname;
9368 char *tmp;
9369 int i;
9370 #if defined (OBJECT_FORMAT_ELF)
9371 tree comdat_key;
9372 #endif
9374 /* First mark all the DIEs in this CU so we know which get local refs. */
9375 mark_dies (node->root_die);
9377 external_ref_hash_type *extern_map = optimize_external_refs (node->root_die);
9379 build_abbrev_table (node->root_die, extern_map);
9381 delete extern_map;
9382 extern_map = NULL;
9384 /* Initialize the beginning DIE offset - and calculate sizes/offsets. */
9385 next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9386 calc_die_sizes (node->root_die);
9388 #if defined (OBJECT_FORMAT_ELF)
9389 if (!dwarf_split_debug_info)
9390 secname = ".debug_types";
9391 else
9392 secname = ".debug_types.dwo";
9394 tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9395 sprintf (tmp, "wt.");
9396 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9397 sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9398 comdat_key = get_identifier (tmp);
9399 targetm.asm_out.named_section (secname,
9400 SECTION_DEBUG | SECTION_LINKONCE,
9401 comdat_key);
9402 #else
9403 tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9404 sprintf (tmp, ".gnu.linkonce.wt.");
9405 for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9406 sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9407 secname = tmp;
9408 switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9409 #endif
9411 /* Output debugging information. */
9412 output_compilation_unit_header ();
9413 output_signature (node->signature, "Type Signature");
9414 dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9415 "Offset to Type DIE");
9416 output_die (node->root_die);
9418 unmark_dies (node->root_die);
9421 /* Return the DWARF2/3 pubname associated with a decl. */
9423 static const char *
9424 dwarf2_name (tree decl, int scope)
9426 if (DECL_NAMELESS (decl))
9427 return NULL;
9428 return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9431 /* Add a new entry to .debug_pubnames if appropriate. */
9433 static void
9434 add_pubname_string (const char *str, dw_die_ref die)
9436 pubname_entry e;
9438 e.die = die;
9439 e.name = xstrdup (str);
9440 vec_safe_push (pubname_table, e);
9443 static void
9444 add_pubname (tree decl, dw_die_ref die)
9446 if (!want_pubnames ())
9447 return;
9449 /* Don't add items to the table when we expect that the consumer will have
9450 just read the enclosing die. For example, if the consumer is looking at a
9451 class_member, it will either be inside the class already, or will have just
9452 looked up the class to find the member. Either way, searching the class is
9453 faster than searching the index. */
9454 if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
9455 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9457 const char *name = dwarf2_name (decl, 1);
9459 if (name)
9460 add_pubname_string (name, die);
9464 /* Add an enumerator to the pubnames section. */
9466 static void
9467 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
9469 pubname_entry e;
9471 gcc_assert (scope_name);
9472 e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
9473 e.die = die;
9474 vec_safe_push (pubname_table, e);
9477 /* Add a new entry to .debug_pubtypes if appropriate. */
9479 static void
9480 add_pubtype (tree decl, dw_die_ref die)
9482 pubname_entry e;
9484 if (!want_pubnames ())
9485 return;
9487 if ((TREE_PUBLIC (decl)
9488 || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
9489 && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9491 tree scope = NULL;
9492 const char *scope_name = "";
9493 const char *sep = is_cxx () ? "::" : ".";
9494 const char *name;
9496 scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
9497 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9499 scope_name = lang_hooks.dwarf_name (scope, 1);
9500 if (scope_name != NULL && scope_name[0] != '\0')
9501 scope_name = concat (scope_name, sep, NULL);
9502 else
9503 scope_name = "";
9506 if (TYPE_P (decl))
9507 name = type_tag (decl);
9508 else
9509 name = lang_hooks.dwarf_name (decl, 1);
9511 /* If we don't have a name for the type, there's no point in adding
9512 it to the table. */
9513 if (name != NULL && name[0] != '\0')
9515 e.die = die;
9516 e.name = concat (scope_name, name, NULL);
9517 vec_safe_push (pubtype_table, e);
9520 /* Although it might be more consistent to add the pubinfo for the
9521 enumerators as their dies are created, they should only be added if the
9522 enum type meets the criteria above. So rather than re-check the parent
9523 enum type whenever an enumerator die is created, just output them all
9524 here. This isn't protected by the name conditional because anonymous
9525 enums don't have names. */
9526 if (die->die_tag == DW_TAG_enumeration_type)
9528 dw_die_ref c;
9530 FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
9535 /* Output a single entry in the pubnames table. */
9537 static void
9538 output_pubname (dw_offset die_offset, pubname_entry *entry)
9540 dw_die_ref die = entry->die;
9541 int is_static = get_AT_flag (die, DW_AT_external) ? 0 : 1;
9543 dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9545 if (debug_generate_pub_sections == 2)
9547 /* This logic follows gdb's method for determining the value of the flag
9548 byte. */
9549 uint32_t flags = GDB_INDEX_SYMBOL_KIND_NONE;
9550 switch (die->die_tag)
9552 case DW_TAG_typedef:
9553 case DW_TAG_base_type:
9554 case DW_TAG_subrange_type:
9555 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9556 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9557 break;
9558 case DW_TAG_enumerator:
9559 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9560 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9561 if (!is_cxx () && !is_java ())
9562 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9563 break;
9564 case DW_TAG_subprogram:
9565 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9566 GDB_INDEX_SYMBOL_KIND_FUNCTION);
9567 if (!is_ada ())
9568 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9569 break;
9570 case DW_TAG_constant:
9571 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9572 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9573 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9574 break;
9575 case DW_TAG_variable:
9576 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags,
9577 GDB_INDEX_SYMBOL_KIND_VARIABLE);
9578 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, is_static);
9579 break;
9580 case DW_TAG_namespace:
9581 case DW_TAG_imported_declaration:
9582 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9583 break;
9584 case DW_TAG_class_type:
9585 case DW_TAG_interface_type:
9586 case DW_TAG_structure_type:
9587 case DW_TAG_union_type:
9588 case DW_TAG_enumeration_type:
9589 GDB_INDEX_SYMBOL_KIND_SET_VALUE(flags, GDB_INDEX_SYMBOL_KIND_TYPE);
9590 if (!is_cxx () && !is_java ())
9591 GDB_INDEX_SYMBOL_STATIC_SET_VALUE(flags, 1);
9592 break;
9593 default:
9594 /* An unusual tag. Leave the flag-byte empty. */
9595 break;
9597 dw2_asm_output_data (1, flags >> GDB_INDEX_CU_BITSIZE,
9598 "GDB-index flags");
9601 dw2_asm_output_nstring (entry->name, -1, "external name");
9605 /* Output the public names table used to speed up access to externally
9606 visible names; or the public types table used to find type definitions. */
9608 static void
9609 output_pubnames (vec<pubname_entry, va_gc> *names)
9611 unsigned i;
9612 unsigned long pubnames_length = size_of_pubnames (names);
9613 pubname_ref pub;
9615 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9616 dw2_asm_output_data (4, 0xffffffff,
9617 "Initial length escape value indicating 64-bit DWARF extension");
9618 dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length, "Pub Info Length");
9620 /* Version number for pubnames/pubtypes is independent of dwarf version. */
9621 dw2_asm_output_data (2, 2, "DWARF Version");
9623 if (dwarf_split_debug_info)
9624 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9625 debug_skeleton_info_section,
9626 "Offset of Compilation Unit Info");
9627 else
9628 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9629 debug_info_section,
9630 "Offset of Compilation Unit Info");
9631 dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9632 "Compilation Unit Length");
9634 FOR_EACH_VEC_ELT (*names, i, pub)
9636 if (include_pubname_in_output (names, pub))
9638 dw_offset die_offset = pub->die->die_offset;
9640 /* We shouldn't see pubnames for DIEs outside of the main CU. */
9641 if (names == pubname_table && pub->die->die_tag != DW_TAG_enumerator)
9642 gcc_assert (pub->die->die_mark);
9644 /* If we're putting types in their own .debug_types sections,
9645 the .debug_pubtypes table will still point to the compile
9646 unit (not the type unit), so we want to use the offset of
9647 the skeleton DIE (if there is one). */
9648 if (pub->die->comdat_type_p && names == pubtype_table)
9650 comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9652 if (type_node != NULL)
9653 die_offset = (type_node->skeleton_die != NULL
9654 ? type_node->skeleton_die->die_offset
9655 : comp_unit_die ()->die_offset);
9658 output_pubname (die_offset, pub);
9662 dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9665 /* Output public names and types tables if necessary. */
9667 static void
9668 output_pubtables (void)
9670 if (!want_pubnames () || !info_section_emitted)
9671 return;
9673 switch_to_section (debug_pubnames_section);
9674 output_pubnames (pubname_table);
9675 /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
9676 It shouldn't hurt to emit it always, since pure DWARF2 consumers
9677 simply won't look for the section. */
9678 switch_to_section (debug_pubtypes_section);
9679 output_pubnames (pubtype_table);
9683 /* Output the information that goes into the .debug_aranges table.
9684 Namely, define the beginning and ending address range of the
9685 text section generated for this compilation unit. */
9687 static void
9688 output_aranges (unsigned long aranges_length)
9690 unsigned i;
9692 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9693 dw2_asm_output_data (4, 0xffffffff,
9694 "Initial length escape value indicating 64-bit DWARF extension");
9695 dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9696 "Length of Address Ranges Info");
9697 /* Version number for aranges is still 2, even up to DWARF5. */
9698 dw2_asm_output_data (2, 2, "DWARF Version");
9699 if (dwarf_split_debug_info)
9700 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9701 debug_skeleton_info_section,
9702 "Offset of Compilation Unit Info");
9703 else
9704 dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9705 debug_info_section,
9706 "Offset of Compilation Unit Info");
9707 dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9708 dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9710 /* We need to align to twice the pointer size here. */
9711 if (DWARF_ARANGES_PAD_SIZE)
9713 /* Pad using a 2 byte words so that padding is correct for any
9714 pointer size. */
9715 dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9716 2 * DWARF2_ADDR_SIZE);
9717 for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9718 dw2_asm_output_data (2, 0, NULL);
9721 /* It is necessary not to output these entries if the sections were
9722 not used; if the sections were not used, the length will be 0 and
9723 the address may end up as 0 if the section is discarded by ld
9724 --gc-sections, leaving an invalid (0, 0) entry that can be
9725 confused with the terminator. */
9726 if (text_section_used)
9728 dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9729 dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9730 text_section_label, "Length");
9732 if (cold_text_section_used)
9734 dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9735 "Address");
9736 dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9737 cold_text_section_label, "Length");
9740 if (have_multiple_function_sections)
9742 unsigned fde_idx;
9743 dw_fde_ref fde;
9745 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9747 if (DECL_IGNORED_P (fde->decl))
9748 continue;
9749 if (!fde->in_std_section)
9751 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9752 "Address");
9753 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9754 fde->dw_fde_begin, "Length");
9756 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9758 dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9759 "Address");
9760 dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9761 fde->dw_fde_second_begin, "Length");
9766 /* Output the terminator words. */
9767 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9768 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9771 /* Add a new entry to .debug_ranges. Return the offset at which it
9772 was placed. */
9774 static unsigned int
9775 add_ranges_num (int num)
9777 unsigned int in_use = ranges_table_in_use;
9779 if (in_use == ranges_table_allocated)
9781 ranges_table_allocated += RANGES_TABLE_INCREMENT;
9782 ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9783 ranges_table_allocated);
9784 memset (ranges_table + ranges_table_in_use, 0,
9785 RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9788 ranges_table[in_use].num = num;
9789 ranges_table_in_use = in_use + 1;
9791 return in_use * 2 * DWARF2_ADDR_SIZE;
9794 /* Add a new entry to .debug_ranges corresponding to a block, or a
9795 range terminator if BLOCK is NULL. */
9797 static unsigned int
9798 add_ranges (const_tree block)
9800 return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9803 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9804 When using dwarf_split_debug_info, address attributes in dies destined
9805 for the final executable should be direct references--setting the
9806 parameter force_direct ensures this behavior. */
9808 static void
9809 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9810 bool *added, bool force_direct)
9812 unsigned int in_use = ranges_by_label_in_use;
9813 unsigned int offset;
9815 if (in_use == ranges_by_label_allocated)
9817 ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9818 ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9819 ranges_by_label,
9820 ranges_by_label_allocated);
9821 memset (ranges_by_label + ranges_by_label_in_use, 0,
9822 RANGES_TABLE_INCREMENT
9823 * sizeof (struct dw_ranges_by_label_struct));
9826 ranges_by_label[in_use].begin = begin;
9827 ranges_by_label[in_use].end = end;
9828 ranges_by_label_in_use = in_use + 1;
9830 offset = add_ranges_num (-(int)in_use - 1);
9831 if (!*added)
9833 add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9834 *added = true;
9838 static void
9839 output_ranges (void)
9841 unsigned i;
9842 static const char *const start_fmt = "Offset %#x";
9843 const char *fmt = start_fmt;
9845 for (i = 0; i < ranges_table_in_use; i++)
9847 int block_num = ranges_table[i].num;
9849 if (block_num > 0)
9851 char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9852 char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9854 ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9855 ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9857 /* If all code is in the text section, then the compilation
9858 unit base address defaults to DW_AT_low_pc, which is the
9859 base of the text section. */
9860 if (!have_multiple_function_sections)
9862 dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9863 text_section_label,
9864 fmt, i * 2 * DWARF2_ADDR_SIZE);
9865 dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9866 text_section_label, NULL);
9869 /* Otherwise, the compilation unit base address is zero,
9870 which allows us to use absolute addresses, and not worry
9871 about whether the target supports cross-section
9872 arithmetic. */
9873 else
9875 dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9876 fmt, i * 2 * DWARF2_ADDR_SIZE);
9877 dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9880 fmt = NULL;
9883 /* Negative block_num stands for an index into ranges_by_label. */
9884 else if (block_num < 0)
9886 int lab_idx = - block_num - 1;
9888 if (!have_multiple_function_sections)
9890 gcc_unreachable ();
9891 #if 0
9892 /* If we ever use add_ranges_by_labels () for a single
9893 function section, all we have to do is to take out
9894 the #if 0 above. */
9895 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9896 ranges_by_label[lab_idx].begin,
9897 text_section_label,
9898 fmt, i * 2 * DWARF2_ADDR_SIZE);
9899 dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9900 ranges_by_label[lab_idx].end,
9901 text_section_label, NULL);
9902 #endif
9904 else
9906 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9907 ranges_by_label[lab_idx].begin,
9908 fmt, i * 2 * DWARF2_ADDR_SIZE);
9909 dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9910 ranges_by_label[lab_idx].end,
9911 NULL);
9914 else
9916 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9917 dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9918 fmt = start_fmt;
9923 /* Data structure containing information about input files. */
9924 struct file_info
9926 const char *path; /* Complete file name. */
9927 const char *fname; /* File name part. */
9928 int length; /* Length of entire string. */
9929 struct dwarf_file_data * file_idx; /* Index in input file table. */
9930 int dir_idx; /* Index in directory table. */
9933 /* Data structure containing information about directories with source
9934 files. */
9935 struct dir_info
9937 const char *path; /* Path including directory name. */
9938 int length; /* Path length. */
9939 int prefix; /* Index of directory entry which is a prefix. */
9940 int count; /* Number of files in this directory. */
9941 int dir_idx; /* Index of directory used as base. */
9944 /* Callback function for file_info comparison. We sort by looking at
9945 the directories in the path. */
9947 static int
9948 file_info_cmp (const void *p1, const void *p2)
9950 const struct file_info *const s1 = (const struct file_info *) p1;
9951 const struct file_info *const s2 = (const struct file_info *) p2;
9952 const unsigned char *cp1;
9953 const unsigned char *cp2;
9955 /* Take care of file names without directories. We need to make sure that
9956 we return consistent values to qsort since some will get confused if
9957 we return the same value when identical operands are passed in opposite
9958 orders. So if neither has a directory, return 0 and otherwise return
9959 1 or -1 depending on which one has the directory. */
9960 if ((s1->path == s1->fname || s2->path == s2->fname))
9961 return (s2->path == s2->fname) - (s1->path == s1->fname);
9963 cp1 = (const unsigned char *) s1->path;
9964 cp2 = (const unsigned char *) s2->path;
9966 while (1)
9968 ++cp1;
9969 ++cp2;
9970 /* Reached the end of the first path? If so, handle like above. */
9971 if ((cp1 == (const unsigned char *) s1->fname)
9972 || (cp2 == (const unsigned char *) s2->fname))
9973 return ((cp2 == (const unsigned char *) s2->fname)
9974 - (cp1 == (const unsigned char *) s1->fname));
9976 /* Character of current path component the same? */
9977 else if (*cp1 != *cp2)
9978 return *cp1 - *cp2;
9982 struct file_name_acquire_data
9984 struct file_info *files;
9985 int used_files;
9986 int max_files;
9989 /* Traversal function for the hash table. */
9992 file_name_acquire (dwarf_file_data **slot, file_name_acquire_data *fnad)
9994 struct dwarf_file_data *d = *slot;
9995 struct file_info *fi;
9996 const char *f;
9998 gcc_assert (fnad->max_files >= d->emitted_number);
10000 if (! d->emitted_number)
10001 return 1;
10003 gcc_assert (fnad->max_files != fnad->used_files);
10005 fi = fnad->files + fnad->used_files++;
10007 /* Skip all leading "./". */
10008 f = d->filename;
10009 while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
10010 f += 2;
10012 /* Create a new array entry. */
10013 fi->path = f;
10014 fi->length = strlen (f);
10015 fi->file_idx = d;
10017 /* Search for the file name part. */
10018 f = strrchr (f, DIR_SEPARATOR);
10019 #if defined (DIR_SEPARATOR_2)
10021 char *g = strrchr (fi->path, DIR_SEPARATOR_2);
10023 if (g != NULL)
10025 if (f == NULL || f < g)
10026 f = g;
10029 #endif
10031 fi->fname = f == NULL ? fi->path : f + 1;
10032 return 1;
10035 /* Output the directory table and the file name table. We try to minimize
10036 the total amount of memory needed. A heuristic is used to avoid large
10037 slowdowns with many input files. */
10039 static void
10040 output_file_names (void)
10042 struct file_name_acquire_data fnad;
10043 int numfiles;
10044 struct file_info *files;
10045 struct dir_info *dirs;
10046 int *saved;
10047 int *savehere;
10048 int *backmap;
10049 int ndirs;
10050 int idx_offset;
10051 int i;
10053 if (!last_emitted_file)
10055 dw2_asm_output_data (1, 0, "End directory table");
10056 dw2_asm_output_data (1, 0, "End file name table");
10057 return;
10060 numfiles = last_emitted_file->emitted_number;
10062 /* Allocate the various arrays we need. */
10063 files = XALLOCAVEC (struct file_info, numfiles);
10064 dirs = XALLOCAVEC (struct dir_info, numfiles);
10066 fnad.files = files;
10067 fnad.used_files = 0;
10068 fnad.max_files = numfiles;
10069 file_table->traverse<file_name_acquire_data *, file_name_acquire> (&fnad);
10070 gcc_assert (fnad.used_files == fnad.max_files);
10072 qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
10074 /* Find all the different directories used. */
10075 dirs[0].path = files[0].path;
10076 dirs[0].length = files[0].fname - files[0].path;
10077 dirs[0].prefix = -1;
10078 dirs[0].count = 1;
10079 dirs[0].dir_idx = 0;
10080 files[0].dir_idx = 0;
10081 ndirs = 1;
10083 for (i = 1; i < numfiles; i++)
10084 if (files[i].fname - files[i].path == dirs[ndirs - 1].length
10085 && memcmp (dirs[ndirs - 1].path, files[i].path,
10086 dirs[ndirs - 1].length) == 0)
10088 /* Same directory as last entry. */
10089 files[i].dir_idx = ndirs - 1;
10090 ++dirs[ndirs - 1].count;
10092 else
10094 int j;
10096 /* This is a new directory. */
10097 dirs[ndirs].path = files[i].path;
10098 dirs[ndirs].length = files[i].fname - files[i].path;
10099 dirs[ndirs].count = 1;
10100 dirs[ndirs].dir_idx = ndirs;
10101 files[i].dir_idx = ndirs;
10103 /* Search for a prefix. */
10104 dirs[ndirs].prefix = -1;
10105 for (j = 0; j < ndirs; j++)
10106 if (dirs[j].length < dirs[ndirs].length
10107 && dirs[j].length > 1
10108 && (dirs[ndirs].prefix == -1
10109 || dirs[j].length > dirs[dirs[ndirs].prefix].length)
10110 && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
10111 dirs[ndirs].prefix = j;
10113 ++ndirs;
10116 /* Now to the actual work. We have to find a subset of the directories which
10117 allow expressing the file name using references to the directory table
10118 with the least amount of characters. We do not do an exhaustive search
10119 where we would have to check out every combination of every single
10120 possible prefix. Instead we use a heuristic which provides nearly optimal
10121 results in most cases and never is much off. */
10122 saved = XALLOCAVEC (int, ndirs);
10123 savehere = XALLOCAVEC (int, ndirs);
10125 memset (saved, '\0', ndirs * sizeof (saved[0]));
10126 for (i = 0; i < ndirs; i++)
10128 int j;
10129 int total;
10131 /* We can always save some space for the current directory. But this
10132 does not mean it will be enough to justify adding the directory. */
10133 savehere[i] = dirs[i].length;
10134 total = (savehere[i] - saved[i]) * dirs[i].count;
10136 for (j = i + 1; j < ndirs; j++)
10138 savehere[j] = 0;
10139 if (saved[j] < dirs[i].length)
10141 /* Determine whether the dirs[i] path is a prefix of the
10142 dirs[j] path. */
10143 int k;
10145 k = dirs[j].prefix;
10146 while (k != -1 && k != (int) i)
10147 k = dirs[k].prefix;
10149 if (k == (int) i)
10151 /* Yes it is. We can possibly save some memory by
10152 writing the filenames in dirs[j] relative to
10153 dirs[i]. */
10154 savehere[j] = dirs[i].length;
10155 total += (savehere[j] - saved[j]) * dirs[j].count;
10160 /* Check whether we can save enough to justify adding the dirs[i]
10161 directory. */
10162 if (total > dirs[i].length + 1)
10164 /* It's worthwhile adding. */
10165 for (j = i; j < ndirs; j++)
10166 if (savehere[j] > 0)
10168 /* Remember how much we saved for this directory so far. */
10169 saved[j] = savehere[j];
10171 /* Remember the prefix directory. */
10172 dirs[j].dir_idx = i;
10177 /* Emit the directory name table. */
10178 idx_offset = dirs[0].length > 0 ? 1 : 0;
10179 for (i = 1 - idx_offset; i < ndirs; i++)
10180 dw2_asm_output_nstring (dirs[i].path,
10181 dirs[i].length
10182 - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
10183 "Directory Entry: %#x", i + idx_offset);
10185 dw2_asm_output_data (1, 0, "End directory table");
10187 /* We have to emit them in the order of emitted_number since that's
10188 used in the debug info generation. To do this efficiently we
10189 generate a back-mapping of the indices first. */
10190 backmap = XALLOCAVEC (int, numfiles);
10191 for (i = 0; i < numfiles; i++)
10192 backmap[files[i].file_idx->emitted_number - 1] = i;
10194 /* Now write all the file names. */
10195 for (i = 0; i < numfiles; i++)
10197 int file_idx = backmap[i];
10198 int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
10200 #ifdef VMS_DEBUGGING_INFO
10201 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
10203 /* Setting these fields can lead to debugger miscomparisons,
10204 but VMS Debug requires them to be set correctly. */
10206 int ver;
10207 long long cdt;
10208 long siz;
10209 int maxfilelen = strlen (files[file_idx].path)
10210 + dirs[dir_idx].length
10211 + MAX_VMS_VERSION_LEN + 1;
10212 char *filebuf = XALLOCAVEC (char, maxfilelen);
10214 vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
10215 snprintf (filebuf, maxfilelen, "%s;%d",
10216 files[file_idx].path + dirs[dir_idx].length, ver);
10218 dw2_asm_output_nstring
10219 (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
10221 /* Include directory index. */
10222 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10224 /* Modification time. */
10225 dw2_asm_output_data_uleb128
10226 ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
10227 ? cdt : 0,
10228 NULL);
10230 /* File length in bytes. */
10231 dw2_asm_output_data_uleb128
10232 ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
10233 ? siz : 0,
10234 NULL);
10235 #else
10236 dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
10237 "File Entry: %#x", (unsigned) i + 1);
10239 /* Include directory index. */
10240 dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
10242 /* Modification time. */
10243 dw2_asm_output_data_uleb128 (0, NULL);
10245 /* File length in bytes. */
10246 dw2_asm_output_data_uleb128 (0, NULL);
10247 #endif /* VMS_DEBUGGING_INFO */
10250 dw2_asm_output_data (1, 0, "End file name table");
10254 /* Output one line number table into the .debug_line section. */
10256 static void
10257 output_one_line_info_table (dw_line_info_table *table)
10259 char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
10260 unsigned int current_line = 1;
10261 bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
10262 dw_line_info_entry *ent;
10263 size_t i;
10265 FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
10267 switch (ent->opcode)
10269 case LI_set_address:
10270 /* ??? Unfortunately, we have little choice here currently, and
10271 must always use the most general form. GCC does not know the
10272 address delta itself, so we can't use DW_LNS_advance_pc. Many
10273 ports do have length attributes which will give an upper bound
10274 on the address range. We could perhaps use length attributes
10275 to determine when it is safe to use DW_LNS_fixed_advance_pc. */
10276 ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
10278 /* This can handle any delta. This takes
10279 4+DWARF2_ADDR_SIZE bytes. */
10280 dw2_asm_output_data (1, 0, "set address %s", line_label);
10281 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10282 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10283 dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10284 break;
10286 case LI_set_line:
10287 if (ent->val == current_line)
10289 /* We still need to start a new row, so output a copy insn. */
10290 dw2_asm_output_data (1, DW_LNS_copy,
10291 "copy line %u", current_line);
10293 else
10295 int line_offset = ent->val - current_line;
10296 int line_delta = line_offset - DWARF_LINE_BASE;
10298 current_line = ent->val;
10299 if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10301 /* This can handle deltas from -10 to 234, using the current
10302 definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
10303 This takes 1 byte. */
10304 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10305 "line %u", current_line);
10307 else
10309 /* This can handle any delta. This takes at least 4 bytes,
10310 depending on the value being encoded. */
10311 dw2_asm_output_data (1, DW_LNS_advance_line,
10312 "advance to line %u", current_line);
10313 dw2_asm_output_data_sleb128 (line_offset, NULL);
10314 dw2_asm_output_data (1, DW_LNS_copy, NULL);
10317 break;
10319 case LI_set_file:
10320 dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
10321 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10322 break;
10324 case LI_set_column:
10325 dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
10326 dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
10327 break;
10329 case LI_negate_stmt:
10330 current_is_stmt = !current_is_stmt;
10331 dw2_asm_output_data (1, DW_LNS_negate_stmt,
10332 "is_stmt %d", current_is_stmt);
10333 break;
10335 case LI_set_prologue_end:
10336 dw2_asm_output_data (1, DW_LNS_set_prologue_end,
10337 "set prologue end");
10338 break;
10340 case LI_set_epilogue_begin:
10341 dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
10342 "set epilogue begin");
10343 break;
10345 case LI_set_discriminator:
10346 dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
10347 dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
10348 dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
10349 dw2_asm_output_data_uleb128 (ent->val, NULL);
10350 break;
10354 /* Emit debug info for the address of the end of the table. */
10355 dw2_asm_output_data (1, 0, "set address %s", table->end_label);
10356 dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10357 dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10358 dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
10360 dw2_asm_output_data (1, 0, "end sequence");
10361 dw2_asm_output_data_uleb128 (1, NULL);
10362 dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10365 /* Output the source line number correspondence information. This
10366 information goes into the .debug_line section. */
10368 static void
10369 output_line_info (bool prologue_only)
10371 char l1[20], l2[20], p1[20], p2[20];
10372 /* We don't support DWARFv5 line tables yet. */
10373 int ver = dwarf_version < 5 ? dwarf_version : 4;
10374 bool saw_one = false;
10375 int opc;
10377 ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
10378 ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
10379 ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
10380 ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
10382 if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10383 dw2_asm_output_data (4, 0xffffffff,
10384 "Initial length escape value indicating 64-bit DWARF extension");
10385 dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
10386 "Length of Source Line Info");
10387 ASM_OUTPUT_LABEL (asm_out_file, l1);
10389 dw2_asm_output_data (2, ver, "DWARF Version");
10390 dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10391 ASM_OUTPUT_LABEL (asm_out_file, p1);
10393 /* Define the architecture-dependent minimum instruction length (in bytes).
10394 In this implementation of DWARF, this field is used for information
10395 purposes only. Since GCC generates assembly language, we have no
10396 a priori knowledge of how many instruction bytes are generated for each
10397 source line, and therefore can use only the DW_LNE_set_address and
10398 DW_LNS_fixed_advance_pc line information commands. Accordingly, we fix
10399 this as '1', which is "correct enough" for all architectures,
10400 and don't let the target override. */
10401 dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10403 if (ver >= 4)
10404 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10405 "Maximum Operations Per Instruction");
10406 dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10407 "Default is_stmt_start flag");
10408 dw2_asm_output_data (1, DWARF_LINE_BASE,
10409 "Line Base Value (Special Opcodes)");
10410 dw2_asm_output_data (1, DWARF_LINE_RANGE,
10411 "Line Range Value (Special Opcodes)");
10412 dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10413 "Special Opcode Base");
10415 for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10417 int n_op_args;
10418 switch (opc)
10420 case DW_LNS_advance_pc:
10421 case DW_LNS_advance_line:
10422 case DW_LNS_set_file:
10423 case DW_LNS_set_column:
10424 case DW_LNS_fixed_advance_pc:
10425 case DW_LNS_set_isa:
10426 n_op_args = 1;
10427 break;
10428 default:
10429 n_op_args = 0;
10430 break;
10433 dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10434 opc, n_op_args);
10437 /* Write out the information about the files we use. */
10438 output_file_names ();
10439 ASM_OUTPUT_LABEL (asm_out_file, p2);
10440 if (prologue_only)
10442 /* Output the marker for the end of the line number info. */
10443 ASM_OUTPUT_LABEL (asm_out_file, l2);
10444 return;
10447 if (separate_line_info)
10449 dw_line_info_table *table;
10450 size_t i;
10452 FOR_EACH_VEC_ELT (*separate_line_info, i, table)
10453 if (table->in_use)
10455 output_one_line_info_table (table);
10456 saw_one = true;
10459 if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10461 output_one_line_info_table (cold_text_section_line_info);
10462 saw_one = true;
10465 /* ??? Some Darwin linkers crash on a .debug_line section with no
10466 sequences. Further, merely a DW_LNE_end_sequence entry is not
10467 sufficient -- the address column must also be initialized.
10468 Make sure to output at least one set_address/end_sequence pair,
10469 choosing .text since that section is always present. */
10470 if (text_section_line_info->in_use || !saw_one)
10471 output_one_line_info_table (text_section_line_info);
10473 /* Output the marker for the end of the line number info. */
10474 ASM_OUTPUT_LABEL (asm_out_file, l2);
10477 /* Given a pointer to a tree node for some base type, return a pointer to
10478 a DIE that describes the given type.
10480 This routine must only be called for GCC type nodes that correspond to
10481 Dwarf base (fundamental) types. */
10483 static dw_die_ref
10484 base_type_die (tree type)
10486 dw_die_ref base_type_result;
10487 enum dwarf_type encoding;
10489 if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10490 return 0;
10492 /* If this is a subtype that should not be emitted as a subrange type,
10493 use the base type. See subrange_type_for_debug_p. */
10494 if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10495 type = TREE_TYPE (type);
10497 switch (TREE_CODE (type))
10499 case INTEGER_TYPE:
10500 if ((dwarf_version >= 4 || !dwarf_strict)
10501 && TYPE_NAME (type)
10502 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10503 && DECL_IS_BUILTIN (TYPE_NAME (type))
10504 && DECL_NAME (TYPE_NAME (type)))
10506 const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10507 if (strcmp (name, "char16_t") == 0
10508 || strcmp (name, "char32_t") == 0)
10510 encoding = DW_ATE_UTF;
10511 break;
10514 if (TYPE_STRING_FLAG (type))
10516 if (TYPE_UNSIGNED (type))
10517 encoding = DW_ATE_unsigned_char;
10518 else
10519 encoding = DW_ATE_signed_char;
10521 else if (TYPE_UNSIGNED (type))
10522 encoding = DW_ATE_unsigned;
10523 else
10524 encoding = DW_ATE_signed;
10525 break;
10527 case REAL_TYPE:
10528 if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10530 if (dwarf_version >= 3 || !dwarf_strict)
10531 encoding = DW_ATE_decimal_float;
10532 else
10533 encoding = DW_ATE_lo_user;
10535 else
10536 encoding = DW_ATE_float;
10537 break;
10539 case FIXED_POINT_TYPE:
10540 if (!(dwarf_version >= 3 || !dwarf_strict))
10541 encoding = DW_ATE_lo_user;
10542 else if (TYPE_UNSIGNED (type))
10543 encoding = DW_ATE_unsigned_fixed;
10544 else
10545 encoding = DW_ATE_signed_fixed;
10546 break;
10548 /* Dwarf2 doesn't know anything about complex ints, so use
10549 a user defined type for it. */
10550 case COMPLEX_TYPE:
10551 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10552 encoding = DW_ATE_complex_float;
10553 else
10554 encoding = DW_ATE_lo_user;
10555 break;
10557 case BOOLEAN_TYPE:
10558 /* GNU FORTRAN/Ada/C++ BOOLEAN type. */
10559 encoding = DW_ATE_boolean;
10560 break;
10562 default:
10563 /* No other TREE_CODEs are Dwarf fundamental types. */
10564 gcc_unreachable ();
10567 base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10569 add_AT_unsigned (base_type_result, DW_AT_byte_size,
10570 int_size_in_bytes (type));
10571 add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10572 add_pubtype (type, base_type_result);
10574 return base_type_result;
10577 /* A C++ function with deduced return type can have a TEMPLATE_TYPE_PARM
10578 named 'auto' in its type: return true for it, false otherwise. */
10580 static inline bool
10581 is_cxx_auto (tree type)
10583 if (is_cxx ())
10585 tree name = TYPE_IDENTIFIER (type);
10586 if (name == get_identifier ("auto")
10587 || name == get_identifier ("decltype(auto)"))
10588 return true;
10590 return false;
10593 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10594 given input type is a Dwarf "fundamental" type. Otherwise return null. */
10596 static inline int
10597 is_base_type (tree type)
10599 switch (TREE_CODE (type))
10601 case ERROR_MARK:
10602 case VOID_TYPE:
10603 case INTEGER_TYPE:
10604 case REAL_TYPE:
10605 case FIXED_POINT_TYPE:
10606 case COMPLEX_TYPE:
10607 case BOOLEAN_TYPE:
10608 case POINTER_BOUNDS_TYPE:
10609 return 1;
10611 case ARRAY_TYPE:
10612 case RECORD_TYPE:
10613 case UNION_TYPE:
10614 case QUAL_UNION_TYPE:
10615 case ENUMERAL_TYPE:
10616 case FUNCTION_TYPE:
10617 case METHOD_TYPE:
10618 case POINTER_TYPE:
10619 case REFERENCE_TYPE:
10620 case NULLPTR_TYPE:
10621 case OFFSET_TYPE:
10622 case LANG_TYPE:
10623 case VECTOR_TYPE:
10624 return 0;
10626 default:
10627 if (is_cxx_auto (type))
10628 return 0;
10629 gcc_unreachable ();
10632 return 0;
10635 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10636 node, return the size in bits for the type if it is a constant, or else
10637 return the alignment for the type if the type's size is not constant, or
10638 else return BITS_PER_WORD if the type actually turns out to be an
10639 ERROR_MARK node. */
10641 static inline unsigned HOST_WIDE_INT
10642 simple_type_size_in_bits (const_tree type)
10644 if (TREE_CODE (type) == ERROR_MARK)
10645 return BITS_PER_WORD;
10646 else if (TYPE_SIZE (type) == NULL_TREE)
10647 return 0;
10648 else if (tree_fits_uhwi_p (TYPE_SIZE (type)))
10649 return tree_to_uhwi (TYPE_SIZE (type));
10650 else
10651 return TYPE_ALIGN (type);
10654 /* Similarly, but return an offset_int instead of UHWI. */
10656 static inline offset_int
10657 offset_int_type_size_in_bits (const_tree type)
10659 if (TREE_CODE (type) == ERROR_MARK)
10660 return BITS_PER_WORD;
10661 else if (TYPE_SIZE (type) == NULL_TREE)
10662 return 0;
10663 else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10664 return wi::to_offset (TYPE_SIZE (type));
10665 else
10666 return TYPE_ALIGN (type);
10669 /* Given a pointer to a tree node for a subrange type, return a pointer
10670 to a DIE that describes the given type. */
10672 static dw_die_ref
10673 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10675 dw_die_ref subrange_die;
10676 const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10678 if (context_die == NULL)
10679 context_die = comp_unit_die ();
10681 subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10683 if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10685 /* The size of the subrange type and its base type do not match,
10686 so we need to generate a size attribute for the subrange type. */
10687 add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10690 if (low)
10691 add_bound_info (subrange_die, DW_AT_lower_bound, low, NULL);
10692 if (high)
10693 add_bound_info (subrange_die, DW_AT_upper_bound, high, NULL);
10695 return subrange_die;
10698 /* Returns the (const and/or volatile) cv_qualifiers associated with
10699 the decl node. This will normally be augmented with the
10700 cv_qualifiers of the underlying type in add_type_attribute. */
10702 static int
10703 decl_quals (const_tree decl)
10705 return ((TREE_READONLY (decl)
10706 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED)
10707 | (TREE_THIS_VOLATILE (decl)
10708 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED));
10711 /* Determine the TYPE whose qualifiers match the largest strict subset
10712 of the given TYPE_QUALS, and return its qualifiers. Ignore all
10713 qualifiers outside QUAL_MASK. */
10715 static int
10716 get_nearest_type_subqualifiers (tree type, int type_quals, int qual_mask)
10718 tree t;
10719 int best_rank = 0, best_qual = 0, max_rank;
10721 type_quals &= qual_mask;
10722 max_rank = popcount_hwi (type_quals) - 1;
10724 for (t = TYPE_MAIN_VARIANT (type); t && best_rank < max_rank;
10725 t = TYPE_NEXT_VARIANT (t))
10727 int q = TYPE_QUALS (t) & qual_mask;
10729 if ((q & type_quals) == q && q != type_quals
10730 && check_base_type (t, type))
10732 int rank = popcount_hwi (q);
10734 if (rank > best_rank)
10736 best_rank = rank;
10737 best_qual = q;
10742 return best_qual;
10745 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10746 entry that chains various modifiers in front of the given type. */
10748 static dw_die_ref
10749 modified_type_die (tree type, int cv_quals, dw_die_ref context_die)
10751 enum tree_code code = TREE_CODE (type);
10752 dw_die_ref mod_type_die;
10753 dw_die_ref sub_die = NULL;
10754 tree item_type = NULL;
10755 tree qualified_type;
10756 tree name, low, high;
10757 dw_die_ref mod_scope;
10758 /* Only these cv-qualifiers are currently handled. */
10759 const int cv_qual_mask = (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE
10760 | TYPE_QUAL_RESTRICT | TYPE_QUAL_ATOMIC);
10762 if (code == ERROR_MARK)
10763 return NULL;
10765 cv_quals &= cv_qual_mask;
10767 /* Don't emit DW_TAG_restrict_type for DWARFv2, since it is a type
10768 tag modifier (and not an attribute) old consumers won't be able
10769 to handle it. */
10770 if (dwarf_version < 3)
10771 cv_quals &= ~TYPE_QUAL_RESTRICT;
10773 /* Likewise for DW_TAG_atomic_type for DWARFv5. */
10774 if (dwarf_version < 5)
10775 cv_quals &= ~TYPE_QUAL_ATOMIC;
10777 /* See if we already have the appropriately qualified variant of
10778 this type. */
10779 qualified_type = get_qualified_type (type, cv_quals);
10781 if (qualified_type == sizetype
10782 && TYPE_NAME (qualified_type)
10783 && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10785 tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10787 gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10788 && TYPE_PRECISION (t)
10789 == TYPE_PRECISION (qualified_type)
10790 && TYPE_UNSIGNED (t)
10791 == TYPE_UNSIGNED (qualified_type));
10792 qualified_type = t;
10795 /* If we do, then we can just use its DIE, if it exists. */
10796 if (qualified_type)
10798 mod_type_die = lookup_type_die (qualified_type);
10799 if (mod_type_die)
10800 return mod_type_die;
10803 name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10805 /* Handle C typedef types. */
10806 if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10807 && !DECL_ARTIFICIAL (name))
10809 tree dtype = TREE_TYPE (name);
10811 if (qualified_type == dtype)
10813 /* For a named type, use the typedef. */
10814 gen_type_die (qualified_type, context_die);
10815 return lookup_type_die (qualified_type);
10817 else
10819 int dquals = TYPE_QUALS_NO_ADDR_SPACE (dtype);
10820 dquals &= cv_qual_mask;
10821 if ((dquals & ~cv_quals) != TYPE_UNQUALIFIED
10822 || (cv_quals == dquals && DECL_ORIGINAL_TYPE (name) != type))
10823 /* cv-unqualified version of named type. Just use
10824 the unnamed type to which it refers. */
10825 return modified_type_die (DECL_ORIGINAL_TYPE (name),
10826 cv_quals, context_die);
10827 /* Else cv-qualified version of named type; fall through. */
10831 mod_scope = scope_die_for (type, context_die);
10833 if (cv_quals)
10835 struct qual_info { int q; enum dwarf_tag t; };
10836 static const struct qual_info qual_info[] =
10838 { TYPE_QUAL_ATOMIC, DW_TAG_atomic_type },
10839 { TYPE_QUAL_RESTRICT, DW_TAG_restrict_type },
10840 { TYPE_QUAL_VOLATILE, DW_TAG_volatile_type },
10841 { TYPE_QUAL_CONST, DW_TAG_const_type },
10843 int sub_quals;
10844 unsigned i;
10846 /* Determine a lesser qualified type that most closely matches
10847 this one. Then generate DW_TAG_* entries for the remaining
10848 qualifiers. */
10849 sub_quals = get_nearest_type_subqualifiers (type, cv_quals,
10850 cv_qual_mask);
10851 mod_type_die = modified_type_die (type, sub_quals, context_die);
10853 for (i = 0; i < sizeof (qual_info) / sizeof (qual_info[0]); i++)
10854 if (qual_info[i].q & cv_quals & ~sub_quals)
10856 dw_die_ref d = new_die (qual_info[i].t, mod_scope, type);
10857 if (mod_type_die)
10858 add_AT_die_ref (d, DW_AT_type, mod_type_die);
10859 mod_type_die = d;
10862 else if (code == POINTER_TYPE)
10864 mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10865 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10866 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10867 item_type = TREE_TYPE (type);
10868 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10869 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10870 TYPE_ADDR_SPACE (item_type));
10872 else if (code == REFERENCE_TYPE)
10874 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10875 mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10876 type);
10877 else
10878 mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10879 add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10880 simple_type_size_in_bits (type) / BITS_PER_UNIT);
10881 item_type = TREE_TYPE (type);
10882 if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10883 add_AT_unsigned (mod_type_die, DW_AT_address_class,
10884 TYPE_ADDR_SPACE (item_type));
10886 else if (code == INTEGER_TYPE
10887 && TREE_TYPE (type) != NULL_TREE
10888 && subrange_type_for_debug_p (type, &low, &high))
10890 mod_type_die = subrange_type_die (type, low, high, context_die);
10891 item_type = TREE_TYPE (type);
10893 else if (is_base_type (type))
10894 mod_type_die = base_type_die (type);
10895 else
10897 gen_type_die (type, context_die);
10899 /* We have to get the type_main_variant here (and pass that to the
10900 `lookup_type_die' routine) because the ..._TYPE node we have
10901 might simply be a *copy* of some original type node (where the
10902 copy was created to help us keep track of typedef names) and
10903 that copy might have a different TYPE_UID from the original
10904 ..._TYPE node. */
10905 if (TREE_CODE (type) != VECTOR_TYPE)
10906 return lookup_type_die (type_main_variant (type));
10907 else
10908 /* Vectors have the debugging information in the type,
10909 not the main variant. */
10910 return lookup_type_die (type);
10913 /* Builtin types don't have a DECL_ORIGINAL_TYPE. For those,
10914 don't output a DW_TAG_typedef, since there isn't one in the
10915 user's program; just attach a DW_AT_name to the type.
10916 Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10917 if the base type already has the same name. */
10918 if (name
10919 && ((TREE_CODE (name) != TYPE_DECL
10920 && (qualified_type == TYPE_MAIN_VARIANT (type)
10921 || (cv_quals == TYPE_UNQUALIFIED)))
10922 || (TREE_CODE (name) == TYPE_DECL
10923 && TREE_TYPE (name) == qualified_type
10924 && DECL_NAME (name))))
10926 if (TREE_CODE (name) == TYPE_DECL)
10927 /* Could just call add_name_and_src_coords_attributes here,
10928 but since this is a builtin type it doesn't have any
10929 useful source coordinates anyway. */
10930 name = DECL_NAME (name);
10931 add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10933 /* This probably indicates a bug. */
10934 else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10936 name = TYPE_IDENTIFIER (type);
10937 add_name_attribute (mod_type_die,
10938 name ? IDENTIFIER_POINTER (name) : "__unknown__");
10941 if (qualified_type)
10942 equate_type_number_to_die (qualified_type, mod_type_die);
10944 if (item_type)
10945 /* We must do this after the equate_type_number_to_die call, in case
10946 this is a recursive type. This ensures that the modified_type_die
10947 recursion will terminate even if the type is recursive. Recursive
10948 types are possible in Ada. */
10949 sub_die = modified_type_die (item_type,
10950 TYPE_QUALS_NO_ADDR_SPACE (item_type),
10951 context_die);
10953 if (sub_die != NULL)
10954 add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10956 add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10957 if (TYPE_ARTIFICIAL (type))
10958 add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10960 return mod_type_die;
10963 /* Generate DIEs for the generic parameters of T.
10964 T must be either a generic type or a generic function.
10965 See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more. */
10967 static void
10968 gen_generic_params_dies (tree t)
10970 tree parms, args;
10971 int parms_num, i;
10972 dw_die_ref die = NULL;
10973 int non_default;
10975 if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10976 return;
10978 if (TYPE_P (t))
10979 die = lookup_type_die (t);
10980 else if (DECL_P (t))
10981 die = lookup_decl_die (t);
10983 gcc_assert (die);
10985 parms = lang_hooks.get_innermost_generic_parms (t);
10986 if (!parms)
10987 /* T has no generic parameter. It means T is neither a generic type
10988 or function. End of story. */
10989 return;
10991 parms_num = TREE_VEC_LENGTH (parms);
10992 args = lang_hooks.get_innermost_generic_args (t);
10993 if (TREE_CHAIN (args) && TREE_CODE (TREE_CHAIN (args)) == INTEGER_CST)
10994 non_default = int_cst_value (TREE_CHAIN (args));
10995 else
10996 non_default = TREE_VEC_LENGTH (args);
10997 for (i = 0; i < parms_num; i++)
10999 tree parm, arg, arg_pack_elems;
11000 dw_die_ref parm_die;
11002 parm = TREE_VEC_ELT (parms, i);
11003 arg = TREE_VEC_ELT (args, i);
11004 arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
11005 gcc_assert (parm && TREE_VALUE (parm) && arg);
11007 if (parm && TREE_VALUE (parm) && arg)
11009 /* If PARM represents a template parameter pack,
11010 emit a DW_TAG_GNU_template_parameter_pack DIE, followed
11011 by DW_TAG_template_*_parameter DIEs for the argument
11012 pack elements of ARG. Note that ARG would then be
11013 an argument pack. */
11014 if (arg_pack_elems)
11015 parm_die = template_parameter_pack_die (TREE_VALUE (parm),
11016 arg_pack_elems,
11017 die);
11018 else
11019 parm_die = generic_parameter_die (TREE_VALUE (parm), arg,
11020 true /* emit name */, die);
11021 if (i >= non_default)
11022 add_AT_flag (parm_die, DW_AT_default_value, 1);
11027 /* Create and return a DIE for PARM which should be
11028 the representation of a generic type parameter.
11029 For instance, in the C++ front end, PARM would be a template parameter.
11030 ARG is the argument to PARM.
11031 EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
11032 name of the PARM.
11033 PARENT_DIE is the parent DIE which the new created DIE should be added to,
11034 as a child node. */
11036 static dw_die_ref
11037 generic_parameter_die (tree parm, tree arg,
11038 bool emit_name_p,
11039 dw_die_ref parent_die)
11041 dw_die_ref tmpl_die = NULL;
11042 const char *name = NULL;
11044 if (!parm || !DECL_NAME (parm) || !arg)
11045 return NULL;
11047 /* We support non-type generic parameters and arguments,
11048 type generic parameters and arguments, as well as
11049 generic generic parameters (a.k.a. template template parameters in C++)
11050 and arguments. */
11051 if (TREE_CODE (parm) == PARM_DECL)
11052 /* PARM is a nontype generic parameter */
11053 tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
11054 else if (TREE_CODE (parm) == TYPE_DECL)
11055 /* PARM is a type generic parameter. */
11056 tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
11057 else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11058 /* PARM is a generic generic parameter.
11059 Its DIE is a GNU extension. It shall have a
11060 DW_AT_name attribute to represent the name of the template template
11061 parameter, and a DW_AT_GNU_template_name attribute to represent the
11062 name of the template template argument. */
11063 tmpl_die = new_die (DW_TAG_GNU_template_template_param,
11064 parent_die, parm);
11065 else
11066 gcc_unreachable ();
11068 if (tmpl_die)
11070 tree tmpl_type;
11072 /* If PARM is a generic parameter pack, it means we are
11073 emitting debug info for a template argument pack element.
11074 In other terms, ARG is a template argument pack element.
11075 In that case, we don't emit any DW_AT_name attribute for
11076 the die. */
11077 if (emit_name_p)
11079 name = IDENTIFIER_POINTER (DECL_NAME (parm));
11080 gcc_assert (name);
11081 add_AT_string (tmpl_die, DW_AT_name, name);
11084 if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
11086 /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
11087 TMPL_DIE should have a child DW_AT_type attribute that is set
11088 to the type of the argument to PARM, which is ARG.
11089 If PARM is a type generic parameter, TMPL_DIE should have a
11090 child DW_AT_type that is set to ARG. */
11091 tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
11092 add_type_attribute (tmpl_die, tmpl_type,
11093 (TREE_THIS_VOLATILE (tmpl_type)
11094 ? TYPE_QUAL_VOLATILE : TYPE_UNQUALIFIED),
11095 parent_die);
11097 else
11099 /* So TMPL_DIE is a DIE representing a
11100 a generic generic template parameter, a.k.a template template
11101 parameter in C++ and arg is a template. */
11103 /* The DW_AT_GNU_template_name attribute of the DIE must be set
11104 to the name of the argument. */
11105 name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
11106 if (name)
11107 add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
11110 if (TREE_CODE (parm) == PARM_DECL)
11111 /* So PARM is a non-type generic parameter.
11112 DWARF3 5.6.8 says we must set a DW_AT_const_value child
11113 attribute of TMPL_DIE which value represents the value
11114 of ARG.
11115 We must be careful here:
11116 The value of ARG might reference some function decls.
11117 We might currently be emitting debug info for a generic
11118 type and types are emitted before function decls, we don't
11119 know if the function decls referenced by ARG will actually be
11120 emitted after cgraph computations.
11121 So must defer the generation of the DW_AT_const_value to
11122 after cgraph is ready. */
11123 append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
11126 return tmpl_die;
11129 /* Generate and return a DW_TAG_GNU_template_parameter_pack DIE representing.
11130 PARM_PACK must be a template parameter pack. The returned DIE
11131 will be child DIE of PARENT_DIE. */
11133 static dw_die_ref
11134 template_parameter_pack_die (tree parm_pack,
11135 tree parm_pack_args,
11136 dw_die_ref parent_die)
11138 dw_die_ref die;
11139 int j;
11141 gcc_assert (parent_die && parm_pack);
11143 die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
11144 add_name_and_src_coords_attributes (die, parm_pack);
11145 for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
11146 generic_parameter_die (parm_pack,
11147 TREE_VEC_ELT (parm_pack_args, j),
11148 false /* Don't emit DW_AT_name */,
11149 die);
11150 return die;
11153 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
11154 an enumerated type. */
11156 static inline int
11157 type_is_enum (const_tree type)
11159 return TREE_CODE (type) == ENUMERAL_TYPE;
11162 /* Return the DBX register number described by a given RTL node. */
11164 static unsigned int
11165 dbx_reg_number (const_rtx rtl)
11167 unsigned regno = REGNO (rtl);
11169 gcc_assert (regno < FIRST_PSEUDO_REGISTER);
11171 #ifdef LEAF_REG_REMAP
11172 if (crtl->uses_only_leaf_regs)
11174 int leaf_reg = LEAF_REG_REMAP (regno);
11175 if (leaf_reg != -1)
11176 regno = (unsigned) leaf_reg;
11178 #endif
11180 regno = DBX_REGISTER_NUMBER (regno);
11181 gcc_assert (regno != INVALID_REGNUM);
11182 return regno;
11185 /* Optionally add a DW_OP_piece term to a location description expression.
11186 DW_OP_piece is only added if the location description expression already
11187 doesn't end with DW_OP_piece. */
11189 static void
11190 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
11192 dw_loc_descr_ref loc;
11194 if (*list_head != NULL)
11196 /* Find the end of the chain. */
11197 for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
11200 if (loc->dw_loc_opc != DW_OP_piece)
11201 loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
11205 /* Return a location descriptor that designates a machine register or
11206 zero if there is none. */
11208 static dw_loc_descr_ref
11209 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
11211 rtx regs;
11213 if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
11214 return 0;
11216 /* We only use "frame base" when we're sure we're talking about the
11217 post-prologue local stack frame. We do this by *not* running
11218 register elimination until this point, and recognizing the special
11219 argument pointer and soft frame pointer rtx's.
11220 Use DW_OP_fbreg offset DW_OP_stack_value in this case. */
11221 if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
11222 && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
11224 dw_loc_descr_ref result = NULL;
11226 if (dwarf_version >= 4 || !dwarf_strict)
11228 result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
11229 initialized);
11230 if (result)
11231 add_loc_descr (&result,
11232 new_loc_descr (DW_OP_stack_value, 0, 0));
11234 return result;
11237 regs = targetm.dwarf_register_span (rtl);
11239 if (REG_NREGS (rtl) > 1 || regs)
11240 return multiple_reg_loc_descriptor (rtl, regs, initialized);
11241 else
11243 unsigned int dbx_regnum = dbx_reg_number (rtl);
11244 if (dbx_regnum == IGNORED_DWARF_REGNUM)
11245 return 0;
11246 return one_reg_loc_descriptor (dbx_regnum, initialized);
11250 /* Return a location descriptor that designates a machine register for
11251 a given hard register number. */
11253 static dw_loc_descr_ref
11254 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
11256 dw_loc_descr_ref reg_loc_descr;
11258 if (regno <= 31)
11259 reg_loc_descr
11260 = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
11261 else
11262 reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
11264 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11265 add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11267 return reg_loc_descr;
11270 /* Given an RTL of a register, return a location descriptor that
11271 designates a value that spans more than one register. */
11273 static dw_loc_descr_ref
11274 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
11275 enum var_init_status initialized)
11277 int size, i;
11278 dw_loc_descr_ref loc_result = NULL;
11280 /* Simple, contiguous registers. */
11281 if (regs == NULL_RTX)
11283 unsigned reg = REGNO (rtl);
11284 int nregs;
11286 #ifdef LEAF_REG_REMAP
11287 if (crtl->uses_only_leaf_regs)
11289 int leaf_reg = LEAF_REG_REMAP (reg);
11290 if (leaf_reg != -1)
11291 reg = (unsigned) leaf_reg;
11293 #endif
11295 gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
11296 nregs = REG_NREGS (rtl);
11298 size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
11300 loc_result = NULL;
11301 while (nregs--)
11303 dw_loc_descr_ref t;
11305 t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
11306 VAR_INIT_STATUS_INITIALIZED);
11307 add_loc_descr (&loc_result, t);
11308 add_loc_descr_op_piece (&loc_result, size);
11309 ++reg;
11311 return loc_result;
11314 /* Now onto stupid register sets in non contiguous locations. */
11316 gcc_assert (GET_CODE (regs) == PARALLEL);
11318 size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
11319 loc_result = NULL;
11321 for (i = 0; i < XVECLEN (regs, 0); ++i)
11323 dw_loc_descr_ref t;
11325 t = one_reg_loc_descriptor (dbx_reg_number (XVECEXP (regs, 0, i)),
11326 VAR_INIT_STATUS_INITIALIZED);
11327 add_loc_descr (&loc_result, t);
11328 add_loc_descr_op_piece (&loc_result, size);
11331 if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11332 add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11333 return loc_result;
11336 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
11338 /* Return a location descriptor that designates a constant i,
11339 as a compound operation from constant (i >> shift), constant shift
11340 and DW_OP_shl. */
11342 static dw_loc_descr_ref
11343 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11345 dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
11346 add_loc_descr (&ret, int_loc_descriptor (shift));
11347 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11348 return ret;
11351 /* Return a location descriptor that designates a constant. */
11353 static dw_loc_descr_ref
11354 int_loc_descriptor (HOST_WIDE_INT i)
11356 enum dwarf_location_atom op;
11358 /* Pick the smallest representation of a constant, rather than just
11359 defaulting to the LEB encoding. */
11360 if (i >= 0)
11362 int clz = clz_hwi (i);
11363 int ctz = ctz_hwi (i);
11364 if (i <= 31)
11365 op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
11366 else if (i <= 0xff)
11367 op = DW_OP_const1u;
11368 else if (i <= 0xffff)
11369 op = DW_OP_const2u;
11370 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11371 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11372 /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
11373 DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
11374 while DW_OP_const4u is 5 bytes. */
11375 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
11376 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11377 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11378 /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
11379 while DW_OP_const4u is 5 bytes. */
11380 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11381 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11382 op = DW_OP_const4u;
11383 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11384 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11385 /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
11386 while DW_OP_constu of constant >= 0x100000000 takes at least
11387 6 bytes. */
11388 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
11389 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11390 && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
11391 >= HOST_BITS_PER_WIDE_INT)
11392 /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
11393 DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
11394 while DW_OP_constu takes in this case at least 6 bytes. */
11395 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
11396 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11397 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11398 && size_of_uleb128 (i) > 6)
11399 /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes. */
11400 return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
11401 else
11402 op = DW_OP_constu;
11404 else
11406 if (i >= -0x80)
11407 op = DW_OP_const1s;
11408 else if (i >= -0x8000)
11409 op = DW_OP_const2s;
11410 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11412 if (size_of_int_loc_descriptor (i) < 5)
11414 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11415 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11416 return ret;
11418 op = DW_OP_const4s;
11420 else
11422 if (size_of_int_loc_descriptor (i)
11423 < (unsigned long) 1 + size_of_sleb128 (i))
11425 dw_loc_descr_ref ret = int_loc_descriptor (-i);
11426 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11427 return ret;
11429 op = DW_OP_consts;
11433 return new_loc_descr (op, i, 0);
11436 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
11437 without actually allocating it. */
11439 static unsigned long
11440 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
11442 return size_of_int_loc_descriptor (i >> shift)
11443 + size_of_int_loc_descriptor (shift)
11444 + 1;
11447 /* Return size_of_locs (int_loc_descriptor (i)) without
11448 actually allocating it. */
11450 static unsigned long
11451 size_of_int_loc_descriptor (HOST_WIDE_INT i)
11453 unsigned long s;
11455 if (i >= 0)
11457 int clz, ctz;
11458 if (i <= 31)
11459 return 1;
11460 else if (i <= 0xff)
11461 return 2;
11462 else if (i <= 0xffff)
11463 return 3;
11464 clz = clz_hwi (i);
11465 ctz = ctz_hwi (i);
11466 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
11467 && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
11468 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11469 - clz - 5);
11470 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11471 && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
11472 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11473 - clz - 8);
11474 else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
11475 return 5;
11476 s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11477 if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
11478 && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
11479 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11480 - clz - 8);
11481 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
11482 && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
11483 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11484 - clz - 16);
11485 else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
11486 && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
11487 && s > 6)
11488 return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
11489 - clz - 32);
11490 else
11491 return 1 + s;
11493 else
11495 if (i >= -0x80)
11496 return 2;
11497 else if (i >= -0x8000)
11498 return 3;
11499 else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
11501 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11503 s = size_of_int_loc_descriptor (-i) + 1;
11504 if (s < 5)
11505 return s;
11507 return 5;
11509 else
11511 unsigned long r = 1 + size_of_sleb128 (i);
11512 if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
11514 s = size_of_int_loc_descriptor (-i) + 1;
11515 if (s < r)
11516 return s;
11518 return r;
11523 /* Return loc description representing "address" of integer value.
11524 This can appear only as toplevel expression. */
11526 static dw_loc_descr_ref
11527 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
11529 int litsize;
11530 dw_loc_descr_ref loc_result = NULL;
11532 if (!(dwarf_version >= 4 || !dwarf_strict))
11533 return NULL;
11535 litsize = size_of_int_loc_descriptor (i);
11536 /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11537 is more compact. For DW_OP_stack_value we need:
11538 litsize + 1 (DW_OP_stack_value)
11539 and for DW_OP_implicit_value:
11540 1 (DW_OP_implicit_value) + 1 (length) + size. */
11541 if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
11543 loc_result = int_loc_descriptor (i);
11544 add_loc_descr (&loc_result,
11545 new_loc_descr (DW_OP_stack_value, 0, 0));
11546 return loc_result;
11549 loc_result = new_loc_descr (DW_OP_implicit_value,
11550 size, 0);
11551 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11552 loc_result->dw_loc_oprnd2.v.val_int = i;
11553 return loc_result;
11556 /* Return a location descriptor that designates a base+offset location. */
11558 static dw_loc_descr_ref
11559 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11560 enum var_init_status initialized)
11562 unsigned int regno;
11563 dw_loc_descr_ref result;
11564 dw_fde_ref fde = cfun->fde;
11566 /* We only use "frame base" when we're sure we're talking about the
11567 post-prologue local stack frame. We do this by *not* running
11568 register elimination until this point, and recognizing the special
11569 argument pointer and soft frame pointer rtx's. */
11570 if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11572 rtx elim = (ira_use_lra_p
11573 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
11574 : eliminate_regs (reg, VOIDmode, NULL_RTX));
11576 if (elim != reg)
11578 if (GET_CODE (elim) == PLUS)
11580 offset += INTVAL (XEXP (elim, 1));
11581 elim = XEXP (elim, 0);
11583 gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11584 && (elim == hard_frame_pointer_rtx
11585 || elim == stack_pointer_rtx))
11586 || elim == (frame_pointer_needed
11587 ? hard_frame_pointer_rtx
11588 : stack_pointer_rtx));
11590 /* If drap register is used to align stack, use frame
11591 pointer + offset to access stack variables. If stack
11592 is aligned without drap, use stack pointer + offset to
11593 access stack variables. */
11594 if (crtl->stack_realign_tried
11595 && reg == frame_pointer_rtx)
11597 int base_reg
11598 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11599 ? HARD_FRAME_POINTER_REGNUM
11600 : REGNO (elim));
11601 return new_reg_loc_descr (base_reg, offset);
11604 gcc_assert (frame_pointer_fb_offset_valid);
11605 offset += frame_pointer_fb_offset;
11606 return new_loc_descr (DW_OP_fbreg, offset, 0);
11610 regno = REGNO (reg);
11611 #ifdef LEAF_REG_REMAP
11612 if (crtl->uses_only_leaf_regs)
11614 int leaf_reg = LEAF_REG_REMAP (regno);
11615 if (leaf_reg != -1)
11616 regno = (unsigned) leaf_reg;
11618 #endif
11619 regno = DWARF_FRAME_REGNUM (regno);
11621 if (!optimize && fde
11622 && (fde->drap_reg == regno || fde->vdrap_reg == regno))
11624 /* Use cfa+offset to represent the location of arguments passed
11625 on the stack when drap is used to align stack.
11626 Only do this when not optimizing, for optimized code var-tracking
11627 is supposed to track where the arguments live and the register
11628 used as vdrap or drap in some spot might be used for something
11629 else in other part of the routine. */
11630 return new_loc_descr (DW_OP_fbreg, offset, 0);
11633 if (regno <= 31)
11634 result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11635 offset, 0);
11636 else
11637 result = new_loc_descr (DW_OP_bregx, regno, offset);
11639 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11640 add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11642 return result;
11645 /* Return true if this RTL expression describes a base+offset calculation. */
11647 static inline int
11648 is_based_loc (const_rtx rtl)
11650 return (GET_CODE (rtl) == PLUS
11651 && ((REG_P (XEXP (rtl, 0))
11652 && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11653 && CONST_INT_P (XEXP (rtl, 1)))));
11656 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11657 failed. */
11659 static dw_loc_descr_ref
11660 tls_mem_loc_descriptor (rtx mem)
11662 tree base;
11663 dw_loc_descr_ref loc_result;
11665 if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
11666 return NULL;
11668 base = get_base_address (MEM_EXPR (mem));
11669 if (base == NULL
11670 || TREE_CODE (base) != VAR_DECL
11671 || !DECL_THREAD_LOCAL_P (base))
11672 return NULL;
11674 loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1, NULL);
11675 if (loc_result == NULL)
11676 return NULL;
11678 if (MEM_OFFSET (mem))
11679 loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
11681 return loc_result;
11684 /* Output debug info about reason why we failed to expand expression as dwarf
11685 expression. */
11687 static void
11688 expansion_failed (tree expr, rtx rtl, char const *reason)
11690 if (dump_file && (dump_flags & TDF_DETAILS))
11692 fprintf (dump_file, "Failed to expand as dwarf: ");
11693 if (expr)
11694 print_generic_expr (dump_file, expr, dump_flags);
11695 if (rtl)
11697 fprintf (dump_file, "\n");
11698 print_rtl (dump_file, rtl);
11700 fprintf (dump_file, "\nReason: %s\n", reason);
11704 /* Helper function for const_ok_for_output. */
11706 static bool
11707 const_ok_for_output_1 (rtx rtl)
11709 if (GET_CODE (rtl) == UNSPEC)
11711 /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11712 we can't express it in the debug info. */
11713 #ifdef ENABLE_CHECKING
11714 /* Don't complain about TLS UNSPECs, those are just too hard to
11715 delegitimize. Note this could be a non-decl SYMBOL_REF such as
11716 one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
11717 rather than DECL_THREAD_LOCAL_P is not just an optimization. */
11718 if (XVECLEN (rtl, 0) == 0
11719 || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11720 || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
11721 inform (current_function_decl
11722 ? DECL_SOURCE_LOCATION (current_function_decl)
11723 : UNKNOWN_LOCATION,
11724 #if NUM_UNSPEC_VALUES > 0
11725 "non-delegitimized UNSPEC %s (%d) found in variable location",
11726 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11727 ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11728 XINT (rtl, 1));
11729 #else
11730 "non-delegitimized UNSPEC %d found in variable location",
11731 XINT (rtl, 1));
11732 #endif
11733 #endif
11734 expansion_failed (NULL_TREE, rtl,
11735 "UNSPEC hasn't been delegitimized.\n");
11736 return false;
11739 if (targetm.const_not_ok_for_debug_p (rtl))
11741 expansion_failed (NULL_TREE, rtl,
11742 "Expression rejected for debug by the backend.\n");
11743 return false;
11746 /* FIXME: Refer to PR60655. It is possible for simplification
11747 of rtl expressions in var tracking to produce such expressions.
11748 We should really identify / validate expressions
11749 enclosed in CONST that can be handled by assemblers on various
11750 targets and only handle legitimate cases here. */
11751 if (GET_CODE (rtl) != SYMBOL_REF)
11753 if (GET_CODE (rtl) == NOT)
11754 return false;
11755 return true;
11758 if (CONSTANT_POOL_ADDRESS_P (rtl))
11760 bool marked;
11761 get_pool_constant_mark (rtl, &marked);
11762 /* If all references to this pool constant were optimized away,
11763 it was not output and thus we can't represent it. */
11764 if (!marked)
11766 expansion_failed (NULL_TREE, rtl,
11767 "Constant was removed from constant pool.\n");
11768 return false;
11772 if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11773 return false;
11775 /* Avoid references to external symbols in debug info, on several targets
11776 the linker might even refuse to link when linking a shared library,
11777 and in many other cases the relocations for .debug_info/.debug_loc are
11778 dropped, so the address becomes zero anyway. Hidden symbols, guaranteed
11779 to be defined within the same shared library or executable are fine. */
11780 if (SYMBOL_REF_EXTERNAL_P (rtl))
11782 tree decl = SYMBOL_REF_DECL (rtl);
11784 if (decl == NULL || !targetm.binds_local_p (decl))
11786 expansion_failed (NULL_TREE, rtl,
11787 "Symbol not defined in current TU.\n");
11788 return false;
11792 return true;
11795 /* Return true if constant RTL can be emitted in DW_OP_addr or
11796 DW_AT_const_value. TLS SYMBOL_REFs, external SYMBOL_REFs or
11797 non-marked constant pool SYMBOL_REFs can't be referenced in it. */
11799 static bool
11800 const_ok_for_output (rtx rtl)
11802 if (GET_CODE (rtl) == SYMBOL_REF)
11803 return const_ok_for_output_1 (rtl);
11805 if (GET_CODE (rtl) == CONST)
11807 subrtx_var_iterator::array_type array;
11808 FOR_EACH_SUBRTX_VAR (iter, array, XEXP (rtl, 0), ALL)
11809 if (!const_ok_for_output_1 (*iter))
11810 return false;
11811 return true;
11814 return true;
11817 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11818 if possible, NULL otherwise. */
11820 static dw_die_ref
11821 base_type_for_mode (machine_mode mode, bool unsignedp)
11823 dw_die_ref type_die;
11824 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11826 if (type == NULL)
11827 return NULL;
11828 switch (TREE_CODE (type))
11830 case INTEGER_TYPE:
11831 case REAL_TYPE:
11832 break;
11833 default:
11834 return NULL;
11836 type_die = lookup_type_die (type);
11837 if (!type_die)
11838 type_die = modified_type_die (type, TYPE_UNQUALIFIED, comp_unit_die ());
11839 if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11840 return NULL;
11841 return type_die;
11844 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11845 type matching MODE, or, if MODE is narrower than or as wide as
11846 DWARF2_ADDR_SIZE, untyped. Return NULL if the conversion is not
11847 possible. */
11849 static dw_loc_descr_ref
11850 convert_descriptor_to_mode (machine_mode mode, dw_loc_descr_ref op)
11852 machine_mode outer_mode = mode;
11853 dw_die_ref type_die;
11854 dw_loc_descr_ref cvt;
11856 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11858 add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11859 return op;
11861 type_die = base_type_for_mode (outer_mode, 1);
11862 if (type_die == NULL)
11863 return NULL;
11864 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11865 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11866 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11867 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11868 add_loc_descr (&op, cvt);
11869 return op;
11872 /* Return location descriptor for comparison OP with operands OP0 and OP1. */
11874 static dw_loc_descr_ref
11875 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11876 dw_loc_descr_ref op1)
11878 dw_loc_descr_ref ret = op0;
11879 add_loc_descr (&ret, op1);
11880 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11881 if (STORE_FLAG_VALUE != 1)
11883 add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11884 add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11886 return ret;
11889 /* Return location descriptor for signed comparison OP RTL. */
11891 static dw_loc_descr_ref
11892 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11893 machine_mode mem_mode)
11895 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11896 dw_loc_descr_ref op0, op1;
11897 int shift;
11899 if (op_mode == VOIDmode)
11900 op_mode = GET_MODE (XEXP (rtl, 1));
11901 if (op_mode == VOIDmode)
11902 return NULL;
11904 if (dwarf_strict
11905 && (GET_MODE_CLASS (op_mode) != MODE_INT
11906 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11907 return NULL;
11909 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11910 VAR_INIT_STATUS_INITIALIZED);
11911 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11912 VAR_INIT_STATUS_INITIALIZED);
11914 if (op0 == NULL || op1 == NULL)
11915 return NULL;
11917 if (GET_MODE_CLASS (op_mode) != MODE_INT
11918 || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11919 return compare_loc_descriptor (op, op0, op1);
11921 if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11923 dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11924 dw_loc_descr_ref cvt;
11926 if (type_die == NULL)
11927 return NULL;
11928 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11929 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11930 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11931 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11932 add_loc_descr (&op0, cvt);
11933 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11934 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11935 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11936 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11937 add_loc_descr (&op1, cvt);
11938 return compare_loc_descriptor (op, op0, op1);
11941 shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11942 /* For eq/ne, if the operands are known to be zero-extended,
11943 there is no need to do the fancy shifting up. */
11944 if (op == DW_OP_eq || op == DW_OP_ne)
11946 dw_loc_descr_ref last0, last1;
11947 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11949 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11951 /* deref_size zero extends, and for constants we can check
11952 whether they are zero extended or not. */
11953 if (((last0->dw_loc_opc == DW_OP_deref_size
11954 && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11955 || (CONST_INT_P (XEXP (rtl, 0))
11956 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11957 == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11958 && ((last1->dw_loc_opc == DW_OP_deref_size
11959 && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11960 || (CONST_INT_P (XEXP (rtl, 1))
11961 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11962 == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11963 return compare_loc_descriptor (op, op0, op1);
11965 /* EQ/NE comparison against constant in narrower type than
11966 DWARF2_ADDR_SIZE can be performed either as
11967 DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11968 DW_OP_{eq,ne}
11970 DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11971 DW_OP_{eq,ne}. Pick whatever is shorter. */
11972 if (CONST_INT_P (XEXP (rtl, 1))
11973 && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11974 && (size_of_int_loc_descriptor (shift) + 1
11975 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11976 >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11977 + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11978 & GET_MODE_MASK (op_mode))))
11980 add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11981 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11982 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11983 & GET_MODE_MASK (op_mode));
11984 return compare_loc_descriptor (op, op0, op1);
11987 add_loc_descr (&op0, int_loc_descriptor (shift));
11988 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11989 if (CONST_INT_P (XEXP (rtl, 1)))
11990 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11991 else
11993 add_loc_descr (&op1, int_loc_descriptor (shift));
11994 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11996 return compare_loc_descriptor (op, op0, op1);
11999 /* Return location descriptor for unsigned comparison OP RTL. */
12001 static dw_loc_descr_ref
12002 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
12003 machine_mode mem_mode)
12005 machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
12006 dw_loc_descr_ref op0, op1;
12008 if (op_mode == VOIDmode)
12009 op_mode = GET_MODE (XEXP (rtl, 1));
12010 if (op_mode == VOIDmode)
12011 return NULL;
12012 if (GET_MODE_CLASS (op_mode) != MODE_INT)
12013 return NULL;
12015 if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
12016 return NULL;
12018 op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
12019 VAR_INIT_STATUS_INITIALIZED);
12020 op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
12021 VAR_INIT_STATUS_INITIALIZED);
12023 if (op0 == NULL || op1 == NULL)
12024 return NULL;
12026 if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
12028 HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
12029 dw_loc_descr_ref last0, last1;
12030 for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
12032 for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
12034 if (CONST_INT_P (XEXP (rtl, 0)))
12035 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
12036 /* deref_size zero extends, so no need to mask it again. */
12037 else if (last0->dw_loc_opc != DW_OP_deref_size
12038 || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12040 add_loc_descr (&op0, int_loc_descriptor (mask));
12041 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12043 if (CONST_INT_P (XEXP (rtl, 1)))
12044 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
12045 /* deref_size zero extends, so no need to mask it again. */
12046 else if (last1->dw_loc_opc != DW_OP_deref_size
12047 || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
12049 add_loc_descr (&op1, int_loc_descriptor (mask));
12050 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12053 else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
12055 HOST_WIDE_INT bias = 1;
12056 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12057 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12058 if (CONST_INT_P (XEXP (rtl, 1)))
12059 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
12060 + INTVAL (XEXP (rtl, 1)));
12061 else
12062 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
12063 bias, 0));
12065 return compare_loc_descriptor (op, op0, op1);
12068 /* Return location descriptor for {U,S}{MIN,MAX}. */
12070 static dw_loc_descr_ref
12071 minmax_loc_descriptor (rtx rtl, machine_mode mode,
12072 machine_mode mem_mode)
12074 enum dwarf_location_atom op;
12075 dw_loc_descr_ref op0, op1, ret;
12076 dw_loc_descr_ref bra_node, drop_node;
12078 if (dwarf_strict
12079 && (GET_MODE_CLASS (mode) != MODE_INT
12080 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
12081 return NULL;
12083 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12084 VAR_INIT_STATUS_INITIALIZED);
12085 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12086 VAR_INIT_STATUS_INITIALIZED);
12088 if (op0 == NULL || op1 == NULL)
12089 return NULL;
12091 add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
12092 add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
12093 add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
12094 if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
12096 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12098 HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12099 add_loc_descr (&op0, int_loc_descriptor (mask));
12100 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
12101 add_loc_descr (&op1, int_loc_descriptor (mask));
12102 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
12104 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12106 HOST_WIDE_INT bias = 1;
12107 bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
12108 add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12109 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
12112 else if (GET_MODE_CLASS (mode) == MODE_INT
12113 && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12115 int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
12116 add_loc_descr (&op0, int_loc_descriptor (shift));
12117 add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
12118 add_loc_descr (&op1, int_loc_descriptor (shift));
12119 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
12121 else if (GET_MODE_CLASS (mode) == MODE_INT
12122 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12124 dw_die_ref type_die = base_type_for_mode (mode, 0);
12125 dw_loc_descr_ref cvt;
12126 if (type_die == NULL)
12127 return NULL;
12128 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12129 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12130 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12131 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12132 add_loc_descr (&op0, cvt);
12133 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12134 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12135 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12136 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12137 add_loc_descr (&op1, cvt);
12140 if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
12141 op = DW_OP_lt;
12142 else
12143 op = DW_OP_gt;
12144 ret = op0;
12145 add_loc_descr (&ret, op1);
12146 add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12147 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12148 add_loc_descr (&ret, bra_node);
12149 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12150 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12151 add_loc_descr (&ret, drop_node);
12152 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12153 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12154 if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
12155 && GET_MODE_CLASS (mode) == MODE_INT
12156 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12157 ret = convert_descriptor_to_mode (mode, ret);
12158 return ret;
12161 /* Helper function for mem_loc_descriptor. Perform OP binary op,
12162 but after converting arguments to type_die, afterwards
12163 convert back to unsigned. */
12165 static dw_loc_descr_ref
12166 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
12167 machine_mode mode, machine_mode mem_mode)
12169 dw_loc_descr_ref cvt, op0, op1;
12171 if (type_die == NULL)
12172 return NULL;
12173 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12174 VAR_INIT_STATUS_INITIALIZED);
12175 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12176 VAR_INIT_STATUS_INITIALIZED);
12177 if (op0 == NULL || op1 == NULL)
12178 return NULL;
12179 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12180 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12181 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12182 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12183 add_loc_descr (&op0, cvt);
12184 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12185 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12186 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12187 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12188 add_loc_descr (&op1, cvt);
12189 add_loc_descr (&op0, op1);
12190 add_loc_descr (&op0, new_loc_descr (op, 0, 0));
12191 return convert_descriptor_to_mode (mode, op0);
12194 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
12195 const0 is DW_OP_lit0 or corresponding typed constant,
12196 const1 is DW_OP_lit1 or corresponding typed constant
12197 and constMSB is constant with just the MSB bit set
12198 for the mode):
12199 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12200 L1: const0 DW_OP_swap
12201 L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
12202 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12203 L3: DW_OP_drop
12204 L4: DW_OP_nop
12206 CTZ is similar:
12207 DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
12208 L1: const0 DW_OP_swap
12209 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12210 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12211 L3: DW_OP_drop
12212 L4: DW_OP_nop
12214 FFS is similar:
12215 DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
12216 L1: const1 DW_OP_swap
12217 L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
12218 DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
12219 L3: DW_OP_drop
12220 L4: DW_OP_nop */
12222 static dw_loc_descr_ref
12223 clz_loc_descriptor (rtx rtl, machine_mode mode,
12224 machine_mode mem_mode)
12226 dw_loc_descr_ref op0, ret, tmp;
12227 HOST_WIDE_INT valv;
12228 dw_loc_descr_ref l1jump, l1label;
12229 dw_loc_descr_ref l2jump, l2label;
12230 dw_loc_descr_ref l3jump, l3label;
12231 dw_loc_descr_ref l4jump, l4label;
12232 rtx msb;
12234 if (GET_MODE_CLASS (mode) != MODE_INT
12235 || GET_MODE (XEXP (rtl, 0)) != mode)
12236 return NULL;
12238 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12239 VAR_INIT_STATUS_INITIALIZED);
12240 if (op0 == NULL)
12241 return NULL;
12242 ret = op0;
12243 if (GET_CODE (rtl) == CLZ)
12245 if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12246 valv = GET_MODE_BITSIZE (mode);
12248 else if (GET_CODE (rtl) == FFS)
12249 valv = 0;
12250 else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
12251 valv = GET_MODE_BITSIZE (mode);
12252 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12253 l1jump = new_loc_descr (DW_OP_bra, 0, 0);
12254 add_loc_descr (&ret, l1jump);
12255 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12256 tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
12257 VAR_INIT_STATUS_INITIALIZED);
12258 if (tmp == NULL)
12259 return NULL;
12260 add_loc_descr (&ret, tmp);
12261 l4jump = new_loc_descr (DW_OP_skip, 0, 0);
12262 add_loc_descr (&ret, l4jump);
12263 l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
12264 ? const1_rtx : const0_rtx,
12265 mode, mem_mode,
12266 VAR_INIT_STATUS_INITIALIZED);
12267 if (l1label == NULL)
12268 return NULL;
12269 add_loc_descr (&ret, l1label);
12270 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12271 l2label = new_loc_descr (DW_OP_dup, 0, 0);
12272 add_loc_descr (&ret, l2label);
12273 if (GET_CODE (rtl) != CLZ)
12274 msb = const1_rtx;
12275 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
12276 msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
12277 << (GET_MODE_BITSIZE (mode) - 1));
12278 else
12279 msb = immed_wide_int_const
12280 (wi::set_bit_in_zero (GET_MODE_PRECISION (mode) - 1,
12281 GET_MODE_PRECISION (mode)), mode);
12282 if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
12283 tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12284 ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
12285 ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
12286 else
12287 tmp = mem_loc_descriptor (msb, mode, mem_mode,
12288 VAR_INIT_STATUS_INITIALIZED);
12289 if (tmp == NULL)
12290 return NULL;
12291 add_loc_descr (&ret, tmp);
12292 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12293 l3jump = new_loc_descr (DW_OP_bra, 0, 0);
12294 add_loc_descr (&ret, l3jump);
12295 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12296 VAR_INIT_STATUS_INITIALIZED);
12297 if (tmp == NULL)
12298 return NULL;
12299 add_loc_descr (&ret, tmp);
12300 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
12301 ? DW_OP_shl : DW_OP_shr, 0, 0));
12302 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12303 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
12304 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12305 l2jump = new_loc_descr (DW_OP_skip, 0, 0);
12306 add_loc_descr (&ret, l2jump);
12307 l3label = new_loc_descr (DW_OP_drop, 0, 0);
12308 add_loc_descr (&ret, l3label);
12309 l4label = new_loc_descr (DW_OP_nop, 0, 0);
12310 add_loc_descr (&ret, l4label);
12311 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12312 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12313 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12314 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12315 l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12316 l3jump->dw_loc_oprnd1.v.val_loc = l3label;
12317 l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12318 l4jump->dw_loc_oprnd1.v.val_loc = l4label;
12319 return ret;
12322 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
12323 const1 is DW_OP_lit1 or corresponding typed constant):
12324 const0 DW_OP_swap
12325 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12326 DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12327 L2: DW_OP_drop
12329 PARITY is similar:
12330 L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
12331 DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
12332 L2: DW_OP_drop */
12334 static dw_loc_descr_ref
12335 popcount_loc_descriptor (rtx rtl, machine_mode mode,
12336 machine_mode mem_mode)
12338 dw_loc_descr_ref op0, ret, tmp;
12339 dw_loc_descr_ref l1jump, l1label;
12340 dw_loc_descr_ref l2jump, l2label;
12342 if (GET_MODE_CLASS (mode) != MODE_INT
12343 || GET_MODE (XEXP (rtl, 0)) != mode)
12344 return NULL;
12346 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12347 VAR_INIT_STATUS_INITIALIZED);
12348 if (op0 == NULL)
12349 return NULL;
12350 ret = op0;
12351 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12352 VAR_INIT_STATUS_INITIALIZED);
12353 if (tmp == NULL)
12354 return NULL;
12355 add_loc_descr (&ret, tmp);
12356 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12357 l1label = new_loc_descr (DW_OP_dup, 0, 0);
12358 add_loc_descr (&ret, l1label);
12359 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12360 add_loc_descr (&ret, l2jump);
12361 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12362 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12363 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12364 VAR_INIT_STATUS_INITIALIZED);
12365 if (tmp == NULL)
12366 return NULL;
12367 add_loc_descr (&ret, tmp);
12368 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12369 add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
12370 ? DW_OP_plus : DW_OP_xor, 0, 0));
12371 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12372 tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
12373 VAR_INIT_STATUS_INITIALIZED);
12374 add_loc_descr (&ret, tmp);
12375 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12376 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12377 add_loc_descr (&ret, l1jump);
12378 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12379 add_loc_descr (&ret, l2label);
12380 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12381 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12382 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12383 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12384 return ret;
12387 /* BSWAP (constS is initial shift count, either 56 or 24):
12388 constS const0
12389 L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
12390 const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
12391 DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
12392 DW_OP_minus DW_OP_swap DW_OP_skip <L1>
12393 L2: DW_OP_drop DW_OP_swap DW_OP_drop */
12395 static dw_loc_descr_ref
12396 bswap_loc_descriptor (rtx rtl, machine_mode mode,
12397 machine_mode mem_mode)
12399 dw_loc_descr_ref op0, ret, tmp;
12400 dw_loc_descr_ref l1jump, l1label;
12401 dw_loc_descr_ref l2jump, l2label;
12403 if (GET_MODE_CLASS (mode) != MODE_INT
12404 || BITS_PER_UNIT != 8
12405 || (GET_MODE_BITSIZE (mode) != 32
12406 && GET_MODE_BITSIZE (mode) != 64))
12407 return NULL;
12409 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12410 VAR_INIT_STATUS_INITIALIZED);
12411 if (op0 == NULL)
12412 return NULL;
12414 ret = op0;
12415 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12416 mode, mem_mode,
12417 VAR_INIT_STATUS_INITIALIZED);
12418 if (tmp == NULL)
12419 return NULL;
12420 add_loc_descr (&ret, tmp);
12421 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12422 VAR_INIT_STATUS_INITIALIZED);
12423 if (tmp == NULL)
12424 return NULL;
12425 add_loc_descr (&ret, tmp);
12426 l1label = new_loc_descr (DW_OP_pick, 2, 0);
12427 add_loc_descr (&ret, l1label);
12428 tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
12429 mode, mem_mode,
12430 VAR_INIT_STATUS_INITIALIZED);
12431 add_loc_descr (&ret, tmp);
12432 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
12433 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12434 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12435 tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
12436 VAR_INIT_STATUS_INITIALIZED);
12437 if (tmp == NULL)
12438 return NULL;
12439 add_loc_descr (&ret, tmp);
12440 add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
12441 add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
12442 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12443 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12444 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12445 add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
12446 tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
12447 VAR_INIT_STATUS_INITIALIZED);
12448 add_loc_descr (&ret, tmp);
12449 add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
12450 l2jump = new_loc_descr (DW_OP_bra, 0, 0);
12451 add_loc_descr (&ret, l2jump);
12452 tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
12453 VAR_INIT_STATUS_INITIALIZED);
12454 add_loc_descr (&ret, tmp);
12455 add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
12456 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12457 l1jump = new_loc_descr (DW_OP_skip, 0, 0);
12458 add_loc_descr (&ret, l1jump);
12459 l2label = new_loc_descr (DW_OP_drop, 0, 0);
12460 add_loc_descr (&ret, l2label);
12461 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12462 add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
12463 l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12464 l1jump->dw_loc_oprnd1.v.val_loc = l1label;
12465 l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
12466 l2jump->dw_loc_oprnd1.v.val_loc = l2label;
12467 return ret;
12470 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
12471 DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12472 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
12473 DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
12475 ROTATERT is similar:
12476 DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
12477 DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
12478 [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or */
12480 static dw_loc_descr_ref
12481 rotate_loc_descriptor (rtx rtl, machine_mode mode,
12482 machine_mode mem_mode)
12484 rtx rtlop1 = XEXP (rtl, 1);
12485 dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
12486 int i;
12488 if (GET_MODE_CLASS (mode) != MODE_INT)
12489 return NULL;
12491 if (GET_MODE (rtlop1) != VOIDmode
12492 && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
12493 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12494 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12495 VAR_INIT_STATUS_INITIALIZED);
12496 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12497 VAR_INIT_STATUS_INITIALIZED);
12498 if (op0 == NULL || op1 == NULL)
12499 return NULL;
12500 if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
12501 for (i = 0; i < 2; i++)
12503 if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
12504 mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
12505 mode, mem_mode,
12506 VAR_INIT_STATUS_INITIALIZED);
12507 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12508 mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
12509 ? DW_OP_const4u
12510 : HOST_BITS_PER_WIDE_INT == 64
12511 ? DW_OP_const8u : DW_OP_constu,
12512 GET_MODE_MASK (mode), 0);
12513 else
12514 mask[i] = NULL;
12515 if (mask[i] == NULL)
12516 return NULL;
12517 add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
12519 ret = op0;
12520 add_loc_descr (&ret, op1);
12521 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12522 add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
12523 if (GET_CODE (rtl) == ROTATERT)
12525 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12526 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12527 GET_MODE_BITSIZE (mode), 0));
12529 add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
12530 if (mask[0] != NULL)
12531 add_loc_descr (&ret, mask[0]);
12532 add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
12533 if (mask[1] != NULL)
12535 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12536 add_loc_descr (&ret, mask[1]);
12537 add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
12539 if (GET_CODE (rtl) == ROTATE)
12541 add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
12542 add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
12543 GET_MODE_BITSIZE (mode), 0));
12545 add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
12546 add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
12547 return ret;
12550 /* Helper function for mem_loc_descriptor. Return DW_OP_GNU_parameter_ref
12551 for DEBUG_PARAMETER_REF RTL. */
12553 static dw_loc_descr_ref
12554 parameter_ref_descriptor (rtx rtl)
12556 dw_loc_descr_ref ret;
12557 dw_die_ref ref;
12559 if (dwarf_strict)
12560 return NULL;
12561 gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
12562 ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
12563 ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
12564 if (ref)
12566 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12567 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12568 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12570 else
12572 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12573 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
12575 return ret;
12578 /* The following routine converts the RTL for a variable or parameter
12579 (resident in memory) into an equivalent Dwarf representation of a
12580 mechanism for getting the address of that same variable onto the top of a
12581 hypothetical "address evaluation" stack.
12583 When creating memory location descriptors, we are effectively transforming
12584 the RTL for a memory-resident object into its Dwarf postfix expression
12585 equivalent. This routine recursively descends an RTL tree, turning
12586 it into Dwarf postfix code as it goes.
12588 MODE is the mode that should be assumed for the rtl if it is VOIDmode.
12590 MEM_MODE is the mode of the memory reference, needed to handle some
12591 autoincrement addressing modes.
12593 Return 0 if we can't represent the location. */
12595 dw_loc_descr_ref
12596 mem_loc_descriptor (rtx rtl, machine_mode mode,
12597 machine_mode mem_mode,
12598 enum var_init_status initialized)
12600 dw_loc_descr_ref mem_loc_result = NULL;
12601 enum dwarf_location_atom op;
12602 dw_loc_descr_ref op0, op1;
12603 rtx inner = NULL_RTX;
12605 if (mode == VOIDmode)
12606 mode = GET_MODE (rtl);
12608 /* Note that for a dynamically sized array, the location we will generate a
12609 description of here will be the lowest numbered location which is
12610 actually within the array. That's *not* necessarily the same as the
12611 zeroth element of the array. */
12613 rtl = targetm.delegitimize_address (rtl);
12615 if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
12616 return NULL;
12618 switch (GET_CODE (rtl))
12620 case POST_INC:
12621 case POST_DEC:
12622 case POST_MODIFY:
12623 return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
12625 case SUBREG:
12626 /* The case of a subreg may arise when we have a local (register)
12627 variable or a formal (register) parameter which doesn't quite fill
12628 up an entire register. For now, just assume that it is
12629 legitimate to make the Dwarf info refer to the whole register which
12630 contains the given subreg. */
12631 if (!subreg_lowpart_p (rtl))
12632 break;
12633 inner = SUBREG_REG (rtl);
12634 case TRUNCATE:
12635 if (inner == NULL_RTX)
12636 inner = XEXP (rtl, 0);
12637 if (GET_MODE_CLASS (mode) == MODE_INT
12638 && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
12639 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12640 #ifdef POINTERS_EXTEND_UNSIGNED
12641 || (mode == Pmode && mem_mode != VOIDmode)
12642 #endif
12644 && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
12646 mem_loc_result = mem_loc_descriptor (inner,
12647 GET_MODE (inner),
12648 mem_mode, initialized);
12649 break;
12651 if (dwarf_strict)
12652 break;
12653 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
12654 break;
12655 if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
12656 && (GET_MODE_CLASS (mode) != MODE_INT
12657 || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
12658 break;
12659 else
12661 dw_die_ref type_die;
12662 dw_loc_descr_ref cvt;
12664 mem_loc_result = mem_loc_descriptor (inner,
12665 GET_MODE (inner),
12666 mem_mode, initialized);
12667 if (mem_loc_result == NULL)
12668 break;
12669 type_die = base_type_for_mode (mode,
12670 GET_MODE_CLASS (mode) == MODE_INT);
12671 if (type_die == NULL)
12673 mem_loc_result = NULL;
12674 break;
12676 if (GET_MODE_SIZE (mode)
12677 != GET_MODE_SIZE (GET_MODE (inner)))
12678 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12679 else
12680 cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12681 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12682 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12683 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12684 add_loc_descr (&mem_loc_result, cvt);
12686 break;
12688 case REG:
12689 if (GET_MODE_CLASS (mode) != MODE_INT
12690 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12691 && rtl != arg_pointer_rtx
12692 && rtl != frame_pointer_rtx
12693 #ifdef POINTERS_EXTEND_UNSIGNED
12694 && (mode != Pmode || mem_mode == VOIDmode)
12695 #endif
12698 dw_die_ref type_die;
12699 unsigned int dbx_regnum;
12701 if (dwarf_strict)
12702 break;
12703 if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12704 break;
12705 type_die = base_type_for_mode (mode,
12706 GET_MODE_CLASS (mode) == MODE_INT);
12707 if (type_die == NULL)
12708 break;
12710 dbx_regnum = dbx_reg_number (rtl);
12711 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12712 break;
12713 mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12714 dbx_regnum, 0);
12715 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12716 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12717 mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12718 break;
12720 /* Whenever a register number forms a part of the description of the
12721 method for calculating the (dynamic) address of a memory resident
12722 object, DWARF rules require the register number be referred to as
12723 a "base register". This distinction is not based in any way upon
12724 what category of register the hardware believes the given register
12725 belongs to. This is strictly DWARF terminology we're dealing with
12726 here. Note that in cases where the location of a memory-resident
12727 data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12728 OP_CONST (0)) the actual DWARF location descriptor that we generate
12729 may just be OP_BASEREG (basereg). This may look deceptively like
12730 the object in question was allocated to a register (rather than in
12731 memory) so DWARF consumers need to be aware of the subtle
12732 distinction between OP_REG and OP_BASEREG. */
12733 if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12734 mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12735 else if (stack_realign_drap
12736 && crtl->drap_reg
12737 && crtl->args.internal_arg_pointer == rtl
12738 && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12740 /* If RTL is internal_arg_pointer, which has been optimized
12741 out, use DRAP instead. */
12742 mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12743 VAR_INIT_STATUS_INITIALIZED);
12745 break;
12747 case SIGN_EXTEND:
12748 case ZERO_EXTEND:
12749 if (GET_MODE_CLASS (mode) != MODE_INT)
12750 break;
12751 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12752 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12753 if (op0 == 0)
12754 break;
12755 else if (GET_CODE (rtl) == ZERO_EXTEND
12756 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12757 && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12758 < HOST_BITS_PER_WIDE_INT
12759 /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12760 to expand zero extend as two shifts instead of
12761 masking. */
12762 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12764 machine_mode imode = GET_MODE (XEXP (rtl, 0));
12765 mem_loc_result = op0;
12766 add_loc_descr (&mem_loc_result,
12767 int_loc_descriptor (GET_MODE_MASK (imode)));
12768 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12770 else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12772 int shift = DWARF2_ADDR_SIZE
12773 - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12774 shift *= BITS_PER_UNIT;
12775 if (GET_CODE (rtl) == SIGN_EXTEND)
12776 op = DW_OP_shra;
12777 else
12778 op = DW_OP_shr;
12779 mem_loc_result = op0;
12780 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12781 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12782 add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12783 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12785 else if (!dwarf_strict)
12787 dw_die_ref type_die1, type_die2;
12788 dw_loc_descr_ref cvt;
12790 type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12791 GET_CODE (rtl) == ZERO_EXTEND);
12792 if (type_die1 == NULL)
12793 break;
12794 type_die2 = base_type_for_mode (mode, 1);
12795 if (type_die2 == NULL)
12796 break;
12797 mem_loc_result = op0;
12798 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12799 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12800 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12801 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12802 add_loc_descr (&mem_loc_result, cvt);
12803 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12804 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12805 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12806 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12807 add_loc_descr (&mem_loc_result, cvt);
12809 break;
12811 case MEM:
12813 rtx new_rtl = avoid_constant_pool_reference (rtl);
12814 if (new_rtl != rtl)
12816 mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12817 initialized);
12818 if (mem_loc_result != NULL)
12819 return mem_loc_result;
12822 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12823 get_address_mode (rtl), mode,
12824 VAR_INIT_STATUS_INITIALIZED);
12825 if (mem_loc_result == NULL)
12826 mem_loc_result = tls_mem_loc_descriptor (rtl);
12827 if (mem_loc_result != NULL)
12829 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12830 || GET_MODE_CLASS (mode) != MODE_INT)
12832 dw_die_ref type_die;
12833 dw_loc_descr_ref deref;
12835 if (dwarf_strict)
12836 return NULL;
12837 type_die
12838 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12839 if (type_die == NULL)
12840 return NULL;
12841 deref = new_loc_descr (DW_OP_GNU_deref_type,
12842 GET_MODE_SIZE (mode), 0);
12843 deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12844 deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12845 deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12846 add_loc_descr (&mem_loc_result, deref);
12848 else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12849 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12850 else
12851 add_loc_descr (&mem_loc_result,
12852 new_loc_descr (DW_OP_deref_size,
12853 GET_MODE_SIZE (mode), 0));
12855 break;
12857 case LO_SUM:
12858 return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12860 case LABEL_REF:
12861 /* Some ports can transform a symbol ref into a label ref, because
12862 the symbol ref is too far away and has to be dumped into a constant
12863 pool. */
12864 case CONST:
12865 case SYMBOL_REF:
12866 if ((GET_MODE_CLASS (mode) != MODE_INT
12867 && GET_MODE_CLASS (mode) != MODE_PARTIAL_INT)
12868 || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12869 #ifdef POINTERS_EXTEND_UNSIGNED
12870 && (mode != Pmode || mem_mode == VOIDmode)
12871 #endif
12873 break;
12874 if (GET_CODE (rtl) == SYMBOL_REF
12875 && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12877 dw_loc_descr_ref temp;
12879 /* If this is not defined, we have no way to emit the data. */
12880 if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12881 break;
12883 temp = new_addr_loc_descr (rtl, dtprel_true);
12885 mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12886 add_loc_descr (&mem_loc_result, temp);
12888 break;
12891 if (!const_ok_for_output (rtl))
12893 if (GET_CODE (rtl) == CONST)
12894 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12895 initialized);
12896 break;
12899 symref:
12900 mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12901 vec_safe_push (used_rtx_array, rtl);
12902 break;
12904 case CONCAT:
12905 case CONCATN:
12906 case VAR_LOCATION:
12907 case DEBUG_IMPLICIT_PTR:
12908 expansion_failed (NULL_TREE, rtl,
12909 "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12910 return 0;
12912 case ENTRY_VALUE:
12913 if (dwarf_strict)
12914 return NULL;
12915 if (REG_P (ENTRY_VALUE_EXP (rtl)))
12917 if (GET_MODE_CLASS (mode) != MODE_INT
12918 || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12919 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12920 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12921 else
12923 unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12924 if (dbx_regnum == IGNORED_DWARF_REGNUM)
12925 return NULL;
12926 op0 = one_reg_loc_descriptor (dbx_regnum,
12927 VAR_INIT_STATUS_INITIALIZED);
12930 else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12931 && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12933 op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12934 VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12935 if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12936 return NULL;
12938 else
12939 gcc_unreachable ();
12940 if (op0 == NULL)
12941 return NULL;
12942 mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12943 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12944 mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12945 break;
12947 case DEBUG_PARAMETER_REF:
12948 mem_loc_result = parameter_ref_descriptor (rtl);
12949 break;
12951 case PRE_MODIFY:
12952 /* Extract the PLUS expression nested inside and fall into
12953 PLUS code below. */
12954 rtl = XEXP (rtl, 1);
12955 goto plus;
12957 case PRE_INC:
12958 case PRE_DEC:
12959 /* Turn these into a PLUS expression and fall into the PLUS code
12960 below. */
12961 rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12962 gen_int_mode (GET_CODE (rtl) == PRE_INC
12963 ? GET_MODE_UNIT_SIZE (mem_mode)
12964 : -GET_MODE_UNIT_SIZE (mem_mode),
12965 mode));
12967 /* ... fall through ... */
12969 case PLUS:
12970 plus:
12971 if (is_based_loc (rtl)
12972 && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12973 || XEXP (rtl, 0) == arg_pointer_rtx
12974 || XEXP (rtl, 0) == frame_pointer_rtx)
12975 && GET_MODE_CLASS (mode) == MODE_INT)
12976 mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12977 INTVAL (XEXP (rtl, 1)),
12978 VAR_INIT_STATUS_INITIALIZED);
12979 else
12981 mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12982 VAR_INIT_STATUS_INITIALIZED);
12983 if (mem_loc_result == 0)
12984 break;
12986 if (CONST_INT_P (XEXP (rtl, 1))
12987 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12988 loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12989 else
12991 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12992 VAR_INIT_STATUS_INITIALIZED);
12993 if (op1 == 0)
12994 return NULL;
12995 add_loc_descr (&mem_loc_result, op1);
12996 add_loc_descr (&mem_loc_result,
12997 new_loc_descr (DW_OP_plus, 0, 0));
13000 break;
13002 /* If a pseudo-reg is optimized away, it is possible for it to
13003 be replaced with a MEM containing a multiply or shift. */
13004 case MINUS:
13005 op = DW_OP_minus;
13006 goto do_binop;
13008 case MULT:
13009 op = DW_OP_mul;
13010 goto do_binop;
13012 case DIV:
13013 if (!dwarf_strict
13014 && GET_MODE_CLASS (mode) == MODE_INT
13015 && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
13017 mem_loc_result = typed_binop (DW_OP_div, rtl,
13018 base_type_for_mode (mode, 0),
13019 mode, mem_mode);
13020 break;
13022 op = DW_OP_div;
13023 goto do_binop;
13025 case UMOD:
13026 op = DW_OP_mod;
13027 goto do_binop;
13029 case ASHIFT:
13030 op = DW_OP_shl;
13031 goto do_shift;
13033 case ASHIFTRT:
13034 op = DW_OP_shra;
13035 goto do_shift;
13037 case LSHIFTRT:
13038 op = DW_OP_shr;
13039 goto do_shift;
13041 do_shift:
13042 if (GET_MODE_CLASS (mode) != MODE_INT)
13043 break;
13044 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13045 VAR_INIT_STATUS_INITIALIZED);
13047 rtx rtlop1 = XEXP (rtl, 1);
13048 if (GET_MODE (rtlop1) != VOIDmode
13049 && GET_MODE_BITSIZE (GET_MODE (rtlop1))
13050 < GET_MODE_BITSIZE (mode))
13051 rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
13052 op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
13053 VAR_INIT_STATUS_INITIALIZED);
13056 if (op0 == 0 || op1 == 0)
13057 break;
13059 mem_loc_result = op0;
13060 add_loc_descr (&mem_loc_result, op1);
13061 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13062 break;
13064 case AND:
13065 op = DW_OP_and;
13066 goto do_binop;
13068 case IOR:
13069 op = DW_OP_or;
13070 goto do_binop;
13072 case XOR:
13073 op = DW_OP_xor;
13074 goto do_binop;
13076 do_binop:
13077 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13078 VAR_INIT_STATUS_INITIALIZED);
13079 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13080 VAR_INIT_STATUS_INITIALIZED);
13082 if (op0 == 0 || op1 == 0)
13083 break;
13085 mem_loc_result = op0;
13086 add_loc_descr (&mem_loc_result, op1);
13087 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13088 break;
13090 case MOD:
13091 if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
13093 mem_loc_result = typed_binop (DW_OP_mod, rtl,
13094 base_type_for_mode (mode, 0),
13095 mode, mem_mode);
13096 break;
13099 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13100 VAR_INIT_STATUS_INITIALIZED);
13101 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13102 VAR_INIT_STATUS_INITIALIZED);
13104 if (op0 == 0 || op1 == 0)
13105 break;
13107 mem_loc_result = op0;
13108 add_loc_descr (&mem_loc_result, op1);
13109 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13110 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13111 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13112 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13113 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13114 break;
13116 case UDIV:
13117 if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
13119 if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
13121 op = DW_OP_div;
13122 goto do_binop;
13124 mem_loc_result = typed_binop (DW_OP_div, rtl,
13125 base_type_for_mode (mode, 1),
13126 mode, mem_mode);
13128 break;
13130 case NOT:
13131 op = DW_OP_not;
13132 goto do_unop;
13134 case ABS:
13135 op = DW_OP_abs;
13136 goto do_unop;
13138 case NEG:
13139 op = DW_OP_neg;
13140 goto do_unop;
13142 do_unop:
13143 op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
13144 VAR_INIT_STATUS_INITIALIZED);
13146 if (op0 == 0)
13147 break;
13149 mem_loc_result = op0;
13150 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13151 break;
13153 case CONST_INT:
13154 if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13155 #ifdef POINTERS_EXTEND_UNSIGNED
13156 || (mode == Pmode
13157 && mem_mode != VOIDmode
13158 && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
13159 #endif
13162 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13163 break;
13165 if (!dwarf_strict
13166 && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
13167 || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
13169 dw_die_ref type_die = base_type_for_mode (mode, 1);
13170 machine_mode amode;
13171 if (type_die == NULL)
13172 return NULL;
13173 amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13174 MODE_INT, 0);
13175 if (INTVAL (rtl) >= 0
13176 && amode != BLKmode
13177 && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
13178 /* const DW_OP_GNU_convert <XXX> vs.
13179 DW_OP_GNU_const_type <XXX, 1, const>. */
13180 && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
13181 < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
13183 mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13184 op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13185 op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13186 op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13187 op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
13188 add_loc_descr (&mem_loc_result, op0);
13189 return mem_loc_result;
13191 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
13192 INTVAL (rtl));
13193 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13194 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13195 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13196 if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
13197 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13198 else
13200 mem_loc_result->dw_loc_oprnd2.val_class
13201 = dw_val_class_const_double;
13202 mem_loc_result->dw_loc_oprnd2.v.val_double
13203 = double_int::from_shwi (INTVAL (rtl));
13206 break;
13208 case CONST_DOUBLE:
13209 if (!dwarf_strict)
13211 dw_die_ref type_die;
13213 /* Note that if TARGET_SUPPORTS_WIDE_INT == 0, a
13214 CONST_DOUBLE rtx could represent either a large integer
13215 or a floating-point constant. If TARGET_SUPPORTS_WIDE_INT != 0,
13216 the value is always a floating point constant.
13218 When it is an integer, a CONST_DOUBLE is used whenever
13219 the constant requires 2 HWIs to be adequately represented.
13220 We output CONST_DOUBLEs as blocks. */
13221 if (mode == VOIDmode
13222 || (GET_MODE (rtl) == VOIDmode
13223 && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
13224 break;
13225 type_die = base_type_for_mode (mode,
13226 GET_MODE_CLASS (mode) == MODE_INT);
13227 if (type_die == NULL)
13228 return NULL;
13229 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13230 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13231 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13232 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13233 #if TARGET_SUPPORTS_WIDE_INT == 0
13234 if (!SCALAR_FLOAT_MODE_P (mode))
13236 mem_loc_result->dw_loc_oprnd2.val_class
13237 = dw_val_class_const_double;
13238 mem_loc_result->dw_loc_oprnd2.v.val_double
13239 = rtx_to_double_int (rtl);
13241 else
13242 #endif
13244 unsigned int length = GET_MODE_SIZE (mode);
13245 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13247 insert_float (rtl, array);
13248 mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13249 mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13250 mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13251 mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13254 break;
13256 case CONST_WIDE_INT:
13257 if (!dwarf_strict)
13259 dw_die_ref type_die;
13261 type_die = base_type_for_mode (mode,
13262 GET_MODE_CLASS (mode) == MODE_INT);
13263 if (type_die == NULL)
13264 return NULL;
13265 mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
13266 mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13267 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13268 mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
13269 mem_loc_result->dw_loc_oprnd2.val_class
13270 = dw_val_class_wide_int;
13271 mem_loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13272 *mem_loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13274 break;
13276 case EQ:
13277 mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
13278 break;
13280 case GE:
13281 mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13282 break;
13284 case GT:
13285 mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13286 break;
13288 case LE:
13289 mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13290 break;
13292 case LT:
13293 mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13294 break;
13296 case NE:
13297 mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
13298 break;
13300 case GEU:
13301 mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
13302 break;
13304 case GTU:
13305 mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
13306 break;
13308 case LEU:
13309 mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
13310 break;
13312 case LTU:
13313 mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
13314 break;
13316 case UMIN:
13317 case UMAX:
13318 if (GET_MODE_CLASS (mode) != MODE_INT)
13319 break;
13320 /* FALLTHRU */
13321 case SMIN:
13322 case SMAX:
13323 mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
13324 break;
13326 case ZERO_EXTRACT:
13327 case SIGN_EXTRACT:
13328 if (CONST_INT_P (XEXP (rtl, 1))
13329 && CONST_INT_P (XEXP (rtl, 2))
13330 && ((unsigned) INTVAL (XEXP (rtl, 1))
13331 + (unsigned) INTVAL (XEXP (rtl, 2))
13332 <= GET_MODE_BITSIZE (mode))
13333 && GET_MODE_CLASS (mode) == MODE_INT
13334 && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13335 && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13337 int shift, size;
13338 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13339 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13340 if (op0 == 0)
13341 break;
13342 if (GET_CODE (rtl) == SIGN_EXTRACT)
13343 op = DW_OP_shra;
13344 else
13345 op = DW_OP_shr;
13346 mem_loc_result = op0;
13347 size = INTVAL (XEXP (rtl, 1));
13348 shift = INTVAL (XEXP (rtl, 2));
13349 if (BITS_BIG_ENDIAN)
13350 shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13351 - shift - size;
13352 if (shift + size != (int) DWARF2_ADDR_SIZE)
13354 add_loc_descr (&mem_loc_result,
13355 int_loc_descriptor (DWARF2_ADDR_SIZE
13356 - shift - size));
13357 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13359 if (size != (int) DWARF2_ADDR_SIZE)
13361 add_loc_descr (&mem_loc_result,
13362 int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13363 add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13366 break;
13368 case IF_THEN_ELSE:
13370 dw_loc_descr_ref op2, bra_node, drop_node;
13371 op0 = mem_loc_descriptor (XEXP (rtl, 0),
13372 GET_MODE (XEXP (rtl, 0)) == VOIDmode
13373 ? word_mode : GET_MODE (XEXP (rtl, 0)),
13374 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13375 op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
13376 VAR_INIT_STATUS_INITIALIZED);
13377 op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
13378 VAR_INIT_STATUS_INITIALIZED);
13379 if (op0 == NULL || op1 == NULL || op2 == NULL)
13380 break;
13382 mem_loc_result = op1;
13383 add_loc_descr (&mem_loc_result, op2);
13384 add_loc_descr (&mem_loc_result, op0);
13385 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13386 add_loc_descr (&mem_loc_result, bra_node);
13387 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13388 drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13389 add_loc_descr (&mem_loc_result, drop_node);
13390 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13391 bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13393 break;
13395 case FLOAT_EXTEND:
13396 case FLOAT_TRUNCATE:
13397 case FLOAT:
13398 case UNSIGNED_FLOAT:
13399 case FIX:
13400 case UNSIGNED_FIX:
13401 if (!dwarf_strict)
13403 dw_die_ref type_die;
13404 dw_loc_descr_ref cvt;
13406 op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13407 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13408 if (op0 == NULL)
13409 break;
13410 if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
13411 && (GET_CODE (rtl) == FLOAT
13412 || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
13413 <= DWARF2_ADDR_SIZE))
13415 type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13416 GET_CODE (rtl) == UNSIGNED_FLOAT);
13417 if (type_die == NULL)
13418 break;
13419 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13420 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13421 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13422 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13423 add_loc_descr (&op0, cvt);
13425 type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
13426 if (type_die == NULL)
13427 break;
13428 cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13429 cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13430 cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13431 cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13432 add_loc_descr (&op0, cvt);
13433 if (GET_MODE_CLASS (mode) == MODE_INT
13434 && (GET_CODE (rtl) == FIX
13435 || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
13437 op0 = convert_descriptor_to_mode (mode, op0);
13438 if (op0 == NULL)
13439 break;
13441 mem_loc_result = op0;
13443 break;
13445 case CLZ:
13446 case CTZ:
13447 case FFS:
13448 mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
13449 break;
13451 case POPCOUNT:
13452 case PARITY:
13453 mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
13454 break;
13456 case BSWAP:
13457 mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
13458 break;
13460 case ROTATE:
13461 case ROTATERT:
13462 mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
13463 break;
13465 case COMPARE:
13466 /* In theory, we could implement the above. */
13467 /* DWARF cannot represent the unsigned compare operations
13468 natively. */
13469 case SS_MULT:
13470 case US_MULT:
13471 case SS_DIV:
13472 case US_DIV:
13473 case SS_PLUS:
13474 case US_PLUS:
13475 case SS_MINUS:
13476 case US_MINUS:
13477 case SS_NEG:
13478 case US_NEG:
13479 case SS_ABS:
13480 case SS_ASHIFT:
13481 case US_ASHIFT:
13482 case SS_TRUNCATE:
13483 case US_TRUNCATE:
13484 case UNORDERED:
13485 case ORDERED:
13486 case UNEQ:
13487 case UNGE:
13488 case UNGT:
13489 case UNLE:
13490 case UNLT:
13491 case LTGT:
13492 case FRACT_CONVERT:
13493 case UNSIGNED_FRACT_CONVERT:
13494 case SAT_FRACT:
13495 case UNSIGNED_SAT_FRACT:
13496 case SQRT:
13497 case ASM_OPERANDS:
13498 case VEC_MERGE:
13499 case VEC_SELECT:
13500 case VEC_CONCAT:
13501 case VEC_DUPLICATE:
13502 case UNSPEC:
13503 case HIGH:
13504 case FMA:
13505 case STRICT_LOW_PART:
13506 case CONST_VECTOR:
13507 case CONST_FIXED:
13508 case CLRSB:
13509 case CLOBBER:
13510 /* If delegitimize_address couldn't do anything with the UNSPEC, we
13511 can't express it in the debug info. This can happen e.g. with some
13512 TLS UNSPECs. */
13513 break;
13515 case CONST_STRING:
13516 resolve_one_addr (&rtl);
13517 goto symref;
13519 default:
13520 #ifdef ENABLE_CHECKING
13521 print_rtl (stderr, rtl);
13522 gcc_unreachable ();
13523 #else
13524 break;
13525 #endif
13528 if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13529 add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13531 return mem_loc_result;
13534 /* Return a descriptor that describes the concatenation of two locations.
13535 This is typically a complex variable. */
13537 static dw_loc_descr_ref
13538 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13540 dw_loc_descr_ref cc_loc_result = NULL;
13541 dw_loc_descr_ref x0_ref
13542 = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13543 dw_loc_descr_ref x1_ref
13544 = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13546 if (x0_ref == 0 || x1_ref == 0)
13547 return 0;
13549 cc_loc_result = x0_ref;
13550 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13552 add_loc_descr (&cc_loc_result, x1_ref);
13553 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13555 if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13556 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13558 return cc_loc_result;
13561 /* Return a descriptor that describes the concatenation of N
13562 locations. */
13564 static dw_loc_descr_ref
13565 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13567 unsigned int i;
13568 dw_loc_descr_ref cc_loc_result = NULL;
13569 unsigned int n = XVECLEN (concatn, 0);
13571 for (i = 0; i < n; ++i)
13573 dw_loc_descr_ref ref;
13574 rtx x = XVECEXP (concatn, 0, i);
13576 ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13577 if (ref == NULL)
13578 return NULL;
13580 add_loc_descr (&cc_loc_result, ref);
13581 add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13584 if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13585 add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13587 return cc_loc_result;
13590 /* Helper function for loc_descriptor. Return DW_OP_GNU_implicit_pointer
13591 for DEBUG_IMPLICIT_PTR RTL. */
13593 static dw_loc_descr_ref
13594 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
13596 dw_loc_descr_ref ret;
13597 dw_die_ref ref;
13599 if (dwarf_strict)
13600 return NULL;
13601 gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
13602 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
13603 || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
13604 ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
13605 ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
13606 ret->dw_loc_oprnd2.val_class = dw_val_class_const;
13607 if (ref)
13609 ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13610 ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
13611 ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
13613 else
13615 ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
13616 ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
13618 return ret;
13621 /* Output a proper Dwarf location descriptor for a variable or parameter
13622 which is either allocated in a register or in a memory location. For a
13623 register, we just generate an OP_REG and the register number. For a
13624 memory location we provide a Dwarf postfix expression describing how to
13625 generate the (dynamic) address of the object onto the address stack.
13627 MODE is mode of the decl if this loc_descriptor is going to be used in
13628 .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13629 allowed, VOIDmode otherwise.
13631 If we don't know how to describe it, return 0. */
13633 static dw_loc_descr_ref
13634 loc_descriptor (rtx rtl, machine_mode mode,
13635 enum var_init_status initialized)
13637 dw_loc_descr_ref loc_result = NULL;
13639 switch (GET_CODE (rtl))
13641 case SUBREG:
13642 /* The case of a subreg may arise when we have a local (register)
13643 variable or a formal (register) parameter which doesn't quite fill
13644 up an entire register. For now, just assume that it is
13645 legitimate to make the Dwarf info refer to the whole register which
13646 contains the given subreg. */
13647 if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
13648 loc_result = loc_descriptor (SUBREG_REG (rtl),
13649 GET_MODE (SUBREG_REG (rtl)), initialized);
13650 else
13651 goto do_default;
13652 break;
13654 case REG:
13655 loc_result = reg_loc_descriptor (rtl, initialized);
13656 break;
13658 case MEM:
13659 loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13660 GET_MODE (rtl), initialized);
13661 if (loc_result == NULL)
13662 loc_result = tls_mem_loc_descriptor (rtl);
13663 if (loc_result == NULL)
13665 rtx new_rtl = avoid_constant_pool_reference (rtl);
13666 if (new_rtl != rtl)
13667 loc_result = loc_descriptor (new_rtl, mode, initialized);
13669 break;
13671 case CONCAT:
13672 loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13673 initialized);
13674 break;
13676 case CONCATN:
13677 loc_result = concatn_loc_descriptor (rtl, initialized);
13678 break;
13680 case VAR_LOCATION:
13681 /* Single part. */
13682 if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
13684 rtx loc = PAT_VAR_LOCATION_LOC (rtl);
13685 if (GET_CODE (loc) == EXPR_LIST)
13686 loc = XEXP (loc, 0);
13687 loc_result = loc_descriptor (loc, mode, initialized);
13688 break;
13691 rtl = XEXP (rtl, 1);
13692 /* FALLTHRU */
13694 case PARALLEL:
13696 rtvec par_elems = XVEC (rtl, 0);
13697 int num_elem = GET_NUM_ELEM (par_elems);
13698 machine_mode mode;
13699 int i;
13701 /* Create the first one, so we have something to add to. */
13702 loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13703 VOIDmode, initialized);
13704 if (loc_result == NULL)
13705 return NULL;
13706 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13707 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13708 for (i = 1; i < num_elem; i++)
13710 dw_loc_descr_ref temp;
13712 temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13713 VOIDmode, initialized);
13714 if (temp == NULL)
13715 return NULL;
13716 add_loc_descr (&loc_result, temp);
13717 mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13718 add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13721 break;
13723 case CONST_INT:
13724 if (mode != VOIDmode && mode != BLKmode)
13725 loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13726 INTVAL (rtl));
13727 break;
13729 case CONST_DOUBLE:
13730 if (mode == VOIDmode)
13731 mode = GET_MODE (rtl);
13733 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13735 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13737 /* Note that a CONST_DOUBLE rtx could represent either an integer
13738 or a floating-point constant. A CONST_DOUBLE is used whenever
13739 the constant requires more than one word in order to be
13740 adequately represented. We output CONST_DOUBLEs as blocks. */
13741 loc_result = new_loc_descr (DW_OP_implicit_value,
13742 GET_MODE_SIZE (mode), 0);
13743 #if TARGET_SUPPORTS_WIDE_INT == 0
13744 if (!SCALAR_FLOAT_MODE_P (mode))
13746 loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13747 loc_result->dw_loc_oprnd2.v.val_double
13748 = rtx_to_double_int (rtl);
13750 else
13751 #endif
13753 unsigned int length = GET_MODE_SIZE (mode);
13754 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
13756 insert_float (rtl, array);
13757 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13758 loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13759 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13760 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13763 break;
13765 case CONST_WIDE_INT:
13766 if (mode == VOIDmode)
13767 mode = GET_MODE (rtl);
13769 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13771 loc_result = new_loc_descr (DW_OP_implicit_value,
13772 GET_MODE_SIZE (mode), 0);
13773 loc_result->dw_loc_oprnd2.val_class = dw_val_class_wide_int;
13774 loc_result->dw_loc_oprnd2.v.val_wide = ggc_alloc<wide_int> ();
13775 *loc_result->dw_loc_oprnd2.v.val_wide = std::make_pair (rtl, mode);
13777 break;
13779 case CONST_VECTOR:
13780 if (mode == VOIDmode)
13781 mode = GET_MODE (rtl);
13783 if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13785 unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13786 unsigned int length = CONST_VECTOR_NUNITS (rtl);
13787 unsigned char *array
13788 = ggc_vec_alloc<unsigned char> (length * elt_size);
13789 unsigned int i;
13790 unsigned char *p;
13791 machine_mode imode = GET_MODE_INNER (mode);
13793 gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13794 switch (GET_MODE_CLASS (mode))
13796 case MODE_VECTOR_INT:
13797 for (i = 0, p = array; i < length; i++, p += elt_size)
13799 rtx elt = CONST_VECTOR_ELT (rtl, i);
13800 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
13802 break;
13804 case MODE_VECTOR_FLOAT:
13805 for (i = 0, p = array; i < length; i++, p += elt_size)
13807 rtx elt = CONST_VECTOR_ELT (rtl, i);
13808 insert_float (elt, p);
13810 break;
13812 default:
13813 gcc_unreachable ();
13816 loc_result = new_loc_descr (DW_OP_implicit_value,
13817 length * elt_size, 0);
13818 loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13819 loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13820 loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13821 loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13823 break;
13825 case CONST:
13826 if (mode == VOIDmode
13827 || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13828 || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13829 || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13831 loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13832 break;
13834 /* FALLTHROUGH */
13835 case SYMBOL_REF:
13836 if (!const_ok_for_output (rtl))
13837 break;
13838 case LABEL_REF:
13839 if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13840 && (dwarf_version >= 4 || !dwarf_strict))
13842 loc_result = new_addr_loc_descr (rtl, dtprel_false);
13843 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13844 vec_safe_push (used_rtx_array, rtl);
13846 break;
13848 case DEBUG_IMPLICIT_PTR:
13849 loc_result = implicit_ptr_descriptor (rtl, 0);
13850 break;
13852 case PLUS:
13853 if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13854 && CONST_INT_P (XEXP (rtl, 1)))
13856 loc_result
13857 = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13858 break;
13860 /* FALLTHRU */
13861 do_default:
13862 default:
13863 if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13864 && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13865 && dwarf_version >= 4)
13866 || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13868 /* Value expression. */
13869 loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13870 if (loc_result)
13871 add_loc_descr (&loc_result,
13872 new_loc_descr (DW_OP_stack_value, 0, 0));
13874 break;
13877 return loc_result;
13880 /* We need to figure out what section we should use as the base for the
13881 address ranges where a given location is valid.
13882 1. If this particular DECL has a section associated with it, use that.
13883 2. If this function has a section associated with it, use that.
13884 3. Otherwise, use the text section.
13885 XXX: If you split a variable across multiple sections, we won't notice. */
13887 static const char *
13888 secname_for_decl (const_tree decl)
13890 const char *secname;
13892 if (VAR_OR_FUNCTION_DECL_P (decl)
13893 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl) || TREE_STATIC (decl))
13894 && DECL_SECTION_NAME (decl))
13895 secname = DECL_SECTION_NAME (decl);
13896 else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13897 secname = DECL_SECTION_NAME (current_function_decl);
13898 else if (cfun && in_cold_section_p)
13899 secname = crtl->subsections.cold_section_label;
13900 else
13901 secname = text_section_label;
13903 return secname;
13906 /* Return true when DECL_BY_REFERENCE is defined and set for DECL. */
13908 static bool
13909 decl_by_reference_p (tree decl)
13911 return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13912 || TREE_CODE (decl) == VAR_DECL)
13913 && DECL_BY_REFERENCE (decl));
13916 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
13917 for VARLOC. */
13919 static dw_loc_descr_ref
13920 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13921 enum var_init_status initialized)
13923 int have_address = 0;
13924 dw_loc_descr_ref descr;
13925 machine_mode mode;
13927 if (want_address != 2)
13929 gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13930 /* Single part. */
13931 if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13933 varloc = PAT_VAR_LOCATION_LOC (varloc);
13934 if (GET_CODE (varloc) == EXPR_LIST)
13935 varloc = XEXP (varloc, 0);
13936 mode = GET_MODE (varloc);
13937 if (MEM_P (varloc))
13939 rtx addr = XEXP (varloc, 0);
13940 descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13941 mode, initialized);
13942 if (descr)
13943 have_address = 1;
13944 else
13946 rtx x = avoid_constant_pool_reference (varloc);
13947 if (x != varloc)
13948 descr = mem_loc_descriptor (x, mode, VOIDmode,
13949 initialized);
13952 else
13953 descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13955 else
13956 return 0;
13958 else
13960 if (GET_CODE (varloc) == VAR_LOCATION)
13961 mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13962 else
13963 mode = DECL_MODE (loc);
13964 descr = loc_descriptor (varloc, mode, initialized);
13965 have_address = 1;
13968 if (!descr)
13969 return 0;
13971 if (want_address == 2 && !have_address
13972 && (dwarf_version >= 4 || !dwarf_strict))
13974 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13976 expansion_failed (loc, NULL_RTX,
13977 "DWARF address size mismatch");
13978 return 0;
13980 add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13981 have_address = 1;
13983 /* Show if we can't fill the request for an address. */
13984 if (want_address && !have_address)
13986 expansion_failed (loc, NULL_RTX,
13987 "Want address and only have value");
13988 return 0;
13991 /* If we've got an address and don't want one, dereference. */
13992 if (!want_address && have_address)
13994 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13995 enum dwarf_location_atom op;
13997 if (size > DWARF2_ADDR_SIZE || size == -1)
13999 expansion_failed (loc, NULL_RTX,
14000 "DWARF address size mismatch");
14001 return 0;
14003 else if (size == DWARF2_ADDR_SIZE)
14004 op = DW_OP_deref;
14005 else
14006 op = DW_OP_deref_size;
14008 add_loc_descr (&descr, new_loc_descr (op, size, 0));
14011 return descr;
14014 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14015 if it is not possible. */
14017 static dw_loc_descr_ref
14018 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14020 if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14021 return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14022 else if (dwarf_version >= 3 || !dwarf_strict)
14023 return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14024 else
14025 return NULL;
14028 /* Helper function for dw_loc_list. Compute proper Dwarf location descriptor
14029 for VAR_LOC_NOTE for variable DECL that has been optimized by SRA. */
14031 static dw_loc_descr_ref
14032 dw_sra_loc_expr (tree decl, rtx loc)
14034 rtx p;
14035 unsigned HOST_WIDE_INT padsize = 0;
14036 dw_loc_descr_ref descr, *descr_tail;
14037 unsigned HOST_WIDE_INT decl_size;
14038 rtx varloc;
14039 enum var_init_status initialized;
14041 if (DECL_SIZE (decl) == NULL
14042 || !tree_fits_uhwi_p (DECL_SIZE (decl)))
14043 return NULL;
14045 decl_size = tree_to_uhwi (DECL_SIZE (decl));
14046 descr = NULL;
14047 descr_tail = &descr;
14049 for (p = loc; p; p = XEXP (p, 1))
14051 unsigned HOST_WIDE_INT bitsize = decl_piece_bitsize (p);
14052 rtx loc_note = *decl_piece_varloc_ptr (p);
14053 dw_loc_descr_ref cur_descr;
14054 dw_loc_descr_ref *tail, last = NULL;
14055 unsigned HOST_WIDE_INT opsize = 0;
14057 if (loc_note == NULL_RTX
14058 || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14060 padsize += bitsize;
14061 continue;
14063 initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14064 varloc = NOTE_VAR_LOCATION (loc_note);
14065 cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14066 if (cur_descr == NULL)
14068 padsize += bitsize;
14069 continue;
14072 /* Check that cur_descr either doesn't use
14073 DW_OP_*piece operations, or their sum is equal
14074 to bitsize. Otherwise we can't embed it. */
14075 for (tail = &cur_descr; *tail != NULL;
14076 tail = &(*tail)->dw_loc_next)
14077 if ((*tail)->dw_loc_opc == DW_OP_piece)
14079 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14080 * BITS_PER_UNIT;
14081 last = *tail;
14083 else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14085 opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14086 last = *tail;
14089 if (last != NULL && opsize != bitsize)
14091 padsize += bitsize;
14092 /* Discard the current piece of the descriptor and release any
14093 addr_table entries it uses. */
14094 remove_loc_list_addr_table_entries (cur_descr);
14095 continue;
14098 /* If there is a hole, add DW_OP_*piece after empty DWARF
14099 expression, which means that those bits are optimized out. */
14100 if (padsize)
14102 if (padsize > decl_size)
14104 remove_loc_list_addr_table_entries (cur_descr);
14105 goto discard_descr;
14107 decl_size -= padsize;
14108 *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14109 if (*descr_tail == NULL)
14111 remove_loc_list_addr_table_entries (cur_descr);
14112 goto discard_descr;
14114 descr_tail = &(*descr_tail)->dw_loc_next;
14115 padsize = 0;
14117 *descr_tail = cur_descr;
14118 descr_tail = tail;
14119 if (bitsize > decl_size)
14120 goto discard_descr;
14121 decl_size -= bitsize;
14122 if (last == NULL)
14124 HOST_WIDE_INT offset = 0;
14125 if (GET_CODE (varloc) == VAR_LOCATION
14126 && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14128 varloc = PAT_VAR_LOCATION_LOC (varloc);
14129 if (GET_CODE (varloc) == EXPR_LIST)
14130 varloc = XEXP (varloc, 0);
14134 if (GET_CODE (varloc) == CONST
14135 || GET_CODE (varloc) == SIGN_EXTEND
14136 || GET_CODE (varloc) == ZERO_EXTEND)
14137 varloc = XEXP (varloc, 0);
14138 else if (GET_CODE (varloc) == SUBREG)
14139 varloc = SUBREG_REG (varloc);
14140 else
14141 break;
14143 while (1);
14144 /* DW_OP_bit_size offset should be zero for register
14145 or implicit location descriptions and empty location
14146 descriptions, but for memory addresses needs big endian
14147 adjustment. */
14148 if (MEM_P (varloc))
14150 unsigned HOST_WIDE_INT memsize
14151 = MEM_SIZE (varloc) * BITS_PER_UNIT;
14152 if (memsize != bitsize)
14154 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14155 && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14156 goto discard_descr;
14157 if (memsize < bitsize)
14158 goto discard_descr;
14159 if (BITS_BIG_ENDIAN)
14160 offset = memsize - bitsize;
14164 *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14165 if (*descr_tail == NULL)
14166 goto discard_descr;
14167 descr_tail = &(*descr_tail)->dw_loc_next;
14171 /* If there were any non-empty expressions, add padding till the end of
14172 the decl. */
14173 if (descr != NULL && decl_size != 0)
14175 *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14176 if (*descr_tail == NULL)
14177 goto discard_descr;
14179 return descr;
14181 discard_descr:
14182 /* Discard the descriptor and release any addr_table entries it uses. */
14183 remove_loc_list_addr_table_entries (descr);
14184 return NULL;
14187 /* Return the dwarf representation of the location list LOC_LIST of
14188 DECL. WANT_ADDRESS has the same meaning as in loc_list_from_tree
14189 function. */
14191 static dw_loc_list_ref
14192 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14194 const char *endname, *secname;
14195 rtx varloc;
14196 enum var_init_status initialized;
14197 struct var_loc_node *node;
14198 dw_loc_descr_ref descr;
14199 char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14200 dw_loc_list_ref list = NULL;
14201 dw_loc_list_ref *listp = &list;
14203 /* Now that we know what section we are using for a base,
14204 actually construct the list of locations.
14205 The first location information is what is passed to the
14206 function that creates the location list, and the remaining
14207 locations just get added on to that list.
14208 Note that we only know the start address for a location
14209 (IE location changes), so to build the range, we use
14210 the range [current location start, next location start].
14211 This means we have to special case the last node, and generate
14212 a range of [last location start, end of function label]. */
14214 secname = secname_for_decl (decl);
14216 for (node = loc_list->first; node; node = node->next)
14217 if (GET_CODE (node->loc) == EXPR_LIST
14218 || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14220 if (GET_CODE (node->loc) == EXPR_LIST)
14222 /* This requires DW_OP_{,bit_}piece, which is not usable
14223 inside DWARF expressions. */
14224 if (want_address != 2)
14225 continue;
14226 descr = dw_sra_loc_expr (decl, node->loc);
14227 if (descr == NULL)
14228 continue;
14230 else
14232 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14233 varloc = NOTE_VAR_LOCATION (node->loc);
14234 descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14236 if (descr)
14238 bool range_across_switch = false;
14239 /* If section switch happens in between node->label
14240 and node->next->label (or end of function) and
14241 we can't emit it as a single entry list,
14242 emit two ranges, first one ending at the end
14243 of first partition and second one starting at the
14244 beginning of second partition. */
14245 if (node == loc_list->last_before_switch
14246 && (node != loc_list->first || loc_list->first->next)
14247 && current_function_decl)
14249 endname = cfun->fde->dw_fde_end;
14250 range_across_switch = true;
14252 /* The variable has a location between NODE->LABEL and
14253 NODE->NEXT->LABEL. */
14254 else if (node->next)
14255 endname = node->next->label;
14256 /* If the variable has a location at the last label
14257 it keeps its location until the end of function. */
14258 else if (!current_function_decl)
14259 endname = text_end_label;
14260 else
14262 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14263 current_function_funcdef_no);
14264 endname = ggc_strdup (label_id);
14267 *listp = new_loc_list (descr, node->label, endname, secname);
14268 if (TREE_CODE (decl) == PARM_DECL
14269 && node == loc_list->first
14270 && NOTE_P (node->loc)
14271 && strcmp (node->label, endname) == 0)
14272 (*listp)->force = true;
14273 listp = &(*listp)->dw_loc_next;
14275 if (range_across_switch)
14277 if (GET_CODE (node->loc) == EXPR_LIST)
14278 descr = dw_sra_loc_expr (decl, node->loc);
14279 else
14281 initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14282 varloc = NOTE_VAR_LOCATION (node->loc);
14283 descr = dw_loc_list_1 (decl, varloc, want_address,
14284 initialized);
14286 gcc_assert (descr);
14287 /* The variable has a location between NODE->LABEL and
14288 NODE->NEXT->LABEL. */
14289 if (node->next)
14290 endname = node->next->label;
14291 else
14292 endname = cfun->fde->dw_fde_second_end;
14293 *listp = new_loc_list (descr,
14294 cfun->fde->dw_fde_second_begin,
14295 endname, secname);
14296 listp = &(*listp)->dw_loc_next;
14301 /* Try to avoid the overhead of a location list emitting a location
14302 expression instead, but only if we didn't have more than one
14303 location entry in the first place. If some entries were not
14304 representable, we don't want to pretend a single entry that was
14305 applies to the entire scope in which the variable is
14306 available. */
14307 if (list && loc_list->first->next)
14308 gen_llsym (list);
14310 return list;
14313 /* Return if the loc_list has only single element and thus can be represented
14314 as location description. */
14316 static bool
14317 single_element_loc_list_p (dw_loc_list_ref list)
14319 gcc_assert (!list->dw_loc_next || list->ll_symbol);
14320 return !list->ll_symbol;
14323 /* To each location in list LIST add loc descr REF. */
14325 static void
14326 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14328 dw_loc_descr_ref copy;
14329 add_loc_descr (&list->expr, ref);
14330 list = list->dw_loc_next;
14331 while (list)
14333 copy = ggc_alloc<dw_loc_descr_node> ();
14334 memcpy (copy, ref, sizeof (dw_loc_descr_node));
14335 add_loc_descr (&list->expr, copy);
14336 while (copy->dw_loc_next)
14338 dw_loc_descr_ref new_copy = ggc_alloc<dw_loc_descr_node> ();
14339 memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14340 copy->dw_loc_next = new_copy;
14341 copy = new_copy;
14343 list = list->dw_loc_next;
14347 /* Given two lists RET and LIST
14348 produce location list that is result of adding expression in LIST
14349 to expression in RET on each position in program.
14350 Might be destructive on both RET and LIST.
14352 TODO: We handle only simple cases of RET or LIST having at most one
14353 element. General case would inolve sorting the lists in program order
14354 and merging them that will need some additional work.
14355 Adding that will improve quality of debug info especially for SRA-ed
14356 structures. */
14358 static void
14359 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14361 if (!list)
14362 return;
14363 if (!*ret)
14365 *ret = list;
14366 return;
14368 if (!list->dw_loc_next)
14370 add_loc_descr_to_each (*ret, list->expr);
14371 return;
14373 if (!(*ret)->dw_loc_next)
14375 add_loc_descr_to_each (list, (*ret)->expr);
14376 *ret = list;
14377 return;
14379 expansion_failed (NULL_TREE, NULL_RTX,
14380 "Don't know how to merge two non-trivial"
14381 " location lists.\n");
14382 *ret = NULL;
14383 return;
14386 /* LOC is constant expression. Try a luck, look it up in constant
14387 pool and return its loc_descr of its address. */
14389 static dw_loc_descr_ref
14390 cst_pool_loc_descr (tree loc)
14392 /* Get an RTL for this, if something has been emitted. */
14393 rtx rtl = lookup_constant_def (loc);
14395 if (!rtl || !MEM_P (rtl))
14397 gcc_assert (!rtl);
14398 return 0;
14400 gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14402 /* TODO: We might get more coverage if we was actually delaying expansion
14403 of all expressions till end of compilation when constant pools are fully
14404 populated. */
14405 if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14407 expansion_failed (loc, NULL_RTX,
14408 "CST value in contant pool but not marked.");
14409 return 0;
14411 return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
14412 GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
14415 /* Return dw_loc_list representing address of addr_expr LOC
14416 by looking for inner INDIRECT_REF expression and turning
14417 it into simple arithmetics.
14419 See loc_list_from_tree for the meaning of CONTEXT. */
14421 static dw_loc_list_ref
14422 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev,
14423 const loc_descr_context *context)
14425 tree obj, offset;
14426 HOST_WIDE_INT bitsize, bitpos, bytepos;
14427 machine_mode mode;
14428 int unsignedp, volatilep = 0;
14429 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14431 obj = get_inner_reference (TREE_OPERAND (loc, 0),
14432 &bitsize, &bitpos, &offset, &mode,
14433 &unsignedp, &volatilep, false);
14434 STRIP_NOPS (obj);
14435 if (bitpos % BITS_PER_UNIT)
14437 expansion_failed (loc, NULL_RTX, "bitfield access");
14438 return 0;
14440 if (!INDIRECT_REF_P (obj))
14442 expansion_failed (obj,
14443 NULL_RTX, "no indirect ref in inner refrence");
14444 return 0;
14446 if (!offset && !bitpos)
14447 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1,
14448 context);
14449 else if (toplev
14450 && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14451 && (dwarf_version >= 4 || !dwarf_strict))
14453 list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0, context);
14454 if (!list_ret)
14455 return 0;
14456 if (offset)
14458 /* Variable offset. */
14459 list_ret1 = loc_list_from_tree (offset, 0, context);
14460 if (list_ret1 == 0)
14461 return 0;
14462 add_loc_list (&list_ret, list_ret1);
14463 if (!list_ret)
14464 return 0;
14465 add_loc_descr_to_each (list_ret,
14466 new_loc_descr (DW_OP_plus, 0, 0));
14468 bytepos = bitpos / BITS_PER_UNIT;
14469 if (bytepos > 0)
14470 add_loc_descr_to_each (list_ret,
14471 new_loc_descr (DW_OP_plus_uconst,
14472 bytepos, 0));
14473 else if (bytepos < 0)
14474 loc_list_plus_const (list_ret, bytepos);
14475 add_loc_descr_to_each (list_ret,
14476 new_loc_descr (DW_OP_stack_value, 0, 0));
14478 return list_ret;
14482 /* Helper structure for location descriptions generation. */
14483 struct loc_descr_context
14485 /* The type that is implicitly referenced by DW_OP_push_object_address, or
14486 NULL_TREE if DW_OP_push_object_address in invalid for this location
14487 description. This is used when processing PLACEHOLDER_EXPR nodes. */
14488 tree context_type;
14489 /* The ..._DECL node that should be translated as a
14490 DW_OP_push_object_address operation. */
14491 tree base_decl;
14494 /* Generate Dwarf location list representing LOC.
14495 If WANT_ADDRESS is false, expression computing LOC will be computed
14496 If WANT_ADDRESS is 1, expression computing address of LOC will be returned
14497 if WANT_ADDRESS is 2, expression computing address useable in location
14498 will be returned (i.e. DW_OP_reg can be used
14499 to refer to register values).
14501 CONTEXT provides information to customize the location descriptions
14502 generation. Its context_type field specifies what type is implicitly
14503 referenced by DW_OP_push_object_address. If it is NULL_TREE, this operation
14504 will not be generated.
14506 If CONTEXT is NULL, the behavior is the same as if both context_type and
14507 base_decl fields were NULL_TREE. */
14509 static dw_loc_list_ref
14510 loc_list_from_tree (tree loc, int want_address,
14511 const struct loc_descr_context *context)
14513 dw_loc_descr_ref ret = NULL, ret1 = NULL;
14514 dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14515 int have_address = 0;
14516 enum dwarf_location_atom op;
14518 /* ??? Most of the time we do not take proper care for sign/zero
14519 extending the values properly. Hopefully this won't be a real
14520 problem... */
14522 if (context != NULL
14523 && context->base_decl == loc
14524 && want_address == 0)
14526 if (dwarf_version >= 3 || !dwarf_strict)
14527 return new_loc_list (new_loc_descr (DW_OP_push_object_address, 0, 0),
14528 NULL, NULL, NULL);
14529 else
14530 return NULL;
14533 switch (TREE_CODE (loc))
14535 case ERROR_MARK:
14536 expansion_failed (loc, NULL_RTX, "ERROR_MARK");
14537 return 0;
14539 case PLACEHOLDER_EXPR:
14540 /* This case involves extracting fields from an object to determine the
14541 position of other fields. It is supposed to appear only as the first
14542 operand of COMPONENT_REF nodes and to reference precisely the type
14543 that the context allows. */
14544 if (context != NULL
14545 && TREE_TYPE (loc) == context->context_type
14546 && want_address >= 1)
14548 if (dwarf_version >= 3 || !dwarf_strict)
14550 ret = new_loc_descr (DW_OP_push_object_address, 0, 0);
14551 have_address = 1;
14552 break;
14554 else
14555 return NULL;
14557 else
14558 expansion_failed (loc, NULL_RTX,
14559 "PLACEHOLDER_EXPR for an unexpected type");
14560 break;
14562 case CALL_EXPR:
14563 expansion_failed (loc, NULL_RTX, "CALL_EXPR");
14564 /* There are no opcodes for these operations. */
14565 return 0;
14567 case PREINCREMENT_EXPR:
14568 case PREDECREMENT_EXPR:
14569 case POSTINCREMENT_EXPR:
14570 case POSTDECREMENT_EXPR:
14571 expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
14572 /* There are no opcodes for these operations. */
14573 return 0;
14575 case ADDR_EXPR:
14576 /* If we already want an address, see if there is INDIRECT_REF inside
14577 e.g. for &this->field. */
14578 if (want_address)
14580 list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
14581 (loc, want_address == 2, context);
14582 if (list_ret)
14583 have_address = 1;
14584 else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
14585 && (ret = cst_pool_loc_descr (loc)))
14586 have_address = 1;
14588 /* Otherwise, process the argument and look for the address. */
14589 if (!list_ret && !ret)
14590 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1, context);
14591 else
14593 if (want_address)
14594 expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14595 return NULL;
14597 break;
14599 case VAR_DECL:
14600 if (DECL_THREAD_LOCAL_P (loc))
14602 rtx rtl;
14603 enum dwarf_location_atom tls_op;
14604 enum dtprel_bool dtprel = dtprel_false;
14606 if (targetm.have_tls)
14608 /* If this is not defined, we have no way to emit the
14609 data. */
14610 if (!targetm.asm_out.output_dwarf_dtprel)
14611 return 0;
14613 /* The way DW_OP_GNU_push_tls_address is specified, we
14614 can only look up addresses of objects in the current
14615 module. We used DW_OP_addr as first op, but that's
14616 wrong, because DW_OP_addr is relocated by the debug
14617 info consumer, while DW_OP_GNU_push_tls_address
14618 operand shouldn't be. */
14619 if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14620 return 0;
14621 dtprel = dtprel_true;
14622 tls_op = DW_OP_GNU_push_tls_address;
14624 else
14626 if (!targetm.emutls.debug_form_tls_address
14627 || !(dwarf_version >= 3 || !dwarf_strict))
14628 return 0;
14629 /* We stuffed the control variable into the DECL_VALUE_EXPR
14630 to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
14631 no longer appear in gimple code. We used the control
14632 variable in specific so that we could pick it up here. */
14633 loc = DECL_VALUE_EXPR (loc);
14634 tls_op = DW_OP_form_tls_address;
14637 rtl = rtl_for_decl_location (loc);
14638 if (rtl == NULL_RTX)
14639 return 0;
14641 if (!MEM_P (rtl))
14642 return 0;
14643 rtl = XEXP (rtl, 0);
14644 if (! CONSTANT_P (rtl))
14645 return 0;
14647 ret = new_addr_loc_descr (rtl, dtprel);
14648 ret1 = new_loc_descr (tls_op, 0, 0);
14649 add_loc_descr (&ret, ret1);
14651 have_address = 1;
14652 break;
14654 /* FALLTHRU */
14656 case PARM_DECL:
14657 case RESULT_DECL:
14658 if (DECL_HAS_VALUE_EXPR_P (loc))
14659 return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14660 want_address, context);
14661 /* FALLTHRU */
14663 case FUNCTION_DECL:
14665 rtx rtl;
14666 var_loc_list *loc_list = lookup_decl_loc (loc);
14668 if (loc_list && loc_list->first)
14670 list_ret = dw_loc_list (loc_list, loc, want_address);
14671 have_address = want_address != 0;
14672 break;
14674 rtl = rtl_for_decl_location (loc);
14675 if (rtl == NULL_RTX)
14677 expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14678 return 0;
14680 else if (CONST_INT_P (rtl))
14682 HOST_WIDE_INT val = INTVAL (rtl);
14683 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14684 val &= GET_MODE_MASK (DECL_MODE (loc));
14685 ret = int_loc_descriptor (val);
14687 else if (GET_CODE (rtl) == CONST_STRING)
14689 expansion_failed (loc, NULL_RTX, "CONST_STRING");
14690 return 0;
14692 else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14693 ret = new_addr_loc_descr (rtl, dtprel_false);
14694 else
14696 machine_mode mode, mem_mode;
14698 /* Certain constructs can only be represented at top-level. */
14699 if (want_address == 2)
14701 ret = loc_descriptor (rtl, VOIDmode,
14702 VAR_INIT_STATUS_INITIALIZED);
14703 have_address = 1;
14705 else
14707 mode = GET_MODE (rtl);
14708 mem_mode = VOIDmode;
14709 if (MEM_P (rtl))
14711 mem_mode = mode;
14712 mode = get_address_mode (rtl);
14713 rtl = XEXP (rtl, 0);
14714 have_address = 1;
14716 ret = mem_loc_descriptor (rtl, mode, mem_mode,
14717 VAR_INIT_STATUS_INITIALIZED);
14719 if (!ret)
14720 expansion_failed (loc, rtl,
14721 "failed to produce loc descriptor for rtl");
14724 break;
14726 case MEM_REF:
14727 if (!integer_zerop (TREE_OPERAND (loc, 1)))
14729 have_address = 1;
14730 goto do_plus;
14732 /* Fallthru. */
14733 case INDIRECT_REF:
14734 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14735 have_address = 1;
14736 break;
14738 case TARGET_MEM_REF:
14739 case SSA_NAME:
14740 case DEBUG_EXPR_DECL:
14741 return NULL;
14743 case COMPOUND_EXPR:
14744 return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address, context);
14746 CASE_CONVERT:
14747 case VIEW_CONVERT_EXPR:
14748 case SAVE_EXPR:
14749 case MODIFY_EXPR:
14750 return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address, context);
14752 case COMPONENT_REF:
14753 case BIT_FIELD_REF:
14754 case ARRAY_REF:
14755 case ARRAY_RANGE_REF:
14756 case REALPART_EXPR:
14757 case IMAGPART_EXPR:
14759 tree obj, offset;
14760 HOST_WIDE_INT bitsize, bitpos, bytepos;
14761 machine_mode mode;
14762 int unsignedp, volatilep = 0;
14764 obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14765 &unsignedp, &volatilep, false);
14767 gcc_assert (obj != loc);
14769 list_ret = loc_list_from_tree (obj,
14770 want_address == 2
14771 && !bitpos && !offset ? 2 : 1,
14772 context);
14773 /* TODO: We can extract value of the small expression via shifting even
14774 for nonzero bitpos. */
14775 if (list_ret == 0)
14776 return 0;
14777 if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14779 expansion_failed (loc, NULL_RTX,
14780 "bitfield access");
14781 return 0;
14784 if (offset != NULL_TREE)
14786 /* Variable offset. */
14787 list_ret1 = loc_list_from_tree (offset, 0, context);
14788 if (list_ret1 == 0)
14789 return 0;
14790 add_loc_list (&list_ret, list_ret1);
14791 if (!list_ret)
14792 return 0;
14793 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14796 bytepos = bitpos / BITS_PER_UNIT;
14797 if (bytepos > 0)
14798 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14799 else if (bytepos < 0)
14800 loc_list_plus_const (list_ret, bytepos);
14802 have_address = 1;
14803 break;
14806 case INTEGER_CST:
14807 if ((want_address || !tree_fits_shwi_p (loc))
14808 && (ret = cst_pool_loc_descr (loc)))
14809 have_address = 1;
14810 else if (want_address == 2
14811 && tree_fits_shwi_p (loc)
14812 && (ret = address_of_int_loc_descriptor
14813 (int_size_in_bytes (TREE_TYPE (loc)),
14814 tree_to_shwi (loc))))
14815 have_address = 1;
14816 else if (tree_fits_shwi_p (loc))
14817 ret = int_loc_descriptor (tree_to_shwi (loc));
14818 else
14820 expansion_failed (loc, NULL_RTX,
14821 "Integer operand is not host integer");
14822 return 0;
14824 break;
14826 case CONSTRUCTOR:
14827 case REAL_CST:
14828 case STRING_CST:
14829 case COMPLEX_CST:
14830 if ((ret = cst_pool_loc_descr (loc)))
14831 have_address = 1;
14832 else
14833 /* We can construct small constants here using int_loc_descriptor. */
14834 expansion_failed (loc, NULL_RTX,
14835 "constructor or constant not in constant pool");
14836 break;
14838 case TRUTH_AND_EXPR:
14839 case TRUTH_ANDIF_EXPR:
14840 case BIT_AND_EXPR:
14841 op = DW_OP_and;
14842 goto do_binop;
14844 case TRUTH_XOR_EXPR:
14845 case BIT_XOR_EXPR:
14846 op = DW_OP_xor;
14847 goto do_binop;
14849 case TRUTH_OR_EXPR:
14850 case TRUTH_ORIF_EXPR:
14851 case BIT_IOR_EXPR:
14852 op = DW_OP_or;
14853 goto do_binop;
14855 case FLOOR_DIV_EXPR:
14856 case CEIL_DIV_EXPR:
14857 case ROUND_DIV_EXPR:
14858 case TRUNC_DIV_EXPR:
14859 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14860 return 0;
14861 op = DW_OP_div;
14862 goto do_binop;
14864 case MINUS_EXPR:
14865 op = DW_OP_minus;
14866 goto do_binop;
14868 case FLOOR_MOD_EXPR:
14869 case CEIL_MOD_EXPR:
14870 case ROUND_MOD_EXPR:
14871 case TRUNC_MOD_EXPR:
14872 if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14874 op = DW_OP_mod;
14875 goto do_binop;
14877 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14878 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14879 if (list_ret == 0 || list_ret1 == 0)
14880 return 0;
14882 add_loc_list (&list_ret, list_ret1);
14883 if (list_ret == 0)
14884 return 0;
14885 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14886 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14887 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14888 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14889 add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14890 break;
14892 case MULT_EXPR:
14893 op = DW_OP_mul;
14894 goto do_binop;
14896 case LSHIFT_EXPR:
14897 op = DW_OP_shl;
14898 goto do_binop;
14900 case RSHIFT_EXPR:
14901 op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14902 goto do_binop;
14904 case POINTER_PLUS_EXPR:
14905 case PLUS_EXPR:
14906 do_plus:
14907 if (tree_fits_shwi_p (TREE_OPERAND (loc, 1)))
14909 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14910 if (list_ret == 0)
14911 return 0;
14913 loc_list_plus_const (list_ret, tree_to_shwi (TREE_OPERAND (loc, 1)));
14914 break;
14917 op = DW_OP_plus;
14918 goto do_binop;
14920 case LE_EXPR:
14921 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14922 return 0;
14924 op = DW_OP_le;
14925 goto do_binop;
14927 case GE_EXPR:
14928 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14929 return 0;
14931 op = DW_OP_ge;
14932 goto do_binop;
14934 case LT_EXPR:
14935 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14936 return 0;
14938 op = DW_OP_lt;
14939 goto do_binop;
14941 case GT_EXPR:
14942 if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14943 return 0;
14945 op = DW_OP_gt;
14946 goto do_binop;
14948 case EQ_EXPR:
14949 op = DW_OP_eq;
14950 goto do_binop;
14952 case NE_EXPR:
14953 op = DW_OP_ne;
14954 goto do_binop;
14956 do_binop:
14957 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14958 list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0, context);
14959 if (list_ret == 0 || list_ret1 == 0)
14960 return 0;
14962 add_loc_list (&list_ret, list_ret1);
14963 if (list_ret == 0)
14964 return 0;
14965 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14966 break;
14968 case TRUTH_NOT_EXPR:
14969 case BIT_NOT_EXPR:
14970 op = DW_OP_not;
14971 goto do_unop;
14973 case ABS_EXPR:
14974 op = DW_OP_abs;
14975 goto do_unop;
14977 case NEGATE_EXPR:
14978 op = DW_OP_neg;
14979 goto do_unop;
14981 do_unop:
14982 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
14983 if (list_ret == 0)
14984 return 0;
14986 add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14987 break;
14989 case MIN_EXPR:
14990 case MAX_EXPR:
14992 const enum tree_code code =
14993 TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14995 loc = build3 (COND_EXPR, TREE_TYPE (loc),
14996 build2 (code, integer_type_node,
14997 TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14998 TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15001 /* ... fall through ... */
15003 case COND_EXPR:
15005 dw_loc_descr_ref lhs
15006 = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0, context);
15007 dw_loc_list_ref rhs
15008 = loc_list_from_tree (TREE_OPERAND (loc, 2), 0, context);
15009 dw_loc_descr_ref bra_node, jump_node, tmp;
15011 list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0, context);
15012 if (list_ret == 0 || lhs == 0 || rhs == 0)
15013 return 0;
15015 bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15016 add_loc_descr_to_each (list_ret, bra_node);
15018 add_loc_list (&list_ret, rhs);
15019 jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15020 add_loc_descr_to_each (list_ret, jump_node);
15022 add_loc_descr_to_each (list_ret, lhs);
15023 bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15024 bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15026 /* ??? Need a node to point the skip at. Use a nop. */
15027 tmp = new_loc_descr (DW_OP_nop, 0, 0);
15028 add_loc_descr_to_each (list_ret, tmp);
15029 jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15030 jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15032 break;
15034 case FIX_TRUNC_EXPR:
15035 return 0;
15037 default:
15038 /* Leave front-end specific codes as simply unknown. This comes
15039 up, for instance, with the C STMT_EXPR. */
15040 if ((unsigned int) TREE_CODE (loc)
15041 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15043 expansion_failed (loc, NULL_RTX,
15044 "language specific tree node");
15045 return 0;
15048 #ifdef ENABLE_CHECKING
15049 /* Otherwise this is a generic code; we should just lists all of
15050 these explicitly. We forgot one. */
15051 gcc_unreachable ();
15052 #else
15053 /* In a release build, we want to degrade gracefully: better to
15054 generate incomplete debugging information than to crash. */
15055 return NULL;
15056 #endif
15059 if (!ret && !list_ret)
15060 return 0;
15062 if (want_address == 2 && !have_address
15063 && (dwarf_version >= 4 || !dwarf_strict))
15065 if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15067 expansion_failed (loc, NULL_RTX,
15068 "DWARF address size mismatch");
15069 return 0;
15071 if (ret)
15072 add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15073 else
15074 add_loc_descr_to_each (list_ret,
15075 new_loc_descr (DW_OP_stack_value, 0, 0));
15076 have_address = 1;
15078 /* Show if we can't fill the request for an address. */
15079 if (want_address && !have_address)
15081 expansion_failed (loc, NULL_RTX,
15082 "Want address and only have value");
15083 return 0;
15086 gcc_assert (!ret || !list_ret);
15088 /* If we've got an address and don't want one, dereference. */
15089 if (!want_address && have_address)
15091 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15093 if (size > DWARF2_ADDR_SIZE || size == -1)
15095 expansion_failed (loc, NULL_RTX,
15096 "DWARF address size mismatch");
15097 return 0;
15099 else if (size == DWARF2_ADDR_SIZE)
15100 op = DW_OP_deref;
15101 else
15102 op = DW_OP_deref_size;
15104 if (ret)
15105 add_loc_descr (&ret, new_loc_descr (op, size, 0));
15106 else
15107 add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15109 if (ret)
15110 list_ret = new_loc_list (ret, NULL, NULL, NULL);
15112 return list_ret;
15115 /* Same as above but return only single location expression. */
15116 static dw_loc_descr_ref
15117 loc_descriptor_from_tree (tree loc, int want_address,
15118 const struct loc_descr_context *context)
15120 dw_loc_list_ref ret = loc_list_from_tree (loc, want_address, context);
15121 if (!ret)
15122 return NULL;
15123 if (ret->dw_loc_next)
15125 expansion_failed (loc, NULL_RTX,
15126 "Location list where only loc descriptor needed");
15127 return NULL;
15129 return ret->expr;
15132 /* Given a value, round it up to the lowest multiple of `boundary'
15133 which is not less than the value itself. */
15135 static inline HOST_WIDE_INT
15136 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15138 return (((value + boundary - 1) / boundary) * boundary);
15141 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15142 pointer to the declared type for the relevant field variable, or return
15143 `integer_type_node' if the given node turns out to be an
15144 ERROR_MARK node. */
15146 static inline tree
15147 field_type (const_tree decl)
15149 tree type;
15151 if (TREE_CODE (decl) == ERROR_MARK)
15152 return integer_type_node;
15154 type = DECL_BIT_FIELD_TYPE (decl);
15155 if (type == NULL_TREE)
15156 type = TREE_TYPE (decl);
15158 return type;
15161 /* Given a pointer to a tree node, return the alignment in bits for
15162 it, or else return BITS_PER_WORD if the node actually turns out to
15163 be an ERROR_MARK node. */
15165 static inline unsigned
15166 simple_type_align_in_bits (const_tree type)
15168 return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15171 static inline unsigned
15172 simple_decl_align_in_bits (const_tree decl)
15174 return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15177 /* Return the result of rounding T up to ALIGN. */
15179 static inline offset_int
15180 round_up_to_align (const offset_int &t, unsigned int align)
15182 return wi::udiv_trunc (t + align - 1, align) * align;
15185 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15186 lowest addressed byte of the "containing object" for the given FIELD_DECL,
15187 or return 0 if we are unable to determine what that offset is, either
15188 because the argument turns out to be a pointer to an ERROR_MARK node, or
15189 because the offset is actually variable. (We can't handle the latter case
15190 just yet). */
15192 static HOST_WIDE_INT
15193 field_byte_offset (const_tree decl)
15195 offset_int object_offset_in_bits;
15196 offset_int object_offset_in_bytes;
15197 offset_int bitpos_int;
15199 if (TREE_CODE (decl) == ERROR_MARK)
15200 return 0;
15202 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15204 /* We cannot yet cope with fields whose positions are variable, so
15205 for now, when we see such things, we simply return 0. Someday, we may
15206 be able to handle such cases, but it will be damn difficult. */
15207 if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15208 return 0;
15210 bitpos_int = wi::to_offset (bit_position (decl));
15212 if (PCC_BITFIELD_TYPE_MATTERS)
15214 tree type;
15215 tree field_size_tree;
15216 offset_int deepest_bitpos;
15217 offset_int field_size_in_bits;
15218 unsigned int type_align_in_bits;
15219 unsigned int decl_align_in_bits;
15220 offset_int type_size_in_bits;
15222 type = field_type (decl);
15223 type_size_in_bits = offset_int_type_size_in_bits (type);
15224 type_align_in_bits = simple_type_align_in_bits (type);
15226 field_size_tree = DECL_SIZE (decl);
15228 /* The size could be unspecified if there was an error, or for
15229 a flexible array member. */
15230 if (!field_size_tree)
15231 field_size_tree = bitsize_zero_node;
15233 /* If the size of the field is not constant, use the type size. */
15234 if (TREE_CODE (field_size_tree) == INTEGER_CST)
15235 field_size_in_bits = wi::to_offset (field_size_tree);
15236 else
15237 field_size_in_bits = type_size_in_bits;
15239 decl_align_in_bits = simple_decl_align_in_bits (decl);
15241 /* The GCC front-end doesn't make any attempt to keep track of the
15242 starting bit offset (relative to the start of the containing
15243 structure type) of the hypothetical "containing object" for a
15244 bit-field. Thus, when computing the byte offset value for the
15245 start of the "containing object" of a bit-field, we must deduce
15246 this information on our own. This can be rather tricky to do in
15247 some cases. For example, handling the following structure type
15248 definition when compiling for an i386/i486 target (which only
15249 aligns long long's to 32-bit boundaries) can be very tricky:
15251 struct S { int field1; long long field2:31; };
15253 Fortunately, there is a simple rule-of-thumb which can be used
15254 in such cases. When compiling for an i386/i486, GCC will
15255 allocate 8 bytes for the structure shown above. It decides to
15256 do this based upon one simple rule for bit-field allocation.
15257 GCC allocates each "containing object" for each bit-field at
15258 the first (i.e. lowest addressed) legitimate alignment boundary
15259 (based upon the required minimum alignment for the declared
15260 type of the field) which it can possibly use, subject to the
15261 condition that there is still enough available space remaining
15262 in the containing object (when allocated at the selected point)
15263 to fully accommodate all of the bits of the bit-field itself.
15265 This simple rule makes it obvious why GCC allocates 8 bytes for
15266 each object of the structure type shown above. When looking
15267 for a place to allocate the "containing object" for `field2',
15268 the compiler simply tries to allocate a 64-bit "containing
15269 object" at each successive 32-bit boundary (starting at zero)
15270 until it finds a place to allocate that 64- bit field such that
15271 at least 31 contiguous (and previously unallocated) bits remain
15272 within that selected 64 bit field. (As it turns out, for the
15273 example above, the compiler finds it is OK to allocate the
15274 "containing object" 64-bit field at bit-offset zero within the
15275 structure type.)
15277 Here we attempt to work backwards from the limited set of facts
15278 we're given, and we try to deduce from those facts, where GCC
15279 must have believed that the containing object started (within
15280 the structure type). The value we deduce is then used (by the
15281 callers of this routine) to generate DW_AT_location and
15282 DW_AT_bit_offset attributes for fields (both bit-fields and, in
15283 the case of DW_AT_location, regular fields as well). */
15285 /* Figure out the bit-distance from the start of the structure to
15286 the "deepest" bit of the bit-field. */
15287 deepest_bitpos = bitpos_int + field_size_in_bits;
15289 /* This is the tricky part. Use some fancy footwork to deduce
15290 where the lowest addressed bit of the containing object must
15291 be. */
15292 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15294 /* Round up to type_align by default. This works best for
15295 bitfields. */
15296 object_offset_in_bits
15297 = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15299 if (wi::gtu_p (object_offset_in_bits, bitpos_int))
15301 object_offset_in_bits = deepest_bitpos - type_size_in_bits;
15303 /* Round up to decl_align instead. */
15304 object_offset_in_bits
15305 = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15308 else
15309 object_offset_in_bits = bitpos_int;
15311 object_offset_in_bytes
15312 = wi::lrshift (object_offset_in_bits, LOG2_BITS_PER_UNIT);
15313 return object_offset_in_bytes.to_shwi ();
15316 /* The following routines define various Dwarf attributes and any data
15317 associated with them. */
15319 /* Add a location description attribute value to a DIE.
15321 This emits location attributes suitable for whole variables and
15322 whole parameters. Note that the location attributes for struct fields are
15323 generated by the routine `data_member_location_attribute' below. */
15325 static inline void
15326 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15327 dw_loc_list_ref descr)
15329 if (descr == 0)
15330 return;
15331 if (single_element_loc_list_p (descr))
15332 add_AT_loc (die, attr_kind, descr->expr);
15333 else
15334 add_AT_loc_list (die, attr_kind, descr);
15337 /* Add DW_AT_accessibility attribute to DIE if needed. */
15339 static void
15340 add_accessibility_attribute (dw_die_ref die, tree decl)
15342 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
15343 children, otherwise the default is DW_ACCESS_public. In DWARF2
15344 the default has always been DW_ACCESS_public. */
15345 if (TREE_PROTECTED (decl))
15346 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15347 else if (TREE_PRIVATE (decl))
15349 if (dwarf_version == 2
15350 || die->die_parent == NULL
15351 || die->die_parent->die_tag != DW_TAG_class_type)
15352 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15354 else if (dwarf_version > 2
15355 && die->die_parent
15356 && die->die_parent->die_tag == DW_TAG_class_type)
15357 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
15360 /* Attach the specialized form of location attribute used for data members of
15361 struct and union types. In the special case of a FIELD_DECL node which
15362 represents a bit-field, the "offset" part of this special location
15363 descriptor must indicate the distance in bytes from the lowest-addressed
15364 byte of the containing struct or union type to the lowest-addressed byte of
15365 the "containing object" for the bit-field. (See the `field_byte_offset'
15366 function above).
15368 For any given bit-field, the "containing object" is a hypothetical object
15369 (of some integral or enum type) within which the given bit-field lives. The
15370 type of this hypothetical "containing object" is always the same as the
15371 declared type of the individual bit-field itself (for GCC anyway... the
15372 DWARF spec doesn't actually mandate this). Note that it is the size (in
15373 bytes) of the hypothetical "containing object" which will be given in the
15374 DW_AT_byte_size attribute for this bit-field. (See the
15375 `byte_size_attribute' function below.) It is also used when calculating the
15376 value of the DW_AT_bit_offset attribute. (See the `bit_offset_attribute'
15377 function below.) */
15379 static void
15380 add_data_member_location_attribute (dw_die_ref die, tree decl)
15382 HOST_WIDE_INT offset;
15383 dw_loc_descr_ref loc_descr = 0;
15385 if (TREE_CODE (decl) == TREE_BINFO)
15387 /* We're working on the TAG_inheritance for a base class. */
15388 if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15390 /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15391 aren't at a fixed offset from all (sub)objects of the same
15392 type. We need to extract the appropriate offset from our
15393 vtable. The following dwarf expression means
15395 BaseAddr = ObAddr + *((*ObAddr) - Offset)
15397 This is specific to the V3 ABI, of course. */
15399 dw_loc_descr_ref tmp;
15401 /* Make a copy of the object address. */
15402 tmp = new_loc_descr (DW_OP_dup, 0, 0);
15403 add_loc_descr (&loc_descr, tmp);
15405 /* Extract the vtable address. */
15406 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15407 add_loc_descr (&loc_descr, tmp);
15409 /* Calculate the address of the offset. */
15410 offset = tree_to_shwi (BINFO_VPTR_FIELD (decl));
15411 gcc_assert (offset < 0);
15413 tmp = int_loc_descriptor (-offset);
15414 add_loc_descr (&loc_descr, tmp);
15415 tmp = new_loc_descr (DW_OP_minus, 0, 0);
15416 add_loc_descr (&loc_descr, tmp);
15418 /* Extract the offset. */
15419 tmp = new_loc_descr (DW_OP_deref, 0, 0);
15420 add_loc_descr (&loc_descr, tmp);
15422 /* Add it to the object address. */
15423 tmp = new_loc_descr (DW_OP_plus, 0, 0);
15424 add_loc_descr (&loc_descr, tmp);
15426 else
15427 offset = tree_to_shwi (BINFO_OFFSET (decl));
15429 else
15430 offset = field_byte_offset (decl);
15432 if (! loc_descr)
15434 if (dwarf_version > 2)
15436 /* Don't need to output a location expression, just the constant. */
15437 if (offset < 0)
15438 add_AT_int (die, DW_AT_data_member_location, offset);
15439 else
15440 add_AT_unsigned (die, DW_AT_data_member_location, offset);
15441 return;
15443 else
15445 enum dwarf_location_atom op;
15447 /* The DWARF2 standard says that we should assume that the structure
15448 address is already on the stack, so we can specify a structure
15449 field address by using DW_OP_plus_uconst. */
15450 op = DW_OP_plus_uconst;
15451 loc_descr = new_loc_descr (op, offset, 0);
15455 add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15458 /* Writes integer values to dw_vec_const array. */
15460 static void
15461 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15463 while (size != 0)
15465 *dest++ = val & 0xff;
15466 val >>= 8;
15467 --size;
15471 /* Reads integers from dw_vec_const array. Inverse of insert_int. */
15473 static HOST_WIDE_INT
15474 extract_int (const unsigned char *src, unsigned int size)
15476 HOST_WIDE_INT val = 0;
15478 src += size;
15479 while (size != 0)
15481 val <<= 8;
15482 val |= *--src & 0xff;
15483 --size;
15485 return val;
15488 /* Writes wide_int values to dw_vec_const array. */
15490 static void
15491 insert_wide_int (const wide_int &val, unsigned char *dest, int elt_size)
15493 int i;
15495 if (elt_size <= HOST_BITS_PER_WIDE_INT/BITS_PER_UNIT)
15497 insert_int ((HOST_WIDE_INT) val.elt (0), elt_size, dest);
15498 return;
15501 /* We'd have to extend this code to support odd sizes. */
15502 gcc_assert (elt_size % (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT) == 0);
15504 int n = elt_size / (HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT);
15506 if (WORDS_BIG_ENDIAN)
15507 for (i = n - 1; i >= 0; i--)
15509 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15510 dest += sizeof (HOST_WIDE_INT);
15512 else
15513 for (i = 0; i < n; i++)
15515 insert_int ((HOST_WIDE_INT) val.elt (i), sizeof (HOST_WIDE_INT), dest);
15516 dest += sizeof (HOST_WIDE_INT);
15520 /* Writes floating point values to dw_vec_const array. */
15522 static void
15523 insert_float (const_rtx rtl, unsigned char *array)
15525 REAL_VALUE_TYPE rv;
15526 long val[4];
15527 int i;
15529 REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
15530 real_to_target (val, &rv, GET_MODE (rtl));
15532 /* real_to_target puts 32-bit pieces in each long. Pack them. */
15533 for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
15535 insert_int (val[i], 4, array);
15536 array += 4;
15540 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
15541 does not have a "location" either in memory or in a register. These
15542 things can arise in GNU C when a constant is passed as an actual parameter
15543 to an inlined function. They can also arise in C++ where declared
15544 constants do not necessarily get memory "homes". */
15546 static bool
15547 add_const_value_attribute (dw_die_ref die, rtx rtl)
15549 switch (GET_CODE (rtl))
15551 case CONST_INT:
15553 HOST_WIDE_INT val = INTVAL (rtl);
15555 if (val < 0)
15556 add_AT_int (die, DW_AT_const_value, val);
15557 else
15558 add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
15560 return true;
15562 case CONST_WIDE_INT:
15563 add_AT_wide (die, DW_AT_const_value,
15564 std::make_pair (rtl, GET_MODE (rtl)));
15565 return true;
15567 case CONST_DOUBLE:
15568 /* Note that a CONST_DOUBLE rtx could represent either an integer or a
15569 floating-point constant. A CONST_DOUBLE is used whenever the
15570 constant requires more than one word in order to be adequately
15571 represented. */
15573 machine_mode mode = GET_MODE (rtl);
15575 if (TARGET_SUPPORTS_WIDE_INT == 0 && !SCALAR_FLOAT_MODE_P (mode))
15576 add_AT_double (die, DW_AT_const_value,
15577 CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
15578 else
15580 unsigned int length = GET_MODE_SIZE (mode);
15581 unsigned char *array = ggc_vec_alloc<unsigned char> (length);
15583 insert_float (rtl, array);
15584 add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
15587 return true;
15589 case CONST_VECTOR:
15591 machine_mode mode = GET_MODE (rtl);
15592 unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
15593 unsigned int length = CONST_VECTOR_NUNITS (rtl);
15594 unsigned char *array
15595 = ggc_vec_alloc<unsigned char> (length * elt_size);
15596 unsigned int i;
15597 unsigned char *p;
15598 machine_mode imode = GET_MODE_INNER (mode);
15600 switch (GET_MODE_CLASS (mode))
15602 case MODE_VECTOR_INT:
15603 for (i = 0, p = array; i < length; i++, p += elt_size)
15605 rtx elt = CONST_VECTOR_ELT (rtl, i);
15606 insert_wide_int (std::make_pair (elt, imode), p, elt_size);
15608 break;
15610 case MODE_VECTOR_FLOAT:
15611 for (i = 0, p = array; i < length; i++, p += elt_size)
15613 rtx elt = CONST_VECTOR_ELT (rtl, i);
15614 insert_float (elt, p);
15616 break;
15618 default:
15619 gcc_unreachable ();
15622 add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
15624 return true;
15626 case CONST_STRING:
15627 if (dwarf_version >= 4 || !dwarf_strict)
15629 dw_loc_descr_ref loc_result;
15630 resolve_one_addr (&rtl);
15631 rtl_addr:
15632 loc_result = new_addr_loc_descr (rtl, dtprel_false);
15633 add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15634 add_AT_loc (die, DW_AT_location, loc_result);
15635 vec_safe_push (used_rtx_array, rtl);
15636 return true;
15638 return false;
15640 case CONST:
15641 if (CONSTANT_P (XEXP (rtl, 0)))
15642 return add_const_value_attribute (die, XEXP (rtl, 0));
15643 /* FALLTHROUGH */
15644 case SYMBOL_REF:
15645 if (!const_ok_for_output (rtl))
15646 return false;
15647 case LABEL_REF:
15648 if (dwarf_version >= 4 || !dwarf_strict)
15649 goto rtl_addr;
15650 return false;
15652 case PLUS:
15653 /* In cases where an inlined instance of an inline function is passed
15654 the address of an `auto' variable (which is local to the caller) we
15655 can get a situation where the DECL_RTL of the artificial local
15656 variable (for the inlining) which acts as a stand-in for the
15657 corresponding formal parameter (of the inline function) will look
15658 like (plus:SI (reg:SI FRAME_PTR) (const_int ...)). This is not
15659 exactly a compile-time constant expression, but it isn't the address
15660 of the (artificial) local variable either. Rather, it represents the
15661 *value* which the artificial local variable always has during its
15662 lifetime. We currently have no way to represent such quasi-constant
15663 values in Dwarf, so for now we just punt and generate nothing. */
15664 return false;
15666 case HIGH:
15667 case CONST_FIXED:
15668 return false;
15670 case MEM:
15671 if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15672 && MEM_READONLY_P (rtl)
15673 && GET_MODE (rtl) == BLKmode)
15675 add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15676 return true;
15678 return false;
15680 default:
15681 /* No other kinds of rtx should be possible here. */
15682 gcc_unreachable ();
15684 return false;
15687 /* Determine whether the evaluation of EXPR references any variables
15688 or functions which aren't otherwise used (and therefore may not be
15689 output). */
15690 static tree
15691 reference_to_unused (tree * tp, int * walk_subtrees,
15692 void * data ATTRIBUTE_UNUSED)
15694 if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15695 *walk_subtrees = 0;
15697 if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15698 && ! TREE_ASM_WRITTEN (*tp))
15699 return *tp;
15700 /* ??? The C++ FE emits debug information for using decls, so
15701 putting gcc_unreachable here falls over. See PR31899. For now
15702 be conservative. */
15703 else if (!symtab->global_info_ready
15704 && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15705 return *tp;
15706 else if (TREE_CODE (*tp) == VAR_DECL)
15708 varpool_node *node = varpool_node::get (*tp);
15709 if (!node || !node->definition)
15710 return *tp;
15712 else if (TREE_CODE (*tp) == FUNCTION_DECL
15713 && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15715 /* The call graph machinery must have finished analyzing,
15716 optimizing and gimplifying the CU by now.
15717 So if *TP has no call graph node associated
15718 to it, it means *TP will not be emitted. */
15719 if (!cgraph_node::get (*tp))
15720 return *tp;
15722 else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15723 return *tp;
15725 return NULL_TREE;
15728 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15729 for use in a later add_const_value_attribute call. */
15731 static rtx
15732 rtl_for_decl_init (tree init, tree type)
15734 rtx rtl = NULL_RTX;
15736 STRIP_NOPS (init);
15738 /* If a variable is initialized with a string constant without embedded
15739 zeros, build CONST_STRING. */
15740 if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15742 tree enttype = TREE_TYPE (type);
15743 tree domain = TYPE_DOMAIN (type);
15744 machine_mode mode = TYPE_MODE (enttype);
15746 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15747 && domain
15748 && integer_zerop (TYPE_MIN_VALUE (domain))
15749 && compare_tree_int (TYPE_MAX_VALUE (domain),
15750 TREE_STRING_LENGTH (init) - 1) == 0
15751 && ((size_t) TREE_STRING_LENGTH (init)
15752 == strlen (TREE_STRING_POINTER (init)) + 1))
15754 rtl = gen_rtx_CONST_STRING (VOIDmode,
15755 ggc_strdup (TREE_STRING_POINTER (init)));
15756 rtl = gen_rtx_MEM (BLKmode, rtl);
15757 MEM_READONLY_P (rtl) = 1;
15760 /* Other aggregates, and complex values, could be represented using
15761 CONCAT: FIXME! */
15762 else if (AGGREGATE_TYPE_P (type)
15763 || (TREE_CODE (init) == VIEW_CONVERT_EXPR
15764 && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
15765 || TREE_CODE (type) == COMPLEX_TYPE)
15767 /* Vectors only work if their mode is supported by the target.
15768 FIXME: generic vectors ought to work too. */
15769 else if (TREE_CODE (type) == VECTOR_TYPE
15770 && !VECTOR_MODE_P (TYPE_MODE (type)))
15772 /* If the initializer is something that we know will expand into an
15773 immediate RTL constant, expand it now. We must be careful not to
15774 reference variables which won't be output. */
15775 else if (initializer_constant_valid_p (init, type)
15776 && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15778 /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15779 possible. */
15780 if (TREE_CODE (type) == VECTOR_TYPE)
15781 switch (TREE_CODE (init))
15783 case VECTOR_CST:
15784 break;
15785 case CONSTRUCTOR:
15786 if (TREE_CONSTANT (init))
15788 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
15789 bool constant_p = true;
15790 tree value;
15791 unsigned HOST_WIDE_INT ix;
15793 /* Even when ctor is constant, it might contain non-*_CST
15794 elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15795 belong into VECTOR_CST nodes. */
15796 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15797 if (!CONSTANT_CLASS_P (value))
15799 constant_p = false;
15800 break;
15803 if (constant_p)
15805 init = build_vector_from_ctor (type, elts);
15806 break;
15809 /* FALLTHRU */
15811 default:
15812 return NULL;
15815 rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15817 /* If expand_expr returns a MEM, it wasn't immediate. */
15818 gcc_assert (!rtl || !MEM_P (rtl));
15821 return rtl;
15824 /* Generate RTL for the variable DECL to represent its location. */
15826 static rtx
15827 rtl_for_decl_location (tree decl)
15829 rtx rtl;
15831 /* Here we have to decide where we are going to say the parameter "lives"
15832 (as far as the debugger is concerned). We only have a couple of
15833 choices. GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15835 DECL_RTL normally indicates where the parameter lives during most of the
15836 activation of the function. If optimization is enabled however, this
15837 could be either NULL or else a pseudo-reg. Both of those cases indicate
15838 that the parameter doesn't really live anywhere (as far as the code
15839 generation parts of GCC are concerned) during most of the function's
15840 activation. That will happen (for example) if the parameter is never
15841 referenced within the function.
15843 We could just generate a location descriptor here for all non-NULL
15844 non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15845 a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15846 where DECL_RTL is NULL or is a pseudo-reg.
15848 Note however that we can only get away with using DECL_INCOMING_RTL as
15849 a backup substitute for DECL_RTL in certain limited cases. In cases
15850 where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15851 we can be sure that the parameter was passed using the same type as it is
15852 declared to have within the function, and that its DECL_INCOMING_RTL
15853 points us to a place where a value of that type is passed.
15855 In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15856 we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15857 because in these cases DECL_INCOMING_RTL points us to a value of some
15858 type which is *different* from the type of the parameter itself. Thus,
15859 if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15860 such cases, the debugger would end up (for example) trying to fetch a
15861 `float' from a place which actually contains the first part of a
15862 `double'. That would lead to really incorrect and confusing
15863 output at debug-time.
15865 So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15866 in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl). There
15867 are a couple of exceptions however. On little-endian machines we can
15868 get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15869 not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15870 an integral type that is smaller than TREE_TYPE (decl). These cases arise
15871 when (on a little-endian machine) a non-prototyped function has a
15872 parameter declared to be of type `short' or `char'. In such cases,
15873 TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15874 be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15875 passed `int' value. If the debugger then uses that address to fetch
15876 a `short' or a `char' (on a little-endian machine) the result will be
15877 the correct data, so we allow for such exceptional cases below.
15879 Note that our goal here is to describe the place where the given formal
15880 parameter lives during most of the function's activation (i.e. between the
15881 end of the prologue and the start of the epilogue). We'll do that as best
15882 as we can. Note however that if the given formal parameter is modified
15883 sometime during the execution of the function, then a stack backtrace (at
15884 debug-time) will show the function as having been called with the *new*
15885 value rather than the value which was originally passed in. This happens
15886 rarely enough that it is not a major problem, but it *is* a problem, and
15887 I'd like to fix it.
15889 A future version of dwarf2out.c may generate two additional attributes for
15890 any given DW_TAG_formal_parameter DIE which will describe the "passed
15891 type" and the "passed location" for the given formal parameter in addition
15892 to the attributes we now generate to indicate the "declared type" and the
15893 "active location" for each parameter. This additional set of attributes
15894 could be used by debuggers for stack backtraces. Separately, note that
15895 sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15896 This happens (for example) for inlined-instances of inline function formal
15897 parameters which are never referenced. This really shouldn't be
15898 happening. All PARM_DECL nodes should get valid non-NULL
15899 DECL_INCOMING_RTL values. FIXME. */
15901 /* Use DECL_RTL as the "location" unless we find something better. */
15902 rtl = DECL_RTL_IF_SET (decl);
15904 /* When generating abstract instances, ignore everything except
15905 constants, symbols living in memory, and symbols living in
15906 fixed registers. */
15907 if (! reload_completed)
15909 if (rtl
15910 && (CONSTANT_P (rtl)
15911 || (MEM_P (rtl)
15912 && CONSTANT_P (XEXP (rtl, 0)))
15913 || (REG_P (rtl)
15914 && TREE_CODE (decl) == VAR_DECL
15915 && TREE_STATIC (decl))))
15917 rtl = targetm.delegitimize_address (rtl);
15918 return rtl;
15920 rtl = NULL_RTX;
15922 else if (TREE_CODE (decl) == PARM_DECL)
15924 if (rtl == NULL_RTX
15925 || is_pseudo_reg (rtl)
15926 || (MEM_P (rtl)
15927 && is_pseudo_reg (XEXP (rtl, 0))
15928 && DECL_INCOMING_RTL (decl)
15929 && MEM_P (DECL_INCOMING_RTL (decl))
15930 && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15932 tree declared_type = TREE_TYPE (decl);
15933 tree passed_type = DECL_ARG_TYPE (decl);
15934 machine_mode dmode = TYPE_MODE (declared_type);
15935 machine_mode pmode = TYPE_MODE (passed_type);
15937 /* This decl represents a formal parameter which was optimized out.
15938 Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15939 all cases where (rtl == NULL_RTX) just below. */
15940 if (dmode == pmode)
15941 rtl = DECL_INCOMING_RTL (decl);
15942 else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15943 && SCALAR_INT_MODE_P (dmode)
15944 && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15945 && DECL_INCOMING_RTL (decl))
15947 rtx inc = DECL_INCOMING_RTL (decl);
15948 if (REG_P (inc))
15949 rtl = inc;
15950 else if (MEM_P (inc))
15952 if (BYTES_BIG_ENDIAN)
15953 rtl = adjust_address_nv (inc, dmode,
15954 GET_MODE_SIZE (pmode)
15955 - GET_MODE_SIZE (dmode));
15956 else
15957 rtl = inc;
15962 /* If the parm was passed in registers, but lives on the stack, then
15963 make a big endian correction if the mode of the type of the
15964 parameter is not the same as the mode of the rtl. */
15965 /* ??? This is the same series of checks that are made in dbxout.c before
15966 we reach the big endian correction code there. It isn't clear if all
15967 of these checks are necessary here, but keeping them all is the safe
15968 thing to do. */
15969 else if (MEM_P (rtl)
15970 && XEXP (rtl, 0) != const0_rtx
15971 && ! CONSTANT_P (XEXP (rtl, 0))
15972 /* Not passed in memory. */
15973 && !MEM_P (DECL_INCOMING_RTL (decl))
15974 /* Not passed by invisible reference. */
15975 && (!REG_P (XEXP (rtl, 0))
15976 || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15977 || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15978 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15979 || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15980 #endif
15982 /* Big endian correction check. */
15983 && BYTES_BIG_ENDIAN
15984 && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15985 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15986 < UNITS_PER_WORD))
15988 machine_mode addr_mode = get_address_mode (rtl);
15989 int offset = (UNITS_PER_WORD
15990 - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15992 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15993 plus_constant (addr_mode, XEXP (rtl, 0), offset));
15996 else if (TREE_CODE (decl) == VAR_DECL
15997 && rtl
15998 && MEM_P (rtl)
15999 && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16000 && BYTES_BIG_ENDIAN)
16002 machine_mode addr_mode = get_address_mode (rtl);
16003 int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16004 int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16006 /* If a variable is declared "register" yet is smaller than
16007 a register, then if we store the variable to memory, it
16008 looks like we're storing a register-sized value, when in
16009 fact we are not. We need to adjust the offset of the
16010 storage location to reflect the actual value's bytes,
16011 else gdb will not be able to display it. */
16012 if (rsize > dsize)
16013 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16014 plus_constant (addr_mode, XEXP (rtl, 0),
16015 rsize - dsize));
16018 /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16019 and will have been substituted directly into all expressions that use it.
16020 C does not have such a concept, but C++ and other languages do. */
16021 if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16022 rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16024 if (rtl)
16025 rtl = targetm.delegitimize_address (rtl);
16027 /* If we don't look past the constant pool, we risk emitting a
16028 reference to a constant pool entry that isn't referenced from
16029 code, and thus is not emitted. */
16030 if (rtl)
16031 rtl = avoid_constant_pool_reference (rtl);
16033 /* Try harder to get a rtl. If this symbol ends up not being emitted
16034 in the current CU, resolve_addr will remove the expression referencing
16035 it. */
16036 if (rtl == NULL_RTX
16037 && TREE_CODE (decl) == VAR_DECL
16038 && !DECL_EXTERNAL (decl)
16039 && TREE_STATIC (decl)
16040 && DECL_NAME (decl)
16041 && !DECL_HARD_REGISTER (decl)
16042 && DECL_MODE (decl) != VOIDmode)
16044 rtl = make_decl_rtl_for_debug (decl);
16045 if (!MEM_P (rtl)
16046 || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16047 || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16048 rtl = NULL_RTX;
16051 return rtl;
16054 /* Check whether decl is a Fortran COMMON symbol. If not, NULL_TREE is
16055 returned. If so, the decl for the COMMON block is returned, and the
16056 value is the offset into the common block for the symbol. */
16058 static tree
16059 fortran_common (tree decl, HOST_WIDE_INT *value)
16061 tree val_expr, cvar;
16062 machine_mode mode;
16063 HOST_WIDE_INT bitsize, bitpos;
16064 tree offset;
16065 int unsignedp, volatilep = 0;
16067 /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16068 it does not have a value (the offset into the common area), or if it
16069 is thread local (as opposed to global) then it isn't common, and shouldn't
16070 be handled as such. */
16071 if (TREE_CODE (decl) != VAR_DECL
16072 || !TREE_STATIC (decl)
16073 || !DECL_HAS_VALUE_EXPR_P (decl)
16074 || !is_fortran ())
16075 return NULL_TREE;
16077 val_expr = DECL_VALUE_EXPR (decl);
16078 if (TREE_CODE (val_expr) != COMPONENT_REF)
16079 return NULL_TREE;
16081 cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16082 &mode, &unsignedp, &volatilep, true);
16084 if (cvar == NULL_TREE
16085 || TREE_CODE (cvar) != VAR_DECL
16086 || DECL_ARTIFICIAL (cvar)
16087 || !TREE_PUBLIC (cvar))
16088 return NULL_TREE;
16090 *value = 0;
16091 if (offset != NULL)
16093 if (!tree_fits_shwi_p (offset))
16094 return NULL_TREE;
16095 *value = tree_to_shwi (offset);
16097 if (bitpos != 0)
16098 *value += bitpos / BITS_PER_UNIT;
16100 return cvar;
16103 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16104 data attribute for a variable or a parameter. We generate the
16105 DW_AT_const_value attribute only in those cases where the given variable
16106 or parameter does not have a true "location" either in memory or in a
16107 register. This can happen (for example) when a constant is passed as an
16108 actual argument in a call to an inline function. (It's possible that
16109 these things can crop up in other ways also.) Note that one type of
16110 constant value which can be passed into an inlined function is a constant
16111 pointer. This can happen for example if an actual argument in an inlined
16112 function call evaluates to a compile-time constant address.
16114 CACHE_P is true if it is worth caching the location list for DECL,
16115 so that future calls can reuse it rather than regenerate it from scratch.
16116 This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16117 since we will need to refer to them each time the function is inlined. */
16119 static bool
16120 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16121 enum dwarf_attribute attr)
16123 rtx rtl;
16124 dw_loc_list_ref list;
16125 var_loc_list *loc_list;
16126 cached_dw_loc_list *cache;
16128 if (TREE_CODE (decl) == ERROR_MARK)
16129 return false;
16131 if (get_AT (die, attr))
16132 return true;
16134 gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16135 || TREE_CODE (decl) == RESULT_DECL);
16137 /* Try to get some constant RTL for this decl, and use that as the value of
16138 the location. */
16140 rtl = rtl_for_decl_location (decl);
16141 if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16142 && add_const_value_attribute (die, rtl))
16143 return true;
16145 /* See if we have single element location list that is equivalent to
16146 a constant value. That way we are better to use add_const_value_attribute
16147 rather than expanding constant value equivalent. */
16148 loc_list = lookup_decl_loc (decl);
16149 if (loc_list
16150 && loc_list->first
16151 && loc_list->first->next == NULL
16152 && NOTE_P (loc_list->first->loc)
16153 && NOTE_VAR_LOCATION (loc_list->first->loc)
16154 && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16156 struct var_loc_node *node;
16158 node = loc_list->first;
16159 rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16160 if (GET_CODE (rtl) == EXPR_LIST)
16161 rtl = XEXP (rtl, 0);
16162 if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16163 && add_const_value_attribute (die, rtl))
16164 return true;
16166 /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16167 list several times. See if we've already cached the contents. */
16168 list = NULL;
16169 if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16170 cache_p = false;
16171 if (cache_p)
16173 cache = cached_dw_loc_list_table->find_with_hash (decl, DECL_UID (decl));
16174 if (cache)
16175 list = cache->loc_list;
16177 if (list == NULL)
16179 list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2,
16180 NULL);
16181 /* It is usually worth caching this result if the decl is from
16182 BLOCK_NONLOCALIZED_VARS and if the list has at least two elements. */
16183 if (cache_p && list && list->dw_loc_next)
16185 cached_dw_loc_list **slot
16186 = cached_dw_loc_list_table->find_slot_with_hash (decl,
16187 DECL_UID (decl),
16188 INSERT);
16189 cache = ggc_cleared_alloc<cached_dw_loc_list> ();
16190 cache->decl_id = DECL_UID (decl);
16191 cache->loc_list = list;
16192 *slot = cache;
16195 if (list)
16197 add_AT_location_description (die, attr, list);
16198 return true;
16200 /* None of that worked, so it must not really have a location;
16201 try adding a constant value attribute from the DECL_INITIAL. */
16202 return tree_add_const_value_attribute_for_decl (die, decl);
16205 /* Helper function for tree_add_const_value_attribute. Natively encode
16206 initializer INIT into an array. Return true if successful. */
16208 static bool
16209 native_encode_initializer (tree init, unsigned char *array, int size)
16211 tree type;
16213 if (init == NULL_TREE)
16214 return false;
16216 STRIP_NOPS (init);
16217 switch (TREE_CODE (init))
16219 case STRING_CST:
16220 type = TREE_TYPE (init);
16221 if (TREE_CODE (type) == ARRAY_TYPE)
16223 tree enttype = TREE_TYPE (type);
16224 machine_mode mode = TYPE_MODE (enttype);
16226 if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16227 return false;
16228 if (int_size_in_bytes (type) != size)
16229 return false;
16230 if (size > TREE_STRING_LENGTH (init))
16232 memcpy (array, TREE_STRING_POINTER (init),
16233 TREE_STRING_LENGTH (init));
16234 memset (array + TREE_STRING_LENGTH (init),
16235 '\0', size - TREE_STRING_LENGTH (init));
16237 else
16238 memcpy (array, TREE_STRING_POINTER (init), size);
16239 return true;
16241 return false;
16242 case CONSTRUCTOR:
16243 type = TREE_TYPE (init);
16244 if (int_size_in_bytes (type) != size)
16245 return false;
16246 if (TREE_CODE (type) == ARRAY_TYPE)
16248 HOST_WIDE_INT min_index;
16249 unsigned HOST_WIDE_INT cnt;
16250 int curpos = 0, fieldsize;
16251 constructor_elt *ce;
16253 if (TYPE_DOMAIN (type) == NULL_TREE
16254 || !tree_fits_shwi_p (TYPE_MIN_VALUE (TYPE_DOMAIN (type))))
16255 return false;
16257 fieldsize = int_size_in_bytes (TREE_TYPE (type));
16258 if (fieldsize <= 0)
16259 return false;
16261 min_index = tree_to_shwi (TYPE_MIN_VALUE (TYPE_DOMAIN (type)));
16262 memset (array, '\0', size);
16263 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16265 tree val = ce->value;
16266 tree index = ce->index;
16267 int pos = curpos;
16268 if (index && TREE_CODE (index) == RANGE_EXPR)
16269 pos = (tree_to_shwi (TREE_OPERAND (index, 0)) - min_index)
16270 * fieldsize;
16271 else if (index)
16272 pos = (tree_to_shwi (index) - min_index) * fieldsize;
16274 if (val)
16276 STRIP_NOPS (val);
16277 if (!native_encode_initializer (val, array + pos, fieldsize))
16278 return false;
16280 curpos = pos + fieldsize;
16281 if (index && TREE_CODE (index) == RANGE_EXPR)
16283 int count = tree_to_shwi (TREE_OPERAND (index, 1))
16284 - tree_to_shwi (TREE_OPERAND (index, 0));
16285 while (count-- > 0)
16287 if (val)
16288 memcpy (array + curpos, array + pos, fieldsize);
16289 curpos += fieldsize;
16292 gcc_assert (curpos <= size);
16294 return true;
16296 else if (TREE_CODE (type) == RECORD_TYPE
16297 || TREE_CODE (type) == UNION_TYPE)
16299 tree field = NULL_TREE;
16300 unsigned HOST_WIDE_INT cnt;
16301 constructor_elt *ce;
16303 if (int_size_in_bytes (type) != size)
16304 return false;
16306 if (TREE_CODE (type) == RECORD_TYPE)
16307 field = TYPE_FIELDS (type);
16309 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
16311 tree val = ce->value;
16312 int pos, fieldsize;
16314 if (ce->index != 0)
16315 field = ce->index;
16317 if (val)
16318 STRIP_NOPS (val);
16320 if (field == NULL_TREE || DECL_BIT_FIELD (field))
16321 return false;
16323 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16324 && TYPE_DOMAIN (TREE_TYPE (field))
16325 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16326 return false;
16327 else if (DECL_SIZE_UNIT (field) == NULL_TREE
16328 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
16329 return false;
16330 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
16331 pos = int_byte_position (field);
16332 gcc_assert (pos + fieldsize <= size);
16333 if (val
16334 && !native_encode_initializer (val, array + pos, fieldsize))
16335 return false;
16337 return true;
16339 return false;
16340 case VIEW_CONVERT_EXPR:
16341 case NON_LVALUE_EXPR:
16342 return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16343 default:
16344 return native_encode_expr (init, array, size) == size;
16348 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16349 attribute is the const value T. */
16351 static bool
16352 tree_add_const_value_attribute (dw_die_ref die, tree t)
16354 tree init;
16355 tree type = TREE_TYPE (t);
16356 rtx rtl;
16358 if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16359 return false;
16361 init = t;
16362 gcc_assert (!DECL_P (init));
16364 rtl = rtl_for_decl_init (init, type);
16365 if (rtl)
16366 return add_const_value_attribute (die, rtl);
16367 /* If the host and target are sane, try harder. */
16368 else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16369 && initializer_constant_valid_p (init, type))
16371 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16372 if (size > 0 && (int) size == size)
16374 unsigned char *array = ggc_cleared_vec_alloc<unsigned char> (size);
16376 if (native_encode_initializer (init, array, size))
16378 add_AT_vec (die, DW_AT_const_value, size, 1, array);
16379 return true;
16381 ggc_free (array);
16384 return false;
16387 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16388 attribute is the const value of T, where T is an integral constant
16389 variable with static storage duration
16390 (so it can't be a PARM_DECL or a RESULT_DECL). */
16392 static bool
16393 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16396 if (!decl
16397 || (TREE_CODE (decl) != VAR_DECL
16398 && TREE_CODE (decl) != CONST_DECL)
16399 || (TREE_CODE (decl) == VAR_DECL
16400 && !TREE_STATIC (decl)))
16401 return false;
16403 if (TREE_READONLY (decl)
16404 && ! TREE_THIS_VOLATILE (decl)
16405 && DECL_INITIAL (decl))
16406 /* OK */;
16407 else
16408 return false;
16410 /* Don't add DW_AT_const_value if abstract origin already has one. */
16411 if (get_AT (var_die, DW_AT_const_value))
16412 return false;
16414 return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16417 /* Convert the CFI instructions for the current function into a
16418 location list. This is used for DW_AT_frame_base when we targeting
16419 a dwarf2 consumer that does not support the dwarf3
16420 DW_OP_call_frame_cfa. OFFSET is a constant to be added to all CFA
16421 expressions. */
16423 static dw_loc_list_ref
16424 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16426 int ix;
16427 dw_fde_ref fde;
16428 dw_loc_list_ref list, *list_tail;
16429 dw_cfi_ref cfi;
16430 dw_cfa_location last_cfa, next_cfa;
16431 const char *start_label, *last_label, *section;
16432 dw_cfa_location remember;
16434 fde = cfun->fde;
16435 gcc_assert (fde != NULL);
16437 section = secname_for_decl (current_function_decl);
16438 list_tail = &list;
16439 list = NULL;
16441 memset (&next_cfa, 0, sizeof (next_cfa));
16442 next_cfa.reg = INVALID_REGNUM;
16443 remember = next_cfa;
16445 start_label = fde->dw_fde_begin;
16447 /* ??? Bald assumption that the CIE opcode list does not contain
16448 advance opcodes. */
16449 FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
16450 lookup_cfa_1 (cfi, &next_cfa, &remember);
16452 last_cfa = next_cfa;
16453 last_label = start_label;
16455 if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
16457 /* If the first partition contained no CFI adjustments, the
16458 CIE opcodes apply to the whole first partition. */
16459 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16460 fde->dw_fde_begin, fde->dw_fde_end, section);
16461 list_tail =&(*list_tail)->dw_loc_next;
16462 start_label = last_label = fde->dw_fde_second_begin;
16465 FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
16467 switch (cfi->dw_cfi_opc)
16469 case DW_CFA_set_loc:
16470 case DW_CFA_advance_loc1:
16471 case DW_CFA_advance_loc2:
16472 case DW_CFA_advance_loc4:
16473 if (!cfa_equal_p (&last_cfa, &next_cfa))
16475 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16476 start_label, last_label, section);
16478 list_tail = &(*list_tail)->dw_loc_next;
16479 last_cfa = next_cfa;
16480 start_label = last_label;
16482 last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16483 break;
16485 case DW_CFA_advance_loc:
16486 /* The encoding is complex enough that we should never emit this. */
16487 gcc_unreachable ();
16489 default:
16490 lookup_cfa_1 (cfi, &next_cfa, &remember);
16491 break;
16493 if (ix + 1 == fde->dw_fde_switch_cfi_index)
16495 if (!cfa_equal_p (&last_cfa, &next_cfa))
16497 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16498 start_label, last_label, section);
16500 list_tail = &(*list_tail)->dw_loc_next;
16501 last_cfa = next_cfa;
16502 start_label = last_label;
16504 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16505 start_label, fde->dw_fde_end, section);
16506 list_tail = &(*list_tail)->dw_loc_next;
16507 start_label = last_label = fde->dw_fde_second_begin;
16511 if (!cfa_equal_p (&last_cfa, &next_cfa))
16513 *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16514 start_label, last_label, section);
16515 list_tail = &(*list_tail)->dw_loc_next;
16516 start_label = last_label;
16519 *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16520 start_label,
16521 fde->dw_fde_second_begin
16522 ? fde->dw_fde_second_end : fde->dw_fde_end,
16523 section);
16525 if (list && list->dw_loc_next)
16526 gen_llsym (list);
16528 return list;
16531 /* Compute a displacement from the "steady-state frame pointer" to the
16532 frame base (often the same as the CFA), and store it in
16533 frame_pointer_fb_offset. OFFSET is added to the displacement
16534 before the latter is negated. */
16536 static void
16537 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16539 rtx reg, elim;
16541 #ifdef FRAME_POINTER_CFA_OFFSET
16542 reg = frame_pointer_rtx;
16543 offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16544 #else
16545 reg = arg_pointer_rtx;
16546 offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16547 #endif
16549 elim = (ira_use_lra_p
16550 ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
16551 : eliminate_regs (reg, VOIDmode, NULL_RTX));
16552 if (GET_CODE (elim) == PLUS)
16554 offset += INTVAL (XEXP (elim, 1));
16555 elim = XEXP (elim, 0);
16558 frame_pointer_fb_offset = -offset;
16560 /* ??? AVR doesn't set up valid eliminations when there is no stack frame
16561 in which to eliminate. This is because it's stack pointer isn't
16562 directly accessible as a register within the ISA. To work around
16563 this, assume that while we cannot provide a proper value for
16564 frame_pointer_fb_offset, we won't need one either. */
16565 frame_pointer_fb_offset_valid
16566 = ((SUPPORTS_STACK_ALIGNMENT
16567 && (elim == hard_frame_pointer_rtx
16568 || elim == stack_pointer_rtx))
16569 || elim == (frame_pointer_needed
16570 ? hard_frame_pointer_rtx
16571 : stack_pointer_rtx));
16574 /* Generate a DW_AT_name attribute given some string value to be included as
16575 the value of the attribute. */
16577 static void
16578 add_name_attribute (dw_die_ref die, const char *name_string)
16580 if (name_string != NULL && *name_string != 0)
16582 if (demangle_name_func)
16583 name_string = (*demangle_name_func) (name_string);
16585 add_AT_string (die, DW_AT_name, name_string);
16589 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
16590 DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
16591 of TYPE accordingly.
16593 ??? This is a temporary measure until after we're able to generate
16594 regular DWARF for the complex Ada type system. */
16596 static void
16597 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
16598 dw_die_ref context_die)
16600 tree dtype;
16601 dw_die_ref dtype_die;
16603 if (!lang_hooks.types.descriptive_type)
16604 return;
16606 dtype = lang_hooks.types.descriptive_type (type);
16607 if (!dtype)
16608 return;
16610 dtype_die = lookup_type_die (dtype);
16611 if (!dtype_die)
16613 gen_type_die (dtype, context_die);
16614 dtype_die = lookup_type_die (dtype);
16615 gcc_assert (dtype_die);
16618 add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
16621 /* Retrieve the comp_dir string suitable for use with DW_AT_comp_dir. */
16623 static const char *
16624 comp_dir_string (void)
16626 const char *wd;
16627 char *wd1;
16628 static const char *cached_wd = NULL;
16630 if (cached_wd != NULL)
16631 return cached_wd;
16633 wd = get_src_pwd ();
16634 if (wd == NULL)
16635 return NULL;
16637 if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
16639 int wdlen;
16641 wdlen = strlen (wd);
16642 wd1 = ggc_vec_alloc<char> (wdlen + 2);
16643 strcpy (wd1, wd);
16644 wd1 [wdlen] = DIR_SEPARATOR;
16645 wd1 [wdlen + 1] = 0;
16646 wd = wd1;
16649 cached_wd = remap_debug_filename (wd);
16650 return cached_wd;
16653 /* Generate a DW_AT_comp_dir attribute for DIE. */
16655 static void
16656 add_comp_dir_attribute (dw_die_ref die)
16658 const char * wd = comp_dir_string ();
16659 if (wd != NULL)
16660 add_AT_string (die, DW_AT_comp_dir, wd);
16663 /* Given a tree node VALUE describing a scalar attribute ATTR (i.e. a bound, a
16664 pointer computation, ...), output a representation for that bound according
16665 to the accepted FORMS (see enum dw_scalar_form) and add it to DIE. See
16666 loc_list_from_tree for the meaning of CONTEXT. */
16668 static void
16669 add_scalar_info (dw_die_ref die, enum dwarf_attribute attr, tree value,
16670 int forms, const struct loc_descr_context *context)
16672 dw_die_ref ctx, decl_die;
16673 dw_loc_list_ref list;
16675 bool strip_conversions = true;
16677 while (strip_conversions)
16678 switch (TREE_CODE (value))
16680 case ERROR_MARK:
16681 case SAVE_EXPR:
16682 return;
16684 CASE_CONVERT:
16685 case VIEW_CONVERT_EXPR:
16686 value = TREE_OPERAND (value, 0);
16687 break;
16689 default:
16690 strip_conversions = false;
16691 break;
16694 /* If possible and permitted, output the attribute as a constant. */
16695 if ((forms & dw_scalar_form_constant) != 0
16696 && TREE_CODE (value) == INTEGER_CST)
16698 unsigned int prec = simple_type_size_in_bits (TREE_TYPE (value));
16700 /* If HOST_WIDE_INT is big enough then represent the bound as
16701 a constant value. We need to choose a form based on
16702 whether the type is signed or unsigned. We cannot just
16703 call add_AT_unsigned if the value itself is positive
16704 (add_AT_unsigned might add the unsigned value encoded as
16705 DW_FORM_data[1248]). Some DWARF consumers will lookup the
16706 bounds type and then sign extend any unsigned values found
16707 for signed types. This is needed only for
16708 DW_AT_{lower,upper}_bound, since for most other attributes,
16709 consumers will treat DW_FORM_data[1248] as unsigned values,
16710 regardless of the underlying type. */
16711 if (prec <= HOST_BITS_PER_WIDE_INT
16712 || tree_fits_uhwi_p (value))
16714 if (TYPE_UNSIGNED (TREE_TYPE (value)))
16715 add_AT_unsigned (die, attr, TREE_INT_CST_LOW (value));
16716 else
16717 add_AT_int (die, attr, TREE_INT_CST_LOW (value));
16719 else
16720 /* Otherwise represent the bound as an unsigned value with
16721 the precision of its type. The precision and signedness
16722 of the type will be necessary to re-interpret it
16723 unambiguously. */
16724 add_AT_wide (die, attr, value);
16725 return;
16728 /* Otherwise, if it's possible and permitted too, output a reference to
16729 another DIE. */
16730 if ((forms & dw_scalar_form_reference) != 0)
16732 tree decl = NULL_TREE;
16734 /* Some type attributes reference an outer type. For instance, the upper
16735 bound of an array may reference an embedding record (this happens in
16736 Ada). */
16737 if (TREE_CODE (value) == COMPONENT_REF
16738 && TREE_CODE (TREE_OPERAND (value, 0)) == PLACEHOLDER_EXPR
16739 && TREE_CODE (TREE_OPERAND (value, 1)) == FIELD_DECL)
16740 decl = TREE_OPERAND (value, 1);
16742 else if (TREE_CODE (value) == VAR_DECL
16743 || TREE_CODE (value) == PARM_DECL
16744 || TREE_CODE (value) == RESULT_DECL)
16745 decl = value;
16747 if (decl != NULL_TREE)
16749 dw_die_ref decl_die = lookup_decl_die (decl);
16751 /* ??? Can this happen, or should the variable have been bound
16752 first? Probably it can, since I imagine that we try to create
16753 the types of parameters in the order in which they exist in
16754 the list, and won't have created a forward reference to a
16755 later parameter. */
16756 if (decl_die != NULL)
16758 add_AT_die_ref (die, attr, decl_die);
16759 return;
16764 /* Last chance: try to create a stack operation procedure to evaluate the
16765 value. Do nothing if even that is not possible or permitted. */
16766 if ((forms & dw_scalar_form_exprloc) == 0)
16767 return;
16769 list = loc_list_from_tree (value, 2, context);
16770 if (list == NULL || single_element_loc_list_p (list))
16772 /* If this attribute is not a reference nor constant, it is
16773 a DWARF expression rather than location description. For that
16774 loc_list_from_tree (value, 0, &context) is needed. */
16775 dw_loc_list_ref list2 = loc_list_from_tree (value, 0, context);
16776 if (list2 && single_element_loc_list_p (list2))
16778 add_AT_loc (die, attr, list2->expr);
16779 return;
16783 /* If that failed to give a single element location list, fall back to
16784 outputting this as a reference... still if permitted. */
16785 if (list == NULL || (forms & dw_scalar_form_reference) == 0)
16786 return;
16788 if (current_function_decl == 0)
16789 ctx = comp_unit_die ();
16790 else
16791 ctx = lookup_decl_die (current_function_decl);
16793 decl_die = new_die (DW_TAG_variable, ctx, value);
16794 add_AT_flag (decl_die, DW_AT_artificial, 1);
16795 add_type_attribute (decl_die, TREE_TYPE (value), TYPE_QUAL_CONST, ctx);
16796 add_AT_location_description (decl_die, DW_AT_location, list);
16797 add_AT_die_ref (die, attr, decl_die);
16800 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
16801 default. */
16803 static int
16804 lower_bound_default (void)
16806 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
16808 case DW_LANG_C:
16809 case DW_LANG_C89:
16810 case DW_LANG_C99:
16811 case DW_LANG_C11:
16812 case DW_LANG_C_plus_plus:
16813 case DW_LANG_C_plus_plus_11:
16814 case DW_LANG_C_plus_plus_14:
16815 case DW_LANG_ObjC:
16816 case DW_LANG_ObjC_plus_plus:
16817 case DW_LANG_Java:
16818 return 0;
16819 case DW_LANG_Fortran77:
16820 case DW_LANG_Fortran90:
16821 case DW_LANG_Fortran95:
16822 case DW_LANG_Fortran03:
16823 case DW_LANG_Fortran08:
16824 return 1;
16825 case DW_LANG_UPC:
16826 case DW_LANG_D:
16827 case DW_LANG_Python:
16828 return dwarf_version >= 4 ? 0 : -1;
16829 case DW_LANG_Ada95:
16830 case DW_LANG_Ada83:
16831 case DW_LANG_Cobol74:
16832 case DW_LANG_Cobol85:
16833 case DW_LANG_Pascal83:
16834 case DW_LANG_Modula2:
16835 case DW_LANG_PLI:
16836 return dwarf_version >= 4 ? 1 : -1;
16837 default:
16838 return -1;
16842 /* Given a tree node describing an array bound (either lower or upper) output
16843 a representation for that bound. */
16845 static void
16846 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr,
16847 tree bound, const struct loc_descr_context *context)
16849 int dflt;
16851 while (1)
16852 switch (TREE_CODE (bound))
16854 /* Strip all conversions. */
16855 CASE_CONVERT:
16856 case VIEW_CONVERT_EXPR:
16857 bound = TREE_OPERAND (bound, 0);
16858 break;
16860 /* All fixed-bounds are represented by INTEGER_CST nodes. Lower bounds
16861 are even omitted when they are the default. */
16862 case INTEGER_CST:
16863 /* If the value for this bound is the default one, we can even omit the
16864 attribute. */
16865 if (bound_attr == DW_AT_lower_bound
16866 && tree_fits_shwi_p (bound)
16867 && (dflt = lower_bound_default ()) != -1
16868 && tree_to_shwi (bound) == dflt)
16869 return;
16871 /* FALLTHRU */
16873 default:
16874 add_scalar_info (subrange_die, bound_attr, bound,
16875 dw_scalar_form_constant
16876 | dw_scalar_form_exprloc
16877 | dw_scalar_form_reference,
16878 context);
16879 return;
16883 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16884 possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16885 Note that the block of subscript information for an array type also
16886 includes information about the element type of the given array type.
16888 This function reuses previously set type and bound information if
16889 available. */
16891 static void
16892 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16894 unsigned dimension_number;
16895 tree lower, upper;
16896 dw_die_ref child = type_die->die_child;
16898 for (dimension_number = 0;
16899 TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16900 type = TREE_TYPE (type), dimension_number++)
16902 tree domain = TYPE_DOMAIN (type);
16904 if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16905 break;
16907 /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16908 and (in GNU C only) variable bounds. Handle all three forms
16909 here. */
16911 /* Find and reuse a previously generated DW_TAG_subrange_type if
16912 available.
16914 For multi-dimensional arrays, as we iterate through the
16915 various dimensions in the enclosing for loop above, we also
16916 iterate through the DIE children and pick at each
16917 DW_TAG_subrange_type previously generated (if available).
16918 Each child DW_TAG_subrange_type DIE describes the range of
16919 the current dimension. At this point we should have as many
16920 DW_TAG_subrange_type's as we have dimensions in the
16921 array. */
16922 dw_die_ref subrange_die = NULL;
16923 if (child)
16924 while (1)
16926 child = child->die_sib;
16927 if (child->die_tag == DW_TAG_subrange_type)
16928 subrange_die = child;
16929 if (child == type_die->die_child)
16931 /* If we wrapped around, stop looking next time. */
16932 child = NULL;
16933 break;
16935 if (child->die_tag == DW_TAG_subrange_type)
16936 break;
16938 if (!subrange_die)
16939 subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16941 if (domain)
16943 /* We have an array type with specified bounds. */
16944 lower = TYPE_MIN_VALUE (domain);
16945 upper = TYPE_MAX_VALUE (domain);
16947 /* Define the index type. */
16948 if (TREE_TYPE (domain)
16949 && !get_AT (subrange_die, DW_AT_type))
16951 /* ??? This is probably an Ada unnamed subrange type. Ignore the
16952 TREE_TYPE field. We can't emit debug info for this
16953 because it is an unnamed integral type. */
16954 if (TREE_CODE (domain) == INTEGER_TYPE
16955 && TYPE_NAME (domain) == NULL_TREE
16956 && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16957 && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16959 else
16960 add_type_attribute (subrange_die, TREE_TYPE (domain),
16961 TYPE_UNQUALIFIED, type_die);
16964 /* ??? If upper is NULL, the array has unspecified length,
16965 but it does have a lower bound. This happens with Fortran
16966 dimension arr(N:*)
16967 Since the debugger is definitely going to need to know N
16968 to produce useful results, go ahead and output the lower
16969 bound solo, and hope the debugger can cope. */
16971 if (!get_AT (subrange_die, DW_AT_lower_bound))
16972 add_bound_info (subrange_die, DW_AT_lower_bound, lower, NULL);
16973 if (upper && !get_AT (subrange_die, DW_AT_upper_bound))
16974 add_bound_info (subrange_die, DW_AT_upper_bound, upper, NULL);
16977 /* Otherwise we have an array type with an unspecified length. The
16978 DWARF-2 spec does not say how to handle this; let's just leave out the
16979 bounds. */
16983 /* Add a DW_AT_byte_size attribute to DIE with TREE_NODE's size. */
16985 static void
16986 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16988 dw_die_ref decl_die;
16989 HOST_WIDE_INT size;
16991 switch (TREE_CODE (tree_node))
16993 case ERROR_MARK:
16994 size = 0;
16995 break;
16996 case ENUMERAL_TYPE:
16997 case RECORD_TYPE:
16998 case UNION_TYPE:
16999 case QUAL_UNION_TYPE:
17000 if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
17001 && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
17003 add_AT_die_ref (die, DW_AT_byte_size, decl_die);
17004 return;
17006 size = int_size_in_bytes (tree_node);
17007 break;
17008 case FIELD_DECL:
17009 /* For a data member of a struct or union, the DW_AT_byte_size is
17010 generally given as the number of bytes normally allocated for an
17011 object of the *declared* type of the member itself. This is true
17012 even for bit-fields. */
17013 size = int_size_in_bytes (field_type (tree_node));
17014 break;
17015 default:
17016 gcc_unreachable ();
17019 /* Note that `size' might be -1 when we get to this point. If it is, that
17020 indicates that the byte size of the entity in question is variable. We
17021 have no good way of expressing this fact in Dwarf at the present time,
17022 when location description was not used by the caller code instead. */
17023 if (size >= 0)
17024 add_AT_unsigned (die, DW_AT_byte_size, size);
17027 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17028 which specifies the distance in bits from the highest order bit of the
17029 "containing object" for the bit-field to the highest order bit of the
17030 bit-field itself.
17032 For any given bit-field, the "containing object" is a hypothetical object
17033 (of some integral or enum type) within which the given bit-field lives. The
17034 type of this hypothetical "containing object" is always the same as the
17035 declared type of the individual bit-field itself. The determination of the
17036 exact location of the "containing object" for a bit-field is rather
17037 complicated. It's handled by the `field_byte_offset' function (above).
17039 Note that it is the size (in bytes) of the hypothetical "containing object"
17040 which will be given in the DW_AT_byte_size attribute for this bit-field.
17041 (See `byte_size_attribute' above). */
17043 static inline void
17044 add_bit_offset_attribute (dw_die_ref die, tree decl)
17046 HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17047 tree type = DECL_BIT_FIELD_TYPE (decl);
17048 HOST_WIDE_INT bitpos_int;
17049 HOST_WIDE_INT highest_order_object_bit_offset;
17050 HOST_WIDE_INT highest_order_field_bit_offset;
17051 HOST_WIDE_INT bit_offset;
17053 /* Must be a field and a bit field. */
17054 gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17056 /* We can't yet handle bit-fields whose offsets are variable, so if we
17057 encounter such things, just return without generating any attribute
17058 whatsoever. Likewise for variable or too large size. */
17059 if (! tree_fits_shwi_p (bit_position (decl))
17060 || ! tree_fits_uhwi_p (DECL_SIZE (decl)))
17061 return;
17063 bitpos_int = int_bit_position (decl);
17065 /* Note that the bit offset is always the distance (in bits) from the
17066 highest-order bit of the "containing object" to the highest-order bit of
17067 the bit-field itself. Since the "high-order end" of any object or field
17068 is different on big-endian and little-endian machines, the computation
17069 below must take account of these differences. */
17070 highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17071 highest_order_field_bit_offset = bitpos_int;
17073 if (! BYTES_BIG_ENDIAN)
17075 highest_order_field_bit_offset += tree_to_shwi (DECL_SIZE (decl));
17076 highest_order_object_bit_offset += simple_type_size_in_bits (type);
17079 bit_offset
17080 = (! BYTES_BIG_ENDIAN
17081 ? highest_order_object_bit_offset - highest_order_field_bit_offset
17082 : highest_order_field_bit_offset - highest_order_object_bit_offset);
17084 if (bit_offset < 0)
17085 add_AT_int (die, DW_AT_bit_offset, bit_offset);
17086 else
17087 add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17090 /* For a FIELD_DECL node which represents a bit field, output an attribute
17091 which specifies the length in bits of the given field. */
17093 static inline void
17094 add_bit_size_attribute (dw_die_ref die, tree decl)
17096 /* Must be a field and a bit field. */
17097 gcc_assert (TREE_CODE (decl) == FIELD_DECL
17098 && DECL_BIT_FIELD_TYPE (decl));
17100 if (tree_fits_uhwi_p (DECL_SIZE (decl)))
17101 add_AT_unsigned (die, DW_AT_bit_size, tree_to_uhwi (DECL_SIZE (decl)));
17104 /* If the compiled language is ANSI C, then add a 'prototyped'
17105 attribute, if arg types are given for the parameters of a function. */
17107 static inline void
17108 add_prototyped_attribute (dw_die_ref die, tree func_type)
17110 switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17112 case DW_LANG_C:
17113 case DW_LANG_C89:
17114 case DW_LANG_C99:
17115 case DW_LANG_C11:
17116 case DW_LANG_ObjC:
17117 if (prototype_p (func_type))
17118 add_AT_flag (die, DW_AT_prototyped, 1);
17119 break;
17120 default:
17121 break;
17125 /* Add an 'abstract_origin' attribute below a given DIE. The DIE is found
17126 by looking in either the type declaration or object declaration
17127 equate table. */
17129 static inline dw_die_ref
17130 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17132 dw_die_ref origin_die = NULL;
17134 if (TREE_CODE (origin) != FUNCTION_DECL)
17136 /* We may have gotten separated from the block for the inlined
17137 function, if we're in an exception handler or some such; make
17138 sure that the abstract function has been written out.
17140 Doing this for nested functions is wrong, however; functions are
17141 distinct units, and our context might not even be inline. */
17142 tree fn = origin;
17144 if (TYPE_P (fn))
17145 fn = TYPE_STUB_DECL (fn);
17147 fn = decl_function_context (fn);
17148 if (fn)
17149 dwarf2out_abstract_function (fn);
17152 if (DECL_P (origin))
17153 origin_die = lookup_decl_die (origin);
17154 else if (TYPE_P (origin))
17155 origin_die = lookup_type_die (origin);
17157 /* XXX: Functions that are never lowered don't always have correct block
17158 trees (in the case of java, they simply have no block tree, in some other
17159 languages). For these functions, there is nothing we can really do to
17160 output correct debug info for inlined functions in all cases. Rather
17161 than die, we'll just produce deficient debug info now, in that we will
17162 have variables without a proper abstract origin. In the future, when all
17163 functions are lowered, we should re-add a gcc_assert (origin_die)
17164 here. */
17166 if (origin_die)
17167 add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17168 return origin_die;
17171 /* We do not currently support the pure_virtual attribute. */
17173 static inline void
17174 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17176 if (DECL_VINDEX (func_decl))
17178 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17180 if (tree_fits_shwi_p (DECL_VINDEX (func_decl)))
17181 add_AT_loc (die, DW_AT_vtable_elem_location,
17182 new_loc_descr (DW_OP_constu,
17183 tree_to_shwi (DECL_VINDEX (func_decl)),
17184 0));
17186 /* GNU extension: Record what type this method came from originally. */
17187 if (debug_info_level > DINFO_LEVEL_TERSE
17188 && DECL_CONTEXT (func_decl))
17189 add_AT_die_ref (die, DW_AT_containing_type,
17190 lookup_type_die (DECL_CONTEXT (func_decl)));
17194 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17195 given decl. This used to be a vendor extension until after DWARF 4
17196 standardized it. */
17198 static void
17199 add_linkage_attr (dw_die_ref die, tree decl)
17201 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17203 /* Mimic what assemble_name_raw does with a leading '*'. */
17204 if (name[0] == '*')
17205 name = &name[1];
17207 if (dwarf_version >= 4)
17208 add_AT_string (die, DW_AT_linkage_name, name);
17209 else
17210 add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17213 /* Add source coordinate attributes for the given decl. */
17215 static void
17216 add_src_coords_attributes (dw_die_ref die, tree decl)
17218 expanded_location s;
17220 if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
17221 return;
17222 s = expand_location (DECL_SOURCE_LOCATION (decl));
17223 add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17224 add_AT_unsigned (die, DW_AT_decl_line, s.line);
17227 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl. */
17229 static void
17230 add_linkage_name (dw_die_ref die, tree decl)
17232 if (debug_info_level > DINFO_LEVEL_NONE
17233 && (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17234 && TREE_PUBLIC (decl)
17235 && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17236 && die->die_tag != DW_TAG_member)
17238 /* Defer until we have an assembler name set. */
17239 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17241 limbo_die_node *asm_name;
17243 asm_name = ggc_cleared_alloc<limbo_die_node> ();
17244 asm_name->die = die;
17245 asm_name->created_for = decl;
17246 asm_name->next = deferred_asm_name;
17247 deferred_asm_name = asm_name;
17249 else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17250 add_linkage_attr (die, decl);
17254 /* Add a DW_AT_name attribute and source coordinate attribute for the
17255 given decl, but only if it actually has a name. */
17257 static void
17258 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17260 tree decl_name;
17262 decl_name = DECL_NAME (decl);
17263 if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17265 const char *name = dwarf2_name (decl, 0);
17266 if (name)
17267 add_name_attribute (die, name);
17268 if (! DECL_ARTIFICIAL (decl))
17269 add_src_coords_attributes (die, decl);
17271 add_linkage_name (die, decl);
17274 #ifdef VMS_DEBUGGING_INFO
17275 /* Get the function's name, as described by its RTL. This may be different
17276 from the DECL_NAME name used in the source file. */
17277 if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17279 add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17280 XEXP (DECL_RTL (decl), 0), false);
17281 vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
17283 #endif /* VMS_DEBUGGING_INFO */
17286 #ifdef VMS_DEBUGGING_INFO
17287 /* Output the debug main pointer die for VMS */
17289 void
17290 dwarf2out_vms_debug_main_pointer (void)
17292 char label[MAX_ARTIFICIAL_LABEL_BYTES];
17293 dw_die_ref die;
17295 /* Allocate the VMS debug main subprogram die. */
17296 die = ggc_cleared_alloc<die_node> ();
17297 die->die_tag = DW_TAG_subprogram;
17298 add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17299 ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17300 current_function_funcdef_no);
17301 add_AT_lbl_id (die, DW_AT_entry_pc, label);
17303 /* Make it the first child of comp_unit_die (). */
17304 die->die_parent = comp_unit_die ();
17305 if (comp_unit_die ()->die_child)
17307 die->die_sib = comp_unit_die ()->die_child->die_sib;
17308 comp_unit_die ()->die_child->die_sib = die;
17310 else
17312 die->die_sib = die;
17313 comp_unit_die ()->die_child = die;
17316 #endif /* VMS_DEBUGGING_INFO */
17318 /* Push a new declaration scope. */
17320 static void
17321 push_decl_scope (tree scope)
17323 vec_safe_push (decl_scope_table, scope);
17326 /* Pop a declaration scope. */
17328 static inline void
17329 pop_decl_scope (void)
17331 decl_scope_table->pop ();
17334 /* walk_tree helper function for uses_local_type, below. */
17336 static tree
17337 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
17339 if (!TYPE_P (*tp))
17340 *walk_subtrees = 0;
17341 else
17343 tree name = TYPE_NAME (*tp);
17344 if (name && DECL_P (name) && decl_function_context (name))
17345 return *tp;
17347 return NULL_TREE;
17350 /* If TYPE involves a function-local type (including a local typedef to a
17351 non-local type), returns that type; otherwise returns NULL_TREE. */
17353 static tree
17354 uses_local_type (tree type)
17356 tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
17357 return used;
17360 /* Return the DIE for the scope that immediately contains this type.
17361 Non-named types that do not involve a function-local type get global
17362 scope. Named types nested in namespaces or other types get their
17363 containing scope. All other types (i.e. function-local named types) get
17364 the current active scope. */
17366 static dw_die_ref
17367 scope_die_for (tree t, dw_die_ref context_die)
17369 dw_die_ref scope_die = NULL;
17370 tree containing_scope;
17372 /* Non-types always go in the current scope. */
17373 gcc_assert (TYPE_P (t));
17375 /* Use the scope of the typedef, rather than the scope of the type
17376 it refers to. */
17377 if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
17378 containing_scope = DECL_CONTEXT (TYPE_NAME (t));
17379 else
17380 containing_scope = TYPE_CONTEXT (t);
17382 /* Use the containing namespace if there is one. */
17383 if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17385 if (context_die == lookup_decl_die (containing_scope))
17386 /* OK */;
17387 else if (debug_info_level > DINFO_LEVEL_TERSE)
17388 context_die = get_context_die (containing_scope);
17389 else
17390 containing_scope = NULL_TREE;
17393 /* Ignore function type "scopes" from the C frontend. They mean that
17394 a tagged type is local to a parmlist of a function declarator, but
17395 that isn't useful to DWARF. */
17396 if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17397 containing_scope = NULL_TREE;
17399 if (SCOPE_FILE_SCOPE_P (containing_scope))
17401 /* If T uses a local type keep it local as well, to avoid references
17402 to function-local DIEs from outside the function. */
17403 if (current_function_decl && uses_local_type (t))
17404 scope_die = context_die;
17405 else
17406 scope_die = comp_unit_die ();
17408 else if (TYPE_P (containing_scope))
17410 /* For types, we can just look up the appropriate DIE. */
17411 if (debug_info_level > DINFO_LEVEL_TERSE)
17412 scope_die = get_context_die (containing_scope);
17413 else
17415 scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17416 if (scope_die == NULL)
17417 scope_die = comp_unit_die ();
17420 else
17421 scope_die = context_die;
17423 return scope_die;
17426 /* Returns nonzero if CONTEXT_DIE is internal to a function. */
17428 static inline int
17429 local_scope_p (dw_die_ref context_die)
17431 for (; context_die; context_die = context_die->die_parent)
17432 if (context_die->die_tag == DW_TAG_inlined_subroutine
17433 || context_die->die_tag == DW_TAG_subprogram)
17434 return 1;
17436 return 0;
17439 /* Returns nonzero if CONTEXT_DIE is a class. */
17441 static inline int
17442 class_scope_p (dw_die_ref context_die)
17444 return (context_die
17445 && (context_die->die_tag == DW_TAG_structure_type
17446 || context_die->die_tag == DW_TAG_class_type
17447 || context_die->die_tag == DW_TAG_interface_type
17448 || context_die->die_tag == DW_TAG_union_type));
17451 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17452 whether or not to treat a DIE in this context as a declaration. */
17454 static inline int
17455 class_or_namespace_scope_p (dw_die_ref context_die)
17457 return (class_scope_p (context_die)
17458 || (context_die && context_die->die_tag == DW_TAG_namespace));
17461 /* Many forms of DIEs require a "type description" attribute. This
17462 routine locates the proper "type descriptor" die for the type given
17463 by 'type' plus any additional qualifiers given by 'cv_quals', and
17464 adds a DW_AT_type attribute below the given die. */
17466 static void
17467 add_type_attribute (dw_die_ref object_die, tree type, int cv_quals,
17468 dw_die_ref context_die)
17470 enum tree_code code = TREE_CODE (type);
17471 dw_die_ref type_die = NULL;
17473 /* ??? If this type is an unnamed subrange type of an integral, floating-point
17474 or fixed-point type, use the inner type. This is because we have no
17475 support for unnamed types in base_type_die. This can happen if this is
17476 an Ada subrange type. Correct solution is emit a subrange type die. */
17477 if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17478 && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17479 type = TREE_TYPE (type), code = TREE_CODE (type);
17481 if (code == ERROR_MARK
17482 /* Handle a special case. For functions whose return type is void, we
17483 generate *no* type attribute. (Note that no object may have type
17484 `void', so this only applies to function return types). */
17485 || code == VOID_TYPE)
17486 return;
17488 type_die = modified_type_die (type,
17489 cv_quals | TYPE_QUALS_NO_ADDR_SPACE (type),
17490 context_die);
17492 if (type_die != NULL)
17493 add_AT_die_ref (object_die, DW_AT_type, type_die);
17496 /* Given an object die, add the calling convention attribute for the
17497 function call type. */
17498 static void
17499 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17501 enum dwarf_calling_convention value = DW_CC_normal;
17503 value = ((enum dwarf_calling_convention)
17504 targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17506 if (is_fortran ()
17507 && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17509 /* DWARF 2 doesn't provide a way to identify a program's source-level
17510 entry point. DW_AT_calling_convention attributes are only meant
17511 to describe functions' calling conventions. However, lacking a
17512 better way to signal the Fortran main program, we used this for
17513 a long time, following existing custom. Now, DWARF 4 has
17514 DW_AT_main_subprogram, which we add below, but some tools still
17515 rely on the old way, which we thus keep. */
17516 value = DW_CC_program;
17518 if (dwarf_version >= 4 || !dwarf_strict)
17519 add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17522 /* Only add the attribute if the backend requests it, and
17523 is not DW_CC_normal. */
17524 if (value && (value != DW_CC_normal))
17525 add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17528 /* Given a tree pointer to a struct, class, union, or enum type node, return
17529 a pointer to the (string) tag name for the given type, or zero if the type
17530 was declared without a tag. */
17532 static const char *
17533 type_tag (const_tree type)
17535 const char *name = 0;
17537 if (TYPE_NAME (type) != 0)
17539 tree t = 0;
17541 /* Find the IDENTIFIER_NODE for the type name. */
17542 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17543 && !TYPE_NAMELESS (type))
17544 t = TYPE_NAME (type);
17546 /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17547 a TYPE_DECL node, regardless of whether or not a `typedef' was
17548 involved. */
17549 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17550 && ! DECL_IGNORED_P (TYPE_NAME (type)))
17552 /* We want to be extra verbose. Don't call dwarf_name if
17553 DECL_NAME isn't set. The default hook for decl_printable_name
17554 doesn't like that, and in this context it's correct to return
17555 0, instead of "<anonymous>" or the like. */
17556 if (DECL_NAME (TYPE_NAME (type))
17557 && !DECL_NAMELESS (TYPE_NAME (type)))
17558 name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17561 /* Now get the name as a string, or invent one. */
17562 if (!name && t != 0)
17563 name = IDENTIFIER_POINTER (t);
17566 return (name == 0 || *name == '\0') ? 0 : name;
17569 /* Return the type associated with a data member, make a special check
17570 for bit field types. */
17572 static inline tree
17573 member_declared_type (const_tree member)
17575 return (DECL_BIT_FIELD_TYPE (member)
17576 ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17579 /* Get the decl's label, as described by its RTL. This may be different
17580 from the DECL_NAME name used in the source file. */
17582 #if 0
17583 static const char *
17584 decl_start_label (tree decl)
17586 rtx x;
17587 const char *fnname;
17589 x = DECL_RTL (decl);
17590 gcc_assert (MEM_P (x));
17592 x = XEXP (x, 0);
17593 gcc_assert (GET_CODE (x) == SYMBOL_REF);
17595 fnname = XSTR (x, 0);
17596 return fnname;
17598 #endif
17600 /* For variable-length arrays that have been previously generated, but
17601 may be incomplete due to missing subscript info, fill the subscript
17602 info. Return TRUE if this is one of those cases. */
17603 static bool
17604 fill_variable_array_bounds (tree type)
17606 if (TREE_ASM_WRITTEN (type)
17607 && TREE_CODE (type) == ARRAY_TYPE
17608 && variably_modified_type_p (type, NULL))
17610 dw_die_ref array_die = lookup_type_die (type);
17611 if (!array_die)
17612 return false;
17613 add_subscript_info (array_die, type, !is_ada ());
17614 return true;
17616 return false;
17619 /* These routines generate the internal representation of the DIE's for
17620 the compilation unit. Debugging information is collected by walking
17621 the declaration trees passed in from dwarf2out_decl(). */
17623 static void
17624 gen_array_type_die (tree type, dw_die_ref context_die)
17626 dw_die_ref array_die;
17628 /* GNU compilers represent multidimensional array types as sequences of one
17629 dimensional array types whose element types are themselves array types.
17630 We sometimes squish that down to a single array_type DIE with multiple
17631 subscripts in the Dwarf debugging info. The draft Dwarf specification
17632 say that we are allowed to do this kind of compression in C, because
17633 there is no difference between an array of arrays and a multidimensional
17634 array. We don't do this for Ada to remain as close as possible to the
17635 actual representation, which is especially important against the language
17636 flexibilty wrt arrays of variable size. */
17638 bool collapse_nested_arrays = !is_ada ();
17640 if (fill_variable_array_bounds (type))
17641 return;
17643 dw_die_ref scope_die = scope_die_for (type, context_die);
17644 tree element_type;
17646 /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17647 DW_TAG_string_type doesn't have DW_AT_type attribute). */
17648 if (TYPE_STRING_FLAG (type)
17649 && TREE_CODE (type) == ARRAY_TYPE
17650 && is_fortran ()
17651 && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17653 HOST_WIDE_INT size;
17655 array_die = new_die (DW_TAG_string_type, scope_die, type);
17656 add_name_attribute (array_die, type_tag (type));
17657 equate_type_number_to_die (type, array_die);
17658 size = int_size_in_bytes (type);
17659 if (size >= 0)
17660 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17661 else if (TYPE_DOMAIN (type) != NULL_TREE
17662 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17663 && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17665 tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17666 dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2, NULL);
17668 size = int_size_in_bytes (TREE_TYPE (szdecl));
17669 if (loc && size > 0)
17671 add_AT_location_description (array_die, DW_AT_string_length, loc);
17672 if (size != DWARF2_ADDR_SIZE)
17673 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17676 return;
17679 array_die = new_die (DW_TAG_array_type, scope_die, type);
17680 add_name_attribute (array_die, type_tag (type));
17681 equate_type_number_to_die (type, array_die);
17683 if (TREE_CODE (type) == VECTOR_TYPE)
17684 add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17686 /* For Fortran multidimensional arrays use DW_ORD_col_major ordering. */
17687 if (is_fortran ()
17688 && TREE_CODE (type) == ARRAY_TYPE
17689 && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17690 && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17691 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17693 #if 0
17694 /* We default the array ordering. SDB will probably do
17695 the right things even if DW_AT_ordering is not present. It's not even
17696 an issue until we start to get into multidimensional arrays anyway. If
17697 SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17698 then we'll have to put the DW_AT_ordering attribute back in. (But if
17699 and when we find out that we need to put these in, we will only do so
17700 for multidimensional arrays. */
17701 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17702 #endif
17704 if (TREE_CODE (type) == VECTOR_TYPE)
17706 /* For VECTOR_TYPEs we use an array die with appropriate bounds. */
17707 dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
17708 add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node, NULL);
17709 add_bound_info (subrange_die, DW_AT_upper_bound,
17710 size_int (TYPE_VECTOR_SUBPARTS (type) - 1), NULL);
17712 else
17713 add_subscript_info (array_die, type, collapse_nested_arrays);
17715 /* Add representation of the type of the elements of this array type and
17716 emit the corresponding DIE if we haven't done it already. */
17717 element_type = TREE_TYPE (type);
17718 if (collapse_nested_arrays)
17719 while (TREE_CODE (element_type) == ARRAY_TYPE)
17721 if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17722 break;
17723 element_type = TREE_TYPE (element_type);
17726 add_type_attribute (array_die, element_type, TYPE_UNQUALIFIED, context_die);
17728 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17729 if (TYPE_ARTIFICIAL (type))
17730 add_AT_flag (array_die, DW_AT_artificial, 1);
17732 if (get_AT (array_die, DW_AT_name))
17733 add_pubtype (type, array_die);
17736 /* This routine generates DIE for array with hidden descriptor, details
17737 are filled into *info by a langhook. */
17739 static void
17740 gen_descr_array_type_die (tree type, struct array_descr_info *info,
17741 dw_die_ref context_die)
17743 const dw_die_ref scope_die = scope_die_for (type, context_die);
17744 const dw_die_ref array_die = new_die (DW_TAG_array_type, scope_die, type);
17745 const struct loc_descr_context context = { type, info->base_decl };
17746 int dim;
17748 add_name_attribute (array_die, type_tag (type));
17749 equate_type_number_to_die (type, array_die);
17751 if (info->ndimensions > 1)
17752 switch (info->ordering)
17754 case array_descr_ordering_row_major:
17755 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17756 break;
17757 case array_descr_ordering_column_major:
17758 add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17759 break;
17760 default:
17761 break;
17764 if (dwarf_version >= 3 || !dwarf_strict)
17766 if (info->data_location)
17767 add_scalar_info (array_die, DW_AT_data_location, info->data_location,
17768 dw_scalar_form_exprloc, &context);
17769 if (info->associated)
17770 add_scalar_info (array_die, DW_AT_associated, info->associated,
17771 dw_scalar_form_constant
17772 | dw_scalar_form_exprloc
17773 | dw_scalar_form_reference, &context);
17774 if (info->allocated)
17775 add_scalar_info (array_die, DW_AT_allocated, info->allocated,
17776 dw_scalar_form_constant
17777 | dw_scalar_form_exprloc
17778 | dw_scalar_form_reference, &context);
17781 add_gnat_descriptive_type_attribute (array_die, type, context_die);
17783 for (dim = 0; dim < info->ndimensions; dim++)
17785 dw_die_ref subrange_die
17786 = new_die (DW_TAG_subrange_type, array_die, NULL);
17788 if (info->dimen[dim].bounds_type)
17789 add_type_attribute (subrange_die,
17790 info->dimen[dim].bounds_type, 0,
17791 context_die);
17792 if (info->dimen[dim].lower_bound)
17793 add_bound_info (subrange_die, DW_AT_lower_bound,
17794 info->dimen[dim].lower_bound, &context);
17795 if (info->dimen[dim].upper_bound)
17796 add_bound_info (subrange_die, DW_AT_upper_bound,
17797 info->dimen[dim].upper_bound, &context);
17798 if ((dwarf_version >= 3 || !dwarf_strict) && info->dimen[dim].stride)
17799 add_scalar_info (subrange_die, DW_AT_byte_stride,
17800 info->dimen[dim].stride,
17801 dw_scalar_form_constant
17802 | dw_scalar_form_exprloc
17803 | dw_scalar_form_reference,
17804 &context);
17807 gen_type_die (info->element_type, context_die);
17808 add_type_attribute (array_die, info->element_type, TYPE_UNQUALIFIED,
17809 context_die);
17811 if (get_AT (array_die, DW_AT_name))
17812 add_pubtype (type, array_die);
17815 #if 0
17816 static void
17817 gen_entry_point_die (tree decl, dw_die_ref context_die)
17819 tree origin = decl_ultimate_origin (decl);
17820 dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
17822 if (origin != NULL)
17823 add_abstract_origin_attribute (decl_die, origin);
17824 else
17826 add_name_and_src_coords_attributes (decl_die, decl);
17827 add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
17828 TYPE_UNQUALIFIED, context_die);
17831 if (DECL_ABSTRACT_P (decl))
17832 equate_decl_number_to_die (decl, decl_die);
17833 else
17834 add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
17836 #endif
17838 /* Walk through the list of incomplete types again, trying once more to
17839 emit full debugging info for them. */
17841 static void
17842 retry_incomplete_types (void)
17844 int i;
17846 for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
17847 if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
17848 gen_type_die ((*incomplete_types)[i], comp_unit_die ());
17851 /* Determine what tag to use for a record type. */
17853 static enum dwarf_tag
17854 record_type_tag (tree type)
17856 if (! lang_hooks.types.classify_record)
17857 return DW_TAG_structure_type;
17859 switch (lang_hooks.types.classify_record (type))
17861 case RECORD_IS_STRUCT:
17862 return DW_TAG_structure_type;
17864 case RECORD_IS_CLASS:
17865 return DW_TAG_class_type;
17867 case RECORD_IS_INTERFACE:
17868 if (dwarf_version >= 3 || !dwarf_strict)
17869 return DW_TAG_interface_type;
17870 return DW_TAG_structure_type;
17872 default:
17873 gcc_unreachable ();
17877 /* Generate a DIE to represent an enumeration type. Note that these DIEs
17878 include all of the information about the enumeration values also. Each
17879 enumerated type name/value is listed as a child of the enumerated type
17880 DIE. */
17882 static dw_die_ref
17883 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17885 dw_die_ref type_die = lookup_type_die (type);
17887 if (type_die == NULL)
17889 type_die = new_die (DW_TAG_enumeration_type,
17890 scope_die_for (type, context_die), type);
17891 equate_type_number_to_die (type, type_die);
17892 add_name_attribute (type_die, type_tag (type));
17893 if (dwarf_version >= 4 || !dwarf_strict)
17895 if (ENUM_IS_SCOPED (type))
17896 add_AT_flag (type_die, DW_AT_enum_class, 1);
17897 if (ENUM_IS_OPAQUE (type))
17898 add_AT_flag (type_die, DW_AT_declaration, 1);
17901 else if (! TYPE_SIZE (type))
17902 return type_die;
17903 else
17904 remove_AT (type_die, DW_AT_declaration);
17906 /* Handle a GNU C/C++ extension, i.e. incomplete enum types. If the
17907 given enum type is incomplete, do not generate the DW_AT_byte_size
17908 attribute or the DW_AT_element_list attribute. */
17909 if (TYPE_SIZE (type))
17911 tree link;
17913 TREE_ASM_WRITTEN (type) = 1;
17914 add_byte_size_attribute (type_die, type);
17915 if (dwarf_version >= 3 || !dwarf_strict)
17917 tree underlying = lang_hooks.types.enum_underlying_base_type (type);
17918 add_type_attribute (type_die, underlying, TYPE_UNQUALIFIED,
17919 context_die);
17921 if (TYPE_STUB_DECL (type) != NULL_TREE)
17923 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17924 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17927 /* If the first reference to this type was as the return type of an
17928 inline function, then it may not have a parent. Fix this now. */
17929 if (type_die->die_parent == NULL)
17930 add_child_die (scope_die_for (type, context_die), type_die);
17932 for (link = TYPE_VALUES (type);
17933 link != NULL; link = TREE_CHAIN (link))
17935 dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17936 tree value = TREE_VALUE (link);
17938 add_name_attribute (enum_die,
17939 IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17941 if (TREE_CODE (value) == CONST_DECL)
17942 value = DECL_INITIAL (value);
17944 if (simple_type_size_in_bits (TREE_TYPE (value))
17945 <= HOST_BITS_PER_WIDE_INT || tree_fits_shwi_p (value))
17947 /* For constant forms created by add_AT_unsigned DWARF
17948 consumers (GDB, elfutils, etc.) always zero extend
17949 the value. Only when the actual value is negative
17950 do we need to use add_AT_int to generate a constant
17951 form that can represent negative values. */
17952 HOST_WIDE_INT val = TREE_INT_CST_LOW (value);
17953 if (TYPE_UNSIGNED (TREE_TYPE (value)) || val >= 0)
17954 add_AT_unsigned (enum_die, DW_AT_const_value,
17955 (unsigned HOST_WIDE_INT) val);
17956 else
17957 add_AT_int (enum_die, DW_AT_const_value, val);
17959 else
17960 /* Enumeration constants may be wider than HOST_WIDE_INT. Handle
17961 that here. TODO: This should be re-worked to use correct
17962 signed/unsigned double tags for all cases. */
17963 add_AT_wide (enum_die, DW_AT_const_value, value);
17966 add_gnat_descriptive_type_attribute (type_die, type, context_die);
17967 if (TYPE_ARTIFICIAL (type))
17968 add_AT_flag (type_die, DW_AT_artificial, 1);
17970 else
17971 add_AT_flag (type_die, DW_AT_declaration, 1);
17973 add_pubtype (type, type_die);
17975 return type_die;
17978 /* Generate a DIE to represent either a real live formal parameter decl or to
17979 represent just the type of some formal parameter position in some function
17980 type.
17982 Note that this routine is a bit unusual because its argument may be a
17983 ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17984 represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17985 node. If it's the former then this function is being called to output a
17986 DIE to represent a formal parameter object (or some inlining thereof). If
17987 it's the latter, then this function is only being called to output a
17988 DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17989 argument type of some subprogram type.
17990 If EMIT_NAME_P is true, name and source coordinate attributes
17991 are emitted. */
17993 static dw_die_ref
17994 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17995 dw_die_ref context_die)
17997 tree node_or_origin = node ? node : origin;
17998 tree ultimate_origin;
17999 dw_die_ref parm_die = NULL;
18001 if (TREE_CODE_CLASS (TREE_CODE (node_or_origin)) == tcc_declaration)
18003 parm_die = lookup_decl_die (node);
18005 /* If the contexts differ, we may not be talking about the same
18006 thing. */
18007 if (parm_die && parm_die->die_parent != context_die)
18009 if (!DECL_ABSTRACT_P (node))
18011 /* This can happen when creating an inlined instance, in
18012 which case we need to create a new DIE that will get
18013 annotated with DW_AT_abstract_origin. */
18014 parm_die = NULL;
18016 else
18018 /* FIXME: Reuse DIE even with a differing context.
18020 This can happen when calling
18021 dwarf2out_abstract_function to build debug info for
18022 the abstract instance of a function for which we have
18023 already generated a DIE in
18024 dwarf2out_early_global_decl.
18026 Once we remove dwarf2out_abstract_function, we should
18027 have a call to gcc_unreachable here. */
18031 if (parm_die && parm_die->die_parent == NULL)
18033 /* Check that parm_die already has the right attributes that
18034 we would have added below. If any attributes are
18035 missing, fall through to add them. */
18036 if (! DECL_ABSTRACT_P (node_or_origin)
18037 && !get_AT (parm_die, DW_AT_location)
18038 && !get_AT (parm_die, DW_AT_const_value))
18039 /* We are missing location info, and are about to add it. */
18041 else
18043 add_child_die (context_die, parm_die);
18044 return parm_die;
18049 /* If we have a previously generated DIE, use it, unless this is an
18050 concrete instance (origin != NULL), in which case we need a new
18051 DIE with a corresponding DW_AT_abstract_origin. */
18052 bool reusing_die;
18053 if (parm_die && origin == NULL)
18054 reusing_die = true;
18055 else
18057 parm_die = new_die (DW_TAG_formal_parameter, context_die, node);
18058 reusing_die = false;
18061 switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18063 case tcc_declaration:
18064 ultimate_origin = decl_ultimate_origin (node_or_origin);
18065 if (node || ultimate_origin)
18066 origin = ultimate_origin;
18068 if (reusing_die)
18069 goto add_location;
18071 if (origin != NULL)
18072 add_abstract_origin_attribute (parm_die, origin);
18073 else if (emit_name_p)
18074 add_name_and_src_coords_attributes (parm_die, node);
18075 if (origin == NULL
18076 || (! DECL_ABSTRACT_P (node_or_origin)
18077 && variably_modified_type_p (TREE_TYPE (node_or_origin),
18078 decl_function_context
18079 (node_or_origin))))
18081 tree type = TREE_TYPE (node_or_origin);
18082 if (decl_by_reference_p (node_or_origin))
18083 add_type_attribute (parm_die, TREE_TYPE (type),
18084 TYPE_UNQUALIFIED, context_die);
18085 else
18086 add_type_attribute (parm_die, type,
18087 decl_quals (node_or_origin),
18088 context_die);
18090 if (origin == NULL && DECL_ARTIFICIAL (node))
18091 add_AT_flag (parm_die, DW_AT_artificial, 1);
18092 add_location:
18093 if (node && node != origin)
18094 equate_decl_number_to_die (node, parm_die);
18095 if (! DECL_ABSTRACT_P (node_or_origin))
18096 add_location_or_const_value_attribute (parm_die, node_or_origin,
18097 node == NULL, DW_AT_location);
18099 break;
18101 case tcc_type:
18102 /* We were called with some kind of a ..._TYPE node. */
18103 add_type_attribute (parm_die, node_or_origin, TYPE_UNQUALIFIED,
18104 context_die);
18105 break;
18107 default:
18108 gcc_unreachable ();
18111 return parm_die;
18114 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18115 children DW_TAG_formal_parameter DIEs representing the arguments of the
18116 parameter pack.
18118 PARM_PACK must be a function parameter pack.
18119 PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18120 must point to the subsequent arguments of the function PACK_ARG belongs to.
18121 SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18122 If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18123 following the last one for which a DIE was generated. */
18125 static dw_die_ref
18126 gen_formal_parameter_pack_die (tree parm_pack,
18127 tree pack_arg,
18128 dw_die_ref subr_die,
18129 tree *next_arg)
18131 tree arg;
18132 dw_die_ref parm_pack_die;
18134 gcc_assert (parm_pack
18135 && lang_hooks.function_parameter_pack_p (parm_pack)
18136 && subr_die);
18138 parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18139 add_src_coords_attributes (parm_pack_die, parm_pack);
18141 for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18143 if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18144 parm_pack))
18145 break;
18146 gen_formal_parameter_die (arg, NULL,
18147 false /* Don't emit name attribute. */,
18148 parm_pack_die);
18150 if (next_arg)
18151 *next_arg = arg;
18152 return parm_pack_die;
18155 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18156 at the end of an (ANSI prototyped) formal parameters list. */
18158 static void
18159 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18161 new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18164 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18165 DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18166 parameters as specified in some function type specification (except for
18167 those which appear as part of a function *definition*). */
18169 static void
18170 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18172 tree link;
18173 tree formal_type = NULL;
18174 tree first_parm_type;
18175 tree arg;
18177 if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18179 arg = DECL_ARGUMENTS (function_or_method_type);
18180 function_or_method_type = TREE_TYPE (function_or_method_type);
18182 else
18183 arg = NULL_TREE;
18185 first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18187 /* Make our first pass over the list of formal parameter types and output a
18188 DW_TAG_formal_parameter DIE for each one. */
18189 for (link = first_parm_type; link; )
18191 dw_die_ref parm_die;
18193 formal_type = TREE_VALUE (link);
18194 if (formal_type == void_type_node)
18195 break;
18197 /* Output a (nameless) DIE to represent the formal parameter itself. */
18198 if (!POINTER_BOUNDS_TYPE_P (formal_type))
18200 parm_die = gen_formal_parameter_die (formal_type, NULL,
18201 true /* Emit name attribute. */,
18202 context_die);
18203 if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18204 && link == first_parm_type)
18206 add_AT_flag (parm_die, DW_AT_artificial, 1);
18207 if (dwarf_version >= 3 || !dwarf_strict)
18208 add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18210 else if (arg && DECL_ARTIFICIAL (arg))
18211 add_AT_flag (parm_die, DW_AT_artificial, 1);
18214 link = TREE_CHAIN (link);
18215 if (arg)
18216 arg = DECL_CHAIN (arg);
18219 /* If this function type has an ellipsis, add a
18220 DW_TAG_unspecified_parameters DIE to the end of the parameter list. */
18221 if (formal_type != void_type_node)
18222 gen_unspecified_parameters_die (function_or_method_type, context_die);
18224 /* Make our second (and final) pass over the list of formal parameter types
18225 and output DIEs to represent those types (as necessary). */
18226 for (link = TYPE_ARG_TYPES (function_or_method_type);
18227 link && TREE_VALUE (link);
18228 link = TREE_CHAIN (link))
18229 gen_type_die (TREE_VALUE (link), context_die);
18232 /* We want to generate the DIE for TYPE so that we can generate the
18233 die for MEMBER, which has been defined; we will need to refer back
18234 to the member declaration nested within TYPE. If we're trying to
18235 generate minimal debug info for TYPE, processing TYPE won't do the
18236 trick; we need to attach the member declaration by hand. */
18238 static void
18239 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18241 gen_type_die (type, context_die);
18243 /* If we're trying to avoid duplicate debug info, we may not have
18244 emitted the member decl for this function. Emit it now. */
18245 if (TYPE_STUB_DECL (type)
18246 && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18247 && ! lookup_decl_die (member))
18249 dw_die_ref type_die;
18250 gcc_assert (!decl_ultimate_origin (member));
18252 push_decl_scope (type);
18253 type_die = lookup_type_die_strip_naming_typedef (type);
18254 if (TREE_CODE (member) == FUNCTION_DECL)
18255 gen_subprogram_die (member, type_die);
18256 else if (TREE_CODE (member) == FIELD_DECL)
18258 /* Ignore the nameless fields that are used to skip bits but handle
18259 C++ anonymous unions and structs. */
18260 if (DECL_NAME (member) != NULL_TREE
18261 || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18262 || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18264 gen_type_die (member_declared_type (member), type_die);
18265 gen_field_die (member, type_die);
18268 else
18269 gen_variable_die (member, NULL_TREE, type_die);
18271 pop_decl_scope ();
18275 /* Forward declare these functions, because they are mutually recursive
18276 with their set_block_* pairing functions. */
18277 static void set_decl_origin_self (tree);
18278 static void set_decl_abstract_flags (tree, vec<tree> &);
18280 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
18281 given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
18282 that it points to the node itself, thus indicating that the node is its
18283 own (abstract) origin. Additionally, if the BLOCK_ABSTRACT_ORIGIN for
18284 the given node is NULL, recursively descend the decl/block tree which
18285 it is the root of, and for each other ..._DECL or BLOCK node contained
18286 therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
18287 still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
18288 values to point to themselves. */
18290 static void
18291 set_block_origin_self (tree stmt)
18293 if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
18295 BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
18298 tree local_decl;
18300 for (local_decl = BLOCK_VARS (stmt);
18301 local_decl != NULL_TREE;
18302 local_decl = DECL_CHAIN (local_decl))
18303 /* Do not recurse on nested functions since the inlining status
18304 of parent and child can be different as per the DWARF spec. */
18305 if (TREE_CODE (local_decl) != FUNCTION_DECL
18306 && !DECL_EXTERNAL (local_decl))
18307 set_decl_origin_self (local_decl);
18311 tree subblock;
18313 for (subblock = BLOCK_SUBBLOCKS (stmt);
18314 subblock != NULL_TREE;
18315 subblock = BLOCK_CHAIN (subblock))
18316 set_block_origin_self (subblock); /* Recurse. */
18321 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
18322 the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
18323 node to so that it points to the node itself, thus indicating that the
18324 node represents its own (abstract) origin. Additionally, if the
18325 DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
18326 the decl/block tree of which the given node is the root of, and for
18327 each other ..._DECL or BLOCK node contained therein whose
18328 DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
18329 set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
18330 point to themselves. */
18332 static void
18333 set_decl_origin_self (tree decl)
18335 if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
18337 DECL_ABSTRACT_ORIGIN (decl) = decl;
18338 if (TREE_CODE (decl) == FUNCTION_DECL)
18340 tree arg;
18342 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18343 DECL_ABSTRACT_ORIGIN (arg) = arg;
18344 if (DECL_INITIAL (decl) != NULL_TREE
18345 && DECL_INITIAL (decl) != error_mark_node)
18346 set_block_origin_self (DECL_INITIAL (decl));
18351 /* Given a pointer to some BLOCK node, set the BLOCK_ABSTRACT flag to 1
18352 and if it wasn't 1 before, push it to abstract_vec vector.
18353 For all local decls and all local sub-blocks (recursively) do it
18354 too. */
18356 static void
18357 set_block_abstract_flags (tree stmt, vec<tree> &abstract_vec)
18359 tree local_decl;
18360 tree subblock;
18361 unsigned int i;
18363 if (!BLOCK_ABSTRACT (stmt))
18365 abstract_vec.safe_push (stmt);
18366 BLOCK_ABSTRACT (stmt) = 1;
18369 for (local_decl = BLOCK_VARS (stmt);
18370 local_decl != NULL_TREE;
18371 local_decl = DECL_CHAIN (local_decl))
18372 if (! DECL_EXTERNAL (local_decl))
18373 set_decl_abstract_flags (local_decl, abstract_vec);
18375 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18377 local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
18378 if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
18379 || TREE_CODE (local_decl) == PARM_DECL)
18380 set_decl_abstract_flags (local_decl, abstract_vec);
18383 for (subblock = BLOCK_SUBBLOCKS (stmt);
18384 subblock != NULL_TREE;
18385 subblock = BLOCK_CHAIN (subblock))
18386 set_block_abstract_flags (subblock, abstract_vec);
18389 /* Given a pointer to some ..._DECL node, set DECL_ABSTRACT_P flag on it
18390 to 1 and if it wasn't 1 before, push to abstract_vec vector.
18391 In the case where the decl is a FUNCTION_DECL also set the abstract
18392 flags for all of the parameters, local vars, local
18393 blocks and sub-blocks (recursively). */
18395 static void
18396 set_decl_abstract_flags (tree decl, vec<tree> &abstract_vec)
18398 if (!DECL_ABSTRACT_P (decl))
18400 abstract_vec.safe_push (decl);
18401 DECL_ABSTRACT_P (decl) = 1;
18404 if (TREE_CODE (decl) == FUNCTION_DECL)
18406 tree arg;
18408 for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
18409 if (!DECL_ABSTRACT_P (arg))
18411 abstract_vec.safe_push (arg);
18412 DECL_ABSTRACT_P (arg) = 1;
18414 if (DECL_INITIAL (decl) != NULL_TREE
18415 && DECL_INITIAL (decl) != error_mark_node)
18416 set_block_abstract_flags (DECL_INITIAL (decl), abstract_vec);
18420 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18421 may later generate inlined and/or out-of-line instances of.
18423 FIXME: In the early-dwarf world, this function, and most of the
18424 DECL_ABSTRACT code should be obsoleted. The early DIE _is_
18425 the abstract instance. All we would need to do is annotate
18426 the early DIE with the appropriate DW_AT_inline in late
18427 dwarf (perhaps in gen_inlined_subroutine_die).
18429 However, we can't do this yet, because LTO streaming of DIEs
18430 has not been implemented yet. */
18432 static void
18433 dwarf2out_abstract_function (tree decl)
18435 dw_die_ref old_die;
18436 tree save_fn;
18437 tree context;
18438 hash_table<decl_loc_hasher> *old_decl_loc_table;
18439 hash_table<dw_loc_list_hasher> *old_cached_dw_loc_list_table;
18440 int old_call_site_count, old_tail_call_site_count;
18441 struct call_arg_loc_node *old_call_arg_locations;
18443 /* Make sure we have the actual abstract inline, not a clone. */
18444 decl = DECL_ORIGIN (decl);
18446 old_die = lookup_decl_die (decl);
18447 if (old_die && get_AT (old_die, DW_AT_inline))
18448 /* We've already generated the abstract instance. */
18449 return;
18451 /* We can be called while recursively when seeing block defining inlined subroutine
18452 DIE. Be sure to not clobber the outer location table nor use it or we would
18453 get locations in abstract instantces. */
18454 old_decl_loc_table = decl_loc_table;
18455 decl_loc_table = NULL;
18456 old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18457 cached_dw_loc_list_table = NULL;
18458 old_call_arg_locations = call_arg_locations;
18459 call_arg_locations = NULL;
18460 old_call_site_count = call_site_count;
18461 call_site_count = -1;
18462 old_tail_call_site_count = tail_call_site_count;
18463 tail_call_site_count = -1;
18465 /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18466 we don't get confused by DECL_ABSTRACT_P. */
18467 if (debug_info_level > DINFO_LEVEL_TERSE)
18469 context = decl_class_context (decl);
18470 if (context)
18471 gen_type_die_for_member
18472 (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18475 /* Pretend we've just finished compiling this function. */
18476 save_fn = current_function_decl;
18477 current_function_decl = decl;
18479 auto_vec<tree, 64> abstract_vec;
18480 set_decl_abstract_flags (decl, abstract_vec);
18481 dwarf2out_decl (decl);
18482 unsigned int i;
18483 tree t;
18484 FOR_EACH_VEC_ELT (abstract_vec, i, t)
18485 if (TREE_CODE (t) == BLOCK)
18486 BLOCK_ABSTRACT (t) = 0;
18487 else
18488 DECL_ABSTRACT_P (t) = 0;
18490 current_function_decl = save_fn;
18491 decl_loc_table = old_decl_loc_table;
18492 cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18493 call_arg_locations = old_call_arg_locations;
18494 call_site_count = old_call_site_count;
18495 tail_call_site_count = old_tail_call_site_count;
18498 /* Helper function of premark_used_types() which gets called through
18499 htab_traverse.
18501 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18502 marked as unused by prune_unused_types. */
18504 bool
18505 premark_used_types_helper (tree const &type, void *)
18507 dw_die_ref die;
18509 die = lookup_type_die (type);
18510 if (die != NULL)
18511 die->die_perennial_p = 1;
18512 return true;
18515 /* Helper function of premark_types_used_by_global_vars which gets called
18516 through htab_traverse.
18518 Marks the DIE of a given type in *SLOT as perennial, so it never gets
18519 marked as unused by prune_unused_types. The DIE of the type is marked
18520 only if the global variable using the type will actually be emitted. */
18523 premark_types_used_by_global_vars_helper (types_used_by_vars_entry **slot,
18524 void *)
18526 struct types_used_by_vars_entry *entry;
18527 dw_die_ref die;
18529 entry = (struct types_used_by_vars_entry *) *slot;
18530 gcc_assert (entry->type != NULL
18531 && entry->var_decl != NULL);
18532 die = lookup_type_die (entry->type);
18533 if (die)
18535 /* Ask cgraph if the global variable really is to be emitted.
18536 If yes, then we'll keep the DIE of ENTRY->TYPE. */
18537 varpool_node *node = varpool_node::get (entry->var_decl);
18538 if (node && node->definition)
18540 die->die_perennial_p = 1;
18541 /* Keep the parent DIEs as well. */
18542 while ((die = die->die_parent) && die->die_perennial_p == 0)
18543 die->die_perennial_p = 1;
18546 return 1;
18549 /* Mark all members of used_types_hash as perennial. */
18551 static void
18552 premark_used_types (struct function *fun)
18554 if (fun && fun->used_types_hash)
18555 fun->used_types_hash->traverse<void *, premark_used_types_helper> (NULL);
18558 /* Mark all members of types_used_by_vars_entry as perennial. */
18560 static void
18561 premark_types_used_by_global_vars (void)
18563 if (types_used_by_vars_hash)
18564 types_used_by_vars_hash
18565 ->traverse<void *, premark_types_used_by_global_vars_helper> (NULL);
18568 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18569 for CA_LOC call arg loc node. */
18571 static dw_die_ref
18572 gen_call_site_die (tree decl, dw_die_ref subr_die,
18573 struct call_arg_loc_node *ca_loc)
18575 dw_die_ref stmt_die = NULL, die;
18576 tree block = ca_loc->block;
18578 while (block
18579 && block != DECL_INITIAL (decl)
18580 && TREE_CODE (block) == BLOCK)
18582 stmt_die = BLOCK_DIE (block);
18583 if (stmt_die)
18584 break;
18585 block = BLOCK_SUPERCONTEXT (block);
18587 if (stmt_die == NULL)
18588 stmt_die = subr_die;
18589 die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18590 add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18591 if (ca_loc->tail_call_p)
18592 add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18593 if (ca_loc->symbol_ref)
18595 dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18596 if (tdie)
18597 add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18598 else
18599 add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
18601 return die;
18604 /* Generate a DIE to represent a declared function (either file-scope or
18605 block-local). */
18607 static void
18608 gen_subprogram_die (tree decl, dw_die_ref context_die)
18610 tree origin = decl_ultimate_origin (decl);
18611 dw_die_ref subr_die;
18612 dw_die_ref old_die = lookup_decl_die (decl);
18614 /* This function gets called multiple times for different stages of
18615 the debug process. For example, for func() in this code:
18617 namespace S
18619 void func() { ... }
18622 ...we get called 4 times. Twice in early debug and twice in
18623 late debug:
18625 Early debug
18626 -----------
18628 1. Once while generating func() within the namespace. This is
18629 the declaration. The declaration bit below is set, as the
18630 context is the namespace.
18632 A new DIE will be generated with DW_AT_declaration set.
18634 2. Once for func() itself. This is the specification. The
18635 declaration bit below is clear as the context is the CU.
18637 We will use the cached DIE from (1) to create a new DIE with
18638 DW_AT_specification pointing to the declaration in (1).
18640 Late debug via rest_of_handle_final()
18641 -------------------------------------
18643 3. Once generating func() within the namespace. This is also the
18644 declaration, as in (1), but this time we will early exit below
18645 as we have a cached DIE and a declaration needs no additional
18646 annotations (no locations), as the source declaration line
18647 info is enough.
18649 4. Once for func() itself. As in (2), this is the specification,
18650 but this time we will re-use the cached DIE, and just annotate
18651 it with the location information that should now be available.
18653 For something without namespaces, but with abstract instances, we
18654 are also called a multiple times:
18656 class Base
18658 public:
18659 Base (); // constructor declaration (1)
18662 Base::Base () { } // constructor specification (2)
18664 Early debug
18665 -----------
18667 1. Once for the Base() constructor by virtue of it being a
18668 member of the Base class. This is done via
18669 rest_of_type_compilation.
18671 This is a declaration, so a new DIE will be created with
18672 DW_AT_declaration.
18674 2. Once for the Base() constructor definition, but this time
18675 while generating the abstract instance of the base
18676 constructor (__base_ctor) which is being generated via early
18677 debug of reachable functions.
18679 Even though we have a cached version of the declaration (1),
18680 we will create a DW_AT_specification of the declaration DIE
18681 in (1).
18683 3. Once for the __base_ctor itself, but this time, we generate
18684 an DW_AT_abstract_origin version of the DW_AT_specification in
18685 (2).
18687 Late debug via rest_of_handle_final
18688 -----------------------------------
18690 4. One final time for the __base_ctor (which will have a cached
18691 DIE with DW_AT_abstract_origin created in (3). This time,
18692 we will just annotate the location information now
18693 available.
18695 int declaration = (current_function_decl != decl
18696 || class_or_namespace_scope_p (context_die));
18698 premark_used_types (DECL_STRUCT_FUNCTION (decl));
18700 /* Now that the C++ front end lazily declares artificial member fns, we
18701 might need to retrofit the declaration into its class. */
18702 if (!declaration && !origin && !old_die
18703 && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18704 && !class_or_namespace_scope_p (context_die)
18705 && debug_info_level > DINFO_LEVEL_TERSE)
18706 old_die = force_decl_die (decl);
18708 /* An inlined instance, tag a new DIE with DW_AT_abstract_origin. */
18709 if (origin != NULL)
18711 gcc_assert (!declaration || local_scope_p (context_die));
18713 /* Fixup die_parent for the abstract instance of a nested
18714 inline function. */
18715 if (old_die && old_die->die_parent == NULL)
18716 add_child_die (context_die, old_die);
18718 if (old_die && get_AT_ref (old_die, DW_AT_abstract_origin))
18720 /* If we have a DW_AT_abstract_origin we have a working
18721 cached version. */
18722 subr_die = old_die;
18724 else
18726 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18727 add_abstract_origin_attribute (subr_die, origin);
18728 /* This is where the actual code for a cloned function is.
18729 Let's emit linkage name attribute for it. This helps
18730 debuggers to e.g, set breakpoints into
18731 constructors/destructors when the user asks "break
18732 K::K". */
18733 add_linkage_name (subr_die, decl);
18736 /* A cached copy, possibly from early dwarf generation. Reuse as
18737 much as possible. */
18738 else if (old_die)
18740 /* A declaration that has been previously dumped needs no
18741 additional information. */
18742 if (declaration)
18743 return;
18745 if (!get_AT_flag (old_die, DW_AT_declaration)
18746 /* We can have a normal definition following an inline one in the
18747 case of redefinition of GNU C extern inlines.
18748 It seems reasonable to use AT_specification in this case. */
18749 && !get_AT (old_die, DW_AT_inline))
18751 /* Detect and ignore this case, where we are trying to output
18752 something we have already output. */
18753 if (get_AT (old_die, DW_AT_low_pc)
18754 || get_AT (old_die, DW_AT_ranges))
18755 return;
18757 /* If we have no location information, this must be a
18758 partially generated DIE from early dwarf generation.
18759 Fall through and generate it. */
18762 /* If the definition comes from the same place as the declaration,
18763 maybe use the old DIE. We always want the DIE for this function
18764 that has the *_pc attributes to be under comp_unit_die so the
18765 debugger can find it. We also need to do this for abstract
18766 instances of inlines, since the spec requires the out-of-line copy
18767 to have the same parent. For local class methods, this doesn't
18768 apply; we just use the old DIE. */
18769 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18770 struct dwarf_file_data * file_index = lookup_filename (s.file);
18771 if ((is_cu_die (old_die->die_parent)
18772 /* This condition fixes the inconsistency/ICE with the
18773 following Fortran test (or some derivative thereof) while
18774 building libgfortran:
18776 module some_m
18777 contains
18778 logical function funky (FLAG)
18779 funky = .true.
18780 end function
18781 end module
18783 || (old_die->die_parent
18784 && old_die->die_parent->die_tag == DW_TAG_module)
18785 || context_die == NULL)
18786 && (DECL_ARTIFICIAL (decl)
18787 || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18788 && (get_AT_unsigned (old_die, DW_AT_decl_line)
18789 == (unsigned) s.line))))
18791 subr_die = old_die;
18793 /* Clear out the declaration attribute, but leave the
18794 parameters so they can be augmented with location
18795 information later. Unless this was a declaration, in
18796 which case, wipe out the nameless parameters and recreate
18797 them further down. */
18798 if (remove_AT (subr_die, DW_AT_declaration))
18801 remove_AT (subr_die, DW_AT_object_pointer);
18802 remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18805 /* Make a specification pointing to the previously built
18806 declaration. */
18807 else
18809 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18810 add_AT_specification (subr_die, old_die);
18811 add_pubname (decl, subr_die);
18812 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18813 add_AT_file (subr_die, DW_AT_decl_file, file_index);
18814 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18815 add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18817 /* If the prototype had an 'auto' or 'decltype(auto)' return type,
18818 emit the real type on the definition die. */
18819 if (is_cxx() && debug_info_level > DINFO_LEVEL_TERSE)
18821 dw_die_ref die = get_AT_ref (old_die, DW_AT_type);
18822 if (die == auto_die || die == decltype_auto_die)
18823 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18824 TYPE_UNQUALIFIED, context_die);
18828 /* Create a fresh DIE for anything else. */
18829 else
18831 subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18833 if (TREE_PUBLIC (decl))
18834 add_AT_flag (subr_die, DW_AT_external, 1);
18836 add_name_and_src_coords_attributes (subr_die, decl);
18837 add_pubname (decl, subr_die);
18838 if (debug_info_level > DINFO_LEVEL_TERSE)
18840 add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18841 add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18842 TYPE_UNQUALIFIED, context_die);
18845 add_pure_or_virtual_attribute (subr_die, decl);
18846 if (DECL_ARTIFICIAL (decl))
18847 add_AT_flag (subr_die, DW_AT_artificial, 1);
18849 if (TREE_THIS_VOLATILE (decl) && (dwarf_version >= 5 || !dwarf_strict))
18850 add_AT_flag (subr_die, DW_AT_noreturn, 1);
18852 add_accessibility_attribute (subr_die, decl);
18855 /* Unless we have an existing non-declaration DIE, equate the new
18856 DIE. */
18857 if (!old_die || is_declaration_die (old_die))
18858 equate_decl_number_to_die (decl, subr_die);
18860 if (declaration)
18862 if (!old_die || !get_AT (old_die, DW_AT_inline))
18864 add_AT_flag (subr_die, DW_AT_declaration, 1);
18866 /* If this is an explicit function declaration then generate
18867 a DW_AT_explicit attribute. */
18868 if (lang_hooks.decls.function_decl_explicit_p (decl)
18869 && (dwarf_version >= 3 || !dwarf_strict))
18870 add_AT_flag (subr_die, DW_AT_explicit, 1);
18872 /* If this is a C++11 deleted special function member then generate
18873 a DW_AT_GNU_deleted attribute. */
18874 if (lang_hooks.decls.function_decl_deleted_p (decl)
18875 && (! dwarf_strict))
18876 add_AT_flag (subr_die, DW_AT_GNU_deleted, 1);
18879 /* Tag abstract instances with DW_AT_inline. */
18880 else if (DECL_ABSTRACT_P (decl))
18882 if (DECL_DECLARED_INLINE_P (decl))
18884 if (cgraph_function_possibly_inlined_p (decl))
18885 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18886 else
18887 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18889 else
18891 if (cgraph_function_possibly_inlined_p (decl))
18892 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18893 else
18894 add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18897 if (DECL_DECLARED_INLINE_P (decl)
18898 && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18899 add_AT_flag (subr_die, DW_AT_artificial, 1);
18901 /* For non DECL_EXTERNALs, if range information is available, fill
18902 the DIE with it. */
18903 else if (!DECL_EXTERNAL (decl) && !early_dwarf)
18905 HOST_WIDE_INT cfa_fb_offset;
18907 struct function *fun = DECL_STRUCT_FUNCTION (decl);
18909 if (!flag_reorder_blocks_and_partition)
18911 dw_fde_ref fde = fun->fde;
18912 if (fde->dw_fde_begin)
18914 /* We have already generated the labels. */
18915 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18916 fde->dw_fde_end, false);
18918 else
18920 /* Create start/end labels and add the range. */
18921 char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
18922 char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
18923 ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
18924 current_function_funcdef_no);
18925 ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
18926 current_function_funcdef_no);
18927 add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
18928 false);
18931 #if VMS_DEBUGGING_INFO
18932 /* HP OpenVMS Industry Standard 64: DWARF Extensions
18933 Section 2.3 Prologue and Epilogue Attributes:
18934 When a breakpoint is set on entry to a function, it is generally
18935 desirable for execution to be suspended, not on the very first
18936 instruction of the function, but rather at a point after the
18937 function's frame has been set up, after any language defined local
18938 declaration processing has been completed, and before execution of
18939 the first statement of the function begins. Debuggers generally
18940 cannot properly determine where this point is. Similarly for a
18941 breakpoint set on exit from a function. The prologue and epilogue
18942 attributes allow a compiler to communicate the location(s) to use. */
18945 if (fde->dw_fde_vms_end_prologue)
18946 add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18947 fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18949 if (fde->dw_fde_vms_begin_epilogue)
18950 add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18951 fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18953 #endif
18956 else
18958 /* Generate pubnames entries for the split function code ranges. */
18959 dw_fde_ref fde = fun->fde;
18961 if (fde->dw_fde_second_begin)
18963 if (dwarf_version >= 3 || !dwarf_strict)
18965 /* We should use ranges for non-contiguous code section
18966 addresses. Use the actual code range for the initial
18967 section, since the HOT/COLD labels might precede an
18968 alignment offset. */
18969 bool range_list_added = false;
18970 add_ranges_by_labels (subr_die, fde->dw_fde_begin,
18971 fde->dw_fde_end, &range_list_added,
18972 false);
18973 add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
18974 fde->dw_fde_second_end,
18975 &range_list_added, false);
18976 if (range_list_added)
18977 add_ranges (NULL);
18979 else
18981 /* There is no real support in DW2 for this .. so we make
18982 a work-around. First, emit the pub name for the segment
18983 containing the function label. Then make and emit a
18984 simplified subprogram DIE for the second segment with the
18985 name pre-fixed by __hot/cold_sect_of_. We use the same
18986 linkage name for the second die so that gdb will find both
18987 sections when given "b foo". */
18988 const char *name = NULL;
18989 tree decl_name = DECL_NAME (decl);
18990 dw_die_ref seg_die;
18992 /* Do the 'primary' section. */
18993 add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
18994 fde->dw_fde_end, false);
18996 /* Build a minimal DIE for the secondary section. */
18997 seg_die = new_die (DW_TAG_subprogram,
18998 subr_die->die_parent, decl);
19000 if (TREE_PUBLIC (decl))
19001 add_AT_flag (seg_die, DW_AT_external, 1);
19003 if (decl_name != NULL
19004 && IDENTIFIER_POINTER (decl_name) != NULL)
19006 name = dwarf2_name (decl, 1);
19007 if (! DECL_ARTIFICIAL (decl))
19008 add_src_coords_attributes (seg_die, decl);
19010 add_linkage_name (seg_die, decl);
19012 gcc_assert (name != NULL);
19013 add_pure_or_virtual_attribute (seg_die, decl);
19014 if (DECL_ARTIFICIAL (decl))
19015 add_AT_flag (seg_die, DW_AT_artificial, 1);
19017 name = concat ("__second_sect_of_", name, NULL);
19018 add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
19019 fde->dw_fde_second_end, false);
19020 add_name_attribute (seg_die, name);
19021 if (want_pubnames ())
19022 add_pubname_string (name, seg_die);
19025 else
19026 add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
19027 false);
19030 cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19032 /* We define the "frame base" as the function's CFA. This is more
19033 convenient for several reasons: (1) It's stable across the prologue
19034 and epilogue, which makes it better than just a frame pointer,
19035 (2) With dwarf3, there exists a one-byte encoding that allows us
19036 to reference the .debug_frame data by proxy, but failing that,
19037 (3) We can at least reuse the code inspection and interpretation
19038 code that determines the CFA position at various points in the
19039 function. */
19040 if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
19042 dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19043 add_AT_loc (subr_die, DW_AT_frame_base, op);
19045 else
19047 dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19048 if (list->dw_loc_next)
19049 add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19050 else
19051 add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19054 /* Compute a displacement from the "steady-state frame pointer" to
19055 the CFA. The former is what all stack slots and argument slots
19056 will reference in the rtl; the latter is what we've told the
19057 debugger about. We'll need to adjust all frame_base references
19058 by this displacement. */
19059 compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19061 if (fun->static_chain_decl)
19062 add_AT_location_description
19063 (subr_die, DW_AT_static_link,
19064 loc_list_from_tree (fun->static_chain_decl, 2, NULL));
19067 /* Generate child dies for template paramaters. */
19068 if (early_dwarf && debug_info_level > DINFO_LEVEL_TERSE)
19069 gen_generic_params_dies (decl);
19071 /* Now output descriptions of the arguments for this function. This gets
19072 (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19073 for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19074 `...' at the end of the formal parameter list. In order to find out if
19075 there was a trailing ellipsis or not, we must instead look at the type
19076 associated with the FUNCTION_DECL. This will be a node of type
19077 FUNCTION_TYPE. If the chain of type nodes hanging off of this
19078 FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19079 an ellipsis at the end. */
19081 /* In the case where we are describing a mere function declaration, all we
19082 need to do here (and all we *can* do here) is to describe the *types* of
19083 its formal parameters. */
19084 if (debug_info_level <= DINFO_LEVEL_TERSE)
19086 else if (declaration)
19087 gen_formal_types_die (decl, subr_die);
19088 else
19090 /* Generate DIEs to represent all known formal parameters. */
19091 tree parm = DECL_ARGUMENTS (decl);
19092 tree generic_decl = early_dwarf
19093 ? lang_hooks.decls.get_generic_function_decl (decl) : NULL;
19094 tree generic_decl_parm = generic_decl
19095 ? DECL_ARGUMENTS (generic_decl)
19096 : NULL;
19098 /* Now we want to walk the list of parameters of the function and
19099 emit their relevant DIEs.
19101 We consider the case of DECL being an instance of a generic function
19102 as well as it being a normal function.
19104 If DECL is an instance of a generic function we walk the
19105 parameters of the generic function declaration _and_ the parameters of
19106 DECL itself. This is useful because we want to emit specific DIEs for
19107 function parameter packs and those are declared as part of the
19108 generic function declaration. In that particular case,
19109 the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19110 That DIE has children DIEs representing the set of arguments
19111 of the pack. Note that the set of pack arguments can be empty.
19112 In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19113 children DIE.
19115 Otherwise, we just consider the parameters of DECL. */
19116 while (generic_decl_parm || parm)
19118 if (generic_decl_parm
19119 && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19120 gen_formal_parameter_pack_die (generic_decl_parm,
19121 parm, subr_die,
19122 &parm);
19123 else if (parm && !POINTER_BOUNDS_P (parm))
19125 dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19127 if (parm == DECL_ARGUMENTS (decl)
19128 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19129 && parm_die
19130 && (dwarf_version >= 3 || !dwarf_strict))
19131 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19133 parm = DECL_CHAIN (parm);
19135 else if (parm)
19136 parm = DECL_CHAIN (parm);
19138 if (generic_decl_parm)
19139 generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19142 /* Decide whether we need an unspecified_parameters DIE at the end.
19143 There are 2 more cases to do this for: 1) the ansi ... declaration -
19144 this is detectable when the end of the arg list is not a
19145 void_type_node 2) an unprototyped function declaration (not a
19146 definition). This just means that we have no info about the
19147 parameters at all. */
19148 if (prototype_p (TREE_TYPE (decl)))
19150 /* This is the prototyped case, check for.... */
19151 if (stdarg_p (TREE_TYPE (decl)))
19152 gen_unspecified_parameters_die (decl, subr_die);
19154 else if (DECL_INITIAL (decl) == NULL_TREE)
19155 gen_unspecified_parameters_die (decl, subr_die);
19158 if (subr_die != old_die)
19159 /* Add the calling convention attribute if requested. */
19160 add_calling_convention_attribute (subr_die, decl);
19162 /* Output Dwarf info for all of the stuff within the body of the function
19163 (if it has one - it may be just a declaration).
19165 OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19166 a function. This BLOCK actually represents the outermost binding contour
19167 for the function, i.e. the contour in which the function's formal
19168 parameters and labels get declared. Curiously, it appears that the front
19169 end doesn't actually put the PARM_DECL nodes for the current function onto
19170 the BLOCK_VARS list for this outer scope, but are strung off of the
19171 DECL_ARGUMENTS list for the function instead.
19173 The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19174 the LABEL_DECL nodes for the function however, and we output DWARF info
19175 for those in decls_for_scope. Just within the `outer_scope' there will be
19176 a BLOCK node representing the function's outermost pair of curly braces,
19177 and any blocks used for the base and member initializers of a C++
19178 constructor function. */
19179 tree outer_scope = DECL_INITIAL (decl);
19180 if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
19182 int call_site_note_count = 0;
19183 int tail_call_site_note_count = 0;
19185 /* Emit a DW_TAG_variable DIE for a named return value. */
19186 if (DECL_NAME (DECL_RESULT (decl)))
19187 gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19189 /* The first time through decls_for_scope we will generate the
19190 DIEs for the locals. The second time, we fill in the
19191 location info. */
19192 decls_for_scope (outer_scope, subr_die);
19194 if (call_arg_locations && !dwarf_strict)
19196 struct call_arg_loc_node *ca_loc;
19197 for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19199 dw_die_ref die = NULL;
19200 rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19201 rtx arg, next_arg;
19203 for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19204 arg; arg = next_arg)
19206 dw_loc_descr_ref reg, val;
19207 machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19208 dw_die_ref cdie, tdie = NULL;
19210 next_arg = XEXP (arg, 1);
19211 if (REG_P (XEXP (XEXP (arg, 0), 0))
19212 && next_arg
19213 && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19214 && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19215 && REGNO (XEXP (XEXP (arg, 0), 0))
19216 == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19217 next_arg = XEXP (next_arg, 1);
19218 if (mode == VOIDmode)
19220 mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19221 if (mode == VOIDmode)
19222 mode = GET_MODE (XEXP (arg, 0));
19224 if (mode == VOIDmode || mode == BLKmode)
19225 continue;
19226 if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19228 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19229 tloc = XEXP (XEXP (arg, 0), 1);
19230 continue;
19232 else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19233 && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19235 gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19236 tlocc = XEXP (XEXP (arg, 0), 1);
19237 continue;
19239 reg = NULL;
19240 if (REG_P (XEXP (XEXP (arg, 0), 0)))
19241 reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19242 VAR_INIT_STATUS_INITIALIZED);
19243 else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19245 rtx mem = XEXP (XEXP (arg, 0), 0);
19246 reg = mem_loc_descriptor (XEXP (mem, 0),
19247 get_address_mode (mem),
19248 GET_MODE (mem),
19249 VAR_INIT_STATUS_INITIALIZED);
19251 else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
19252 == DEBUG_PARAMETER_REF)
19254 tree tdecl
19255 = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
19256 tdie = lookup_decl_die (tdecl);
19257 if (tdie == NULL)
19258 continue;
19260 else
19261 continue;
19262 if (reg == NULL
19263 && GET_CODE (XEXP (XEXP (arg, 0), 0))
19264 != DEBUG_PARAMETER_REF)
19265 continue;
19266 val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
19267 VOIDmode,
19268 VAR_INIT_STATUS_INITIALIZED);
19269 if (val == NULL)
19270 continue;
19271 if (die == NULL)
19272 die = gen_call_site_die (decl, subr_die, ca_loc);
19273 cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19274 NULL_TREE);
19275 if (reg != NULL)
19276 add_AT_loc (cdie, DW_AT_location, reg);
19277 else if (tdie != NULL)
19278 add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
19279 add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19280 if (next_arg != XEXP (arg, 1))
19282 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
19283 if (mode == VOIDmode)
19284 mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
19285 val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19286 0), 1),
19287 mode, VOIDmode,
19288 VAR_INIT_STATUS_INITIALIZED);
19289 if (val != NULL)
19290 add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19293 if (die == NULL
19294 && (ca_loc->symbol_ref || tloc))
19295 die = gen_call_site_die (decl, subr_die, ca_loc);
19296 if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19298 dw_loc_descr_ref tval = NULL;
19300 if (tloc != NULL_RTX)
19301 tval = mem_loc_descriptor (tloc,
19302 GET_MODE (tloc) == VOIDmode
19303 ? Pmode : GET_MODE (tloc),
19304 VOIDmode,
19305 VAR_INIT_STATUS_INITIALIZED);
19306 if (tval)
19307 add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19308 else if (tlocc != NULL_RTX)
19310 tval = mem_loc_descriptor (tlocc,
19311 GET_MODE (tlocc) == VOIDmode
19312 ? Pmode : GET_MODE (tlocc),
19313 VOIDmode,
19314 VAR_INIT_STATUS_INITIALIZED);
19315 if (tval)
19316 add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19317 tval);
19320 if (die != NULL)
19322 call_site_note_count++;
19323 if (ca_loc->tail_call_p)
19324 tail_call_site_note_count++;
19328 call_arg_locations = NULL;
19329 call_arg_loc_last = NULL;
19330 if (tail_call_site_count >= 0
19331 && tail_call_site_count == tail_call_site_note_count
19332 && !dwarf_strict)
19334 if (call_site_count >= 0
19335 && call_site_count == call_site_note_count)
19336 add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19337 else
19338 add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19340 call_site_count = -1;
19341 tail_call_site_count = -1;
19345 /* Returns a hash value for X (which really is a die_struct). */
19347 hashval_t
19348 block_die_hasher::hash (die_struct *d)
19350 return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19353 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19354 as decl_id and die_parent of die_struct Y. */
19356 bool
19357 block_die_hasher::equal (die_struct *x, die_struct *y)
19359 return x->decl_id == y->decl_id && x->die_parent == y->die_parent;
19362 /* Return TRUE if DECL, which may have been previously generated as
19363 OLD_DIE, is a candidate for a DW_AT_specification. DECLARATION is
19364 true if decl (or its origin) is either an extern declaration or a
19365 class/namespace scoped declaration.
19367 The declare_in_namespace support causes us to get two DIEs for one
19368 variable, both of which are declarations. We want to avoid
19369 considering one to be a specification, so we must test for
19370 DECLARATION and DW_AT_declaration. */
19371 static inline bool
19372 decl_will_get_specification_p (dw_die_ref old_die, tree decl, bool declaration)
19374 return (old_die && TREE_STATIC (decl) && !declaration
19375 && get_AT_flag (old_die, DW_AT_declaration) == 1);
19378 /* Return true if DECL is a local static. */
19380 static inline bool
19381 local_function_static (tree decl)
19383 gcc_assert (TREE_CODE (decl) == VAR_DECL);
19384 return TREE_STATIC (decl)
19385 && DECL_CONTEXT (decl)
19386 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL;
19389 /* Generate a DIE to represent a declared data object.
19390 Either DECL or ORIGIN must be non-null. */
19392 static void
19393 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19395 HOST_WIDE_INT off = 0;
19396 tree com_decl;
19397 tree decl_or_origin = decl ? decl : origin;
19398 tree ultimate_origin;
19399 dw_die_ref var_die;
19400 dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19401 dw_die_ref origin_die = NULL;
19402 bool declaration = (DECL_EXTERNAL (decl_or_origin)
19403 || class_or_namespace_scope_p (context_die));
19404 bool specialization_p = false;
19406 ultimate_origin = decl_ultimate_origin (decl_or_origin);
19407 if (decl || ultimate_origin)
19408 origin = ultimate_origin;
19409 com_decl = fortran_common (decl_or_origin, &off);
19411 /* Symbol in common gets emitted as a child of the common block, in the form
19412 of a data member. */
19413 if (com_decl)
19415 dw_die_ref com_die;
19416 dw_loc_list_ref loc;
19417 die_node com_die_arg;
19419 var_die = lookup_decl_die (decl_or_origin);
19420 if (var_die)
19422 if (get_AT (var_die, DW_AT_location) == NULL)
19424 loc = loc_list_from_tree (com_decl, off ? 1 : 2, NULL);
19425 if (loc)
19427 if (off)
19429 /* Optimize the common case. */
19430 if (single_element_loc_list_p (loc)
19431 && loc->expr->dw_loc_opc == DW_OP_addr
19432 && loc->expr->dw_loc_next == NULL
19433 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19434 == SYMBOL_REF)
19436 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19437 loc->expr->dw_loc_oprnd1.v.val_addr
19438 = plus_constant (GET_MODE (x), x , off);
19440 else
19441 loc_list_plus_const (loc, off);
19443 add_AT_location_description (var_die, DW_AT_location, loc);
19444 remove_AT (var_die, DW_AT_declaration);
19447 return;
19450 if (common_block_die_table == NULL)
19451 common_block_die_table = hash_table<block_die_hasher>::create_ggc (10);
19453 com_die_arg.decl_id = DECL_UID (com_decl);
19454 com_die_arg.die_parent = context_die;
19455 com_die = common_block_die_table->find (&com_die_arg);
19456 loc = loc_list_from_tree (com_decl, 2, NULL);
19457 if (com_die == NULL)
19459 const char *cnam
19460 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19461 die_node **slot;
19463 com_die = new_die (DW_TAG_common_block, context_die, decl);
19464 add_name_and_src_coords_attributes (com_die, com_decl);
19465 if (loc)
19467 add_AT_location_description (com_die, DW_AT_location, loc);
19468 /* Avoid sharing the same loc descriptor between
19469 DW_TAG_common_block and DW_TAG_variable. */
19470 loc = loc_list_from_tree (com_decl, 2, NULL);
19472 else if (DECL_EXTERNAL (decl))
19473 add_AT_flag (com_die, DW_AT_declaration, 1);
19474 if (want_pubnames ())
19475 add_pubname_string (cnam, com_die); /* ??? needed? */
19476 com_die->decl_id = DECL_UID (com_decl);
19477 slot = common_block_die_table->find_slot (com_die, INSERT);
19478 *slot = com_die;
19480 else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19482 add_AT_location_description (com_die, DW_AT_location, loc);
19483 loc = loc_list_from_tree (com_decl, 2, NULL);
19484 remove_AT (com_die, DW_AT_declaration);
19486 var_die = new_die (DW_TAG_variable, com_die, decl);
19487 add_name_and_src_coords_attributes (var_die, decl);
19488 add_type_attribute (var_die, TREE_TYPE (decl), decl_quals (decl),
19489 context_die);
19490 add_AT_flag (var_die, DW_AT_external, 1);
19491 if (loc)
19493 if (off)
19495 /* Optimize the common case. */
19496 if (single_element_loc_list_p (loc)
19497 && loc->expr->dw_loc_opc == DW_OP_addr
19498 && loc->expr->dw_loc_next == NULL
19499 && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19501 rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
19502 loc->expr->dw_loc_oprnd1.v.val_addr
19503 = plus_constant (GET_MODE (x), x, off);
19505 else
19506 loc_list_plus_const (loc, off);
19508 add_AT_location_description (var_die, DW_AT_location, loc);
19510 else if (DECL_EXTERNAL (decl))
19511 add_AT_flag (var_die, DW_AT_declaration, 1);
19512 equate_decl_number_to_die (decl, var_die);
19513 return;
19516 if (old_die)
19518 if (declaration)
19520 /* A declaration that has been previously dumped, needs no
19521 further annotations, since it doesn't need location on
19522 the second pass. */
19523 return;
19525 else if (decl_will_get_specification_p (old_die, decl, declaration)
19526 && !get_AT (old_die, DW_AT_specification))
19528 /* Fall-thru so we can make a new variable die along with a
19529 DW_AT_specification. */
19531 else if (origin && old_die->die_parent != context_die)
19533 /* If we will be creating an inlined instance, we need a
19534 new DIE that will get annotated with
19535 DW_AT_abstract_origin. Clear things so we can get a
19536 new DIE. */
19537 gcc_assert (!DECL_ABSTRACT_P (decl));
19538 old_die = NULL;
19540 else
19542 /* If a DIE was dumped early, it still needs location info.
19543 Skip to where we fill the location bits. */
19544 var_die = old_die;
19545 goto gen_variable_die_location;
19549 /* For static data members, the declaration in the class is supposed
19550 to have DW_TAG_member tag; the specification should still be
19551 DW_TAG_variable referencing the DW_TAG_member DIE. */
19552 if (declaration && class_scope_p (context_die))
19553 var_die = new_die (DW_TAG_member, context_die, decl);
19554 else
19555 var_die = new_die (DW_TAG_variable, context_die, decl);
19557 if (origin != NULL)
19558 origin_die = add_abstract_origin_attribute (var_die, origin);
19560 /* Loop unrolling can create multiple blocks that refer to the same
19561 static variable, so we must test for the DW_AT_declaration flag.
19563 ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19564 copy decls and set the DECL_ABSTRACT_P flag on them instead of
19565 sharing them.
19567 ??? Duplicated blocks have been rewritten to use .debug_ranges. */
19568 else if (decl_will_get_specification_p (old_die, decl, declaration))
19570 /* This is a definition of a C++ class level static. */
19571 add_AT_specification (var_die, old_die);
19572 specialization_p = true;
19573 if (DECL_NAME (decl))
19575 expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19576 struct dwarf_file_data * file_index = lookup_filename (s.file);
19578 if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19579 add_AT_file (var_die, DW_AT_decl_file, file_index);
19581 if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19582 add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19584 if (old_die->die_tag == DW_TAG_member)
19585 add_linkage_name (var_die, decl);
19588 else
19589 add_name_and_src_coords_attributes (var_die, decl);
19591 if ((origin == NULL && !specialization_p)
19592 || (origin != NULL
19593 && !DECL_ABSTRACT_P (decl_or_origin)
19594 && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19595 decl_function_context
19596 (decl_or_origin))))
19598 tree type = TREE_TYPE (decl_or_origin);
19600 if (decl_by_reference_p (decl_or_origin))
19601 add_type_attribute (var_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19602 context_die);
19603 else
19604 add_type_attribute (var_die, type, decl_quals (decl_or_origin),
19605 context_die);
19608 if (origin == NULL && !specialization_p)
19610 if (TREE_PUBLIC (decl))
19611 add_AT_flag (var_die, DW_AT_external, 1);
19613 if (DECL_ARTIFICIAL (decl))
19614 add_AT_flag (var_die, DW_AT_artificial, 1);
19616 add_accessibility_attribute (var_die, decl);
19619 if (declaration)
19620 add_AT_flag (var_die, DW_AT_declaration, 1);
19622 if (decl && (DECL_ABSTRACT_P (decl)
19623 || !old_die || is_declaration_die (old_die)))
19624 equate_decl_number_to_die (decl, var_die);
19626 gen_variable_die_location:
19627 if (! declaration
19628 && (! DECL_ABSTRACT_P (decl_or_origin)
19629 /* Local static vars are shared between all clones/inlines,
19630 so emit DW_AT_location on the abstract DIE if DECL_RTL is
19631 already set. */
19632 || (TREE_CODE (decl_or_origin) == VAR_DECL
19633 && TREE_STATIC (decl_or_origin)
19634 && DECL_RTL_SET_P (decl_or_origin)))
19635 /* When abstract origin already has DW_AT_location attribute, no need
19636 to add it again. */
19637 && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19639 if (early_dwarf)
19640 add_pubname (decl_or_origin, var_die);
19641 else
19642 add_location_or_const_value_attribute (var_die, decl_or_origin,
19643 decl == NULL, DW_AT_location);
19645 else
19646 tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19649 /* Generate a DIE to represent a named constant. */
19651 static void
19652 gen_const_die (tree decl, dw_die_ref context_die)
19654 dw_die_ref const_die;
19655 tree type = TREE_TYPE (decl);
19657 const_die = lookup_decl_die (decl);
19658 if (const_die)
19659 return;
19661 const_die = new_die (DW_TAG_constant, context_die, decl);
19662 equate_decl_number_to_die (decl, const_die);
19663 add_name_and_src_coords_attributes (const_die, decl);
19664 add_type_attribute (const_die, type, TYPE_QUAL_CONST, context_die);
19665 if (TREE_PUBLIC (decl))
19666 add_AT_flag (const_die, DW_AT_external, 1);
19667 if (DECL_ARTIFICIAL (decl))
19668 add_AT_flag (const_die, DW_AT_artificial, 1);
19669 tree_add_const_value_attribute_for_decl (const_die, decl);
19672 /* Generate a DIE to represent a label identifier. */
19674 static void
19675 gen_label_die (tree decl, dw_die_ref context_die)
19677 tree origin = decl_ultimate_origin (decl);
19678 dw_die_ref lbl_die = lookup_decl_die (decl);
19679 rtx insn;
19680 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19682 if (!lbl_die)
19684 lbl_die = new_die (DW_TAG_label, context_die, decl);
19685 equate_decl_number_to_die (decl, lbl_die);
19687 if (origin != NULL)
19688 add_abstract_origin_attribute (lbl_die, origin);
19689 else
19690 add_name_and_src_coords_attributes (lbl_die, decl);
19693 if (DECL_ABSTRACT_P (decl))
19694 equate_decl_number_to_die (decl, lbl_die);
19695 else
19697 insn = DECL_RTL_IF_SET (decl);
19699 /* Deleted labels are programmer specified labels which have been
19700 eliminated because of various optimizations. We still emit them
19701 here so that it is possible to put breakpoints on them. */
19702 if (insn
19703 && (LABEL_P (insn)
19704 || ((NOTE_P (insn)
19705 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19707 /* When optimization is enabled (via -O) some parts of the compiler
19708 (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19709 represent source-level labels which were explicitly declared by
19710 the user. This really shouldn't be happening though, so catch
19711 it if it ever does happen. */
19712 gcc_assert (!as_a<rtx_insn *> (insn)->deleted ());
19714 ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19715 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19717 else if (insn
19718 && NOTE_P (insn)
19719 && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
19720 && CODE_LABEL_NUMBER (insn) != -1)
19722 ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
19723 add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19728 /* A helper function for gen_inlined_subroutine_die. Add source coordinate
19729 attributes to the DIE for a block STMT, to describe where the inlined
19730 function was called from. This is similar to add_src_coords_attributes. */
19732 static inline void
19733 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19735 expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19737 if (dwarf_version >= 3 || !dwarf_strict)
19739 add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19740 add_AT_unsigned (die, DW_AT_call_line, s.line);
19745 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19746 Add low_pc and high_pc attributes to the DIE for a block STMT. */
19748 static inline void
19749 add_high_low_attributes (tree stmt, dw_die_ref die)
19751 char label[MAX_ARTIFICIAL_LABEL_BYTES];
19753 if (BLOCK_FRAGMENT_CHAIN (stmt)
19754 && (dwarf_version >= 3 || !dwarf_strict))
19756 tree chain, superblock = NULL_TREE;
19757 dw_die_ref pdie;
19758 dw_attr_ref attr = NULL;
19760 if (inlined_function_outer_scope_p (stmt))
19762 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19763 BLOCK_NUMBER (stmt));
19764 add_AT_lbl_id (die, DW_AT_entry_pc, label);
19767 /* Optimize duplicate .debug_ranges lists or even tails of
19768 lists. If this BLOCK has same ranges as its supercontext,
19769 lookup DW_AT_ranges attribute in the supercontext (and
19770 recursively so), verify that the ranges_table contains the
19771 right values and use it instead of adding a new .debug_range. */
19772 for (chain = stmt, pdie = die;
19773 BLOCK_SAME_RANGE (chain);
19774 chain = BLOCK_SUPERCONTEXT (chain))
19776 dw_attr_ref new_attr;
19778 pdie = pdie->die_parent;
19779 if (pdie == NULL)
19780 break;
19781 if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
19782 break;
19783 new_attr = get_AT (pdie, DW_AT_ranges);
19784 if (new_attr == NULL
19785 || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
19786 break;
19787 attr = new_attr;
19788 superblock = BLOCK_SUPERCONTEXT (chain);
19790 if (attr != NULL
19791 && (ranges_table[attr->dw_attr_val.v.val_offset
19792 / 2 / DWARF2_ADDR_SIZE].num
19793 == BLOCK_NUMBER (superblock))
19794 && BLOCK_FRAGMENT_CHAIN (superblock))
19796 unsigned long off = attr->dw_attr_val.v.val_offset
19797 / 2 / DWARF2_ADDR_SIZE;
19798 unsigned long supercnt = 0, thiscnt = 0;
19799 for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
19800 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19802 ++supercnt;
19803 gcc_checking_assert (ranges_table[off + supercnt].num
19804 == BLOCK_NUMBER (chain));
19806 gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
19807 for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
19808 chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
19809 ++thiscnt;
19810 gcc_assert (supercnt >= thiscnt);
19811 add_AT_range_list (die, DW_AT_ranges,
19812 ((off + supercnt - thiscnt)
19813 * 2 * DWARF2_ADDR_SIZE),
19814 false);
19815 return;
19818 add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
19820 chain = BLOCK_FRAGMENT_CHAIN (stmt);
19823 add_ranges (chain);
19824 chain = BLOCK_FRAGMENT_CHAIN (chain);
19826 while (chain);
19827 add_ranges (NULL);
19829 else
19831 char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
19832 ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19833 BLOCK_NUMBER (stmt));
19834 ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
19835 BLOCK_NUMBER (stmt));
19836 add_AT_low_high_pc (die, label, label_high, false);
19840 /* Generate a DIE for a lexical block. */
19842 static void
19843 gen_lexical_block_die (tree stmt, dw_die_ref context_die)
19845 dw_die_ref old_die = BLOCK_DIE (stmt);
19846 dw_die_ref stmt_die = NULL;
19847 if (!old_die)
19849 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19850 BLOCK_DIE (stmt) = stmt_die;
19853 if (BLOCK_ABSTRACT (stmt))
19855 if (old_die)
19857 #ifdef ENABLE_CHECKING
19858 /* This must have been generated early and it won't even
19859 need location information since it's a DW_AT_inline
19860 function. */
19861 for (dw_die_ref c = context_die; c; c = c->die_parent)
19862 if (c->die_tag == DW_TAG_inlined_subroutine
19863 || c->die_tag == DW_TAG_subprogram)
19865 gcc_assert (get_AT (c, DW_AT_inline));
19866 break;
19868 #endif
19869 return;
19872 else if (BLOCK_ABSTRACT_ORIGIN (stmt))
19874 /* If this is an inlined instance, create a new lexical die for
19875 anything below to attach DW_AT_abstract_origin to. */
19876 if (old_die)
19878 stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19879 BLOCK_DIE (stmt) = stmt_die;
19880 old_die = NULL;
19884 if (old_die)
19885 stmt_die = old_die;
19887 /* A non abstract block whose blocks have already been reordered
19888 should have the instruction range for this block. If so, set the
19889 high/low attributes. */
19890 if (!early_dwarf && !BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19892 gcc_assert (stmt_die);
19893 add_high_low_attributes (stmt, stmt_die);
19896 decls_for_scope (stmt, stmt_die);
19899 /* Generate a DIE for an inlined subprogram. */
19901 static void
19902 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die)
19904 tree decl;
19906 /* The instance of function that is effectively being inlined shall not
19907 be abstract. */
19908 gcc_assert (! BLOCK_ABSTRACT (stmt));
19910 decl = block_ultimate_origin (stmt);
19912 /* Emit info for the abstract instance first, if we haven't yet. We
19913 must emit this even if the block is abstract, otherwise when we
19914 emit the block below (or elsewhere), we may end up trying to emit
19915 a die whose origin die hasn't been emitted, and crashing. */
19916 dwarf2out_abstract_function (decl);
19918 if (! BLOCK_ABSTRACT (stmt))
19920 dw_die_ref subr_die
19921 = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19923 if (call_arg_locations)
19924 BLOCK_DIE (stmt) = subr_die;
19925 add_abstract_origin_attribute (subr_die, decl);
19926 if (TREE_ASM_WRITTEN (stmt))
19927 add_high_low_attributes (stmt, subr_die);
19928 add_call_src_coords_attributes (stmt, subr_die);
19930 decls_for_scope (stmt, subr_die);
19934 /* Generate a DIE for a field in a record, or structure. */
19936 static void
19937 gen_field_die (tree decl, dw_die_ref context_die)
19939 dw_die_ref decl_die;
19941 if (TREE_TYPE (decl) == error_mark_node)
19942 return;
19944 decl_die = new_die (DW_TAG_member, context_die, decl);
19945 add_name_and_src_coords_attributes (decl_die, decl);
19946 add_type_attribute (decl_die, member_declared_type (decl),
19947 decl_quals (decl), context_die);
19949 if (DECL_BIT_FIELD_TYPE (decl))
19951 add_byte_size_attribute (decl_die, decl);
19952 add_bit_size_attribute (decl_die, decl);
19953 add_bit_offset_attribute (decl_die, decl);
19956 if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19957 add_data_member_location_attribute (decl_die, decl);
19959 if (DECL_ARTIFICIAL (decl))
19960 add_AT_flag (decl_die, DW_AT_artificial, 1);
19962 add_accessibility_attribute (decl_die, decl);
19964 /* Equate decl number to die, so that we can look up this decl later on. */
19965 equate_decl_number_to_die (decl, decl_die);
19968 #if 0
19969 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19970 Use modified_type_die instead.
19971 We keep this code here just in case these types of DIEs may be needed to
19972 represent certain things in other languages (e.g. Pascal) someday. */
19974 static void
19975 gen_pointer_type_die (tree type, dw_die_ref context_die)
19977 dw_die_ref ptr_die
19978 = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19980 equate_type_number_to_die (type, ptr_die);
19981 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
19982 context_die);
19983 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19986 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19987 Use modified_type_die instead.
19988 We keep this code here just in case these types of DIEs may be needed to
19989 represent certain things in other languages (e.g. Pascal) someday. */
19991 static void
19992 gen_reference_type_die (tree type, dw_die_ref context_die)
19994 dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19996 if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19997 ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19998 else
19999 ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20001 equate_type_number_to_die (type, ref_die);
20002 add_type_attribute (ref_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20003 context_die);
20004 add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20006 #endif
20008 /* Generate a DIE for a pointer to a member type. */
20010 static void
20011 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20013 dw_die_ref ptr_die
20014 = new_die (DW_TAG_ptr_to_member_type,
20015 scope_die_for (type, context_die), type);
20017 equate_type_number_to_die (type, ptr_die);
20018 add_AT_die_ref (ptr_die, DW_AT_containing_type,
20019 lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20020 add_type_attribute (ptr_die, TREE_TYPE (type), TYPE_UNQUALIFIED,
20021 context_die);
20024 typedef const char *dchar_p; /* For DEF_VEC_P. */
20026 static char *producer_string;
20028 /* Return a heap allocated producer string including command line options
20029 if -grecord-gcc-switches. */
20031 static char *
20032 gen_producer_string (void)
20034 size_t j;
20035 auto_vec<dchar_p> switches;
20036 const char *language_string = lang_hooks.name;
20037 char *producer, *tail;
20038 const char *p;
20039 size_t len = dwarf_record_gcc_switches ? 0 : 3;
20040 size_t plen = strlen (language_string) + 1 + strlen (version_string);
20042 for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
20043 switch (save_decoded_options[j].opt_index)
20045 case OPT_o:
20046 case OPT_d:
20047 case OPT_dumpbase:
20048 case OPT_dumpdir:
20049 case OPT_auxbase:
20050 case OPT_auxbase_strip:
20051 case OPT_quiet:
20052 case OPT_version:
20053 case OPT_v:
20054 case OPT_w:
20055 case OPT_L:
20056 case OPT_D:
20057 case OPT_I:
20058 case OPT_U:
20059 case OPT_SPECIAL_unknown:
20060 case OPT_SPECIAL_ignore:
20061 case OPT_SPECIAL_program_name:
20062 case OPT_SPECIAL_input_file:
20063 case OPT_grecord_gcc_switches:
20064 case OPT_gno_record_gcc_switches:
20065 case OPT__output_pch_:
20066 case OPT_fdiagnostics_show_location_:
20067 case OPT_fdiagnostics_show_option:
20068 case OPT_fdiagnostics_show_caret:
20069 case OPT_fdiagnostics_color_:
20070 case OPT_fverbose_asm:
20071 case OPT____:
20072 case OPT__sysroot_:
20073 case OPT_nostdinc:
20074 case OPT_nostdinc__:
20075 case OPT_fpreprocessed:
20076 case OPT_fltrans_output_list_:
20077 case OPT_fresolution_:
20078 /* Ignore these. */
20079 continue;
20080 default:
20081 if (cl_options[save_decoded_options[j].opt_index].flags
20082 & CL_NO_DWARF_RECORD)
20083 continue;
20084 gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
20085 == '-');
20086 switch (save_decoded_options[j].canonical_option[0][1])
20088 case 'M':
20089 case 'i':
20090 case 'W':
20091 continue;
20092 case 'f':
20093 if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
20094 "dump", 4) == 0)
20095 continue;
20096 break;
20097 default:
20098 break;
20100 switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
20101 len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
20102 break;
20105 producer = XNEWVEC (char, plen + 1 + len + 1);
20106 tail = producer;
20107 sprintf (tail, "%s %s", language_string, version_string);
20108 tail += plen;
20110 FOR_EACH_VEC_ELT (switches, j, p)
20112 len = strlen (p);
20113 *tail = ' ';
20114 memcpy (tail + 1, p, len);
20115 tail += len + 1;
20118 *tail = '\0';
20119 return producer;
20122 /* Given a C and/or C++ language/version string return the "highest".
20123 C++ is assumed to be "higher" than C in this case. Used for merging
20124 LTO translation unit languages. */
20125 static const char *
20126 highest_c_language (const char *lang1, const char *lang2)
20128 if (strcmp ("GNU C++14", lang1) == 0 || strcmp ("GNU C++14", lang2) == 0)
20129 return "GNU C++14";
20130 if (strcmp ("GNU C++11", lang1) == 0 || strcmp ("GNU C++11", lang2) == 0)
20131 return "GNU C++11";
20132 if (strcmp ("GNU C++98", lang1) == 0 || strcmp ("GNU C++98", lang2) == 0)
20133 return "GNU C++98";
20135 if (strcmp ("GNU C11", lang1) == 0 || strcmp ("GNU C11", lang2) == 0)
20136 return "GNU C11";
20137 if (strcmp ("GNU C99", lang1) == 0 || strcmp ("GNU C99", lang2) == 0)
20138 return "GNU C99";
20139 if (strcmp ("GNU C89", lang1) == 0 || strcmp ("GNU C89", lang2) == 0)
20140 return "GNU C89";
20142 gcc_unreachable ();
20146 /* Generate the DIE for the compilation unit. */
20148 static dw_die_ref
20149 gen_compile_unit_die (const char *filename)
20151 dw_die_ref die;
20152 const char *language_string = lang_hooks.name;
20153 int language;
20155 die = new_die (DW_TAG_compile_unit, NULL, NULL);
20157 if (filename)
20159 add_name_attribute (die, filename);
20160 /* Don't add cwd for <built-in>. */
20161 if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20162 add_comp_dir_attribute (die);
20165 add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
20167 /* If our producer is LTO try to figure out a common language to use
20168 from the global list of translation units. */
20169 if (strcmp (language_string, "GNU GIMPLE") == 0)
20171 unsigned i;
20172 tree t;
20173 const char *common_lang = NULL;
20175 FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
20177 if (!TRANSLATION_UNIT_LANGUAGE (t))
20178 continue;
20179 if (!common_lang)
20180 common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20181 else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20183 else if (strncmp (common_lang, "GNU C", 5) == 0
20184 && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20185 /* Mixing C and C++ is ok, use C++ in that case. */
20186 common_lang = highest_c_language (common_lang,
20187 TRANSLATION_UNIT_LANGUAGE (t));
20188 else
20190 /* Fall back to C. */
20191 common_lang = NULL;
20192 break;
20196 if (common_lang)
20197 language_string = common_lang;
20200 language = DW_LANG_C;
20201 if (strncmp (language_string, "GNU C", 5) == 0
20202 && ISDIGIT (language_string[5]))
20204 language = DW_LANG_C89;
20205 if (dwarf_version >= 3 || !dwarf_strict)
20207 if (strcmp (language_string, "GNU C89") != 0)
20208 language = DW_LANG_C99;
20210 if (dwarf_version >= 5 /* || !dwarf_strict */)
20211 if (strcmp (language_string, "GNU C11") == 0)
20212 language = DW_LANG_C11;
20215 else if (strncmp (language_string, "GNU C++", 7) == 0)
20217 language = DW_LANG_C_plus_plus;
20218 if (dwarf_version >= 5 /* || !dwarf_strict */)
20220 if (strcmp (language_string, "GNU C++11") == 0)
20221 language = DW_LANG_C_plus_plus_11;
20222 else if (strcmp (language_string, "GNU C++14") == 0)
20223 language = DW_LANG_C_plus_plus_14;
20226 else if (strcmp (language_string, "GNU F77") == 0)
20227 language = DW_LANG_Fortran77;
20228 else if (strcmp (language_string, "GNU Pascal") == 0)
20229 language = DW_LANG_Pascal83;
20230 else if (dwarf_version >= 3 || !dwarf_strict)
20232 if (strcmp (language_string, "GNU Ada") == 0)
20233 language = DW_LANG_Ada95;
20234 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20236 language = DW_LANG_Fortran95;
20237 if (dwarf_version >= 5 /* || !dwarf_strict */)
20239 if (strcmp (language_string, "GNU Fortran2003") == 0)
20240 language = DW_LANG_Fortran03;
20241 else if (strcmp (language_string, "GNU Fortran2008") == 0)
20242 language = DW_LANG_Fortran08;
20245 else if (strcmp (language_string, "GNU Java") == 0)
20246 language = DW_LANG_Java;
20247 else if (strcmp (language_string, "GNU Objective-C") == 0)
20248 language = DW_LANG_ObjC;
20249 else if (strcmp (language_string, "GNU Objective-C++") == 0)
20250 language = DW_LANG_ObjC_plus_plus;
20251 else if (dwarf_version >= 5 || !dwarf_strict)
20253 if (strcmp (language_string, "GNU Go") == 0)
20254 language = DW_LANG_Go;
20257 /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works. */
20258 else if (strncmp (language_string, "GNU Fortran", 11) == 0)
20259 language = DW_LANG_Fortran90;
20261 add_AT_unsigned (die, DW_AT_language, language);
20263 switch (language)
20265 case DW_LANG_Fortran77:
20266 case DW_LANG_Fortran90:
20267 case DW_LANG_Fortran95:
20268 case DW_LANG_Fortran03:
20269 case DW_LANG_Fortran08:
20270 /* Fortran has case insensitive identifiers and the front-end
20271 lowercases everything. */
20272 add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20273 break;
20274 default:
20275 /* The default DW_ID_case_sensitive doesn't need to be specified. */
20276 break;
20278 return die;
20281 /* Generate the DIE for a base class. */
20283 static void
20284 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20286 dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20288 add_type_attribute (die, BINFO_TYPE (binfo), TYPE_UNQUALIFIED, context_die);
20289 add_data_member_location_attribute (die, binfo);
20291 if (BINFO_VIRTUAL_P (binfo))
20292 add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20294 /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20295 children, otherwise the default is DW_ACCESS_public. In DWARF2
20296 the default has always been DW_ACCESS_private. */
20297 if (access == access_public_node)
20299 if (dwarf_version == 2
20300 || context_die->die_tag == DW_TAG_class_type)
20301 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20303 else if (access == access_protected_node)
20304 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20305 else if (dwarf_version > 2
20306 && context_die->die_tag != DW_TAG_class_type)
20307 add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20310 /* Generate a DIE for a class member. */
20312 static void
20313 gen_member_die (tree type, dw_die_ref context_die)
20315 tree member;
20316 tree binfo = TYPE_BINFO (type);
20317 dw_die_ref child;
20319 /* If this is not an incomplete type, output descriptions of each of its
20320 members. Note that as we output the DIEs necessary to represent the
20321 members of this record or union type, we will also be trying to output
20322 DIEs to represent the *types* of those members. However the `type'
20323 function (above) will specifically avoid generating type DIEs for member
20324 types *within* the list of member DIEs for this (containing) type except
20325 for those types (of members) which are explicitly marked as also being
20326 members of this (containing) type themselves. The g++ front- end can
20327 force any given type to be treated as a member of some other (containing)
20328 type by setting the TYPE_CONTEXT of the given (member) type to point to
20329 the TREE node representing the appropriate (containing) type. */
20331 /* First output info about the base classes. */
20332 if (binfo)
20334 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
20335 int i;
20336 tree base;
20338 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20339 gen_inheritance_die (base,
20340 (accesses ? (*accesses)[i] : access_public_node),
20341 context_die);
20344 /* Now output info about the data members and type members. */
20345 for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20347 /* If we thought we were generating minimal debug info for TYPE
20348 and then changed our minds, some of the member declarations
20349 may have already been defined. Don't define them again, but
20350 do put them in the right order. */
20352 child = lookup_decl_die (member);
20353 if (child)
20354 splice_child_die (context_die, child);
20355 else
20356 gen_decl_die (member, NULL, context_die);
20359 /* We do not keep type methods in type variants. */
20360 gcc_assert (TYPE_MAIN_VARIANT (type) == type);
20361 /* Now output info about the function members (if any). */
20362 if (TYPE_METHODS (type) != error_mark_node)
20363 for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20365 /* Don't include clones in the member list. */
20366 if (DECL_ABSTRACT_ORIGIN (member))
20367 continue;
20368 /* Nor constructors for anonymous classes. */
20369 if (DECL_ARTIFICIAL (member)
20370 && dwarf2_name (member, 0) == NULL)
20371 continue;
20373 child = lookup_decl_die (member);
20374 if (child)
20375 splice_child_die (context_die, child);
20376 else
20377 gen_decl_die (member, NULL, context_die);
20381 /* Generate a DIE for a structure or union type. If TYPE_DECL_SUPPRESS_DEBUG
20382 is set, we pretend that the type was never defined, so we only get the
20383 member DIEs needed by later specification DIEs. */
20385 static void
20386 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20387 enum debug_info_usage usage)
20389 if (TREE_ASM_WRITTEN (type))
20391 /* Fill in the bound of variable-length fields in late dwarf if
20392 still incomplete. */
20393 if (!early_dwarf && variably_modified_type_p (type, NULL))
20394 for (tree member = TYPE_FIELDS (type);
20395 member;
20396 member = DECL_CHAIN (member))
20397 fill_variable_array_bounds (TREE_TYPE (member));
20398 return;
20401 dw_die_ref type_die = lookup_type_die (type);
20402 dw_die_ref scope_die = 0;
20403 int nested = 0;
20404 int complete = (TYPE_SIZE (type)
20405 && (! TYPE_STUB_DECL (type)
20406 || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20407 int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20408 complete = complete && should_emit_struct_debug (type, usage);
20410 if (type_die && ! complete)
20411 return;
20413 if (TYPE_CONTEXT (type) != NULL_TREE
20414 && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20415 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20416 nested = 1;
20418 scope_die = scope_die_for (type, context_die);
20420 /* Generate child dies for template paramaters. */
20421 if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
20422 schedule_generic_params_dies_gen (type);
20424 if (! type_die || (nested && is_cu_die (scope_die)))
20425 /* First occurrence of type or toplevel definition of nested class. */
20427 dw_die_ref old_die = type_die;
20429 type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20430 ? record_type_tag (type) : DW_TAG_union_type,
20431 scope_die, type);
20432 equate_type_number_to_die (type, type_die);
20433 if (old_die)
20434 add_AT_specification (type_die, old_die);
20435 else
20436 add_name_attribute (type_die, type_tag (type));
20438 else
20439 remove_AT (type_die, DW_AT_declaration);
20441 /* If this type has been completed, then give it a byte_size attribute and
20442 then give a list of members. */
20443 if (complete && !ns_decl)
20445 /* Prevent infinite recursion in cases where the type of some member of
20446 this type is expressed in terms of this type itself. */
20447 TREE_ASM_WRITTEN (type) = 1;
20448 add_byte_size_attribute (type_die, type);
20449 if (TYPE_STUB_DECL (type) != NULL_TREE)
20451 add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20452 add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20455 /* If the first reference to this type was as the return type of an
20456 inline function, then it may not have a parent. Fix this now. */
20457 if (type_die->die_parent == NULL)
20458 add_child_die (scope_die, type_die);
20460 push_decl_scope (type);
20461 gen_member_die (type, type_die);
20462 pop_decl_scope ();
20464 add_gnat_descriptive_type_attribute (type_die, type, context_die);
20465 if (TYPE_ARTIFICIAL (type))
20466 add_AT_flag (type_die, DW_AT_artificial, 1);
20468 /* GNU extension: Record what type our vtable lives in. */
20469 if (TYPE_VFIELD (type))
20471 tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20473 gen_type_die (vtype, context_die);
20474 add_AT_die_ref (type_die, DW_AT_containing_type,
20475 lookup_type_die (vtype));
20478 else
20480 add_AT_flag (type_die, DW_AT_declaration, 1);
20482 /* We don't need to do this for function-local types. */
20483 if (TYPE_STUB_DECL (type)
20484 && ! decl_function_context (TYPE_STUB_DECL (type)))
20485 vec_safe_push (incomplete_types, type);
20488 if (get_AT (type_die, DW_AT_name))
20489 add_pubtype (type, type_die);
20492 /* Generate a DIE for a subroutine _type_. */
20494 static void
20495 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20497 tree return_type = TREE_TYPE (type);
20498 dw_die_ref subr_die
20499 = new_die (DW_TAG_subroutine_type,
20500 scope_die_for (type, context_die), type);
20502 equate_type_number_to_die (type, subr_die);
20503 add_prototyped_attribute (subr_die, type);
20504 add_type_attribute (subr_die, return_type, TYPE_UNQUALIFIED, context_die);
20505 gen_formal_types_die (type, subr_die);
20507 if (get_AT (subr_die, DW_AT_name))
20508 add_pubtype (type, subr_die);
20511 /* Generate a DIE for a type definition. */
20513 static void
20514 gen_typedef_die (tree decl, dw_die_ref context_die)
20516 dw_die_ref type_die;
20517 tree origin;
20519 if (TREE_ASM_WRITTEN (decl))
20521 if (DECL_ORIGINAL_TYPE (decl))
20522 fill_variable_array_bounds (DECL_ORIGINAL_TYPE (decl));
20523 return;
20526 TREE_ASM_WRITTEN (decl) = 1;
20527 type_die = new_die (DW_TAG_typedef, context_die, decl);
20528 origin = decl_ultimate_origin (decl);
20529 if (origin != NULL)
20530 add_abstract_origin_attribute (type_die, origin);
20531 else
20533 tree type;
20535 add_name_and_src_coords_attributes (type_die, decl);
20536 if (DECL_ORIGINAL_TYPE (decl))
20538 type = DECL_ORIGINAL_TYPE (decl);
20540 if (type == error_mark_node)
20541 return;
20543 gcc_assert (type != TREE_TYPE (decl));
20544 equate_type_number_to_die (TREE_TYPE (decl), type_die);
20546 else
20548 type = TREE_TYPE (decl);
20550 if (type == error_mark_node)
20551 return;
20553 if (is_naming_typedef_decl (TYPE_NAME (type)))
20555 /* Here, we are in the case of decl being a typedef naming
20556 an anonymous type, e.g:
20557 typedef struct {...} foo;
20558 In that case TREE_TYPE (decl) is not a typedef variant
20559 type and TYPE_NAME of the anonymous type is set to the
20560 TYPE_DECL of the typedef. This construct is emitted by
20561 the C++ FE.
20563 TYPE is the anonymous struct named by the typedef
20564 DECL. As we need the DW_AT_type attribute of the
20565 DW_TAG_typedef to point to the DIE of TYPE, let's
20566 generate that DIE right away. add_type_attribute
20567 called below will then pick (via lookup_type_die) that
20568 anonymous struct DIE. */
20569 if (!TREE_ASM_WRITTEN (type))
20570 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20572 /* This is a GNU Extension. We are adding a
20573 DW_AT_linkage_name attribute to the DIE of the
20574 anonymous struct TYPE. The value of that attribute
20575 is the name of the typedef decl naming the anonymous
20576 struct. This greatly eases the work of consumers of
20577 this debug info. */
20578 add_linkage_attr (lookup_type_die (type), decl);
20582 add_type_attribute (type_die, type, decl_quals (decl), context_die);
20584 if (is_naming_typedef_decl (decl))
20585 /* We want that all subsequent calls to lookup_type_die with
20586 TYPE in argument yield the DW_TAG_typedef we have just
20587 created. */
20588 equate_type_number_to_die (type, type_die);
20590 add_accessibility_attribute (type_die, decl);
20593 if (DECL_ABSTRACT_P (decl))
20594 equate_decl_number_to_die (decl, type_die);
20596 if (get_AT (type_die, DW_AT_name))
20597 add_pubtype (decl, type_die);
20600 /* Generate a DIE for a struct, class, enum or union type. */
20602 static void
20603 gen_tagged_type_die (tree type,
20604 dw_die_ref context_die,
20605 enum debug_info_usage usage)
20607 int need_pop;
20609 if (type == NULL_TREE
20610 || !is_tagged_type (type))
20611 return;
20613 if (TREE_ASM_WRITTEN (type))
20614 need_pop = 0;
20615 /* If this is a nested type whose containing class hasn't been written
20616 out yet, writing it out will cover this one, too. This does not apply
20617 to instantiations of member class templates; they need to be added to
20618 the containing class as they are generated. FIXME: This hurts the
20619 idea of combining type decls from multiple TUs, since we can't predict
20620 what set of template instantiations we'll get. */
20621 else if (TYPE_CONTEXT (type)
20622 && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20623 && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20625 gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20627 if (TREE_ASM_WRITTEN (type))
20628 return;
20630 /* If that failed, attach ourselves to the stub. */
20631 push_decl_scope (TYPE_CONTEXT (type));
20632 context_die = lookup_type_die (TYPE_CONTEXT (type));
20633 need_pop = 1;
20635 else if (TYPE_CONTEXT (type) != NULL_TREE
20636 && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20638 /* If this type is local to a function that hasn't been written
20639 out yet, use a NULL context for now; it will be fixed up in
20640 decls_for_scope. */
20641 context_die = lookup_decl_die (TYPE_CONTEXT (type));
20642 /* A declaration DIE doesn't count; nested types need to go in the
20643 specification. */
20644 if (context_die && is_declaration_die (context_die))
20645 context_die = NULL;
20646 need_pop = 0;
20648 else
20650 context_die = declare_in_namespace (type, context_die);
20651 need_pop = 0;
20654 if (TREE_CODE (type) == ENUMERAL_TYPE)
20656 /* This might have been written out by the call to
20657 declare_in_namespace. */
20658 if (!TREE_ASM_WRITTEN (type))
20659 gen_enumeration_type_die (type, context_die);
20661 else
20662 gen_struct_or_union_type_die (type, context_die, usage);
20664 if (need_pop)
20665 pop_decl_scope ();
20667 /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20668 it up if it is ever completed. gen_*_type_die will set it for us
20669 when appropriate. */
20672 /* Generate a type description DIE. */
20674 static void
20675 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20676 enum debug_info_usage usage)
20678 struct array_descr_info info;
20680 if (type == NULL_TREE || type == error_mark_node)
20681 return;
20683 #ifdef ENABLE_CHECKING
20684 if (type)
20685 verify_type (type);
20686 #endif
20688 if (TYPE_NAME (type) != NULL_TREE
20689 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20690 && is_redundant_typedef (TYPE_NAME (type))
20691 && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20692 /* The DECL of this type is a typedef we don't want to emit debug
20693 info for but we want debug info for its underlying typedef.
20694 This can happen for e.g, the injected-class-name of a C++
20695 type. */
20696 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20698 /* If TYPE is a typedef type variant, let's generate debug info
20699 for the parent typedef which TYPE is a type of. */
20700 if (typedef_variant_p (type))
20702 if (TREE_ASM_WRITTEN (type))
20703 return;
20705 /* Prevent broken recursion; we can't hand off to the same type. */
20706 gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20708 /* Give typedefs the right scope. */
20709 context_die = scope_die_for (type, context_die);
20711 TREE_ASM_WRITTEN (type) = 1;
20713 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20714 return;
20717 /* If type is an anonymous tagged type named by a typedef, let's
20718 generate debug info for the typedef. */
20719 if (is_naming_typedef_decl (TYPE_NAME (type)))
20721 /* Use the DIE of the containing namespace as the parent DIE of
20722 the type description DIE we want to generate. */
20723 if (DECL_CONTEXT (TYPE_NAME (type))
20724 && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20725 context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20727 gen_decl_die (TYPE_NAME (type), NULL, context_die);
20728 return;
20731 /* We are going to output a DIE to represent the unqualified version
20732 of this type (i.e. without any const or volatile qualifiers) so
20733 get the main variant (i.e. the unqualified version) of this type
20734 now. (Vectors are special because the debugging info is in the
20735 cloned type itself). */
20736 if (TREE_CODE (type) != VECTOR_TYPE)
20737 type = type_main_variant (type);
20739 /* If this is an array type with hidden descriptor, handle it first. */
20740 if (!TREE_ASM_WRITTEN (type)
20741 && lang_hooks.types.get_array_descr_info)
20743 memset (&info, 0, sizeof (info));
20744 if (lang_hooks.types.get_array_descr_info (type, &info))
20746 gen_descr_array_type_die (type, &info, context_die);
20747 TREE_ASM_WRITTEN (type) = 1;
20748 return;
20752 if (TREE_ASM_WRITTEN (type))
20754 /* Variable-length types may be incomplete even if
20755 TREE_ASM_WRITTEN. For such types, fall through to
20756 gen_array_type_die() and possibly fill in
20757 DW_AT_{upper,lower}_bound attributes. */
20758 if ((TREE_CODE (type) != ARRAY_TYPE
20759 && TREE_CODE (type) != RECORD_TYPE
20760 && TREE_CODE (type) != UNION_TYPE
20761 && TREE_CODE (type) != QUAL_UNION_TYPE)
20762 || !variably_modified_type_p (type, NULL))
20763 return;
20766 switch (TREE_CODE (type))
20768 case ERROR_MARK:
20769 break;
20771 case POINTER_TYPE:
20772 case REFERENCE_TYPE:
20773 /* We must set TREE_ASM_WRITTEN in case this is a recursive type. This
20774 ensures that the gen_type_die recursion will terminate even if the
20775 type is recursive. Recursive types are possible in Ada. */
20776 /* ??? We could perhaps do this for all types before the switch
20777 statement. */
20778 TREE_ASM_WRITTEN (type) = 1;
20780 /* For these types, all that is required is that we output a DIE (or a
20781 set of DIEs) to represent the "basis" type. */
20782 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20783 DINFO_USAGE_IND_USE);
20784 break;
20786 case OFFSET_TYPE:
20787 /* This code is used for C++ pointer-to-data-member types.
20788 Output a description of the relevant class type. */
20789 gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20790 DINFO_USAGE_IND_USE);
20792 /* Output a description of the type of the object pointed to. */
20793 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20794 DINFO_USAGE_IND_USE);
20796 /* Now output a DIE to represent this pointer-to-data-member type
20797 itself. */
20798 gen_ptr_to_mbr_type_die (type, context_die);
20799 break;
20801 case FUNCTION_TYPE:
20802 /* Force out return type (in case it wasn't forced out already). */
20803 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20804 DINFO_USAGE_DIR_USE);
20805 gen_subroutine_type_die (type, context_die);
20806 break;
20808 case METHOD_TYPE:
20809 /* Force out return type (in case it wasn't forced out already). */
20810 gen_type_die_with_usage (TREE_TYPE (type), context_die,
20811 DINFO_USAGE_DIR_USE);
20812 gen_subroutine_type_die (type, context_die);
20813 break;
20815 case ARRAY_TYPE:
20816 case VECTOR_TYPE:
20817 gen_array_type_die (type, context_die);
20818 break;
20820 case ENUMERAL_TYPE:
20821 case RECORD_TYPE:
20822 case UNION_TYPE:
20823 case QUAL_UNION_TYPE:
20824 gen_tagged_type_die (type, context_die, usage);
20825 return;
20827 case VOID_TYPE:
20828 case INTEGER_TYPE:
20829 case REAL_TYPE:
20830 case FIXED_POINT_TYPE:
20831 case COMPLEX_TYPE:
20832 case BOOLEAN_TYPE:
20833 case POINTER_BOUNDS_TYPE:
20834 /* No DIEs needed for fundamental types. */
20835 break;
20837 case NULLPTR_TYPE:
20838 case LANG_TYPE:
20839 /* Just use DW_TAG_unspecified_type. */
20841 dw_die_ref type_die = lookup_type_die (type);
20842 if (type_die == NULL)
20844 tree name = TYPE_IDENTIFIER (type);
20845 type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (),
20846 type);
20847 add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20848 equate_type_number_to_die (type, type_die);
20851 break;
20853 default:
20854 if (is_cxx_auto (type))
20856 tree name = TYPE_IDENTIFIER (type);
20857 dw_die_ref *die = (name == get_identifier ("auto")
20858 ? &auto_die : &decltype_auto_die);
20859 if (!*die)
20861 *die = new_die (DW_TAG_unspecified_type,
20862 comp_unit_die (), NULL_TREE);
20863 add_name_attribute (*die, IDENTIFIER_POINTER (name));
20865 equate_type_number_to_die (type, *die);
20866 break;
20868 gcc_unreachable ();
20871 TREE_ASM_WRITTEN (type) = 1;
20874 static void
20875 gen_type_die (tree type, dw_die_ref context_die)
20877 if (type != error_mark_node)
20879 gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20880 #ifdef ENABLE_CHECKING
20881 dw_die_ref die = lookup_type_die (type);
20882 if (die)
20883 check_die (die);
20884 #endif
20888 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20889 things which are local to the given block. */
20891 static void
20892 gen_block_die (tree stmt, dw_die_ref context_die)
20894 int must_output_die = 0;
20895 bool inlined_func;
20897 /* Ignore blocks that are NULL. */
20898 if (stmt == NULL_TREE)
20899 return;
20901 inlined_func = inlined_function_outer_scope_p (stmt);
20903 /* If the block is one fragment of a non-contiguous block, do not
20904 process the variables, since they will have been done by the
20905 origin block. Do process subblocks. */
20906 if (BLOCK_FRAGMENT_ORIGIN (stmt))
20908 tree sub;
20910 for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20911 gen_block_die (sub, context_die);
20913 return;
20916 /* Determine if we need to output any Dwarf DIEs at all to represent this
20917 block. */
20918 if (inlined_func)
20919 /* The outer scopes for inlinings *must* always be represented. We
20920 generate DW_TAG_inlined_subroutine DIEs for them. (See below.) */
20921 must_output_die = 1;
20922 else
20924 /* Determine if this block directly contains any "significant"
20925 local declarations which we will need to output DIEs for. */
20926 if (debug_info_level > DINFO_LEVEL_TERSE)
20927 /* We are not in terse mode so *any* local declaration counts
20928 as being a "significant" one. */
20929 must_output_die = ((BLOCK_VARS (stmt) != NULL
20930 || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20931 && (TREE_USED (stmt)
20932 || TREE_ASM_WRITTEN (stmt)
20933 || BLOCK_ABSTRACT (stmt)));
20934 else if ((TREE_USED (stmt)
20935 || TREE_ASM_WRITTEN (stmt)
20936 || BLOCK_ABSTRACT (stmt))
20937 && !dwarf2out_ignore_block (stmt))
20938 must_output_die = 1;
20941 /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20942 DIE for any block which contains no significant local declarations at
20943 all. Rather, in such cases we just call `decls_for_scope' so that any
20944 needed Dwarf info for any sub-blocks will get properly generated. Note
20945 that in terse mode, our definition of what constitutes a "significant"
20946 local declaration gets restricted to include only inlined function
20947 instances and local (nested) function definitions. */
20948 if (must_output_die)
20950 if (inlined_func)
20952 /* If STMT block is abstract, that means we have been called
20953 indirectly from dwarf2out_abstract_function.
20954 That function rightfully marks the descendent blocks (of
20955 the abstract function it is dealing with) as being abstract,
20956 precisely to prevent us from emitting any
20957 DW_TAG_inlined_subroutine DIE as a descendent
20958 of an abstract function instance. So in that case, we should
20959 not call gen_inlined_subroutine_die.
20961 Later though, when cgraph asks dwarf2out to emit info
20962 for the concrete instance of the function decl into which
20963 the concrete instance of STMT got inlined, the later will lead
20964 to the generation of a DW_TAG_inlined_subroutine DIE. */
20965 if (! BLOCK_ABSTRACT (stmt))
20966 gen_inlined_subroutine_die (stmt, context_die);
20968 else
20969 gen_lexical_block_die (stmt, context_die);
20971 else
20972 decls_for_scope (stmt, context_die);
20975 /* Process variable DECL (or variable with origin ORIGIN) within
20976 block STMT and add it to CONTEXT_DIE. */
20977 static void
20978 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20980 dw_die_ref die;
20981 tree decl_or_origin = decl ? decl : origin;
20983 if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20984 die = lookup_decl_die (decl_or_origin);
20985 else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20986 && TYPE_DECL_IS_STUB (decl_or_origin))
20987 die = lookup_type_die (TREE_TYPE (decl_or_origin));
20988 else
20989 die = NULL;
20991 if (die != NULL && die->die_parent == NULL)
20992 add_child_die (context_die, die);
20993 else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20995 if (early_dwarf)
20996 dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20997 stmt, context_die);
20999 else
21000 gen_decl_die (decl, origin, context_die);
21003 /* Generate all of the decls declared within a given scope and (recursively)
21004 all of its sub-blocks. */
21006 static void
21007 decls_for_scope (tree stmt, dw_die_ref context_die)
21009 tree decl;
21010 unsigned int i;
21011 tree subblocks;
21013 /* Ignore NULL blocks. */
21014 if (stmt == NULL_TREE)
21015 return;
21017 /* Output the DIEs to represent all of the data objects and typedefs
21018 declared directly within this block but not within any nested
21019 sub-blocks. Also, nested function and tag DIEs have been
21020 generated with a parent of NULL; fix that up now. We don't
21021 have to do this if we're at -g1. */
21022 if (debug_info_level > DINFO_LEVEL_TERSE)
21024 for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21025 process_scope_var (stmt, decl, NULL_TREE, context_die);
21026 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21027 process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21028 context_die);
21031 /* Even if we're at -g1, we need to process the subblocks in order to get
21032 inlined call information. */
21034 /* Output the DIEs to represent all sub-blocks (and the items declared
21035 therein) of this block. */
21036 for (subblocks = BLOCK_SUBBLOCKS (stmt);
21037 subblocks != NULL;
21038 subblocks = BLOCK_CHAIN (subblocks))
21039 gen_block_die (subblocks, context_die);
21042 /* Is this a typedef we can avoid emitting? */
21044 static inline int
21045 is_redundant_typedef (const_tree decl)
21047 if (TYPE_DECL_IS_STUB (decl))
21048 return 1;
21050 if (DECL_ARTIFICIAL (decl)
21051 && DECL_CONTEXT (decl)
21052 && is_tagged_type (DECL_CONTEXT (decl))
21053 && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21054 && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21055 /* Also ignore the artificial member typedef for the class name. */
21056 return 1;
21058 return 0;
21061 /* Return TRUE if TYPE is a typedef that names a type for linkage
21062 purposes. This kind of typedefs is produced by the C++ FE for
21063 constructs like:
21065 typedef struct {...} foo;
21067 In that case, there is no typedef variant type produced for foo.
21068 Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21069 struct type. */
21071 static bool
21072 is_naming_typedef_decl (const_tree decl)
21074 if (decl == NULL_TREE
21075 || TREE_CODE (decl) != TYPE_DECL
21076 || !is_tagged_type (TREE_TYPE (decl))
21077 || DECL_IS_BUILTIN (decl)
21078 || is_redundant_typedef (decl)
21079 /* It looks like Ada produces TYPE_DECLs that are very similar
21080 to C++ naming typedefs but that have different
21081 semantics. Let's be specific to c++ for now. */
21082 || !is_cxx ())
21083 return FALSE;
21085 return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21086 && TYPE_NAME (TREE_TYPE (decl)) == decl
21087 && (TYPE_STUB_DECL (TREE_TYPE (decl))
21088 != TYPE_NAME (TREE_TYPE (decl))));
21091 /* Looks up the DIE for a context. */
21093 static inline dw_die_ref
21094 lookup_context_die (tree context)
21096 if (context)
21098 /* Find die that represents this context. */
21099 if (TYPE_P (context))
21101 context = TYPE_MAIN_VARIANT (context);
21102 dw_die_ref ctx = lookup_type_die (context);
21103 if (!ctx)
21104 return NULL;
21105 return strip_naming_typedef (context, ctx);
21107 else
21108 return lookup_decl_die (context);
21110 return comp_unit_die ();
21113 /* Returns the DIE for a context. */
21115 static inline dw_die_ref
21116 get_context_die (tree context)
21118 if (context)
21120 /* Find die that represents this context. */
21121 if (TYPE_P (context))
21123 context = TYPE_MAIN_VARIANT (context);
21124 return strip_naming_typedef (context, force_type_die (context));
21126 else
21127 return force_decl_die (context);
21129 return comp_unit_die ();
21132 /* Returns the DIE for decl. A DIE will always be returned. */
21134 static dw_die_ref
21135 force_decl_die (tree decl)
21137 dw_die_ref decl_die;
21138 unsigned saved_external_flag;
21139 tree save_fn = NULL_TREE;
21140 decl_die = lookup_decl_die (decl);
21141 if (!decl_die)
21143 dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21145 decl_die = lookup_decl_die (decl);
21146 if (decl_die)
21147 return decl_die;
21149 switch (TREE_CODE (decl))
21151 case FUNCTION_DECL:
21152 /* Clear current_function_decl, so that gen_subprogram_die thinks
21153 that this is a declaration. At this point, we just want to force
21154 declaration die. */
21155 save_fn = current_function_decl;
21156 current_function_decl = NULL_TREE;
21157 gen_subprogram_die (decl, context_die);
21158 current_function_decl = save_fn;
21159 break;
21161 case VAR_DECL:
21162 /* Set external flag to force declaration die. Restore it after
21163 gen_decl_die() call. */
21164 saved_external_flag = DECL_EXTERNAL (decl);
21165 DECL_EXTERNAL (decl) = 1;
21166 gen_decl_die (decl, NULL, context_die);
21167 DECL_EXTERNAL (decl) = saved_external_flag;
21168 break;
21170 case NAMESPACE_DECL:
21171 if (dwarf_version >= 3 || !dwarf_strict)
21172 dwarf2out_decl (decl);
21173 else
21174 /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace. */
21175 decl_die = comp_unit_die ();
21176 break;
21178 case TRANSLATION_UNIT_DECL:
21179 decl_die = comp_unit_die ();
21180 break;
21182 default:
21183 gcc_unreachable ();
21186 /* We should be able to find the DIE now. */
21187 if (!decl_die)
21188 decl_die = lookup_decl_die (decl);
21189 gcc_assert (decl_die);
21192 return decl_die;
21195 /* Returns the DIE for TYPE, that must not be a base type. A DIE is
21196 always returned. */
21198 static dw_die_ref
21199 force_type_die (tree type)
21201 dw_die_ref type_die;
21203 type_die = lookup_type_die (type);
21204 if (!type_die)
21206 dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21208 type_die = modified_type_die (type, TYPE_QUALS_NO_ADDR_SPACE (type),
21209 context_die);
21210 gcc_assert (type_die);
21212 return type_die;
21215 /* Force out any required namespaces to be able to output DECL,
21216 and return the new context_die for it, if it's changed. */
21218 static dw_die_ref
21219 setup_namespace_context (tree thing, dw_die_ref context_die)
21221 tree context = (DECL_P (thing)
21222 ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21223 if (context && TREE_CODE (context) == NAMESPACE_DECL)
21224 /* Force out the namespace. */
21225 context_die = force_decl_die (context);
21227 return context_die;
21230 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21231 type) within its namespace, if appropriate.
21233 For compatibility with older debuggers, namespace DIEs only contain
21234 declarations; all definitions are emitted at CU scope, with
21235 DW_AT_specification pointing to the declaration (like with class
21236 members). */
21238 static dw_die_ref
21239 declare_in_namespace (tree thing, dw_die_ref context_die)
21241 dw_die_ref ns_context;
21243 if (debug_info_level <= DINFO_LEVEL_TERSE)
21244 return context_die;
21246 /* External declarations in the local scope only need to be emitted
21247 once, not once in the namespace and once in the scope.
21249 This avoids declaring the `extern' below in the
21250 namespace DIE as well as in the innermost scope:
21252 namespace S
21254 int i=5;
21255 int foo()
21257 int i=8;
21258 extern int i;
21259 return i;
21263 if (DECL_P (thing) && DECL_EXTERNAL (thing) && local_scope_p (context_die))
21264 return context_die;
21266 /* If this decl is from an inlined function, then don't try to emit it in its
21267 namespace, as we will get confused. It would have already been emitted
21268 when the abstract instance of the inline function was emitted anyways. */
21269 if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21270 return context_die;
21272 ns_context = setup_namespace_context (thing, context_die);
21274 if (ns_context != context_die)
21276 if (is_fortran ())
21277 return ns_context;
21278 if (DECL_P (thing))
21279 gen_decl_die (thing, NULL, ns_context);
21280 else
21281 gen_type_die (thing, ns_context);
21283 return context_die;
21286 /* Generate a DIE for a namespace or namespace alias. */
21288 static void
21289 gen_namespace_die (tree decl, dw_die_ref context_die)
21291 dw_die_ref namespace_die;
21293 /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21294 they are an alias of. */
21295 if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21297 /* Output a real namespace or module. */
21298 context_die = setup_namespace_context (decl, comp_unit_die ());
21299 namespace_die = new_die (is_fortran ()
21300 ? DW_TAG_module : DW_TAG_namespace,
21301 context_die, decl);
21302 /* For Fortran modules defined in different CU don't add src coords. */
21303 if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21305 const char *name = dwarf2_name (decl, 0);
21306 if (name)
21307 add_name_attribute (namespace_die, name);
21309 else
21310 add_name_and_src_coords_attributes (namespace_die, decl);
21311 if (DECL_EXTERNAL (decl))
21312 add_AT_flag (namespace_die, DW_AT_declaration, 1);
21313 equate_decl_number_to_die (decl, namespace_die);
21315 else
21317 /* Output a namespace alias. */
21319 /* Force out the namespace we are an alias of, if necessary. */
21320 dw_die_ref origin_die
21321 = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21323 if (DECL_FILE_SCOPE_P (decl)
21324 || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21325 context_die = setup_namespace_context (decl, comp_unit_die ());
21326 /* Now create the namespace alias DIE. */
21327 namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21328 add_name_and_src_coords_attributes (namespace_die, decl);
21329 add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21330 equate_decl_number_to_die (decl, namespace_die);
21332 /* Bypass dwarf2_name's check for DECL_NAMELESS. */
21333 if (want_pubnames ())
21334 add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
21337 /* Generate Dwarf debug information for a decl described by DECL.
21338 The return value is currently only meaningful for PARM_DECLs,
21339 for all other decls it returns NULL. */
21341 static dw_die_ref
21342 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21344 tree decl_or_origin = decl ? decl : origin;
21345 tree class_origin = NULL, ultimate_origin;
21347 if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21348 return NULL;
21350 /* Ignore pointer bounds decls. */
21351 if (DECL_P (decl_or_origin)
21352 && TREE_TYPE (decl_or_origin)
21353 && POINTER_BOUNDS_P (decl_or_origin))
21354 return NULL;
21356 switch (TREE_CODE (decl_or_origin))
21358 case ERROR_MARK:
21359 break;
21361 case CONST_DECL:
21362 if (!is_fortran () && !is_ada ())
21364 /* The individual enumerators of an enum type get output when we output
21365 the Dwarf representation of the relevant enum type itself. */
21366 break;
21369 /* Emit its type. */
21370 gen_type_die (TREE_TYPE (decl), context_die);
21372 /* And its containing namespace. */
21373 context_die = declare_in_namespace (decl, context_die);
21375 gen_const_die (decl, context_die);
21376 break;
21378 case FUNCTION_DECL:
21379 /* Don't output any DIEs to represent mere function declarations,
21380 unless they are class members or explicit block externs. */
21381 if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21382 && DECL_FILE_SCOPE_P (decl_or_origin)
21383 && (current_function_decl == NULL_TREE
21384 || DECL_ARTIFICIAL (decl_or_origin)))
21385 break;
21387 #if 0
21388 /* FIXME */
21389 /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21390 on local redeclarations of global functions. That seems broken. */
21391 if (current_function_decl != decl)
21392 /* This is only a declaration. */;
21393 #endif
21395 /* If we're emitting a clone, emit info for the abstract instance. */
21396 if (origin || DECL_ORIGIN (decl) != decl)
21397 dwarf2out_abstract_function (origin
21398 ? DECL_ORIGIN (origin)
21399 : DECL_ABSTRACT_ORIGIN (decl));
21401 /* If we're emitting an out-of-line copy of an inline function,
21402 emit info for the abstract instance and set up to refer to it. */
21403 else if (cgraph_function_possibly_inlined_p (decl)
21404 && ! DECL_ABSTRACT_P (decl)
21405 && ! class_or_namespace_scope_p (context_die)
21406 /* dwarf2out_abstract_function won't emit a die if this is just
21407 a declaration. We must avoid setting DECL_ABSTRACT_ORIGIN in
21408 that case, because that works only if we have a die. */
21409 && DECL_INITIAL (decl) != NULL_TREE)
21411 dwarf2out_abstract_function (decl);
21412 set_decl_origin_self (decl);
21415 /* Otherwise we're emitting the primary DIE for this decl. */
21416 else if (debug_info_level > DINFO_LEVEL_TERSE)
21418 /* Before we describe the FUNCTION_DECL itself, make sure that we
21419 have its containing type. */
21420 if (!origin)
21421 origin = decl_class_context (decl);
21422 if (origin != NULL_TREE)
21423 gen_type_die (origin, context_die);
21425 /* And its return type. */
21426 gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21428 /* And its virtual context. */
21429 if (DECL_VINDEX (decl) != NULL_TREE)
21430 gen_type_die (DECL_CONTEXT (decl), context_die);
21432 /* Make sure we have a member DIE for decl. */
21433 if (origin != NULL_TREE)
21434 gen_type_die_for_member (origin, decl, context_die);
21436 /* And its containing namespace. */
21437 context_die = declare_in_namespace (decl, context_die);
21440 /* Now output a DIE to represent the function itself. */
21441 if (decl)
21442 gen_subprogram_die (decl, context_die);
21443 break;
21445 case TYPE_DECL:
21446 /* If we are in terse mode, don't generate any DIEs to represent any
21447 actual typedefs. */
21448 if (debug_info_level <= DINFO_LEVEL_TERSE)
21449 break;
21451 /* In the special case of a TYPE_DECL node representing the declaration
21452 of some type tag, if the given TYPE_DECL is marked as having been
21453 instantiated from some other (original) TYPE_DECL node (e.g. one which
21454 was generated within the original definition of an inline function) we
21455 used to generate a special (abbreviated) DW_TAG_structure_type,
21456 DW_TAG_union_type, or DW_TAG_enumeration_type DIE here. But nothing
21457 should be actually referencing those DIEs, as variable DIEs with that
21458 type would be emitted already in the abstract origin, so it was always
21459 removed during unused type prunning. Don't add anything in this
21460 case. */
21461 if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21462 break;
21464 if (is_redundant_typedef (decl))
21465 gen_type_die (TREE_TYPE (decl), context_die);
21466 else
21467 /* Output a DIE to represent the typedef itself. */
21468 gen_typedef_die (decl, context_die);
21469 break;
21471 case LABEL_DECL:
21472 if (debug_info_level >= DINFO_LEVEL_NORMAL)
21473 gen_label_die (decl, context_die);
21474 break;
21476 case VAR_DECL:
21477 case RESULT_DECL:
21478 /* If we are in terse mode, don't generate any DIEs to represent any
21479 variable declarations or definitions. */
21480 if (debug_info_level <= DINFO_LEVEL_TERSE)
21481 break;
21483 /* Output any DIEs that are needed to specify the type of this data
21484 object. */
21485 if (decl_by_reference_p (decl_or_origin))
21486 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21487 else
21488 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21490 /* And its containing type. */
21491 class_origin = decl_class_context (decl_or_origin);
21492 if (class_origin != NULL_TREE)
21493 gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21495 /* And its containing namespace. */
21496 context_die = declare_in_namespace (decl_or_origin, context_die);
21498 /* Now output the DIE to represent the data object itself. This gets
21499 complicated because of the possibility that the VAR_DECL really
21500 represents an inlined instance of a formal parameter for an inline
21501 function. */
21502 ultimate_origin = decl_ultimate_origin (decl_or_origin);
21503 if (ultimate_origin != NULL_TREE
21504 && TREE_CODE (ultimate_origin) == PARM_DECL)
21505 gen_formal_parameter_die (decl, origin,
21506 true /* Emit name attribute. */,
21507 context_die);
21508 else
21509 gen_variable_die (decl, origin, context_die);
21510 break;
21512 case FIELD_DECL:
21513 /* Ignore the nameless fields that are used to skip bits but handle C++
21514 anonymous unions and structs. */
21515 if (DECL_NAME (decl) != NULL_TREE
21516 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21517 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21519 gen_type_die (member_declared_type (decl), context_die);
21520 gen_field_die (decl, context_die);
21522 break;
21524 case PARM_DECL:
21525 if (DECL_BY_REFERENCE (decl_or_origin))
21526 gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21527 else
21528 gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21529 return gen_formal_parameter_die (decl, origin,
21530 true /* Emit name attribute. */,
21531 context_die);
21533 case NAMESPACE_DECL:
21534 case IMPORTED_DECL:
21535 if (dwarf_version >= 3 || !dwarf_strict)
21536 gen_namespace_die (decl, context_die);
21537 break;
21539 case NAMELIST_DECL:
21540 gen_namelist_decl (DECL_NAME (decl), context_die,
21541 NAMELIST_DECL_ASSOCIATED_DECL (decl));
21542 break;
21544 default:
21545 /* Probably some frontend-internal decl. Assume we don't care. */
21546 gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21547 break;
21550 return NULL;
21553 /* Output initial debug information for global DECL. Called at the
21554 end of the parsing process.
21556 This is the initial debug generation process. As such, the DIEs
21557 generated may be incomplete. A later debug generation pass
21558 (dwarf2out_late_global_decl) will augment the information generated
21559 in this pass (e.g., with complete location info). */
21561 static void
21562 dwarf2out_early_global_decl (tree decl)
21564 set_early_dwarf s;
21566 /* gen_decl_die() will set DECL_ABSTRACT because
21567 cgraph_function_possibly_inlined_p() returns true. This is in
21568 turn will cause DW_AT_inline attributes to be set.
21570 This happens because at early dwarf generation, there is no
21571 cgraph information, causing cgraph_function_possibly_inlined_p()
21572 to return true. Trick cgraph_function_possibly_inlined_p()
21573 while we generate dwarf early. */
21574 bool save = symtab->global_info_ready;
21575 symtab->global_info_ready = true;
21577 /* We don't handle TYPE_DECLs. If required, they'll be reached via
21578 other DECLs and they can point to template types or other things
21579 that dwarf2out can't handle when done via dwarf2out_decl. */
21580 if (TREE_CODE (decl) != TYPE_DECL
21581 && TREE_CODE (decl) != PARM_DECL)
21583 tree save_fndecl = current_function_decl;
21584 if (TREE_CODE (decl) == FUNCTION_DECL)
21586 /* No cfun means the symbol has no body, so there's nothing
21587 to emit. */
21588 if (!DECL_STRUCT_FUNCTION (decl))
21589 goto early_decl_exit;
21591 current_function_decl = decl;
21593 dwarf2out_decl (decl);
21594 if (TREE_CODE (decl) == FUNCTION_DECL)
21595 current_function_decl = save_fndecl;
21597 early_decl_exit:
21598 symtab->global_info_ready = save;
21601 /* Output debug information for global decl DECL. Called from
21602 toplev.c after compilation proper has finished. */
21604 static void
21605 dwarf2out_late_global_decl (tree decl)
21607 /* Output any global decls we missed or fill-in any location
21608 information we were unable to determine on the first pass.
21610 Skip over functions because they were handled by the
21611 debug_hooks->function_decl() call in rest_of_handle_final. */
21612 if ((TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21613 && !POINTER_BOUNDS_P (decl))
21614 dwarf2out_decl (decl);
21617 /* Output debug information for type decl DECL. Called from toplev.c
21618 and from language front ends (to record built-in types). */
21619 static void
21620 dwarf2out_type_decl (tree decl, int local)
21622 if (!local)
21624 set_early_dwarf s;
21625 dwarf2out_decl (decl);
21629 /* Output debug information for imported module or decl DECL.
21630 NAME is non-NULL name in the lexical block if the decl has been renamed.
21631 LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21632 that DECL belongs to.
21633 LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK. */
21634 static void
21635 dwarf2out_imported_module_or_decl_1 (tree decl,
21636 tree name,
21637 tree lexical_block,
21638 dw_die_ref lexical_block_die)
21640 expanded_location xloc;
21641 dw_die_ref imported_die = NULL;
21642 dw_die_ref at_import_die;
21644 if (TREE_CODE (decl) == IMPORTED_DECL)
21646 xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21647 decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21648 gcc_assert (decl);
21650 else
21651 xloc = expand_location (input_location);
21653 if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21655 at_import_die = force_type_die (TREE_TYPE (decl));
21656 /* For namespace N { typedef void T; } using N::T; base_type_die
21657 returns NULL, but DW_TAG_imported_declaration requires
21658 the DW_AT_import tag. Force creation of DW_TAG_typedef. */
21659 if (!at_import_die)
21661 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21662 gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21663 at_import_die = lookup_type_die (TREE_TYPE (decl));
21664 gcc_assert (at_import_die);
21667 else
21669 at_import_die = lookup_decl_die (decl);
21670 if (!at_import_die)
21672 /* If we're trying to avoid duplicate debug info, we may not have
21673 emitted the member decl for this field. Emit it now. */
21674 if (TREE_CODE (decl) == FIELD_DECL)
21676 tree type = DECL_CONTEXT (decl);
21678 if (TYPE_CONTEXT (type)
21679 && TYPE_P (TYPE_CONTEXT (type))
21680 && !should_emit_struct_debug (TYPE_CONTEXT (type),
21681 DINFO_USAGE_DIR_USE))
21682 return;
21683 gen_type_die_for_member (type, decl,
21684 get_context_die (TYPE_CONTEXT (type)));
21686 if (TREE_CODE (decl) == NAMELIST_DECL)
21687 at_import_die = gen_namelist_decl (DECL_NAME (decl),
21688 get_context_die (DECL_CONTEXT (decl)),
21689 NULL_TREE);
21690 else
21691 at_import_die = force_decl_die (decl);
21695 if (TREE_CODE (decl) == NAMESPACE_DECL)
21697 if (dwarf_version >= 3 || !dwarf_strict)
21698 imported_die = new_die (DW_TAG_imported_module,
21699 lexical_block_die,
21700 lexical_block);
21701 else
21702 return;
21704 else
21705 imported_die = new_die (DW_TAG_imported_declaration,
21706 lexical_block_die,
21707 lexical_block);
21709 add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21710 add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21711 if (name)
21712 add_AT_string (imported_die, DW_AT_name,
21713 IDENTIFIER_POINTER (name));
21714 add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21717 /* Output debug information for imported module or decl DECL.
21718 NAME is non-NULL name in context if the decl has been renamed.
21719 CHILD is true if decl is one of the renamed decls as part of
21720 importing whole module. */
21722 static void
21723 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21724 bool child)
21726 /* dw_die_ref at_import_die; */
21727 dw_die_ref scope_die;
21729 if (debug_info_level <= DINFO_LEVEL_TERSE)
21730 return;
21732 gcc_assert (decl);
21734 set_early_dwarf s;
21736 /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21737 We need decl DIE for reference and scope die. First, get DIE for the decl
21738 itself. */
21740 /* Get the scope die for decl context. Use comp_unit_die for global module
21741 or decl. If die is not found for non globals, force new die. */
21742 if (context
21743 && TYPE_P (context)
21744 && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21745 return;
21747 if (!(dwarf_version >= 3 || !dwarf_strict))
21748 return;
21750 scope_die = get_context_die (context);
21752 if (child)
21754 gcc_assert (scope_die->die_child);
21755 gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21756 gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21757 scope_die = scope_die->die_child;
21760 /* OK, now we have DIEs for decl as well as scope. Emit imported die. */
21761 dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21764 /* Output debug information for namelists. */
21766 static dw_die_ref
21767 gen_namelist_decl (tree name, dw_die_ref scope_die, tree item_decls)
21769 dw_die_ref nml_die, nml_item_die, nml_item_ref_die;
21770 tree value;
21771 unsigned i;
21773 if (debug_info_level <= DINFO_LEVEL_TERSE)
21774 return NULL;
21776 gcc_assert (scope_die != NULL);
21777 nml_die = new_die (DW_TAG_namelist, scope_die, NULL);
21778 add_AT_string (nml_die, DW_AT_name, IDENTIFIER_POINTER (name));
21780 /* If there are no item_decls, we have a nondefining namelist, e.g.
21781 with USE association; hence, set DW_AT_declaration. */
21782 if (item_decls == NULL_TREE)
21784 add_AT_flag (nml_die, DW_AT_declaration, 1);
21785 return nml_die;
21788 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (item_decls), i, value)
21790 nml_item_ref_die = lookup_decl_die (value);
21791 if (!nml_item_ref_die)
21792 nml_item_ref_die = force_decl_die (value);
21794 nml_item_die = new_die (DW_TAG_namelist_item, nml_die, NULL);
21795 add_AT_die_ref (nml_item_die, DW_AT_namelist_items, nml_item_ref_die);
21797 return nml_die;
21801 /* Write the debugging output for DECL and return the DIE. */
21803 static void
21804 dwarf2out_decl (tree decl)
21806 dw_die_ref context_die = comp_unit_die ();
21808 switch (TREE_CODE (decl))
21810 case ERROR_MARK:
21811 return;
21813 case FUNCTION_DECL:
21814 /* What we would really like to do here is to filter out all mere
21815 file-scope declarations of file-scope functions which are never
21816 referenced later within this translation unit (and keep all of ones
21817 that *are* referenced later on) but we aren't clairvoyant, so we have
21818 no idea which functions will be referenced in the future (i.e. later
21819 on within the current translation unit). So here we just ignore all
21820 file-scope function declarations which are not also definitions. If
21821 and when the debugger needs to know something about these functions,
21822 it will have to hunt around and find the DWARF information associated
21823 with the definition of the function.
21825 We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21826 nodes represent definitions and which ones represent mere
21827 declarations. We have to check DECL_INITIAL instead. That's because
21828 the C front-end supports some weird semantics for "extern inline"
21829 function definitions. These can get inlined within the current
21830 translation unit (and thus, we need to generate Dwarf info for their
21831 abstract instances so that the Dwarf info for the concrete inlined
21832 instances can have something to refer to) but the compiler never
21833 generates any out-of-lines instances of such things (despite the fact
21834 that they *are* definitions).
21836 The important point is that the C front-end marks these "extern
21837 inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21838 them anyway. Note that the C++ front-end also plays some similar games
21839 for inline function definitions appearing within include files which
21840 also contain `#pragma interface' pragmas.
21842 If we are called from dwarf2out_abstract_function output a DIE
21843 anyway. We can end up here this way with early inlining and LTO
21844 where the inlined function is output in a different LTRANS unit
21845 or not at all. */
21846 if (DECL_INITIAL (decl) == NULL_TREE
21847 && ! DECL_ABSTRACT_P (decl))
21848 return;
21850 /* If we're a nested function, initially use a parent of NULL; if we're
21851 a plain function, this will be fixed up in decls_for_scope. If
21852 we're a method, it will be ignored, since we already have a DIE. */
21853 if (decl_function_context (decl)
21854 /* But if we're in terse mode, we don't care about scope. */
21855 && debug_info_level > DINFO_LEVEL_TERSE)
21856 context_die = NULL;
21857 break;
21859 case VAR_DECL:
21860 /* For local statics lookup proper context die. */
21861 if (local_function_static (decl))
21862 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21864 /* If we are in terse mode, don't generate any DIEs to represent any
21865 variable declarations or definitions. */
21866 if (debug_info_level <= DINFO_LEVEL_TERSE)
21867 return;
21868 break;
21870 case CONST_DECL:
21871 if (debug_info_level <= DINFO_LEVEL_TERSE)
21872 return;
21873 if (!is_fortran () && !is_ada ())
21874 return;
21875 if (TREE_STATIC (decl) && decl_function_context (decl))
21876 context_die = lookup_decl_die (DECL_CONTEXT (decl));
21877 break;
21879 case NAMESPACE_DECL:
21880 case IMPORTED_DECL:
21881 if (debug_info_level <= DINFO_LEVEL_TERSE)
21882 return;
21883 if (lookup_decl_die (decl) != NULL)
21884 return;
21885 break;
21887 case TYPE_DECL:
21888 /* Don't emit stubs for types unless they are needed by other DIEs. */
21889 if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21890 return;
21892 /* Don't bother trying to generate any DIEs to represent any of the
21893 normal built-in types for the language we are compiling. */
21894 if (DECL_IS_BUILTIN (decl))
21895 return;
21897 /* If we are in terse mode, don't generate any DIEs for types. */
21898 if (debug_info_level <= DINFO_LEVEL_TERSE)
21899 return;
21901 /* If we're a function-scope tag, initially use a parent of NULL;
21902 this will be fixed up in decls_for_scope. */
21903 if (decl_function_context (decl))
21904 context_die = NULL;
21906 break;
21908 case NAMELIST_DECL:
21909 break;
21911 default:
21912 return;
21915 gen_decl_die (decl, NULL, context_die);
21917 #ifdef ENABLE_CHECKING
21918 dw_die_ref die = lookup_decl_die (decl);
21919 if (die)
21920 check_die (die);
21921 #endif
21924 /* Write the debugging output for DECL. */
21926 static void
21927 dwarf2out_function_decl (tree decl)
21929 dwarf2out_decl (decl);
21930 call_arg_locations = NULL;
21931 call_arg_loc_last = NULL;
21932 call_site_count = -1;
21933 tail_call_site_count = -1;
21934 decl_loc_table->empty ();
21935 cached_dw_loc_list_table->empty ();
21938 /* Output a marker (i.e. a label) for the beginning of the generated code for
21939 a lexical block. */
21941 static void
21942 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21943 unsigned int blocknum)
21945 switch_to_section (current_function_section ());
21946 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21949 /* Output a marker (i.e. a label) for the end of the generated code for a
21950 lexical block. */
21952 static void
21953 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21955 switch_to_section (current_function_section ());
21956 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21959 /* Returns nonzero if it is appropriate not to emit any debugging
21960 information for BLOCK, because it doesn't contain any instructions.
21962 Don't allow this for blocks with nested functions or local classes
21963 as we would end up with orphans, and in the presence of scheduling
21964 we may end up calling them anyway. */
21966 static bool
21967 dwarf2out_ignore_block (const_tree block)
21969 tree decl;
21970 unsigned int i;
21972 for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21973 if (TREE_CODE (decl) == FUNCTION_DECL
21974 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21975 return 0;
21976 for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21978 decl = BLOCK_NONLOCALIZED_VAR (block, i);
21979 if (TREE_CODE (decl) == FUNCTION_DECL
21980 || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21981 return 0;
21984 return 1;
21987 /* Hash table routines for file_hash. */
21989 bool
21990 dwarf_file_hasher::equal (dwarf_file_data *p1, const char *p2)
21992 return filename_cmp (p1->filename, p2) == 0;
21995 hashval_t
21996 dwarf_file_hasher::hash (dwarf_file_data *p)
21998 return htab_hash_string (p->filename);
22001 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22002 dwarf2out.c) and return its "index". The index of each (known) filename is
22003 just a unique number which is associated with only that one filename. We
22004 need such numbers for the sake of generating labels (in the .debug_sfnames
22005 section) and references to those files numbers (in the .debug_srcinfo
22006 and.debug_macinfo sections). If the filename given as an argument is not
22007 found in our current list, add it to the list and assign it the next
22008 available unique index number. */
22010 static struct dwarf_file_data *
22011 lookup_filename (const char *file_name)
22013 struct dwarf_file_data * created;
22015 if (!file_name)
22016 return NULL;
22018 dwarf_file_data **slot
22019 = file_table->find_slot_with_hash (file_name, htab_hash_string (file_name),
22020 INSERT);
22021 if (*slot)
22022 return *slot;
22024 created = ggc_alloc<dwarf_file_data> ();
22025 created->filename = file_name;
22026 created->emitted_number = 0;
22027 *slot = created;
22028 return created;
22031 /* If the assembler will construct the file table, then translate the compiler
22032 internal file table number into the assembler file table number, and emit
22033 a .file directive if we haven't already emitted one yet. The file table
22034 numbers are different because we prune debug info for unused variables and
22035 types, which may include filenames. */
22037 static int
22038 maybe_emit_file (struct dwarf_file_data * fd)
22040 if (! fd->emitted_number)
22042 if (last_emitted_file)
22043 fd->emitted_number = last_emitted_file->emitted_number + 1;
22044 else
22045 fd->emitted_number = 1;
22046 last_emitted_file = fd;
22048 if (DWARF2_ASM_LINE_DEBUG_INFO)
22050 fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22051 output_quoted_string (asm_out_file,
22052 remap_debug_filename (fd->filename));
22053 fputc ('\n', asm_out_file);
22057 return fd->emitted_number;
22060 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22061 That generation should happen after function debug info has been
22062 generated. The value of the attribute is the constant value of ARG. */
22064 static void
22065 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22067 die_arg_entry entry;
22069 if (!die || !arg)
22070 return;
22072 if (!tmpl_value_parm_die_table)
22073 vec_alloc (tmpl_value_parm_die_table, 32);
22075 entry.die = die;
22076 entry.arg = arg;
22077 vec_safe_push (tmpl_value_parm_die_table, entry);
22080 /* Return TRUE if T is an instance of generic type, FALSE
22081 otherwise. */
22083 static bool
22084 generic_type_p (tree t)
22086 if (t == NULL_TREE || !TYPE_P (t))
22087 return false;
22088 return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22091 /* Schedule the generation of the generic parameter dies for the
22092 instance of generic type T. The proper generation itself is later
22093 done by gen_scheduled_generic_parms_dies. */
22095 static void
22096 schedule_generic_params_dies_gen (tree t)
22098 if (!generic_type_p (t))
22099 return;
22101 if (!generic_type_instances)
22102 vec_alloc (generic_type_instances, 256);
22104 vec_safe_push (generic_type_instances, t);
22107 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22108 by append_entry_to_tmpl_value_parm_die_table. This function must
22109 be called after function DIEs have been generated. */
22111 static void
22112 gen_remaining_tmpl_value_param_die_attribute (void)
22114 if (tmpl_value_parm_die_table)
22116 unsigned i;
22117 die_arg_entry *e;
22119 FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
22120 tree_add_const_value_attribute (e->die, e->arg);
22124 /* Generate generic parameters DIEs for instances of generic types
22125 that have been previously scheduled by
22126 schedule_generic_params_dies_gen. This function must be called
22127 after all the types of the CU have been laid out. */
22129 static void
22130 gen_scheduled_generic_parms_dies (void)
22132 unsigned i;
22133 tree t;
22135 if (!generic_type_instances)
22136 return;
22138 FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
22139 if (COMPLETE_TYPE_P (t))
22140 gen_generic_params_dies (t);
22144 /* Replace DW_AT_name for the decl with name. */
22146 static void
22147 dwarf2out_set_name (tree decl, tree name)
22149 dw_die_ref die;
22150 dw_attr_ref attr;
22151 const char *dname;
22153 die = TYPE_SYMTAB_DIE (decl);
22154 if (!die)
22155 return;
22157 dname = dwarf2_name (name, 0);
22158 if (!dname)
22159 return;
22161 attr = get_AT (die, DW_AT_name);
22162 if (attr)
22164 struct indirect_string_node *node;
22166 node = find_AT_string (dname);
22167 /* replace the string. */
22168 attr->dw_attr_val.v.val_str = node;
22171 else
22172 add_name_attribute (die, dname);
22175 /* True if before or during processing of the first function being emitted. */
22176 static bool in_first_function_p = true;
22177 /* True if loc_note during dwarf2out_var_location call might still be
22178 before first real instruction at address equal to .Ltext0. */
22179 static bool maybe_at_text_label_p = true;
22180 /* One above highest N where .LVLN label might be equal to .Ltext0 label. */
22181 static unsigned int first_loclabel_num_not_at_text_label;
22183 /* Called by the final INSN scan whenever we see a var location. We
22184 use it to drop labels in the right places, and throw the location in
22185 our lookup table. */
22187 static void
22188 dwarf2out_var_location (rtx_insn *loc_note)
22190 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22191 struct var_loc_node *newloc;
22192 rtx_insn *next_real, *next_note;
22193 static const char *last_label;
22194 static const char *last_postcall_label;
22195 static bool last_in_cold_section_p;
22196 static rtx_insn *expected_next_loc_note;
22197 tree decl;
22198 bool var_loc_p;
22200 if (!NOTE_P (loc_note))
22202 if (CALL_P (loc_note))
22204 call_site_count++;
22205 if (SIBLING_CALL_P (loc_note))
22206 tail_call_site_count++;
22208 return;
22211 var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22212 if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22213 return;
22215 /* Optimize processing a large consecutive sequence of location
22216 notes so we don't spend too much time in next_real_insn. If the
22217 next insn is another location note, remember the next_real_insn
22218 calculation for next time. */
22219 next_real = cached_next_real_insn;
22220 if (next_real)
22222 if (expected_next_loc_note != loc_note)
22223 next_real = NULL;
22226 next_note = NEXT_INSN (loc_note);
22227 if (! next_note
22228 || next_note->deleted ()
22229 || ! NOTE_P (next_note)
22230 || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
22231 && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
22232 next_note = NULL;
22234 if (! next_real)
22235 next_real = next_real_insn (loc_note);
22237 if (next_note)
22239 expected_next_loc_note = next_note;
22240 cached_next_real_insn = next_real;
22242 else
22243 cached_next_real_insn = NULL;
22245 /* If there are no instructions which would be affected by this note,
22246 don't do anything. */
22247 if (var_loc_p
22248 && next_real == NULL_RTX
22249 && !NOTE_DURING_CALL_P (loc_note))
22250 return;
22252 if (next_real == NULL_RTX)
22253 next_real = get_last_insn ();
22255 /* If there were any real insns between note we processed last time
22256 and this note (or if it is the first note), clear
22257 last_{,postcall_}label so that they are not reused this time. */
22258 if (last_var_location_insn == NULL_RTX
22259 || last_var_location_insn != next_real
22260 || last_in_cold_section_p != in_cold_section_p)
22262 last_label = NULL;
22263 last_postcall_label = NULL;
22266 if (var_loc_p)
22268 decl = NOTE_VAR_LOCATION_DECL (loc_note);
22269 newloc = add_var_loc_to_decl (decl, loc_note,
22270 NOTE_DURING_CALL_P (loc_note)
22271 ? last_postcall_label : last_label);
22272 if (newloc == NULL)
22273 return;
22275 else
22277 decl = NULL_TREE;
22278 newloc = NULL;
22281 /* If there were no real insns between note we processed last time
22282 and this note, use the label we emitted last time. Otherwise
22283 create a new label and emit it. */
22284 if (last_label == NULL)
22286 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22287 ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22288 loclabel_num++;
22289 last_label = ggc_strdup (loclabel);
22290 /* See if loclabel might be equal to .Ltext0. If yes,
22291 bump first_loclabel_num_not_at_text_label. */
22292 if (!have_multiple_function_sections
22293 && in_first_function_p
22294 && maybe_at_text_label_p)
22296 static rtx_insn *last_start;
22297 rtx_insn *insn;
22298 for (insn = loc_note; insn; insn = previous_insn (insn))
22299 if (insn == last_start)
22300 break;
22301 else if (!NONDEBUG_INSN_P (insn))
22302 continue;
22303 else
22305 rtx body = PATTERN (insn);
22306 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
22307 continue;
22308 /* Inline asm could occupy zero bytes. */
22309 else if (GET_CODE (body) == ASM_INPUT
22310 || asm_noperands (body) >= 0)
22311 continue;
22312 #ifdef HAVE_attr_length
22313 else if (get_attr_min_length (insn) == 0)
22314 continue;
22315 #endif
22316 else
22318 /* Assume insn has non-zero length. */
22319 maybe_at_text_label_p = false;
22320 break;
22323 if (maybe_at_text_label_p)
22325 last_start = loc_note;
22326 first_loclabel_num_not_at_text_label = loclabel_num;
22331 if (!var_loc_p)
22333 struct call_arg_loc_node *ca_loc
22334 = ggc_cleared_alloc<call_arg_loc_node> ();
22335 rtx_insn *prev = prev_real_insn (loc_note);
22336 rtx x;
22337 ca_loc->call_arg_loc_note = loc_note;
22338 ca_loc->next = NULL;
22339 ca_loc->label = last_label;
22340 gcc_assert (prev
22341 && (CALL_P (prev)
22342 || (NONJUMP_INSN_P (prev)
22343 && GET_CODE (PATTERN (prev)) == SEQUENCE
22344 && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22345 if (!CALL_P (prev))
22346 prev = as_a <rtx_sequence *> (PATTERN (prev))->insn (0);
22347 ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22348 x = get_call_rtx_from (PATTERN (prev));
22349 if (x)
22351 x = XEXP (XEXP (x, 0), 0);
22352 if (GET_CODE (x) == SYMBOL_REF
22353 && SYMBOL_REF_DECL (x)
22354 && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22355 ca_loc->symbol_ref = x;
22357 ca_loc->block = insn_scope (prev);
22358 if (call_arg_locations)
22359 call_arg_loc_last->next = ca_loc;
22360 else
22361 call_arg_locations = ca_loc;
22362 call_arg_loc_last = ca_loc;
22364 else if (!NOTE_DURING_CALL_P (loc_note))
22365 newloc->label = last_label;
22366 else
22368 if (!last_postcall_label)
22370 sprintf (loclabel, "%s-1", last_label);
22371 last_postcall_label = ggc_strdup (loclabel);
22373 newloc->label = last_postcall_label;
22376 last_var_location_insn = next_real;
22377 last_in_cold_section_p = in_cold_section_p;
22380 /* Note in one location list that text section has changed. */
22383 var_location_switch_text_section_1 (var_loc_list **slot, void *)
22385 var_loc_list *list = *slot;
22386 if (list->first)
22387 list->last_before_switch
22388 = list->last->next ? list->last->next : list->last;
22389 return 1;
22392 /* Note in all location lists that text section has changed. */
22394 static void
22395 var_location_switch_text_section (void)
22397 if (decl_loc_table == NULL)
22398 return;
22400 decl_loc_table->traverse<void *, var_location_switch_text_section_1> (NULL);
22403 /* Create a new line number table. */
22405 static dw_line_info_table *
22406 new_line_info_table (void)
22408 dw_line_info_table *table;
22410 table = ggc_cleared_alloc<dw_line_info_table_struct> ();
22411 table->file_num = 1;
22412 table->line_num = 1;
22413 table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22415 return table;
22418 /* Lookup the "current" table into which we emit line info, so
22419 that we don't have to do it for every source line. */
22421 static void
22422 set_cur_line_info_table (section *sec)
22424 dw_line_info_table *table;
22426 if (sec == text_section)
22427 table = text_section_line_info;
22428 else if (sec == cold_text_section)
22430 table = cold_text_section_line_info;
22431 if (!table)
22433 cold_text_section_line_info = table = new_line_info_table ();
22434 table->end_label = cold_end_label;
22437 else
22439 const char *end_label;
22441 if (flag_reorder_blocks_and_partition)
22443 if (in_cold_section_p)
22444 end_label = crtl->subsections.cold_section_end_label;
22445 else
22446 end_label = crtl->subsections.hot_section_end_label;
22448 else
22450 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22451 ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22452 current_function_funcdef_no);
22453 end_label = ggc_strdup (label);
22456 table = new_line_info_table ();
22457 table->end_label = end_label;
22459 vec_safe_push (separate_line_info, table);
22462 if (DWARF2_ASM_LINE_DEBUG_INFO)
22463 table->is_stmt = (cur_line_info_table
22464 ? cur_line_info_table->is_stmt
22465 : DWARF_LINE_DEFAULT_IS_STMT_START);
22466 cur_line_info_table = table;
22470 /* We need to reset the locations at the beginning of each
22471 function. We can't do this in the end_function hook, because the
22472 declarations that use the locations won't have been output when
22473 that hook is called. Also compute have_multiple_function_sections here. */
22475 static void
22476 dwarf2out_begin_function (tree fun)
22478 section *sec = function_section (fun);
22480 if (sec != text_section)
22481 have_multiple_function_sections = true;
22483 if (flag_reorder_blocks_and_partition && !cold_text_section)
22485 gcc_assert (current_function_decl == fun);
22486 cold_text_section = unlikely_text_section ();
22487 switch_to_section (cold_text_section);
22488 ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22489 switch_to_section (sec);
22492 dwarf2out_note_section_used ();
22493 call_site_count = 0;
22494 tail_call_site_count = 0;
22496 set_cur_line_info_table (sec);
22499 /* Helper function of dwarf2out_end_function, called only after emitting
22500 the very first function into assembly. Check if some .debug_loc range
22501 might end with a .LVL* label that could be equal to .Ltext0.
22502 In that case we must force using absolute addresses in .debug_loc ranges,
22503 because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
22504 .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
22505 list terminator.
22506 Set have_multiple_function_sections to true in that case and
22507 terminate htab traversal. */
22510 find_empty_loc_ranges_at_text_label (var_loc_list **slot, int)
22512 var_loc_list *entry = *slot;
22513 struct var_loc_node *node;
22515 node = entry->first;
22516 if (node && node->next && node->next->label)
22518 unsigned int i;
22519 const char *label = node->next->label;
22520 char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
22522 for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
22524 ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
22525 if (strcmp (label, loclabel) == 0)
22527 have_multiple_function_sections = true;
22528 return 0;
22532 return 1;
22535 /* Hook called after emitting a function into assembly.
22536 This does something only for the very first function emitted. */
22538 static void
22539 dwarf2out_end_function (unsigned int)
22541 if (in_first_function_p
22542 && !have_multiple_function_sections
22543 && first_loclabel_num_not_at_text_label
22544 && decl_loc_table)
22545 decl_loc_table->traverse<int, find_empty_loc_ranges_at_text_label> (0);
22546 in_first_function_p = false;
22547 maybe_at_text_label_p = false;
22550 /* Temporary holder for dwarf2out_register_main_translation_unit. Used to let
22551 front-ends register a translation unit even before dwarf2out_init is
22552 called. */
22553 static tree main_translation_unit = NULL_TREE;
22555 /* Hook called by front-ends after they built their main translation unit.
22556 Associate comp_unit_die to UNIT. */
22558 static void
22559 dwarf2out_register_main_translation_unit (tree unit)
22561 gcc_assert (TREE_CODE (unit) == TRANSLATION_UNIT_DECL
22562 && main_translation_unit == NULL_TREE);
22563 main_translation_unit = unit;
22564 /* If dwarf2out_init has not been called yet, it will perform the association
22565 itself looking at main_translation_unit. */
22566 if (decl_die_table != NULL)
22567 equate_decl_number_to_die (unit, comp_unit_die ());
22570 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE. */
22572 static void
22573 push_dw_line_info_entry (dw_line_info_table *table,
22574 enum dw_line_info_opcode opcode, unsigned int val)
22576 dw_line_info_entry e;
22577 e.opcode = opcode;
22578 e.val = val;
22579 vec_safe_push (table->entries, e);
22582 /* Output a label to mark the beginning of a source code line entry
22583 and record information relating to this source line, in
22584 'line_info_table' for later output of the .debug_line section. */
22585 /* ??? The discriminator parameter ought to be unsigned. */
22587 static void
22588 dwarf2out_source_line (unsigned int line, const char *filename,
22589 int discriminator, bool is_stmt)
22591 unsigned int file_num;
22592 dw_line_info_table *table;
22594 if (debug_info_level < DINFO_LEVEL_TERSE || line == 0)
22595 return;
22597 /* The discriminator column was added in dwarf4. Simplify the below
22598 by simply removing it if we're not supposed to output it. */
22599 if (dwarf_version < 4 && dwarf_strict)
22600 discriminator = 0;
22602 table = cur_line_info_table;
22603 file_num = maybe_emit_file (lookup_filename (filename));
22605 /* ??? TODO: Elide duplicate line number entries. Traditionally,
22606 the debugger has used the second (possibly duplicate) line number
22607 at the beginning of the function to mark the end of the prologue.
22608 We could eliminate any other duplicates within the function. For
22609 Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22610 that second line number entry. */
22611 /* Recall that this end-of-prologue indication is *not* the same thing
22612 as the end_prologue debug hook. The NOTE_INSN_PROLOGUE_END note,
22613 to which the hook corresponds, follows the last insn that was
22614 emitted by gen_prologue. What we need is to precede the first insn
22615 that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22616 insn that corresponds to something the user wrote. These may be
22617 very different locations once scheduling is enabled. */
22619 if (0 && file_num == table->file_num
22620 && line == table->line_num
22621 && discriminator == table->discrim_num
22622 && is_stmt == table->is_stmt)
22623 return;
22625 switch_to_section (current_function_section ());
22627 /* If requested, emit something human-readable. */
22628 if (flag_debug_asm)
22629 fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22631 if (DWARF2_ASM_LINE_DEBUG_INFO)
22633 /* Emit the .loc directive understood by GNU as. */
22634 /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
22635 file_num, line, is_stmt, discriminator */
22636 fputs ("\t.loc ", asm_out_file);
22637 fprint_ul (asm_out_file, file_num);
22638 putc (' ', asm_out_file);
22639 fprint_ul (asm_out_file, line);
22640 putc (' ', asm_out_file);
22641 putc ('0', asm_out_file);
22643 if (is_stmt != table->is_stmt)
22645 fputs (" is_stmt ", asm_out_file);
22646 putc (is_stmt ? '1' : '0', asm_out_file);
22648 if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22650 gcc_assert (discriminator > 0);
22651 fputs (" discriminator ", asm_out_file);
22652 fprint_ul (asm_out_file, (unsigned long) discriminator);
22654 putc ('\n', asm_out_file);
22656 else
22658 unsigned int label_num = ++line_info_label_num;
22660 targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22662 push_dw_line_info_entry (table, LI_set_address, label_num);
22663 if (file_num != table->file_num)
22664 push_dw_line_info_entry (table, LI_set_file, file_num);
22665 if (discriminator != table->discrim_num)
22666 push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22667 if (is_stmt != table->is_stmt)
22668 push_dw_line_info_entry (table, LI_negate_stmt, 0);
22669 push_dw_line_info_entry (table, LI_set_line, line);
22672 table->file_num = file_num;
22673 table->line_num = line;
22674 table->discrim_num = discriminator;
22675 table->is_stmt = is_stmt;
22676 table->in_use = true;
22679 /* Record the beginning of a new source file. */
22681 static void
22682 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22684 if (flag_eliminate_dwarf2_dups)
22686 /* Record the beginning of the file for break_out_includes. */
22687 dw_die_ref bincl_die;
22689 bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22690 add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22693 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22695 macinfo_entry e;
22696 e.code = DW_MACINFO_start_file;
22697 e.lineno = lineno;
22698 e.info = ggc_strdup (filename);
22699 vec_safe_push (macinfo_table, e);
22703 /* Record the end of a source file. */
22705 static void
22706 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22708 if (flag_eliminate_dwarf2_dups)
22709 /* Record the end of the file for break_out_includes. */
22710 new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22712 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22714 macinfo_entry e;
22715 e.code = DW_MACINFO_end_file;
22716 e.lineno = lineno;
22717 e.info = NULL;
22718 vec_safe_push (macinfo_table, e);
22722 /* Called from debug_define in toplev.c. The `buffer' parameter contains
22723 the tail part of the directive line, i.e. the part which is past the
22724 initial whitespace, #, whitespace, directive-name, whitespace part. */
22726 static void
22727 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22728 const char *buffer ATTRIBUTE_UNUSED)
22730 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22732 macinfo_entry e;
22733 /* Insert a dummy first entry to be able to optimize the whole
22734 predefined macro block using DW_MACRO_GNU_transparent_include. */
22735 if (macinfo_table->is_empty () && lineno <= 1)
22737 e.code = 0;
22738 e.lineno = 0;
22739 e.info = NULL;
22740 vec_safe_push (macinfo_table, e);
22742 e.code = DW_MACINFO_define;
22743 e.lineno = lineno;
22744 e.info = ggc_strdup (buffer);
22745 vec_safe_push (macinfo_table, e);
22749 /* Called from debug_undef in toplev.c. The `buffer' parameter contains
22750 the tail part of the directive line, i.e. the part which is past the
22751 initial whitespace, #, whitespace, directive-name, whitespace part. */
22753 static void
22754 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22755 const char *buffer ATTRIBUTE_UNUSED)
22757 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22759 macinfo_entry e;
22760 /* Insert a dummy first entry to be able to optimize the whole
22761 predefined macro block using DW_MACRO_GNU_transparent_include. */
22762 if (macinfo_table->is_empty () && lineno <= 1)
22764 e.code = 0;
22765 e.lineno = 0;
22766 e.info = NULL;
22767 vec_safe_push (macinfo_table, e);
22769 e.code = DW_MACINFO_undef;
22770 e.lineno = lineno;
22771 e.info = ggc_strdup (buffer);
22772 vec_safe_push (macinfo_table, e);
22776 /* Helpers to manipulate hash table of CUs. */
22778 struct macinfo_entry_hasher : nofree_ptr_hash <macinfo_entry>
22780 static inline hashval_t hash (const macinfo_entry *);
22781 static inline bool equal (const macinfo_entry *, const macinfo_entry *);
22784 inline hashval_t
22785 macinfo_entry_hasher::hash (const macinfo_entry *entry)
22787 return htab_hash_string (entry->info);
22790 inline bool
22791 macinfo_entry_hasher::equal (const macinfo_entry *entry1,
22792 const macinfo_entry *entry2)
22794 return !strcmp (entry1->info, entry2->info);
22797 typedef hash_table<macinfo_entry_hasher> macinfo_hash_type;
22799 /* Output a single .debug_macinfo entry. */
22801 static void
22802 output_macinfo_op (macinfo_entry *ref)
22804 int file_num;
22805 size_t len;
22806 struct indirect_string_node *node;
22807 char label[MAX_ARTIFICIAL_LABEL_BYTES];
22808 struct dwarf_file_data *fd;
22810 switch (ref->code)
22812 case DW_MACINFO_start_file:
22813 fd = lookup_filename (ref->info);
22814 file_num = maybe_emit_file (fd);
22815 dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22816 dw2_asm_output_data_uleb128 (ref->lineno,
22817 "Included from line number %lu",
22818 (unsigned long) ref->lineno);
22819 dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22820 break;
22821 case DW_MACINFO_end_file:
22822 dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22823 break;
22824 case DW_MACINFO_define:
22825 case DW_MACINFO_undef:
22826 len = strlen (ref->info) + 1;
22827 if (!dwarf_strict
22828 && len > DWARF_OFFSET_SIZE
22829 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
22830 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
22832 ref->code = ref->code == DW_MACINFO_define
22833 ? DW_MACRO_GNU_define_indirect
22834 : DW_MACRO_GNU_undef_indirect;
22835 output_macinfo_op (ref);
22836 return;
22838 dw2_asm_output_data (1, ref->code,
22839 ref->code == DW_MACINFO_define
22840 ? "Define macro" : "Undefine macro");
22841 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22842 (unsigned long) ref->lineno);
22843 dw2_asm_output_nstring (ref->info, -1, "The macro");
22844 break;
22845 case DW_MACRO_GNU_define_indirect:
22846 case DW_MACRO_GNU_undef_indirect:
22847 node = find_AT_string (ref->info);
22848 gcc_assert (node
22849 && ((node->form == DW_FORM_strp)
22850 || (node->form == DW_FORM_GNU_str_index)));
22851 dw2_asm_output_data (1, ref->code,
22852 ref->code == DW_MACRO_GNU_define_indirect
22853 ? "Define macro indirect"
22854 : "Undefine macro indirect");
22855 dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22856 (unsigned long) ref->lineno);
22857 if (node->form == DW_FORM_strp)
22858 dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
22859 debug_str_section, "The macro: \"%s\"",
22860 ref->info);
22861 else
22862 dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
22863 ref->info);
22864 break;
22865 case DW_MACRO_GNU_transparent_include:
22866 dw2_asm_output_data (1, ref->code, "Transparent include");
22867 ASM_GENERATE_INTERNAL_LABEL (label,
22868 DEBUG_MACRO_SECTION_LABEL, ref->lineno);
22869 dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
22870 break;
22871 default:
22872 fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22873 ASM_COMMENT_START, (unsigned long) ref->code);
22874 break;
22878 /* Attempt to make a sequence of define/undef macinfo ops shareable with
22879 other compilation unit .debug_macinfo sections. IDX is the first
22880 index of a define/undef, return the number of ops that should be
22881 emitted in a comdat .debug_macinfo section and emit
22882 a DW_MACRO_GNU_transparent_include entry referencing it.
22883 If the define/undef entry should be emitted normally, return 0. */
22885 static unsigned
22886 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
22887 macinfo_hash_type **macinfo_htab)
22889 macinfo_entry *first, *second, *cur, *inc;
22890 char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
22891 unsigned char checksum[16];
22892 struct md5_ctx ctx;
22893 char *grp_name, *tail;
22894 const char *base;
22895 unsigned int i, count, encoded_filename_len, linebuf_len;
22896 macinfo_entry **slot;
22898 first = &(*macinfo_table)[idx];
22899 second = &(*macinfo_table)[idx + 1];
22901 /* Optimize only if there are at least two consecutive define/undef ops,
22902 and either all of them are before first DW_MACINFO_start_file
22903 with lineno {0,1} (i.e. predefined macro block), or all of them are
22904 in some included header file. */
22905 if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
22906 return 0;
22907 if (vec_safe_is_empty (files))
22909 if (first->lineno > 1 || second->lineno > 1)
22910 return 0;
22912 else if (first->lineno == 0)
22913 return 0;
22915 /* Find the last define/undef entry that can be grouped together
22916 with first and at the same time compute md5 checksum of their
22917 codes, linenumbers and strings. */
22918 md5_init_ctx (&ctx);
22919 for (i = idx; macinfo_table->iterate (i, &cur); i++)
22920 if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
22921 break;
22922 else if (vec_safe_is_empty (files) && cur->lineno > 1)
22923 break;
22924 else
22926 unsigned char code = cur->code;
22927 md5_process_bytes (&code, 1, &ctx);
22928 checksum_uleb128 (cur->lineno, &ctx);
22929 md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
22931 md5_finish_ctx (&ctx, checksum);
22932 count = i - idx;
22934 /* From the containing include filename (if any) pick up just
22935 usable characters from its basename. */
22936 if (vec_safe_is_empty (files))
22937 base = "";
22938 else
22939 base = lbasename (files->last ().info);
22940 for (encoded_filename_len = 0, i = 0; base[i]; i++)
22941 if (ISIDNUM (base[i]) || base[i] == '.')
22942 encoded_filename_len++;
22943 /* Count . at the end. */
22944 if (encoded_filename_len)
22945 encoded_filename_len++;
22947 sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
22948 linebuf_len = strlen (linebuf);
22950 /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum> */
22951 grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
22952 + 16 * 2 + 1);
22953 memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
22954 tail = grp_name + 4;
22955 if (encoded_filename_len)
22957 for (i = 0; base[i]; i++)
22958 if (ISIDNUM (base[i]) || base[i] == '.')
22959 *tail++ = base[i];
22960 *tail++ = '.';
22962 memcpy (tail, linebuf, linebuf_len);
22963 tail += linebuf_len;
22964 *tail++ = '.';
22965 for (i = 0; i < 16; i++)
22966 sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
22968 /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
22969 in the empty vector entry before the first define/undef. */
22970 inc = &(*macinfo_table)[idx - 1];
22971 inc->code = DW_MACRO_GNU_transparent_include;
22972 inc->lineno = 0;
22973 inc->info = ggc_strdup (grp_name);
22974 if (!*macinfo_htab)
22975 *macinfo_htab = new macinfo_hash_type (10);
22976 /* Avoid emitting duplicates. */
22977 slot = (*macinfo_htab)->find_slot (inc, INSERT);
22978 if (*slot != NULL)
22980 inc->code = 0;
22981 inc->info = NULL;
22982 /* If such an entry has been used before, just emit
22983 a DW_MACRO_GNU_transparent_include op. */
22984 inc = *slot;
22985 output_macinfo_op (inc);
22986 /* And clear all macinfo_entry in the range to avoid emitting them
22987 in the second pass. */
22988 for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
22990 cur->code = 0;
22991 cur->info = NULL;
22994 else
22996 *slot = inc;
22997 inc->lineno = (*macinfo_htab)->elements ();
22998 output_macinfo_op (inc);
23000 return count;
23003 /* Save any strings needed by the macinfo table in the debug str
23004 table. All strings must be collected into the table by the time
23005 index_string is called. */
23007 static void
23008 save_macinfo_strings (void)
23010 unsigned len;
23011 unsigned i;
23012 macinfo_entry *ref;
23014 for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
23016 switch (ref->code)
23018 /* Match the logic in output_macinfo_op to decide on
23019 indirect strings. */
23020 case DW_MACINFO_define:
23021 case DW_MACINFO_undef:
23022 len = strlen (ref->info) + 1;
23023 if (!dwarf_strict
23024 && len > DWARF_OFFSET_SIZE
23025 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
23026 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
23027 set_indirect_string (find_AT_string (ref->info));
23028 break;
23029 case DW_MACRO_GNU_define_indirect:
23030 case DW_MACRO_GNU_undef_indirect:
23031 set_indirect_string (find_AT_string (ref->info));
23032 break;
23033 default:
23034 break;
23039 /* Output macinfo section(s). */
23041 static void
23042 output_macinfo (void)
23044 unsigned i;
23045 unsigned long length = vec_safe_length (macinfo_table);
23046 macinfo_entry *ref;
23047 vec<macinfo_entry, va_gc> *files = NULL;
23048 macinfo_hash_type *macinfo_htab = NULL;
23050 if (! length)
23051 return;
23053 /* output_macinfo* uses these interchangeably. */
23054 gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
23055 && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
23056 && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
23057 && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
23059 /* For .debug_macro emit the section header. */
23060 if (!dwarf_strict)
23062 dw2_asm_output_data (2, 4, "DWARF macro version number");
23063 if (DWARF_OFFSET_SIZE == 8)
23064 dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
23065 else
23066 dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
23067 dw2_asm_output_offset (DWARF_OFFSET_SIZE,
23068 (!dwarf_split_debug_info ? debug_line_section_label
23069 : debug_skeleton_line_section_label),
23070 debug_line_section, NULL);
23073 /* In the first loop, it emits the primary .debug_macinfo section
23074 and after each emitted op the macinfo_entry is cleared.
23075 If a longer range of define/undef ops can be optimized using
23076 DW_MACRO_GNU_transparent_include, the
23077 DW_MACRO_GNU_transparent_include op is emitted and kept in
23078 the vector before the first define/undef in the range and the
23079 whole range of define/undef ops is not emitted and kept. */
23080 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23082 switch (ref->code)
23084 case DW_MACINFO_start_file:
23085 vec_safe_push (files, *ref);
23086 break;
23087 case DW_MACINFO_end_file:
23088 if (!vec_safe_is_empty (files))
23089 files->pop ();
23090 break;
23091 case DW_MACINFO_define:
23092 case DW_MACINFO_undef:
23093 if (!dwarf_strict
23094 && HAVE_COMDAT_GROUP
23095 && vec_safe_length (files) != 1
23096 && i > 0
23097 && i + 1 < length
23098 && (*macinfo_table)[i - 1].code == 0)
23100 unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
23101 if (count)
23103 i += count - 1;
23104 continue;
23107 break;
23108 case 0:
23109 /* A dummy entry may be inserted at the beginning to be able
23110 to optimize the whole block of predefined macros. */
23111 if (i == 0)
23112 continue;
23113 default:
23114 break;
23116 output_macinfo_op (ref);
23117 ref->info = NULL;
23118 ref->code = 0;
23121 if (!macinfo_htab)
23122 return;
23124 delete macinfo_htab;
23125 macinfo_htab = NULL;
23127 /* If any DW_MACRO_GNU_transparent_include were used, on those
23128 DW_MACRO_GNU_transparent_include entries terminate the
23129 current chain and switch to a new comdat .debug_macinfo
23130 section and emit the define/undef entries within it. */
23131 for (i = 0; macinfo_table->iterate (i, &ref); i++)
23132 switch (ref->code)
23134 case 0:
23135 continue;
23136 case DW_MACRO_GNU_transparent_include:
23138 char label[MAX_ARTIFICIAL_LABEL_BYTES];
23139 tree comdat_key = get_identifier (ref->info);
23140 /* Terminate the previous .debug_macinfo section. */
23141 dw2_asm_output_data (1, 0, "End compilation unit");
23142 targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
23143 SECTION_DEBUG
23144 | SECTION_LINKONCE,
23145 comdat_key);
23146 ASM_GENERATE_INTERNAL_LABEL (label,
23147 DEBUG_MACRO_SECTION_LABEL,
23148 ref->lineno);
23149 ASM_OUTPUT_LABEL (asm_out_file, label);
23150 ref->code = 0;
23151 ref->info = NULL;
23152 dw2_asm_output_data (2, 4, "DWARF macro version number");
23153 if (DWARF_OFFSET_SIZE == 8)
23154 dw2_asm_output_data (1, 1, "Flags: 64-bit");
23155 else
23156 dw2_asm_output_data (1, 0, "Flags: 32-bit");
23158 break;
23159 case DW_MACINFO_define:
23160 case DW_MACINFO_undef:
23161 output_macinfo_op (ref);
23162 ref->code = 0;
23163 ref->info = NULL;
23164 break;
23165 default:
23166 gcc_unreachable ();
23170 /* Set up for Dwarf output at the start of compilation. */
23172 static void
23173 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23175 /* This option is currently broken, see (PR53118 and PR46102). */
23176 if (flag_eliminate_dwarf2_dups
23177 && strstr (lang_hooks.name, "C++"))
23179 warning (0, "-feliminate-dwarf2-dups is broken for C++, ignoring");
23180 flag_eliminate_dwarf2_dups = 0;
23183 /* Allocate the file_table. */
23184 file_table = hash_table<dwarf_file_hasher>::create_ggc (50);
23186 /* Allocate the decl_die_table. */
23187 decl_die_table = hash_table<decl_die_hasher>::create_ggc (10);
23189 /* Allocate the decl_loc_table. */
23190 decl_loc_table = hash_table<decl_loc_hasher>::create_ggc (10);
23192 /* Allocate the cached_dw_loc_list_table. */
23193 cached_dw_loc_list_table = hash_table<dw_loc_list_hasher>::create_ggc (10);
23195 /* Allocate the initial hunk of the decl_scope_table. */
23196 vec_alloc (decl_scope_table, 256);
23198 /* Allocate the initial hunk of the abbrev_die_table. */
23199 abbrev_die_table = ggc_cleared_vec_alloc<dw_die_ref>
23200 (ABBREV_DIE_TABLE_INCREMENT);
23201 abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23202 /* Zero-th entry is allocated, but unused. */
23203 abbrev_die_table_in_use = 1;
23205 /* Allocate the pubtypes and pubnames vectors. */
23206 vec_alloc (pubname_table, 32);
23207 vec_alloc (pubtype_table, 32);
23209 vec_alloc (incomplete_types, 64);
23211 vec_alloc (used_rtx_array, 32);
23213 if (!dwarf_split_debug_info)
23215 debug_info_section = get_section (DEBUG_INFO_SECTION,
23216 SECTION_DEBUG, NULL);
23217 debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23218 SECTION_DEBUG, NULL);
23219 debug_loc_section = get_section (DEBUG_LOC_SECTION,
23220 SECTION_DEBUG, NULL);
23222 else
23224 debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
23225 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23226 debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
23227 SECTION_DEBUG | SECTION_EXCLUDE,
23228 NULL);
23229 debug_addr_section = get_section (DEBUG_ADDR_SECTION,
23230 SECTION_DEBUG, NULL);
23231 debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
23232 SECTION_DEBUG, NULL);
23233 debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23234 SECTION_DEBUG, NULL);
23235 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
23236 DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
23238 /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
23239 the main .o, but the skeleton_line goes into the split off dwo. */
23240 debug_skeleton_line_section
23241 = get_section (DEBUG_DWO_LINE_SECTION,
23242 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23243 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
23244 DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
23245 debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
23246 SECTION_DEBUG | SECTION_EXCLUDE,
23247 NULL);
23248 ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
23249 DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
23250 debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
23251 SECTION_DEBUG | SECTION_EXCLUDE, NULL);
23252 debug_str_dwo_section = get_section (DEBUG_STR_DWO_SECTION,
23253 DEBUG_STR_DWO_SECTION_FLAGS, NULL);
23255 debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23256 SECTION_DEBUG, NULL);
23257 debug_macinfo_section = get_section (dwarf_strict
23258 ? DEBUG_MACINFO_SECTION
23259 : DEBUG_MACRO_SECTION,
23260 DEBUG_MACRO_SECTION_FLAGS, NULL);
23261 debug_line_section = get_section (DEBUG_LINE_SECTION,
23262 SECTION_DEBUG, NULL);
23263 debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23264 SECTION_DEBUG, NULL);
23265 debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23266 SECTION_DEBUG, NULL);
23267 debug_str_section = get_section (DEBUG_STR_SECTION,
23268 DEBUG_STR_SECTION_FLAGS, NULL);
23269 debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23270 SECTION_DEBUG, NULL);
23271 debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23272 SECTION_DEBUG, NULL);
23274 ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23275 ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23276 DEBUG_ABBREV_SECTION_LABEL, 0);
23277 ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23278 ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23279 COLD_TEXT_SECTION_LABEL, 0);
23280 ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23282 ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23283 DEBUG_INFO_SECTION_LABEL, 0);
23284 ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23285 DEBUG_LINE_SECTION_LABEL, 0);
23286 ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23287 DEBUG_RANGES_SECTION_LABEL, 0);
23288 ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
23289 DEBUG_ADDR_SECTION_LABEL, 0);
23290 ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23291 dwarf_strict
23292 ? DEBUG_MACINFO_SECTION_LABEL
23293 : DEBUG_MACRO_SECTION_LABEL, 0);
23294 ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
23296 if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23297 vec_alloc (macinfo_table, 64);
23299 switch_to_section (text_section);
23300 ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23302 /* Make sure the line number table for .text always exists. */
23303 text_section_line_info = new_line_info_table ();
23304 text_section_line_info->end_label = text_end_label;
23306 /* If front-ends already registered a main translation unit but we were not
23307 ready to perform the association, do this now. */
23308 if (main_translation_unit != NULL_TREE)
23309 equate_decl_number_to_die (main_translation_unit, comp_unit_die ());
23312 /* Called before compile () starts outputtting functions, variables
23313 and toplevel asms into assembly. */
23315 static void
23316 dwarf2out_assembly_start (void)
23318 if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23319 && dwarf2out_do_cfi_asm ()
23320 && (!(flag_unwind_tables || flag_exceptions)
23321 || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23322 fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23325 /* A helper function for dwarf2out_finish called through
23326 htab_traverse. Assign a string its index. All strings must be
23327 collected into the table by the time index_string is called,
23328 because the indexing code relies on htab_traverse to traverse nodes
23329 in the same order for each run. */
23332 index_string (indirect_string_node **h, unsigned int *index)
23334 indirect_string_node *node = *h;
23336 find_string_form (node);
23337 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23339 gcc_assert (node->index == NO_INDEX_ASSIGNED);
23340 node->index = *index;
23341 *index += 1;
23343 return 1;
23346 /* A helper function for output_indirect_strings called through
23347 htab_traverse. Output the offset to a string and update the
23348 current offset. */
23351 output_index_string_offset (indirect_string_node **h, unsigned int *offset)
23353 indirect_string_node *node = *h;
23355 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23357 /* Assert that this node has been assigned an index. */
23358 gcc_assert (node->index != NO_INDEX_ASSIGNED
23359 && node->index != NOT_INDEXED);
23360 dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
23361 "indexed string 0x%x: %s", node->index, node->str);
23362 *offset += strlen (node->str) + 1;
23364 return 1;
23367 /* A helper function for dwarf2out_finish called through
23368 htab_traverse. Output the indexed string. */
23371 output_index_string (indirect_string_node **h, unsigned int *cur_idx)
23373 struct indirect_string_node *node = *h;
23375 if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
23377 /* Assert that the strings are output in the same order as their
23378 indexes were assigned. */
23379 gcc_assert (*cur_idx == node->index);
23380 assemble_string (node->str, strlen (node->str) + 1);
23381 *cur_idx += 1;
23383 return 1;
23386 /* A helper function for dwarf2out_finish called through
23387 htab_traverse. Emit one queued .debug_str string. */
23390 output_indirect_string (indirect_string_node **h, void *)
23392 struct indirect_string_node *node = *h;
23394 node->form = find_string_form (node);
23395 if (node->form == DW_FORM_strp && node->refcount > 0)
23397 ASM_OUTPUT_LABEL (asm_out_file, node->label);
23398 assemble_string (node->str, strlen (node->str) + 1);
23401 return 1;
23404 /* Output the indexed string table. */
23406 static void
23407 output_indirect_strings (void)
23409 switch_to_section (debug_str_section);
23410 if (!dwarf_split_debug_info)
23411 debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23412 else
23414 unsigned int offset = 0;
23415 unsigned int cur_idx = 0;
23417 skeleton_debug_str_hash->traverse<void *, output_indirect_string> (NULL);
23419 switch_to_section (debug_str_offsets_section);
23420 debug_str_hash->traverse_noresize
23421 <unsigned int *, output_index_string_offset> (&offset);
23422 switch_to_section (debug_str_dwo_section);
23423 debug_str_hash->traverse_noresize<unsigned int *, output_index_string>
23424 (&cur_idx);
23428 /* Callback for htab_traverse to assign an index to an entry in the
23429 table, and to write that entry to the .debug_addr section. */
23432 output_addr_table_entry (addr_table_entry **slot, unsigned int *cur_index)
23434 addr_table_entry *entry = *slot;
23436 if (entry->refcount == 0)
23438 gcc_assert (entry->index == NO_INDEX_ASSIGNED
23439 || entry->index == NOT_INDEXED);
23440 return 1;
23443 gcc_assert (entry->index == *cur_index);
23444 (*cur_index)++;
23446 switch (entry->kind)
23448 case ate_kind_rtx:
23449 dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
23450 "0x%x", entry->index);
23451 break;
23452 case ate_kind_rtx_dtprel:
23453 gcc_assert (targetm.asm_out.output_dwarf_dtprel);
23454 targetm.asm_out.output_dwarf_dtprel (asm_out_file,
23455 DWARF2_ADDR_SIZE,
23456 entry->addr.rtl);
23457 fputc ('\n', asm_out_file);
23458 break;
23459 case ate_kind_label:
23460 dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
23461 "0x%x", entry->index);
23462 break;
23463 default:
23464 gcc_unreachable ();
23466 return 1;
23469 /* Produce the .debug_addr section. */
23471 static void
23472 output_addr_table (void)
23474 unsigned int index = 0;
23475 if (addr_index_table == NULL || addr_index_table->size () == 0)
23476 return;
23478 switch_to_section (debug_addr_section);
23479 addr_index_table
23480 ->traverse_noresize<unsigned int *, output_addr_table_entry> (&index);
23483 #if ENABLE_ASSERT_CHECKING
23484 /* Verify that all marks are clear. */
23486 static void
23487 verify_marks_clear (dw_die_ref die)
23489 dw_die_ref c;
23491 gcc_assert (! die->die_mark);
23492 FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23494 #endif /* ENABLE_ASSERT_CHECKING */
23496 /* Clear the marks for a die and its children.
23497 Be cool if the mark isn't set. */
23499 static void
23500 prune_unmark_dies (dw_die_ref die)
23502 dw_die_ref c;
23504 if (die->die_mark)
23505 die->die_mark = 0;
23506 FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23509 /* Given DIE that we're marking as used, find any other dies
23510 it references as attributes and mark them as used. */
23512 static void
23513 prune_unused_types_walk_attribs (dw_die_ref die)
23515 dw_attr_ref a;
23516 unsigned ix;
23518 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23520 if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23522 /* A reference to another DIE.
23523 Make sure that it will get emitted.
23524 If it was broken out into a comdat group, don't follow it. */
23525 if (! AT_ref (a)->comdat_type_p
23526 || a->dw_attr == DW_AT_specification)
23527 prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23529 /* Set the string's refcount to 0 so that prune_unused_types_mark
23530 accounts properly for it. */
23531 if (AT_class (a) == dw_val_class_str)
23532 a->dw_attr_val.v.val_str->refcount = 0;
23536 /* Mark the generic parameters and arguments children DIEs of DIE. */
23538 static void
23539 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23541 dw_die_ref c;
23543 if (die == NULL || die->die_child == NULL)
23544 return;
23545 c = die->die_child;
23548 if (is_template_parameter (c))
23549 prune_unused_types_mark (c, 1);
23550 c = c->die_sib;
23551 } while (c && c != die->die_child);
23554 /* Mark DIE as being used. If DOKIDS is true, then walk down
23555 to DIE's children. */
23557 static void
23558 prune_unused_types_mark (dw_die_ref die, int dokids)
23560 dw_die_ref c;
23562 if (die->die_mark == 0)
23564 /* We haven't done this node yet. Mark it as used. */
23565 die->die_mark = 1;
23566 /* If this is the DIE of a generic type instantiation,
23567 mark the children DIEs that describe its generic parms and
23568 args. */
23569 prune_unused_types_mark_generic_parms_dies (die);
23571 /* We also have to mark its parents as used.
23572 (But we don't want to mark our parent's kids due to this,
23573 unless it is a class.) */
23574 if (die->die_parent)
23575 prune_unused_types_mark (die->die_parent,
23576 class_scope_p (die->die_parent));
23578 /* Mark any referenced nodes. */
23579 prune_unused_types_walk_attribs (die);
23581 /* If this node is a specification,
23582 also mark the definition, if it exists. */
23583 if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23584 prune_unused_types_mark (die->die_definition, 1);
23587 if (dokids && die->die_mark != 2)
23589 /* We need to walk the children, but haven't done so yet.
23590 Remember that we've walked the kids. */
23591 die->die_mark = 2;
23593 /* If this is an array type, we need to make sure our
23594 kids get marked, even if they're types. If we're
23595 breaking out types into comdat sections, do this
23596 for all type definitions. */
23597 if (die->die_tag == DW_TAG_array_type
23598 || (use_debug_types
23599 && is_type_die (die) && ! is_declaration_die (die)))
23600 FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23601 else
23602 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23606 /* For local classes, look if any static member functions were emitted
23607 and if so, mark them. */
23609 static void
23610 prune_unused_types_walk_local_classes (dw_die_ref die)
23612 dw_die_ref c;
23614 if (die->die_mark == 2)
23615 return;
23617 switch (die->die_tag)
23619 case DW_TAG_structure_type:
23620 case DW_TAG_union_type:
23621 case DW_TAG_class_type:
23622 break;
23624 case DW_TAG_subprogram:
23625 if (!get_AT_flag (die, DW_AT_declaration)
23626 || die->die_definition != NULL)
23627 prune_unused_types_mark (die, 1);
23628 return;
23630 default:
23631 return;
23634 /* Mark children. */
23635 FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23638 /* Walk the tree DIE and mark types that we actually use. */
23640 static void
23641 prune_unused_types_walk (dw_die_ref die)
23643 dw_die_ref c;
23645 /* Don't do anything if this node is already marked and
23646 children have been marked as well. */
23647 if (die->die_mark == 2)
23648 return;
23650 switch (die->die_tag)
23652 case DW_TAG_structure_type:
23653 case DW_TAG_union_type:
23654 case DW_TAG_class_type:
23655 if (die->die_perennial_p)
23656 break;
23658 for (c = die->die_parent; c; c = c->die_parent)
23659 if (c->die_tag == DW_TAG_subprogram)
23660 break;
23662 /* Finding used static member functions inside of classes
23663 is needed just for local classes, because for other classes
23664 static member function DIEs with DW_AT_specification
23665 are emitted outside of the DW_TAG_*_type. If we ever change
23666 it, we'd need to call this even for non-local classes. */
23667 if (c)
23668 prune_unused_types_walk_local_classes (die);
23670 /* It's a type node --- don't mark it. */
23671 return;
23673 case DW_TAG_const_type:
23674 case DW_TAG_packed_type:
23675 case DW_TAG_pointer_type:
23676 case DW_TAG_reference_type:
23677 case DW_TAG_rvalue_reference_type:
23678 case DW_TAG_volatile_type:
23679 case DW_TAG_typedef:
23680 case DW_TAG_array_type:
23681 case DW_TAG_interface_type:
23682 case DW_TAG_friend:
23683 case DW_TAG_variant_part:
23684 case DW_TAG_enumeration_type:
23685 case DW_TAG_subroutine_type:
23686 case DW_TAG_string_type:
23687 case DW_TAG_set_type:
23688 case DW_TAG_subrange_type:
23689 case DW_TAG_ptr_to_member_type:
23690 case DW_TAG_file_type:
23691 if (die->die_perennial_p)
23692 break;
23694 /* It's a type node --- don't mark it. */
23695 return;
23697 default:
23698 /* Mark everything else. */
23699 break;
23702 if (die->die_mark == 0)
23704 die->die_mark = 1;
23706 /* Now, mark any dies referenced from here. */
23707 prune_unused_types_walk_attribs (die);
23710 die->die_mark = 2;
23712 /* Mark children. */
23713 FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23716 /* Increment the string counts on strings referred to from DIE's
23717 attributes. */
23719 static void
23720 prune_unused_types_update_strings (dw_die_ref die)
23722 dw_attr_ref a;
23723 unsigned ix;
23725 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23726 if (AT_class (a) == dw_val_class_str)
23728 struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23729 s->refcount++;
23730 /* Avoid unnecessarily putting strings that are used less than
23731 twice in the hash table. */
23732 if (s->refcount
23733 == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23735 indirect_string_node **slot
23736 = debug_str_hash->find_slot_with_hash (s->str,
23737 htab_hash_string (s->str),
23738 INSERT);
23739 gcc_assert (*slot == NULL);
23740 *slot = s;
23745 /* Remove from the tree DIE any dies that aren't marked. */
23747 static void
23748 prune_unused_types_prune (dw_die_ref die)
23750 dw_die_ref c;
23752 gcc_assert (die->die_mark);
23753 prune_unused_types_update_strings (die);
23755 if (! die->die_child)
23756 return;
23758 c = die->die_child;
23759 do {
23760 dw_die_ref prev = c;
23761 for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23762 if (c == die->die_child)
23764 /* No marked children between 'prev' and the end of the list. */
23765 if (prev == c)
23766 /* No marked children at all. */
23767 die->die_child = NULL;
23768 else
23770 prev->die_sib = c->die_sib;
23771 die->die_child = prev;
23773 return;
23776 if (c != prev->die_sib)
23777 prev->die_sib = c;
23778 prune_unused_types_prune (c);
23779 } while (c != die->die_child);
23782 /* Remove dies representing declarations that we never use. */
23784 static void
23785 prune_unused_types (void)
23787 unsigned int i;
23788 limbo_die_node *node;
23789 comdat_type_node *ctnode;
23790 pubname_ref pub;
23791 dw_die_ref base_type;
23793 #if ENABLE_ASSERT_CHECKING
23794 /* All the marks should already be clear. */
23795 verify_marks_clear (comp_unit_die ());
23796 for (node = limbo_die_list; node; node = node->next)
23797 verify_marks_clear (node->die);
23798 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23799 verify_marks_clear (ctnode->root_die);
23800 #endif /* ENABLE_ASSERT_CHECKING */
23802 /* Mark types that are used in global variables. */
23803 premark_types_used_by_global_vars ();
23805 /* Set the mark on nodes that are actually used. */
23806 prune_unused_types_walk (comp_unit_die ());
23807 for (node = limbo_die_list; node; node = node->next)
23808 prune_unused_types_walk (node->die);
23809 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23811 prune_unused_types_walk (ctnode->root_die);
23812 prune_unused_types_mark (ctnode->type_die, 1);
23815 /* Also set the mark on nodes referenced from the pubname_table. Enumerators
23816 are unusual in that they are pubnames that are the children of pubtypes.
23817 They should only be marked via their parent DW_TAG_enumeration_type die,
23818 not as roots in themselves. */
23819 FOR_EACH_VEC_ELT (*pubname_table, i, pub)
23820 if (pub->die->die_tag != DW_TAG_enumerator)
23821 prune_unused_types_mark (pub->die, 1);
23822 for (i = 0; base_types.iterate (i, &base_type); i++)
23823 prune_unused_types_mark (base_type, 1);
23825 if (debug_str_hash)
23826 debug_str_hash->empty ();
23827 if (skeleton_debug_str_hash)
23828 skeleton_debug_str_hash->empty ();
23829 prune_unused_types_prune (comp_unit_die ());
23830 for (limbo_die_node **pnode = &limbo_die_list; *pnode; )
23832 node = *pnode;
23833 if (!node->die->die_mark)
23834 *pnode = node->next;
23835 else
23837 prune_unused_types_prune (node->die);
23838 pnode = &node->next;
23841 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23842 prune_unused_types_prune (ctnode->root_die);
23844 /* Leave the marks clear. */
23845 prune_unmark_dies (comp_unit_die ());
23846 for (node = limbo_die_list; node; node = node->next)
23847 prune_unmark_dies (node->die);
23848 for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23849 prune_unmark_dies (ctnode->root_die);
23852 /* Set the parameter to true if there are any relative pathnames in
23853 the file table. */
23855 file_table_relative_p (dwarf_file_data **slot, bool *p)
23857 struct dwarf_file_data *d = *slot;
23858 if (!IS_ABSOLUTE_PATH (d->filename))
23860 *p = true;
23861 return 0;
23863 return 1;
23866 /* Helpers to manipulate hash table of comdat type units. */
23868 struct comdat_type_hasher : nofree_ptr_hash <comdat_type_node>
23870 static inline hashval_t hash (const comdat_type_node *);
23871 static inline bool equal (const comdat_type_node *, const comdat_type_node *);
23874 inline hashval_t
23875 comdat_type_hasher::hash (const comdat_type_node *type_node)
23877 hashval_t h;
23878 memcpy (&h, type_node->signature, sizeof (h));
23879 return h;
23882 inline bool
23883 comdat_type_hasher::equal (const comdat_type_node *type_node_1,
23884 const comdat_type_node *type_node_2)
23886 return (! memcmp (type_node_1->signature, type_node_2->signature,
23887 DWARF_TYPE_SIGNATURE_SIZE));
23890 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23891 to the location it would have been added, should we know its
23892 DECL_ASSEMBLER_NAME when we added other attributes. This will
23893 probably improve compactness of debug info, removing equivalent
23894 abbrevs, and hide any differences caused by deferring the
23895 computation of the assembler name, triggered by e.g. PCH. */
23897 static inline void
23898 move_linkage_attr (dw_die_ref die)
23900 unsigned ix = vec_safe_length (die->die_attr);
23901 dw_attr_node linkage = (*die->die_attr)[ix - 1];
23903 gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23904 || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23906 while (--ix > 0)
23908 dw_attr_node *prev = &(*die->die_attr)[ix - 1];
23910 if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23911 break;
23914 if (ix != vec_safe_length (die->die_attr) - 1)
23916 die->die_attr->pop ();
23917 die->die_attr->quick_insert (ix, linkage);
23921 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23922 referenced from typed stack ops and count how often they are used. */
23924 static void
23925 mark_base_types (dw_loc_descr_ref loc)
23927 dw_die_ref base_type = NULL;
23929 for (; loc; loc = loc->dw_loc_next)
23931 switch (loc->dw_loc_opc)
23933 case DW_OP_GNU_regval_type:
23934 case DW_OP_GNU_deref_type:
23935 base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23936 break;
23937 case DW_OP_GNU_convert:
23938 case DW_OP_GNU_reinterpret:
23939 if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
23940 continue;
23941 /* FALLTHRU */
23942 case DW_OP_GNU_const_type:
23943 base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23944 break;
23945 case DW_OP_GNU_entry_value:
23946 mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23947 continue;
23948 default:
23949 continue;
23951 gcc_assert (base_type->die_parent == comp_unit_die ());
23952 if (base_type->die_mark)
23953 base_type->die_mark++;
23954 else
23956 base_types.safe_push (base_type);
23957 base_type->die_mark = 1;
23962 /* Comparison function for sorting marked base types. */
23964 static int
23965 base_type_cmp (const void *x, const void *y)
23967 dw_die_ref dx = *(const dw_die_ref *) x;
23968 dw_die_ref dy = *(const dw_die_ref *) y;
23969 unsigned int byte_size1, byte_size2;
23970 unsigned int encoding1, encoding2;
23971 if (dx->die_mark > dy->die_mark)
23972 return -1;
23973 if (dx->die_mark < dy->die_mark)
23974 return 1;
23975 byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23976 byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23977 if (byte_size1 < byte_size2)
23978 return 1;
23979 if (byte_size1 > byte_size2)
23980 return -1;
23981 encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23982 encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23983 if (encoding1 < encoding2)
23984 return 1;
23985 if (encoding1 > encoding2)
23986 return -1;
23987 return 0;
23990 /* Move base types marked by mark_base_types as early as possible
23991 in the CU, sorted by decreasing usage count both to make the
23992 uleb128 references as small as possible and to make sure they
23993 will have die_offset already computed by calc_die_sizes when
23994 sizes of typed stack loc ops is computed. */
23996 static void
23997 move_marked_base_types (void)
23999 unsigned int i;
24000 dw_die_ref base_type, die, c;
24002 if (base_types.is_empty ())
24003 return;
24005 /* Sort by decreasing usage count, they will be added again in that
24006 order later on. */
24007 base_types.qsort (base_type_cmp);
24008 die = comp_unit_die ();
24009 c = die->die_child;
24012 dw_die_ref prev = c;
24013 c = c->die_sib;
24014 while (c->die_mark)
24016 remove_child_with_prev (c, prev);
24017 /* As base types got marked, there must be at least
24018 one node other than DW_TAG_base_type. */
24019 gcc_assert (c != c->die_sib);
24020 c = c->die_sib;
24023 while (c != die->die_child);
24024 gcc_assert (die->die_child);
24025 c = die->die_child;
24026 for (i = 0; base_types.iterate (i, &base_type); i++)
24028 base_type->die_mark = 0;
24029 base_type->die_sib = c->die_sib;
24030 c->die_sib = base_type;
24031 c = base_type;
24035 /* Helper function for resolve_addr, attempt to resolve
24036 one CONST_STRING, return true if successful. Similarly verify that
24037 SYMBOL_REFs refer to variables emitted in the current CU. */
24039 static bool
24040 resolve_one_addr (rtx *addr)
24042 rtx rtl = *addr;
24044 if (GET_CODE (rtl) == CONST_STRING)
24046 size_t len = strlen (XSTR (rtl, 0)) + 1;
24047 tree t = build_string (len, XSTR (rtl, 0));
24048 tree tlen = size_int (len - 1);
24049 TREE_TYPE (t)
24050 = build_array_type (char_type_node, build_index_type (tlen));
24051 rtl = lookup_constant_def (t);
24052 if (!rtl || !MEM_P (rtl))
24053 return false;
24054 rtl = XEXP (rtl, 0);
24055 if (GET_CODE (rtl) == SYMBOL_REF
24056 && SYMBOL_REF_DECL (rtl)
24057 && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24058 return false;
24059 vec_safe_push (used_rtx_array, rtl);
24060 *addr = rtl;
24061 return true;
24064 if (GET_CODE (rtl) == SYMBOL_REF
24065 && SYMBOL_REF_DECL (rtl))
24067 if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24069 if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24070 return false;
24072 else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24073 return false;
24076 if (GET_CODE (rtl) == CONST)
24078 subrtx_ptr_iterator::array_type array;
24079 FOR_EACH_SUBRTX_PTR (iter, array, &XEXP (rtl, 0), ALL)
24080 if (!resolve_one_addr (*iter))
24081 return false;
24084 return true;
24087 /* For STRING_CST, return SYMBOL_REF of its constant pool entry,
24088 if possible, and create DW_TAG_dwarf_procedure that can be referenced
24089 from DW_OP_GNU_implicit_pointer if the string hasn't been seen yet. */
24091 static rtx
24092 string_cst_pool_decl (tree t)
24094 rtx rtl = output_constant_def (t, 1);
24095 unsigned char *array;
24096 dw_loc_descr_ref l;
24097 tree decl;
24098 size_t len;
24099 dw_die_ref ref;
24101 if (!rtl || !MEM_P (rtl))
24102 return NULL_RTX;
24103 rtl = XEXP (rtl, 0);
24104 if (GET_CODE (rtl) != SYMBOL_REF
24105 || SYMBOL_REF_DECL (rtl) == NULL_TREE)
24106 return NULL_RTX;
24108 decl = SYMBOL_REF_DECL (rtl);
24109 if (!lookup_decl_die (decl))
24111 len = TREE_STRING_LENGTH (t);
24112 vec_safe_push (used_rtx_array, rtl);
24113 ref = new_die (DW_TAG_dwarf_procedure, comp_unit_die (), decl);
24114 array = ggc_vec_alloc<unsigned char> (len);
24115 memcpy (array, TREE_STRING_POINTER (t), len);
24116 l = new_loc_descr (DW_OP_implicit_value, len, 0);
24117 l->dw_loc_oprnd2.val_class = dw_val_class_vec;
24118 l->dw_loc_oprnd2.v.val_vec.length = len;
24119 l->dw_loc_oprnd2.v.val_vec.elt_size = 1;
24120 l->dw_loc_oprnd2.v.val_vec.array = array;
24121 add_AT_loc (ref, DW_AT_location, l);
24122 equate_decl_number_to_die (decl, ref);
24124 return rtl;
24127 /* Helper function of resolve_addr_in_expr. LOC is
24128 a DW_OP_addr followed by DW_OP_stack_value, either at the start
24129 of exprloc or after DW_OP_{,bit_}piece, and val_addr can't be
24130 resolved. Replace it (both DW_OP_addr and DW_OP_stack_value)
24131 with DW_OP_GNU_implicit_pointer if possible
24132 and return true, if unsuccessful, return false. */
24134 static bool
24135 optimize_one_addr_into_implicit_ptr (dw_loc_descr_ref loc)
24137 rtx rtl = loc->dw_loc_oprnd1.v.val_addr;
24138 HOST_WIDE_INT offset = 0;
24139 dw_die_ref ref = NULL;
24140 tree decl;
24142 if (GET_CODE (rtl) == CONST
24143 && GET_CODE (XEXP (rtl, 0)) == PLUS
24144 && CONST_INT_P (XEXP (XEXP (rtl, 0), 1)))
24146 offset = INTVAL (XEXP (XEXP (rtl, 0), 1));
24147 rtl = XEXP (XEXP (rtl, 0), 0);
24149 if (GET_CODE (rtl) == CONST_STRING)
24151 size_t len = strlen (XSTR (rtl, 0)) + 1;
24152 tree t = build_string (len, XSTR (rtl, 0));
24153 tree tlen = size_int (len - 1);
24155 TREE_TYPE (t)
24156 = build_array_type (char_type_node, build_index_type (tlen));
24157 rtl = string_cst_pool_decl (t);
24158 if (!rtl)
24159 return false;
24161 if (GET_CODE (rtl) == SYMBOL_REF && SYMBOL_REF_DECL (rtl))
24163 decl = SYMBOL_REF_DECL (rtl);
24164 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
24166 ref = lookup_decl_die (decl);
24167 if (ref && (get_AT (ref, DW_AT_location)
24168 || get_AT (ref, DW_AT_const_value)))
24170 loc->dw_loc_opc = DW_OP_GNU_implicit_pointer;
24171 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24172 loc->dw_loc_oprnd1.val_entry = NULL;
24173 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24174 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24175 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24176 loc->dw_loc_oprnd2.v.val_int = offset;
24177 return true;
24181 return false;
24184 /* Helper function for resolve_addr, handle one location
24185 expression, return false if at least one CONST_STRING or SYMBOL_REF in
24186 the location list couldn't be resolved. */
24188 static bool
24189 resolve_addr_in_expr (dw_loc_descr_ref loc)
24191 dw_loc_descr_ref keep = NULL;
24192 for (dw_loc_descr_ref prev = NULL; loc; prev = loc, loc = loc->dw_loc_next)
24193 switch (loc->dw_loc_opc)
24195 case DW_OP_addr:
24196 if (!resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24198 if ((prev == NULL
24199 || prev->dw_loc_opc == DW_OP_piece
24200 || prev->dw_loc_opc == DW_OP_bit_piece)
24201 && loc->dw_loc_next
24202 && loc->dw_loc_next->dw_loc_opc == DW_OP_stack_value
24203 && !dwarf_strict
24204 && optimize_one_addr_into_implicit_ptr (loc))
24205 break;
24206 return false;
24208 break;
24209 case DW_OP_GNU_addr_index:
24210 case DW_OP_GNU_const_index:
24211 if (loc->dw_loc_opc == DW_OP_GNU_addr_index
24212 || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
24214 rtx rtl = loc->dw_loc_oprnd1.val_entry->addr.rtl;
24215 if (!resolve_one_addr (&rtl))
24216 return false;
24217 remove_addr_table_entry (loc->dw_loc_oprnd1.val_entry);
24218 loc->dw_loc_oprnd1.val_entry =
24219 add_addr_table_entry (rtl, ate_kind_rtx);
24221 break;
24222 case DW_OP_const4u:
24223 case DW_OP_const8u:
24224 if (loc->dtprel
24225 && !resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr))
24226 return false;
24227 break;
24228 case DW_OP_plus_uconst:
24229 if (size_of_loc_descr (loc)
24230 > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
24232 && loc->dw_loc_oprnd1.v.val_unsigned > 0)
24234 dw_loc_descr_ref repl
24235 = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
24236 add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
24237 add_loc_descr (&repl, loc->dw_loc_next);
24238 *loc = *repl;
24240 break;
24241 case DW_OP_implicit_value:
24242 if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24243 && !resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr))
24244 return false;
24245 break;
24246 case DW_OP_GNU_implicit_pointer:
24247 case DW_OP_GNU_parameter_ref:
24248 if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24250 dw_die_ref ref
24251 = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24252 if (ref == NULL)
24253 return false;
24254 loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24255 loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24256 loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24258 break;
24259 case DW_OP_GNU_const_type:
24260 case DW_OP_GNU_regval_type:
24261 case DW_OP_GNU_deref_type:
24262 case DW_OP_GNU_convert:
24263 case DW_OP_GNU_reinterpret:
24264 while (loc->dw_loc_next
24265 && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24267 dw_die_ref base1, base2;
24268 unsigned enc1, enc2, size1, size2;
24269 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24270 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24271 base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24272 else if (loc->dw_loc_oprnd1.val_class
24273 == dw_val_class_unsigned_const)
24274 break;
24275 else
24276 base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24277 if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24278 == dw_val_class_unsigned_const)
24279 break;
24280 base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24281 gcc_assert (base1->die_tag == DW_TAG_base_type
24282 && base2->die_tag == DW_TAG_base_type);
24283 enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24284 enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24285 size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24286 size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24287 if (size1 == size2
24288 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24289 && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24290 && loc != keep)
24291 || enc1 == enc2))
24293 /* Optimize away next DW_OP_GNU_convert after
24294 adjusting LOC's base type die reference. */
24295 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24296 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24297 loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24298 else
24299 loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24300 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24301 continue;
24303 /* Don't change integer DW_OP_GNU_convert after e.g. floating
24304 point typed stack entry. */
24305 else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24306 keep = loc->dw_loc_next;
24307 break;
24309 break;
24310 default:
24311 break;
24313 return true;
24316 /* Helper function of resolve_addr. DIE had DW_AT_location of
24317 DW_OP_addr alone, which referred to DECL in DW_OP_addr's operand
24318 and DW_OP_addr couldn't be resolved. resolve_addr has already
24319 removed the DW_AT_location attribute. This function attempts to
24320 add a new DW_AT_location attribute with DW_OP_GNU_implicit_pointer
24321 to it or DW_AT_const_value attribute, if possible. */
24323 static void
24324 optimize_location_into_implicit_ptr (dw_die_ref die, tree decl)
24326 if (TREE_CODE (decl) != VAR_DECL
24327 || lookup_decl_die (decl) != die
24328 || DECL_EXTERNAL (decl)
24329 || !TREE_STATIC (decl)
24330 || DECL_INITIAL (decl) == NULL_TREE
24331 || DECL_P (DECL_INITIAL (decl))
24332 || get_AT (die, DW_AT_const_value))
24333 return;
24335 tree init = DECL_INITIAL (decl);
24336 HOST_WIDE_INT offset = 0;
24337 /* For variables that have been optimized away and thus
24338 don't have a memory location, see if we can emit
24339 DW_AT_const_value instead. */
24340 if (tree_add_const_value_attribute (die, init))
24341 return;
24342 if (dwarf_strict)
24343 return;
24344 /* If init is ADDR_EXPR or POINTER_PLUS_EXPR of ADDR_EXPR,
24345 and ADDR_EXPR refers to a decl that has DW_AT_location or
24346 DW_AT_const_value (but isn't addressable, otherwise
24347 resolving the original DW_OP_addr wouldn't fail), see if
24348 we can add DW_OP_GNU_implicit_pointer. */
24349 STRIP_NOPS (init);
24350 if (TREE_CODE (init) == POINTER_PLUS_EXPR
24351 && tree_fits_shwi_p (TREE_OPERAND (init, 1)))
24353 offset = tree_to_shwi (TREE_OPERAND (init, 1));
24354 init = TREE_OPERAND (init, 0);
24355 STRIP_NOPS (init);
24357 if (TREE_CODE (init) != ADDR_EXPR)
24358 return;
24359 if ((TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST
24360 && !TREE_ASM_WRITTEN (TREE_OPERAND (init, 0)))
24361 || (TREE_CODE (TREE_OPERAND (init, 0)) == VAR_DECL
24362 && !DECL_EXTERNAL (TREE_OPERAND (init, 0))
24363 && TREE_OPERAND (init, 0) != decl))
24365 dw_die_ref ref;
24366 dw_loc_descr_ref l;
24368 if (TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST)
24370 rtx rtl = string_cst_pool_decl (TREE_OPERAND (init, 0));
24371 if (!rtl)
24372 return;
24373 decl = SYMBOL_REF_DECL (rtl);
24375 else
24376 decl = TREE_OPERAND (init, 0);
24377 ref = lookup_decl_die (decl);
24378 if (ref == NULL
24379 || (!get_AT (ref, DW_AT_location)
24380 && !get_AT (ref, DW_AT_const_value)))
24381 return;
24382 l = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
24383 l->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24384 l->dw_loc_oprnd1.v.val_die_ref.die = ref;
24385 l->dw_loc_oprnd1.v.val_die_ref.external = 0;
24386 add_AT_loc (die, DW_AT_location, l);
24390 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24391 an address in .rodata section if the string literal is emitted there,
24392 or remove the containing location list or replace DW_AT_const_value
24393 with DW_AT_location and empty location expression, if it isn't found
24394 in .rodata. Similarly for SYMBOL_REFs, keep only those that refer
24395 to something that has been emitted in the current CU. */
24397 static void
24398 resolve_addr (dw_die_ref die)
24400 dw_die_ref c;
24401 dw_attr_ref a;
24402 dw_loc_list_ref *curr, *start, loc;
24403 unsigned ix;
24405 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
24406 switch (AT_class (a))
24408 case dw_val_class_loc_list:
24409 start = curr = AT_loc_list_ptr (a);
24410 loc = *curr;
24411 gcc_assert (loc);
24412 /* The same list can be referenced more than once. See if we have
24413 already recorded the result from a previous pass. */
24414 if (loc->replaced)
24415 *curr = loc->dw_loc_next;
24416 else if (!loc->resolved_addr)
24418 /* As things stand, we do not expect or allow one die to
24419 reference a suffix of another die's location list chain.
24420 References must be identical or completely separate.
24421 There is therefore no need to cache the result of this
24422 pass on any list other than the first; doing so
24423 would lead to unnecessary writes. */
24424 while (*curr)
24426 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24427 if (!resolve_addr_in_expr ((*curr)->expr))
24429 dw_loc_list_ref next = (*curr)->dw_loc_next;
24430 dw_loc_descr_ref l = (*curr)->expr;
24432 if (next && (*curr)->ll_symbol)
24434 gcc_assert (!next->ll_symbol);
24435 next->ll_symbol = (*curr)->ll_symbol;
24437 if (dwarf_split_debug_info)
24438 remove_loc_list_addr_table_entries (l);
24439 *curr = next;
24441 else
24443 mark_base_types ((*curr)->expr);
24444 curr = &(*curr)->dw_loc_next;
24447 if (loc == *start)
24448 loc->resolved_addr = 1;
24449 else
24451 loc->replaced = 1;
24452 loc->dw_loc_next = *start;
24455 if (!*start)
24457 remove_AT (die, a->dw_attr);
24458 ix--;
24460 break;
24461 case dw_val_class_loc:
24463 dw_loc_descr_ref l = AT_loc (a);
24464 /* For -gdwarf-2 don't attempt to optimize
24465 DW_AT_data_member_location containing
24466 DW_OP_plus_uconst - older consumers might
24467 rely on it being that op instead of a more complex,
24468 but shorter, location description. */
24469 if ((dwarf_version > 2
24470 || a->dw_attr != DW_AT_data_member_location
24471 || l == NULL
24472 || l->dw_loc_opc != DW_OP_plus_uconst
24473 || l->dw_loc_next != NULL)
24474 && !resolve_addr_in_expr (l))
24476 if (dwarf_split_debug_info)
24477 remove_loc_list_addr_table_entries (l);
24478 if (l != NULL
24479 && l->dw_loc_next == NULL
24480 && l->dw_loc_opc == DW_OP_addr
24481 && GET_CODE (l->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF
24482 && SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr)
24483 && a->dw_attr == DW_AT_location)
24485 tree decl = SYMBOL_REF_DECL (l->dw_loc_oprnd1.v.val_addr);
24486 remove_AT (die, a->dw_attr);
24487 ix--;
24488 optimize_location_into_implicit_ptr (die, decl);
24489 break;
24491 remove_AT (die, a->dw_attr);
24492 ix--;
24494 else
24495 mark_base_types (l);
24497 break;
24498 case dw_val_class_addr:
24499 if (a->dw_attr == DW_AT_const_value
24500 && !resolve_one_addr (&a->dw_attr_val.v.val_addr))
24502 if (AT_index (a) != NOT_INDEXED)
24503 remove_addr_table_entry (a->dw_attr_val.val_entry);
24504 remove_AT (die, a->dw_attr);
24505 ix--;
24507 if (die->die_tag == DW_TAG_GNU_call_site
24508 && a->dw_attr == DW_AT_abstract_origin)
24510 tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24511 dw_die_ref tdie = lookup_decl_die (tdecl);
24512 dw_die_ref cdie;
24513 if (tdie == NULL
24514 && DECL_EXTERNAL (tdecl)
24515 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE
24516 && (cdie = lookup_context_die (DECL_CONTEXT (tdecl))))
24518 /* Creating a full DIE for tdecl is overly expensive and
24519 at this point even wrong when in the LTO phase
24520 as it can end up generating new type DIEs we didn't
24521 output and thus optimize_external_refs will crash. */
24522 tdie = new_die (DW_TAG_subprogram, cdie, NULL_TREE);
24523 add_AT_flag (tdie, DW_AT_external, 1);
24524 add_AT_flag (tdie, DW_AT_declaration, 1);
24525 add_linkage_attr (tdie, tdecl);
24526 add_name_and_src_coords_attributes (tdie, tdecl);
24527 equate_decl_number_to_die (tdecl, tdie);
24529 if (tdie)
24531 a->dw_attr_val.val_class = dw_val_class_die_ref;
24532 a->dw_attr_val.v.val_die_ref.die = tdie;
24533 a->dw_attr_val.v.val_die_ref.external = 0;
24535 else
24537 if (AT_index (a) != NOT_INDEXED)
24538 remove_addr_table_entry (a->dw_attr_val.val_entry);
24539 remove_AT (die, a->dw_attr);
24540 ix--;
24543 break;
24544 default:
24545 break;
24548 FOR_EACH_CHILD (die, c, resolve_addr (c));
24551 /* Helper routines for optimize_location_lists.
24552 This pass tries to share identical local lists in .debug_loc
24553 section. */
24555 /* Iteratively hash operands of LOC opcode into HSTATE. */
24557 static void
24558 hash_loc_operands (dw_loc_descr_ref loc, inchash::hash &hstate)
24560 dw_val_ref val1 = &loc->dw_loc_oprnd1;
24561 dw_val_ref val2 = &loc->dw_loc_oprnd2;
24563 switch (loc->dw_loc_opc)
24565 case DW_OP_const4u:
24566 case DW_OP_const8u:
24567 if (loc->dtprel)
24568 goto hash_addr;
24569 /* FALLTHRU */
24570 case DW_OP_const1u:
24571 case DW_OP_const1s:
24572 case DW_OP_const2u:
24573 case DW_OP_const2s:
24574 case DW_OP_const4s:
24575 case DW_OP_const8s:
24576 case DW_OP_constu:
24577 case DW_OP_consts:
24578 case DW_OP_pick:
24579 case DW_OP_plus_uconst:
24580 case DW_OP_breg0:
24581 case DW_OP_breg1:
24582 case DW_OP_breg2:
24583 case DW_OP_breg3:
24584 case DW_OP_breg4:
24585 case DW_OP_breg5:
24586 case DW_OP_breg6:
24587 case DW_OP_breg7:
24588 case DW_OP_breg8:
24589 case DW_OP_breg9:
24590 case DW_OP_breg10:
24591 case DW_OP_breg11:
24592 case DW_OP_breg12:
24593 case DW_OP_breg13:
24594 case DW_OP_breg14:
24595 case DW_OP_breg15:
24596 case DW_OP_breg16:
24597 case DW_OP_breg17:
24598 case DW_OP_breg18:
24599 case DW_OP_breg19:
24600 case DW_OP_breg20:
24601 case DW_OP_breg21:
24602 case DW_OP_breg22:
24603 case DW_OP_breg23:
24604 case DW_OP_breg24:
24605 case DW_OP_breg25:
24606 case DW_OP_breg26:
24607 case DW_OP_breg27:
24608 case DW_OP_breg28:
24609 case DW_OP_breg29:
24610 case DW_OP_breg30:
24611 case DW_OP_breg31:
24612 case DW_OP_regx:
24613 case DW_OP_fbreg:
24614 case DW_OP_piece:
24615 case DW_OP_deref_size:
24616 case DW_OP_xderef_size:
24617 hstate.add_object (val1->v.val_int);
24618 break;
24619 case DW_OP_skip:
24620 case DW_OP_bra:
24622 int offset;
24624 gcc_assert (val1->val_class == dw_val_class_loc);
24625 offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24626 hstate.add_object (offset);
24628 break;
24629 case DW_OP_implicit_value:
24630 hstate.add_object (val1->v.val_unsigned);
24631 switch (val2->val_class)
24633 case dw_val_class_const:
24634 hstate.add_object (val2->v.val_int);
24635 break;
24636 case dw_val_class_vec:
24638 unsigned int elt_size = val2->v.val_vec.elt_size;
24639 unsigned int len = val2->v.val_vec.length;
24641 hstate.add_int (elt_size);
24642 hstate.add_int (len);
24643 hstate.add (val2->v.val_vec.array, len * elt_size);
24645 break;
24646 case dw_val_class_const_double:
24647 hstate.add_object (val2->v.val_double.low);
24648 hstate.add_object (val2->v.val_double.high);
24649 break;
24650 case dw_val_class_wide_int:
24651 hstate.add (val2->v.val_wide->get_val (),
24652 get_full_len (*val2->v.val_wide)
24653 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24654 break;
24655 case dw_val_class_addr:
24656 inchash::add_rtx (val2->v.val_addr, hstate);
24657 break;
24658 default:
24659 gcc_unreachable ();
24661 break;
24662 case DW_OP_bregx:
24663 case DW_OP_bit_piece:
24664 hstate.add_object (val1->v.val_int);
24665 hstate.add_object (val2->v.val_int);
24666 break;
24667 case DW_OP_addr:
24668 hash_addr:
24669 if (loc->dtprel)
24671 unsigned char dtprel = 0xd1;
24672 hstate.add_object (dtprel);
24674 inchash::add_rtx (val1->v.val_addr, hstate);
24675 break;
24676 case DW_OP_GNU_addr_index:
24677 case DW_OP_GNU_const_index:
24679 if (loc->dtprel)
24681 unsigned char dtprel = 0xd1;
24682 hstate.add_object (dtprel);
24684 inchash::add_rtx (val1->val_entry->addr.rtl, hstate);
24686 break;
24687 case DW_OP_GNU_implicit_pointer:
24688 hstate.add_int (val2->v.val_int);
24689 break;
24690 case DW_OP_GNU_entry_value:
24691 hstate.add_object (val1->v.val_loc);
24692 break;
24693 case DW_OP_GNU_regval_type:
24694 case DW_OP_GNU_deref_type:
24696 unsigned int byte_size
24697 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24698 unsigned int encoding
24699 = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24700 hstate.add_object (val1->v.val_int);
24701 hstate.add_object (byte_size);
24702 hstate.add_object (encoding);
24704 break;
24705 case DW_OP_GNU_convert:
24706 case DW_OP_GNU_reinterpret:
24707 if (val1->val_class == dw_val_class_unsigned_const)
24709 hstate.add_object (val1->v.val_unsigned);
24710 break;
24712 /* FALLTHRU */
24713 case DW_OP_GNU_const_type:
24715 unsigned int byte_size
24716 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24717 unsigned int encoding
24718 = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24719 hstate.add_object (byte_size);
24720 hstate.add_object (encoding);
24721 if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24722 break;
24723 hstate.add_object (val2->val_class);
24724 switch (val2->val_class)
24726 case dw_val_class_const:
24727 hstate.add_object (val2->v.val_int);
24728 break;
24729 case dw_val_class_vec:
24731 unsigned int elt_size = val2->v.val_vec.elt_size;
24732 unsigned int len = val2->v.val_vec.length;
24734 hstate.add_object (elt_size);
24735 hstate.add_object (len);
24736 hstate.add (val2->v.val_vec.array, len * elt_size);
24738 break;
24739 case dw_val_class_const_double:
24740 hstate.add_object (val2->v.val_double.low);
24741 hstate.add_object (val2->v.val_double.high);
24742 break;
24743 case dw_val_class_wide_int:
24744 hstate.add (val2->v.val_wide->get_val (),
24745 get_full_len (*val2->v.val_wide)
24746 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR);
24747 break;
24748 default:
24749 gcc_unreachable ();
24752 break;
24754 default:
24755 /* Other codes have no operands. */
24756 break;
24760 /* Iteratively hash the whole DWARF location expression LOC into HSTATE. */
24762 static inline void
24763 hash_locs (dw_loc_descr_ref loc, inchash::hash &hstate)
24765 dw_loc_descr_ref l;
24766 bool sizes_computed = false;
24767 /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed. */
24768 size_of_locs (loc);
24770 for (l = loc; l != NULL; l = l->dw_loc_next)
24772 enum dwarf_location_atom opc = l->dw_loc_opc;
24773 hstate.add_object (opc);
24774 if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24776 size_of_locs (loc);
24777 sizes_computed = true;
24779 hash_loc_operands (l, hstate);
24783 /* Compute hash of the whole location list LIST_HEAD. */
24785 static inline void
24786 hash_loc_list (dw_loc_list_ref list_head)
24788 dw_loc_list_ref curr = list_head;
24789 inchash::hash hstate;
24791 for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24793 hstate.add (curr->begin, strlen (curr->begin) + 1);
24794 hstate.add (curr->end, strlen (curr->end) + 1);
24795 if (curr->section)
24796 hstate.add (curr->section, strlen (curr->section) + 1);
24797 hash_locs (curr->expr, hstate);
24799 list_head->hash = hstate.end ();
24802 /* Return true if X and Y opcodes have the same operands. */
24804 static inline bool
24805 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24807 dw_val_ref valx1 = &x->dw_loc_oprnd1;
24808 dw_val_ref valx2 = &x->dw_loc_oprnd2;
24809 dw_val_ref valy1 = &y->dw_loc_oprnd1;
24810 dw_val_ref valy2 = &y->dw_loc_oprnd2;
24812 switch (x->dw_loc_opc)
24814 case DW_OP_const4u:
24815 case DW_OP_const8u:
24816 if (x->dtprel)
24817 goto hash_addr;
24818 /* FALLTHRU */
24819 case DW_OP_const1u:
24820 case DW_OP_const1s:
24821 case DW_OP_const2u:
24822 case DW_OP_const2s:
24823 case DW_OP_const4s:
24824 case DW_OP_const8s:
24825 case DW_OP_constu:
24826 case DW_OP_consts:
24827 case DW_OP_pick:
24828 case DW_OP_plus_uconst:
24829 case DW_OP_breg0:
24830 case DW_OP_breg1:
24831 case DW_OP_breg2:
24832 case DW_OP_breg3:
24833 case DW_OP_breg4:
24834 case DW_OP_breg5:
24835 case DW_OP_breg6:
24836 case DW_OP_breg7:
24837 case DW_OP_breg8:
24838 case DW_OP_breg9:
24839 case DW_OP_breg10:
24840 case DW_OP_breg11:
24841 case DW_OP_breg12:
24842 case DW_OP_breg13:
24843 case DW_OP_breg14:
24844 case DW_OP_breg15:
24845 case DW_OP_breg16:
24846 case DW_OP_breg17:
24847 case DW_OP_breg18:
24848 case DW_OP_breg19:
24849 case DW_OP_breg20:
24850 case DW_OP_breg21:
24851 case DW_OP_breg22:
24852 case DW_OP_breg23:
24853 case DW_OP_breg24:
24854 case DW_OP_breg25:
24855 case DW_OP_breg26:
24856 case DW_OP_breg27:
24857 case DW_OP_breg28:
24858 case DW_OP_breg29:
24859 case DW_OP_breg30:
24860 case DW_OP_breg31:
24861 case DW_OP_regx:
24862 case DW_OP_fbreg:
24863 case DW_OP_piece:
24864 case DW_OP_deref_size:
24865 case DW_OP_xderef_size:
24866 return valx1->v.val_int == valy1->v.val_int;
24867 case DW_OP_skip:
24868 case DW_OP_bra:
24869 /* If splitting debug info, the use of DW_OP_GNU_addr_index
24870 can cause irrelevant differences in dw_loc_addr. */
24871 gcc_assert (valx1->val_class == dw_val_class_loc
24872 && valy1->val_class == dw_val_class_loc
24873 && (dwarf_split_debug_info
24874 || x->dw_loc_addr == y->dw_loc_addr));
24875 return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24876 case DW_OP_implicit_value:
24877 if (valx1->v.val_unsigned != valy1->v.val_unsigned
24878 || valx2->val_class != valy2->val_class)
24879 return false;
24880 switch (valx2->val_class)
24882 case dw_val_class_const:
24883 return valx2->v.val_int == valy2->v.val_int;
24884 case dw_val_class_vec:
24885 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24886 && valx2->v.val_vec.length == valy2->v.val_vec.length
24887 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24888 valx2->v.val_vec.elt_size
24889 * valx2->v.val_vec.length) == 0;
24890 case dw_val_class_const_double:
24891 return valx2->v.val_double.low == valy2->v.val_double.low
24892 && valx2->v.val_double.high == valy2->v.val_double.high;
24893 case dw_val_class_wide_int:
24894 return *valx2->v.val_wide == *valy2->v.val_wide;
24895 case dw_val_class_addr:
24896 return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24897 default:
24898 gcc_unreachable ();
24900 case DW_OP_bregx:
24901 case DW_OP_bit_piece:
24902 return valx1->v.val_int == valy1->v.val_int
24903 && valx2->v.val_int == valy2->v.val_int;
24904 case DW_OP_addr:
24905 hash_addr:
24906 return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24907 case DW_OP_GNU_addr_index:
24908 case DW_OP_GNU_const_index:
24910 rtx ax1 = valx1->val_entry->addr.rtl;
24911 rtx ay1 = valy1->val_entry->addr.rtl;
24912 return rtx_equal_p (ax1, ay1);
24914 case DW_OP_GNU_implicit_pointer:
24915 return valx1->val_class == dw_val_class_die_ref
24916 && valx1->val_class == valy1->val_class
24917 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24918 && valx2->v.val_int == valy2->v.val_int;
24919 case DW_OP_GNU_entry_value:
24920 return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24921 case DW_OP_GNU_const_type:
24922 if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24923 || valx2->val_class != valy2->val_class)
24924 return false;
24925 switch (valx2->val_class)
24927 case dw_val_class_const:
24928 return valx2->v.val_int == valy2->v.val_int;
24929 case dw_val_class_vec:
24930 return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24931 && valx2->v.val_vec.length == valy2->v.val_vec.length
24932 && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24933 valx2->v.val_vec.elt_size
24934 * valx2->v.val_vec.length) == 0;
24935 case dw_val_class_const_double:
24936 return valx2->v.val_double.low == valy2->v.val_double.low
24937 && valx2->v.val_double.high == valy2->v.val_double.high;
24938 case dw_val_class_wide_int:
24939 return *valx2->v.val_wide == *valy2->v.val_wide;
24940 default:
24941 gcc_unreachable ();
24943 case DW_OP_GNU_regval_type:
24944 case DW_OP_GNU_deref_type:
24945 return valx1->v.val_int == valy1->v.val_int
24946 && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24947 case DW_OP_GNU_convert:
24948 case DW_OP_GNU_reinterpret:
24949 if (valx1->val_class != valy1->val_class)
24950 return false;
24951 if (valx1->val_class == dw_val_class_unsigned_const)
24952 return valx1->v.val_unsigned == valy1->v.val_unsigned;
24953 return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24954 case DW_OP_GNU_parameter_ref:
24955 return valx1->val_class == dw_val_class_die_ref
24956 && valx1->val_class == valy1->val_class
24957 && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24958 default:
24959 /* Other codes have no operands. */
24960 return true;
24964 /* Return true if DWARF location expressions X and Y are the same. */
24966 static inline bool
24967 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24969 for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24970 if (x->dw_loc_opc != y->dw_loc_opc
24971 || x->dtprel != y->dtprel
24972 || !compare_loc_operands (x, y))
24973 break;
24974 return x == NULL && y == NULL;
24977 /* Hashtable helpers. */
24979 struct loc_list_hasher : nofree_ptr_hash <dw_loc_list_struct>
24981 static inline hashval_t hash (const dw_loc_list_struct *);
24982 static inline bool equal (const dw_loc_list_struct *,
24983 const dw_loc_list_struct *);
24986 /* Return precomputed hash of location list X. */
24988 inline hashval_t
24989 loc_list_hasher::hash (const dw_loc_list_struct *x)
24991 return x->hash;
24994 /* Return true if location lists A and B are the same. */
24996 inline bool
24997 loc_list_hasher::equal (const dw_loc_list_struct *a,
24998 const dw_loc_list_struct *b)
25000 if (a == b)
25001 return 1;
25002 if (a->hash != b->hash)
25003 return 0;
25004 for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
25005 if (strcmp (a->begin, b->begin) != 0
25006 || strcmp (a->end, b->end) != 0
25007 || (a->section == NULL) != (b->section == NULL)
25008 || (a->section && strcmp (a->section, b->section) != 0)
25009 || !compare_locs (a->expr, b->expr))
25010 break;
25011 return a == NULL && b == NULL;
25014 typedef hash_table<loc_list_hasher> loc_list_hash_type;
25017 /* Recursively optimize location lists referenced from DIE
25018 children and share them whenever possible. */
25020 static void
25021 optimize_location_lists_1 (dw_die_ref die, loc_list_hash_type *htab)
25023 dw_die_ref c;
25024 dw_attr_ref a;
25025 unsigned ix;
25026 dw_loc_list_struct **slot;
25028 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25029 if (AT_class (a) == dw_val_class_loc_list)
25031 dw_loc_list_ref list = AT_loc_list (a);
25032 /* TODO: perform some optimizations here, before hashing
25033 it and storing into the hash table. */
25034 hash_loc_list (list);
25035 slot = htab->find_slot_with_hash (list, list->hash, INSERT);
25036 if (*slot == NULL)
25037 *slot = list;
25038 else
25039 a->dw_attr_val.v.val_loc_list = *slot;
25042 FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
25046 /* Recursively assign each location list a unique index into the debug_addr
25047 section. */
25049 static void
25050 index_location_lists (dw_die_ref die)
25052 dw_die_ref c;
25053 dw_attr_ref a;
25054 unsigned ix;
25056 FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
25057 if (AT_class (a) == dw_val_class_loc_list)
25059 dw_loc_list_ref list = AT_loc_list (a);
25060 dw_loc_list_ref curr;
25061 for (curr = list; curr != NULL; curr = curr->dw_loc_next)
25063 /* Don't index an entry that has already been indexed
25064 or won't be output. */
25065 if (curr->begin_entry != NULL
25066 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
25067 continue;
25069 curr->begin_entry
25070 = add_addr_table_entry (xstrdup (curr->begin),
25071 ate_kind_label);
25075 FOR_EACH_CHILD (die, c, index_location_lists (c));
25078 /* Optimize location lists referenced from DIE
25079 children and share them whenever possible. */
25081 static void
25082 optimize_location_lists (dw_die_ref die)
25084 loc_list_hash_type htab (500);
25085 optimize_location_lists_1 (die, &htab);
25088 /* Output stuff that dwarf requires at the end of every file,
25089 and generate the DWARF-2 debugging info. */
25091 static void
25092 dwarf2out_finish (const char *filename)
25094 comdat_type_node *ctnode;
25095 dw_die_ref main_comp_unit_die;
25097 /* Flush out any latecomers to the limbo party. */
25098 dwarf2out_early_finish ();
25100 /* PCH might result in DW_AT_producer string being restored from the
25101 header compilation, so always fill it with empty string initially
25102 and overwrite only here. */
25103 dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
25104 producer_string = gen_producer_string ();
25105 producer->dw_attr_val.v.val_str->refcount--;
25106 producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
25108 gen_scheduled_generic_parms_dies ();
25109 gen_remaining_tmpl_value_param_die_attribute ();
25111 /* Add the name for the main input file now. We delayed this from
25112 dwarf2out_init to avoid complications with PCH.
25113 For LTO produced units use a fixed artificial name to avoid
25114 leaking tempfile names into the dwarf. */
25115 if (!in_lto_p)
25116 add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25117 else
25118 add_name_attribute (comp_unit_die (), "<artificial>");
25119 if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
25120 add_comp_dir_attribute (comp_unit_die ());
25121 else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25123 bool p = false;
25124 file_table->traverse<bool *, file_table_relative_p> (&p);
25125 if (p)
25126 add_comp_dir_attribute (comp_unit_die ());
25129 #if ENABLE_ASSERT_CHECKING
25131 dw_die_ref die = comp_unit_die (), c;
25132 FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25134 #endif
25135 resolve_addr (comp_unit_die ());
25136 move_marked_base_types ();
25138 /* Walk through the list of incomplete types again, trying once more to
25139 emit full debugging info for them. */
25140 retry_incomplete_types ();
25142 if (flag_eliminate_unused_debug_types)
25143 prune_unused_types ();
25145 /* Generate separate COMDAT sections for type DIEs. */
25146 if (use_debug_types)
25148 break_out_comdat_types (comp_unit_die ());
25150 /* Each new type_unit DIE was added to the limbo die list when created.
25151 Since these have all been added to comdat_type_list, clear the
25152 limbo die list. */
25153 limbo_die_list = NULL;
25155 /* For each new comdat type unit, copy declarations for incomplete
25156 types to make the new unit self-contained (i.e., no direct
25157 references to the main compile unit). */
25158 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25159 copy_decls_for_unworthy_types (ctnode->root_die);
25160 copy_decls_for_unworthy_types (comp_unit_die ());
25162 /* In the process of copying declarations from one unit to another,
25163 we may have left some declarations behind that are no longer
25164 referenced. Prune them. */
25165 prune_unused_types ();
25168 /* Generate separate CUs for each of the include files we've seen.
25169 They will go into limbo_die_list. */
25170 if (flag_eliminate_dwarf2_dups)
25171 break_out_includes (comp_unit_die ());
25173 /* Traverse the DIE's and add add sibling attributes to those DIE's
25174 that have children. */
25175 add_sibling_attributes (comp_unit_die ());
25176 limbo_die_node *node;
25177 for (node = limbo_die_list; node; node = node->next)
25178 add_sibling_attributes (node->die);
25179 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25180 add_sibling_attributes (ctnode->root_die);
25182 /* When splitting DWARF info, we put some attributes in the
25183 skeleton compile_unit DIE that remains in the .o, while
25184 most attributes go in the DWO compile_unit_die. */
25185 if (dwarf_split_debug_info)
25186 main_comp_unit_die = gen_compile_unit_die (NULL);
25187 else
25188 main_comp_unit_die = comp_unit_die ();
25190 /* Output a terminator label for the .text section. */
25191 switch_to_section (text_section);
25192 targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25193 if (cold_text_section)
25195 switch_to_section (cold_text_section);
25196 targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25199 /* We can only use the low/high_pc attributes if all of the code was
25200 in .text. */
25201 if (!have_multiple_function_sections
25202 || (dwarf_version < 3 && dwarf_strict))
25204 /* Don't add if the CU has no associated code. */
25205 if (text_section_used)
25206 add_AT_low_high_pc (main_comp_unit_die, text_section_label,
25207 text_end_label, true);
25209 else
25211 unsigned fde_idx;
25212 dw_fde_ref fde;
25213 bool range_list_added = false;
25215 if (text_section_used)
25216 add_ranges_by_labels (main_comp_unit_die, text_section_label,
25217 text_end_label, &range_list_added, true);
25218 if (cold_text_section_used)
25219 add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
25220 cold_end_label, &range_list_added, true);
25222 FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
25224 if (DECL_IGNORED_P (fde->decl))
25225 continue;
25226 if (!fde->in_std_section)
25227 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
25228 fde->dw_fde_end, &range_list_added,
25229 true);
25230 if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25231 add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
25232 fde->dw_fde_second_end, &range_list_added,
25233 true);
25236 if (range_list_added)
25238 /* We need to give .debug_loc and .debug_ranges an appropriate
25239 "base address". Use zero so that these addresses become
25240 absolute. Historically, we've emitted the unexpected
25241 DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25242 Emit both to give time for other tools to adapt. */
25243 add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
25244 if (! dwarf_strict && dwarf_version < 4)
25245 add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
25247 add_ranges (NULL);
25251 if (debug_info_level >= DINFO_LEVEL_TERSE)
25252 add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
25253 debug_line_section_label);
25255 if (have_macinfo)
25256 add_AT_macptr (comp_unit_die (),
25257 dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
25258 macinfo_section_label);
25260 if (dwarf_split_debug_info)
25262 /* optimize_location_lists calculates the size of the lists,
25263 so index them first, and assign indices to the entries.
25264 Although optimize_location_lists will remove entries from
25265 the table, it only does so for duplicates, and therefore
25266 only reduces ref_counts to 1. */
25267 index_location_lists (comp_unit_die ());
25269 if (addr_index_table != NULL)
25271 unsigned int index = 0;
25272 addr_index_table
25273 ->traverse_noresize<unsigned int *, index_addr_table_entry>
25274 (&index);
25278 if (have_location_lists)
25279 optimize_location_lists (comp_unit_die ());
25281 save_macinfo_strings ();
25283 if (dwarf_split_debug_info)
25285 unsigned int index = 0;
25287 /* Add attributes common to skeleton compile_units and
25288 type_units. Because these attributes include strings, it
25289 must be done before freezing the string table. Top-level
25290 skeleton die attrs are added when the skeleton type unit is
25291 created, so ensure it is created by this point. */
25292 add_top_level_skeleton_die_attrs (main_comp_unit_die);
25293 debug_str_hash->traverse_noresize<unsigned int *, index_string> (&index);
25296 /* Output all of the compilation units. We put the main one last so that
25297 the offsets are available to output_pubnames. */
25298 for (node = limbo_die_list; node; node = node->next)
25299 output_comp_unit (node->die, 0);
25301 hash_table<comdat_type_hasher> comdat_type_table (100);
25302 for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25304 comdat_type_node **slot = comdat_type_table.find_slot (ctnode, INSERT);
25306 /* Don't output duplicate types. */
25307 if (*slot != HTAB_EMPTY_ENTRY)
25308 continue;
25310 /* Add a pointer to the line table for the main compilation unit
25311 so that the debugger can make sense of DW_AT_decl_file
25312 attributes. */
25313 if (debug_info_level >= DINFO_LEVEL_TERSE)
25314 add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25315 (!dwarf_split_debug_info
25316 ? debug_line_section_label
25317 : debug_skeleton_line_section_label));
25319 output_comdat_type_unit (ctnode);
25320 *slot = ctnode;
25323 /* The AT_pubnames attribute needs to go in all skeleton dies, including
25324 both the main_cu and all skeleton TUs. Making this call unconditional
25325 would end up either adding a second copy of the AT_pubnames attribute, or
25326 requiring a special case in add_top_level_skeleton_die_attrs. */
25327 if (!dwarf_split_debug_info)
25328 add_AT_pubnames (comp_unit_die ());
25330 if (dwarf_split_debug_info)
25332 int mark;
25333 unsigned char checksum[16];
25334 struct md5_ctx ctx;
25336 /* Compute a checksum of the comp_unit to use as the dwo_id. */
25337 md5_init_ctx (&ctx);
25338 mark = 0;
25339 die_checksum (comp_unit_die (), &ctx, &mark);
25340 unmark_all_dies (comp_unit_die ());
25341 md5_finish_ctx (&ctx, checksum);
25343 /* Use the first 8 bytes of the checksum as the dwo_id,
25344 and add it to both comp-unit DIEs. */
25345 add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
25346 add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
25348 /* Add the base offset of the ranges table to the skeleton
25349 comp-unit DIE. */
25350 if (ranges_table_in_use)
25351 add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
25352 ranges_section_label);
25354 switch_to_section (debug_addr_section);
25355 ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
25356 output_addr_table ();
25359 /* Output the main compilation unit if non-empty or if .debug_macinfo
25360 or .debug_macro will be emitted. */
25361 output_comp_unit (comp_unit_die (), have_macinfo);
25363 if (dwarf_split_debug_info && info_section_emitted)
25364 output_skeleton_debug_sections (main_comp_unit_die);
25366 /* Output the abbreviation table. */
25367 if (abbrev_die_table_in_use != 1)
25369 switch_to_section (debug_abbrev_section);
25370 ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25371 output_abbrev_section ();
25374 /* Output location list section if necessary. */
25375 if (have_location_lists)
25377 /* Output the location lists info. */
25378 switch_to_section (debug_loc_section);
25379 ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25380 output_location_lists (comp_unit_die ());
25383 output_pubtables ();
25385 /* Output the address range information if a CU (.debug_info section)
25386 was emitted. We output an empty table even if we had no functions
25387 to put in it. This because the consumer has no way to tell the
25388 difference between an empty table that we omitted and failure to
25389 generate a table that would have contained data. */
25390 if (info_section_emitted)
25392 unsigned long aranges_length = size_of_aranges ();
25394 switch_to_section (debug_aranges_section);
25395 output_aranges (aranges_length);
25398 /* Output ranges section if necessary. */
25399 if (ranges_table_in_use)
25401 switch_to_section (debug_ranges_section);
25402 ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25403 output_ranges ();
25406 /* Have to end the macro section. */
25407 if (have_macinfo)
25409 switch_to_section (debug_macinfo_section);
25410 ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25411 output_macinfo ();
25412 dw2_asm_output_data (1, 0, "End compilation unit");
25415 /* Output the source line correspondence table. We must do this
25416 even if there is no line information. Otherwise, on an empty
25417 translation unit, we will generate a present, but empty,
25418 .debug_info section. IRIX 6.5 `nm' will then complain when
25419 examining the file. This is done late so that any filenames
25420 used by the debug_info section are marked as 'used'. */
25421 switch_to_section (debug_line_section);
25422 ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25423 if (! DWARF2_ASM_LINE_DEBUG_INFO)
25424 output_line_info (false);
25426 if (dwarf_split_debug_info && info_section_emitted)
25428 switch_to_section (debug_skeleton_line_section);
25429 ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
25430 output_line_info (true);
25433 /* If we emitted any indirect strings, output the string table too. */
25434 if (debug_str_hash || skeleton_debug_str_hash)
25435 output_indirect_strings ();
25438 /* Perform any cleanups needed after the early debug generation pass
25439 has run. */
25441 static void
25442 dwarf2out_early_finish (void)
25444 limbo_die_node *node, *next_node;
25446 /* Add DW_AT_linkage_name for all deferred DIEs. */
25447 for (node = deferred_asm_name; node; node = node->next)
25449 tree decl = node->created_for;
25450 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
25451 /* A missing DECL_ASSEMBLER_NAME can be a constant DIE that
25452 ended up in in deferred_asm_name before we knew it was
25453 constant and never written to disk. */
25454 && DECL_ASSEMBLER_NAME (decl))
25456 add_linkage_attr (node->die, decl);
25457 move_linkage_attr (node->die);
25460 deferred_asm_name = NULL;
25462 /* Traverse the limbo die list, and add parent/child links. The only
25463 dies without parents that should be here are concrete instances of
25464 inline functions, and the comp_unit_die. We can ignore the comp_unit_die.
25465 For concrete instances, we can get the parent die from the abstract
25466 instance.
25468 The point here is to flush out the limbo list so that it is empty
25469 and we don't need to stream it for LTO. */
25470 for (node = limbo_die_list; node; node = next_node)
25472 dw_die_ref die = node->die;
25473 next_node = node->next;
25475 if (die->die_parent == NULL)
25477 dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25479 if (origin && origin->die_parent)
25480 add_child_die (origin->die_parent, die);
25481 else if (is_cu_die (die))
25483 else if (seen_error ())
25484 /* It's OK to be confused by errors in the input. */
25485 add_child_die (comp_unit_die (), die);
25486 else
25488 /* In certain situations, the lexical block containing a
25489 nested function can be optimized away, which results
25490 in the nested function die being orphaned. Likewise
25491 with the return type of that nested function. Force
25492 this to be a child of the containing function.
25494 It may happen that even the containing function got fully
25495 inlined and optimized out. In that case we are lost and
25496 assign the empty child. This should not be big issue as
25497 the function is likely unreachable too. */
25498 gcc_assert (node->created_for);
25500 if (DECL_P (node->created_for))
25501 origin = get_context_die (DECL_CONTEXT (node->created_for));
25502 else if (TYPE_P (node->created_for))
25503 origin = scope_die_for (node->created_for, comp_unit_die ());
25504 else
25505 origin = comp_unit_die ();
25507 add_child_die (origin, die);
25512 limbo_die_list = NULL;
25515 /* Reset all state within dwarf2out.c so that we can rerun the compiler
25516 within the same process. For use by toplev::finalize. */
25518 void
25519 dwarf2out_c_finalize (void)
25521 last_var_location_insn = NULL;
25522 cached_next_real_insn = NULL;
25523 used_rtx_array = NULL;
25524 incomplete_types = NULL;
25525 decl_scope_table = NULL;
25526 debug_info_section = NULL;
25527 debug_skeleton_info_section = NULL;
25528 debug_abbrev_section = NULL;
25529 debug_skeleton_abbrev_section = NULL;
25530 debug_aranges_section = NULL;
25531 debug_addr_section = NULL;
25532 debug_macinfo_section = NULL;
25533 debug_line_section = NULL;
25534 debug_skeleton_line_section = NULL;
25535 debug_loc_section = NULL;
25536 debug_pubnames_section = NULL;
25537 debug_pubtypes_section = NULL;
25538 debug_str_section = NULL;
25539 debug_str_dwo_section = NULL;
25540 debug_str_offsets_section = NULL;
25541 debug_ranges_section = NULL;
25542 debug_frame_section = NULL;
25543 fde_vec = NULL;
25544 debug_str_hash = NULL;
25545 skeleton_debug_str_hash = NULL;
25546 dw2_string_counter = 0;
25547 have_multiple_function_sections = false;
25548 text_section_used = false;
25549 cold_text_section_used = false;
25550 cold_text_section = NULL;
25551 current_unit_personality = NULL;
25553 next_die_offset = 0;
25554 single_comp_unit_die = NULL;
25555 comdat_type_list = NULL;
25556 limbo_die_list = NULL;
25557 file_table = NULL;
25558 decl_die_table = NULL;
25559 common_block_die_table = NULL;
25560 decl_loc_table = NULL;
25561 call_arg_locations = NULL;
25562 call_arg_loc_last = NULL;
25563 call_site_count = -1;
25564 tail_call_site_count = -1;
25565 cached_dw_loc_list_table = NULL;
25566 abbrev_die_table = NULL;
25567 abbrev_die_table_allocated = 0;
25568 abbrev_die_table_in_use = 0;
25569 line_info_label_num = 0;
25570 cur_line_info_table = NULL;
25571 text_section_line_info = NULL;
25572 cold_text_section_line_info = NULL;
25573 separate_line_info = NULL;
25574 info_section_emitted = false;
25575 pubname_table = NULL;
25576 pubtype_table = NULL;
25577 macinfo_table = NULL;
25578 ranges_table = NULL;
25579 ranges_table_allocated = 0;
25580 ranges_table_in_use = 0;
25581 ranges_by_label = 0;
25582 ranges_by_label_allocated = 0;
25583 ranges_by_label_in_use = 0;
25584 have_location_lists = false;
25585 loclabel_num = 0;
25586 poc_label_num = 0;
25587 last_emitted_file = NULL;
25588 label_num = 0;
25589 tmpl_value_parm_die_table = NULL;
25590 generic_type_instances = NULL;
25591 frame_pointer_fb_offset = 0;
25592 frame_pointer_fb_offset_valid = false;
25593 base_types.release ();
25594 XDELETEVEC (producer_string);
25595 producer_string = NULL;
25598 #include "gt-dwarf2out.h"